Update Fork struct to v0.5.0

This commit is contained in:
Paul Hauner 2019-03-15 13:33:07 +11:00
parent ef86948259
commit f9964ebd8b
No known key found for this signature in database
GPG Key ID: D362883A9218FCC6
4 changed files with 79 additions and 11 deletions

View File

@ -119,11 +119,7 @@ fn main() {
// Get domain from genesis fork_version // Get domain from genesis fork_version
spec.genesis_epoch, spec.genesis_epoch,
Domain::Deposit, Domain::Deposit,
&Fork { &Fork::genesis(&spec),
previous_version: spec.genesis_fork_version,
current_version: spec.genesis_fork_version,
epoch: spec.genesis_epoch,
},
), ),
), ),
}, },

View File

@ -1,4 +1,4 @@
use crate::{Address, Epoch, Fork, Hash256, Slot}; use crate::*;
use bls::Signature; use bls::Signature;
use int_to_bytes::int_to_bytes4; use int_to_bytes::int_to_bytes4;
use serde_derive::Deserialize; use serde_derive::Deserialize;
@ -127,7 +127,7 @@ impl ChainSpec {
/// Get the domain number that represents the fork meta and signature domain. /// 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 { pub fn get_domain(&self, epoch: Epoch, domain: Domain, fork: &Fork) -> u64 {
let domain_constant = match domain { let domain_constant = match domain {
Domain::Deposit => self.domain_deposit, Domain::Deposit => self.domain_deposit,
@ -138,9 +138,11 @@ impl ChainSpec {
Domain::Transfer => self.domain_transfer, Domain::Transfer => self.domain_transfer,
}; };
let mut bytes: Vec<u8> = fork.get_fork_version(epoch).to_vec();
bytes.append(&mut int_to_bytes4(domain_constant));
let mut fork_and_domain = [0; 8]; let mut fork_and_domain = [0; 8];
fork_and_domain.copy_from_slice(&fork.get_fork_version(epoch)); fork_and_domain.copy_from_slice(&bytes);
fork_and_domain.copy_from_slice(&int_to_bytes4(domain_constant));
u64::from_le_bytes(fork_and_domain) u64::from_le_bytes(fork_and_domain)
} }
@ -269,9 +271,34 @@ impl Default for ChainSpec {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use int_to_bytes::int_to_bytes8;
#[test] #[test]
fn test_foundation_spec_can_be_constructed() { fn test_foundation_spec_can_be_constructed() {
let _ = ChainSpec::foundation(); 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);
}
} }

View File

@ -48,4 +48,49 @@ mod tests {
use super::*; use super::*;
ssz_tests!(Fork); 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);
}
} }

View File

@ -81,8 +81,8 @@ impl DutiesReader for EpochDutiesMap {
// //
// It will almost certainly cause signatures to fail verification. // It will almost certainly cause signatures to fail verification.
Ok(Fork { Ok(Fork {
previous_version: 0, previous_version: [0; 4],
current_version: 0, current_version: [0; 4],
epoch: Epoch::new(0), epoch: Epoch::new(0),
}) })
} }