Skip to content

Commit

Permalink
fix: implement proper rlp encoding for dencun block bodies (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
njgheorghita authored and KolbyML committed Mar 15, 2024
1 parent 29ca129 commit 9e5356b
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 20 deletions.
1 change: 0 additions & 1 deletion ethportal-api/src/types/execution/block_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,6 @@ mod tests {
#[case(TX17, 1544975)]
// EIP1559 w/ populated access list
#[case(TX6, 41942)]
// todo blob
fn encode_and_decode_txs(#[case] tx: &str, #[case] expected_nonce: u32) {
let tx_rlp = hex_decode(tx).unwrap();
let tx = rlp::decode(&tx_rlp).expect("error decoding tx");
Expand Down
46 changes: 33 additions & 13 deletions ethportal-api/src/types/execution/receipts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,15 +510,14 @@ mod tests {
use ethereum_types::H160;
use serde_json::json;
use ssz::{Decode, Encode};
use test_log::test;

use crate::utils::bytes::hex_encode;

//
// Tests using custom generated rlp encoded receipts from block 14764013
//

#[test]
#[test_log::test]
fn legacy_receipt() {
let receipt_rlp = hex_decode(RECEIPT_6).unwrap();
let receipt = Receipt::decode(&receipt_rlp).expect("error decoding receipt");
Expand All @@ -530,7 +529,7 @@ mod tests {
assert_eq!(encoded, receipt_rlp);
}

#[test]
#[test_log::test]
fn typed_receipt() {
let receipt_rlp = hex_decode(RECEIPT_0).unwrap();
let receipt = Receipt::decode(&receipt_rlp).expect("error decoding receipt");
Expand All @@ -542,7 +541,7 @@ mod tests {
assert_eq!(encoded, receipt_rlp);
}

#[test]
#[test_log::test]
fn cumulative_gas_used() {
// cumulative gas for last tx in block should match block's gas used
let receipt_rlp = hex_decode(RECEIPT_18).unwrap();
Expand All @@ -551,7 +550,7 @@ mod tests {
assert_eq!(receipt.cumulative_gas_used, U256::from(1314225));
}

#[test]
#[test_log::test]
fn calculate_receipts_root() {
let receipts = Receipts {
receipt_list: vec![
Expand Down Expand Up @@ -582,7 +581,7 @@ mod tests {
);
}

#[test]
#[test_log::test]
fn ssz_encoding_decoding_receipts() {
let receipts = Receipts {
receipt_list: vec![
Expand Down Expand Up @@ -621,7 +620,7 @@ mod tests {
// OpenEthereum Tests
// https://github.com/openethereum/openethereum/blob/main/crates/ethcore/types/src/receipt.rs

#[test]
#[test_log::test]
fn no_state_root() {
let expected = hex_decode("0xf90162a02f697d671e9ae4ee24a43c4b0d7e15f1cb4ba6de1561120d43b9a4e8c4a8a6ee83040caeb9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000f838f794dcf421d093428b096ca501a7cd1a740855a7976fc0a00000000000000000000000000000000000000000000000000000000000000000").unwrap();
let receipt = Receipt::new(
Expand All @@ -647,7 +646,7 @@ mod tests {
assert_eq!(decoded, receipt);
}

#[test]
#[test_log::test]
fn basic_legacy() {
let expected = hex_decode("0xf90162a02f697d671e9ae4ee24a43c4b0d7e15f1cb4ba6de1561120d43b9a4e8c4a8a6ee83040caeb9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000f838f794dcf421d093428b096ca501a7cd1a740855a7976fc0a00000000000000000000000000000000000000000000000000000000000000000").unwrap();
let receipt = Receipt::new(
Expand All @@ -673,7 +672,7 @@ mod tests {
assert_eq!(decoded, receipt);
}

#[test]
#[test_log::test]
fn basic_access_list() {
let expected = hex_decode("0x01f90162a02f697d671e9ae4ee24a43c4b0d7e15f1cb4ba6de1561120d43b9a4e8c4a8a6ee83040caeb9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000f838f794dcf421d093428b096ca501a7cd1a740855a7976fc0a00000000000000000000000000000000000000000000000000000000000000000").unwrap();
let receipt = Receipt::new(
Expand All @@ -699,7 +698,7 @@ mod tests {
assert_eq!(decoded, receipt);
}

#[test]
#[test_log::test]
fn basic_eip1559() {
let expected = hex_decode("0x02f90162a02f697d671e9ae4ee24a43c4b0d7e15f1cb4ba6de1561120d43b9a4e8c4a8a6ee83040caeb9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000f838f794dcf421d093428b096ca501a7cd1a740855a7976fc0a00000000000000000000000000000000000000000000000000000000000000000").unwrap();
let receipt = Receipt::new(
Expand All @@ -725,7 +724,7 @@ mod tests {
assert_eq!(decoded, receipt);
}

#[test]
#[test_log::test]
fn status_code() {
let expected = hex_decode("0xf901428083040caeb9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000f838f794dcf421d093428b096ca501a7cd1a740855a7976fc0a00000000000000000000000000000000000000000000000000000000000000000").unwrap();
let receipt = Receipt::new(
Expand All @@ -746,7 +745,7 @@ mod tests {
assert_eq!(decoded, receipt);
}

#[test]
#[test_log::test]
fn from_json_legacy() {
// 0x147c84ddb366ae572ce5aa4d815e62de3a151133479fbb414e25d32bd7db9aa5
let response = json!({"blockHash": "0x720704f3aa11c53cf344ea069db95cecb81ad7453c8f276b2a1062979611f09c", "blockNumber": "0xe147ed", "contractAddress": null, "cumulativeGasUsed": "0x8d727", "effectiveGasPrice": "0x2aa7599fe2", "from": "0xeb6c4be4b92a52e969f4bf405025d997703d5383", "gasUsed": "0x5208", "logs": [], "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "status": "0x1", "to": "0x4c875e8bd31969f4b753b3ab1611e29f270ba47e", "transactionHash": "0x147c84ddb366ae572ce5aa4d815e62de3a151133479fbb414e25d32bd7db9aa5", "transactionIndex": "0x6", "type": "0x0"});
Expand All @@ -761,7 +760,7 @@ mod tests {
);
}

#[test]
#[test_log::test]
fn from_json_typed() {
//0x163dae461ab32787eaecdad0748c9cf5fe0a22b443bc694efae9b80e319d9559
let response = json!({"blockHash": "0x720704f3aa11c53cf344ea069db95cecb81ad7453c8f276b2a1062979611f09c", "blockNumber": "0xe147ed", "contractAddress": null, "cumulativeGasUsed": "0x2e56f", "effectiveGasPrice": "0x1b05c3919a", "from": "0xdd19b32a084be0a318f11edb3f7034889c03c51f", "gasUsed": "0x2e56f", "logs": [{"address": "0xdac17f958d2ee523a2206206994597c13d831ec7", "topics": ["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", "0x000000000000000000000000dd19b32a084be0a318f11edb3f7034889c03c51f", "0x00000000000000000000000074de5d4fcbf63e00296fd95d33236b9794016631"], "data": "0x00000000000000000000000000000000000000000000000000000000979aedeb", "blockNumber": "0xe147ed", "transactionHash": "0x163dae461ab32787eaecdad0748c9cf5fe0a22b443bc694efae9b80e319d9559", "transactionIndex": "0x0", "blockHash": "0x720704f3aa11c53cf344ea069db95cecb81ad7453c8f276b2a1062979611f09c", "logIndex": "0x0", "removed": false}, {"address": "0xdac17f958d2ee523a2206206994597c13d831ec7", "topics": ["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", "0x00000000000000000000000074de5d4fcbf63e00296fd95d33236b9794016631", "0x00000000000000000000000074c99f3f5331676f6aec2756e1f39b4fc029a83e"], "data": "0x00000000000000000000000000000000000000000000000000000000979aedeb", "blockNumber": "0xe147ed", "transactionHash": "0x163dae461ab32787eaecdad0748c9cf5fe0a22b443bc694efae9b80e319d9559", "transactionIndex": "0x0", "blockHash": "0x720704f3aa11c53cf344ea069db95cecb81ad7453c8f276b2a1062979611f09c", "logIndex": "0x1", "removed": false}, {"address": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", "topics": ["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", "0x00000000000000000000000074c99f3f5331676f6aec2756e1f39b4fc029a83e", "0x0000000000000000000000001111111254fb6c44bac0bed2854e76f90643097d"], "data": "0x00000000000000000000000000000000000000000000000011f8b9803bc57124", "blockNumber": "0xe147ed", "transactionHash": "0x163dae461ab32787eaecdad0748c9cf5fe0a22b443bc694efae9b80e319d9559", "transactionIndex": "0x0", "blockHash": "0x720704f3aa11c53cf344ea069db95cecb81ad7453c8f276b2a1062979611f09c", "logIndex": "0x2", "removed": false}, {"address": "0x74c99f3f5331676f6aec2756e1f39b4fc029a83e", "topics": ["0x1c411e9a96e071241c2f21f7726b17ae89e3cab4c78be50e062b03a9fffbbad1"], "data": "0x0000000000000000000000000000000000000000000000657acd23da825d7df70000000000000000000000000000000000000000000000000000035616e4172a", "blockNumber": "0xe147ed", "transactionHash": "0x163dae461ab32787eaecdad0748c9cf5fe0a22b443bc694efae9b80e319d9559", "transactionIndex": "0x0", "blockHash": "0x720704f3aa11c53cf344ea069db95cecb81ad7453c8f276b2a1062979611f09c", "logIndex": "0x3", "removed": false}, {"address": "0x74c99f3f5331676f6aec2756e1f39b4fc029a83e", "topics": ["0xd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822", "0x0000000000000000000000001111111254fb6c44bac0bed2854e76f90643097d", "0x0000000000000000000000001111111254fb6c44bac0bed2854e76f90643097d"], "data": "0x000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000979aedeb00000000000000000000000000000000000000000000000011f8b9803bc571240000000000000000000000000000000000000000000000000000000000000000", "blockNumber": "0xe147ed", "transactionHash": "0x163dae461ab32787eaecdad0748c9cf5fe0a22b443bc694efae9b80e319d9559", "transactionIndex": "0x0", "blockHash": "0x720704f3aa11c53cf344ea069db95cecb81ad7453c8f276b2a1062979611f09c", "logIndex": "0x4", "removed": false}, {"address": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", "topics": ["0x7fcf532c15f0a6db0bd6d0e038bea71d30d808c7d98cb3bf7268a95bf5081b65", "0x0000000000000000000000001111111254fb6c44bac0bed2854e76f90643097d"], "data": "0x00000000000000000000000000000000000000000000000011f8b9803bc57124", "blockNumber": "0xe147ed", "transactionHash": "0x163dae461ab32787eaecdad0748c9cf5fe0a22b443bc694efae9b80e319d9559", "transactionIndex": "0x0", "blockHash": "0x720704f3aa11c53cf344ea069db95cecb81ad7453c8f276b2a1062979611f09c", "logIndex": "0x5", "removed": false}, {"address": "0x881d40237659c251811cec9c364ef91dc08d300c", "topics": ["0xbeee1e6e7fe307ddcf84b0a16137a4430ad5e2480fc4f4a8e250ab56ccd7630d", "0xbd5c436f8c83379009c1962310b8347e561d1900906d3fe4075b1596f8955f88", "0x000000000000000000000000dd19b32a084be0a318f11edb3f7034889c03c51f"], "data": "0x", "blockNumber": "0xe147ed", "transactionHash": "0x163dae461ab32787eaecdad0748c9cf5fe0a22b443bc694efae9b80e319d9559", "transactionIndex": "0x0", "blockHash": "0x720704f3aa11c53cf344ea069db95cecb81ad7453c8f276b2a1062979611f09c", "logIndex": "0x6", "removed": false}], "logsBloom": "0x00200000000000001000000080000000000000000000010000000000000000000000010000000000000090000001010002000000080008000000000000000000000000000000000000020008000000200000000000400000000004000000400000000000000000000000000000000000000000000000040000000010000000000000010000001100000000000000008000000000000000080020004000100000000000000000000000000080000000000000000000000000000000000000000001000002000000100004000000000000000000000000001000000002000000000024200000000000000000000000000000000000004000000000000000001000", "status": "0x1", "to": "0x881d40237659c251811cec9c364ef91dc08d300c", "transactionHash": "0x163dae461ab32787eaecdad0748c9cf5fe0a22b443bc694efae9b80e319d9559", "transactionIndex": "0x0", "type": "0x2"});
Expand Down Expand Up @@ -800,6 +799,27 @@ mod tests {
assert_eq!(receipts.root().unwrap(), expected_receipts_root);
}

#[rstest::rstest]
// without blob txs
#[case(
"19433902",
"0xe83c57ae3bcb0945878a6880421f617200a17d00656a1f0c681380cfc4a46a09"
)]
// with blob txs
#[case(
"19433903",
"0x6c7e4d6a0eadb934d2ca845f11f6aa81c5fdec53310048e8cd0b3f33d06f46d8"
)]
fn dencun_receipts(#[case] block_number: &str, #[case] expected_root: &str) {
let receipts = std::fs::read_to_string(format!(
"../test_assets/infura_batch/receipts-{block_number}.json"
))
.unwrap();
let receipts: Receipts = serde_json::from_str(&receipts).unwrap();
let expected_receipts_root: H256 = H256::from_str(expected_root).unwrap();
assert_eq!(receipts.root().unwrap(), expected_receipts_root);
}

const EXPECTED_RECEIPTS_ROOT: &str =
"0x168a3827607627e781941dc777737fc4b6beb69a8b139240b881992b35b854ea";
const RECEIPT_0: &str = "0x02f90554018302e56fb9010000200000000000001000000080000000000000000000010000000000000000000000010000000000000090000001010002000000080008000000000000000000000000000000000000020008000000200000000000400000000004000000400000000000000000000000000000000000000000000000040000000010000000000000010000001100000000000000008000000000000000080020004000100000000000000000000000000080000000000000000000000000000000000000000001000002000000100004000000000000000000000000001000000002000000000024200000000000000000000000000000000000004000000000000000001000f90449f89b94dac17f958d2ee523a2206206994597c13d831ec7f863a0ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3efa0000000000000000000000000dd19b32a084be0a318f11edb3f7034889c03c51fa000000000000000000000000074de5d4fcbf63e00296fd95d33236b9794016631a000000000000000000000000000000000000000000000000000000000979aedebf89b94dac17f958d2ee523a2206206994597c13d831ec7f863a0ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3efa000000000000000000000000074de5d4fcbf63e00296fd95d33236b9794016631a000000000000000000000000074c99f3f5331676f6aec2756e1f39b4fc029a83ea000000000000000000000000000000000000000000000000000000000979aedebf89b94c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2f863a0ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3efa000000000000000000000000074c99f3f5331676f6aec2756e1f39b4fc029a83ea00000000000000000000000001111111254fb6c44bac0bed2854e76f90643097da000000000000000000000000000000000000000000000000011f8b9803bc57124f8799474c99f3f5331676f6aec2756e1f39b4fc029a83ee1a01c411e9a96e071241c2f21f7726b17ae89e3cab4c78be50e062b03a9fffbbad1b8400000000000000000000000000000000000000000000000657acd23da825d7df70000000000000000000000000000000000000000000000000000035616e4172af8fc9474c99f3f5331676f6aec2756e1f39b4fc029a83ef863a0d78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822a00000000000000000000000001111111254fb6c44bac0bed2854e76f90643097da00000000000000000000000001111111254fb6c44bac0bed2854e76f90643097db880000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000979aedeb00000000000000000000000000000000000000000000000011f8b9803bc571240000000000000000000000000000000000000000000000000000000000000000f87a94c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2f842a07fcf532c15f0a6db0bd6d0e038bea71d30d808c7d98cb3bf7268a95bf5081b65a00000000000000000000000001111111254fb6c44bac0bed2854e76f90643097da000000000000000000000000000000000000000000000000011f8b9803bc57124f87b94881d40237659c251811cec9c364ef91dc08d300cf863a0beee1e6e7fe307ddcf84b0a16137a4430ad5e2480fc4f4a8e250ab56ccd7630da0bd5c436f8c83379009c1962310b8347e561d1900906d3fe4075b1596f8955f88a0000000000000000000000000dd19b32a084be0a318f11edb3f7034889c03c51f80";
Expand Down
12 changes: 6 additions & 6 deletions ethportal-api/src/types/execution/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,11 @@ pub struct BlobTransaction {
pub value: U256,
pub data: Bytes,
pub access_list: AccessList,
pub max_fee_per_blob_gas: U256,
pub blob_versioned_hashes: Vec<H256>,
pub y_parity: U64,
pub r: U256,
pub s: U256,
pub max_fee_per_blob_gas: U256,
pub blob_versioned_hashes: Vec<H256>,
}

#[derive(Eq, Debug, Clone, PartialEq, Deserialize)]
Expand All @@ -297,12 +297,12 @@ struct BlobTransactionHelper {
#[serde(rename(deserialize = "input"))]
pub data: JsonBytes,
pub access_list: Vec<AccessListItem>,
pub max_fee_per_blob_gas: U256,
pub blob_versioned_hashes: Vec<H256>,
#[serde(rename(deserialize = "v"))]
pub y_parity: U64,
pub r: U256,
pub s: U256,
pub max_fee_per_blob_gas: U256,
pub blob_versioned_hashes: Vec<H256>,
}

#[allow(clippy::from_over_into)]
Expand All @@ -317,14 +317,14 @@ impl Into<BlobTransaction> for BlobTransactionHelper {
to: self.to,
value: self.value,
data: self.data.0,
max_fee_per_blob_gas: self.max_fee_per_blob_gas,
blob_versioned_hashes: self.blob_versioned_hashes,
access_list: AccessList {
list: self.access_list,
},
y_parity: self.y_parity,
r: self.r,
s: self.s,
max_fee_per_blob_gas: self.max_fee_per_blob_gas,
blob_versioned_hashes: self.blob_versioned_hashes,
}
}
}
Expand Down
19 changes: 19 additions & 0 deletions portal-bridge/src/types/full_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,25 @@ mod tests {
block_body.validate_against_header(&header).unwrap();
}

#[rstest::rstest]
// without blob txs
#[case("19433902")]
// with blob txs
#[case("19433903")]
fn full_header_block_body_dencun(#[case] case: &str) {
let body =
std::fs::read_to_string(format!("../test_assets/mainnet/block_{case}_value.json"))
.unwrap();
let body: Value = serde_json::from_str(&body).unwrap();
let full_header = FullHeader::try_from(body["result"].clone()).unwrap();
let header: Header = serde_json::from_value(body["result"].clone()).unwrap();
let block_body = BlockBody::Shanghai(BlockBodyShanghai {
txs: full_header.txs.clone(),
withdrawals: full_header.withdrawals.unwrap(),
});
block_body.validate_against_header(&header).unwrap();
}

#[test_log::test]
fn full_header_batch() {
// this block (15573637) was chosen since it contains all tx types (legacy, access list,
Expand Down
1 change: 1 addition & 0 deletions test_assets/infura_batch/receipts-19433902.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions test_assets/infura_batch/receipts-19433903.json

Large diffs are not rendered by default.

0 comments on commit 9e5356b

Please sign in to comment.