feat(core): add block conversion helpers (#1186)

* feat: add full to sparse block conversion

* feat: add sparse to full block conversion
This commit is contained in:
Matthias Seitz 2022-04-27 20:46:06 +02:00 committed by GitHub
parent 5de7086ba7
commit 19f7a93243
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 204 additions and 1 deletions

View File

@ -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};
@ -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