From 563304c8d7e9b33dd3bd1d2d92baa5ab2a2a76f9 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Fri, 15 Mar 2019 16:30:05 +1100 Subject: [PATCH] Update "block" family types to 0.5.0 - Removes Proposal - Removes "readers" as they aren't actually being used anywhere. --- eth2/types/src/beacon_block.rs | 40 ++++------- eth2/types/src/beacon_block_body.rs | 10 +-- eth2/types/src/lib.rs | 3 - eth2/types/src/proposal.rs | 67 ------------------- eth2/types/src/readers/block_reader.rs | 35 ---------- eth2/types/src/readers/mod.rs | 5 -- eth2/types/src/readers/state_reader.rs | 25 ------- .../testing_beacon_block_builder.rs | 5 +- 8 files changed, 20 insertions(+), 170 deletions(-) delete mode 100644 eth2/types/src/proposal.rs delete mode 100644 eth2/types/src/readers/block_reader.rs delete mode 100644 eth2/types/src/readers/mod.rs delete mode 100644 eth2/types/src/readers/state_reader.rs diff --git a/eth2/types/src/beacon_block.rs b/eth2/types/src/beacon_block.rs index 56f77c8d2..7fa3f5e11 100644 --- a/eth2/types/src/beacon_block.rs +++ b/eth2/types/src/beacon_block.rs @@ -1,15 +1,15 @@ use crate::test_utils::TestRandom; -use crate::{BeaconBlockBody, ChainSpec, Eth1Data, Hash256, Proposal, Slot}; +use crate::{BeaconBlockBody, ChainSpec, Eth1Data, Hash256, Slot}; use bls::Signature; use rand::RngCore; use serde_derive::{Deserialize, Serialize}; -use ssz::{SignedRoot, TreeHash}; +use ssz::TreeHash; use ssz_derive::{Decode, Encode, SignedRoot, TreeHash}; use test_random_derive::TestRandom; /// A block of the `BeaconChain`. /// -/// Spec v0.4.0 +/// Spec v0.5.0 #[derive( Debug, PartialEq, @@ -24,29 +24,27 @@ use test_random_derive::TestRandom; )] pub struct BeaconBlock { pub slot: Slot, - pub parent_root: Hash256, + pub previous_block_root: Hash256, pub state_root: Hash256, - pub randao_reveal: Signature, - pub eth1_data: Eth1Data, pub body: BeaconBlockBody, pub signature: Signature, } impl BeaconBlock { - /// Produce the first block of the Beacon Chain. + /// The first block of the Beacon Chain. /// - /// Spec v0.4.0 + /// Spec v0.5.0 pub fn genesis(state_root: Hash256, spec: &ChainSpec) -> BeaconBlock { BeaconBlock { slot: spec.genesis_slot, - parent_root: spec.zero_hash, + previous_block_root: spec.zero_hash, state_root, - randao_reveal: spec.empty_signature.clone(), - eth1_data: Eth1Data { - deposit_root: spec.zero_hash, - block_hash: spec.zero_hash, - }, body: BeaconBlockBody { + randao_reveal: spec.empty_signature.clone(), + eth1_data: Eth1Data { + deposit_root: spec.zero_hash, + block_hash: spec.zero_hash, + }, proposer_slashings: vec![], attester_slashings: vec![], attestations: vec![], @@ -60,22 +58,10 @@ impl BeaconBlock { /// Returns the `hash_tree_root` of the block. /// - /// Spec v0.4.0 + /// Spec v0.5.0 pub fn canonical_root(&self) -> Hash256 { Hash256::from_slice(&self.hash_tree_root()[..]) } - - /// Returns an unsigned proposal for block. - /// - /// Spec v0.4.0 - pub fn proposal(&self, spec: &ChainSpec) -> Proposal { - Proposal { - slot: self.slot, - shard: spec.beacon_chain_shard_number, - block_root: Hash256::from_slice(&self.signed_root()), - signature: spec.empty_signature.clone(), - } - } } #[cfg(test)] diff --git a/eth2/types/src/beacon_block_body.rs b/eth2/types/src/beacon_block_body.rs index ce8020fec..677e24cec 100644 --- a/eth2/types/src/beacon_block_body.rs +++ b/eth2/types/src/beacon_block_body.rs @@ -1,5 +1,5 @@ -use super::{Attestation, AttesterSlashing, Deposit, ProposerSlashing, Transfer, VoluntaryExit}; use crate::test_utils::TestRandom; +use crate::*; use rand::RngCore; use serde_derive::{Deserialize, Serialize}; use ssz_derive::{Decode, Encode, TreeHash}; @@ -7,11 +7,11 @@ use test_random_derive::TestRandom; /// The body of a `BeaconChain` block, containing operations. /// -/// Spec v0.4.0 -#[derive( - Debug, PartialEq, Clone, Default, Serialize, Deserialize, Encode, Decode, TreeHash, TestRandom, -)] +/// Spec v0.5.0 +#[derive(Debug, PartialEq, Clone, Serialize, Deserialize, Encode, Decode, TreeHash, TestRandom)] pub struct BeaconBlockBody { + pub randao_reveal: Signature, + pub eth1_data: Eth1Data, pub proposer_slashings: Vec, pub attester_slashings: Vec, pub attestations: Vec, diff --git a/eth2/types/src/lib.rs b/eth2/types/src/lib.rs index 4f50e0ea5..a1a58198b 100644 --- a/eth2/types/src/lib.rs +++ b/eth2/types/src/lib.rs @@ -22,9 +22,7 @@ pub mod fork; pub mod free_attestation; pub mod historical_batch; pub mod pending_attestation; -pub mod proposal; pub mod proposer_slashing; -pub mod readers; pub mod shard_reassignment_record; pub mod slashable_attestation; pub mod transfer; @@ -58,7 +56,6 @@ pub use crate::fork::Fork; pub use crate::free_attestation::FreeAttestation; pub use crate::historical_batch::HistoricalBatch; pub use crate::pending_attestation::PendingAttestation; -pub use crate::proposal::Proposal; pub use crate::proposer_slashing::ProposerSlashing; pub use crate::slashable_attestation::SlashableAttestation; pub use crate::slot_epoch::{Epoch, Slot}; diff --git a/eth2/types/src/proposal.rs b/eth2/types/src/proposal.rs deleted file mode 100644 index 36fba5603..000000000 --- a/eth2/types/src/proposal.rs +++ /dev/null @@ -1,67 +0,0 @@ -use crate::test_utils::TestRandom; -use crate::{Hash256, Slot}; -use bls::Signature; -use rand::RngCore; -use serde_derive::{Deserialize, Serialize}; -use ssz::TreeHash; -use ssz_derive::{Decode, Encode, SignedRoot, TreeHash}; -use test_random_derive::TestRandom; - -/// A proposal for some shard or beacon block. -/// -/// Spec v0.4.0 -#[derive( - Debug, - PartialEq, - Clone, - Serialize, - Deserialize, - Encode, - Decode, - TreeHash, - TestRandom, - SignedRoot, -)] -pub struct Proposal { - pub slot: Slot, - /// Shard number (spec.beacon_chain_shard_number for beacon chain) - pub shard: u64, - pub block_root: Hash256, - pub signature: Signature, -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; - use ssz::{SignedRoot, TreeHash}; - - #[derive(TreeHash)] - struct SignedProposal { - pub slot: Slot, - pub shard: u64, - pub block_root: Hash256, - } - - impl Into for Proposal { - fn into(self) -> SignedProposal { - SignedProposal { - slot: self.slot, - shard: self.shard, - block_root: self.block_root, - } - } - } - - #[test] - pub fn test_signed_root() { - let mut rng = XorShiftRng::from_seed([42; 16]); - let original = Proposal::random_for_test(&mut rng); - - let other: SignedProposal = original.clone().into(); - - assert_eq!(original.signed_root(), other.hash_tree_root()); - } - - ssz_tests!(Proposal); -} diff --git a/eth2/types/src/readers/block_reader.rs b/eth2/types/src/readers/block_reader.rs deleted file mode 100644 index 93157a1a3..000000000 --- a/eth2/types/src/readers/block_reader.rs +++ /dev/null @@ -1,35 +0,0 @@ -use crate::{BeaconBlock, Hash256, Slot}; -use std::fmt::Debug; - -/// The `BeaconBlockReader` provides interfaces for reading a subset of fields of a `BeaconBlock`. -/// -/// The purpose of this trait is to allow reading from either; -/// - a standard `BeaconBlock` struct, or -/// - a SSZ serialized byte array. -/// -/// Note: presently, direct SSZ reading has not been implemented so this trait is being used for -/// "future proofing". -pub trait BeaconBlockReader: Debug + PartialEq { - fn slot(&self) -> Slot; - fn parent_root(&self) -> Hash256; - fn state_root(&self) -> Hash256; - fn into_beacon_block(self) -> Option; -} - -impl BeaconBlockReader for BeaconBlock { - fn slot(&self) -> Slot { - self.slot - } - - fn parent_root(&self) -> Hash256 { - self.parent_root - } - - fn state_root(&self) -> Hash256 { - self.state_root - } - - fn into_beacon_block(self) -> Option { - Some(self) - } -} diff --git a/eth2/types/src/readers/mod.rs b/eth2/types/src/readers/mod.rs deleted file mode 100644 index 4ccb14a8c..000000000 --- a/eth2/types/src/readers/mod.rs +++ /dev/null @@ -1,5 +0,0 @@ -mod block_reader; -mod state_reader; - -pub use self::block_reader::BeaconBlockReader; -pub use self::state_reader::BeaconStateReader; diff --git a/eth2/types/src/readers/state_reader.rs b/eth2/types/src/readers/state_reader.rs deleted file mode 100644 index e469bee57..000000000 --- a/eth2/types/src/readers/state_reader.rs +++ /dev/null @@ -1,25 +0,0 @@ -use crate::{BeaconState, Slot}; -use std::fmt::Debug; - -/// The `BeaconStateReader` provides interfaces for reading a subset of fields of a `BeaconState`. -/// -/// The purpose of this trait is to allow reading from either; -/// - a standard `BeaconState` struct, or -/// - a SSZ serialized byte array. -/// -/// Note: presently, direct SSZ reading has not been implemented so this trait is being used for -/// "future proofing". -pub trait BeaconStateReader: Debug + PartialEq { - fn slot(&self) -> Slot; - fn into_beacon_state(self) -> Option; -} - -impl BeaconStateReader for BeaconState { - fn slot(&self) -> Slot { - self.slot - } - - fn into_beacon_state(self) -> Option { - Some(self) - } -} diff --git a/eth2/types/src/test_utils/testing_beacon_block_builder.rs b/eth2/types/src/test_utils/testing_beacon_block_builder.rs index 58633b5ce..e0e4677d4 100644 --- a/eth2/types/src/test_utils/testing_beacon_block_builder.rs +++ b/eth2/types/src/test_utils/testing_beacon_block_builder.rs @@ -32,8 +32,7 @@ impl TestingBeaconBlockBuilder { /// /// Modifying the block after signing may invalidate the signature. pub fn sign(&mut self, sk: &SecretKey, fork: &Fork, spec: &ChainSpec) { - let proposal = self.block.proposal(spec); - let message = proposal.signed_root(); + let message = self.block.signed_root(); let epoch = self.block.slot.epoch(spec.slots_per_epoch); let domain = spec.get_domain(epoch, Domain::Proposal, fork); self.block.signature = Signature::new(&message, domain, sk); @@ -46,7 +45,7 @@ impl TestingBeaconBlockBuilder { let epoch = self.block.slot.epoch(spec.slots_per_epoch); let message = epoch.hash_tree_root(); let domain = spec.get_domain(epoch, Domain::Randao, fork); - self.block.randao_reveal = Signature::new(&message, domain, sk); + self.block.body.randao_reveal = Signature::new(&message, domain, sk); } /// Inserts a signed, valid `ProposerSlashing` for the validator.