Skip to content

Commit

Permalink
feat: rlp enc/dec for requests (alloy-rs#728)
Browse files Browse the repository at this point in the history
* feat: rlp enc/dec for requests

We need to encode/decode requests sent to us over P2P,
where they are RLP encoded.

* chore: no alloc decode

* decode as bytes

---------

Co-authored-by: Oliver Nordbjerg <[email protected]>
  • Loading branch information
2 people authored and ben186 committed Jul 27, 2024
1 parent 1dc4277 commit 06df155
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
15 changes: 14 additions & 1 deletion crates/consensus/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use alloy_eips::{
eip7002::WithdrawalRequest,
eip7685::{Decodable7685, Eip7685Error, Encodable7685},
};
use alloy_rlp::{Decodable, Encodable};
use alloy_rlp::{Decodable, Encodable, Header};

/// Ethereum execution layer requests.
///
Expand Down Expand Up @@ -86,3 +86,16 @@ impl Decodable7685 for Request {
})
}
}

impl Encodable for Request {
fn encode(&self, out: &mut dyn alloy_rlp::BufMut) {
self.encoded_7685().encode(out)
}
}

impl Decodable for Request {
fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
let mut data = Header::decode_bytes(buf, false)?;
Ok(Self::decode_7685(&mut data)?)
}
}
14 changes: 14 additions & 0 deletions crates/eips/src/eip7685.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ impl From<alloy_rlp::Error> for Eip7685Error {
}
}

impl From<Eip7685Error> for alloy_rlp::Error {
fn from(err: Eip7685Error) -> Self {
match err {
Eip7685Error::RlpError(err) => err,
Eip7685Error::MissingType => {
alloy_rlp::Error::Custom("eip7685 decoding failed: missing type")
}
Eip7685Error::UnexpectedType(_) => {
alloy_rlp::Error::Custom("eip7685 decoding failed: unexpected type")
}
}
}
}

/// Decoding trait for [EIP-7685] requests. The trait should be implemented for an envelope that
/// wraps each possible request type.
///
Expand Down

0 comments on commit 06df155

Please sign in to comment.