Merge current master

This commit is contained in:
Kirk Baird 2019-02-20 13:59:11 +11:00
commit 673c8ea388
No known key found for this signature in database
GPG Key ID: BF864B7ED0BEA33F
57 changed files with 677 additions and 973 deletions

View File

@ -1,7 +1,7 @@
[workspace] [workspace]
members = [ members = [
"eth2/attester", "eth2/attester",
"eth2/block_producer", "eth2/block_proposer",
"eth2/fork_choice", "eth2/fork_choice",
"eth2/state_processing", "eth2/state_processing",
"eth2/types", "eth2/types",
@ -12,6 +12,7 @@ members = [
"eth2/utils/int_to_bytes", "eth2/utils/int_to_bytes",
"eth2/utils/slot_clock", "eth2/utils/slot_clock",
"eth2/utils/ssz", "eth2/utils/ssz",
"eth2/utils/ssz_derive",
"eth2/utils/swap_or_not_shuffle", "eth2/utils/swap_or_not_shuffle",
"eth2/utils/fisher_yates_shuffle", "eth2/utils/fisher_yates_shuffle",
"beacon_node", "beacon_node",

View File

@ -5,7 +5,7 @@ authors = ["Paul Hauner <paul@paulhauner.com>"]
edition = "2018" edition = "2018"
[dependencies] [dependencies]
block_producer = { path = "../../eth2/block_producer" } block_proposer = { path = "../../eth2/block_proposer" }
bls = { path = "../../eth2/utils/bls" } bls = { path = "../../eth2/utils/bls" }
boolean-bitfield = { path = "../../eth2/utils/boolean-bitfield" } boolean-bitfield = { path = "../../eth2/utils/boolean-bitfield" }
db = { path = "../db" } db = { path = "../db" }

View File

@ -14,7 +14,7 @@ criterion = "0.2"
[dependencies] [dependencies]
attester = { path = "../../../eth2/attester" } attester = { path = "../../../eth2/attester" }
beacon_chain = { path = "../../beacon_chain" } beacon_chain = { path = "../../beacon_chain" }
block_producer = { path = "../../../eth2/block_producer" } block_proposer = { path = "../../../eth2/block_proposer" }
bls = { path = "../../../eth2/utils/bls" } bls = { path = "../../../eth2/utils/bls" }
boolean-bitfield = { path = "../../../eth2/utils/boolean-bitfield" } boolean-bitfield = { path = "../../../eth2/utils/boolean-bitfield" }
db = { path = "../../db" } db = { path = "../../db" }

View File

@ -3,7 +3,7 @@ use attester::{
PublishOutcome as AttestationPublishOutcome, PublishOutcome as AttestationPublishOutcome,
}; };
use beacon_chain::BeaconChain; use beacon_chain::BeaconChain;
use block_producer::{ use block_proposer::{
BeaconNode as BeaconBlockNode, BeaconNodeError as BeaconBlockNodeError, BeaconNode as BeaconBlockNode, BeaconNodeError as BeaconBlockNodeError,
PublishOutcome as BlockPublishOutcome, PublishOutcome as BlockPublishOutcome,
}; };

View File

@ -2,7 +2,7 @@ use attester::{
DutiesReader as AttesterDutiesReader, DutiesReaderError as AttesterDutiesReaderError, DutiesReader as AttesterDutiesReader, DutiesReaderError as AttesterDutiesReaderError,
}; };
use beacon_chain::BeaconChain; use beacon_chain::BeaconChain;
use block_producer::{ use block_proposer::{
DutiesReader as ProducerDutiesReader, DutiesReaderError as ProducerDutiesReaderError, DutiesReader as ProducerDutiesReader, DutiesReaderError as ProducerDutiesReaderError,
}; };
use db::ClientDB; use db::ClientDB;

View File

@ -1,5 +1,5 @@
use attester::Signer as AttesterSigner; use attester::Signer as AttesterSigner;
use block_producer::Signer as BlockProposerSigner; use block_proposer::Signer as BlockProposerSigner;
use std::sync::RwLock; use std::sync::RwLock;
use types::{Keypair, Signature}; use types::{Keypair, Signature};

View File

@ -5,8 +5,8 @@ mod local_signer;
use attester::PollOutcome as AttestationPollOutcome; use attester::PollOutcome as AttestationPollOutcome;
use attester::{Attester, Error as AttestationPollError}; use attester::{Attester, Error as AttestationPollError};
use beacon_chain::BeaconChain; use beacon_chain::BeaconChain;
use block_producer::PollOutcome as BlockPollOutcome; use block_proposer::PollOutcome as BlockPollOutcome;
use block_producer::{BlockProducer, Error as BlockPollError}; use block_proposer::{BlockProducer, Error as BlockPollError};
use db::MemoryDB; use db::MemoryDB;
use direct_beacon_node::DirectBeaconNode; use direct_beacon_node::DirectBeaconNode;
use direct_duties::DirectDuties; use direct_duties::DirectDuties;

View File

@ -1,5 +1,5 @@
[package] [package]
name = "block_producer" name = "block_proposer"
version = "0.1.0" version = "0.1.0"
authors = ["Paul Hauner <paul@paulhauner.com>"] authors = ["Paul Hauner <paul@paulhauner.com>"]
edition = "2018" edition = "2018"

View File

@ -239,7 +239,7 @@ mod tests {
epoch_map.map.insert(produce_epoch, produce_slot); epoch_map.map.insert(produce_epoch, produce_slot);
let epoch_map = Arc::new(epoch_map); let epoch_map = Arc::new(epoch_map);
let mut block_producer = BlockProducer::new( let mut block_proposer = BlockProducer::new(
spec.clone(), spec.clone(),
epoch_map.clone(), epoch_map.clone(),
slot_clock.clone(), slot_clock.clone(),
@ -254,28 +254,28 @@ mod tests {
// One slot before production slot... // One slot before production slot...
slot_clock.set_slot(produce_slot.as_u64() - 1); slot_clock.set_slot(produce_slot.as_u64() - 1);
assert_eq!( assert_eq!(
block_producer.poll(), block_proposer.poll(),
Ok(PollOutcome::BlockProductionNotRequired(produce_slot - 1)) Ok(PollOutcome::BlockProductionNotRequired(produce_slot - 1))
); );
// On the produce slot... // On the produce slot...
slot_clock.set_slot(produce_slot.as_u64()); slot_clock.set_slot(produce_slot.as_u64());
assert_eq!( assert_eq!(
block_producer.poll(), block_proposer.poll(),
Ok(PollOutcome::BlockProduced(produce_slot.into())) Ok(PollOutcome::BlockProduced(produce_slot.into()))
); );
// Trying the same produce slot again... // Trying the same produce slot again...
slot_clock.set_slot(produce_slot.as_u64()); slot_clock.set_slot(produce_slot.as_u64());
assert_eq!( assert_eq!(
block_producer.poll(), block_proposer.poll(),
Ok(PollOutcome::SlotAlreadyProcessed(produce_slot)) Ok(PollOutcome::SlotAlreadyProcessed(produce_slot))
); );
// One slot after the produce slot... // One slot after the produce slot...
slot_clock.set_slot(produce_slot.as_u64() + 1); slot_clock.set_slot(produce_slot.as_u64() + 1);
assert_eq!( assert_eq!(
block_producer.poll(), block_proposer.poll(),
Ok(PollOutcome::BlockProductionNotRequired(produce_slot + 1)) Ok(PollOutcome::BlockProductionNotRequired(produce_slot + 1))
); );
@ -283,7 +283,7 @@ mod tests {
let slot = (produce_epoch.as_u64() + 1) * spec.epoch_length; let slot = (produce_epoch.as_u64() + 1) * spec.epoch_length;
slot_clock.set_slot(slot); slot_clock.set_slot(slot);
assert_eq!( assert_eq!(
block_producer.poll(), block_proposer.poll(),
Ok(PollOutcome::ProducerDutiesUnknown(Slot::new(slot))) Ok(PollOutcome::ProducerDutiesUnknown(Slot::new(slot)))
); );
} }

View File

@ -18,6 +18,7 @@ serde_derive = "1.0"
serde_json = "1.0" serde_json = "1.0"
slog = "^2.2.3" slog = "^2.2.3"
ssz = { path = "../utils/ssz" } ssz = { path = "../utils/ssz" }
ssz_derive = { path = "../utils/ssz_derive" }
swap_or_not_shuffle = { path = "../utils/swap_or_not_shuffle" } swap_or_not_shuffle = { path = "../utils/swap_or_not_shuffle" }
[dev-dependencies] [dev-dependencies]

View File

@ -2,9 +2,10 @@ use super::{AggregatePublicKey, AggregateSignature, AttestationData, Bitfield, H
use crate::test_utils::TestRandom; use crate::test_utils::TestRandom;
use rand::RngCore; use rand::RngCore;
use serde_derive::Serialize; use serde_derive::Serialize;
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; use ssz::{hash, TreeHash};
use ssz_derive::{Decode, Encode};
#[derive(Debug, Clone, PartialEq, Serialize)] #[derive(Debug, Clone, PartialEq, Serialize, Encode, Decode)]
pub struct Attestation { pub struct Attestation {
pub aggregation_bitfield: Bitfield, pub aggregation_bitfield: Bitfield,
pub data: AttestationData, pub data: AttestationData,
@ -35,39 +36,13 @@ impl Attestation {
} }
} }
impl Encodable for Attestation {
fn ssz_append(&self, s: &mut SszStream) {
s.append(&self.aggregation_bitfield);
s.append(&self.data);
s.append(&self.custody_bitfield);
s.append(&self.aggregate_signature);
}
}
impl Decodable for Attestation {
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
let (aggregation_bitfield, i) = Bitfield::ssz_decode(bytes, i)?;
let (data, i) = AttestationData::ssz_decode(bytes, i)?;
let (custody_bitfield, i) = Bitfield::ssz_decode(bytes, i)?;
let (aggregate_signature, i) = AggregateSignature::ssz_decode(bytes, i)?;
let attestation_record = Self {
aggregation_bitfield,
data,
custody_bitfield,
aggregate_signature,
};
Ok((attestation_record, i))
}
}
impl TreeHash for Attestation { impl TreeHash for Attestation {
fn hash_tree_root(&self) -> Vec<u8> { fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![]; let mut result: Vec<u8> = vec![];
result.append(&mut self.aggregation_bitfield.hash_tree_root()); result.append(&mut self.aggregation_bitfield.hash_tree_root_internal());
result.append(&mut self.data.hash_tree_root()); result.append(&mut self.data.hash_tree_root_internal());
result.append(&mut self.custody_bitfield.hash_tree_root()); result.append(&mut self.custody_bitfield.hash_tree_root_internal());
result.append(&mut self.aggregate_signature.hash_tree_root()); result.append(&mut self.aggregate_signature.hash_tree_root_internal());
hash(&result) hash(&result)
} }
} }
@ -87,7 +62,7 @@ impl<T: RngCore> TestRandom<T> for Attestation {
mod tests { mod tests {
use super::*; use super::*;
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
use ssz::ssz_encode; use ssz::{ssz_encode, Decodable};
#[test] #[test]
pub fn test_ssz_round_trip() { pub fn test_ssz_round_trip() {
@ -101,11 +76,11 @@ mod tests {
} }
#[test] #[test]
pub fn test_hash_tree_root() { pub fn test_hash_tree_root_internal() {
let mut rng = XorShiftRng::from_seed([42; 16]); let mut rng = XorShiftRng::from_seed([42; 16]);
let original = Attestation::random_for_test(&mut rng); let original = Attestation::random_for_test(&mut rng);
let result = original.hash_tree_root(); let result = original.hash_tree_root_internal();
assert_eq!(result.len(), 32); assert_eq!(result.len(), 32);
// TODO: Add further tests // TODO: Add further tests

View File

@ -2,7 +2,8 @@ use crate::test_utils::TestRandom;
use crate::{AttestationDataAndCustodyBit, Crosslink, Epoch, Hash256, Slot}; use crate::{AttestationDataAndCustodyBit, Crosslink, Epoch, Hash256, Slot};
use rand::RngCore; use rand::RngCore;
use serde_derive::Serialize; use serde_derive::Serialize;
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; use ssz::{hash, TreeHash};
use ssz_derive::{Decode, Encode};
pub const SSZ_ATTESTION_DATA_LENGTH: usize = { pub const SSZ_ATTESTION_DATA_LENGTH: usize = {
8 + // slot 8 + // slot
@ -15,7 +16,7 @@ pub const SSZ_ATTESTION_DATA_LENGTH: usize = {
32 // justified_block_root 32 // justified_block_root
}; };
#[derive(Debug, Clone, PartialEq, Default, Serialize, Hash)] #[derive(Debug, Clone, PartialEq, Default, Serialize, Hash, Encode, Decode)]
pub struct AttestationData { pub struct AttestationData {
pub slot: Slot, pub slot: Slot,
pub shard: u64, pub shard: u64,
@ -43,55 +44,17 @@ impl AttestationData {
} }
} }
impl Encodable for AttestationData {
fn ssz_append(&self, s: &mut SszStream) {
s.append(&self.slot);
s.append(&self.shard);
s.append(&self.beacon_block_root);
s.append(&self.epoch_boundary_root);
s.append(&self.shard_block_root);
s.append(&self.latest_crosslink);
s.append(&self.justified_epoch);
s.append(&self.justified_block_root);
}
}
impl Decodable for AttestationData {
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
let (slot, i) = <_>::ssz_decode(bytes, i)?;
let (shard, i) = <_>::ssz_decode(bytes, i)?;
let (beacon_block_root, i) = <_>::ssz_decode(bytes, i)?;
let (epoch_boundary_root, i) = <_>::ssz_decode(bytes, i)?;
let (shard_block_root, i) = <_>::ssz_decode(bytes, i)?;
let (latest_crosslink, i) = <_>::ssz_decode(bytes, i)?;
let (justified_epoch, i) = <_>::ssz_decode(bytes, i)?;
let (justified_block_root, i) = <_>::ssz_decode(bytes, i)?;
let attestation_data = AttestationData {
slot,
shard,
beacon_block_root,
epoch_boundary_root,
shard_block_root,
latest_crosslink,
justified_epoch,
justified_block_root,
};
Ok((attestation_data, i))
}
}
impl TreeHash for AttestationData { impl TreeHash for AttestationData {
fn hash_tree_root(&self) -> Vec<u8> { fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![]; let mut result: Vec<u8> = vec![];
result.append(&mut self.slot.hash_tree_root()); result.append(&mut self.slot.hash_tree_root_internal());
result.append(&mut self.shard.hash_tree_root()); result.append(&mut self.shard.hash_tree_root_internal());
result.append(&mut self.beacon_block_root.hash_tree_root()); result.append(&mut self.beacon_block_root.hash_tree_root_internal());
result.append(&mut self.epoch_boundary_root.hash_tree_root()); result.append(&mut self.epoch_boundary_root.hash_tree_root_internal());
result.append(&mut self.shard_block_root.hash_tree_root()); result.append(&mut self.shard_block_root.hash_tree_root_internal());
result.append(&mut self.latest_crosslink.hash_tree_root()); result.append(&mut self.latest_crosslink.hash_tree_root_internal());
result.append(&mut self.justified_epoch.hash_tree_root()); result.append(&mut self.justified_epoch.hash_tree_root_internal());
result.append(&mut self.justified_block_root.hash_tree_root()); result.append(&mut self.justified_block_root.hash_tree_root_internal());
hash(&result) hash(&result)
} }
} }
@ -115,7 +78,7 @@ impl<T: RngCore> TestRandom<T> for AttestationData {
mod tests { mod tests {
use super::*; use super::*;
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
use ssz::ssz_encode; use ssz::{ssz_encode, Decodable};
#[test] #[test]
pub fn test_ssz_round_trip() { pub fn test_ssz_round_trip() {
@ -129,11 +92,11 @@ mod tests {
} }
#[test] #[test]
pub fn test_hash_tree_root() { pub fn test_hash_tree_root_internal() {
let mut rng = XorShiftRng::from_seed([42; 16]); let mut rng = XorShiftRng::from_seed([42; 16]);
let original = AttestationData::random_for_test(&mut rng); let original = AttestationData::random_for_test(&mut rng);
let result = original.hash_tree_root(); let result = original.hash_tree_root_internal();
assert_eq!(result.len(), 32); assert_eq!(result.len(), 32);
// TODO: Add further tests // TODO: Add further tests

View File

@ -2,38 +2,21 @@ use super::AttestationData;
use crate::test_utils::TestRandom; use crate::test_utils::TestRandom;
use rand::RngCore; use rand::RngCore;
use serde_derive::Serialize; use serde_derive::Serialize;
use ssz::{Decodable, DecodeError, Encodable, SszStream, TreeHash}; use ssz::TreeHash;
use ssz_derive::{Decode, Encode};
#[derive(Debug, Clone, PartialEq, Default, Serialize)] #[derive(Debug, Clone, PartialEq, Default, Serialize, Encode, Decode)]
pub struct AttestationDataAndCustodyBit { pub struct AttestationDataAndCustodyBit {
pub data: AttestationData, pub data: AttestationData,
pub custody_bit: bool, pub custody_bit: bool,
} }
impl Encodable for AttestationDataAndCustodyBit {
fn ssz_append(&self, s: &mut SszStream) {
s.append(&self.data);
// TODO: deal with bools
}
}
impl Decodable for AttestationDataAndCustodyBit {
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
let (data, i) = <_>::ssz_decode(bytes, i)?;
let custody_bit = false;
let attestation_data_and_custody_bit = AttestationDataAndCustodyBit { data, custody_bit };
Ok((attestation_data_and_custody_bit, i))
}
}
impl TreeHash for AttestationDataAndCustodyBit { impl TreeHash for AttestationDataAndCustodyBit {
fn hash_tree_root(&self) -> Vec<u8> { fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![]; let mut result: Vec<u8> = vec![];
result.append(&mut self.data.hash_tree_root()); result.append(&mut self.data.hash_tree_root_internal());
// TODO: add bool ssz // TODO: add bool ssz
// result.append(custody_bit.hash_tree_root()); // result.append(custody_bit.hash_tree_root_internal());
ssz::hash(&result) ssz::hash(&result)
} }
} }
@ -52,7 +35,7 @@ impl<T: RngCore> TestRandom<T> for AttestationDataAndCustodyBit {
mod test { mod test {
use super::*; use super::*;
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
use ssz::ssz_encode; use ssz::{ssz_encode, Decodable};
#[test] #[test]
pub fn test_ssz_round_trip() { pub fn test_ssz_round_trip() {
@ -68,11 +51,11 @@ mod test {
} }
#[test] #[test]
pub fn test_hash_tree_root() { pub fn test_hash_tree_root_internal() {
let mut rng = XorShiftRng::from_seed([42; 16]); let mut rng = XorShiftRng::from_seed([42; 16]);
let original = AttestationDataAndCustodyBit::random_for_test(&mut rng); let original = AttestationDataAndCustodyBit::random_for_test(&mut rng);
let result = original.hash_tree_root(); let result = original.hash_tree_root_internal();
assert_eq!(result.len(), 32); assert_eq!(result.len(), 32);
// TODO: Add further tests // TODO: Add further tests

View File

@ -1,41 +1,20 @@
use crate::{test_utils::TestRandom, SlashableAttestation}; use crate::{test_utils::TestRandom, SlashableAttestation};
use rand::RngCore; use rand::RngCore;
use serde_derive::Serialize; use serde_derive::Serialize;
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; use ssz::{hash, TreeHash};
use ssz_derive::{Decode, Encode};
#[derive(Debug, PartialEq, Clone, Serialize)] #[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode)]
pub struct AttesterSlashing { pub struct AttesterSlashing {
pub slashable_attestation_1: SlashableAttestation, pub slashable_attestation_1: SlashableAttestation,
pub slashable_attestation_2: SlashableAttestation, pub slashable_attestation_2: SlashableAttestation,
} }
impl Encodable for AttesterSlashing {
fn ssz_append(&self, s: &mut SszStream) {
s.append(&self.slashable_attestation_1);
s.append(&self.slashable_attestation_2);
}
}
impl Decodable for AttesterSlashing {
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
let (slashable_attestation_1, i) = <_>::ssz_decode(bytes, i)?;
let (slashable_attestation_2, i) = <_>::ssz_decode(bytes, i)?;
Ok((
AttesterSlashing {
slashable_attestation_1,
slashable_attestation_2,
},
i,
))
}
}
impl TreeHash for AttesterSlashing { impl TreeHash for AttesterSlashing {
fn hash_tree_root(&self) -> Vec<u8> { fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![]; let mut result: Vec<u8> = vec![];
result.append(&mut self.slashable_attestation_1.hash_tree_root()); result.append(&mut self.slashable_attestation_1.hash_tree_root_internal());
result.append(&mut self.slashable_attestation_2.hash_tree_root()); result.append(&mut self.slashable_attestation_2.hash_tree_root_internal());
hash(&result) hash(&result)
} }
} }
@ -53,7 +32,7 @@ impl<T: RngCore> TestRandom<T> for AttesterSlashing {
mod tests { mod tests {
use super::*; use super::*;
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
use ssz::ssz_encode; use ssz::{ssz_encode, Decodable};
#[test] #[test]
pub fn test_ssz_round_trip() { pub fn test_ssz_round_trip() {
@ -67,11 +46,11 @@ mod tests {
} }
#[test] #[test]
pub fn test_hash_tree_root() { pub fn test_hash_tree_root_internal() {
let mut rng = XorShiftRng::from_seed([42; 16]); let mut rng = XorShiftRng::from_seed([42; 16]);
let original = AttesterSlashing::random_for_test(&mut rng); let original = AttesterSlashing::random_for_test(&mut rng);
let result = original.hash_tree_root(); let result = original.hash_tree_root_internal();
assert_eq!(result.len(), 32); assert_eq!(result.len(), 32);
// TODO: Add further tests // TODO: Add further tests

View File

@ -3,9 +3,10 @@ use crate::{BeaconBlockBody, ChainSpec, Eth1Data, Hash256, ProposalSignedData, S
use bls::Signature; use bls::Signature;
use rand::RngCore; use rand::RngCore;
use serde_derive::Serialize; use serde_derive::Serialize;
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; use ssz::{hash, TreeHash};
use ssz_derive::{Decode, Encode};
#[derive(Debug, PartialEq, Clone, Serialize)] #[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode)]
pub struct BeaconBlock { pub struct BeaconBlock {
pub slot: Slot, pub slot: Slot,
pub parent_root: Hash256, pub parent_root: Hash256,
@ -59,53 +60,16 @@ impl BeaconBlock {
} }
} }
impl Encodable for BeaconBlock {
fn ssz_append(&self, s: &mut SszStream) {
s.append(&self.slot);
s.append(&self.parent_root);
s.append(&self.state_root);
s.append(&self.randao_reveal);
s.append(&self.eth1_data);
s.append(&self.signature);
s.append(&self.body);
}
}
impl Decodable for BeaconBlock {
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
let (slot, i) = <_>::ssz_decode(bytes, i)?;
let (parent_root, i) = <_>::ssz_decode(bytes, i)?;
let (state_root, i) = <_>::ssz_decode(bytes, i)?;
let (randao_reveal, i) = <_>::ssz_decode(bytes, i)?;
let (eth1_data, i) = <_>::ssz_decode(bytes, i)?;
let (signature, i) = <_>::ssz_decode(bytes, i)?;
let (body, i) = <_>::ssz_decode(bytes, i)?;
Ok((
Self {
slot,
parent_root,
state_root,
randao_reveal,
eth1_data,
signature,
body,
},
i,
))
}
}
impl TreeHash for BeaconBlock { impl TreeHash for BeaconBlock {
fn hash_tree_root(&self) -> Vec<u8> { fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![]; let mut result: Vec<u8> = vec![];
result.append(&mut self.slot.hash_tree_root()); result.append(&mut self.slot.hash_tree_root_internal());
result.append(&mut self.parent_root.hash_tree_root()); result.append(&mut self.parent_root.hash_tree_root_internal());
result.append(&mut self.state_root.hash_tree_root()); result.append(&mut self.state_root.hash_tree_root_internal());
result.append(&mut self.randao_reveal.hash_tree_root()); result.append(&mut self.randao_reveal.hash_tree_root_internal());
result.append(&mut self.eth1_data.hash_tree_root()); result.append(&mut self.eth1_data.hash_tree_root_internal());
result.append(&mut self.signature.hash_tree_root()); result.append(&mut self.signature.hash_tree_root_internal());
result.append(&mut self.body.hash_tree_root()); result.append(&mut self.body.hash_tree_root_internal());
hash(&result) hash(&result)
} }
} }
@ -128,7 +92,7 @@ impl<T: RngCore> TestRandom<T> for BeaconBlock {
mod tests { mod tests {
use super::*; use super::*;
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
use ssz::ssz_encode; use ssz::{ssz_encode, Decodable};
#[test] #[test]
pub fn test_ssz_round_trip() { pub fn test_ssz_round_trip() {
@ -142,11 +106,11 @@ mod tests {
} }
#[test] #[test]
pub fn test_hash_tree_root() { pub fn test_hash_tree_root_internal() {
let mut rng = XorShiftRng::from_seed([42; 16]); let mut rng = XorShiftRng::from_seed([42; 16]);
let original = BeaconBlock::random_for_test(&mut rng); let original = BeaconBlock::random_for_test(&mut rng);
let result = original.hash_tree_root(); let result = original.hash_tree_root_internal();
assert_eq!(result.len(), 32); assert_eq!(result.len(), 32);
// TODO: Add further tests // TODO: Add further tests

View File

@ -2,9 +2,10 @@ use super::{Attestation, AttesterSlashing, Deposit, Exit, ProposerSlashing};
use crate::test_utils::TestRandom; use crate::test_utils::TestRandom;
use rand::RngCore; use rand::RngCore;
use serde_derive::Serialize; use serde_derive::Serialize;
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; use ssz::{hash, TreeHash};
use ssz_derive::{Decode, Encode};
#[derive(Debug, PartialEq, Clone, Default, Serialize)] #[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode)]
pub struct BeaconBlockBody { pub struct BeaconBlockBody {
pub proposer_slashings: Vec<ProposerSlashing>, pub proposer_slashings: Vec<ProposerSlashing>,
pub attester_slashings: Vec<AttesterSlashing>, pub attester_slashings: Vec<AttesterSlashing>,
@ -13,45 +14,14 @@ pub struct BeaconBlockBody {
pub exits: Vec<Exit>, pub exits: Vec<Exit>,
} }
impl Encodable for BeaconBlockBody {
fn ssz_append(&self, s: &mut SszStream) {
s.append_vec(&self.proposer_slashings);
s.append_vec(&self.attester_slashings);
s.append_vec(&self.attestations);
s.append_vec(&self.deposits);
s.append_vec(&self.exits);
}
}
impl Decodable for BeaconBlockBody {
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
let (proposer_slashings, i) = <_>::ssz_decode(bytes, i)?;
let (attester_slashings, i) = <_>::ssz_decode(bytes, i)?;
let (attestations, i) = <_>::ssz_decode(bytes, i)?;
let (deposits, i) = <_>::ssz_decode(bytes, i)?;
let (exits, i) = <_>::ssz_decode(bytes, i)?;
Ok((
Self {
proposer_slashings,
attester_slashings,
attestations,
deposits,
exits,
},
i,
))
}
}
impl TreeHash for BeaconBlockBody { impl TreeHash for BeaconBlockBody {
fn hash_tree_root(&self) -> Vec<u8> { fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![]; let mut result: Vec<u8> = vec![];
result.append(&mut self.proposer_slashings.hash_tree_root()); result.append(&mut self.proposer_slashings.hash_tree_root_internal());
result.append(&mut self.attester_slashings.hash_tree_root()); result.append(&mut self.attester_slashings.hash_tree_root_internal());
result.append(&mut self.attestations.hash_tree_root()); result.append(&mut self.attestations.hash_tree_root_internal());
result.append(&mut self.deposits.hash_tree_root()); result.append(&mut self.deposits.hash_tree_root_internal());
result.append(&mut self.exits.hash_tree_root()); result.append(&mut self.exits.hash_tree_root_internal());
hash(&result) hash(&result)
} }
} }
@ -72,7 +42,7 @@ impl<T: RngCore> TestRandom<T> for BeaconBlockBody {
mod tests { mod tests {
use super::*; use super::*;
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
use ssz::ssz_encode; use ssz::{ssz_encode, Decodable};
#[test] #[test]
pub fn test_ssz_round_trip() { pub fn test_ssz_round_trip() {
@ -86,11 +56,11 @@ mod tests {
} }
#[test] #[test]
pub fn test_hash_tree_root() { pub fn test_hash_tree_root_internal() {
let mut rng = XorShiftRng::from_seed([42; 16]); let mut rng = XorShiftRng::from_seed([42; 16]);
let original = BeaconBlockBody::random_for_test(&mut rng); let original = BeaconBlockBody::random_for_test(&mut rng);
let result = original.hash_tree_root(); let result = original.hash_tree_root_internal();
assert_eq!(result.len(), 32); assert_eq!(result.len(), 32);
// TODO: Add further tests // TODO: Add further tests

View File

@ -9,7 +9,8 @@ use honey_badger_split::SplitExt;
use log::trace; use log::trace;
use rand::RngCore; use rand::RngCore;
use serde_derive::Serialize; use serde_derive::Serialize;
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; use ssz::{hash, TreeHash};
use ssz_derive::{Decode, Encode};
use swap_or_not_shuffle::get_permutated_index; use swap_or_not_shuffle::get_permutated_index;
mod tests; mod tests;
@ -51,7 +52,7 @@ macro_rules! safe_sub_assign {
}; };
} }
#[derive(Debug, PartialEq, Clone, Default, Serialize)] #[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode)]
pub struct BeaconState { pub struct BeaconState {
// Misc // Misc
pub slot: Slot, pub slot: Slot,
@ -1000,125 +1001,38 @@ impl From<AttestationParticipantsError> for InclusionError {
} }
} }
impl Encodable for BeaconState {
fn ssz_append(&self, s: &mut SszStream) {
s.append(&self.slot);
s.append(&self.genesis_time);
s.append(&self.fork);
s.append(&self.validator_registry);
s.append(&self.validator_balances);
s.append(&self.validator_registry_update_epoch);
s.append(&self.latest_randao_mixes);
s.append(&self.previous_epoch_start_shard);
s.append(&self.current_epoch_start_shard);
s.append(&self.previous_calculation_epoch);
s.append(&self.current_calculation_epoch);
s.append(&self.previous_epoch_seed);
s.append(&self.current_epoch_seed);
s.append(&self.previous_justified_epoch);
s.append(&self.justified_epoch);
s.append(&self.justification_bitfield);
s.append(&self.finalized_epoch);
s.append(&self.latest_crosslinks);
s.append(&self.latest_block_roots);
s.append(&self.latest_index_roots);
s.append(&self.latest_penalized_balances);
s.append(&self.latest_attestations);
s.append(&self.batched_block_roots);
s.append(&self.latest_eth1_data);
s.append(&self.eth1_data_votes);
}
}
impl Decodable for BeaconState {
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
let (slot, i) = <_>::ssz_decode(bytes, i)?;
let (genesis_time, i) = <_>::ssz_decode(bytes, i)?;
let (fork, i) = <_>::ssz_decode(bytes, i)?;
let (validator_registry, i) = <_>::ssz_decode(bytes, i)?;
let (validator_balances, i) = <_>::ssz_decode(bytes, i)?;
let (validator_registry_update_epoch, i) = <_>::ssz_decode(bytes, i)?;
let (latest_randao_mixes, i) = <_>::ssz_decode(bytes, i)?;
let (previous_epoch_start_shard, i) = <_>::ssz_decode(bytes, i)?;
let (current_epoch_start_shard, i) = <_>::ssz_decode(bytes, i)?;
let (previous_calculation_epoch, i) = <_>::ssz_decode(bytes, i)?;
let (current_calculation_epoch, i) = <_>::ssz_decode(bytes, i)?;
let (previous_epoch_seed, i) = <_>::ssz_decode(bytes, i)?;
let (current_epoch_seed, i) = <_>::ssz_decode(bytes, i)?;
let (previous_justified_epoch, i) = <_>::ssz_decode(bytes, i)?;
let (justified_epoch, i) = <_>::ssz_decode(bytes, i)?;
let (justification_bitfield, i) = <_>::ssz_decode(bytes, i)?;
let (finalized_epoch, i) = <_>::ssz_decode(bytes, i)?;
let (latest_crosslinks, i) = <_>::ssz_decode(bytes, i)?;
let (latest_block_roots, i) = <_>::ssz_decode(bytes, i)?;
let (latest_index_roots, i) = <_>::ssz_decode(bytes, i)?;
let (latest_penalized_balances, i) = <_>::ssz_decode(bytes, i)?;
let (latest_attestations, i) = <_>::ssz_decode(bytes, i)?;
let (batched_block_roots, i) = <_>::ssz_decode(bytes, i)?;
let (latest_eth1_data, i) = <_>::ssz_decode(bytes, i)?;
let (eth1_data_votes, i) = <_>::ssz_decode(bytes, i)?;
Ok((
Self {
slot,
genesis_time,
fork,
validator_registry,
validator_balances,
validator_registry_update_epoch,
latest_randao_mixes,
previous_epoch_start_shard,
current_epoch_start_shard,
previous_calculation_epoch,
current_calculation_epoch,
previous_epoch_seed,
current_epoch_seed,
previous_justified_epoch,
justified_epoch,
justification_bitfield,
finalized_epoch,
latest_crosslinks,
latest_block_roots,
latest_index_roots,
latest_penalized_balances,
latest_attestations,
batched_block_roots,
latest_eth1_data,
eth1_data_votes,
},
i,
))
}
}
impl TreeHash for BeaconState { impl TreeHash for BeaconState {
fn hash_tree_root(&self) -> Vec<u8> { fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![]; let mut result: Vec<u8> = vec![];
result.append(&mut self.slot.hash_tree_root()); result.append(&mut self.slot.hash_tree_root_internal());
result.append(&mut self.genesis_time.hash_tree_root()); result.append(&mut self.genesis_time.hash_tree_root_internal());
result.append(&mut self.fork.hash_tree_root()); result.append(&mut self.fork.hash_tree_root_internal());
result.append(&mut self.validator_registry.hash_tree_root()); result.append(&mut self.validator_registry.hash_tree_root_internal());
result.append(&mut self.validator_balances.hash_tree_root()); result.append(&mut self.validator_balances.hash_tree_root_internal());
result.append(&mut self.validator_registry_update_epoch.hash_tree_root()); result.append(
result.append(&mut self.latest_randao_mixes.hash_tree_root()); &mut self
result.append(&mut self.previous_epoch_start_shard.hash_tree_root()); .validator_registry_update_epoch
result.append(&mut self.current_epoch_start_shard.hash_tree_root()); .hash_tree_root_internal(),
result.append(&mut self.previous_calculation_epoch.hash_tree_root()); );
result.append(&mut self.current_calculation_epoch.hash_tree_root()); result.append(&mut self.latest_randao_mixes.hash_tree_root_internal());
result.append(&mut self.previous_epoch_seed.hash_tree_root()); result.append(&mut self.previous_epoch_start_shard.hash_tree_root_internal());
result.append(&mut self.current_epoch_seed.hash_tree_root()); result.append(&mut self.current_epoch_start_shard.hash_tree_root_internal());
result.append(&mut self.previous_justified_epoch.hash_tree_root()); result.append(&mut self.previous_calculation_epoch.hash_tree_root_internal());
result.append(&mut self.justified_epoch.hash_tree_root()); result.append(&mut self.current_calculation_epoch.hash_tree_root_internal());
result.append(&mut self.justification_bitfield.hash_tree_root()); result.append(&mut self.previous_epoch_seed.hash_tree_root_internal());
result.append(&mut self.finalized_epoch.hash_tree_root()); result.append(&mut self.current_epoch_seed.hash_tree_root_internal());
result.append(&mut self.latest_crosslinks.hash_tree_root()); result.append(&mut self.previous_justified_epoch.hash_tree_root_internal());
result.append(&mut self.latest_block_roots.hash_tree_root()); result.append(&mut self.justified_epoch.hash_tree_root_internal());
result.append(&mut self.latest_index_roots.hash_tree_root()); result.append(&mut self.justification_bitfield.hash_tree_root_internal());
result.append(&mut self.latest_penalized_balances.hash_tree_root()); result.append(&mut self.finalized_epoch.hash_tree_root_internal());
result.append(&mut self.latest_attestations.hash_tree_root()); result.append(&mut self.latest_crosslinks.hash_tree_root_internal());
result.append(&mut self.batched_block_roots.hash_tree_root()); result.append(&mut self.latest_block_roots.hash_tree_root_internal());
result.append(&mut self.latest_eth1_data.hash_tree_root()); result.append(&mut self.latest_index_roots.hash_tree_root_internal());
result.append(&mut self.eth1_data_votes.hash_tree_root()); result.append(&mut self.latest_penalized_balances.hash_tree_root_internal());
result.append(&mut self.latest_attestations.hash_tree_root_internal());
result.append(&mut self.batched_block_roots.hash_tree_root_internal());
result.append(&mut self.latest_eth1_data.hash_tree_root_internal());
result.append(&mut self.eth1_data_votes.hash_tree_root_internal());
hash(&result) hash(&result)
} }
} }

View File

@ -7,7 +7,7 @@ use crate::{
Eth1Data, Hash256, Keypair, Eth1Data, Hash256, Keypair,
}; };
use bls::create_proof_of_possession; use bls::create_proof_of_possession;
use ssz::ssz_encode; use ssz::{ssz_encode, Decodable};
struct BeaconStateTestBuilder { struct BeaconStateTestBuilder {
pub genesis_time: u64, pub genesis_time: u64,
@ -85,11 +85,11 @@ pub fn test_ssz_round_trip() {
} }
#[test] #[test]
pub fn test_hash_tree_root() { pub fn test_hash_tree_root_internal() {
let mut rng = XorShiftRng::from_seed([42; 16]); let mut rng = XorShiftRng::from_seed([42; 16]);
let original = BeaconState::random_for_test(&mut rng); let original = BeaconState::random_for_test(&mut rng);
let result = original.hash_tree_root(); let result = original.hash_tree_root_internal();
assert_eq!(result.len(), 32); assert_eq!(result.len(), 32);
// TODO: Add further tests // TODO: Add further tests

View File

@ -2,41 +2,20 @@ use super::SlashableVoteData;
use crate::test_utils::TestRandom; use crate::test_utils::TestRandom;
use rand::RngCore; use rand::RngCore;
use serde_derive::Serialize; use serde_derive::Serialize;
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; use ssz::{hash, TreeHash};
use ssz_derive::{Decode, Encode};
#[derive(Debug, PartialEq, Clone, Serialize)] #[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode)]
pub struct CasperSlashing { pub struct CasperSlashing {
pub slashable_vote_data_1: SlashableVoteData, pub slashable_vote_data_1: SlashableVoteData,
pub slashable_vote_data_2: SlashableVoteData, pub slashable_vote_data_2: SlashableVoteData,
} }
impl Encodable for CasperSlashing {
fn ssz_append(&self, s: &mut SszStream) {
s.append(&self.slashable_vote_data_1);
s.append(&self.slashable_vote_data_2);
}
}
impl Decodable for CasperSlashing {
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
let (slashable_vote_data_1, i) = <_>::ssz_decode(bytes, i)?;
let (slashable_vote_data_2, i) = <_>::ssz_decode(bytes, i)?;
Ok((
CasperSlashing {
slashable_vote_data_1,
slashable_vote_data_2,
},
i,
))
}
}
impl TreeHash for CasperSlashing { impl TreeHash for CasperSlashing {
fn hash_tree_root(&self) -> Vec<u8> { fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![]; let mut result: Vec<u8> = vec![];
result.append(&mut self.slashable_vote_data_1.hash_tree_root()); result.append(&mut self.slashable_vote_data_1.hash_tree_root_internal());
result.append(&mut self.slashable_vote_data_2.hash_tree_root()); result.append(&mut self.slashable_vote_data_2.hash_tree_root_internal());
hash(&result) hash(&result)
} }
} }
@ -54,7 +33,7 @@ impl<T: RngCore> TestRandom<T> for CasperSlashing {
mod tests { mod tests {
use super::*; use super::*;
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
use ssz::ssz_encode; use ssz::{ssz_encode, Decodable};
#[test] #[test]
pub fn test_ssz_round_trip() { pub fn test_ssz_round_trip() {
@ -68,11 +47,11 @@ mod tests {
} }
#[test] #[test]
pub fn test_hash_tree_root() { pub fn test_hash_tree_root_internal() {
let mut rng = XorShiftRng::from_seed([42; 16]); let mut rng = XorShiftRng::from_seed([42; 16]);
let original = CasperSlashing::random_for_test(&mut rng); let original = CasperSlashing::random_for_test(&mut rng);
let result = original.hash_tree_root(); let result = original.hash_tree_root_internal();
assert_eq!(result.len(), 32); assert_eq!(result.len(), 32);
// TODO: Add further tests // TODO: Add further tests

View File

@ -2,9 +2,10 @@ use crate::test_utils::TestRandom;
use crate::{Epoch, Hash256}; use crate::{Epoch, Hash256};
use rand::RngCore; use rand::RngCore;
use serde_derive::Serialize; use serde_derive::Serialize;
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; use ssz::{hash, TreeHash};
use ssz_derive::{Decode, Encode};
#[derive(Debug, Clone, PartialEq, Default, Serialize, Hash)] #[derive(Debug, Clone, PartialEq, Default, Serialize, Hash, Encode, Decode)]
pub struct Crosslink { pub struct Crosslink {
pub epoch: Epoch, pub epoch: Epoch,
pub shard_block_root: Hash256, pub shard_block_root: Hash256,
@ -20,33 +21,11 @@ impl Crosslink {
} }
} }
impl Encodable for Crosslink {
fn ssz_append(&self, s: &mut SszStream) {
s.append(&self.epoch);
s.append(&self.shard_block_root);
}
}
impl Decodable for Crosslink {
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
let (epoch, i) = <_>::ssz_decode(bytes, i)?;
let (shard_block_root, i) = <_>::ssz_decode(bytes, i)?;
Ok((
Self {
epoch,
shard_block_root,
},
i,
))
}
}
impl TreeHash for Crosslink { impl TreeHash for Crosslink {
fn hash_tree_root(&self) -> Vec<u8> { fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![]; let mut result: Vec<u8> = vec![];
result.append(&mut self.epoch.hash_tree_root()); result.append(&mut self.epoch.hash_tree_root_internal());
result.append(&mut self.shard_block_root.hash_tree_root()); result.append(&mut self.shard_block_root.hash_tree_root_internal());
hash(&result) hash(&result)
} }
} }
@ -64,7 +43,7 @@ impl<T: RngCore> TestRandom<T> for Crosslink {
mod tests { mod tests {
use super::*; use super::*;
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
use ssz::ssz_encode; use ssz::{ssz_encode, Decodable};
#[test] #[test]
pub fn test_ssz_round_trip() { pub fn test_ssz_round_trip() {
@ -78,11 +57,11 @@ mod tests {
} }
#[test] #[test]
pub fn test_hash_tree_root() { pub fn test_hash_tree_root_internal() {
let mut rng = XorShiftRng::from_seed([42; 16]); let mut rng = XorShiftRng::from_seed([42; 16]);
let original = Crosslink::random_for_test(&mut rng); let original = Crosslink::random_for_test(&mut rng);
let result = original.hash_tree_root(); let result = original.hash_tree_root_internal();
assert_eq!(result.len(), 32); assert_eq!(result.len(), 32);
// TODO: Add further tests // TODO: Add further tests

View File

@ -2,46 +2,22 @@ use super::{DepositData, Hash256};
use crate::test_utils::TestRandom; use crate::test_utils::TestRandom;
use rand::RngCore; use rand::RngCore;
use serde_derive::Serialize; use serde_derive::Serialize;
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; use ssz::{hash, TreeHash};
use ssz_derive::{Decode, Encode};
#[derive(Debug, PartialEq, Clone, Serialize)] #[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode)]
pub struct Deposit { pub struct Deposit {
pub branch: Vec<Hash256>, pub branch: Vec<Hash256>,
pub index: u64, pub index: u64,
pub deposit_data: DepositData, pub deposit_data: DepositData,
} }
impl Encodable for Deposit {
fn ssz_append(&self, s: &mut SszStream) {
s.append_vec(&self.branch);
s.append(&self.index);
s.append(&self.deposit_data);
}
}
impl Decodable for Deposit {
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
let (branch, i) = <_>::ssz_decode(bytes, i)?;
let (index, i) = <_>::ssz_decode(bytes, i)?;
let (deposit_data, i) = <_>::ssz_decode(bytes, i)?;
Ok((
Self {
branch,
index,
deposit_data,
},
i,
))
}
}
impl TreeHash for Deposit { impl TreeHash for Deposit {
fn hash_tree_root(&self) -> Vec<u8> { fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![]; let mut result: Vec<u8> = vec![];
result.append(&mut self.branch.hash_tree_root()); result.append(&mut self.branch.hash_tree_root_internal());
result.append(&mut self.index.hash_tree_root()); result.append(&mut self.index.hash_tree_root_internal());
result.append(&mut self.deposit_data.hash_tree_root()); result.append(&mut self.deposit_data.hash_tree_root_internal());
hash(&result) hash(&result)
} }
} }
@ -60,7 +36,7 @@ impl<T: RngCore> TestRandom<T> for Deposit {
mod tests { mod tests {
use super::*; use super::*;
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
use ssz::ssz_encode; use ssz::{ssz_encode, Decodable};
#[test] #[test]
pub fn test_ssz_round_trip() { pub fn test_ssz_round_trip() {
@ -74,11 +50,11 @@ mod tests {
} }
#[test] #[test]
pub fn test_hash_tree_root() { pub fn test_hash_tree_root_internal() {
let mut rng = XorShiftRng::from_seed([42; 16]); let mut rng = XorShiftRng::from_seed([42; 16]);
let original = Deposit::random_for_test(&mut rng); let original = Deposit::random_for_test(&mut rng);
let result = original.hash_tree_root(); let result = original.hash_tree_root_internal();
assert_eq!(result.len(), 32); assert_eq!(result.len(), 32);
// TODO: Add further tests // TODO: Add further tests

View File

@ -2,46 +2,22 @@ use super::DepositInput;
use crate::test_utils::TestRandom; use crate::test_utils::TestRandom;
use rand::RngCore; use rand::RngCore;
use serde_derive::Serialize; use serde_derive::Serialize;
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; use ssz::{hash, TreeHash};
use ssz_derive::{Decode, Encode};
#[derive(Debug, PartialEq, Clone, Serialize)] #[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode)]
pub struct DepositData { pub struct DepositData {
pub amount: u64, pub amount: u64,
pub timestamp: u64, pub timestamp: u64,
pub deposit_input: DepositInput, pub deposit_input: DepositInput,
} }
impl Encodable for DepositData {
fn ssz_append(&self, s: &mut SszStream) {
s.append(&self.amount);
s.append(&self.timestamp);
s.append(&self.deposit_input);
}
}
impl Decodable for DepositData {
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
let (amount, i) = <_>::ssz_decode(bytes, i)?;
let (timestamp, i) = <_>::ssz_decode(bytes, i)?;
let (deposit_input, i) = <_>::ssz_decode(bytes, i)?;
Ok((
Self {
amount,
timestamp,
deposit_input,
},
i,
))
}
}
impl TreeHash for DepositData { impl TreeHash for DepositData {
fn hash_tree_root(&self) -> Vec<u8> { fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![]; let mut result: Vec<u8> = vec![];
result.append(&mut self.amount.hash_tree_root()); result.append(&mut self.amount.hash_tree_root_internal());
result.append(&mut self.timestamp.hash_tree_root()); result.append(&mut self.timestamp.hash_tree_root_internal());
result.append(&mut self.deposit_input.hash_tree_root()); result.append(&mut self.deposit_input.hash_tree_root_internal());
hash(&result) hash(&result)
} }
} }
@ -60,7 +36,7 @@ impl<T: RngCore> TestRandom<T> for DepositData {
mod tests { mod tests {
use super::*; use super::*;
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
use ssz::ssz_encode; use ssz::{ssz_encode, Decodable};
#[test] #[test]
pub fn test_ssz_round_trip() { pub fn test_ssz_round_trip() {
@ -74,11 +50,11 @@ mod tests {
} }
#[test] #[test]
pub fn test_hash_tree_root() { pub fn test_hash_tree_root_internal() {
let mut rng = XorShiftRng::from_seed([42; 16]); let mut rng = XorShiftRng::from_seed([42; 16]);
let original = DepositData::random_for_test(&mut rng); let original = DepositData::random_for_test(&mut rng);
let result = original.hash_tree_root(); let result = original.hash_tree_root_internal();
assert_eq!(result.len(), 32); assert_eq!(result.len(), 32);
// TODO: Add further tests // TODO: Add further tests

View File

@ -3,46 +3,22 @@ use crate::test_utils::TestRandom;
use bls::{PublicKey, Signature}; use bls::{PublicKey, Signature};
use rand::RngCore; use rand::RngCore;
use serde_derive::Serialize; use serde_derive::Serialize;
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; use ssz::{hash, TreeHash};
use ssz_derive::{Decode, Encode};
#[derive(Debug, PartialEq, Clone, Serialize)] #[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode)]
pub struct DepositInput { pub struct DepositInput {
pub pubkey: PublicKey, pub pubkey: PublicKey,
pub withdrawal_credentials: Hash256, pub withdrawal_credentials: Hash256,
pub proof_of_possession: Signature, pub proof_of_possession: Signature,
} }
impl Encodable for DepositInput {
fn ssz_append(&self, s: &mut SszStream) {
s.append(&self.pubkey);
s.append(&self.withdrawal_credentials);
s.append(&self.proof_of_possession);
}
}
impl Decodable for DepositInput {
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
let (pubkey, i) = <_>::ssz_decode(bytes, i)?;
let (withdrawal_credentials, i) = <_>::ssz_decode(bytes, i)?;
let (proof_of_possession, i) = <_>::ssz_decode(bytes, i)?;
Ok((
Self {
pubkey,
withdrawal_credentials,
proof_of_possession,
},
i,
))
}
}
impl TreeHash for DepositInput { impl TreeHash for DepositInput {
fn hash_tree_root(&self) -> Vec<u8> { fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![]; let mut result: Vec<u8> = vec![];
result.append(&mut self.pubkey.hash_tree_root()); result.append(&mut self.pubkey.hash_tree_root_internal());
result.append(&mut self.withdrawal_credentials.hash_tree_root()); result.append(&mut self.withdrawal_credentials.hash_tree_root_internal());
result.append(&mut self.proof_of_possession.hash_tree_root()); result.append(&mut self.proof_of_possession.hash_tree_root_internal());
hash(&result) hash(&result)
} }
} }
@ -61,7 +37,7 @@ impl<T: RngCore> TestRandom<T> for DepositInput {
mod tests { mod tests {
use super::*; use super::*;
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
use ssz::ssz_encode; use ssz::{ssz_encode, Decodable};
#[test] #[test]
pub fn test_ssz_round_trip() { pub fn test_ssz_round_trip() {
@ -75,11 +51,11 @@ mod tests {
} }
#[test] #[test]
pub fn test_hash_tree_root() { pub fn test_hash_tree_root_internal() {
let mut rng = XorShiftRng::from_seed([42; 16]); let mut rng = XorShiftRng::from_seed([42; 16]);
let original = DepositInput::random_for_test(&mut rng); let original = DepositInput::random_for_test(&mut rng);
let result = original.hash_tree_root(); let result = original.hash_tree_root_internal();
assert_eq!(result.len(), 32); assert_eq!(result.len(), 32);
// TODO: Add further tests // TODO: Add further tests

View File

@ -2,42 +2,21 @@ use super::Hash256;
use crate::test_utils::TestRandom; use crate::test_utils::TestRandom;
use rand::RngCore; use rand::RngCore;
use serde_derive::Serialize; use serde_derive::Serialize;
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; use ssz::{hash, TreeHash};
use ssz_derive::{Decode, Encode};
// Note: this is refer to as DepositRootVote in specs // Note: this is refer to as DepositRootVote in specs
#[derive(Debug, PartialEq, Clone, Default, Serialize)] #[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode)]
pub struct Eth1Data { pub struct Eth1Data {
pub deposit_root: Hash256, pub deposit_root: Hash256,
pub block_hash: Hash256, pub block_hash: Hash256,
} }
impl Encodable for Eth1Data {
fn ssz_append(&self, s: &mut SszStream) {
s.append(&self.deposit_root);
s.append(&self.block_hash);
}
}
impl Decodable for Eth1Data {
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
let (deposit_root, i) = <_>::ssz_decode(bytes, i)?;
let (block_hash, i) = <_>::ssz_decode(bytes, i)?;
Ok((
Self {
deposit_root,
block_hash,
},
i,
))
}
}
impl TreeHash for Eth1Data { impl TreeHash for Eth1Data {
fn hash_tree_root(&self) -> Vec<u8> { fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![]; let mut result: Vec<u8> = vec![];
result.append(&mut self.deposit_root.hash_tree_root()); result.append(&mut self.deposit_root.hash_tree_root_internal());
result.append(&mut self.block_hash.hash_tree_root()); result.append(&mut self.block_hash.hash_tree_root_internal());
hash(&result) hash(&result)
} }
} }
@ -55,7 +34,7 @@ impl<T: RngCore> TestRandom<T> for Eth1Data {
mod tests { mod tests {
use super::*; use super::*;
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
use ssz::ssz_encode; use ssz::{ssz_encode, Decodable};
#[test] #[test]
pub fn test_ssz_round_trip() { pub fn test_ssz_round_trip() {
@ -69,11 +48,11 @@ mod tests {
} }
#[test] #[test]
pub fn test_hash_tree_root() { pub fn test_hash_tree_root_internal() {
let mut rng = XorShiftRng::from_seed([42; 16]); let mut rng = XorShiftRng::from_seed([42; 16]);
let original = Eth1Data::random_for_test(&mut rng); let original = Eth1Data::random_for_test(&mut rng);
let result = original.hash_tree_root(); let result = original.hash_tree_root_internal();
assert_eq!(result.len(), 32); assert_eq!(result.len(), 32);
// TODO: Add further tests // TODO: Add further tests

View File

@ -2,42 +2,21 @@ use super::Eth1Data;
use crate::test_utils::TestRandom; use crate::test_utils::TestRandom;
use rand::RngCore; use rand::RngCore;
use serde_derive::Serialize; use serde_derive::Serialize;
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; use ssz::{hash, TreeHash};
use ssz_derive::{Decode, Encode};
// Note: this is refer to as DepositRootVote in specs // Note: this is refer to as DepositRootVote in specs
#[derive(Debug, PartialEq, Clone, Default, Serialize)] #[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode)]
pub struct Eth1DataVote { pub struct Eth1DataVote {
pub eth1_data: Eth1Data, pub eth1_data: Eth1Data,
pub vote_count: u64, pub vote_count: u64,
} }
impl Encodable for Eth1DataVote {
fn ssz_append(&self, s: &mut SszStream) {
s.append(&self.eth1_data);
s.append(&self.vote_count);
}
}
impl Decodable for Eth1DataVote {
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
let (eth1_data, i) = <_>::ssz_decode(bytes, i)?;
let (vote_count, i) = <_>::ssz_decode(bytes, i)?;
Ok((
Self {
eth1_data,
vote_count,
},
i,
))
}
}
impl TreeHash for Eth1DataVote { impl TreeHash for Eth1DataVote {
fn hash_tree_root(&self) -> Vec<u8> { fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![]; let mut result: Vec<u8> = vec![];
result.append(&mut self.eth1_data.hash_tree_root()); result.append(&mut self.eth1_data.hash_tree_root_internal());
result.append(&mut self.vote_count.hash_tree_root()); result.append(&mut self.vote_count.hash_tree_root_internal());
hash(&result) hash(&result)
} }
} }
@ -55,7 +34,7 @@ impl<T: RngCore> TestRandom<T> for Eth1DataVote {
mod tests { mod tests {
use super::*; use super::*;
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
use ssz::ssz_encode; use ssz::{ssz_encode, Decodable};
#[test] #[test]
pub fn test_ssz_round_trip() { pub fn test_ssz_round_trip() {
@ -69,11 +48,11 @@ mod tests {
} }
#[test] #[test]
pub fn test_hash_tree_root() { pub fn test_hash_tree_root_internal() {
let mut rng = XorShiftRng::from_seed([42; 16]); let mut rng = XorShiftRng::from_seed([42; 16]);
let original = Eth1DataVote::random_for_test(&mut rng); let original = Eth1DataVote::random_for_test(&mut rng);
let result = original.hash_tree_root(); let result = original.hash_tree_root_internal();
assert_eq!(result.len(), 32); assert_eq!(result.len(), 32);
// TODO: Add further tests // TODO: Add further tests

View File

@ -2,46 +2,22 @@ use crate::{test_utils::TestRandom, Epoch};
use bls::Signature; use bls::Signature;
use rand::RngCore; use rand::RngCore;
use serde_derive::Serialize; use serde_derive::Serialize;
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; use ssz::{hash, TreeHash};
use ssz_derive::{Decode, Encode};
#[derive(Debug, PartialEq, Clone, Serialize)] #[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode)]
pub struct Exit { pub struct Exit {
pub epoch: Epoch, pub epoch: Epoch,
pub validator_index: u64, pub validator_index: u64,
pub signature: Signature, pub signature: Signature,
} }
impl Encodable for Exit {
fn ssz_append(&self, s: &mut SszStream) {
s.append(&self.epoch);
s.append(&self.validator_index);
s.append(&self.signature);
}
}
impl Decodable for Exit {
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
let (epoch, i) = <_>::ssz_decode(bytes, i)?;
let (validator_index, i) = <_>::ssz_decode(bytes, i)?;
let (signature, i) = <_>::ssz_decode(bytes, i)?;
Ok((
Self {
epoch,
validator_index,
signature,
},
i,
))
}
}
impl TreeHash for Exit { impl TreeHash for Exit {
fn hash_tree_root(&self) -> Vec<u8> { fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![]; let mut result: Vec<u8> = vec![];
result.append(&mut self.epoch.hash_tree_root()); result.append(&mut self.epoch.hash_tree_root_internal());
result.append(&mut self.validator_index.hash_tree_root()); result.append(&mut self.validator_index.hash_tree_root_internal());
result.append(&mut self.signature.hash_tree_root()); result.append(&mut self.signature.hash_tree_root_internal());
hash(&result) hash(&result)
} }
} }
@ -60,7 +36,7 @@ impl<T: RngCore> TestRandom<T> for Exit {
mod tests { mod tests {
use super::*; use super::*;
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
use ssz::ssz_encode; use ssz::{ssz_encode, Decodable};
#[test] #[test]
pub fn test_ssz_round_trip() { pub fn test_ssz_round_trip() {
@ -74,11 +50,11 @@ mod tests {
} }
#[test] #[test]
pub fn test_hash_tree_root() { pub fn test_hash_tree_root_internal() {
let mut rng = XorShiftRng::from_seed([42; 16]); let mut rng = XorShiftRng::from_seed([42; 16]);
let original = Exit::random_for_test(&mut rng); let original = Exit::random_for_test(&mut rng);
let result = original.hash_tree_root(); let result = original.hash_tree_root_internal();
assert_eq!(result.len(), 32); assert_eq!(result.len(), 32);
// TODO: Add further tests // TODO: Add further tests

View File

@ -1,9 +1,10 @@
use crate::{test_utils::TestRandom, Epoch}; use crate::{test_utils::TestRandom, Epoch};
use rand::RngCore; use rand::RngCore;
use serde_derive::Serialize; use serde_derive::Serialize;
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; use ssz::{hash, TreeHash};
use ssz_derive::{Decode, Encode};
#[derive(Debug, Clone, PartialEq, Default, Serialize)] #[derive(Debug, Clone, PartialEq, Default, Serialize, Encode, Decode)]
pub struct Fork { pub struct Fork {
pub previous_version: u64, pub previous_version: u64,
pub current_version: u64, pub current_version: u64,
@ -26,37 +27,12 @@ impl Fork {
} }
} }
impl Encodable for Fork {
fn ssz_append(&self, s: &mut SszStream) {
s.append(&self.previous_version);
s.append(&self.current_version);
s.append(&self.epoch);
}
}
impl Decodable for Fork {
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
let (previous_version, i) = <_>::ssz_decode(bytes, i)?;
let (current_version, i) = <_>::ssz_decode(bytes, i)?;
let (epoch, i) = <_>::ssz_decode(bytes, i)?;
Ok((
Self {
previous_version,
current_version,
epoch,
},
i,
))
}
}
impl TreeHash for Fork { impl TreeHash for Fork {
fn hash_tree_root(&self) -> Vec<u8> { fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![]; let mut result: Vec<u8> = vec![];
result.append(&mut self.previous_version.hash_tree_root()); result.append(&mut self.previous_version.hash_tree_root_internal());
result.append(&mut self.current_version.hash_tree_root()); result.append(&mut self.current_version.hash_tree_root_internal());
result.append(&mut self.epoch.hash_tree_root()); result.append(&mut self.epoch.hash_tree_root_internal());
hash(&result) hash(&result)
} }
} }
@ -75,7 +51,7 @@ impl<T: RngCore> TestRandom<T> for Fork {
mod tests { mod tests {
use super::*; use super::*;
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
use ssz::ssz_encode; use ssz::{ssz_encode, Decodable};
#[test] #[test]
pub fn test_ssz_round_trip() { pub fn test_ssz_round_trip() {
@ -89,11 +65,11 @@ mod tests {
} }
#[test] #[test]
pub fn test_hash_tree_root() { pub fn test_hash_tree_root_internal() {
let mut rng = XorShiftRng::from_seed([42; 16]); let mut rng = XorShiftRng::from_seed([42; 16]);
let original = Fork::random_for_test(&mut rng); let original = Fork::random_for_test(&mut rng);
let result = original.hash_tree_root(); let result = original.hash_tree_root_internal();
assert_eq!(result.len(), 32); assert_eq!(result.len(), 32);
// TODO: Add further tests // TODO: Add further tests

View File

@ -2,9 +2,10 @@ use crate::test_utils::TestRandom;
use crate::{AttestationData, Bitfield, Slot}; use crate::{AttestationData, Bitfield, Slot};
use rand::RngCore; use rand::RngCore;
use serde_derive::Serialize; use serde_derive::Serialize;
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; use ssz::{hash, TreeHash};
use ssz_derive::{Decode, Encode};
#[derive(Debug, Clone, PartialEq, Serialize)] #[derive(Debug, Clone, PartialEq, Serialize, Encode, Decode)]
pub struct PendingAttestation { pub struct PendingAttestation {
pub aggregation_bitfield: Bitfield, pub aggregation_bitfield: Bitfield,
pub data: AttestationData, pub data: AttestationData,
@ -12,41 +13,13 @@ pub struct PendingAttestation {
pub inclusion_slot: Slot, pub inclusion_slot: Slot,
} }
impl Encodable for PendingAttestation {
fn ssz_append(&self, s: &mut SszStream) {
s.append(&self.aggregation_bitfield);
s.append(&self.data);
s.append(&self.custody_bitfield);
s.append(&self.inclusion_slot);
}
}
impl Decodable for PendingAttestation {
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
let (aggregation_bitfield, i) = <_>::ssz_decode(bytes, i)?;
let (data, i) = <_>::ssz_decode(bytes, i)?;
let (custody_bitfield, i) = <_>::ssz_decode(bytes, i)?;
let (inclusion_slot, i) = <_>::ssz_decode(bytes, i)?;
Ok((
Self {
data,
aggregation_bitfield,
custody_bitfield,
inclusion_slot,
},
i,
))
}
}
impl TreeHash for PendingAttestation { impl TreeHash for PendingAttestation {
fn hash_tree_root(&self) -> Vec<u8> { fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![]; let mut result: Vec<u8> = vec![];
result.append(&mut self.aggregation_bitfield.hash_tree_root()); result.append(&mut self.aggregation_bitfield.hash_tree_root_internal());
result.append(&mut self.data.hash_tree_root()); result.append(&mut self.data.hash_tree_root_internal());
result.append(&mut self.custody_bitfield.hash_tree_root()); result.append(&mut self.custody_bitfield.hash_tree_root_internal());
result.append(&mut self.inclusion_slot.hash_tree_root()); result.append(&mut self.inclusion_slot.hash_tree_root_internal());
hash(&result) hash(&result)
} }
} }
@ -66,7 +39,7 @@ impl<T: RngCore> TestRandom<T> for PendingAttestation {
mod tests { mod tests {
use super::*; use super::*;
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
use ssz::ssz_encode; use ssz::{ssz_encode, Decodable};
#[test] #[test]
pub fn test_ssz_round_trip() { pub fn test_ssz_round_trip() {
@ -80,11 +53,11 @@ mod tests {
} }
#[test] #[test]
pub fn test_hash_tree_root() { pub fn test_hash_tree_root_internal() {
let mut rng = XorShiftRng::from_seed([42; 16]); let mut rng = XorShiftRng::from_seed([42; 16]);
let original = PendingAttestation::random_for_test(&mut rng); let original = PendingAttestation::random_for_test(&mut rng);
let result = original.hash_tree_root(); let result = original.hash_tree_root_internal();
assert_eq!(result.len(), 32); assert_eq!(result.len(), 32);
// TODO: Add further tests // TODO: Add further tests

View File

@ -2,46 +2,22 @@ use crate::test_utils::TestRandom;
use crate::{Hash256, Slot}; use crate::{Hash256, Slot};
use rand::RngCore; use rand::RngCore;
use serde_derive::Serialize; use serde_derive::Serialize;
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; use ssz::{hash, TreeHash};
use ssz_derive::{Decode, Encode};
#[derive(Debug, PartialEq, Clone, Default, Serialize)] #[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode)]
pub struct ProposalSignedData { pub struct ProposalSignedData {
pub slot: Slot, pub slot: Slot,
pub shard: u64, pub shard: u64,
pub block_root: Hash256, pub block_root: Hash256,
} }
impl Encodable for ProposalSignedData {
fn ssz_append(&self, s: &mut SszStream) {
s.append(&self.slot);
s.append(&self.shard);
s.append(&self.block_root);
}
}
impl Decodable for ProposalSignedData {
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
let (slot, i) = <_>::ssz_decode(bytes, i)?;
let (shard, i) = <_>::ssz_decode(bytes, i)?;
let (block_root, i) = <_>::ssz_decode(bytes, i)?;
Ok((
ProposalSignedData {
slot,
shard,
block_root,
},
i,
))
}
}
impl TreeHash for ProposalSignedData { impl TreeHash for ProposalSignedData {
fn hash_tree_root(&self) -> Vec<u8> { fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![]; let mut result: Vec<u8> = vec![];
result.append(&mut self.slot.hash_tree_root()); result.append(&mut self.slot.hash_tree_root_internal());
result.append(&mut self.shard.hash_tree_root()); result.append(&mut self.shard.hash_tree_root_internal());
result.append(&mut self.block_root.hash_tree_root()); result.append(&mut self.block_root.hash_tree_root_internal());
hash(&result) hash(&result)
} }
} }
@ -60,7 +36,7 @@ impl<T: RngCore> TestRandom<T> for ProposalSignedData {
mod tests { mod tests {
use super::*; use super::*;
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
use ssz::ssz_encode; use ssz::{ssz_encode, Decodable};
#[test] #[test]
pub fn test_ssz_round_trip() { pub fn test_ssz_round_trip() {
@ -74,11 +50,11 @@ mod tests {
} }
#[test] #[test]
pub fn test_hash_tree_root() { pub fn test_hash_tree_root_internal() {
let mut rng = XorShiftRng::from_seed([42; 16]); let mut rng = XorShiftRng::from_seed([42; 16]);
let original = ProposalSignedData::random_for_test(&mut rng); let original = ProposalSignedData::random_for_test(&mut rng);
let result = original.hash_tree_root(); let result = original.hash_tree_root_internal();
assert_eq!(result.len(), 32); assert_eq!(result.len(), 32);
// TODO: Add further tests // TODO: Add further tests

View File

@ -3,9 +3,10 @@ use crate::test_utils::TestRandom;
use bls::Signature; use bls::Signature;
use rand::RngCore; use rand::RngCore;
use serde_derive::Serialize; use serde_derive::Serialize;
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; use ssz::{hash, TreeHash};
use ssz_derive::{Decode, Encode};
#[derive(Debug, PartialEq, Clone, Serialize)] #[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode)]
pub struct ProposerSlashing { pub struct ProposerSlashing {
pub proposer_index: u64, pub proposer_index: u64,
pub proposal_data_1: ProposalSignedData, pub proposal_data_1: ProposalSignedData,
@ -14,45 +15,14 @@ pub struct ProposerSlashing {
pub proposal_signature_2: Signature, pub proposal_signature_2: Signature,
} }
impl Encodable for ProposerSlashing {
fn ssz_append(&self, s: &mut SszStream) {
s.append(&self.proposer_index);
s.append(&self.proposal_data_1);
s.append(&self.proposal_signature_1);
s.append(&self.proposal_data_2);
s.append(&self.proposal_signature_2);
}
}
impl Decodable for ProposerSlashing {
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
let (proposer_index, i) = <_>::ssz_decode(bytes, i)?;
let (proposal_data_1, i) = <_>::ssz_decode(bytes, i)?;
let (proposal_signature_1, i) = <_>::ssz_decode(bytes, i)?;
let (proposal_data_2, i) = <_>::ssz_decode(bytes, i)?;
let (proposal_signature_2, i) = <_>::ssz_decode(bytes, i)?;
Ok((
ProposerSlashing {
proposer_index,
proposal_data_1,
proposal_signature_1,
proposal_data_2,
proposal_signature_2,
},
i,
))
}
}
impl TreeHash for ProposerSlashing { impl TreeHash for ProposerSlashing {
fn hash_tree_root(&self) -> Vec<u8> { fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![]; let mut result: Vec<u8> = vec![];
result.append(&mut self.proposer_index.hash_tree_root()); result.append(&mut self.proposer_index.hash_tree_root_internal());
result.append(&mut self.proposal_data_1.hash_tree_root()); result.append(&mut self.proposal_data_1.hash_tree_root_internal());
result.append(&mut self.proposal_signature_1.hash_tree_root()); result.append(&mut self.proposal_signature_1.hash_tree_root_internal());
result.append(&mut self.proposal_data_2.hash_tree_root()); result.append(&mut self.proposal_data_2.hash_tree_root_internal());
result.append(&mut self.proposal_signature_2.hash_tree_root()); result.append(&mut self.proposal_signature_2.hash_tree_root_internal());
hash(&result) hash(&result)
} }
} }
@ -73,7 +43,7 @@ impl<T: RngCore> TestRandom<T> for ProposerSlashing {
mod tests { mod tests {
use super::*; use super::*;
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
use ssz::ssz_encode; use ssz::{ssz_encode, Decodable};
#[test] #[test]
pub fn test_ssz_round_trip() { pub fn test_ssz_round_trip() {
@ -87,11 +57,11 @@ mod tests {
} }
#[test] #[test]
pub fn test_hash_tree_root() { pub fn test_hash_tree_root_internal() {
let mut rng = XorShiftRng::from_seed([42; 16]); let mut rng = XorShiftRng::from_seed([42; 16]);
let original = ProposerSlashing::random_for_test(&mut rng); let original = ProposerSlashing::random_for_test(&mut rng);
let result = original.hash_tree_root(); let result = original.hash_tree_root_internal();
assert_eq!(result.len(), 32); assert_eq!(result.len(), 32);
// TODO: Add further tests // TODO: Add further tests

View File

@ -1,46 +1,22 @@
use crate::{test_utils::TestRandom, Slot}; use crate::{test_utils::TestRandom, Slot};
use rand::RngCore; use rand::RngCore;
use serde_derive::Serialize; use serde_derive::Serialize;
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; use ssz::{hash, TreeHash};
use ssz_derive::{Decode, Encode};
#[derive(Debug, PartialEq, Clone, Serialize)] #[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode)]
pub struct ShardReassignmentRecord { pub struct ShardReassignmentRecord {
pub validator_index: u64, pub validator_index: u64,
pub shard: u64, pub shard: u64,
pub slot: Slot, pub slot: Slot,
} }
impl Encodable for ShardReassignmentRecord {
fn ssz_append(&self, s: &mut SszStream) {
s.append(&self.validator_index);
s.append(&self.shard);
s.append(&self.slot);
}
}
impl Decodable for ShardReassignmentRecord {
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
let (validator_index, i) = <_>::ssz_decode(bytes, i)?;
let (shard, i) = <_>::ssz_decode(bytes, i)?;
let (slot, i) = <_>::ssz_decode(bytes, i)?;
Ok((
Self {
validator_index,
shard,
slot,
},
i,
))
}
}
impl TreeHash for ShardReassignmentRecord { impl TreeHash for ShardReassignmentRecord {
fn hash_tree_root(&self) -> Vec<u8> { fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![]; let mut result: Vec<u8> = vec![];
result.append(&mut self.validator_index.hash_tree_root()); result.append(&mut self.validator_index.hash_tree_root_internal());
result.append(&mut self.shard.hash_tree_root()); result.append(&mut self.shard.hash_tree_root_internal());
result.append(&mut self.slot.hash_tree_root()); result.append(&mut self.slot.hash_tree_root_internal());
hash(&result) hash(&result)
} }
} }
@ -59,7 +35,7 @@ impl<T: RngCore> TestRandom<T> for ShardReassignmentRecord {
mod tests { mod tests {
use super::*; use super::*;
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
use ssz::ssz_encode; use ssz::{ssz_encode, Decodable};
#[test] #[test]
pub fn test_ssz_round_trip() { pub fn test_ssz_round_trip() {
@ -73,11 +49,11 @@ mod tests {
} }
#[test] #[test]
pub fn test_hash_tree_root() { pub fn test_hash_tree_root_internal() {
let mut rng = XorShiftRng::from_seed([42; 16]); let mut rng = XorShiftRng::from_seed([42; 16]);
let original = ShardReassignmentRecord::random_for_test(&mut rng); let original = ShardReassignmentRecord::random_for_test(&mut rng);
let result = original.hash_tree_root(); let result = original.hash_tree_root_internal();
assert_eq!(result.len(), 32); assert_eq!(result.len(), 32);
// TODO: Add further tests // TODO: Add further tests

View File

@ -1,9 +1,10 @@
use crate::{test_utils::TestRandom, AggregateSignature, AttestationData, Bitfield}; use crate::{test_utils::TestRandom, AggregateSignature, AttestationData, Bitfield};
use rand::RngCore; use rand::RngCore;
use serde_derive::Serialize; use serde_derive::Serialize;
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; use ssz::{hash, TreeHash};
use ssz_derive::{Decode, Encode};
#[derive(Debug, PartialEq, Clone, Serialize)] #[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode)]
pub struct SlashableAttestation { pub struct SlashableAttestation {
pub validator_indices: Vec<u64>, pub validator_indices: Vec<u64>,
pub data: AttestationData, pub data: AttestationData,
@ -11,41 +12,13 @@ pub struct SlashableAttestation {
pub aggregate_signature: AggregateSignature, pub aggregate_signature: AggregateSignature,
} }
impl Encodable for SlashableAttestation {
fn ssz_append(&self, s: &mut SszStream) {
s.append_vec(&self.validator_indices);
s.append(&self.data);
s.append(&self.custody_bitfield);
s.append(&self.aggregate_signature);
}
}
impl Decodable for SlashableAttestation {
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
let (validator_indices, i) = <_>::ssz_decode(bytes, i)?;
let (data, i) = <_>::ssz_decode(bytes, i)?;
let (custody_bitfield, i) = <_>::ssz_decode(bytes, i)?;
let (aggregate_signature, i) = <_>::ssz_decode(bytes, i)?;
Ok((
SlashableAttestation {
validator_indices,
data,
custody_bitfield,
aggregate_signature,
},
i,
))
}
}
impl TreeHash for SlashableAttestation { impl TreeHash for SlashableAttestation {
fn hash_tree_root(&self) -> Vec<u8> { fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![]; let mut result: Vec<u8> = vec![];
result.append(&mut self.validator_indices.hash_tree_root()); result.append(&mut self.validator_indices.hash_tree_root_internal());
result.append(&mut self.data.hash_tree_root()); result.append(&mut self.data.hash_tree_root_internal());
result.append(&mut self.custody_bitfield.hash_tree_root()); result.append(&mut self.custody_bitfield.hash_tree_root_internal());
result.append(&mut self.aggregate_signature.hash_tree_root()); result.append(&mut self.aggregate_signature.hash_tree_root_internal());
hash(&result) hash(&result)
} }
} }
@ -65,7 +38,7 @@ impl<T: RngCore> TestRandom<T> for SlashableAttestation {
mod tests { mod tests {
use super::*; use super::*;
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
use ssz::ssz_encode; use ssz::{ssz_encode, Decodable};
#[test] #[test]
pub fn test_ssz_round_trip() { pub fn test_ssz_round_trip() {
@ -79,11 +52,11 @@ mod tests {
} }
#[test] #[test]
pub fn test_hash_tree_root() { pub fn test_hash_tree_root_internal() {
let mut rng = XorShiftRng::from_seed([42; 16]); let mut rng = XorShiftRng::from_seed([42; 16]);
let original = SlashableAttestation::random_for_test(&mut rng); let original = SlashableAttestation::random_for_test(&mut rng);
let result = original.hash_tree_root(); let result = original.hash_tree_root_internal();
assert_eq!(result.len(), 32); assert_eq!(result.len(), 32);
// TODO: Add further tests // TODO: Add further tests

View File

@ -1,11 +1,13 @@
use super::AttestationData; use super::AttestationData;
use crate::chain_spec::ChainSpec;
use crate::test_utils::TestRandom; use crate::test_utils::TestRandom;
use bls::AggregateSignature; use bls::AggregateSignature;
use rand::RngCore; use rand::RngCore;
use serde_derive::Serialize; use serde_derive::Serialize;
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; use ssz::{hash, TreeHash};
use ssz_derive::{Decode, Encode};
#[derive(Debug, PartialEq, Clone, Serialize)] #[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode)]
pub struct SlashableVoteData { pub struct SlashableVoteData {
pub custody_bit_0_indices: Vec<u32>, pub custody_bit_0_indices: Vec<u32>,
pub custody_bit_1_indices: Vec<u32>, pub custody_bit_1_indices: Vec<u32>,
@ -13,41 +15,34 @@ pub struct SlashableVoteData {
pub aggregate_signature: AggregateSignature, pub aggregate_signature: AggregateSignature,
} }
impl Encodable for SlashableVoteData { impl SlashableVoteData {
fn ssz_append(&self, s: &mut SszStream) { /// Check if ``attestation_data_1`` and ``attestation_data_2`` have the same target.
s.append_vec(&self.custody_bit_0_indices); ///
s.append_vec(&self.custody_bit_1_indices); /// Spec v0.3.0
s.append(&self.data); pub fn is_double_vote(&self, other: &SlashableVoteData, spec: &ChainSpec) -> bool {
s.append(&self.aggregate_signature); self.data.slot.epoch(spec.epoch_length) == other.data.slot.epoch(spec.epoch_length)
} }
}
impl Decodable for SlashableVoteData { /// Check if ``attestation_data_1`` surrounds ``attestation_data_2``.
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> { ///
let (custody_bit_0_indices, i) = <_>::ssz_decode(bytes, i)?; /// Spec v0.3.0
let (custody_bit_1_indices, i) = <_>::ssz_decode(bytes, i)?; pub fn is_surround_vote(&self, other: &SlashableVoteData, spec: &ChainSpec) -> bool {
let (data, i) = <_>::ssz_decode(bytes, i)?; let source_epoch_1 = self.data.justified_epoch;
let (aggregate_signature, i) = <_>::ssz_decode(bytes, i)?; let source_epoch_2 = other.data.justified_epoch;
let target_epoch_1 = self.data.slot.epoch(spec.epoch_length);
let target_epoch_2 = other.data.slot.epoch(spec.epoch_length);
Ok(( (source_epoch_1 < source_epoch_2) && (target_epoch_2 < target_epoch_1)
SlashableVoteData {
custody_bit_0_indices,
custody_bit_1_indices,
data,
aggregate_signature,
},
i,
))
} }
} }
impl TreeHash for SlashableVoteData { impl TreeHash for SlashableVoteData {
fn hash_tree_root(&self) -> Vec<u8> { fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![]; let mut result: Vec<u8> = vec![];
result.append(&mut self.custody_bit_0_indices.hash_tree_root()); result.append(&mut self.custody_bit_0_indices.hash_tree_root_internal());
result.append(&mut self.custody_bit_1_indices.hash_tree_root()); result.append(&mut self.custody_bit_1_indices.hash_tree_root_internal());
result.append(&mut self.data.hash_tree_root()); result.append(&mut self.data.hash_tree_root_internal());
result.append(&mut self.aggregate_signature.hash_tree_root()); result.append(&mut self.aggregate_signature.hash_tree_root_internal());
hash(&result) hash(&result)
} }
} }
@ -66,8 +61,82 @@ impl<T: RngCore> TestRandom<T> for SlashableVoteData {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::chain_spec::ChainSpec;
use crate::slot_epoch::{Epoch, Slot};
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
use ssz::ssz_encode; use ssz::{ssz_encode, Decodable};
#[test]
pub fn test_is_double_vote_true() {
let spec = ChainSpec::foundation();
let slashable_vote_first = create_slashable_vote_data(1, 1, &spec);
let slashable_vote_second = create_slashable_vote_data(1, 1, &spec);
assert_eq!(
slashable_vote_first.is_double_vote(&slashable_vote_second, &spec),
true
)
}
#[test]
pub fn test_is_double_vote_false() {
let spec = ChainSpec::foundation();
let slashable_vote_first = create_slashable_vote_data(1, 1, &spec);
let slashable_vote_second = create_slashable_vote_data(2, 1, &spec);
assert_eq!(
slashable_vote_first.is_double_vote(&slashable_vote_second, &spec),
false
);
}
#[test]
pub fn test_is_surround_vote_true() {
let spec = ChainSpec::foundation();
let slashable_vote_first = create_slashable_vote_data(2, 1, &spec);
let slashable_vote_second = create_slashable_vote_data(1, 2, &spec);
assert_eq!(
slashable_vote_first.is_surround_vote(&slashable_vote_second, &spec),
true
);
}
#[test]
pub fn test_is_surround_vote_true_realistic() {
let spec = ChainSpec::foundation();
let slashable_vote_first = create_slashable_vote_data(4, 1, &spec);
let slashable_vote_second = create_slashable_vote_data(3, 2, &spec);
assert_eq!(
slashable_vote_first.is_surround_vote(&slashable_vote_second, &spec),
true
);
}
#[test]
pub fn test_is_surround_vote_false_source_epoch_fails() {
let spec = ChainSpec::foundation();
let slashable_vote_first = create_slashable_vote_data(2, 2, &spec);
let slashable_vote_second = create_slashable_vote_data(1, 1, &spec);
assert_eq!(
slashable_vote_first.is_surround_vote(&slashable_vote_second, &spec),
false
);
}
#[test]
pub fn test_is_surround_vote_false_target_epoch_fails() {
let spec = ChainSpec::foundation();
let slashable_vote_first = create_slashable_vote_data(1, 1, &spec);
let slashable_vote_second = create_slashable_vote_data(2, 2, &spec);
assert_eq!(
slashable_vote_first.is_surround_vote(&slashable_vote_second, &spec),
false
);
}
#[test] #[test]
pub fn test_ssz_round_trip() { pub fn test_ssz_round_trip() {
@ -81,14 +150,27 @@ mod tests {
} }
#[test] #[test]
pub fn test_hash_tree_root() { pub fn test_hash_tree_root_internal() {
let mut rng = XorShiftRng::from_seed([42; 16]); let mut rng = XorShiftRng::from_seed([42; 16]);
let original = SlashableVoteData::random_for_test(&mut rng); let original = SlashableVoteData::random_for_test(&mut rng);
let result = original.hash_tree_root(); let result = original.hash_tree_root_internal();
assert_eq!(result.len(), 32); assert_eq!(result.len(), 32);
// TODO: Add further tests // TODO: Add further tests
// https://github.com/sigp/lighthouse/issues/170 // https://github.com/sigp/lighthouse/issues/170
} }
fn create_slashable_vote_data(
slot_factor: u64,
justified_epoch: u64,
spec: &ChainSpec,
) -> SlashableVoteData {
let mut rng = XorShiftRng::from_seed([42; 16]);
let mut slashable_vote = SlashableVoteData::random_for_test(&mut rng);
slashable_vote.data.slot = Slot::new(slot_factor * spec.epoch_length);
slashable_vote.data.justified_epoch = Epoch::new(justified_epoch);
slashable_vote
}
} }

View File

@ -224,9 +224,9 @@ macro_rules! impl_ssz {
} }
impl TreeHash for $type { impl TreeHash for $type {
fn hash_tree_root(&self) -> Vec<u8> { fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![]; let mut result: Vec<u8> = vec![];
result.append(&mut self.0.hash_tree_root()); result.append(&mut self.0.hash_tree_root_internal());
hash(&result) hash(&result)
} }
} }
@ -560,11 +560,11 @@ macro_rules! ssz_tests {
} }
#[test] #[test]
pub fn test_hash_tree_root() { pub fn test_hash_tree_root_internal() {
let mut rng = XorShiftRng::from_seed([42; 16]); let mut rng = XorShiftRng::from_seed([42; 16]);
let original = $type::random_for_test(&mut rng); let original = $type::random_for_test(&mut rng);
let result = original.hash_tree_root(); let result = original.hash_tree_root_internal();
assert_eq!(result.len(), 32); assert_eq!(result.len(), 32);
// TODO: Add further tests // TODO: Add further tests

View File

@ -122,15 +122,17 @@ impl Decodable for Validator {
} }
impl TreeHash for Validator { impl TreeHash for Validator {
fn hash_tree_root(&self) -> Vec<u8> { fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![]; let mut result: Vec<u8> = vec![];
result.append(&mut self.pubkey.hash_tree_root()); result.append(&mut self.pubkey.hash_tree_root_internal());
result.append(&mut self.withdrawal_credentials.hash_tree_root()); result.append(&mut self.withdrawal_credentials.hash_tree_root_internal());
result.append(&mut self.activation_epoch.hash_tree_root()); result.append(&mut self.activation_epoch.hash_tree_root_internal());
result.append(&mut self.exit_epoch.hash_tree_root()); result.append(&mut self.exit_epoch.hash_tree_root_internal());
result.append(&mut self.withdrawal_epoch.hash_tree_root()); result.append(&mut self.withdrawal_epoch.hash_tree_root_internal());
result.append(&mut self.penalized_epoch.hash_tree_root()); result.append(&mut self.penalized_epoch.hash_tree_root_internal());
result.append(&mut u64::from(status_flag_to_byte(self.status_flags)).hash_tree_root()); result.append(
&mut u64::from(status_flag_to_byte(self.status_flags)).hash_tree_root_internal(),
);
hash(&result) hash(&result)
} }
} }
@ -190,11 +192,11 @@ mod tests {
} }
#[test] #[test]
pub fn test_hash_tree_root() { pub fn test_hash_tree_root_internal() {
let mut rng = XorShiftRng::from_seed([42; 16]); let mut rng = XorShiftRng::from_seed([42; 16]);
let original = Validator::random_for_test(&mut rng); let original = Validator::random_for_test(&mut rng);
let result = original.hash_tree_root(); let result = original.hash_tree_root_internal();
assert_eq!(result.len(), 32); assert_eq!(result.len(), 32);
// TODO: Add further tests // TODO: Add further tests

View File

@ -2,10 +2,11 @@ use crate::{test_utils::TestRandom, Hash256, Slot};
use bls::PublicKey; use bls::PublicKey;
use rand::RngCore; use rand::RngCore;
use serde_derive::Serialize; use serde_derive::Serialize;
use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; use ssz::{hash, TreeHash};
use ssz_derive::{Decode, Encode};
// The information gathered from the PoW chain validator registration function. // The information gathered from the PoW chain validator registration function.
#[derive(Debug, Clone, PartialEq, Serialize)] #[derive(Debug, Clone, PartialEq, Serialize, Encode, Decode)]
pub struct ValidatorRegistryDeltaBlock { pub struct ValidatorRegistryDeltaBlock {
pub latest_registry_delta_root: Hash256, pub latest_registry_delta_root: Hash256,
pub validator_index: u32, pub validator_index: u32,
@ -27,45 +28,14 @@ impl Default for ValidatorRegistryDeltaBlock {
} }
} }
impl Encodable for ValidatorRegistryDeltaBlock {
fn ssz_append(&self, s: &mut SszStream) {
s.append(&self.latest_registry_delta_root);
s.append(&self.validator_index);
s.append(&self.pubkey);
s.append(&self.slot);
s.append(&self.flag);
}
}
impl Decodable for ValidatorRegistryDeltaBlock {
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), DecodeError> {
let (latest_registry_delta_root, i) = <_>::ssz_decode(bytes, i)?;
let (validator_index, i) = <_>::ssz_decode(bytes, i)?;
let (pubkey, i) = <_>::ssz_decode(bytes, i)?;
let (slot, i) = <_>::ssz_decode(bytes, i)?;
let (flag, i) = <_>::ssz_decode(bytes, i)?;
Ok((
Self {
latest_registry_delta_root,
validator_index,
pubkey,
slot,
flag,
},
i,
))
}
}
impl TreeHash for ValidatorRegistryDeltaBlock { impl TreeHash for ValidatorRegistryDeltaBlock {
fn hash_tree_root(&self) -> Vec<u8> { fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![]; let mut result: Vec<u8> = vec![];
result.append(&mut self.latest_registry_delta_root.hash_tree_root()); result.append(&mut self.latest_registry_delta_root.hash_tree_root_internal());
result.append(&mut self.validator_index.hash_tree_root()); result.append(&mut self.validator_index.hash_tree_root_internal());
result.append(&mut self.pubkey.hash_tree_root()); result.append(&mut self.pubkey.hash_tree_root_internal());
result.append(&mut self.slot.hash_tree_root()); result.append(&mut self.slot.hash_tree_root_internal());
result.append(&mut self.flag.hash_tree_root()); result.append(&mut self.flag.hash_tree_root_internal());
hash(&result) hash(&result)
} }
} }
@ -86,7 +56,7 @@ impl<T: RngCore> TestRandom<T> for ValidatorRegistryDeltaBlock {
mod tests { mod tests {
use super::*; use super::*;
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
use ssz::ssz_encode; use ssz::{ssz_encode, Decodable};
#[test] #[test]
pub fn test_ssz_round_trip() { pub fn test_ssz_round_trip() {
@ -100,11 +70,11 @@ mod tests {
} }
#[test] #[test]
pub fn test_hash_tree_root() { pub fn test_hash_tree_root_internal() {
let mut rng = XorShiftRng::from_seed([42; 16]); let mut rng = XorShiftRng::from_seed([42; 16]);
let original = ValidatorRegistryDeltaBlock::random_for_test(&mut rng); let original = ValidatorRegistryDeltaBlock::random_for_test(&mut rng);
let result = original.hash_tree_root(); let result = original.hash_tree_root_internal();
assert_eq!(result.len(), 32); assert_eq!(result.len(), 32);
// TODO: Add further tests // TODO: Add further tests

View File

@ -62,7 +62,7 @@ impl Serialize for AggregateSignature {
} }
impl TreeHash for AggregateSignature { impl TreeHash for AggregateSignature {
fn hash_tree_root(&self) -> Vec<u8> { fn hash_tree_root_internal(&self) -> Vec<u8> {
hash(&self.0.as_bytes()) hash(&self.0.as_bytes())
} }
} }

View File

@ -66,7 +66,7 @@ impl Serialize for PublicKey {
} }
impl TreeHash for PublicKey { impl TreeHash for PublicKey {
fn hash_tree_root(&self) -> Vec<u8> { fn hash_tree_root_internal(&self) -> Vec<u8> {
hash(&self.0.as_bytes()) hash(&self.0.as_bytes())
} }
} }

View File

@ -41,7 +41,7 @@ impl Decodable for SecretKey {
} }
impl TreeHash for SecretKey { impl TreeHash for SecretKey {
fn hash_tree_root(&self) -> Vec<u8> { fn hash_tree_root_internal(&self) -> Vec<u8> {
self.0.as_bytes().clone() self.0.as_bytes().clone()
} }
} }

View File

@ -73,7 +73,7 @@ impl Decodable for Signature {
} }
impl TreeHash for Signature { impl TreeHash for Signature {
fn hash_tree_root(&self) -> Vec<u8> { fn hash_tree_root_internal(&self) -> Vec<u8> {
hash(&self.0.as_bytes()) hash(&self.0.as_bytes())
} }
} }

View File

@ -187,8 +187,8 @@ impl Serialize for BooleanBitfield {
} }
impl ssz::TreeHash for BooleanBitfield { impl ssz::TreeHash for BooleanBitfield {
fn hash_tree_root(&self) -> Vec<u8> { fn hash_tree_root_internal(&self) -> Vec<u8> {
self.to_bytes().hash_tree_root() self.to_bytes().hash_tree_root_internal()
} }
} }

View File

@ -39,6 +39,21 @@ impl Decodable for u8 {
} }
} }
impl Decodable for bool {
fn ssz_decode(bytes: &[u8], index: usize) -> Result<(Self, usize), DecodeError> {
if index >= bytes.len() {
Err(DecodeError::TooShort)
} else {
let result = match bytes[index] {
0b0000_0000 => false,
0b1000_0000 => true,
_ => return Err(DecodeError::Invalid),
};
Ok((result, index + 1))
}
}
}
impl Decodable for H256 { impl Decodable for H256 {
fn ssz_decode(bytes: &[u8], index: usize) -> Result<(Self, usize), DecodeError> { fn ssz_decode(bytes: &[u8], index: usize) -> Result<(Self, usize), DecodeError> {
if bytes.len() < 32 || bytes.len() - 32 < index { if bytes.len() < 32 || bytes.len() - 32 < index {
@ -215,4 +230,20 @@ mod tests {
let result: u16 = decode_ssz(&vec![0, 0, 0, 0, 1], 3).unwrap().0; let result: u16 = decode_ssz(&vec![0, 0, 0, 0, 1], 3).unwrap().0;
assert_eq!(result, 1); assert_eq!(result, 1);
} }
#[test]
fn test_decode_ssz_bool() {
let ssz = vec![0b0000_0000, 0b1000_0000];
let (result, index): (bool, usize) = decode_ssz(&ssz, 0).unwrap();
assert_eq!(index, 1);
assert_eq!(result, false);
let (result, index): (bool, usize) = decode_ssz(&ssz, 1).unwrap();
assert_eq!(index, 2);
assert_eq!(result, true);
let ssz = vec![0b0100_0000];
let result: Result<(bool, usize), DecodeError> = decode_ssz(&ssz, 0);
assert_eq!(result, Err(DecodeError::Invalid));
}
} }

View File

@ -46,6 +46,13 @@ impl_encodable_for_uint!(u32, 32);
impl_encodable_for_uint!(u64, 64); impl_encodable_for_uint!(u64, 64);
impl_encodable_for_uint!(usize, 64); impl_encodable_for_uint!(usize, 64);
impl Encodable for bool {
fn ssz_append(&self, s: &mut SszStream) {
let byte = if *self { 0b1000_0000 } else { 0b0000_0000 };
s.append_encoded_raw(&[byte]);
}
}
impl Encodable for H256 { impl Encodable for H256 {
fn ssz_append(&self, s: &mut SszStream) { fn ssz_append(&self, s: &mut SszStream) {
s.append_encoded_raw(&self.to_vec()); s.append_encoded_raw(&self.to_vec());
@ -206,4 +213,17 @@ mod tests {
ssz.append(&x); ssz.append(&x);
assert_eq!(ssz.drain(), vec![255, 255, 255, 255, 255, 255, 255, 255]); assert_eq!(ssz.drain(), vec![255, 255, 255, 255, 255, 255, 255, 255]);
} }
#[test]
fn test_ssz_encode_bool() {
let x: bool = false;
let mut ssz = SszStream::new();
ssz.append(&x);
assert_eq!(ssz.drain(), vec![0b0000_0000]);
let x: bool = true;
let mut ssz = SszStream::new();
ssz.append(&x);
assert_eq!(ssz.drain(), vec![0b1000_0000]);
}
} }

View File

@ -3,49 +3,49 @@ use super::{merkle_hash, ssz_encode, TreeHash};
use hashing::hash; use hashing::hash;
impl TreeHash for u8 { impl TreeHash for u8 {
fn hash_tree_root(&self) -> Vec<u8> { fn hash_tree_root_internal(&self) -> Vec<u8> {
ssz_encode(self) ssz_encode(self)
} }
} }
impl TreeHash for u16 { impl TreeHash for u16 {
fn hash_tree_root(&self) -> Vec<u8> { fn hash_tree_root_internal(&self) -> Vec<u8> {
ssz_encode(self) ssz_encode(self)
} }
} }
impl TreeHash for u32 { impl TreeHash for u32 {
fn hash_tree_root(&self) -> Vec<u8> { fn hash_tree_root_internal(&self) -> Vec<u8> {
ssz_encode(self) ssz_encode(self)
} }
} }
impl TreeHash for u64 { impl TreeHash for u64 {
fn hash_tree_root(&self) -> Vec<u8> { fn hash_tree_root_internal(&self) -> Vec<u8> {
ssz_encode(self) ssz_encode(self)
} }
} }
impl TreeHash for usize { impl TreeHash for usize {
fn hash_tree_root(&self) -> Vec<u8> { fn hash_tree_root_internal(&self) -> Vec<u8> {
ssz_encode(self) ssz_encode(self)
} }
} }
impl TreeHash for Address { impl TreeHash for Address {
fn hash_tree_root(&self) -> Vec<u8> { fn hash_tree_root_internal(&self) -> Vec<u8> {
ssz_encode(self) ssz_encode(self)
} }
} }
impl TreeHash for H256 { impl TreeHash for H256 {
fn hash_tree_root(&self) -> Vec<u8> { fn hash_tree_root_internal(&self) -> Vec<u8> {
ssz_encode(self) ssz_encode(self)
} }
} }
impl TreeHash for [u8] { impl TreeHash for [u8] {
fn hash_tree_root(&self) -> Vec<u8> { fn hash_tree_root_internal(&self) -> Vec<u8> {
if self.len() > 32 { if self.len() > 32 {
return hash(&self); return hash(&self);
} }
@ -57,12 +57,12 @@ impl<T> TreeHash for Vec<T>
where where
T: TreeHash, T: TreeHash,
{ {
/// Returns the merkle_hash of a list of hash_tree_root values created /// Returns the merkle_hash of a list of hash_tree_root_internal values created
/// from the given list. /// from the given list.
/// Note: A byte vector, Vec<u8>, must be converted to a slice (as_slice()) /// Note: A byte vector, Vec<u8>, must be converted to a slice (as_slice())
/// to be handled properly (i.e. hashed) as byte array. /// to be handled properly (i.e. hashed) as byte array.
fn hash_tree_root(&self) -> Vec<u8> { fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut tree_hashes = self.iter().map(|x| x.hash_tree_root()).collect(); let mut tree_hashes = self.iter().map(|x| x.hash_tree_root_internal()).collect();
merkle_hash(&mut tree_hashes) merkle_hash(&mut tree_hashes)
} }
} }
@ -73,7 +73,7 @@ mod tests {
#[test] #[test]
fn test_impl_tree_hash_vec() { fn test_impl_tree_hash_vec() {
let result = vec![1u32, 2, 3, 4, 5, 6, 7].hash_tree_root(); let result = vec![1u32, 2, 3, 4, 5, 6, 7].hash_tree_root_internal();
assert_eq!(result.len(), 32); assert_eq!(result.len(), 32);
} }
} }

View File

@ -4,7 +4,14 @@ const SSZ_CHUNK_SIZE: usize = 128;
const HASHSIZE: usize = 32; const HASHSIZE: usize = 32;
pub trait TreeHash { pub trait TreeHash {
fn hash_tree_root(&self) -> Vec<u8>; fn hash_tree_root_internal(&self) -> Vec<u8>;
fn hash_tree_root(&self) -> Vec<u8> {
let mut result = self.hash_tree_root_internal();
if result.len() < HASHSIZE {
zpad(&mut result, HASHSIZE);
}
result
}
} }
/// Returns a 32 byte hash of 'list' - a vector of byte vectors. /// Returns a 32 byte hash of 'list' - a vector of byte vectors.
@ -14,7 +21,8 @@ pub fn merkle_hash(list: &mut Vec<Vec<u8>>) -> Vec<u8> {
let (mut chunk_size, mut chunkz) = list_to_blob(list); let (mut chunk_size, mut chunkz) = list_to_blob(list);
// get data_len as bytes. It will hashed will the merkle root // get data_len as bytes. It will hashed will the merkle root
let datalen = list.len().to_le_bytes(); let mut datalen = list.len().to_le_bytes().to_vec();
zpad(&mut datalen, 32);
// Tree-hash // Tree-hash
while chunkz.len() > HASHSIZE { while chunkz.len() > HASHSIZE {
@ -36,33 +44,68 @@ pub fn merkle_hash(list: &mut Vec<Vec<u8>>) -> Vec<u8> {
chunkz = new_chunkz; chunkz = new_chunkz;
} }
chunkz.append(&mut datalen.to_vec()); chunkz.append(&mut datalen);
hash(&chunkz) hash(&chunkz)
} }
fn list_to_blob(list: &mut Vec<Vec<u8>>) -> (usize, Vec<u8>) { fn list_to_blob(list: &mut Vec<Vec<u8>>) -> (usize, Vec<u8>) {
let chunk_size = if list.is_empty() { let chunk_size = if list.is_empty() || list[0].len() < SSZ_CHUNK_SIZE {
SSZ_CHUNK_SIZE SSZ_CHUNK_SIZE
} else if list[0].len() < SSZ_CHUNK_SIZE {
let items_per_chunk = SSZ_CHUNK_SIZE / list[0].len();
items_per_chunk * list[0].len()
} else { } else {
list[0].len() list[0].len()
}; };
let mut data = Vec::new(); let (items_per_chunk, chunk_count) = if list.is_empty() {
(1, 1)
} else {
let items_per_chunk = SSZ_CHUNK_SIZE / list[0].len();
let chunk_count = list.len() / items_per_chunk;
(items_per_chunk, chunk_count)
};
let mut chunkz = Vec::new();
if list.is_empty() { if list.is_empty() {
// handle and empty list // handle and empty list
data.append(&mut vec![0; SSZ_CHUNK_SIZE]); chunkz.append(&mut vec![0; SSZ_CHUNK_SIZE]);
} else { } else if list[0].len() <= SSZ_CHUNK_SIZE {
// just create a blob here; we'll divide into // just create a blob here; we'll divide into
// chunked slices when we merklize // chunked slices when we merklize
data.reserve(list[0].len() * list.len()); let mut chunk = Vec::with_capacity(chunk_size);
let mut item_count_in_chunk = 0;
chunkz.reserve(chunk_count * chunk_size);
for item in list.iter_mut() { for item in list.iter_mut() {
data.append(item); item_count_in_chunk += 1;
chunk.append(item);
// completed chunk?
if item_count_in_chunk == items_per_chunk {
zpad(&mut chunk, chunk_size);
chunkz.append(&mut chunk);
item_count_in_chunk = 0;
}
}
// left-over uncompleted chunk?
if item_count_in_chunk != 0 {
zpad(&mut chunk, chunk_size);
chunkz.append(&mut chunk);
}
} else {
// chunks larger than SSZ_CHUNK_SIZE
chunkz.reserve(chunk_count * chunk_size);
for item in list.iter_mut() {
chunkz.append(item);
} }
} }
(chunk_size, data)
(chunk_size, chunkz)
}
/// right pads with zeros making 'bytes' 'size' in length
fn zpad(bytes: &mut Vec<u8>, size: usize) {
if bytes.len() < size {
bytes.resize(size, 0);
}
} }
#[cfg(test)] #[cfg(test)]

View File

@ -0,0 +1,14 @@
[package]
name = "ssz_derive"
version = "0.1.0"
authors = ["Paul Hauner <paul@paulhauner.com>"]
edition = "2018"
description = "Procedural derive macros for SSZ encoding and decoding."
[lib]
proc-macro = true
[dependencies]
syn = "0.15"
quote = "0.6"
ssz = { path = "../ssz" }

View File

@ -0,0 +1,128 @@
//! Provides the following procedural derive macros:
//!
//! - `#[derive(Encode)]`
//! - `#[derive(Decode)]`
//!
//! These macros provide SSZ encoding/decoding for a `struct`. Fields are encoded/decoded in the
//! order they are defined.
//!
//! Presently, only `structs` with named fields are supported. `enum`s and tuple-structs are
//! unsupported.
//!
//! Example:
//! ```
//! use ssz::{ssz_encode, Decodable};
//! use ssz_derive::{Encode, Decode};
//!
//! #[derive(Encode, Decode)]
//! struct Foo {
//! pub bar: bool,
//! pub baz: u64,
//! }
//!
//! fn main() {
//! let foo = Foo {
//! bar: true,
//! baz: 42,
//! };
//!
//! let bytes = ssz_encode(&foo);
//!
//! let (decoded_foo, _i) = Foo::ssz_decode(&bytes, 0).unwrap();
//!
//! assert_eq!(foo.baz, decoded_foo.baz);
//! }
//! ```
extern crate proc_macro;
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, DeriveInput};
/// Returns a Vec of `syn::Ident` for each named field in the struct.
///
/// # Panics
/// Any unnamed struct field (like in a tuple struct) will raise a panic at compile time.
fn get_named_field_idents<'a>(struct_data: &'a syn::DataStruct) -> Vec<&'a syn::Ident> {
struct_data
.fields
.iter()
.map(|f| match &f.ident {
Some(ref ident) => ident,
_ => panic!("ssz_derive only supports named struct fields."),
})
.collect()
}
/// Implements `ssz::Encodable` for some `struct`.
///
/// Fields are encoded in the order they are defined.
#[proc_macro_derive(Encode)]
pub fn ssz_encode_derive(input: TokenStream) -> TokenStream {
let item = parse_macro_input!(input as DeriveInput);
let name = &item.ident;
let struct_data = match &item.data {
syn::Data::Struct(s) => s,
_ => panic!("ssz_derive only supports structs."),
};
let field_idents = get_named_field_idents(&struct_data);
let output = quote! {
impl ssz::Encodable for #name {
fn ssz_append(&self, s: &mut ssz::SszStream) {
#(
s.append(&self.#field_idents);
)*
}
}
};
output.into()
}
/// Implements `ssz::Decodable` for some `struct`.
///
/// Fields are decoded in the order they are defined.
#[proc_macro_derive(Decode)]
pub fn ssz_decode_derive(input: TokenStream) -> TokenStream {
let item = parse_macro_input!(input as DeriveInput);
let name = &item.ident;
let struct_data = match &item.data {
syn::Data::Struct(s) => s,
_ => panic!("ssz_derive only supports structs."),
};
let field_idents = get_named_field_idents(&struct_data);
// Using a var in an iteration always consumes the var, therefore we must make a `fields_a` and
// a `fields_b` in order to perform two loops.
//
// https://github.com/dtolnay/quote/issues/8
let field_idents_a = &field_idents;
let field_idents_b = &field_idents;
let output = quote! {
impl ssz::Decodable for #name {
fn ssz_decode(bytes: &[u8], i: usize) -> Result<(Self, usize), ssz::DecodeError> {
#(
let (#field_idents_a, i) = <_>::ssz_decode(bytes, i)?;
)*
Ok((
Self {
#(
#field_idents_b,
)*
},
i
))
}
}
};
output.into()
}

View File

@ -5,7 +5,7 @@ authors = ["Paul Hauner <paul@paulhauner.com>"]
edition = "2018" edition = "2018"
[dependencies] [dependencies]
block_producer = { path = "../eth2/block_producer" } block_proposer = { path = "../eth2/block_proposer" }
bls = { path = "../eth2/utils/bls" } bls = { path = "../eth2/utils/bls" }
clap = "2.32.0" clap = "2.32.0"
dirs = "1.0.3" dirs = "1.0.3"

View File

@ -1,4 +1,4 @@
use block_producer::{BeaconNode, BeaconNodeError, PublishOutcome}; use block_proposer::{BeaconNode, BeaconNodeError, PublishOutcome};
use protos::services::{ use protos::services::{
BeaconBlock as GrpcBeaconBlock, ProduceBeaconBlockRequest, PublishBeaconBlockRequest, BeaconBlock as GrpcBeaconBlock, ProduceBeaconBlockRequest, PublishBeaconBlockRequest,
}; };

View File

@ -1,7 +1,7 @@
mod beacon_block_grpc_client; mod beacon_block_grpc_client;
// mod block_producer_service; // mod block_producer_service;
use block_producer::{ use block_proposer::{
BeaconNode, BlockProducer, DutiesReader, PollOutcome as BlockProducerPollOutcome, Signer, BeaconNode, BlockProducer, DutiesReader, PollOutcome as BlockProducerPollOutcome, Signer,
}; };
use slog::{error, info, warn, Logger}; use slog::{error, info, warn, Logger};

View File

@ -1,4 +1,4 @@
use block_producer::{DutiesReader, DutiesReaderError}; use block_proposer::{DutiesReader, DutiesReaderError};
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::RwLock; use std::sync::RwLock;
use types::{Epoch, Slot}; use types::{Epoch, Slot};

View File

@ -1,7 +1,7 @@
use self::block_producer_service::{BeaconBlockGrpcClient, BlockProducerService}; use self::block_producer_service::{BeaconBlockGrpcClient, BlockProducerService};
use self::duties::{DutiesManager, DutiesManagerService, EpochDutiesMap}; use self::duties::{DutiesManager, DutiesManagerService, EpochDutiesMap};
use crate::config::ClientConfig; use crate::config::ClientConfig;
use block_producer::{test_utils::LocalSigner, BlockProducer}; use block_proposer::{test_utils::LocalSigner, BlockProducer};
use bls::Keypair; use bls::Keypair;
use clap::{App, Arg}; use clap::{App, Arg};
use grpcio::{ChannelBuilder, EnvBuilder}; use grpcio::{ChannelBuilder, EnvBuilder};