referrerpolicy=no-referrer-when-downgrade

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;
22use pallet_revive_types::runtime_api::*;
23pub use receipt::{AccumulateReceipt, LogsBloom};
24
25mod hash_builder;
26pub use hash_builder::{BuilderPhase, IncrementalHashBuilder, IncrementalHashBuilderIR};
27
28mod block_builder;
29pub use block_builder::{EthereumBlockBuilder, EthereumBlockBuilderIR};
30
31use crate::evm::Block;
32
33use alloc::vec::Vec;
34use alloy_core::primitives::{B256, bytes::BufMut};
35
36use codec::{Decode, Encode};
37use scale_info::TypeInfo;
38use sp_core::{H256, U256};
39
40/// Details needed to reconstruct the receipt info in the RPC
41/// layer without losing accuracy.
42#[derive(Encode, Decode, TypeInfo, Clone, Debug, Default, PartialEq, Eq)]
43pub struct ReceiptGasInfo {
44	/// The amount of gas used for this specific transaction alone.
45	pub gas_used: U256,
46
47	/// The effective gas price for this transaction.
48	pub effective_gas_price: U256,
49}
50
51impl From<ReceiptGasInfo> for ReceiptGasInfoV1 {
52	fn from(value: ReceiptGasInfo) -> Self {
53		Self { gas_used: value.gas_used, effective_gas_price: value.effective_gas_price }
54	}
55}
56
57impl Block {
58	/// Compute the trie root using the `(rlp(index), encoded(item))` pairs.
59	pub fn compute_trie_root(items: &[Vec<u8>]) -> B256 {
60		alloy_consensus::proofs::ordered_trie_root_with_encoder(items, |item, buf| {
61			buf.put_slice(item)
62		})
63	}
64
65	/// Compute the ETH header hash.
66	pub fn header_hash(&self) -> H256 {
67		// Note: Cap the gas limit to u64::MAX.
68		// In practice, it should be impossible to fill a u64::MAX gas limit
69		// of an either Ethereum or Substrate block.
70		let gas_limit = self.gas_limit.try_into().unwrap_or(u64::MAX);
71
72		let alloy_header = alloy_consensus::Header {
73			state_root: self.state_root.0.into(),
74			transactions_root: self.transactions_root.0.into(),
75			receipts_root: self.receipts_root.0.into(),
76
77			parent_hash: self.parent_hash.0.into(),
78			beneficiary: self.miner.0.into(),
79			number: self.number.as_u64(),
80			logs_bloom: self.logs_bloom.0.into(),
81			gas_limit,
82			gas_used: self.gas_used.as_u64(),
83			timestamp: self.timestamp.as_u64(),
84
85			ommers_hash: self.sha_3_uncles.0.into(),
86			extra_data: self.extra_data.clone().0.into(),
87			mix_hash: self.mix_hash.0.into(),
88			nonce: self.nonce.0.into(),
89
90			base_fee_per_gas: Some(self.base_fee_per_gas.as_u64()),
91			withdrawals_root: Some(self.withdrawals_root.0.into()),
92			blob_gas_used: Some(self.blob_gas_used.as_u64()),
93			excess_blob_gas: Some(self.excess_blob_gas.as_u64()),
94			parent_beacon_block_root: self.parent_beacon_block_root.map(|root| root.0.into()),
95			requests_hash: self.requests_hash.map(|hash| hash.0.into()),
96
97			..Default::default()
98		};
99
100		alloy_header.hash_slow().0.into()
101	}
102}