From e92daba3ca45eed0b96453abbb0525f122c626c9 Mon Sep 17 00:00:00 2001 From: avilagaston9 Date: Thu, 22 Aug 2024 23:39:14 -0300 Subject: [PATCH 1/7] refactor: BlockSerializable to RpcBlock --- cmd/ef_tests/types.rs | 2 +- crates/core/types/block.rs | 155 ++---------------------- crates/core/types/engine/payload.rs | 2 +- crates/core/types/genesis.rs | 4 +- crates/rpc/Cargo.toml | 1 + crates/rpc/eth/block.rs | 14 ++- crates/rpc/types/block.rs | 176 ++++++++++++++++++++++++++++ crates/rpc/types/mod.rs | 1 + crates/rpc/types/transaction.rs | 2 +- crates/storage/storage.rs | 2 +- justfile | 5 +- 11 files changed, 203 insertions(+), 161 deletions(-) create mode 100644 crates/rpc/types/block.rs diff --git a/cmd/ef_tests/types.rs b/cmd/ef_tests/types.rs index 5e6bf19e4..bbb336337 100644 --- a/cmd/ef_tests/types.rs +++ b/cmd/ef_tests/types.rs @@ -192,7 +192,7 @@ impl From
for BlockHeader { coinbase: val.coinbase, state_root: val.state_root, transactions_root: val.transactions_trie, - receipt_root: val.receipt_trie, + receipts_root: val.receipt_trie, logs_bloom: val.bloom, difficulty: val.difficulty, number: val.number.as_u64(), diff --git a/crates/core/types/block.rs b/crates/core/types/block.rs index c64aa73ac..c6a0ab09d 100644 --- a/crates/core/types/block.rs +++ b/crates/core/types/block.rs @@ -22,8 +22,6 @@ use std::cmp::{max, Ordering}; use super::Transaction; -pub use serializable::BlockSerializable; - pub type BlockNumber = u64; pub type BlockHash = H256; @@ -78,7 +76,7 @@ pub struct BlockHeader { pub coinbase: Address, pub state_root: H256, pub transactions_root: H256, - pub receipt_root: H256, + pub receipts_root: H256, pub logs_bloom: Bloom, pub difficulty: U256, #[serde(with = "crate::serde_utils::u64::hex_str")] @@ -113,7 +111,7 @@ impl RLPEncode for BlockHeader { .encode_field(&self.coinbase) .encode_field(&self.state_root) .encode_field(&self.transactions_root) - .encode_field(&self.receipt_root) + .encode_field(&self.receipts_root) .encode_field(&self.logs_bloom) .encode_field(&self.difficulty) .encode_field(&self.number) @@ -140,7 +138,7 @@ impl RLPDecode for BlockHeader { let (coinbase, decoder) = decoder.decode_field("coinbase")?; let (state_root, decoder) = decoder.decode_field("state_root")?; let (transactions_root, decoder) = decoder.decode_field("transactions_root")?; - let (receipt_root, decoder) = decoder.decode_field("receipt_root")?; + let (receipts_root, decoder) = decoder.decode_field("receipts_root")?; let (logs_bloom, decoder) = decoder.decode_field("logs_bloom")?; let (difficulty, decoder) = decoder.decode_field("difficulty")?; let (number, decoder) = decoder.decode_field("number")?; @@ -164,7 +162,7 @@ impl RLPDecode for BlockHeader { coinbase, state_root, transactions_root, - receipt_root, + receipts_root, logs_bloom, difficulty, number, @@ -430,67 +428,14 @@ fn calc_excess_blob_gas(parent_header: &BlockHeader) -> u64 { } } -#[allow(unused)] -mod serializable { - use super::*; - - #[derive(Debug, Serialize)] - pub struct BlockSerializable { - hash: H256, - #[serde(flatten)] - header: BlockHeader, - #[serde(flatten)] - body: BlockBodyWrapper, - } - - #[derive(Debug, Serialize)] - #[serde(untagged)] - enum BlockBodyWrapper { - Full(BlockBody), - OnlyHashes(OnlyHashesBlockBody), - } - - #[derive(Debug, Serialize)] - struct OnlyHashesBlockBody { - // Only tx hashes - pub transactions: Vec, - pub uncles: Vec, - pub withdrawals: Vec, - } - - impl BlockSerializable { - pub fn from_block( - header: BlockHeader, - body: BlockBody, - full_transactions: bool, - ) -> BlockSerializable { - let body = if full_transactions { - BlockBodyWrapper::Full(body) - } else { - BlockBodyWrapper::OnlyHashes(OnlyHashesBlockBody { - transactions: body.transactions.iter().map(|t| t.compute_hash()).collect(), - uncles: body.ommers, - withdrawals: body.withdrawals.unwrap_or_default(), - }) - }; - let hash = header.compute_block_hash(); - BlockSerializable { hash, header, body } - } - } -} - #[cfg(test)] mod test { use std::str::FromStr; + use super::*; use ethereum_types::H160; use hex_literal::hex; - use serializable::BlockSerializable; - - use crate::types::{EIP1559Transaction, TxKind}; - - use super::*; #[test] fn test_compute_withdrawals_root() { @@ -537,7 +482,7 @@ mod test { "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", ) .unwrap(), - receipt_root: H256::from_str( + receipts_root: H256::from_str( "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", ) .unwrap(), @@ -579,7 +524,7 @@ mod test { "0x578602b2b7e3a3291c3eefca3a08bc13c0d194f9845a39b6f3bcf843d9fed79d", ) .unwrap(), - receipt_root: H256::from_str( + receipts_root: H256::from_str( "0x035d56bac3f47246c5eed0e6642ca40dc262f9144b582f058bc23ded72aa72fa", ) .unwrap(), @@ -606,92 +551,6 @@ mod test { assert!(validate_block_header(&block, &parent_block)) } - #[test] - fn serialize_block() { - let block_header = BlockHeader { - parent_hash: H256::from_str( - "0x1ac1bf1eef97dc6b03daba5af3b89881b7ae4bc1600dc434f450a9ec34d44999", - ) - .unwrap(), - ommers_hash: H256::from_str( - "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347", - ) - .unwrap(), - coinbase: Address::from_str("0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba").unwrap(), - state_root: H256::from_str( - "0x9de6f95cb4ff4ef22a73705d6ba38c4b927c7bca9887ef5d24a734bb863218d9", - ) - .unwrap(), - transactions_root: H256::from_str( - "0x578602b2b7e3a3291c3eefca3a08bc13c0d194f9845a39b6f3bcf843d9fed79d", - ) - .unwrap(), - receipt_root: H256::from_str( - "0x035d56bac3f47246c5eed0e6642ca40dc262f9144b582f058bc23ded72aa72fa", - ) - .unwrap(), - logs_bloom: Bloom::from([0; 256]), - difficulty: U256::zero(), - number: 1, - gas_limit: 0x016345785d8a0000, - gas_used: 0xa8de, - timestamp: 0x03e8, - extra_data: Bytes::new(), - prev_randao: H256::zero(), - nonce: 0x0000000000000000, - base_fee_per_gas: 0x07, - withdrawals_root: Some( - H256::from_str( - "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", - ) - .unwrap(), - ), - blob_gas_used: Some(0x00), - excess_blob_gas: Some(0x00), - parent_beacon_block_root: Some(H256::zero()), - }; - - let tx = EIP1559Transaction { - nonce: 0, - max_fee_per_gas: 78, - max_priority_fee_per_gas: 17, - to: TxKind::Call(Address::from_slice( - &hex::decode("6177843db3138ae69679A54b95cf345ED759450d").unwrap(), - )), - value: 3000000000000000_u64.into(), - data: Bytes::from_static(b"0x1568"), - signature_r: U256::from_str_radix( - "151ccc02146b9b11adf516e6787b59acae3e76544fdcd75e77e67c6b598ce65d", - 16, - ) - .unwrap(), - signature_s: U256::from_str_radix( - "64c5dd5aae2fbb535830ebbdad0234975cd7ece3562013b63ea18cc0df6c97d4", - 16, - ) - .unwrap(), - signature_y_parity: false, - chain_id: 3151908, - gas_limit: 63000, - access_list: vec![( - Address::from_slice( - &hex::decode("6177843db3138ae69679A54b95cf345ED759450d").unwrap(), - ), - vec![], - )], - }; - - let block_body = BlockBody { - transactions: vec![Transaction::EIP1559Transaction(tx)], - ommers: vec![], - withdrawals: Some(vec![]), - }; - - let block = BlockSerializable::from_block(block_header, block_body, true); - let expected_block = r#"{"hash":"0x63d6a2504601fc2db0ccf02a28055eb0cdb40c444ecbceec0f613980421a035e","parentHash":"0x1ac1bf1eef97dc6b03daba5af3b89881b7ae4bc1600dc434f450a9ec34d44999","sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","miner":"0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba","stateRoot":"0x9de6f95cb4ff4ef22a73705d6ba38c4b927c7bca9887ef5d24a734bb863218d9","transactionsRoot":"0x578602b2b7e3a3291c3eefca3a08bc13c0d194f9845a39b6f3bcf843d9fed79d","receiptRoot":"0x035d56bac3f47246c5eed0e6642ca40dc262f9144b582f058bc23ded72aa72fa","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","difficulty":"0x0","number":"0x1","gasLimit":"0x16345785d8a0000","gasUsed":"0xa8de","timestamp":"0x3e8","extraData":"0x","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","nonce":"0x0","baseFeePerGas":"0x7","withdrawalsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","blobGasUsed":"0x0","excessBlobGas":"0x0","parentBeaconBlockRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","transactions":[{"type":"0x2","nonce":"0x0","to":"0x6177843db3138ae69679a54b95cf345ed759450d","gas":"0xf618","value":"0xaa87bee538000","input":"0x307831353638","maxPriorityFeePerGas":"0x11","maxFeePerGas":"0x4e","gasPrice":"0x4e","accessList":[{"address":"0x6177843db3138ae69679a54b95cf345ed759450d","storageKeys":[]}],"chainId":"0x301824","yParity":"0x0","r":"0x151ccc02146b9b11adf516e6787b59acae3e76544fdcd75e77e67c6b598ce65d","s":"0x64c5dd5aae2fbb535830ebbdad0234975cd7ece3562013b63ea18cc0df6c97d4"}],"uncles":[],"withdrawals":[]}"#; - assert_eq!(serde_json::to_string(&block).unwrap(), expected_block) - } - #[test] fn test_compute_transactions_root() { let encoded_transactions = [ diff --git a/crates/core/types/engine/payload.rs b/crates/core/types/engine/payload.rs index 045625ad5..f4d150854 100644 --- a/crates/core/types/engine/payload.rs +++ b/crates/core/types/engine/payload.rs @@ -87,7 +87,7 @@ impl ExecutionPayloadV3 { coinbase: self.fee_recipient, state_root: self.state_root, transactions_root: compute_transactions_root(&body.transactions), - receipt_root: self.receipts_root, + receipts_root: self.receipts_root, logs_bloom: self.logs_bloom, difficulty: 0.into(), number: self.block_number, diff --git a/crates/core/types/genesis.rs b/crates/core/types/genesis.rs index 49471dde6..278f73177 100644 --- a/crates/core/types/genesis.rs +++ b/crates/core/types/genesis.rs @@ -114,7 +114,7 @@ impl Genesis { coinbase: self.coinbase, state_root: self.compute_state_root(), transactions_root: compute_transactions_root(&[]), - receipt_root: compute_receipts_root(&[]), + receipts_root: compute_receipts_root(&[]), logs_bloom: Bloom::zero(), difficulty: self.difficulty, number: 0, @@ -279,7 +279,7 @@ mod tests { .unwrap() ); assert_eq!(header.transactions_root, compute_transactions_root(&[])); - assert_eq!(header.receipt_root, compute_receipts_root(&[])); + assert_eq!(header.receipts_root, compute_receipts_root(&[])); assert_eq!(header.logs_bloom, Bloom::default()); assert_eq!(header.difficulty, U256::from(1)); assert_eq!(header.gas_limit, 25_000_000); diff --git a/crates/rpc/Cargo.toml b/crates/rpc/Cargo.toml index f121fd296..62b55ca7d 100644 --- a/crates/rpc/Cargo.toml +++ b/crates/rpc/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" [dependencies] axum = "0.7.5" +bytes.workspace = true serde = { version = "1.0.203", features = ["derive"] } serde_json = "1.0.117" tokio.workspace = true diff --git a/crates/rpc/eth/block.rs b/crates/rpc/eth/block.rs index 42d09d351..af411e244 100644 --- a/crates/rpc/eth/block.rs +++ b/crates/rpc/eth/block.rs @@ -6,11 +6,13 @@ use serde::{Deserialize, Serialize}; use serde_json::Value; use tracing::info; -use crate::{types::transaction::RpcTransaction, utils::RpcErr}; +use crate::{ + types::{block::RpcBlock, transaction::RpcTransaction}, + utils::RpcErr, +}; use ethereum_rust_core::{ types::{ - AccessListEntry, BlockHash, BlockNumber, BlockSerializable, GenericTransaction, - ReceiptWithTxAndBlockInfo, + AccessListEntry, BlockHash, BlockNumber, GenericTransaction, ReceiptWithTxAndBlockInfo, }, H256, }; @@ -240,7 +242,7 @@ pub fn get_block_by_number( // Block not found _ => return Ok(Value::Null), }; - let block = BlockSerializable::from_block(header, body, request.hydrated); + let block = RpcBlock::from_block(header, body, request.hydrated); serde_json::to_value(&block).map_err(|_| RpcErr::Internal) } @@ -258,8 +260,8 @@ pub fn get_block_by_hash(request: &GetBlockByHashRequest, storage: Store) -> Res // Block not found _ => return Ok(Value::Null), }; - let block = BlockSerializable::from_block(header, body, request.hydrated); - + let block = RpcBlock::from_block(header, body, request.hydrated); + // let block = serde_json::to_value(&block).map_err(|_| RpcErr::Internal) } diff --git a/crates/rpc/types/block.rs b/crates/rpc/types/block.rs new file mode 100644 index 000000000..7de33123c --- /dev/null +++ b/crates/rpc/types/block.rs @@ -0,0 +1,176 @@ +use super::transaction::RpcTransaction; +use ethereum_rust_core::{ + types::{BlockBody, BlockHash, BlockHeader, BlockNumber, Withdrawal}, + H256, +}; + +use serde::Serialize; + +#[allow(unused)] +#[derive(Debug, Serialize)] +pub struct RpcBlock { + hash: H256, + #[serde(flatten)] + header: BlockHeader, + #[serde(flatten)] + body: BlockBodyWrapper, +} + +#[derive(Debug, Serialize)] +#[serde(untagged)] +enum BlockBodyWrapper { + Full(FullBlockBody), + OnlyHashes(OnlyHashesBlockBody), +} + +#[derive(Debug, Serialize)] +struct FullBlockBody { + pub transactions: Vec, + pub uncles: Vec, + pub withdrawals: Vec, +} + +#[derive(Debug, Serialize)] +struct OnlyHashesBlockBody { + // Only tx hashes + pub transactions: Vec, + pub uncles: Vec, + pub withdrawals: Vec, +} + +impl RpcBlock { + pub fn from_block(header: BlockHeader, body: BlockBody, full_transactions: bool) -> RpcBlock { + let hash = header.compute_block_hash(); + let body = if full_transactions { + BlockBodyWrapper::Full(FullBlockBody::from_body(body, header.number, hash)) + } else { + BlockBodyWrapper::OnlyHashes(OnlyHashesBlockBody { + transactions: body.transactions.iter().map(|t| t.compute_hash()).collect(), + uncles: body.ommers, + withdrawals: body.withdrawals.unwrap_or_default(), + }) + }; + RpcBlock { hash, header, body } + } +} + +impl FullBlockBody { + pub fn from_body( + body: BlockBody, + block_number: BlockNumber, + block_hash: BlockHash, + ) -> FullBlockBody { + let mut transactions = Vec::new(); + for (index, tx) in body.transactions.iter().enumerate() { + transactions.push(RpcTransaction::build( + tx.clone(), + block_number, + block_hash, + index, + )); + } + FullBlockBody { + transactions, + uncles: body.ommers, + withdrawals: body.withdrawals.unwrap_or_default(), + } + } +} +#[cfg(test)] +mod test { + + use bytes::Bytes; + use ethereum_rust_core::{ + types::{EIP1559Transaction, Transaction, TxKind}, + Address, Bloom, H256, U256, + }; + use std::str::FromStr; + + use super::*; + + #[test] + fn serialize_block() { + let block_header = BlockHeader { + parent_hash: H256::from_str( + "0x1ac1bf1eef97dc6b03daba5af3b89881b7ae4bc1600dc434f450a9ec34d44999", + ) + .unwrap(), + ommers_hash: H256::from_str( + "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347", + ) + .unwrap(), + coinbase: Address::from_str("0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba").unwrap(), + state_root: H256::from_str( + "0x9de6f95cb4ff4ef22a73705d6ba38c4b927c7bca9887ef5d24a734bb863218d9", + ) + .unwrap(), + transactions_root: H256::from_str( + "0x578602b2b7e3a3291c3eefca3a08bc13c0d194f9845a39b6f3bcf843d9fed79d", + ) + .unwrap(), + receipts_root: H256::from_str( + "0x035d56bac3f47246c5eed0e6642ca40dc262f9144b582f058bc23ded72aa72fa", + ) + .unwrap(), + logs_bloom: Bloom::from([0; 256]), + difficulty: U256::zero(), + number: 1, + gas_limit: 0x016345785d8a0000, + gas_used: 0xa8de, + timestamp: 0x03e8, + extra_data: Bytes::new(), + prev_randao: H256::zero(), + nonce: 0x0000000000000000, + base_fee_per_gas: 0x07, + withdrawals_root: Some( + H256::from_str( + "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421", + ) + .unwrap(), + ), + blob_gas_used: Some(0x00), + excess_blob_gas: Some(0x00), + parent_beacon_block_root: Some(H256::zero()), + }; + + let tx = EIP1559Transaction { + nonce: 0, + max_fee_per_gas: 78, + max_priority_fee_per_gas: 17, + to: TxKind::Call(Address::from_slice( + &hex::decode("6177843db3138ae69679A54b95cf345ED759450d").unwrap(), + )), + value: 3000000000000000_u64.into(), + data: Bytes::from_static(b"0x1568"), + signature_r: U256::from_str_radix( + "151ccc02146b9b11adf516e6787b59acae3e76544fdcd75e77e67c6b598ce65d", + 16, + ) + .unwrap(), + signature_s: U256::from_str_radix( + "64c5dd5aae2fbb535830ebbdad0234975cd7ece3562013b63ea18cc0df6c97d4", + 16, + ) + .unwrap(), + signature_y_parity: false, + chain_id: 3151908, + gas_limit: 63000, + access_list: vec![( + Address::from_slice( + &hex::decode("6177843db3138ae69679A54b95cf345ED759450d").unwrap(), + ), + vec![], + )], + }; + + let block_body = BlockBody { + transactions: vec![Transaction::EIP1559Transaction(tx)], + ommers: vec![], + withdrawals: Some(vec![]), + }; + + let block = RpcBlock::from_block(block_header, block_body, true); + let expected_block = r#"{"hash":"0x63d6a2504601fc2db0ccf02a28055eb0cdb40c444ecbceec0f613980421a035e","parentHash":"0x1ac1bf1eef97dc6b03daba5af3b89881b7ae4bc1600dc434f450a9ec34d44999","sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","miner":"0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba","stateRoot":"0x9de6f95cb4ff4ef22a73705d6ba38c4b927c7bca9887ef5d24a734bb863218d9","transactionsRoot":"0x578602b2b7e3a3291c3eefca3a08bc13c0d194f9845a39b6f3bcf843d9fed79d","receiptsRoot":"0x035d56bac3f47246c5eed0e6642ca40dc262f9144b582f058bc23ded72aa72fa","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","difficulty":"0x0","number":"0x1","gasLimit":"0x16345785d8a0000","gasUsed":"0xa8de","timestamp":"0x3e8","extraData":"0x","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","nonce":"0x0","baseFeePerGas":"0x7","withdrawalsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","blobGasUsed":"0x0","excessBlobGas":"0x0","parentBeaconBlockRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","transactions":[{"type":"0x2","nonce":"0x0","to":"0x6177843db3138ae69679a54b95cf345ed759450d","gas":"0xf618","value":"0xaa87bee538000","input":"0x307831353638","maxPriorityFeePerGas":"0x11","maxFeePerGas":"0x4e","gasPrice":"0x4e","accessList":[{"address":"0x6177843db3138ae69679a54b95cf345ed759450d","storageKeys":[]}],"chainId":"0x301824","yParity":"0x0","r":"0x151ccc02146b9b11adf516e6787b59acae3e76544fdcd75e77e67c6b598ce65d","s":"0x64c5dd5aae2fbb535830ebbdad0234975cd7ece3562013b63ea18cc0df6c97d4","blockNumber":"0x1","blockHash":"0x63d6a2504601fc2db0ccf02a28055eb0cdb40c444ecbceec0f613980421a035e","from":"0x35af8ea983a3ba94c655e19b82b932a30d6b9558","hash":"0xcf250d8f3d17b63a1c73c27ab630df8522c6103ff27e35d209020d2cf351f634","transactionIndex":"0x0"}],"uncles":[],"withdrawals":[]}"#; + assert_eq!(serde_json::to_string(&block).unwrap(), expected_block) + } +} diff --git a/crates/rpc/types/mod.rs b/crates/rpc/types/mod.rs index 37f08066e..54493d0c5 100644 --- a/crates/rpc/types/mod.rs +++ b/crates/rpc/types/mod.rs @@ -1 +1,2 @@ +pub mod block; pub mod transaction; diff --git a/crates/rpc/types/transaction.rs b/crates/rpc/types/transaction.rs index f3fa1c3ba..65e0a4447 100644 --- a/crates/rpc/types/transaction.rs +++ b/crates/rpc/types/transaction.rs @@ -6,7 +6,7 @@ use ethereum_rust_core::{ use serde::Serialize; #[allow(unused)] -#[derive(Serialize)] +#[derive(Debug, Serialize)] #[serde(rename_all = "camelCase")] pub struct RpcTransaction { #[serde(flatten)] diff --git a/crates/storage/storage.rs b/crates/storage/storage.rs index f7bec0513..2892cde3b 100644 --- a/crates/storage/storage.rs +++ b/crates/storage/storage.rs @@ -566,7 +566,7 @@ mod tests { "0x578602b2b7e3a3291c3eefca3a08bc13c0d194f9845a39b6f3bcf843d9fed79d", ) .unwrap(), - receipt_root: H256::from_str( + receipts_root: H256::from_str( "0x035d56bac3f47246c5eed0e6642ca40dc262f9144b582f058bc23ded72aa72fa", ) .unwrap(), diff --git a/justfile b/justfile index 1fa088a42..877358e40 100644 --- a/justfile +++ b/justfile @@ -45,4 +45,7 @@ run-hive test-pattern=test-pattern-default: build_image cd hive && ./hive --sim "ethereum/rpc-compat" --client ethereumrust --sim.limit "{{test-pattern}}" run-hive-debug test-pattern=test-pattern-default: build_image - cd hive && ./hive --sim "ethereum/rpc-compat" --client ethereumrust --sim.limit "{{test-pattern}}" --docker.output + cd hive && ./hive --sim "ethereum/rpc-compat" --client ethereumrust --sim.limit "{{test-pattern}}" --docker.output + +clean-hive-logs: + rm -rf hive/workspace/logs From 61c3c6ed86a4972bf35a4ccfbb0b2bb20c01ea7a Mon Sep 17 00:00:00 2001 From: avilagaston9 Date: Fri, 23 Aug 2024 14:37:10 -0300 Subject: [PATCH 2/7] fix: output format --- crates/core/serde_utils.rs | 20 ++++++++++++++++++++ crates/core/types/block.rs | 3 ++- crates/rpc/types/block.rs | 22 +++++++++++++++++++--- 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/crates/core/serde_utils.rs b/crates/core/serde_utils.rs index 096fd770c..9f6c90ca7 100644 --- a/crates/core/serde_utils.rs +++ b/crates/core/serde_utils.rs @@ -77,6 +77,26 @@ pub mod u64 { serializer.serialize_str(&format!("{:#x}", value)) } } + pub mod hex_str_padding { + use super::*; + + pub fn deserialize<'de, D>(d: D) -> Result + where + D: Deserializer<'de>, + { + let value = String::deserialize(d)?; + let res = u64::from_str_radix(value.trim_start_matches("0x"), 16) + .map_err(|_| D::Error::custom("Failed to deserialize u64 value")); + res + } + + pub fn serialize(value: &u64, serializer: S) -> Result + where + S: Serializer, + { + serializer.serialize_str(&format!("{:#018x}", value)) + } + } pub mod hex_str_opt { use serde::Serialize; diff --git a/crates/core/types/block.rs b/crates/core/types/block.rs index c6a0ab09d..99c280898 100644 --- a/crates/core/types/block.rs +++ b/crates/core/types/block.rs @@ -78,6 +78,7 @@ pub struct BlockHeader { pub transactions_root: H256, pub receipts_root: H256, pub logs_bloom: Bloom, + #[serde(default)] pub difficulty: U256, #[serde(with = "crate::serde_utils::u64::hex_str")] pub number: BlockNumber, @@ -91,7 +92,7 @@ pub struct BlockHeader { pub extra_data: Bytes, #[serde(rename(serialize = "mixHash"))] pub prev_randao: H256, - #[serde(with = "crate::serde_utils::u64::hex_str")] + #[serde(with = "crate::serde_utils::u64::hex_str_padding")] pub nonce: u64, #[serde(with = "crate::serde_utils::u64::hex_str")] pub base_fee_per_gas: u64, diff --git a/crates/rpc/types/block.rs b/crates/rpc/types/block.rs index 7de33123c..d65cf9142 100644 --- a/crates/rpc/types/block.rs +++ b/crates/rpc/types/block.rs @@ -1,6 +1,8 @@ use super::transaction::RpcTransaction; use ethereum_rust_core::{ - types::{BlockBody, BlockHash, BlockHeader, BlockNumber, Withdrawal}, + rlp::encode::RLPEncode, + serde_utils, + types::{Block, BlockBody, BlockHash, BlockHeader, BlockNumber, Withdrawal}, H256, }; @@ -10,6 +12,8 @@ use serde::Serialize; #[derive(Debug, Serialize)] pub struct RpcBlock { hash: H256, + #[serde(with = "serde_utils::u64::hex_str")] + size: u64, #[serde(flatten)] header: BlockHeader, #[serde(flatten)] @@ -41,7 +45,13 @@ struct OnlyHashesBlockBody { impl RpcBlock { pub fn from_block(header: BlockHeader, body: BlockBody, full_transactions: bool) -> RpcBlock { let hash = header.compute_block_hash(); - let body = if full_transactions { + let size = Block { + header: header.clone(), + body: body.clone(), + } + .encode_to_vec() + .len(); + let body_wrapper = if full_transactions { BlockBodyWrapper::Full(FullBlockBody::from_body(body, header.number, hash)) } else { BlockBodyWrapper::OnlyHashes(OnlyHashesBlockBody { @@ -50,7 +60,13 @@ impl RpcBlock { withdrawals: body.withdrawals.unwrap_or_default(), }) }; - RpcBlock { hash, header, body } + + RpcBlock { + hash, + size: size as u64, + header, + body: body_wrapper, + } } } From 1809c0f833220d6dd0b0895c0ade602e4d25c56b Mon Sep 17 00:00:00 2001 From: avilagaston9 Date: Fri, 23 Aug 2024 21:19:45 -0300 Subject: [PATCH 3/7] fix: add block total difficulties --- crates/rpc/eth/block.rs | 22 +++++++++++-- crates/rpc/types/block.rs | 18 ++++++++--- crates/storage/engines/api.rs | 20 ++++++++++++ crates/storage/engines/in_memory.rs | 32 +++++++++++++++++++ crates/storage/engines/libmdbx.rs | 49 ++++++++++++++++++++++++++--- crates/storage/rlp.rs | 2 ++ crates/storage/storage.rs | 40 ++++++++++++++++++++++- 7 files changed, 170 insertions(+), 13 deletions(-) diff --git a/crates/rpc/eth/block.rs b/crates/rpc/eth/block.rs index af411e244..dd538344d 100644 --- a/crates/rpc/eth/block.rs +++ b/crates/rpc/eth/block.rs @@ -14,7 +14,7 @@ use ethereum_rust_core::{ types::{ AccessListEntry, BlockHash, BlockNumber, GenericTransaction, ReceiptWithTxAndBlockInfo, }, - H256, + H256, U256, }; pub struct GetBlockByNumberRequest { @@ -242,7 +242,15 @@ pub fn get_block_by_number( // Block not found _ => return Ok(Value::Null), }; - let block = RpcBlock::from_block(header, body, request.hydrated); + let hash = header.compute_block_hash(); + let total_difficulty = storage.get_block_total_difficulty(hash)?; + let block = RpcBlock::build( + header, + body, + hash, + request.hydrated, + total_difficulty.unwrap_or(U256::zero()), + ); serde_json::to_value(&block).map_err(|_| RpcErr::Internal) } @@ -260,7 +268,15 @@ pub fn get_block_by_hash(request: &GetBlockByHashRequest, storage: Store) -> Res // Block not found _ => return Ok(Value::Null), }; - let block = RpcBlock::from_block(header, body, request.hydrated); + let hash = header.compute_block_hash(); + let total_difficulty = storage.get_block_total_difficulty(hash)?; + let block = RpcBlock::build( + header, + body, + hash, + request.hydrated, + total_difficulty.unwrap_or(U256::zero()), + ); // let block = serde_json::to_value(&block).map_err(|_| RpcErr::Internal) } diff --git a/crates/rpc/types/block.rs b/crates/rpc/types/block.rs index d65cf9142..98682787b 100644 --- a/crates/rpc/types/block.rs +++ b/crates/rpc/types/block.rs @@ -3,17 +3,18 @@ use ethereum_rust_core::{ rlp::encode::RLPEncode, serde_utils, types::{Block, BlockBody, BlockHash, BlockHeader, BlockNumber, Withdrawal}, - H256, + H256, U256, }; use serde::Serialize; -#[allow(unused)] #[derive(Debug, Serialize)] +#[serde(rename_all = "camelCase")] pub struct RpcBlock { hash: H256, #[serde(with = "serde_utils::u64::hex_str")] size: u64, + total_difficulty: U256, #[serde(flatten)] header: BlockHeader, #[serde(flatten)] @@ -43,8 +44,13 @@ struct OnlyHashesBlockBody { } impl RpcBlock { - pub fn from_block(header: BlockHeader, body: BlockBody, full_transactions: bool) -> RpcBlock { - let hash = header.compute_block_hash(); + pub fn build( + header: BlockHeader, + body: BlockBody, + hash: H256, + full_transactions: bool, + total_difficulty: U256, + ) -> RpcBlock { let size = Block { header: header.clone(), body: body.clone(), @@ -63,6 +69,7 @@ impl RpcBlock { RpcBlock { hash, + total_difficulty, size: size as u64, header, body: body_wrapper, @@ -184,8 +191,9 @@ mod test { ommers: vec![], withdrawals: Some(vec![]), }; + let hash = block_header.compute_block_hash(); - let block = RpcBlock::from_block(block_header, block_body, true); + let block = RpcBlock::build(block_header, block_body, hash, true, U256::zero()); let expected_block = r#"{"hash":"0x63d6a2504601fc2db0ccf02a28055eb0cdb40c444ecbceec0f613980421a035e","parentHash":"0x1ac1bf1eef97dc6b03daba5af3b89881b7ae4bc1600dc434f450a9ec34d44999","sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","miner":"0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba","stateRoot":"0x9de6f95cb4ff4ef22a73705d6ba38c4b927c7bca9887ef5d24a734bb863218d9","transactionsRoot":"0x578602b2b7e3a3291c3eefca3a08bc13c0d194f9845a39b6f3bcf843d9fed79d","receiptsRoot":"0x035d56bac3f47246c5eed0e6642ca40dc262f9144b582f058bc23ded72aa72fa","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","difficulty":"0x0","number":"0x1","gasLimit":"0x16345785d8a0000","gasUsed":"0xa8de","timestamp":"0x3e8","extraData":"0x","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","nonce":"0x0","baseFeePerGas":"0x7","withdrawalsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","blobGasUsed":"0x0","excessBlobGas":"0x0","parentBeaconBlockRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","transactions":[{"type":"0x2","nonce":"0x0","to":"0x6177843db3138ae69679a54b95cf345ed759450d","gas":"0xf618","value":"0xaa87bee538000","input":"0x307831353638","maxPriorityFeePerGas":"0x11","maxFeePerGas":"0x4e","gasPrice":"0x4e","accessList":[{"address":"0x6177843db3138ae69679a54b95cf345ed759450d","storageKeys":[]}],"chainId":"0x301824","yParity":"0x0","r":"0x151ccc02146b9b11adf516e6787b59acae3e76544fdcd75e77e67c6b598ce65d","s":"0x64c5dd5aae2fbb535830ebbdad0234975cd7ece3562013b63ea18cc0df6c97d4","blockNumber":"0x1","blockHash":"0x63d6a2504601fc2db0ccf02a28055eb0cdb40c444ecbceec0f613980421a035e","from":"0x35af8ea983a3ba94c655e19b82b932a30d6b9558","hash":"0xcf250d8f3d17b63a1c73c27ab630df8522c6103ff27e35d209020d2cf351f634","transactionIndex":"0x0"}],"uncles":[],"withdrawals":[]}"#; assert_eq!(serde_json::to_string(&block).unwrap(), expected_block) } diff --git a/crates/storage/engines/api.rs b/crates/storage/engines/api.rs index 8a6a4e4a6..ea5a36983 100644 --- a/crates/storage/engines/api.rs +++ b/crates/storage/engines/api.rs @@ -60,6 +60,17 @@ pub trait StoreEngine: Debug + Send { /// Obtain block number fn get_block_number(&self, block_hash: BlockHash) -> Result, StoreError>; + /// Add block total difficulty + fn add_block_total_difficulty( + &mut self, + block_hash: BlockHash, + block_total_difficulty: U256, + ) -> Result<(), StoreError>; + + /// Obtain block total difficulty + fn get_block_total_difficulty(&self, block_hash: BlockHash) + -> Result, StoreError>; + /// Store transaction location (block number and index of the transaction within the block) fn add_transaction_location( &mut self, @@ -217,9 +228,18 @@ pub trait StoreEngine: Debug + Send { // Update latest block number fn update_latest_block_number(&mut self, block_number: BlockNumber) -> Result<(), StoreError>; + // Update latest total difficulty + fn update_latest_total_difficulty( + &mut self, + latest_total_difficulty: U256, + ) -> Result<(), StoreError>; + // Obtain latest block number fn get_latest_block_number(&self) -> Result, StoreError>; + // Obtain latest total difficulty + fn get_latest_total_difficulty(&self) -> Result, StoreError>; + // Update pending block number fn update_pending_block_number(&mut self, block_number: BlockNumber) -> Result<(), StoreError>; diff --git a/crates/storage/engines/in_memory.rs b/crates/storage/engines/in_memory.rs index 3ec46f390..31887daff 100644 --- a/crates/storage/engines/in_memory.rs +++ b/crates/storage/engines/in_memory.rs @@ -13,6 +13,7 @@ pub struct Store { chain_data: ChainData, account_infos: HashMap, block_numbers: HashMap, + block_total_difficulties: HashMap, bodies: HashMap, headers: HashMap, // Maps code hashes to code @@ -30,6 +31,7 @@ struct ChainData { finalized_block_number: Option, safe_block_number: Option, latest_block_number: Option, + latest_total_difficulty: Option, pending_block_number: Option, cancun_time: Option, shanghai_time: Option, @@ -105,6 +107,23 @@ impl StoreEngine for Store { Ok(self.block_numbers.get(&block_hash).copied()) } + fn add_block_total_difficulty( + &mut self, + block_hash: BlockHash, + block_total_difficulty: U256, + ) -> Result<(), StoreError> { + self.block_total_difficulties + .insert(block_hash, block_total_difficulty); + Ok(()) + } + + fn get_block_total_difficulty( + &self, + block_hash: BlockHash, + ) -> Result, StoreError> { + Ok(self.block_total_difficulties.get(&block_hash).copied()) + } + fn add_transaction_location( &mut self, transaction_hash: H256, @@ -254,11 +273,24 @@ impl StoreEngine for Store { self.chain_data.latest_block_number.replace(block_number); Ok(()) } + fn update_latest_total_difficulty( + &mut self, + latest_total_difficulty: U256, + ) -> Result<(), StoreError> { + self.chain_data + .latest_total_difficulty + .replace(latest_total_difficulty); + Ok(()) + } fn get_latest_block_number(&self) -> Result, StoreError> { Ok(self.chain_data.latest_block_number) } + fn get_latest_total_difficulty(&self) -> Result, StoreError> { + Ok(self.chain_data.latest_total_difficulty) + } + fn update_pending_block_number(&mut self, block_number: BlockNumber) -> Result<(), StoreError> { self.chain_data.pending_block_number.replace(block_number); Ok(()) diff --git a/crates/storage/engines/libmdbx.rs b/crates/storage/engines/libmdbx.rs index 9d8a09a14..eeb92ab80 100644 --- a/crates/storage/engines/libmdbx.rs +++ b/crates/storage/engines/libmdbx.rs @@ -2,7 +2,7 @@ use super::api::StoreEngine; use crate::error::StoreError; use crate::rlp::{ AccountCodeHashRLP, AccountCodeRLP, AccountInfoRLP, AddressRLP, BlockBodyRLP, BlockHashRLP, - BlockHeaderRLP, ReceiptRLP, TransactionHashRLP, + BlockHeaderRLP, BlockTotalDifficultyRLP, ReceiptRLP, TransactionHashRLP, }; use anyhow::Result; use bytes::Bytes; @@ -141,6 +141,22 @@ impl StoreEngine for Store { ) -> std::result::Result, StoreError> { self.read::(block_hash.into()) } + fn add_block_total_difficulty( + &mut self, + block_hash: BlockHash, + block_total_difficulty: U256, + ) -> std::result::Result<(), StoreError> { + self.write::(block_hash.into(), block_total_difficulty.into()) + } + + fn get_block_total_difficulty( + &self, + block_hash: BlockHash, + ) -> std::result::Result, StoreError> { + Ok(self + .read::(block_hash.into())? + .map(|b| b.to())) + } fn add_account_code(&mut self, code_hash: H256, code: Bytes) -> Result<(), StoreError> { self.write::(code_hash.into(), code.into()) @@ -342,6 +358,25 @@ impl StoreEngine for Store { } } + fn update_latest_total_difficulty( + &mut self, + latest_total_difficulty: U256, + ) -> std::result::Result<(), StoreError> { + self.write::( + ChainDataIndex::LatestTotalDifficulty, + latest_total_difficulty.encode_to_vec(), + ) + } + + fn get_latest_total_difficulty(&self) -> Result, StoreError> { + match self.read::(ChainDataIndex::LatestTotalDifficulty)? { + None => Ok(None), + Some(ref rlp) => RLPDecode::decode(rlp) + .map(Some) + .map_err(|_| StoreError::DecodeError), + } + } + fn update_pending_block_number(&mut self, block_number: BlockNumber) -> Result<(), StoreError> { self.write::( ChainDataIndex::PendingBlockNumber, @@ -372,6 +407,11 @@ table!( ( BlockNumbers ) BlockHashRLP => BlockNumber ); +table!( + /// Block hash to total difficulties table. + ( BlockTotalDifficulties ) BlockHashRLP => BlockTotalDifficultyRLP +); + table!( /// Block headers table. ( Headers ) BlockNumber => BlockHeaderRLP @@ -475,9 +515,10 @@ pub enum ChainDataIndex { FinalizedBlockNumber = 2, SafeBlockNumber = 3, LatestBlockNumber = 4, - PendingBlockNumber = 5, - CancunTime = 6, - ShanghaiTime = 7, + LatestTotalDifficulty = 5, + PendingBlockNumber = 6, + CancunTime = 7, + ShanghaiTime = 8, } impl Encodable for ChainDataIndex { diff --git a/crates/storage/rlp.rs b/crates/storage/rlp.rs index fde189de7..6e41f24af 100644 --- a/crates/storage/rlp.rs +++ b/crates/storage/rlp.rs @@ -6,6 +6,7 @@ use ethereum_rust_core::{ types::{AccountInfo, BlockBody, BlockHash, BlockHeader, Receipt}, Address, H256, }; +use ethereum_types::U256; #[cfg(feature = "libmdbx")] use libmdbx::orm::{Decodable, Encodable}; @@ -19,6 +20,7 @@ pub type AccountCodeRLP = Rlp; pub type BlockHashRLP = Rlp; pub type BlockHeaderRLP = Rlp; pub type BlockBodyRLP = Rlp; +pub type BlockTotalDifficultyRLP = Rlp; // Receipt types pub type ReceiptRLP = Rlp; diff --git a/crates/storage/storage.rs b/crates/storage/storage.rs index 2892cde3b..176bb153c 100644 --- a/crates/storage/storage.rs +++ b/crates/storage/storage.rs @@ -154,6 +154,29 @@ impl Store { .get_block_number(block_hash) } + pub fn add_block_total_difficulty( + &self, + block_hash: BlockHash, + block_difficulty: U256, + ) -> Result<(), StoreError> { + self.engine + .clone() + .lock() + .unwrap() + .add_block_total_difficulty(block_hash, block_difficulty) + } + + pub fn get_block_total_difficulty( + &self, + block_hash: BlockHash, + ) -> Result, StoreError> { + self.engine + .clone() + .lock() + .unwrap() + .get_block_total_difficulty(block_hash) + } + pub fn add_transaction_location( &self, transaction_hash: H256, @@ -236,11 +259,16 @@ impl Store { // TODO Maybe add both in a single tx? let header = block.header; let number = header.number; + let latest_total_difficulty = self.get_latest_total_difficulty()?; + let block_total_difficulty = + latest_total_difficulty.unwrap_or(U256::zero()) + header.difficulty; let hash = header.compute_block_hash(); self.add_block_body(number, block.body)?; self.add_block_header(number, header)?; self.add_block_number(hash, number)?; - self.update_latest_block_number(number) + self.update_latest_block_number(number)?; + self.add_block_total_difficulty(hash, block_total_difficulty)?; + self.update_latest_total_difficulty(block_total_difficulty) } pub fn add_initial_state(&mut self, genesis: Genesis) -> Result<(), StoreError> { @@ -385,11 +413,21 @@ impl Store { .unwrap() .update_latest_block_number(block_number) } + pub fn update_latest_total_difficulty(&self, block_difficulty: U256) -> Result<(), StoreError> { + self.engine + .lock() + .unwrap() + .update_latest_total_difficulty(block_difficulty) + } pub fn get_latest_block_number(&self) -> Result, StoreError> { self.engine.lock().unwrap().get_latest_block_number() } + pub fn get_latest_total_difficulty(&self) -> Result, StoreError> { + self.engine.lock().unwrap().get_latest_total_difficulty() + } + pub fn update_pending_block_number(&self, block_number: BlockNumber) -> Result<(), StoreError> { self.engine .lock() From e76ff8aadf6aaf7e2d44877edf67d561ce2289cc Mon Sep 17 00:00:00 2001 From: avilagaston9 Date: Mon, 26 Aug 2024 11:45:14 -0300 Subject: [PATCH 4/7] Merge main --- .github/workflows/hive.yaml | 2 +- crates/rpc/Cargo.toml | 1 - crates/rpc/eth/block.rs | 12 ++++-------- 3 files changed, 5 insertions(+), 10 deletions(-) diff --git a/.github/workflows/hive.yaml b/.github/workflows/hive.yaml index 9df6e4f1e..400fdc543 100644 --- a/.github/workflows/hive.yaml +++ b/.github/workflows/hive.yaml @@ -15,7 +15,7 @@ concurrency: env: RUST_VERSION: 1.80.1 - HIVE_TEST_PATTERN: "/eth_chainId|eth_getTransactionByBlockHashAndIndex|eth_getTransactionByBlockNumberAndIndex|eth_getCode|eth_getStorageAt|eth_call|eth_getTransactionByHash" + HIVE_TEST_PATTERN: "/eth_chainId|eth_getTransactionByBlockHashAndIndex|eth_getTransactionByBlockNumberAndIndex|eth_getCode|eth_getStorageAt|eth_call|eth_getTransactionByHash|eth_getBlockByHash" jobs: run-hive: diff --git a/crates/rpc/Cargo.toml b/crates/rpc/Cargo.toml index 302e5e8f6..3b1477951 100644 --- a/crates/rpc/Cargo.toml +++ b/crates/rpc/Cargo.toml @@ -11,7 +11,6 @@ bytes.workspace = true serde = { version = "1.0.203", features = ["derive"] } serde_json = "1.0.117" tokio.workspace = true -bytes.workspace = true tracing.workspace = true tracing-subscriber.workspace = true ethereum_rust-core.workspace = true diff --git a/crates/rpc/eth/block.rs b/crates/rpc/eth/block.rs index 80d71b4a5..1e529d2b7 100644 --- a/crates/rpc/eth/block.rs +++ b/crates/rpc/eth/block.rs @@ -4,16 +4,12 @@ use serde::Deserialize; use serde_json::Value; use tracing::info; -use crate::{ - types::{block::RpcBlock, transaction::RpcTransaction}, - utils::RpcErr, -}; +use crate::{types::block::RpcBlock, utils::RpcErr}; use ethereum_rust_core::{ - types::{ - AccessListEntry, BlockHash, BlockNumber, GenericTransaction, ReceiptWithTxAndBlockInfo, - }, - H256, U256, + types::{BlockHash, BlockNumber, ReceiptWithTxAndBlockInfo}, + U256, }; +use ethereum_rust_storage::{error::StoreError, Store}; pub struct GetBlockByNumberRequest { pub block: BlockIdentifier, From 2e0db9ea667845d753c79a89dea29866cadcef02 Mon Sep 17 00:00:00 2001 From: avilagaston9 Date: Mon, 26 Aug 2024 12:09:07 -0300 Subject: [PATCH 5/7] fix: update expected block in unit tests --- crates/rpc/types/block.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/rpc/types/block.rs b/crates/rpc/types/block.rs index 98682787b..15164cd8d 100644 --- a/crates/rpc/types/block.rs +++ b/crates/rpc/types/block.rs @@ -194,7 +194,7 @@ mod test { let hash = block_header.compute_block_hash(); let block = RpcBlock::build(block_header, block_body, hash, true, U256::zero()); - let expected_block = r#"{"hash":"0x63d6a2504601fc2db0ccf02a28055eb0cdb40c444ecbceec0f613980421a035e","parentHash":"0x1ac1bf1eef97dc6b03daba5af3b89881b7ae4bc1600dc434f450a9ec34d44999","sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","miner":"0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba","stateRoot":"0x9de6f95cb4ff4ef22a73705d6ba38c4b927c7bca9887ef5d24a734bb863218d9","transactionsRoot":"0x578602b2b7e3a3291c3eefca3a08bc13c0d194f9845a39b6f3bcf843d9fed79d","receiptsRoot":"0x035d56bac3f47246c5eed0e6642ca40dc262f9144b582f058bc23ded72aa72fa","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","difficulty":"0x0","number":"0x1","gasLimit":"0x16345785d8a0000","gasUsed":"0xa8de","timestamp":"0x3e8","extraData":"0x","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","nonce":"0x0","baseFeePerGas":"0x7","withdrawalsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","blobGasUsed":"0x0","excessBlobGas":"0x0","parentBeaconBlockRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","transactions":[{"type":"0x2","nonce":"0x0","to":"0x6177843db3138ae69679a54b95cf345ed759450d","gas":"0xf618","value":"0xaa87bee538000","input":"0x307831353638","maxPriorityFeePerGas":"0x11","maxFeePerGas":"0x4e","gasPrice":"0x4e","accessList":[{"address":"0x6177843db3138ae69679a54b95cf345ed759450d","storageKeys":[]}],"chainId":"0x301824","yParity":"0x0","r":"0x151ccc02146b9b11adf516e6787b59acae3e76544fdcd75e77e67c6b598ce65d","s":"0x64c5dd5aae2fbb535830ebbdad0234975cd7ece3562013b63ea18cc0df6c97d4","blockNumber":"0x1","blockHash":"0x63d6a2504601fc2db0ccf02a28055eb0cdb40c444ecbceec0f613980421a035e","from":"0x35af8ea983a3ba94c655e19b82b932a30d6b9558","hash":"0xcf250d8f3d17b63a1c73c27ab630df8522c6103ff27e35d209020d2cf351f634","transactionIndex":"0x0"}],"uncles":[],"withdrawals":[]}"#; + let expected_block = r#"{"hash":"0x63d6a2504601fc2db0ccf02a28055eb0cdb40c444ecbceec0f613980421a035e","size":"0x2d6","totalDifficulty":"0x0","parentHash":"0x1ac1bf1eef97dc6b03daba5af3b89881b7ae4bc1600dc434f450a9ec34d44999","sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","miner":"0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba","stateRoot":"0x9de6f95cb4ff4ef22a73705d6ba38c4b927c7bca9887ef5d24a734bb863218d9","transactionsRoot":"0x578602b2b7e3a3291c3eefca3a08bc13c0d194f9845a39b6f3bcf843d9fed79d","receiptsRoot":"0x035d56bac3f47246c5eed0e6642ca40dc262f9144b582f058bc23ded72aa72fa","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","difficulty":"0x0","number":"0x1","gasLimit":"0x16345785d8a0000","gasUsed":"0xa8de","timestamp":"0x3e8","extraData":"0x","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","nonce":"0x0000000000000000","baseFeePerGas":"0x7","withdrawalsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","blobGasUsed":"0x0","excessBlobGas":"0x0","parentBeaconBlockRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","transactions":[{"type":"0x2","nonce":"0x0","to":"0x6177843db3138ae69679a54b95cf345ed759450d","gas":"0xf618","value":"0xaa87bee538000","input":"0x307831353638","maxPriorityFeePerGas":"0x11","maxFeePerGas":"0x4e","gasPrice":"0x4e","accessList":[{"address":"0x6177843db3138ae69679a54b95cf345ed759450d","storageKeys":[]}],"chainId":"0x301824","yParity":"0x0","v":"0x0","r":"0x151ccc02146b9b11adf516e6787b59acae3e76544fdcd75e77e67c6b598ce65d","s":"0x64c5dd5aae2fbb535830ebbdad0234975cd7ece3562013b63ea18cc0df6c97d4","blockNumber":"0x1","blockHash":"0x63d6a2504601fc2db0ccf02a28055eb0cdb40c444ecbceec0f613980421a035e","from":"0x35af8ea983a3ba94c655e19b82b932a30d6b9558","hash":"0x0b8c8f37731d9493916b06d666c3fd5dee2c3bbda06dfe866160d717e00dda91","transactionIndex":"0x0"}],"uncles":[],"withdrawals":[]}"#; assert_eq!(serde_json::to_string(&block).unwrap(), expected_block) } } From f247e4e5c5e88e037f7d90b3de49430be1bc8531 Mon Sep 17 00:00:00 2001 From: avilagaston9 Date: Mon, 26 Aug 2024 12:52:38 -0300 Subject: [PATCH 6/7] feat: add references to issue #307 --- crates/rpc/eth/block.rs | 3 ++- crates/rpc/types/block.rs | 1 + crates/storage/engines/api.rs | 10 +++++++--- crates/storage/engines/in_memory.rs | 2 ++ crates/storage/engines/libmdbx.rs | 10 ++++++---- crates/storage/rlp.rs | 1 + 6 files changed, 19 insertions(+), 8 deletions(-) diff --git a/crates/rpc/eth/block.rs b/crates/rpc/eth/block.rs index 1e529d2b7..7df7334bc 100644 --- a/crates/rpc/eth/block.rs +++ b/crates/rpc/eth/block.rs @@ -130,6 +130,7 @@ pub fn get_block_by_number( _ => return Ok(Value::Null), }; let hash = header.compute_block_hash(); + // TODO (#307): Remove TotalDifficulty. let total_difficulty = storage.get_block_total_difficulty(hash)?; let block = RpcBlock::build( header, @@ -156,6 +157,7 @@ pub fn get_block_by_hash(request: &GetBlockByHashRequest, storage: Store) -> Res _ => return Ok(Value::Null), }; let hash = header.compute_block_hash(); + // TODO (#307): Remove TotalDifficulty. let total_difficulty = storage.get_block_total_difficulty(hash)?; let block = RpcBlock::build( header, @@ -164,7 +166,6 @@ pub fn get_block_by_hash(request: &GetBlockByHashRequest, storage: Store) -> Res request.hydrated, total_difficulty.unwrap_or(U256::zero()), ); - // let block = serde_json::to_value(&block).map_err(|_| RpcErr::Internal) } diff --git a/crates/rpc/types/block.rs b/crates/rpc/types/block.rs index 15164cd8d..4175d599f 100644 --- a/crates/rpc/types/block.rs +++ b/crates/rpc/types/block.rs @@ -14,6 +14,7 @@ pub struct RpcBlock { hash: H256, #[serde(with = "serde_utils::u64::hex_str")] size: u64, + // TODO (#307): Remove TotalDifficulty. total_difficulty: U256, #[serde(flatten)] header: BlockHeader, diff --git a/crates/storage/engines/api.rs b/crates/storage/engines/api.rs index ea5a36983..62ea88094 100644 --- a/crates/storage/engines/api.rs +++ b/crates/storage/engines/api.rs @@ -60,6 +60,7 @@ pub trait StoreEngine: Debug + Send { /// Obtain block number fn get_block_number(&self, block_hash: BlockHash) -> Result, StoreError>; + // TODO (#307): Remove TotalDifficulty. /// Add block total difficulty fn add_block_total_difficulty( &mut self, @@ -67,6 +68,7 @@ pub trait StoreEngine: Debug + Send { block_total_difficulty: U256, ) -> Result<(), StoreError>; + // TODO (#307): Remove TotalDifficulty. /// Obtain block total difficulty fn get_block_total_difficulty(&self, block_hash: BlockHash) -> Result, StoreError>; @@ -228,15 +230,17 @@ pub trait StoreEngine: Debug + Send { // Update latest block number fn update_latest_block_number(&mut self, block_number: BlockNumber) -> Result<(), StoreError>; + // Obtain latest block number + fn get_latest_block_number(&self) -> Result, StoreError>; + + // TODO (#307): Remove TotalDifficulty. // Update latest total difficulty fn update_latest_total_difficulty( &mut self, latest_total_difficulty: U256, ) -> Result<(), StoreError>; - // Obtain latest block number - fn get_latest_block_number(&self) -> Result, StoreError>; - + // TODO (#307): Remove TotalDifficulty. // Obtain latest total difficulty fn get_latest_total_difficulty(&self) -> Result, StoreError>; diff --git a/crates/storage/engines/in_memory.rs b/crates/storage/engines/in_memory.rs index 31887daff..d5eec383e 100644 --- a/crates/storage/engines/in_memory.rs +++ b/crates/storage/engines/in_memory.rs @@ -13,6 +13,7 @@ pub struct Store { chain_data: ChainData, account_infos: HashMap, block_numbers: HashMap, + // TODO (#307): Remove TotalDifficulty. block_total_difficulties: HashMap, bodies: HashMap, headers: HashMap, @@ -30,6 +31,7 @@ struct ChainData { earliest_block_number: Option, finalized_block_number: Option, safe_block_number: Option, + // TODO (#307): Remove TotalDifficulty. latest_block_number: Option, latest_total_difficulty: Option, pending_block_number: Option, diff --git a/crates/storage/engines/libmdbx.rs b/crates/storage/engines/libmdbx.rs index eeb92ab80..6bcc5715d 100644 --- a/crates/storage/engines/libmdbx.rs +++ b/crates/storage/engines/libmdbx.rs @@ -407,6 +407,7 @@ table!( ( BlockNumbers ) BlockHashRLP => BlockNumber ); +// TODO (#307): Remove TotalDifficulty. table!( /// Block hash to total difficulties table. ( BlockTotalDifficulties ) BlockHashRLP => BlockTotalDifficultyRLP @@ -515,10 +516,11 @@ pub enum ChainDataIndex { FinalizedBlockNumber = 2, SafeBlockNumber = 3, LatestBlockNumber = 4, - LatestTotalDifficulty = 5, - PendingBlockNumber = 6, - CancunTime = 7, - ShanghaiTime = 8, + PendingBlockNumber = 5, + CancunTime = 6, + ShanghaiTime = 7, + // TODO (#307): Remove TotalDifficulty. + LatestTotalDifficulty = 8, } impl Encodable for ChainDataIndex { diff --git a/crates/storage/rlp.rs b/crates/storage/rlp.rs index 6e41f24af..e71f9049c 100644 --- a/crates/storage/rlp.rs +++ b/crates/storage/rlp.rs @@ -20,6 +20,7 @@ pub type AccountCodeRLP = Rlp; pub type BlockHashRLP = Rlp; pub type BlockHeaderRLP = Rlp; pub type BlockBodyRLP = Rlp; +// TODO (#307): Remove TotalDifficulty. pub type BlockTotalDifficultyRLP = Rlp; // Receipt types From 0aa8442e5c65fea151e410b8d2d81816ccf09e5a Mon Sep 17 00:00:00 2001 From: avilagaston9 Date: Mon, 26 Aug 2024 12:56:10 -0300 Subject: [PATCH 7/7] nit changes --- crates/rpc/Cargo.toml | 2 +- crates/storage/engines/in_memory.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/rpc/Cargo.toml b/crates/rpc/Cargo.toml index 3b1477951..7d4d42063 100644 --- a/crates/rpc/Cargo.toml +++ b/crates/rpc/Cargo.toml @@ -7,10 +7,10 @@ edition = "2021" [dependencies] axum = "0.7.5" -bytes.workspace = true serde = { version = "1.0.203", features = ["derive"] } serde_json = "1.0.117" tokio.workspace = true +bytes.workspace = true tracing.workspace = true tracing-subscriber.workspace = true ethereum_rust-core.workspace = true diff --git a/crates/storage/engines/in_memory.rs b/crates/storage/engines/in_memory.rs index d5eec383e..8c6e2ba62 100644 --- a/crates/storage/engines/in_memory.rs +++ b/crates/storage/engines/in_memory.rs @@ -31,8 +31,8 @@ struct ChainData { earliest_block_number: Option, finalized_block_number: Option, safe_block_number: Option, - // TODO (#307): Remove TotalDifficulty. latest_block_number: Option, + // TODO (#307): Remove TotalDifficulty. latest_total_difficulty: Option, pending_block_number: Option, cancun_time: Option,