anvil_core/eth/
block.rs

1use super::transaction::TransactionInfo;
2use alloy_consensus::{
3    BlockBody, EMPTY_OMMER_ROOT_HASH, Header, proofs::calculate_transaction_root,
4};
5use foundry_primitives::FoundryReceiptEnvelope;
6
7// Type alias to optionally support impersonated transactions
8type Transaction = crate::eth::transaction::MaybeImpersonatedTransaction;
9
10/// Type alias for Ethereum Block with Anvil's transaction type
11pub type Block = alloy_consensus::Block<Transaction>;
12
13/// Container type that gathers all block data
14#[derive(Clone, Debug)]
15pub struct BlockInfo {
16    pub block: Block,
17    pub transactions: Vec<TransactionInfo>,
18    pub receipts: Vec<FoundryReceiptEnvelope>,
19}
20
21/// Helper function to create a new block with Header and Anvil transactions
22///
23/// Note: if the `impersonate-tx` feature is enabled this will also accept
24/// `MaybeImpersonatedTransaction`.
25pub fn create_block<T>(mut header: Header, transactions: impl IntoIterator<Item = T>) -> Block
26where
27    T: Into<Transaction>,
28{
29    let transactions: Vec<_> = transactions.into_iter().map(Into::into).collect();
30    let transactions_root = calculate_transaction_root(&transactions);
31
32    header.transactions_root = transactions_root;
33    header.ommers_hash = EMPTY_OMMER_ROOT_HASH;
34
35    let body = BlockBody { transactions, ommers: Vec::new(), withdrawals: None };
36    Block::new(header, body)
37}
38
39#[cfg(test)]
40mod tests {
41    use alloy_primitives::{
42        Address, B64, B256, Bloom, U256, b256,
43        hex::{self, FromHex},
44    };
45    use alloy_rlp::Decodable;
46
47    use super::*;
48    use std::str::FromStr;
49
50    #[test]
51    fn header_rlp_roundtrip() {
52        let mut header = Header {
53            parent_hash: Default::default(),
54            ommers_hash: Default::default(),
55            beneficiary: Default::default(),
56            state_root: Default::default(),
57            transactions_root: Default::default(),
58            receipts_root: Default::default(),
59            logs_bloom: Default::default(),
60            difficulty: Default::default(),
61            number: 124u64,
62            gas_limit: Default::default(),
63            gas_used: 1337u64,
64            timestamp: 0,
65            extra_data: Default::default(),
66            mix_hash: Default::default(),
67            nonce: B64::with_last_byte(99),
68            withdrawals_root: Default::default(),
69            blob_gas_used: Default::default(),
70            excess_blob_gas: Default::default(),
71            parent_beacon_block_root: Default::default(),
72            base_fee_per_gas: None,
73            requests_hash: None,
74        };
75
76        let encoded = alloy_rlp::encode(&header);
77        let decoded: Header = Header::decode(&mut encoded.as_ref()).unwrap();
78        assert_eq!(header, decoded);
79
80        header.base_fee_per_gas = Some(12345u64);
81
82        let encoded = alloy_rlp::encode(&header);
83        let decoded: Header = Header::decode(&mut encoded.as_ref()).unwrap();
84        assert_eq!(header, decoded);
85    }
86
87    #[test]
88    fn test_encode_block_header() {
89        use alloy_rlp::Encodable;
90
91        let expected = hex::decode("f901f9a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000940000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008208ae820d0582115c8215b3821a0a827788a00000000000000000000000000000000000000000000000000000000000000000880000000000000000").unwrap();
92        let mut data = vec![];
93        let header = Header {
94            parent_hash: B256::from_str("0000000000000000000000000000000000000000000000000000000000000000").unwrap(),
95            ommers_hash: B256::from_str("0000000000000000000000000000000000000000000000000000000000000000").unwrap(),
96            beneficiary: Address::from_str("0000000000000000000000000000000000000000").unwrap(),
97            state_root: B256::from_str("0000000000000000000000000000000000000000000000000000000000000000").unwrap(),
98            transactions_root: B256::from_str("0000000000000000000000000000000000000000000000000000000000000000").unwrap(),
99            receipts_root: B256::from_str("0000000000000000000000000000000000000000000000000000000000000000").unwrap(),
100            logs_bloom: Bloom::from_hex("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000").unwrap(),
101            difficulty: U256::from(2222),
102            number: 0xd05u64,
103            gas_limit: 0x115cu64,
104            gas_used: 0x15b3u64,
105            timestamp: 0x1a0au64,
106            extra_data: hex::decode("7788").unwrap().into(),
107            mix_hash: B256::from_str("0000000000000000000000000000000000000000000000000000000000000000").unwrap(),
108            withdrawals_root: None,
109            blob_gas_used: None,
110            excess_blob_gas: None,
111            parent_beacon_block_root: None,
112            nonce: B64::ZERO,
113            base_fee_per_gas: None,
114            requests_hash: None,
115        };
116
117        header.encode(&mut data);
118        assert_eq!(hex::encode(&data), hex::encode(expected));
119        assert_eq!(header.length(), data.len());
120    }
121
122    #[test]
123    // Test vector from: https://eips.ethereum.org/EIPS/eip-2481
124    fn test_decode_block_header() {
125        let data = hex::decode("f901f9a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000940000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008208ae820d0582115c8215b3821a0a827788a00000000000000000000000000000000000000000000000000000000000000000880000000000000000").unwrap();
126        let expected = Header {
127            parent_hash: B256::from_str("0000000000000000000000000000000000000000000000000000000000000000").unwrap(),
128            ommers_hash: B256::from_str("0000000000000000000000000000000000000000000000000000000000000000").unwrap(),
129            beneficiary: Address::from_str("0000000000000000000000000000000000000000").unwrap(),
130            state_root: B256::from_str("0000000000000000000000000000000000000000000000000000000000000000").unwrap(),
131            transactions_root: B256::from_str("0000000000000000000000000000000000000000000000000000000000000000").unwrap(),
132            receipts_root: B256::from_str("0000000000000000000000000000000000000000000000000000000000000000").unwrap(),
133            logs_bloom: <[u8; 256]>::from_hex("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000").unwrap().into(),
134            difficulty: U256::from(2222),
135            number: 0xd05u64,
136            gas_limit: 0x115cu64,
137            gas_used: 0x15b3u64,
138            timestamp: 0x1a0au64,
139            extra_data: hex::decode("7788").unwrap().into(),
140            mix_hash: B256::from_str("0000000000000000000000000000000000000000000000000000000000000000").unwrap(),
141            nonce: B64::ZERO,
142            withdrawals_root: None,
143            blob_gas_used: None,
144            excess_blob_gas: None,
145            parent_beacon_block_root: None,
146            base_fee_per_gas: None,
147            requests_hash: None,
148        };
149        let header = Header::decode(&mut data.as_slice()).unwrap();
150        assert_eq!(header, expected);
151    }
152
153    #[test]
154    // Test vector from: https://github.com/ethereum/tests/blob/f47bbef4da376a49c8fc3166f09ab8a6d182f765/BlockchainTests/ValidBlocks/bcEIP1559/baseFee.json#L15-L36
155    fn test_eip1559_block_header_hash() {
156        let expected_hash =
157            b256!("0x6a251c7c3c5dca7b42407a3752ff48f3bbca1fab7f9868371d9918daf1988d1f");
158        let header = Header {
159            parent_hash: B256::from_str("e0a94a7a3c9617401586b1a27025d2d9671332d22d540e0af72b069170380f2a").unwrap(),
160            ommers_hash: B256::from_str("1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347").unwrap(),
161            beneficiary: Address::from_str("ba5e000000000000000000000000000000000000").unwrap(),
162            state_root: B256::from_str("ec3c94b18b8a1cff7d60f8d258ec723312932928626b4c9355eb4ab3568ec7f7").unwrap(),
163            transactions_root: B256::from_str("50f738580ed699f0469702c7ccc63ed2e51bc034be9479b7bff4e68dee84accf").unwrap(),
164            receipts_root: B256::from_str("29b0562f7140574dd0d50dee8a271b22e1a0a7b78fca58f7c60370d8317ba2a9").unwrap(),
165            logs_bloom: Bloom::from_hex("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000").unwrap(),
166            difficulty: U256::from(0x020000),
167            number: 1u64,
168            gas_limit: U256::from(0x016345785d8a0000u128).to::<u64>(),
169            gas_used: U256::from(0x015534).to::<u64>(),
170            timestamp: 0x079e,
171            extra_data: hex::decode("42").unwrap().into(),
172            mix_hash: B256::from_str("0000000000000000000000000000000000000000000000000000000000000000").unwrap(),
173            nonce: B64::ZERO,
174            base_fee_per_gas: Some(875),
175            withdrawals_root: None,
176            blob_gas_used: None,
177            excess_blob_gas: None,
178            parent_beacon_block_root: None,
179            requests_hash: None,
180        };
181        assert_eq!(header.hash_slow(), expected_hash);
182    }
183
184    #[test]
185    // Test vector from network
186    fn block_network_roundtrip() {
187        use alloy_rlp::Encodable;
188
189        let data = hex::decode("f9034df90348a0fbdbd8d2d0ac5f14bd5fa90e547fe6f1d15019c724f8e7b60972d381cd5d9cf8a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d4934794c9577e7945db22e38fc060909f2278c7746b0f9ba05017cfa3b0247e35197215ae8d610265ffebc8edca8ea66d6567eb0adecda867a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000018355bb7b871fffffffffffff808462bd0e1ab9014bf90148a00000000000000000000000000000000000000000000000000000000000000000f85494319fa8f1bc4e53410e92d10d918659b16540e60a945a573efb304d04c1224cd012313e827eca5dce5d94a9c831c5a268031176ebf5f3de5051e8cba0dbfe94c9577e7945db22e38fc060909f2278c7746b0f9b808400000000f8c9b841a6946f2d16f68338cbcbd8b117374ab421128ce422467088456bceba9d70c34106128e6d4564659cf6776c08a4186063c0a05f7cffd695c10cf26a6f301b67f800b8412b782100c18c35102dc0a37ece1a152544f04ad7dc1868d18a9570f744ace60870f822f53d35e89a2ea9709ccbf1f4a25ee5003944faa845d02dde0a41d5704601b841d53caebd6c8a82456e85c2806a9e08381f959a31fb94a77e58f00e38ad97b2e0355b8519ab2122662cbe022f2a4ef7ff16adc0b2d5dcd123181ec79705116db300a063746963616c2062797a616e74696e65206661756c7420746f6c6572616e6365880000000000000000c0c0").unwrap();
190
191        let block = Block::decode(&mut data.as_slice()).unwrap();
192
193        // encode and check that it matches the original data
194        let mut encoded = Vec::new();
195        block.encode(&mut encoded);
196        assert_eq!(data, encoded);
197
198        // check that length of encoding is the same as the output of `length`
199        assert_eq!(block.length(), encoded.len());
200    }
201}