Skip to content

Commit

Permalink
move checks
Browse files Browse the repository at this point in the history
  • Loading branch information
klkvr committed Sep 28, 2024
1 parent 010399e commit f8f88ae
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 34 deletions.
17 changes: 11 additions & 6 deletions crates/optimism/payload/src/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,17 @@ impl PayloadBuilderAttributes for OptimismPayloadBuilderAttributes {
.unwrap_or_default()
.into_iter()
.map(|data| {
TransactionSigned::decode_2718(&mut data.as_ref())
.map_err(|err| match err {
Eip2718Error::RlpError(err) => err,
_ => alloy_rlp::Error::Custom("invalid transaction"),
})
.map(|tx| WithEncoded::new(data, tx))
let mut buf = data.as_ref();
let tx = TransactionSigned::decode_2718(&mut buf).map_err(|err| match err {
Eip2718Error::RlpError(err) => err,
_ => alloy_rlp::Error::Custom("invalid transaction"),
})?;

if !buf.is_empty() {
return Err(alloy_rlp::Error::UnexpectedLength);
}

Ok(WithEncoded::new(data, tx))
})
.collect::<Result<_, _>>()?;

Expand Down
26 changes: 0 additions & 26 deletions crates/primitives/src/transaction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1982,30 +1982,4 @@ mod tests {

assert!(res.is_err());
}

#[test]
fn decode_envelope_fails_on_trailing_bytes_legacy() {
let data = [201, 3, 56, 56, 128, 43, 36, 27, 128, 3, 192];

let result = TransactionSigned::decode_2718(&mut data.as_ref());

assert!(result.is_err());
let Err(Eip2718Error::RlpError(err)) = result else {
panic!("Expected RlpError, got {:?}", result);
};
assert_eq!(err, RlpError::UnexpectedLength);
}

#[test]
fn decode_envelope_fails_on_trailing_bytes_eip2718() {
let data = hex!("02f872018307910d808507204d2cb1827d0094388c818ca8b9251b393131c08a736a67ccb19297880320d04823e2701c80c001a0cf024f4815304df2867a1a74e9d2707b6abda0337d2d54a4438d453f4160f190a07ac0e6b3bc9395b5b9c8b9e6d77204a236577a5b18467b9175c01de4faa208d900");

let result = TransactionSigned::decode_2718(&mut data.as_ref());

assert!(result.is_err());
let Err(Eip2718Error::RlpError(err)) = result else {
panic!("Expected RlpError, got {:?}", result);
};
assert_eq!(err, RlpError::UnexpectedLength);
}
}
12 changes: 10 additions & 2 deletions crates/rpc/rpc-types-compat/src/engine/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,18 @@ pub fn try_payload_v1_to_block(payload: ExecutionPayloadV1) -> Result<Block, Pay
.transactions
.iter()
.map(|tx| {
TransactionSigned::decode_2718(&mut tx.as_ref()).map_err(|err| match err {
let mut buf = tx.as_ref();

let tx = TransactionSigned::decode_2718(&mut buf).map_err(|err| match err {
Eip2718Error::RlpError(err) => err,
_ => alloy_rlp::Error::Custom("invalid transaction"),
})
})?;

if !buf.is_empty() {
return Err(alloy_rlp::Error::UnexpectedLength);
}

Ok(tx)
})
.collect::<Result<Vec<_>, _>>()?;
let transactions_root = proofs::calculate_transaction_root(&transactions);
Expand Down

0 comments on commit f8f88ae

Please sign in to comment.