pallet_revive/evm/
block_hash.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//!Types, and traits to integrate pallet-revive with EVM.
19#![warn(missing_docs)]
20
21mod receipt;
22pub use receipt::{AccumulateReceipt, LogsBloom};
23
24mod hash_builder;
25pub use hash_builder::{BuilderPhase, IncrementalHashBuilder, IncrementalHashBuilderIR};
26
27mod block_builder;
28pub use block_builder::{EthereumBlockBuilder, EthereumBlockBuilderIR};
29
30use crate::evm::Block;
31
32use alloc::vec::Vec;
33use alloy_core::primitives::{bytes::BufMut, B256};
34
35use codec::{Decode, Encode};
36use scale_info::TypeInfo;
37use sp_core::{H256, U256};
38
39/// Details needed to reconstruct the receipt info in the RPC
40/// layer without losing accuracy.
41#[derive(Encode, Decode, TypeInfo, Clone, Debug, Default, PartialEq, Eq)]
42pub struct ReceiptGasInfo {
43	/// The amount of gas used for this specific transaction alone.
44	pub gas_used: U256,
45
46	/// The effective gas price for this transaction.
47	pub effective_gas_price: U256,
48}
49
50impl Block {
51	/// Compute the trie root using the `(rlp(index), encoded(item))` pairs.
52	pub fn compute_trie_root(items: &[Vec<u8>]) -> B256 {
53		alloy_consensus::proofs::ordered_trie_root_with_encoder(items, |item, buf| {
54			buf.put_slice(item)
55		})
56	}
57
58	/// Compute the ETH header hash.
59	pub fn header_hash(&self) -> H256 {
60		// Note: Cap the gas limit to u64::MAX.
61		// In practice, it should be impossible to fill a u64::MAX gas limit
62		// of an either Ethereum or Substrate block.
63		let gas_limit = self.gas_limit.try_into().unwrap_or(u64::MAX);
64
65		let alloy_header = alloy_consensus::Header {
66			state_root: self.state_root.0.into(),
67			transactions_root: self.transactions_root.0.into(),
68			receipts_root: self.receipts_root.0.into(),
69
70			parent_hash: self.parent_hash.0.into(),
71			beneficiary: self.miner.0.into(),
72			number: self.number.as_u64(),
73			logs_bloom: self.logs_bloom.0.into(),
74			gas_limit,
75			gas_used: self.gas_used.as_u64(),
76			timestamp: self.timestamp.as_u64(),
77
78			ommers_hash: self.sha_3_uncles.0.into(),
79			extra_data: self.extra_data.clone().0.into(),
80			mix_hash: self.mix_hash.0.into(),
81			nonce: self.nonce.0.into(),
82
83			base_fee_per_gas: Some(self.base_fee_per_gas.as_u64()),
84			withdrawals_root: Some(self.withdrawals_root.0.into()),
85			blob_gas_used: Some(self.blob_gas_used.as_u64()),
86			excess_blob_gas: Some(self.excess_blob_gas.as_u64()),
87			parent_beacon_block_root: self.parent_beacon_block_root.map(|root| root.0.into()),
88			requests_hash: self.requests_hash.map(|hash| hash.0.into()),
89
90			..Default::default()
91		};
92
93		alloy_header.hash_slow().0.into()
94	}
95}