Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

feat(core): add block conversion helpers #1186

Merged
merged 2 commits into from
Apr 27, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
205 changes: 204 additions & 1 deletion ethers-core/src/types/block.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Taken from <https://github.com/tomusdrw/rust-web3/blob/master/src/types/block.rs>
use crate::types::{Address, Bloom, Bytes, H256, U256, U64};
use crate::types::{Address, Bloom, Bytes, Transaction, TxHash, H256, U256, U64};
#[cfg(not(feature = "celo"))]
use core::cmp::Ordering;
use serde::{ser::SerializeStruct, Deserialize, Deserializer, Serialize, Serializer};
Expand Down Expand Up @@ -137,6 +137,209 @@ impl<TX> Block<TX> {
}
}

impl Block<TxHash> {
/// Converts this block that only holds transaction hashes into a full block with `Transaction`
pub fn into_full_block(self, transactions: Vec<Transaction>) -> Block<Transaction> {
#[cfg(not(feature = "celo"))]
{
let Block {
hash,
parent_hash,
uncles_hash,
author,
state_root,
transactions_root,
receipts_root,
number,
gas_used,
gas_limit,
extra_data,
logs_bloom,
timestamp,
difficulty,
total_difficulty,
seal_fields,
uncles,
size,
mix_hash,
nonce,
base_fee_per_gas,
..
} = self;
Block {
hash,
parent_hash,
uncles_hash,
author,
state_root,
transactions_root,
receipts_root,
number,
gas_used,
gas_limit,
extra_data,
logs_bloom,
timestamp,
difficulty,
total_difficulty,
seal_fields,
uncles,
size,
mix_hash,
nonce,
base_fee_per_gas,
transactions,
}
}

#[cfg(feature = "celo")]
{
let Block {
hash,
parent_hash,
author,
state_root,
transactions_root,
receipts_root,
number,
gas_used,
extra_data,
logs_bloom,
timestamp,
total_difficulty,
seal_fields,
size,
base_fee_per_gas,
randomness,
epoch_snark_data,
..
} = self;

Block {
hash,
parent_hash,
author,
state_root,
transactions_root,
receipts_root,
number,
gas_used,
extra_data,
logs_bloom,
timestamp,
total_difficulty,
seal_fields,
size,
base_fee_per_gas,
randomness,
epoch_snark_data,
transactions,
}
}
}
}

impl From<Block<Transaction>> for Block<TxHash> {
fn from(full: Block<Transaction>) -> Self {
#[cfg(not(feature = "celo"))]
{
let Block {
hash,
parent_hash,
uncles_hash,
author,
state_root,
transactions_root,
receipts_root,
number,
gas_used,
gas_limit,
extra_data,
logs_bloom,
timestamp,
difficulty,
total_difficulty,
seal_fields,
uncles,
transactions,
size,
mix_hash,
nonce,
base_fee_per_gas,
} = full;
Block {
hash,
parent_hash,
uncles_hash,
author,
state_root,
transactions_root,
receipts_root,
number,
gas_used,
gas_limit,
extra_data,
logs_bloom,
timestamp,
difficulty,
total_difficulty,
seal_fields,
uncles,
size,
mix_hash,
nonce,
base_fee_per_gas,
transactions: transactions.iter().map(|tx| tx.hash).collect(),
}
}

#[cfg(feature = "celo")]
{
let Block {
hash,
parent_hash,
author,
state_root,
transactions_root,
receipts_root,
number,
gas_used,
extra_data,
logs_bloom,
timestamp,
total_difficulty,
seal_fields,
transactions,
size,
base_fee_per_gas,
randomness,
epoch_snark_data,
} = full;

Block {
hash,
parent_hash,
author,
state_root,
transactions_root,
receipts_root,
number,
gas_used,
extra_data,
logs_bloom,
timestamp,
total_difficulty,
seal_fields,
size,
base_fee_per_gas,
randomness,
epoch_snark_data,
transactions: transactions.iter().map(|tx| tx.hash).collect(),
}
}
}
}

#[derive(Debug, Default, Clone, PartialEq, Deserialize, Serialize)]
#[cfg(feature = "celo")]
/// Commit-reveal data for generating randomness in the
Expand Down