Convert Header and Block rpc to primitives (#7660)

This commit is contained in:
Thomas Coratger
2024-04-15 21:30:35 +02:00
committed by GitHub
parent 6f210f1366
commit c59c41701d
6 changed files with 141 additions and 39 deletions

36
Cargo.lock generated
View File

@ -146,7 +146,7 @@ dependencies = [
[[package]]
name = "alloy-consensus"
version = "0.1.0"
source = "git+https://github.com/alloy-rs/alloy?rev=8cb0307#8cb0307b9bdb6cef9058d2d1a2219c8d212a7421"
source = "git+https://github.com/alloy-rs/alloy?rev=a32e6f7#a32e6f7f84cc1f2a1b244cc79a20057b0a3d4cba"
dependencies = [
"alloy-eips",
"alloy-primitives",
@ -178,7 +178,7 @@ dependencies = [
[[package]]
name = "alloy-eips"
version = "0.1.0"
source = "git+https://github.com/alloy-rs/alloy?rev=8cb0307#8cb0307b9bdb6cef9058d2d1a2219c8d212a7421"
source = "git+https://github.com/alloy-rs/alloy?rev=a32e6f7#a32e6f7f84cc1f2a1b244cc79a20057b0a3d4cba"
dependencies = [
"alloy-primitives",
"alloy-rlp",
@ -196,7 +196,7 @@ dependencies = [
[[package]]
name = "alloy-genesis"
version = "0.1.0"
source = "git+https://github.com/alloy-rs/alloy?rev=8cb0307#8cb0307b9bdb6cef9058d2d1a2219c8d212a7421"
source = "git+https://github.com/alloy-rs/alloy?rev=a32e6f7#a32e6f7f84cc1f2a1b244cc79a20057b0a3d4cba"
dependencies = [
"alloy-primitives",
"alloy-serde",
@ -218,7 +218,7 @@ dependencies = [
[[package]]
name = "alloy-json-rpc"
version = "0.1.0"
source = "git+https://github.com/alloy-rs/alloy?rev=8cb0307#8cb0307b9bdb6cef9058d2d1a2219c8d212a7421"
source = "git+https://github.com/alloy-rs/alloy?rev=a32e6f7#a32e6f7f84cc1f2a1b244cc79a20057b0a3d4cba"
dependencies = [
"alloy-primitives",
"serde",
@ -230,7 +230,7 @@ dependencies = [
[[package]]
name = "alloy-network"
version = "0.1.0"
source = "git+https://github.com/alloy-rs/alloy?rev=8cb0307#8cb0307b9bdb6cef9058d2d1a2219c8d212a7421"
source = "git+https://github.com/alloy-rs/alloy?rev=a32e6f7#a32e6f7f84cc1f2a1b244cc79a20057b0a3d4cba"
dependencies = [
"alloy-consensus",
"alloy-eips",
@ -246,7 +246,7 @@ dependencies = [
[[package]]
name = "alloy-node-bindings"
version = "0.1.0"
source = "git+https://github.com/alloy-rs/alloy?rev=8cb0307#8cb0307b9bdb6cef9058d2d1a2219c8d212a7421"
source = "git+https://github.com/alloy-rs/alloy?rev=a32e6f7#a32e6f7f84cc1f2a1b244cc79a20057b0a3d4cba"
dependencies = [
"alloy-genesis",
"alloy-primitives",
@ -288,7 +288,7 @@ dependencies = [
[[package]]
name = "alloy-provider"
version = "0.1.0"
source = "git+https://github.com/alloy-rs/alloy?rev=8cb0307#8cb0307b9bdb6cef9058d2d1a2219c8d212a7421"
source = "git+https://github.com/alloy-rs/alloy?rev=a32e6f7#a32e6f7f84cc1f2a1b244cc79a20057b0a3d4cba"
dependencies = [
"alloy-eips",
"alloy-json-rpc",
@ -338,7 +338,7 @@ dependencies = [
[[package]]
name = "alloy-rpc-client"
version = "0.1.0"
source = "git+https://github.com/alloy-rs/alloy?rev=8cb0307#8cb0307b9bdb6cef9058d2d1a2219c8d212a7421"
source = "git+https://github.com/alloy-rs/alloy?rev=a32e6f7#a32e6f7f84cc1f2a1b244cc79a20057b0a3d4cba"
dependencies = [
"alloy-json-rpc",
"alloy-transport",
@ -358,7 +358,7 @@ dependencies = [
[[package]]
name = "alloy-rpc-types"
version = "0.1.0"
source = "git+https://github.com/alloy-rs/alloy?rev=8cb0307#8cb0307b9bdb6cef9058d2d1a2219c8d212a7421"
source = "git+https://github.com/alloy-rs/alloy?rev=a32e6f7#a32e6f7f84cc1f2a1b244cc79a20057b0a3d4cba"
dependencies = [
"alloy-consensus",
"alloy-eips",
@ -380,7 +380,7 @@ dependencies = [
[[package]]
name = "alloy-rpc-types-anvil"
version = "0.1.0"
source = "git+https://github.com/alloy-rs/alloy?rev=8cb0307#8cb0307b9bdb6cef9058d2d1a2219c8d212a7421"
source = "git+https://github.com/alloy-rs/alloy?rev=a32e6f7#a32e6f7f84cc1f2a1b244cc79a20057b0a3d4cba"
dependencies = [
"alloy-primitives",
"alloy-serde",
@ -390,7 +390,7 @@ dependencies = [
[[package]]
name = "alloy-rpc-types-engine"
version = "0.1.0"
source = "git+https://github.com/alloy-rs/alloy?rev=8cb0307#8cb0307b9bdb6cef9058d2d1a2219c8d212a7421"
source = "git+https://github.com/alloy-rs/alloy?rev=a32e6f7#a32e6f7f84cc1f2a1b244cc79a20057b0a3d4cba"
dependencies = [
"alloy-consensus",
"alloy-eips",
@ -408,7 +408,7 @@ dependencies = [
[[package]]
name = "alloy-rpc-types-trace"
version = "0.1.0"
source = "git+https://github.com/alloy-rs/alloy?rev=8cb0307#8cb0307b9bdb6cef9058d2d1a2219c8d212a7421"
source = "git+https://github.com/alloy-rs/alloy?rev=a32e6f7#a32e6f7f84cc1f2a1b244cc79a20057b0a3d4cba"
dependencies = [
"alloy-primitives",
"alloy-rpc-types",
@ -420,7 +420,7 @@ dependencies = [
[[package]]
name = "alloy-serde"
version = "0.1.0"
source = "git+https://github.com/alloy-rs/alloy?rev=8cb0307#8cb0307b9bdb6cef9058d2d1a2219c8d212a7421"
source = "git+https://github.com/alloy-rs/alloy?rev=a32e6f7#a32e6f7f84cc1f2a1b244cc79a20057b0a3d4cba"
dependencies = [
"alloy-primitives",
"serde",
@ -430,7 +430,7 @@ dependencies = [
[[package]]
name = "alloy-signer"
version = "0.1.0"
source = "git+https://github.com/alloy-rs/alloy?rev=8cb0307#8cb0307b9bdb6cef9058d2d1a2219c8d212a7421"
source = "git+https://github.com/alloy-rs/alloy?rev=a32e6f7#a32e6f7f84cc1f2a1b244cc79a20057b0a3d4cba"
dependencies = [
"alloy-primitives",
"async-trait",
@ -443,7 +443,7 @@ dependencies = [
[[package]]
name = "alloy-signer-wallet"
version = "0.1.0"
source = "git+https://github.com/alloy-rs/alloy?rev=8cb0307#8cb0307b9bdb6cef9058d2d1a2219c8d212a7421"
source = "git+https://github.com/alloy-rs/alloy?rev=a32e6f7#a32e6f7f84cc1f2a1b244cc79a20057b0a3d4cba"
dependencies = [
"alloy-consensus",
"alloy-network",
@ -518,7 +518,7 @@ dependencies = [
[[package]]
name = "alloy-transport"
version = "0.1.0"
source = "git+https://github.com/alloy-rs/alloy?rev=8cb0307#8cb0307b9bdb6cef9058d2d1a2219c8d212a7421"
source = "git+https://github.com/alloy-rs/alloy?rev=a32e6f7#a32e6f7f84cc1f2a1b244cc79a20057b0a3d4cba"
dependencies = [
"alloy-json-rpc",
"base64 0.22.0",
@ -536,7 +536,7 @@ dependencies = [
[[package]]
name = "alloy-transport-http"
version = "0.1.0"
source = "git+https://github.com/alloy-rs/alloy?rev=8cb0307#8cb0307b9bdb6cef9058d2d1a2219c8d212a7421"
source = "git+https://github.com/alloy-rs/alloy?rev=a32e6f7#a32e6f7f84cc1f2a1b244cc79a20057b0a3d4cba"
dependencies = [
"alloy-json-rpc",
"alloy-transport",
@ -7550,7 +7550,7 @@ dependencies = [
[[package]]
name = "revm-inspectors"
version = "0.1.0"
source = "git+https://github.com/paradigmxyz/evm-inspectors?rev=f604dc4#f604dc4d4cd1f013c8bd488b2f28356a77fa2094"
source = "git+https://github.com/paradigmxyz/evm-inspectors?rev=67f9968#67f9968fe56e5968ada322d084a98dd6a405ccdb"
dependencies = [
"alloy-primitives",
"alloy-rpc-types",

View File

@ -267,7 +267,7 @@ revm = { version = "8.0.0", features = [
revm-primitives = { version = "3.1.0", features = [
"std",
], default-features = false }
revm-inspectors = { git = "https://github.com/paradigmxyz/evm-inspectors", rev = "f604dc4" }
revm-inspectors = { git = "https://github.com/paradigmxyz/evm-inspectors", rev = "67f9968" }
# eth
alloy-chains = "0.1.15"
@ -276,20 +276,20 @@ alloy-dyn-abi = "0.7.0"
alloy-sol-types = "0.7.0"
alloy-rlp = "0.3.4"
alloy-trie = "0.3.1"
alloy-rpc-types = { git = "https://github.com/alloy-rs/alloy", rev = "8cb0307" }
alloy-rpc-types-anvil = { git = "https://github.com/alloy-rs/alloy", rev = "8cb0307" }
alloy-rpc-types-trace = { git = "https://github.com/alloy-rs/alloy", rev = "8cb0307" }
alloy-rpc-types-engine = { git = "https://github.com/alloy-rs/alloy", rev = "8cb0307" }
alloy-genesis = { git = "https://github.com/alloy-rs/alloy", rev = "8cb0307" }
alloy-node-bindings = { git = "https://github.com/alloy-rs/alloy", rev = "8cb0307" }
alloy-provider = { git = "https://github.com/alloy-rs/alloy", rev = "8cb0307", default-features = false, features = [
alloy-rpc-types = { git = "https://github.com/alloy-rs/alloy", rev = "a32e6f7" }
alloy-rpc-types-anvil = { git = "https://github.com/alloy-rs/alloy", rev = "a32e6f7" }
alloy-rpc-types-trace = { git = "https://github.com/alloy-rs/alloy", rev = "a32e6f7" }
alloy-rpc-types-engine = { git = "https://github.com/alloy-rs/alloy", rev = "a32e6f7" }
alloy-genesis = { git = "https://github.com/alloy-rs/alloy", rev = "a32e6f7" }
alloy-node-bindings = { git = "https://github.com/alloy-rs/alloy", rev = "a32e6f7" }
alloy-provider = { git = "https://github.com/alloy-rs/alloy", rev = "a32e6f7", default-features = false, features = [
"reqwest",
] }
alloy-eips = { git = "https://github.com/alloy-rs/alloy", rev = "8cb0307" }
alloy-signer = { git = "https://github.com/alloy-rs/alloy", rev = "8cb0307" }
alloy-signer-wallet = { git = "https://github.com/alloy-rs/alloy", rev = "8cb0307" }
alloy-network = { git = "https://github.com/alloy-rs/alloy", rev = "8cb0307" }
alloy-consensus = { git = "https://github.com/alloy-rs/alloy", rev = "8cb0307" }
alloy-eips = { git = "https://github.com/alloy-rs/alloy", rev = "a32e6f7" }
alloy-signer = { git = "https://github.com/alloy-rs/alloy", rev = "a32e6f7" }
alloy-signer-wallet = { git = "https://github.com/alloy-rs/alloy", rev = "a32e6f7" }
alloy-network = { git = "https://github.com/alloy-rs/alloy", rev = "a32e6f7" }
alloy-consensus = { git = "https://github.com/alloy-rs/alloy", rev = "a32e6f7" }
# misc
aquamarine = "0.5"

View File

@ -1,11 +1,12 @@
use crate::{
Address, Bytes, GotExpected, Header, SealedHeader, TransactionSigned,
Address, Bytes, GotExpected, Header, SealedHeader, Signature, TransactionSigned,
TransactionSignedEcRecovered, Withdrawals, B256,
};
use alloy_rlp::{RlpDecodable, RlpEncodable};
#[cfg(any(test, feature = "arbitrary"))]
use proptest::prelude::{any, prop_compose};
use reth_codecs::derive_arbitrary;
use reth_rpc_types::ConversionError;
use serde::{Deserialize, Serialize};
use std::ops::Deref;
@ -147,6 +148,48 @@ impl Deref for Block {
}
}
impl TryFrom<reth_rpc_types::Block> for Block {
type Error = ConversionError;
fn try_from(block: reth_rpc_types::Block) -> Result<Self, Self::Error> {
let body = {
let transactions: Result<Vec<TransactionSigned>, ConversionError> = match block
.transactions
{
reth_rpc_types::BlockTransactions::Full(transactions) => transactions
.into_iter()
.map(|tx| {
let signature = tx.signature.ok_or(ConversionError::MissingSignature)?;
Ok(TransactionSigned::from_transaction_and_signature(
tx.try_into()?,
Signature {
r: signature.r,
s: signature.s,
odd_y_parity: signature
.y_parity
.unwrap_or(reth_rpc_types::Parity(false))
.0,
},
))
})
.collect(),
reth_rpc_types::BlockTransactions::Hashes(_) |
reth_rpc_types::BlockTransactions::Uncle => {
return Err(ConversionError::MissingFullTransactions);
}
};
transactions?
};
Ok(Self {
header: block.header.try_into()?,
body,
ommers: Default::default(),
withdrawals: block.withdrawals.map(Into::into),
})
}
}
/// Sealed block with senders recovered from transactions.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct BlockWithSenders {

View File

@ -16,6 +16,7 @@ use bytes::BufMut;
#[cfg(any(test, feature = "arbitrary"))]
use proptest::prelude::*;
use reth_codecs::{add_arbitrary_tests, derive_arbitrary, main_codec, Compact};
use reth_rpc_types::ConversionError;
use serde::{Deserialize, Serialize};
use std::{mem, ops::Deref};
@ -485,6 +486,50 @@ impl Decodable for Header {
}
}
impl TryFrom<reth_rpc_types::Header> for Header {
type Error = ConversionError;
fn try_from(header: reth_rpc_types::Header) -> Result<Self, Self::Error> {
Ok(Self {
base_fee_per_gas: header
.base_fee_per_gas
.map(|base_fee_per_gas| {
base_fee_per_gas.try_into().map_err(ConversionError::BaseFeePerGasConversion)
})
.transpose()?,
beneficiary: header.miner,
blob_gas_used: header
.blob_gas_used
.map(|blob_gas_used| {
blob_gas_used.try_into().map_err(ConversionError::BlobGasUsedConversion)
})
.transpose()?,
difficulty: header.difficulty,
excess_blob_gas: header
.excess_blob_gas
.map(|excess_blob_gas| {
excess_blob_gas.try_into().map_err(ConversionError::ExcessBlobGasConversion)
})
.transpose()?,
extra_data: header.extra_data,
gas_limit: header.gas_limit.try_into().map_err(ConversionError::GasLimitConversion)?,
gas_used: header.gas_used.try_into().map_err(ConversionError::GasUsedConversion)?,
logs_bloom: header.logs_bloom,
mix_hash: header.mix_hash.unwrap_or_default(),
nonce: u64::from_be_bytes(header.nonce.unwrap_or_default().0),
number: header.number.ok_or(ConversionError::MissingBlockNumber)?,
ommers_hash: header.uncles_hash,
parent_beacon_block_root: header.parent_beacon_block_root,
parent_hash: header.parent_hash,
receipts_root: header.receipts_root,
state_root: header.state_root,
timestamp: header.timestamp,
transactions_root: header.transactions_root,
withdrawals_root: header.withdrawals_root,
})
}
}
/// Errors that can occur during header sanity checks.
#[derive(thiserror::Error, Debug, PartialEq, Eq, Clone)]
pub enum HeaderValidationError {

View File

@ -36,6 +36,17 @@ impl Withdrawal {
}
}
impl From<reth_rpc_types::Withdrawal> for Withdrawal {
fn from(withdrawal: reth_rpc_types::Withdrawal) -> Self {
Self {
index: withdrawal.index,
validator_index: withdrawal.index,
address: withdrawal.address,
amount: withdrawal.amount,
}
}
}
/// Represents a collection of Withdrawals.
#[main_codec]
#[derive(Debug, Clone, PartialEq, Eq, Default, Hash, RlpEncodableWrapper, RlpDecodableWrapper)]
@ -104,6 +115,12 @@ impl DerefMut for Withdrawals {
}
}
impl From<Vec<reth_rpc_types::Withdrawal>> for Withdrawals {
fn from(withdrawals: Vec<reth_rpc_types::Withdrawal>) -> Self {
Self(withdrawals.into_iter().map(Into::into).collect())
}
}
#[cfg(test)]
mod tests {
use super::*;

View File

@ -1,6 +1,6 @@
use async_trait::async_trait;
use jsonrpsee::core::RpcResult as Result;
use reth_primitives::{Address, U256, U64};
use reth_primitives::Address;
use reth_rpc_api::TxPoolApiServer;
use reth_rpc_types::{
txpool::{TxpoolContent, TxpoolContentFrom, TxpoolInspect, TxpoolInspectSummary, TxpoolStatus},
@ -69,10 +69,7 @@ where
async fn txpool_status(&self) -> Result<TxpoolStatus> {
trace!(target: "rpc::eth", "Serving txpool_status");
let all = self.pool.all_transactions();
Ok(TxpoolStatus {
pending: U64::from(all.pending.len()),
queued: U64::from(all.queued.len()),
})
Ok(TxpoolStatus { pending: all.pending.len() as u64, queued: all.queued.len() as u64 })
}
/// Returns a summary of all the transactions currently pending for inclusion in the next
@ -97,8 +94,8 @@ where
TxpoolInspectSummary {
to: tx.to(),
value: tx.value(),
gas: U256::from(tx.gas_limit()),
gas_price: U256::from(tx.transaction.max_fee_per_gas()),
gas: tx.gas_limit() as u128,
gas_price: tx.transaction.max_fee_per_gas(),
},
);
}