Skip to content

Commit

Permalink
fix(rpc-types): set Uncle as default for BlockTransactions (#98)
Browse files Browse the repository at this point in the history
* fix(rpc-types): set Uncle as default for BlockTransactions

* fix: use default = uncle

* nit
  • Loading branch information
Evalir authored Jan 5, 2024
1 parent f517488 commit ec44bb0
Showing 1 changed file with 48 additions and 2 deletions.
50 changes: 48 additions & 2 deletions crates/rpc-types/src/eth/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ pub struct Block {
pub total_difficulty: Option<U256>,
/// Uncles' hashes.
pub uncles: Vec<B256>,
/// Transactions.
#[serde(skip_serializing_if = "BlockTransactions::is_uncle")]
/// Block Transactions. In the case of an uncle block, this field is not included in RPC
/// responses, and when deserialized, it will be set to [BlockTransactions::Uncle].
#[serde(
skip_serializing_if = "BlockTransactions::is_uncle",
default = "BlockTransactions::uncle"
)]
pub transactions: BlockTransactions,
/// Integer the size of this block in bytes.
pub size: Option<U256>,
Expand Down Expand Up @@ -1026,6 +1030,48 @@ mod tests {
assert_eq!(block, deserialized);
}

#[test]
fn serde_uncle_block() {
let block = Block {
header: Header {
hash: Some(B256::with_last_byte(1)),
parent_hash: B256::with_last_byte(2),
uncles_hash: B256::with_last_byte(3),
miner: Address::with_last_byte(4),
state_root: B256::with_last_byte(5),
transactions_root: B256::with_last_byte(6),
receipts_root: B256::with_last_byte(7),
withdrawals_root: Some(B256::with_last_byte(8)),
number: Some(U256::from(9)),
gas_used: U256::from(10),
gas_limit: U256::from(11),
extra_data: Bytes::from(vec![1, 2, 3]),
logs_bloom: Bloom::default(),
timestamp: U256::from(12),
difficulty: U256::from(13),
mix_hash: Some(B256::with_last_byte(14)),
nonce: Some(B64::with_last_byte(15)),
base_fee_per_gas: Some(U256::from(20)),
blob_gas_used: None,
excess_blob_gas: None,
parent_beacon_block_root: None,
},
total_difficulty: Some(U256::from(100000)),
uncles: vec![],
transactions: BlockTransactions::Uncle,
size: Some(U256::from(19)),
withdrawals: None,
other: Default::default(),
};
let serialized = serde_json::to_string(&block).unwrap();
assert_eq!(
serialized,
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":[],"size":"0x13"}"#
);
let deserialized: Block = serde_json::from_str(&serialized).unwrap();
assert_eq!(block, deserialized);
}

#[test]
fn serde_block_with_withdrawals_set_as_none() {
let block = Block {
Expand Down

0 comments on commit ec44bb0

Please sign in to comment.