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

Feat/add withdrawals root #2348

Merged
merged 7 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 11 additions & 3 deletions ethers-core/src/types/block.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Modified from <https://github.com/tomusdrw/rust-web3/blob/master/src/types/block.rs>

use crate::types::{Address, Bloom, Bytes, Transaction, TxHash, H256, U256, U64};
use crate::types::{Address, Bloom, Bytes, Transaction, TxHash, Withdrawal, H256, U256, U64};
use chrono::{DateTime, TimeZone, Utc};
use serde::{
de::{MapAccess, Visitor},
Expand Down Expand Up @@ -84,10 +84,14 @@ pub struct Block<TX> {
/// Base fee per unit of gas (if past London)
#[serde(rename = "baseFeePerGas")]
pub base_fee_per_gas: Option<U256>,
/// Withdrawals root hash (EIP-4895)
#[serde(rename = "withdrawalsRoot")]
/// Withdrawals root hash (if past Shanghai)
#[serde(skip_serializing_if = "Option::is_none", rename = "withdrawalsRoot")]
merklefruit marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(not(feature = "celo"))]
pub withdrawals_root: Option<H256>,
/// Withdrawals (if past Shanghai)
#[serde(skip_serializing_if = "Option::is_none")]
merklefruit marked this conversation as resolved.
Show resolved Hide resolved
merklefruit marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(not(feature = "celo"))]
pub withdrawals: Option<Vec<Withdrawal>>,

#[cfg(feature = "celo")]
#[cfg_attr(docsrs, doc(cfg(feature = "celo")))]
Expand Down Expand Up @@ -223,6 +227,7 @@ impl Block<TxHash> {
nonce,
base_fee_per_gas,
withdrawals_root,
withdrawals,
other,
..
} = self;
Expand All @@ -249,6 +254,7 @@ impl Block<TxHash> {
nonce,
base_fee_per_gas,
withdrawals_root,
withdrawals,
transactions,
other,
}
Expand Down Expand Up @@ -329,6 +335,7 @@ impl From<Block<Transaction>> for Block<TxHash> {
nonce,
base_fee_per_gas,
withdrawals_root,
withdrawals,
other,
} = full;
Block {
Expand All @@ -354,6 +361,7 @@ impl From<Block<Transaction>> for Block<TxHash> {
nonce,
base_fee_per_gas,
withdrawals_root,
withdrawals,
transactions: transactions.iter().map(|tx| tx.hash).collect(),
other,
}
Expand Down
3 changes: 3 additions & 0 deletions ethers-core/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,6 @@ pub use syncing::{SyncProgress, SyncingStatus};

mod opcode;
pub use opcode::Opcode;

mod withdrawal;
pub use withdrawal::Withdrawal;
29 changes: 29 additions & 0 deletions ethers-core/src/types/withdrawal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use crate::types::{Address, U256, U64};
use serde::{Deserialize, Serialize};

/// A validator withdrawal from the consensus layer.
/// See EIP-4895: Beacon chain push withdrawals as operations.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit can derive

pub struct Withdrawal {
/// Monotonically increasing identifier issued by consensus layer
pub index: U64,

/// Index of validator associated with withdrawal
pub validator: U64,
merklefruit marked this conversation as resolved.
Show resolved Hide resolved

/// Target address for withdrawn ether
pub address: Address,

/// Value of withdrawal (in wei)
pub amount: U256,
}

impl rlp::Encodable for Withdrawal {
fn rlp_append(&self, s: &mut rlp::RlpStream) {
s.begin_list(4);
s.append(&self.index);
s.append(&self.validator);
s.append(&self.address);
s.append(&self.amount);
}
}