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

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::*;