From d41a8713d0aafe9802e93bf6c05719eab8c860a8 Mon Sep 17 00:00:00 2001 From: Noah Citron Date: Wed, 14 Sep 2022 17:21:09 -0400 Subject: [PATCH] fix: add missing fields to beacon block body (#31) * add proposer slashings to block body * add attestation slashings to beacon block * add voluntary exits to beacon body * remove comment * fix u256_deserialize --- consensus/src/types.rs | 85 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 74 insertions(+), 11 deletions(-) diff --git a/consensus/src/types.rs b/consensus/src/types.rs index d39f4fa..afeae56 100644 --- a/consensus/src/types.rs +++ b/consensus/src/types.rs @@ -31,14 +31,11 @@ pub struct BeaconBlockBody { eth1_data: Eth1Data, #[serde(deserialize_with = "bytes32_deserialize")] graffiti: Bytes32, - // TODO: handle - proposer_slashings: List, - // TODO: handle - attester_slashings: List, + proposer_slashings: List, + attester_slashings: List, attestations: List, deposits: List, - // TODO: handle - voluntary_exits: List, + voluntary_exits: List, sync_aggregate: SyncAggregate, pub execution_payload: ExecutionPayload, } @@ -75,6 +72,48 @@ pub struct ExecutionPayload { pub transactions: List, } +#[derive(serde::Deserialize, Debug, Default, SimpleSerialize, Clone)] +struct ProposerSlashing { + signed_header_1: SignedBeaconBlockHeader, + signed_header_2: SignedBeaconBlockHeader, +} + +#[derive(serde::Deserialize, Debug, Default, SimpleSerialize, Clone)] +struct SignedBeaconBlockHeader { + message: BeaconBlockHeader, + #[serde(deserialize_with = "signature_deserialize")] + signature: SignatureBytes, +} + +#[derive(serde::Deserialize, Debug, Default, SimpleSerialize, Clone)] +struct BeaconBlockHeader { + #[serde(deserialize_with = "u64_deserialize")] + slot: u64, + #[serde(deserialize_with = "u64_deserialize")] + proposer_index: u64, + #[serde(deserialize_with = "bytes32_deserialize")] + parent_root: Bytes32, + #[serde(deserialize_with = "bytes32_deserialize")] + state_root: Bytes32, + #[serde(deserialize_with = "bytes32_deserialize")] + body_root: Bytes32, +} + +#[derive(serde::Deserialize, Debug, Default, SimpleSerialize, Clone)] +struct AttesterSlashing { + attestation_1: IndexedAttestation, + attestation_2: IndexedAttestation, +} + +#[derive(serde::Deserialize, Debug, Default, SimpleSerialize, Clone)] +struct IndexedAttestation { + #[serde(deserialize_with = "attesting_indices_deserialize")] + attesting_indices: List, + data: AttestationData, + #[serde(deserialize_with = "signature_deserialize")] + signature: SignatureBytes, +} + #[derive(serde::Deserialize, Debug, Default, SimpleSerialize, Clone)] struct Attestation { aggregation_bits: Bitlist<2048>, @@ -104,8 +143,18 @@ struct Checkpoint { } #[derive(serde::Deserialize, Debug, Default, SimpleSerialize, Clone)] -struct Dummy { - t: u64, +struct SignedVoluntaryExit { + message: VoluntaryExit, + #[serde(deserialize_with = "signature_deserialize")] + signature: SignatureBytes, +} + +#[derive(serde::Deserialize, Debug, Default, SimpleSerialize, Clone)] +struct VoluntaryExit { + #[serde(deserialize_with = "u64_deserialize")] + epoch: u64, + #[serde(deserialize_with = "u64_deserialize")] + validator_index: u64, } #[derive(serde::Deserialize, Debug, Default, SimpleSerialize, Clone)] @@ -283,9 +332,10 @@ where D: serde::Deserializer<'de>, { let val: String = serde::Deserialize::deserialize(deserializer)?; - // TODO: support larger values - let i = val.parse::().map_err(D::Error::custom)?; - Ok(U256::from(i)) + let x = ethers::types::U256::from_dec_str(&val).map_err(D::Error::custom)?; + let mut x_bytes = [0; 32]; + x.to_little_endian(&mut x_bytes); + Ok(U256::from_bytes_le(x_bytes)) } fn bytes32_deserialize<'de, D>(deserializer: D) -> Result @@ -339,3 +389,16 @@ where .collect::>(); Ok(transactions) } + +fn attesting_indices_deserialize<'de, D>(deserializer: D) -> Result, D::Error> +where + D: serde::Deserializer<'de>, +{ + let attesting_indices: Vec = serde::Deserialize::deserialize(deserializer)?; + let attesting_indices = attesting_indices + .iter() + .map(|i| i.parse().unwrap()) + .collect::>(); + + Ok(attesting_indices) +}