From 254467bbaee839aabffb7ba34c411994f94edbd0 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Mon, 3 Apr 2023 15:15:51 +0200 Subject: [PATCH] fix(rpc): add basefee to header (#2087) --- crates/rpc/rpc-types/src/eth/block.rs | 54 +++++++++++++++------------ 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/crates/rpc/rpc-types/src/eth/block.rs b/crates/rpc/rpc-types/src/eth/block.rs index 552da56a93..d84a8e3dee 100644 --- a/crates/rpc/rpc-types/src/eth/block.rs +++ b/crates/rpc/rpc-types/src/eth/block.rs @@ -75,9 +75,6 @@ pub struct Block { pub transactions: BlockTransactions, /// Integer the size of this block in bytes. pub size: Option, - /// Base Fee for post-EIP1559 blocks. - #[serde(default, skip_serializing_if = "Option::is_none")] - pub base_fee_per_gas: Option, /// Withdrawals in the block #[serde(default)] pub withdrawals: Vec, @@ -164,15 +161,12 @@ impl Block { ) -> Self { let block_length = block.length(); let uncles = block.ommers.into_iter().map(|h| h.hash_slow()).collect(); - let base_fee_per_gas = block.header.base_fee_per_gas; - let header = Header::from_primitive_with_hash(block.header.seal(block_hash)); Self { header, uncles, transactions, - base_fee_per_gas: base_fee_per_gas.map(U256::from), total_difficulty: Some(total_difficulty), size: Some(U256::from(block_length)), withdrawals: block.withdrawals.unwrap_or_default(), @@ -190,7 +184,6 @@ impl Block { uncles: vec![], header: rpc_header, transactions: BlockTransactions::Uncle, - base_fee_per_gas: None, withdrawals: vec![], size, total_difficulty: None, @@ -217,27 +210,30 @@ pub struct Header { pub transactions_root: H256, /// Transactions receipts root hash pub receipts_root: H256, - /// Withdrawals root hash - #[serde(skip_serializing_if = "Option::is_none")] - pub withdrawals_root: Option, - /// Block number - pub number: Option, - /// Gas Used - pub gas_used: U256, - /// Gas Limit - pub gas_limit: U256, - /// Extra data - pub extra_data: Bytes, /// Logs bloom pub logs_bloom: Bloom, - /// Timestamp - pub timestamp: U256, /// Difficulty pub difficulty: U256, + /// Block number + pub number: Option, + /// Gas Limit + pub gas_limit: U256, + /// Gas Used + pub gas_used: U256, + /// Timestamp + pub timestamp: U256, + /// Extra data + pub extra_data: Bytes, /// Mix Hash pub mix_hash: H256, /// Nonce pub nonce: Option, + /// Base fee per unit of gas (if past London) + #[serde(rename = "baseFeePerGas", skip_serializing_if = "Option::is_none")] + pub base_fee_per_gas: Option, + /// Withdrawals root hash added by EIP-4895 and is ignored in legacy headers. + #[serde(skip_serializing_if = "Option::is_none")] + pub withdrawals_root: Option, } // === impl Header === @@ -264,7 +260,7 @@ impl Header { timestamp, mix_hash, nonce, - base_fee_per_gas: _, + base_fee_per_gas, extra_data, withdrawals_root, }, @@ -289,6 +285,7 @@ impl Header { difficulty, mix_hash, nonce: Some(nonce.to_be_bytes().into()), + base_fee_per_gas: base_fee_per_gas.map(U256::from), } } } @@ -354,6 +351,7 @@ impl Serialize for Rich { #[cfg(test)] mod tests { use super::*; + use jsonrpsee_types::SubscriptionResponse; #[test] fn test_full_conversion() { @@ -364,6 +362,15 @@ mod tests { assert_eq!(BlockTransactionsKind::Hashes, full.into()); } + #[test] + fn serde_header() { + let resp = r#"{"jsonrpc":"2.0","method":"eth_subscribe","params":{"subscription":"0x7eef37ff35d471f8825b1c8f67a5d3c0","result":{"hash":"0x7a7ada12e140961a32395059597764416499f4178daf1917193fad7bd2cc6386","parentHash":"0xdedbd831f496e705e7f2ec3c8dcb79051040a360bf1455dbd7eb8ea6ad03b751","sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","miner":"0x0000000000000000000000000000000000000000","stateRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","transactionsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","receiptsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","number":"0x8","gasUsed":"0x0","gasLimit":"0x1c9c380","extraData":"0x","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","timestamp":"0x642aa48f","difficulty":"0x0","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","nonce":"0x0000000000000000"}}}"#; + let _header: SubscriptionResponse<'_, Header> = serde_json::from_str(resp).unwrap(); + + let resp = r#"{"jsonrpc":"2.0","method":"eth_subscription","params":{"subscription":"0x1a14b6bdcf4542fabf71c4abee244e47","result":{"author":"0x000000568b9b5a365eaa767d42e74ed88915c204","difficulty":"0x1","extraData":"0x4e65746865726d696e6420312e392e32322d302d6463373666616366612d32308639ad8ff3d850a261f3b26bc2a55e0f3a718de0dd040a19a4ce37e7b473f2d7481448a1e1fd8fb69260825377c0478393e6055f471a5cf839467ce919a6ad2700","gasLimit":"0x7a1200","gasUsed":"0x0","hash":"0xa4856602944fdfd18c528ef93cc52a681b38d766a7e39c27a47488c8461adcb0","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","miner":"0x0000000000000000000000000000000000000000","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","nonce":"0x0000000000000000","number":"0x434822","parentHash":"0x1a9bdc31fc785f8a95efeeb7ae58f40f6366b8e805f47447a52335c95f4ceb49","receiptsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","size":"0x261","stateRoot":"0xf38c4bf2958e541ec6df148e54ce073dc6b610f8613147ede568cb7b5c2d81ee","totalDifficulty":"0x633ebd","timestamp":"0x604726b0","transactions":[],"transactionsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","uncles":[]}}}"#; + let _header: SubscriptionResponse<'_, Header> = serde_json::from_str(resp).unwrap(); + } + #[test] fn serde_block() { let block = Block { @@ -385,18 +392,19 @@ mod tests { difficulty: U256::from(13), mix_hash: H256::from_low_u64_be(14), nonce: Some(H64::from_low_u64_be(15)), + base_fee_per_gas: Some(U256::from(20)), }, total_difficulty: Some(U256::from(100000)), uncles: vec![H256::from_low_u64_be(17)], transactions: BlockTransactions::Hashes(vec![H256::from_low_u64_be(18)]), size: Some(U256::from(19)), - base_fee_per_gas: Some(U256::from(20)), withdrawals: vec![], }; let serialized = serde_json::to_string(&block).unwrap(); + println!("{}", serialized); assert_eq!( serialized, - r#"{"hash":"0x0000000000000000000000000000000000000000000000000000000000000001","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000002","sha3Uncles":"0x0000000000000000000000000000000000000000000000000000000000000003","miner":"0x0000000000000000000000000000000000000004","stateRoot":"0x0000000000000000000000000000000000000000000000000000000000000005","transactionsRoot":"0x0000000000000000000000000000000000000000000000000000000000000006","receiptsRoot":"0x0000000000000000000000000000000000000000000000000000000000000007","withdrawalsRoot":"0x0000000000000000000000000000000000000000000000000000000000000008","number":"0x9","gasUsed":"0xa","gasLimit":"0xb","extraData":"0x010203","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","timestamp":"0xc","difficulty":"0xd","mixHash":"0x000000000000000000000000000000000000000000000000000000000000000e","nonce":"0x000000000000000f","totalDifficulty":"0x186a0","uncles":["0x0000000000000000000000000000000000000000000000000000000000000011"],"transactions":["0x0000000000000000000000000000000000000000000000000000000000000012"],"size":"0x13","baseFeePerGas":"0x14","withdrawals":[]}"# + r#"{"hash":"0x0000000000000000000000000000000000000000000000000000000000000001","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000002","sha3Uncles":"0x0000000000000000000000000000000000000000000000000000000000000003","miner":"0x0000000000000000000000000000000000000004","stateRoot":"0x0000000000000000000000000000000000000000000000000000000000000005","transactionsRoot":"0x0000000000000000000000000000000000000000000000000000000000000006","receiptsRoot":"0x0000000000000000000000000000000000000000000000000000000000000007","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","difficulty":"0xd","number":"0x9","gasLimit":"0xb","gasUsed":"0xa","timestamp":"0xc","extraData":"0x010203","mixHash":"0x000000000000000000000000000000000000000000000000000000000000000e","nonce":"0x000000000000000f","baseFeePerGas":"0x14","withdrawalsRoot":"0x0000000000000000000000000000000000000000000000000000000000000008","totalDifficulty":"0x186a0","uncles":["0x0000000000000000000000000000000000000000000000000000000000000011"],"transactions":["0x0000000000000000000000000000000000000000000000000000000000000012"],"size":"0x13","withdrawals":[]}"# ); let deserialized: Block = serde_json::from_str(&serialized).unwrap(); assert_eq!(block, deserialized);