|
| 1 | +//! Custom RPC header types with additional fields |
| 2 | +
|
| 3 | +use alloy_network::primitives::HeaderResponse; |
| 4 | +use alloy_primitives::{BlockHash, U256}; |
| 5 | + |
| 6 | +/// Custom RPC header that extends the standard header with additional fields |
| 7 | +#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)] |
| 8 | +#[serde(rename_all = "camelCase")] |
| 9 | +pub struct CustomRpcHeader<H = alloy_consensus::Header> { |
| 10 | + /// Hash of the block |
| 11 | + pub hash: BlockHash, |
| 12 | + /// Inner consensus header. |
| 13 | + #[serde(flatten)] |
| 14 | + pub inner: H, |
| 15 | + /// Total difficulty |
| 16 | + /// |
| 17 | + /// Note: This field is now effectively deprecated: <https://github.com/ethereum/execution-apis/pull/570> |
| 18 | + #[serde(default, skip_serializing_if = "Option::is_none")] |
| 19 | + pub total_difficulty: Option<U256>, |
| 20 | + /// Integer the size of this block in bytes. |
| 21 | + #[serde(default, skip_serializing_if = "Option::is_none")] |
| 22 | + pub size: Option<U256>, |
| 23 | + /// Millisecond timestamp - custom field for BNB Chain |
| 24 | + #[serde(default, skip_serializing_if = "Option::is_none")] |
| 25 | + pub milli_timestamp: Option<U256>, |
| 26 | +} |
| 27 | + |
| 28 | +impl<H> CustomRpcHeader<H> { |
| 29 | + /// Create a new custom header from consensus header components |
| 30 | + pub const fn new( |
| 31 | + hash: BlockHash, |
| 32 | + inner: H, |
| 33 | + total_difficulty: Option<U256>, |
| 34 | + size: Option<U256>, |
| 35 | + milli_timestamp: Option<U256>, |
| 36 | + ) -> Self { |
| 37 | + Self { hash, inner, total_difficulty, size, milli_timestamp } |
| 38 | + } |
| 39 | + |
| 40 | + /// Create a custom header from a consensus header |
| 41 | + pub fn from_consensus( |
| 42 | + header: alloy_consensus::Header, |
| 43 | + total_difficulty: Option<U256>, |
| 44 | + size: Option<U256>, |
| 45 | + ) -> CustomRpcHeader<alloy_consensus::Header> { |
| 46 | + let hash = header.hash_slow(); |
| 47 | + let milli_timestamp = Some(U256::from(calculate_millisecond_timestamp(&header))); |
| 48 | + |
| 49 | + CustomRpcHeader { hash, inner: header, total_difficulty, size, milli_timestamp } |
| 50 | + } |
| 51 | + |
| 52 | + /// Create a custom header from any block header |
| 53 | + pub fn from_header<T: reth_primitives_traits::BlockHeader>( |
| 54 | + header: T, |
| 55 | + hash: BlockHash, |
| 56 | + total_difficulty: Option<U256>, |
| 57 | + size: Option<U256>, |
| 58 | + ) -> CustomRpcHeader<T> { |
| 59 | + let milli_timestamp = Some(U256::from(calculate_millisecond_timestamp(&header))); |
| 60 | + |
| 61 | + CustomRpcHeader { hash, inner: header, total_difficulty, size, milli_timestamp } |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +impl<H> HeaderResponse for CustomRpcHeader<H> |
| 66 | +where |
| 67 | + H: alloy_consensus::BlockHeader, |
| 68 | +{ |
| 69 | + fn hash(&self) -> BlockHash { |
| 70 | + self.hash |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +impl<H> alloy_consensus::BlockHeader for CustomRpcHeader<H> |
| 75 | +where |
| 76 | + H: alloy_consensus::BlockHeader, |
| 77 | +{ |
| 78 | + fn parent_hash(&self) -> alloy_primitives::B256 { |
| 79 | + self.inner.parent_hash() |
| 80 | + } |
| 81 | + |
| 82 | + fn ommers_hash(&self) -> alloy_primitives::B256 { |
| 83 | + self.inner.ommers_hash() |
| 84 | + } |
| 85 | + |
| 86 | + fn beneficiary(&self) -> alloy_primitives::Address { |
| 87 | + self.inner.beneficiary() |
| 88 | + } |
| 89 | + |
| 90 | + fn state_root(&self) -> alloy_primitives::B256 { |
| 91 | + self.inner.state_root() |
| 92 | + } |
| 93 | + |
| 94 | + fn transactions_root(&self) -> alloy_primitives::B256 { |
| 95 | + self.inner.transactions_root() |
| 96 | + } |
| 97 | + |
| 98 | + fn receipts_root(&self) -> alloy_primitives::B256 { |
| 99 | + self.inner.receipts_root() |
| 100 | + } |
| 101 | + |
| 102 | + fn withdrawals_root(&self) -> Option<alloy_primitives::B256> { |
| 103 | + self.inner.withdrawals_root() |
| 104 | + } |
| 105 | + |
| 106 | + fn logs_bloom(&self) -> alloy_primitives::Bloom { |
| 107 | + self.inner.logs_bloom() |
| 108 | + } |
| 109 | + |
| 110 | + fn difficulty(&self) -> alloy_primitives::U256 { |
| 111 | + self.inner.difficulty() |
| 112 | + } |
| 113 | + |
| 114 | + fn number(&self) -> u64 { |
| 115 | + self.inner.number() |
| 116 | + } |
| 117 | + |
| 118 | + fn gas_limit(&self) -> u64 { |
| 119 | + self.inner.gas_limit() |
| 120 | + } |
| 121 | + |
| 122 | + fn gas_used(&self) -> u64 { |
| 123 | + self.inner.gas_used() |
| 124 | + } |
| 125 | + |
| 126 | + fn timestamp(&self) -> u64 { |
| 127 | + self.inner.timestamp() |
| 128 | + } |
| 129 | + |
| 130 | + fn mix_hash(&self) -> Option<alloy_primitives::B256> { |
| 131 | + self.inner.mix_hash() |
| 132 | + } |
| 133 | + |
| 134 | + fn nonce(&self) -> Option<alloy_primitives::FixedBytes<8>> { |
| 135 | + self.inner.nonce() |
| 136 | + } |
| 137 | + |
| 138 | + fn base_fee_per_gas(&self) -> Option<u64> { |
| 139 | + self.inner.base_fee_per_gas() |
| 140 | + } |
| 141 | + |
| 142 | + fn blob_gas_used(&self) -> Option<u64> { |
| 143 | + self.inner.blob_gas_used() |
| 144 | + } |
| 145 | + |
| 146 | + fn excess_blob_gas(&self) -> Option<u64> { |
| 147 | + self.inner.excess_blob_gas() |
| 148 | + } |
| 149 | + |
| 150 | + fn parent_beacon_block_root(&self) -> Option<alloy_primitives::B256> { |
| 151 | + self.inner.parent_beacon_block_root() |
| 152 | + } |
| 153 | + |
| 154 | + fn requests_hash(&self) -> Option<alloy_primitives::B256> { |
| 155 | + self.inner.requests_hash() |
| 156 | + } |
| 157 | + |
| 158 | + fn extra_data(&self) -> &alloy_primitives::Bytes { |
| 159 | + self.inner.extra_data() |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +// RpcObject is automatically implemented via blanket impl for types that implement Serialize + |
| 164 | +// Deserialize |
| 165 | + |
| 166 | +impl<H> reth_rpc_traits::FromConsensusHeader<H> for CustomRpcHeader<H> |
| 167 | +where |
| 168 | + H: reth_primitives_traits::BlockHeader + Clone, |
| 169 | +{ |
| 170 | + fn from_consensus_header( |
| 171 | + header: reth_primitives_traits::SealedHeader<H>, |
| 172 | + block_size: usize, |
| 173 | + ) -> Self { |
| 174 | + let header_hash = header.hash(); |
| 175 | + let consensus_header = header.into_header(); |
| 176 | + let milli_timestamp = Some(U256::from(calculate_millisecond_timestamp(&consensus_header))); |
| 177 | + |
| 178 | + Self { |
| 179 | + hash: header_hash, |
| 180 | + inner: consensus_header, |
| 181 | + total_difficulty: None, |
| 182 | + size: Some(U256::from(block_size)), |
| 183 | + milli_timestamp, |
| 184 | + } |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +/// Type alias for the standard Ethereum custom header |
| 189 | +pub type EthereumCustomHeader = CustomRpcHeader<alloy_consensus::Header>; |
| 190 | + |
| 191 | +/// Custom header converter that creates `CustomRpcHeader` instances |
| 192 | +#[derive(Debug, Clone)] |
| 193 | +pub struct CustomHeaderConverter; |
| 194 | + |
| 195 | +impl<H> crate::transaction::HeaderConverter<H, CustomRpcHeader<H>> for CustomHeaderConverter |
| 196 | +where |
| 197 | + H: reth_primitives_traits::BlockHeader + Clone, |
| 198 | +{ |
| 199 | + fn convert_header( |
| 200 | + &self, |
| 201 | + header: reth_primitives_traits::SealedHeader<H>, |
| 202 | + block_size: usize, |
| 203 | + td: Option<alloy_primitives::U256>, |
| 204 | + ) -> CustomRpcHeader<H> { |
| 205 | + let header_hash = header.hash(); |
| 206 | + let consensus_header = header.into_header(); |
| 207 | + let milli_timestamp = Some(U256::from(calculate_millisecond_timestamp(&consensus_header))); |
| 208 | + |
| 209 | + CustomRpcHeader { |
| 210 | + hash: header_hash, |
| 211 | + inner: consensus_header, |
| 212 | + total_difficulty: td, |
| 213 | + size: Some(alloy_primitives::U256::from(block_size)), |
| 214 | + milli_timestamp, |
| 215 | + } |
| 216 | + } |
| 217 | +} |
| 218 | + |
| 219 | +/// Calculate millisecond timestamp from header `mix_hash` for any `BlockHeader` type. |
| 220 | +pub fn calculate_millisecond_timestamp<T: reth_primitives_traits::BlockHeader>(header: &T) -> u64 { |
| 221 | + let seconds = header.timestamp(); |
| 222 | + let mix_hash = header.mix_hash(); |
| 223 | + |
| 224 | + let ms_part = if let Some(mix_hash) = mix_hash { |
| 225 | + if mix_hash.is_zero() { |
| 226 | + 0 |
| 227 | + } else { |
| 228 | + let bytes = mix_hash.as_slice(); |
| 229 | + // Convert last 8 bytes to u64 (big-endian), equivalent to Go's |
| 230 | + // uint256.SetBytes32().Uint64() |
| 231 | + let mut result = 0u64; |
| 232 | + for &byte in bytes.iter().skip(24).take(8) { |
| 233 | + result = (result << 8) | u64::from(byte); |
| 234 | + } |
| 235 | + result |
| 236 | + } |
| 237 | + } else { |
| 238 | + 0 |
| 239 | + }; |
| 240 | + |
| 241 | + seconds * 1000 + ms_part |
| 242 | +} |
0 commit comments