From 955fee57adb937c616003e5f381dbff265bb4e07 Mon Sep 17 00:00:00 2001 From: dylanhuang Date: Wed, 20 Nov 2024 19:38:24 +0800 Subject: [PATCH] fix: rlp issue --- crates/chainspec/src/spec.rs | 2 +- crates/primitives-traits/Cargo.toml | 2 +- crates/primitives-traits/src/blob_sidecar.rs | 44 ++++++++++++++++---- 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/crates/chainspec/src/spec.rs b/crates/chainspec/src/spec.rs index 1c2f8a566..8f88f73ec 100644 --- a/crates/chainspec/src/spec.rs +++ b/crates/chainspec/src/spec.rs @@ -993,7 +993,7 @@ mod tests { use alloy_chains::Chain; use alloy_genesis::{ChainConfig, GenesisAccount}; - use alloy_primitives::{b256, hex}; + use alloy_primitives::hex; use alloy_trie::EMPTY_ROOT_HASH; use reth_ethereum_forks::{ForkCondition, ForkHash, ForkId, Head}; use reth_trie_common::TrieAccount; diff --git a/crates/primitives-traits/Cargo.toml b/crates/primitives-traits/Cargo.toml index 96a96fcce..38728cc2a 100644 --- a/crates/primitives-traits/Cargo.toml +++ b/crates/primitives-traits/Cargo.toml @@ -18,7 +18,7 @@ alloy-consensus = { workspace = true, features = ["serde"] } alloy-eips.workspace = true alloy-genesis.workspace = true alloy-primitives.workspace = true -alloy-rlp.workspace = true +alloy-rlp = { workspace = true, features = ["derive"] } revm-primitives = { workspace = true, features = ["serde"] } diff --git a/crates/primitives-traits/src/blob_sidecar.rs b/crates/primitives-traits/src/blob_sidecar.rs index 26d82ae14..a3bfe1f55 100644 --- a/crates/primitives-traits/src/blob_sidecar.rs +++ b/crates/primitives-traits/src/blob_sidecar.rs @@ -74,7 +74,7 @@ impl BlobSidecars { /// check for errors because we assume that `BlobSidecars` will only ever contain valid /// sidecars pub fn encode_index(&self, out: &mut dyn BufMut, index: usize) { - let header = alloy_rlp::Header { list: true, payload_length: self.0[index].length() }; + let header = alloy_rlp::Header { list: false, payload_length: self.0[index].length() }; header.encode(out); self.0[index].encode(out); } @@ -82,16 +82,24 @@ impl BlobSidecars { impl Encodable for BlobSidecar { fn encode(&self, out: &mut dyn BufMut) { - let list_header_self = alloy_rlp::Header { list: true, payload_length: self.length() }; + let payload_length = self.blob_transaction_sidecar.length() + + self.block_number.length() + + self.block_hash.length() + + self.tx_index.length() + + self.tx_hash.length(); + + let list_header_self = alloy_rlp::Header { list: false, payload_length }; list_header_self.encode(out); let list_header_tx_sidecar = alloy_rlp::Header { - list: true, + list: false, payload_length: self.blob_transaction_sidecar.length(), }; list_header_tx_sidecar.encode(out); - self.blob_transaction_sidecar.encode(out); + self.blob_transaction_sidecar.blobs.encode(out); + self.blob_transaction_sidecar.commitments.encode(out); + self.blob_transaction_sidecar.proofs.encode(out); self.block_number.encode(out); self.block_hash.encode(out); self.tx_index.encode(out); @@ -99,8 +107,24 @@ impl Encodable for BlobSidecar { } fn length(&self) -> usize { - self.blob_transaction_sidecar.length() + - self.blob_transaction_sidecar.length().length() + + let payload_length = self.blob_transaction_sidecar.length() + + self.block_number.length() + + self.block_hash.length() + + self.tx_index.length() + + self.tx_hash.length(); + + let list_header_self = alloy_rlp::Header { list: false, payload_length }; + let list_header_self_length = list_header_self.length(); + + let list_header_tx_sidecar = alloy_rlp::Header { + list: false, + payload_length: self.blob_transaction_sidecar.length(), + }; + let header_length = list_header_tx_sidecar.length(); + + list_header_self_length + + header_length + + self.blob_transaction_sidecar.length() + self.block_number.length() + self.block_hash.length() + self.tx_index.length() + @@ -114,7 +138,11 @@ impl Decodable for BlobSidecar { let _rlp_head_tx_sidecar = alloy_rlp::Header::decode(buf)?; let this = Self { - blob_transaction_sidecar: Decodable::decode(buf)?, + blob_transaction_sidecar: BlobTransactionSidecar { + blobs: Decodable::decode(buf)?, + commitments: Decodable::decode(buf)?, + proofs: Decodable::decode(buf)?, + }, block_number: Decodable::decode(buf)?, block_hash: Decodable::decode(buf)?, tx_index: Decodable::decode(buf)?, @@ -222,7 +250,7 @@ mod tests { fn test_blob_sidecar_rlp() { let blob_sidecar = BlobSidecar { blob_transaction_sidecar: BlobTransactionSidecar { - blobs: vec![], + blobs: vec![Default::default()], commitments: vec![Default::default()], proofs: vec![Default::default()], },