Skip to content

Commit

Permalink
feat(core): add block conversion helpers (#1186)
Browse files Browse the repository at this point in the history
* feat: add full to sparse block conversion

* feat: add sparse to full block conversion
  • Loading branch information
mattsse committed Apr 27, 2022
1 parent 5de7086 commit 19f7a93
Showing 1 changed file with 204 additions and 1 deletion.
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

0 comments on commit 19f7a93

Please sign in to comment.