Skip to content

Commit

Permalink
chore: add From<Header> to reth rpc type
Browse files Browse the repository at this point in the history
  • Loading branch information
carver committed Oct 3, 2023
1 parent 813758a commit 601c754
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ethportal-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ rand = "0.8.5"
reth-rpc-types = { version = "0.1.0-alpha.6", git = "https://github.com/paradigmxyz/reth.git"}
rlp = "0.5.0"
rlp-derive = "0.1.0"
ruint = { version = "1.9.0", features = ["primitive-types"] }
serde = { version = "1.0.150", features = ["derive"] }
serde_json = "1.0.89"
serde-this-or-that = "0.4.2"
Expand Down
73 changes: 73 additions & 0 deletions ethportal-api/src/types/execution/header.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use ethereum_types::{Bloom, H160, H256, H64, U256};
use reth_rpc_types::Header as RpcHeader;
use rlp::{Decodable, DecoderError, Encodable, Rlp, RlpStream};
use ruint::Uint;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use ssz::{Encode, SszDecoderBuilder, SszEncoder};
use ssz_derive::{Decode, Encode};
Expand Down Expand Up @@ -195,6 +197,77 @@ impl PartialEq for Header {
}
}

/// Convert the standard header into a reth-style header type for RPC.
///
/// This allows us to easily prepare a header for an RPC response.
/// RpcHeader is a field in reth's `Block` RPC type used in eth_getBlockByHash, for example.
impl From<Header> for RpcHeader {
fn from(header: Header) -> Self {
let hash = Some(header.hash().to_fixed_bytes().into());
let Header {
parent_hash,
uncles_hash,
author,
state_root,
transactions_root,
receipts_root,
logs_bloom,
difficulty,
number,
gas_limit,
gas_used,
timestamp,
extra_data,
mix_hash,
nonce: _,
base_fee_per_gas,
withdrawals_root,
} = header;

Self {
parent_hash: parent_hash.to_fixed_bytes().into(),
uncles_hash: uncles_hash.to_fixed_bytes().into(),
miner: author.to_fixed_bytes().into(),
state_root: state_root.to_fixed_bytes().into(),
transactions_root: transactions_root.to_fixed_bytes().into(),
receipts_root: receipts_root.to_fixed_bytes().into(),
logs_bloom: logs_bloom.to_fixed_bytes().into(),
difficulty: u256_to_uint256(difficulty),
number: Some(u64_to_uint256(number)),
gas_limit: u256_to_uint256(gas_limit),
gas_used: u256_to_uint256(gas_used),
timestamp: u64_to_uint256(timestamp),
extra_data: extra_data.into(),
mix_hash: mix_hash
.map(|mh| mh.to_fixed_bytes().into())
.unwrap_or_default(),
nonce: None,
base_fee_per_gas: base_fee_per_gas.map(u256_to_uint256),
withdrawals_root: withdrawals_root.map(|root| root.to_fixed_bytes().into()),
blob_gas_used: None,
excess_blob_gas: None,
hash,
parent_beacon_block_root: None,
}
}
}

fn u256_to_uint256(u256: U256) -> Uint<256, 4> {
let mut bytes = [0u8; 32];
u256.to_big_endian(&mut bytes);
Uint::from_be_bytes(bytes)
}

fn u64_to_uint256(val: u64) -> Uint<256, 4> {
let u64_bytes: &[u8] = &val.to_be_bytes();
let high_zero_bytes: &[u8] = &[0u8; 24];
let bytes: [u8; 32] = [high_zero_bytes, u64_bytes]
.concat()
.try_into()
.expect("8 bytes + 24 bytes should be 32 bytes");
Uint::from_be_bytes(bytes)
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TxHashes {
pub hashes: Vec<H256>,
Expand Down

0 comments on commit 601c754

Please # to comment.