From 19f7a9324394ab2c1e06d6dfc7666cbc55f56aab Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Wed, 27 Apr 2022 20:46:06 +0200 Subject: [PATCH] feat(core): add block conversion helpers (#1186) * feat: add full to sparse block conversion * feat: add sparse to full block conversion --- ethers-core/src/types/block.rs | 205 ++++++++++++++++++++++++++++++++- 1 file changed, 204 insertions(+), 1 deletion(-) diff --git a/ethers-core/src/types/block.rs b/ethers-core/src/types/block.rs index cfe9d179..c3038dc8 100644 --- a/ethers-core/src/types/block.rs +++ b/ethers-core/src/types/block.rs @@ -1,5 +1,5 @@ // Taken from -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 Block { } } +impl Block { + /// Converts this block that only holds transaction hashes into a full block with `Transaction` + pub fn into_full_block(self, transactions: Vec) -> Block { + #[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> for Block { + fn from(full: Block) -> 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