|
| 1 | +//! Custom RPC types with Ethereum-compatible hex serialization. |
| 2 | +//! |
| 3 | +//! This module provides custom block and header types that serialize numeric fields |
| 4 | +//! as hex strings (e.g., `"0x1fd"` instead of `509`) to comply with the Ethereum |
| 5 | +//! JSON-RPC specification and ensure compatibility with block explorers like Blockscout. |
| 6 | +//! |
| 7 | +//! # Why Custom Types? |
| 8 | +//! |
| 9 | +//! The standard `alloy_consensus::Header` uses `U64HexOrNumber` for numeric fields, |
| 10 | +//! which serializes as integers. While this is valid for deserializing, it causes |
| 11 | +//! compatibility issues with Elixir-based clients (like Blockscout) that expect |
| 12 | +//! strict Ethereum JSON-RPC format with hex-encoded quantities. |
| 13 | +
|
| 14 | +use alloy_primitives::{Address, Bloom, Bytes, B256, B64, U256}; |
| 15 | +use serde::{Deserialize, Serialize}; |
| 16 | + |
| 17 | +/// RPC Block representation with proper hex serialization. |
| 18 | +/// |
| 19 | +/// All numeric fields are serialized as hex strings following the Ethereum |
| 20 | +/// JSON-RPC "quantity" format (e.g., `"0x1fd"` for 509). |
| 21 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 22 | +#[serde(rename_all = "camelCase")] |
| 23 | +pub struct RpcBlock { |
| 24 | + /// Block hash |
| 25 | + pub hash: B256, |
| 26 | + /// Parent block hash |
| 27 | + pub parent_hash: B256, |
| 28 | + /// Ommers/uncles hash (always empty hash in PoS) |
| 29 | + #[serde(rename = "sha3Uncles")] |
| 30 | + pub ommers_hash: B256, |
| 31 | + /// Coinbase/miner address |
| 32 | + pub miner: Address, |
| 33 | + /// State root |
| 34 | + pub state_root: B256, |
| 35 | + /// Transactions root |
| 36 | + pub transactions_root: B256, |
| 37 | + /// Receipts root |
| 38 | + pub receipts_root: B256, |
| 39 | + /// Logs bloom filter |
| 40 | + pub logs_bloom: Bloom, |
| 41 | + /// Difficulty (always 0 in PoS) |
| 42 | + #[serde(serialize_with = "serialize_u256_quantity")] |
| 43 | + pub difficulty: U256, |
| 44 | + /// Block number |
| 45 | + #[serde(serialize_with = "alloy_serde::quantity::serialize")] |
| 46 | + pub number: u64, |
| 47 | + /// Gas limit |
| 48 | + #[serde(serialize_with = "alloy_serde::quantity::serialize")] |
| 49 | + pub gas_limit: u64, |
| 50 | + /// Gas used |
| 51 | + #[serde(serialize_with = "alloy_serde::quantity::serialize")] |
| 52 | + pub gas_used: u64, |
| 53 | + /// Block timestamp |
| 54 | + #[serde(serialize_with = "alloy_serde::quantity::serialize")] |
| 55 | + pub timestamp: u64, |
| 56 | + /// Extra data |
| 57 | + pub extra_data: Bytes, |
| 58 | + /// Mix hash (used in PoW, random value in PoS) |
| 59 | + pub mix_hash: B256, |
| 60 | + /// Nonce (always zero bytes in PoS) |
| 61 | + pub nonce: B64, |
| 62 | + /// Total difficulty (sum of all block difficulties) |
| 63 | + #[serde(serialize_with = "serialize_u256_quantity")] |
| 64 | + pub total_difficulty: U256, |
| 65 | + /// Base fee per gas (EIP-1559) |
| 66 | + #[serde( |
| 67 | + default, |
| 68 | + skip_serializing_if = "Option::is_none", |
| 69 | + serialize_with = "alloy_serde::quantity::opt::serialize" |
| 70 | + )] |
| 71 | + pub base_fee_per_gas: Option<u64>, |
| 72 | + /// Block size in bytes (optional) |
| 73 | + #[serde( |
| 74 | + default, |
| 75 | + skip_serializing_if = "Option::is_none", |
| 76 | + serialize_with = "alloy_serde::quantity::opt::serialize" |
| 77 | + )] |
| 78 | + pub size: Option<u64>, |
| 79 | + /// Withdrawals root (EIP-4895) |
| 80 | + #[serde(default, skip_serializing_if = "Option::is_none")] |
| 81 | + pub withdrawals_root: Option<B256>, |
| 82 | + /// Blob gas used (EIP-4844) |
| 83 | + #[serde( |
| 84 | + default, |
| 85 | + skip_serializing_if = "Option::is_none", |
| 86 | + serialize_with = "alloy_serde::quantity::opt::serialize" |
| 87 | + )] |
| 88 | + pub blob_gas_used: Option<u64>, |
| 89 | + /// Excess blob gas (EIP-4844) |
| 90 | + #[serde( |
| 91 | + default, |
| 92 | + skip_serializing_if = "Option::is_none", |
| 93 | + serialize_with = "alloy_serde::quantity::opt::serialize" |
| 94 | + )] |
| 95 | + pub excess_blob_gas: Option<u64>, |
| 96 | + /// Parent beacon block root (EIP-4788) |
| 97 | + #[serde(default, skip_serializing_if = "Option::is_none")] |
| 98 | + pub parent_beacon_block_root: Option<B256>, |
| 99 | + /// Block transactions (hashes or full transactions) |
| 100 | + pub transactions: Vec<B256>, |
| 101 | + /// Uncle block hashes (always empty in PoS) |
| 102 | + pub uncles: Vec<B256>, |
| 103 | + /// Withdrawals (EIP-4895) |
| 104 | + #[serde(default, skip_serializing_if = "Option::is_none")] |
| 105 | + pub withdrawals: Option<Vec<()>>, |
| 106 | +} |
| 107 | + |
| 108 | +/// Serialize U256 as a hex quantity string. |
| 109 | +/// |
| 110 | +/// This follows the Ethereum JSON-RPC "quantity" format: |
| 111 | +/// - Values are encoded as hex with "0x" prefix |
| 112 | +/// - Leading zeros are removed (except for 0 which is "0x0") |
| 113 | +fn serialize_u256_quantity<S>(value: &U256, serializer: S) -> Result<S::Ok, S::Error> |
| 114 | +where |
| 115 | + S: serde::Serializer, |
| 116 | +{ |
| 117 | + // Use alloy_primitives built-in serialization which already formats as hex |
| 118 | + value.serialize(serializer) |
| 119 | +} |
| 120 | + |
| 121 | +impl RpcBlock { |
| 122 | + /// Create a new RpcBlock from storage block data. |
| 123 | + /// |
| 124 | + /// This is the primary constructor for converting internal storage format |
| 125 | + /// to the RPC-compatible format with proper hex serialization. |
| 126 | + pub fn from_storage(storage_block: cipherbft_storage::blocks::Block) -> Self { |
| 127 | + Self { |
| 128 | + hash: B256::from(storage_block.hash), |
| 129 | + parent_hash: B256::from(storage_block.parent_hash), |
| 130 | + ommers_hash: B256::from(storage_block.ommers_hash), |
| 131 | + miner: Address::from(storage_block.beneficiary), |
| 132 | + state_root: B256::from(storage_block.state_root), |
| 133 | + transactions_root: B256::from(storage_block.transactions_root), |
| 134 | + receipts_root: B256::from(storage_block.receipts_root), |
| 135 | + logs_bloom: Bloom::from_slice(&storage_block.logs_bloom), |
| 136 | + difficulty: U256::from_be_bytes(storage_block.difficulty), |
| 137 | + number: storage_block.number, |
| 138 | + gas_limit: storage_block.gas_limit, |
| 139 | + gas_used: storage_block.gas_used, |
| 140 | + timestamp: storage_block.timestamp, |
| 141 | + extra_data: Bytes::from(storage_block.extra_data), |
| 142 | + mix_hash: B256::from(storage_block.mix_hash), |
| 143 | + nonce: B64::from(storage_block.nonce), |
| 144 | + total_difficulty: U256::from_be_bytes(storage_block.total_difficulty), |
| 145 | + base_fee_per_gas: storage_block.base_fee_per_gas, |
| 146 | + size: None, |
| 147 | + withdrawals_root: None, |
| 148 | + blob_gas_used: None, |
| 149 | + excess_blob_gas: None, |
| 150 | + parent_beacon_block_root: None, |
| 151 | + transactions: storage_block |
| 152 | + .transaction_hashes |
| 153 | + .iter() |
| 154 | + .map(|h| B256::from(*h)) |
| 155 | + .collect(), |
| 156 | + uncles: Vec::new(), |
| 157 | + withdrawals: None, |
| 158 | + } |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +#[cfg(test)] |
| 163 | +mod tests { |
| 164 | + use super::*; |
| 165 | + |
| 166 | + #[test] |
| 167 | + fn test_block_serializes_numbers_as_hex() { |
| 168 | + let block = RpcBlock { |
| 169 | + hash: B256::ZERO, |
| 170 | + parent_hash: B256::ZERO, |
| 171 | + ommers_hash: B256::ZERO, |
| 172 | + miner: Address::ZERO, |
| 173 | + state_root: B256::ZERO, |
| 174 | + transactions_root: B256::ZERO, |
| 175 | + receipts_root: B256::ZERO, |
| 176 | + logs_bloom: Bloom::ZERO, |
| 177 | + difficulty: U256::ZERO, |
| 178 | + number: 509, |
| 179 | + gas_limit: 30_000_000, |
| 180 | + gas_used: 21000, |
| 181 | + timestamp: 1706163600, |
| 182 | + extra_data: Bytes::new(), |
| 183 | + mix_hash: B256::ZERO, |
| 184 | + nonce: B64::ZERO, |
| 185 | + total_difficulty: U256::ZERO, |
| 186 | + base_fee_per_gas: Some(1_000_000_000), |
| 187 | + size: None, |
| 188 | + withdrawals_root: None, |
| 189 | + blob_gas_used: None, |
| 190 | + excess_blob_gas: None, |
| 191 | + parent_beacon_block_root: None, |
| 192 | + transactions: Vec::new(), |
| 193 | + uncles: Vec::new(), |
| 194 | + withdrawals: None, |
| 195 | + }; |
| 196 | + |
| 197 | + let json = serde_json::to_string(&block).unwrap(); |
| 198 | + |
| 199 | + // Verify numeric fields are hex-encoded |
| 200 | + assert!( |
| 201 | + json.contains("\"number\":\"0x1fd\""), |
| 202 | + "number should be hex: {}", |
| 203 | + json |
| 204 | + ); |
| 205 | + assert!( |
| 206 | + json.contains("\"gasLimit\":\"0x1c9c380\""), |
| 207 | + "gasLimit should be hex: {}", |
| 208 | + json |
| 209 | + ); |
| 210 | + assert!( |
| 211 | + json.contains("\"gasUsed\":\"0x5208\""), |
| 212 | + "gasUsed should be hex: {}", |
| 213 | + json |
| 214 | + ); |
| 215 | + // 1706163600 = 0x65b1fd90 |
| 216 | + assert!( |
| 217 | + json.contains("\"timestamp\":\"0x65b1fd90\""), |
| 218 | + "timestamp should be hex: {}", |
| 219 | + json |
| 220 | + ); |
| 221 | + assert!( |
| 222 | + json.contains("\"baseFeePerGas\":\"0x3b9aca00\""), |
| 223 | + "baseFeePerGas should be hex: {}", |
| 224 | + json |
| 225 | + ); |
| 226 | + |
| 227 | + // Verify hash fields remain as full hex strings with 0x prefix |
| 228 | + assert!( |
| 229 | + json.contains( |
| 230 | + "\"hash\":\"0x0000000000000000000000000000000000000000000000000000000000000000\"" |
| 231 | + ), |
| 232 | + "hash should be full hex: {}", |
| 233 | + json |
| 234 | + ); |
| 235 | + } |
| 236 | +} |
0 commit comments