Merge pull request #251 from mjkeating/tree_hash_macro

Use Hashtree macro for most struct types
This commit is contained in:
Paul Hauner 2019-02-26 09:14:37 +13:00 committed by GitHub
commit 87ea95ce35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
25 changed files with 108 additions and 325 deletions

View File

@ -2,10 +2,10 @@ use super::{AggregatePublicKey, AggregateSignature, AttestationData, Bitfield, H
use crate::test_utils::TestRandom;
use rand::RngCore;
use serde_derive::Serialize;
use ssz::{hash, TreeHash};
use ssz_derive::{Decode, Encode};
use ssz::TreeHash;
use ssz_derive::{Decode, Encode, TreeHash};
#[derive(Debug, Clone, PartialEq, Serialize, Encode, Decode)]
#[derive(Debug, Clone, PartialEq, Serialize, Encode, Decode, TreeHash)]
pub struct Attestation {
pub aggregation_bitfield: Bitfield,
pub data: AttestationData,
@ -36,17 +36,6 @@ impl Attestation {
}
}
impl TreeHash for Attestation {
fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![];
result.append(&mut self.aggregation_bitfield.hash_tree_root_internal());
result.append(&mut self.data.hash_tree_root_internal());
result.append(&mut self.custody_bitfield.hash_tree_root_internal());
result.append(&mut self.aggregate_signature.hash_tree_root_internal());
hash(&result)
}
}
impl<T: RngCore> TestRandom<T> for Attestation {
fn random_for_test(rng: &mut T) -> Self {
Self {
@ -62,7 +51,7 @@ impl<T: RngCore> TestRandom<T> for Attestation {
mod tests {
use super::*;
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
use ssz::{ssz_encode, Decodable};
use ssz::{ssz_encode, Decodable, TreeHash};
#[test]
pub fn test_ssz_round_trip() {

View File

@ -2,8 +2,8 @@ use crate::test_utils::TestRandom;
use crate::{AttestationDataAndCustodyBit, Crosslink, Epoch, Hash256, Slot};
use rand::RngCore;
use serde_derive::Serialize;
use ssz::{hash, TreeHash};
use ssz_derive::{Decode, Encode};
use ssz::TreeHash;
use ssz_derive::{Decode, Encode, TreeHash};
pub const SSZ_ATTESTION_DATA_LENGTH: usize = {
8 + // slot
@ -16,7 +16,7 @@ pub const SSZ_ATTESTION_DATA_LENGTH: usize = {
32 // justified_block_root
};
#[derive(Debug, Clone, PartialEq, Default, Serialize, Hash, Encode, Decode)]
#[derive(Debug, Clone, PartialEq, Default, Serialize, Hash, Encode, Decode, TreeHash)]
pub struct AttestationData {
pub slot: Slot,
pub shard: u64,
@ -44,21 +44,6 @@ impl AttestationData {
}
}
impl TreeHash for AttestationData {
fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![];
result.append(&mut self.slot.hash_tree_root_internal());
result.append(&mut self.shard.hash_tree_root_internal());
result.append(&mut self.beacon_block_root.hash_tree_root_internal());
result.append(&mut self.epoch_boundary_root.hash_tree_root_internal());
result.append(&mut self.shard_block_root.hash_tree_root_internal());
result.append(&mut self.latest_crosslink.hash_tree_root_internal());
result.append(&mut self.justified_epoch.hash_tree_root_internal());
result.append(&mut self.justified_block_root.hash_tree_root_internal());
hash(&result)
}
}
impl<T: RngCore> TestRandom<T> for AttestationData {
fn random_for_test(rng: &mut T) -> Self {
Self {
@ -78,7 +63,7 @@ impl<T: RngCore> TestRandom<T> for AttestationData {
mod tests {
use super::*;
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
use ssz::{ssz_encode, Decodable};
use ssz::{ssz_encode, Decodable, TreeHash};
#[test]
pub fn test_ssz_round_trip() {

View File

@ -2,25 +2,14 @@ use super::AttestationData;
use crate::test_utils::TestRandom;
use rand::RngCore;
use serde_derive::Serialize;
use ssz::TreeHash;
use ssz_derive::{Decode, Encode};
use ssz_derive::{Decode, Encode, TreeHash};
#[derive(Debug, Clone, PartialEq, Default, Serialize, Encode, Decode)]
#[derive(Debug, Clone, PartialEq, Default, Serialize, Encode, Decode, TreeHash)]
pub struct AttestationDataAndCustodyBit {
pub data: AttestationData,
pub custody_bit: bool,
}
impl TreeHash for AttestationDataAndCustodyBit {
fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![];
result.append(&mut self.data.hash_tree_root_internal());
// TODO: add bool ssz
// result.append(custody_bit.hash_tree_root_internal());
ssz::hash(&result)
}
}
impl<T: RngCore> TestRandom<T> for AttestationDataAndCustodyBit {
fn random_for_test(rng: &mut T) -> Self {
Self {
@ -35,7 +24,7 @@ impl<T: RngCore> TestRandom<T> for AttestationDataAndCustodyBit {
mod test {
use super::*;
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
use ssz::{ssz_encode, Decodable};
use ssz::{ssz_encode, Decodable, TreeHash};
#[test]
pub fn test_ssz_round_trip() {

View File

@ -1,24 +1,14 @@
use crate::{test_utils::TestRandom, SlashableAttestation};
use rand::RngCore;
use serde_derive::Serialize;
use ssz::{hash, TreeHash};
use ssz_derive::{Decode, Encode};
use ssz_derive::{Decode, Encode, TreeHash};
#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode)]
#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash)]
pub struct AttesterSlashing {
pub slashable_attestation_1: SlashableAttestation,
pub slashable_attestation_2: SlashableAttestation,
}
impl TreeHash for AttesterSlashing {
fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![];
result.append(&mut self.slashable_attestation_1.hash_tree_root_internal());
result.append(&mut self.slashable_attestation_2.hash_tree_root_internal());
hash(&result)
}
}
impl<T: RngCore> TestRandom<T> for AttesterSlashing {
fn random_for_test(rng: &mut T) -> Self {
Self {
@ -32,7 +22,7 @@ impl<T: RngCore> TestRandom<T> for AttesterSlashing {
mod tests {
use super::*;
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
use ssz::{ssz_encode, Decodable};
use ssz::{ssz_encode, Decodable, TreeHash};
#[test]
pub fn test_ssz_round_trip() {

View File

@ -3,10 +3,10 @@ use crate::{BeaconBlockBody, ChainSpec, Eth1Data, Hash256, ProposalSignedData, S
use bls::Signature;
use rand::RngCore;
use serde_derive::Serialize;
use ssz::{hash, TreeHash};
use ssz_derive::{Decode, Encode};
use ssz::TreeHash;
use ssz_derive::{Decode, Encode, TreeHash};
#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode)]
#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash)]
pub struct BeaconBlock {
pub slot: Slot,
pub parent_root: Hash256,
@ -60,20 +60,6 @@ impl BeaconBlock {
}
}
impl TreeHash for BeaconBlock {
fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![];
result.append(&mut self.slot.hash_tree_root_internal());
result.append(&mut self.parent_root.hash_tree_root_internal());
result.append(&mut self.state_root.hash_tree_root_internal());
result.append(&mut self.randao_reveal.hash_tree_root_internal());
result.append(&mut self.eth1_data.hash_tree_root_internal());
result.append(&mut self.signature.hash_tree_root_internal());
result.append(&mut self.body.hash_tree_root_internal());
hash(&result)
}
}
impl<T: RngCore> TestRandom<T> for BeaconBlock {
fn random_for_test(rng: &mut T) -> Self {
Self {
@ -92,7 +78,7 @@ impl<T: RngCore> TestRandom<T> for BeaconBlock {
mod tests {
use super::*;
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
use ssz::{ssz_encode, Decodable};
use ssz::{ssz_encode, Decodable, TreeHash};
#[test]
pub fn test_ssz_round_trip() {

View File

@ -2,10 +2,9 @@ use super::{Attestation, AttesterSlashing, Deposit, Exit, ProposerSlashing};
use crate::test_utils::TestRandom;
use rand::RngCore;
use serde_derive::Serialize;
use ssz::{hash, TreeHash};
use ssz_derive::{Decode, Encode};
use ssz_derive::{Decode, Encode, TreeHash};
#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode)]
#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode, TreeHash)]
pub struct BeaconBlockBody {
pub proposer_slashings: Vec<ProposerSlashing>,
pub attester_slashings: Vec<AttesterSlashing>,
@ -14,18 +13,6 @@ pub struct BeaconBlockBody {
pub exits: Vec<Exit>,
}
impl TreeHash for BeaconBlockBody {
fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![];
result.append(&mut self.proposer_slashings.hash_tree_root_internal());
result.append(&mut self.attester_slashings.hash_tree_root_internal());
result.append(&mut self.attestations.hash_tree_root_internal());
result.append(&mut self.deposits.hash_tree_root_internal());
result.append(&mut self.exits.hash_tree_root_internal());
hash(&result)
}
}
impl<T: RngCore> TestRandom<T> for BeaconBlockBody {
fn random_for_test(rng: &mut T) -> Self {
Self {
@ -42,7 +29,7 @@ impl<T: RngCore> TestRandom<T> for BeaconBlockBody {
mod tests {
use super::*;
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
use ssz::{ssz_encode, Decodable};
use ssz::{ssz_encode, Decodable, TreeHash};
#[test]
pub fn test_ssz_round_trip() {

View File

@ -2,24 +2,14 @@ use super::SlashableVoteData;
use crate::test_utils::TestRandom;
use rand::RngCore;
use serde_derive::Serialize;
use ssz::{hash, TreeHash};
use ssz_derive::{Decode, Encode};
use ssz_derive::{Decode, Encode, TreeHash};
#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode)]
#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash)]
pub struct CasperSlashing {
pub slashable_vote_data_1: SlashableVoteData,
pub slashable_vote_data_2: SlashableVoteData,
}
impl TreeHash for CasperSlashing {
fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![];
result.append(&mut self.slashable_vote_data_1.hash_tree_root_internal());
result.append(&mut self.slashable_vote_data_2.hash_tree_root_internal());
hash(&result)
}
}
impl<T: RngCore> TestRandom<T> for CasperSlashing {
fn random_for_test(rng: &mut T) -> Self {
Self {
@ -33,7 +23,7 @@ impl<T: RngCore> TestRandom<T> for CasperSlashing {
mod tests {
use super::*;
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
use ssz::{ssz_encode, Decodable};
use ssz::{ssz_encode, Decodable, TreeHash};
#[test]
pub fn test_ssz_round_trip() {

View File

@ -2,10 +2,9 @@ use crate::test_utils::TestRandom;
use crate::{Epoch, Hash256};
use rand::RngCore;
use serde_derive::Serialize;
use ssz::{hash, TreeHash};
use ssz_derive::{Decode, Encode};
use ssz_derive::{Decode, Encode, TreeHash};
#[derive(Debug, Clone, PartialEq, Default, Serialize, Hash, Encode, Decode)]
#[derive(Debug, Clone, PartialEq, Default, Serialize, Hash, Encode, Decode, TreeHash)]
pub struct Crosslink {
pub epoch: Epoch,
pub shard_block_root: Hash256,
@ -21,15 +20,6 @@ impl Crosslink {
}
}
impl TreeHash for Crosslink {
fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![];
result.append(&mut self.epoch.hash_tree_root_internal());
result.append(&mut self.shard_block_root.hash_tree_root_internal());
hash(&result)
}
}
impl<T: RngCore> TestRandom<T> for Crosslink {
fn random_for_test(rng: &mut T) -> Self {
Self {
@ -43,7 +33,7 @@ impl<T: RngCore> TestRandom<T> for Crosslink {
mod tests {
use super::*;
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
use ssz::{ssz_encode, Decodable};
use ssz::{ssz_encode, Decodable, TreeHash};
#[test]
pub fn test_ssz_round_trip() {

View File

@ -2,26 +2,15 @@ use super::{DepositData, Hash256};
use crate::test_utils::TestRandom;
use rand::RngCore;
use serde_derive::Serialize;
use ssz::{hash, TreeHash};
use ssz_derive::{Decode, Encode};
use ssz_derive::{Decode, Encode, TreeHash};
#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode)]
#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash)]
pub struct Deposit {
pub branch: Vec<Hash256>,
pub index: u64,
pub deposit_data: DepositData,
}
impl TreeHash for Deposit {
fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![];
result.append(&mut self.branch.hash_tree_root_internal());
result.append(&mut self.index.hash_tree_root_internal());
result.append(&mut self.deposit_data.hash_tree_root_internal());
hash(&result)
}
}
impl<T: RngCore> TestRandom<T> for Deposit {
fn random_for_test(rng: &mut T) -> Self {
Self {
@ -36,7 +25,7 @@ impl<T: RngCore> TestRandom<T> for Deposit {
mod tests {
use super::*;
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
use ssz::{ssz_encode, Decodable};
use ssz::{ssz_encode, Decodable, TreeHash};
#[test]
pub fn test_ssz_round_trip() {

View File

@ -2,26 +2,15 @@ use super::DepositInput;
use crate::test_utils::TestRandom;
use rand::RngCore;
use serde_derive::Serialize;
use ssz::{hash, TreeHash};
use ssz_derive::{Decode, Encode};
use ssz_derive::{Decode, Encode, TreeHash};
#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode)]
#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash)]
pub struct DepositData {
pub amount: u64,
pub timestamp: u64,
pub deposit_input: DepositInput,
}
impl TreeHash for DepositData {
fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![];
result.append(&mut self.amount.hash_tree_root_internal());
result.append(&mut self.timestamp.hash_tree_root_internal());
result.append(&mut self.deposit_input.hash_tree_root_internal());
hash(&result)
}
}
impl<T: RngCore> TestRandom<T> for DepositData {
fn random_for_test(rng: &mut T) -> Self {
Self {
@ -36,7 +25,7 @@ impl<T: RngCore> TestRandom<T> for DepositData {
mod tests {
use super::*;
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
use ssz::{ssz_encode, Decodable};
use ssz::{ssz_encode, Decodable, TreeHash};
#[test]
pub fn test_ssz_round_trip() {

View File

@ -3,26 +3,15 @@ use crate::test_utils::TestRandom;
use bls::{PublicKey, Signature};
use rand::RngCore;
use serde_derive::Serialize;
use ssz::{hash, TreeHash};
use ssz_derive::{Decode, Encode};
use ssz_derive::{Decode, Encode, TreeHash};
#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode)]
#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash)]
pub struct DepositInput {
pub pubkey: PublicKey,
pub withdrawal_credentials: Hash256,
pub proof_of_possession: Signature,
}
impl TreeHash for DepositInput {
fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![];
result.append(&mut self.pubkey.hash_tree_root_internal());
result.append(&mut self.withdrawal_credentials.hash_tree_root_internal());
result.append(&mut self.proof_of_possession.hash_tree_root_internal());
hash(&result)
}
}
impl<T: RngCore> TestRandom<T> for DepositInput {
fn random_for_test(rng: &mut T) -> Self {
Self {
@ -37,7 +26,7 @@ impl<T: RngCore> TestRandom<T> for DepositInput {
mod tests {
use super::*;
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
use ssz::{ssz_encode, Decodable};
use ssz::{ssz_encode, Decodable, TreeHash};
#[test]
pub fn test_ssz_round_trip() {

View File

@ -2,25 +2,15 @@ use super::Hash256;
use crate::test_utils::TestRandom;
use rand::RngCore;
use serde_derive::Serialize;
use ssz::{hash, TreeHash};
use ssz_derive::{Decode, Encode};
use ssz_derive::{Decode, Encode, TreeHash};
// Note: this is refer to as DepositRootVote in specs
#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode)]
#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode, TreeHash)]
pub struct Eth1Data {
pub deposit_root: Hash256,
pub block_hash: Hash256,
}
impl TreeHash for Eth1Data {
fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![];
result.append(&mut self.deposit_root.hash_tree_root_internal());
result.append(&mut self.block_hash.hash_tree_root_internal());
hash(&result)
}
}
impl<T: RngCore> TestRandom<T> for Eth1Data {
fn random_for_test(rng: &mut T) -> Self {
Self {
@ -34,7 +24,7 @@ impl<T: RngCore> TestRandom<T> for Eth1Data {
mod tests {
use super::*;
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
use ssz::{ssz_encode, Decodable};
use ssz::{ssz_encode, Decodable, TreeHash};
#[test]
pub fn test_ssz_round_trip() {

View File

@ -2,25 +2,15 @@ use super::Eth1Data;
use crate::test_utils::TestRandom;
use rand::RngCore;
use serde_derive::Serialize;
use ssz::{hash, TreeHash};
use ssz_derive::{Decode, Encode};
use ssz_derive::{Decode, Encode, TreeHash};
// Note: this is refer to as DepositRootVote in specs
#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode)]
#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode, TreeHash)]
pub struct Eth1DataVote {
pub eth1_data: Eth1Data,
pub vote_count: u64,
}
impl TreeHash for Eth1DataVote {
fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![];
result.append(&mut self.eth1_data.hash_tree_root_internal());
result.append(&mut self.vote_count.hash_tree_root_internal());
hash(&result)
}
}
impl<T: RngCore> TestRandom<T> for Eth1DataVote {
fn random_for_test(rng: &mut T) -> Self {
Self {
@ -34,7 +24,7 @@ impl<T: RngCore> TestRandom<T> for Eth1DataVote {
mod tests {
use super::*;
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
use ssz::{ssz_encode, Decodable};
use ssz::{ssz_encode, Decodable, TreeHash};
#[test]
pub fn test_ssz_round_trip() {

View File

@ -2,26 +2,15 @@ use crate::{test_utils::TestRandom, Epoch};
use bls::Signature;
use rand::RngCore;
use serde_derive::Serialize;
use ssz::{hash, TreeHash};
use ssz_derive::{Decode, Encode};
use ssz_derive::{Decode, Encode, TreeHash};
#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode)]
#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash)]
pub struct Exit {
pub epoch: Epoch,
pub validator_index: u64,
pub signature: Signature,
}
impl TreeHash for Exit {
fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![];
result.append(&mut self.epoch.hash_tree_root_internal());
result.append(&mut self.validator_index.hash_tree_root_internal());
result.append(&mut self.signature.hash_tree_root_internal());
hash(&result)
}
}
impl<T: RngCore> TestRandom<T> for Exit {
fn random_for_test(rng: &mut T) -> Self {
Self {
@ -36,7 +25,7 @@ impl<T: RngCore> TestRandom<T> for Exit {
mod tests {
use super::*;
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
use ssz::{ssz_encode, Decodable};
use ssz::{ssz_encode, Decodable, TreeHash};
#[test]
pub fn test_ssz_round_trip() {

View File

@ -1,10 +1,9 @@
use crate::{test_utils::TestRandom, Epoch};
use rand::RngCore;
use serde_derive::Serialize;
use ssz::{hash, TreeHash};
use ssz_derive::{Decode, Encode};
use ssz_derive::{Decode, Encode, TreeHash};
#[derive(Debug, Clone, PartialEq, Default, Serialize, Encode, Decode)]
#[derive(Debug, Clone, PartialEq, Default, Serialize, Encode, Decode, TreeHash)]
pub struct Fork {
pub previous_version: u64,
pub current_version: u64,
@ -27,16 +26,6 @@ impl Fork {
}
}
impl TreeHash for Fork {
fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![];
result.append(&mut self.previous_version.hash_tree_root_internal());
result.append(&mut self.current_version.hash_tree_root_internal());
result.append(&mut self.epoch.hash_tree_root_internal());
hash(&result)
}
}
impl<T: RngCore> TestRandom<T> for Fork {
fn random_for_test(rng: &mut T) -> Self {
Self {
@ -51,7 +40,7 @@ impl<T: RngCore> TestRandom<T> for Fork {
mod tests {
use super::*;
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
use ssz::{ssz_encode, Decodable};
use ssz::{ssz_encode, Decodable, TreeHash};
#[test]
pub fn test_ssz_round_trip() {

View File

@ -2,10 +2,9 @@ use crate::test_utils::TestRandom;
use crate::{AttestationData, Bitfield, Slot};
use rand::RngCore;
use serde_derive::Serialize;
use ssz::{hash, TreeHash};
use ssz_derive::{Decode, Encode};
use ssz_derive::{Decode, Encode, TreeHash};
#[derive(Debug, Clone, PartialEq, Serialize, Encode, Decode)]
#[derive(Debug, Clone, PartialEq, Serialize, Encode, Decode, TreeHash)]
pub struct PendingAttestation {
pub aggregation_bitfield: Bitfield,
pub data: AttestationData,
@ -13,17 +12,6 @@ pub struct PendingAttestation {
pub inclusion_slot: Slot,
}
impl TreeHash for PendingAttestation {
fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![];
result.append(&mut self.aggregation_bitfield.hash_tree_root_internal());
result.append(&mut self.data.hash_tree_root_internal());
result.append(&mut self.custody_bitfield.hash_tree_root_internal());
result.append(&mut self.inclusion_slot.hash_tree_root_internal());
hash(&result)
}
}
impl<T: RngCore> TestRandom<T> for PendingAttestation {
fn random_for_test(rng: &mut T) -> Self {
Self {
@ -39,7 +27,7 @@ impl<T: RngCore> TestRandom<T> for PendingAttestation {
mod tests {
use super::*;
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
use ssz::{ssz_encode, Decodable};
use ssz::{ssz_encode, Decodable, TreeHash};
#[test]
pub fn test_ssz_round_trip() {

View File

@ -2,26 +2,15 @@ use crate::test_utils::TestRandom;
use crate::{Hash256, Slot};
use rand::RngCore;
use serde_derive::Serialize;
use ssz::{hash, TreeHash};
use ssz_derive::{Decode, Encode};
use ssz_derive::{Decode, Encode, TreeHash};
#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode)]
#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode, TreeHash)]
pub struct ProposalSignedData {
pub slot: Slot,
pub shard: u64,
pub block_root: Hash256,
}
impl TreeHash for ProposalSignedData {
fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![];
result.append(&mut self.slot.hash_tree_root_internal());
result.append(&mut self.shard.hash_tree_root_internal());
result.append(&mut self.block_root.hash_tree_root_internal());
hash(&result)
}
}
impl<T: RngCore> TestRandom<T> for ProposalSignedData {
fn random_for_test(rng: &mut T) -> Self {
Self {
@ -36,7 +25,7 @@ impl<T: RngCore> TestRandom<T> for ProposalSignedData {
mod tests {
use super::*;
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
use ssz::{ssz_encode, Decodable};
use ssz::{ssz_encode, Decodable, TreeHash};
#[test]
pub fn test_ssz_round_trip() {

View File

@ -3,10 +3,9 @@ use crate::test_utils::TestRandom;
use bls::Signature;
use rand::RngCore;
use serde_derive::Serialize;
use ssz::{hash, TreeHash};
use ssz_derive::{Decode, Encode};
use ssz_derive::{Decode, Encode, TreeHash};
#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode)]
#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash)]
pub struct ProposerSlashing {
pub proposer_index: u64,
pub proposal_data_1: ProposalSignedData,
@ -15,18 +14,6 @@ pub struct ProposerSlashing {
pub proposal_signature_2: Signature,
}
impl TreeHash for ProposerSlashing {
fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![];
result.append(&mut self.proposer_index.hash_tree_root_internal());
result.append(&mut self.proposal_data_1.hash_tree_root_internal());
result.append(&mut self.proposal_signature_1.hash_tree_root_internal());
result.append(&mut self.proposal_data_2.hash_tree_root_internal());
result.append(&mut self.proposal_signature_2.hash_tree_root_internal());
hash(&result)
}
}
impl<T: RngCore> TestRandom<T> for ProposerSlashing {
fn random_for_test(rng: &mut T) -> Self {
Self {
@ -43,7 +30,7 @@ impl<T: RngCore> TestRandom<T> for ProposerSlashing {
mod tests {
use super::*;
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
use ssz::{ssz_encode, Decodable};
use ssz::{ssz_encode, Decodable, TreeHash};
#[test]
pub fn test_ssz_round_trip() {

View File

@ -1,26 +1,15 @@
use crate::{test_utils::TestRandom, Slot};
use rand::RngCore;
use serde_derive::Serialize;
use ssz::{hash, TreeHash};
use ssz_derive::{Decode, Encode};
use ssz_derive::{Decode, Encode, TreeHash};
#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode)]
#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash)]
pub struct ShardReassignmentRecord {
pub validator_index: u64,
pub shard: u64,
pub slot: Slot,
}
impl TreeHash for ShardReassignmentRecord {
fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![];
result.append(&mut self.validator_index.hash_tree_root_internal());
result.append(&mut self.shard.hash_tree_root_internal());
result.append(&mut self.slot.hash_tree_root_internal());
hash(&result)
}
}
impl<T: RngCore> TestRandom<T> for ShardReassignmentRecord {
fn random_for_test(rng: &mut T) -> Self {
Self {
@ -35,7 +24,7 @@ impl<T: RngCore> TestRandom<T> for ShardReassignmentRecord {
mod tests {
use super::*;
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
use ssz::{ssz_encode, Decodable};
use ssz::{ssz_encode, Decodable, TreeHash};
#[test]
pub fn test_ssz_round_trip() {

View File

@ -1,10 +1,9 @@
use crate::{test_utils::TestRandom, AggregateSignature, AttestationData, Bitfield};
use rand::RngCore;
use serde_derive::Serialize;
use ssz::{hash, TreeHash};
use ssz_derive::{Decode, Encode};
use ssz_derive::{Decode, Encode, TreeHash};
#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode)]
#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash)]
pub struct SlashableAttestation {
pub validator_indices: Vec<u64>,
pub data: AttestationData,
@ -12,17 +11,6 @@ pub struct SlashableAttestation {
pub aggregate_signature: AggregateSignature,
}
impl TreeHash for SlashableAttestation {
fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![];
result.append(&mut self.validator_indices.hash_tree_root_internal());
result.append(&mut self.data.hash_tree_root_internal());
result.append(&mut self.custody_bitfield.hash_tree_root_internal());
result.append(&mut self.aggregate_signature.hash_tree_root_internal());
hash(&result)
}
}
impl<T: RngCore> TestRandom<T> for SlashableAttestation {
fn random_for_test(rng: &mut T) -> Self {
Self {
@ -38,7 +26,7 @@ impl<T: RngCore> TestRandom<T> for SlashableAttestation {
mod tests {
use super::*;
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
use ssz::{ssz_encode, Decodable};
use ssz::{ssz_encode, Decodable, TreeHash};
#[test]
pub fn test_ssz_round_trip() {

View File

@ -4,10 +4,9 @@ use crate::test_utils::TestRandom;
use bls::AggregateSignature;
use rand::RngCore;
use serde_derive::Serialize;
use ssz::{hash, TreeHash};
use ssz_derive::{Decode, Encode};
use ssz_derive::{Decode, Encode, TreeHash};
#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode)]
#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash)]
pub struct SlashableVoteData {
pub custody_bit_0_indices: Vec<u32>,
pub custody_bit_1_indices: Vec<u32>,
@ -36,17 +35,6 @@ impl SlashableVoteData {
}
}
impl TreeHash for SlashableVoteData {
fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![];
result.append(&mut self.custody_bit_0_indices.hash_tree_root_internal());
result.append(&mut self.custody_bit_1_indices.hash_tree_root_internal());
result.append(&mut self.data.hash_tree_root_internal());
result.append(&mut self.aggregate_signature.hash_tree_root_internal());
hash(&result)
}
}
impl<T: RngCore> TestRandom<T> for SlashableVoteData {
fn random_for_test(rng: &mut T) -> Self {
Self {
@ -64,7 +52,7 @@ mod tests {
use crate::chain_spec::ChainSpec;
use crate::slot_epoch::{Epoch, Slot};
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
use ssz::{ssz_encode, Decodable};
use ssz::{ssz_encode, Decodable, TreeHash};
#[test]
pub fn test_is_double_vote_true() {

View File

@ -2,11 +2,10 @@ use crate::{test_utils::TestRandom, Hash256, Slot};
use bls::PublicKey;
use rand::RngCore;
use serde_derive::Serialize;
use ssz::{hash, TreeHash};
use ssz_derive::{Decode, Encode};
use ssz_derive::{Decode, Encode, TreeHash};
// The information gathered from the PoW chain validator registration function.
#[derive(Debug, Clone, PartialEq, Serialize, Encode, Decode)]
#[derive(Debug, Clone, PartialEq, Serialize, Encode, Decode, TreeHash)]
pub struct ValidatorRegistryDeltaBlock {
pub latest_registry_delta_root: Hash256,
pub validator_index: u32,
@ -28,18 +27,6 @@ impl Default for ValidatorRegistryDeltaBlock {
}
}
impl TreeHash for ValidatorRegistryDeltaBlock {
fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![];
result.append(&mut self.latest_registry_delta_root.hash_tree_root_internal());
result.append(&mut self.validator_index.hash_tree_root_internal());
result.append(&mut self.pubkey.hash_tree_root_internal());
result.append(&mut self.slot.hash_tree_root_internal());
result.append(&mut self.flag.hash_tree_root_internal());
hash(&result)
}
}
impl<T: RngCore> TestRandom<T> for ValidatorRegistryDeltaBlock {
fn random_for_test(rng: &mut T) -> Self {
Self {
@ -56,7 +43,7 @@ impl<T: RngCore> TestRandom<T> for ValidatorRegistryDeltaBlock {
mod tests {
use super::*;
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
use ssz::{ssz_encode, Decodable};
use ssz::{ssz_encode, Decodable, TreeHash};
#[test]
pub fn test_ssz_round_trip() {

View File

@ -32,6 +32,12 @@ impl TreeHash for usize {
}
}
impl TreeHash for bool {
fn hash_tree_root_internal(&self) -> Vec<u8> {
ssz_encode(self)
}
}
impl TreeHash for Address {
fn hash_tree_root_internal(&self) -> Vec<u8> {
ssz_encode(self)

View File

@ -7,9 +7,7 @@ pub trait TreeHash {
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);
}
zpad(&mut result, HASHSIZE);
result
}
}

View File

@ -2,6 +2,7 @@
//!
//! - `#[derive(Encode)]`
//! - `#[derive(Decode)]`
//! - `#[derive(TreeHash)]`
//!
//! These macros provide SSZ encoding/decoding for a `struct`. Fields are encoded/decoded in the
//! order they are defined.
@ -126,3 +127,34 @@ pub fn ssz_decode_derive(input: TokenStream) -> TokenStream {
};
output.into()
}
/// Implements `ssz::TreeHash` for some `struct`.
///
/// Fields are processed in the order they are defined.
#[proc_macro_derive(TreeHash)]
pub fn ssz_tree_hash_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::TreeHash for #name {
fn hash_tree_root_internal(&self) -> Vec<u8> {
let mut result: Vec<u8> = vec![];
#(
result.append(&mut self.#field_idents.hash_tree_root_internal());
)*
ssz::hash(&result)
}
}
};
output.into()
}