diff --git a/beacon_node/src/main.rs b/beacon_node/src/main.rs index c3182c789b..eacbffa3ef 100644 --- a/beacon_node/src/main.rs +++ b/beacon_node/src/main.rs @@ -119,11 +119,7 @@ fn main() { // Get domain from genesis fork_version spec.genesis_epoch, Domain::Deposit, - &Fork { - previous_version: spec.genesis_fork_version, - current_version: spec.genesis_fork_version, - epoch: spec.genesis_epoch, - }, + &Fork::genesis(&spec), ), ), }, diff --git a/eth2/types/src/chain_spec.rs b/eth2/types/src/chain_spec.rs index 108516695d..ae521cc92a 100644 --- a/eth2/types/src/chain_spec.rs +++ b/eth2/types/src/chain_spec.rs @@ -1,4 +1,4 @@ -use crate::{Address, Epoch, Fork, Hash256, Slot}; +use crate::*; use bls::Signature; use int_to_bytes::int_to_bytes4; use serde_derive::Deserialize; @@ -127,7 +127,7 @@ impl ChainSpec { /// Get the domain number that represents the fork meta and signature domain. /// - /// Spec v0.4.0 + /// Spec v0.5.0 pub fn get_domain(&self, epoch: Epoch, domain: Domain, fork: &Fork) -> u64 { let domain_constant = match domain { Domain::Deposit => self.domain_deposit, @@ -138,9 +138,11 @@ impl ChainSpec { Domain::Transfer => self.domain_transfer, }; + let mut bytes: Vec = fork.get_fork_version(epoch).to_vec(); + bytes.append(&mut int_to_bytes4(domain_constant)); + let mut fork_and_domain = [0; 8]; - fork_and_domain.copy_from_slice(&fork.get_fork_version(epoch)); - fork_and_domain.copy_from_slice(&int_to_bytes4(domain_constant)); + fork_and_domain.copy_from_slice(&bytes); u64::from_le_bytes(fork_and_domain) } @@ -269,9 +271,34 @@ impl Default for ChainSpec { #[cfg(test)] mod tests { use super::*; + use int_to_bytes::int_to_bytes8; #[test] fn test_foundation_spec_can_be_constructed() { let _ = ChainSpec::foundation(); } + + fn test_domain(domain_type: Domain, raw_domain: u32, spec: &ChainSpec) { + let fork = Fork::genesis(&spec); + let epoch = Epoch::new(0); + + let domain = spec.get_domain(epoch, domain_type, &fork); + + let mut expected = fork.get_fork_version(epoch).to_vec(); + expected.append(&mut int_to_bytes4(raw_domain)); + + assert_eq!(int_to_bytes8(domain), expected); + } + + #[test] + fn test_get_domain() { + let spec = ChainSpec::foundation(); + + test_domain(Domain::Deposit, spec.domain_deposit, &spec); + test_domain(Domain::Attestation, spec.domain_attestation, &spec); + test_domain(Domain::Proposal, spec.domain_proposal, &spec); + test_domain(Domain::Exit, spec.domain_exit, &spec); + test_domain(Domain::Randao, spec.domain_randao, &spec); + test_domain(Domain::Transfer, spec.domain_transfer, &spec); + } } diff --git a/eth2/types/src/fork.rs b/eth2/types/src/fork.rs index f0e3d10467..b780b95ef3 100644 --- a/eth2/types/src/fork.rs +++ b/eth2/types/src/fork.rs @@ -48,4 +48,49 @@ mod tests { use super::*; ssz_tests!(Fork); + + fn test_genesis(version: u32, epoch: Epoch) { + let mut spec = ChainSpec::foundation(); + + spec.genesis_fork_version = version; + spec.genesis_epoch = epoch; + + let fork = Fork::genesis(&spec); + + assert_eq!(fork.epoch, spec.genesis_epoch, "epoch incorrect"); + assert_eq!( + fork.previous_version, fork.current_version, + "previous and current are not identical" + ); + assert_eq!( + fork.current_version, + version.to_le_bytes(), + "current version incorrect" + ); + } + + #[test] + fn genesis() { + test_genesis(0, Epoch::new(0)); + test_genesis(9, Epoch::new(11)); + test_genesis(2_u32.pow(31), Epoch::new(2_u64.pow(63))); + test_genesis(u32::max_value(), Epoch::max_value()); + } + + #[test] + fn get_fork_version() { + let previous_version = [1; 4]; + let current_version = [2; 4]; + let epoch = Epoch::new(10); + + let fork = Fork { + previous_version, + current_version, + epoch, + }; + + assert_eq!(fork.get_fork_version(epoch - 1), previous_version); + assert_eq!(fork.get_fork_version(epoch), current_version); + assert_eq!(fork.get_fork_version(epoch + 1), current_version); + } } diff --git a/validator_client/src/duties/epoch_duties.rs b/validator_client/src/duties/epoch_duties.rs index 35668b4a9d..71f5f26ab5 100644 --- a/validator_client/src/duties/epoch_duties.rs +++ b/validator_client/src/duties/epoch_duties.rs @@ -81,8 +81,8 @@ impl DutiesReader for EpochDutiesMap { // // It will almost certainly cause signatures to fail verification. Ok(Fork { - previous_version: 0, - current_version: 0, + previous_version: [0; 4], + current_version: [0; 4], epoch: Epoch::new(0), }) }