Remove dupe info between ChainSpec and EthSpec

This commit is contained in:
Paul Hauner
2019-06-08 07:57:25 -04:00
parent f69d9093a3
commit e74d49fc8a
57 changed files with 299 additions and 252 deletions

View File

@@ -140,7 +140,7 @@ pub fn verify_block_signature<T: EthSpec>(
[state.get_beacon_proposer_index(block.slot, RelativeEpoch::Current, spec)?];
let domain = spec.get_domain(
block.slot.epoch(spec.slots_per_epoch),
block.slot.epoch(T::slots_per_epoch()),
Domain::BeaconProposer,
&state.fork,
);
@@ -172,7 +172,7 @@ pub fn process_randao<T: EthSpec>(
block.body.randao_reveal.verify(
&state.current_epoch().tree_hash_root()[..],
spec.get_domain(
block.slot.epoch(spec.slots_per_epoch),
block.slot.epoch(T::slots_per_epoch()),
Domain::Randao,
&state.fork
),

View File

@@ -55,11 +55,13 @@ impl<T: EthSpec> BlockProcessingBuilder<T> {
let keypair = &keypairs[proposer_index];
match randao_sk {
Some(sk) => builder.set_randao_reveal(&sk, &state.fork, spec),
None => builder.set_randao_reveal(&keypair.sk, &state.fork, spec),
Some(sk) => builder.set_randao_reveal::<T>(&sk, &state.fork, spec),
None => builder.set_randao_reveal::<T>(&keypair.sk, &state.fork, spec),
}
let block = self.block_builder.build(&keypair.sk, &state.fork, spec);
let block = self
.block_builder
.build::<T>(&keypair.sk, &state.fork, spec);
(block, state)
}

View File

@@ -9,7 +9,7 @@ pub const VALIDATOR_COUNT: usize = 10;
#[test]
fn valid_block_ok() {
let spec = FoundationEthSpec::spec();
let spec = FoundationEthSpec::default_spec();
let builder = get_builder(&spec);
let (block, mut state) = builder.build(None, None, &spec);
@@ -20,7 +20,7 @@ fn valid_block_ok() {
#[test]
fn invalid_block_header_state_slot() {
let spec = FoundationEthSpec::spec();
let spec = FoundationEthSpec::default_spec();
let builder = get_builder(&spec);
let (mut block, mut state) = builder.build(None, None, &spec);
@@ -39,7 +39,7 @@ fn invalid_block_header_state_slot() {
#[test]
fn invalid_parent_block_root() {
let spec = FoundationEthSpec::spec();
let spec = FoundationEthSpec::default_spec();
let builder = get_builder(&spec);
let invalid_parent_root = Hash256::from([0xAA; 32]);
let (block, mut state) = builder.build(None, Some(invalid_parent_root), &spec);
@@ -59,14 +59,14 @@ fn invalid_parent_block_root() {
#[test]
fn invalid_block_signature() {
let spec = FoundationEthSpec::spec();
let spec = FoundationEthSpec::default_spec();
let builder = get_builder(&spec);
let (mut block, mut state) = builder.build(None, None, &spec);
// sign the block with a keypair that is not the expected proposer
let keypair = Keypair::random();
let message = block.signed_root();
let epoch = block.slot.epoch(spec.slots_per_epoch);
let epoch = block.slot.epoch(FoundationEthSpec::slots_per_epoch());
let domain = spec.get_domain(epoch, Domain::BeaconProposer, &state.fork);
block.signature = Signature::new(&message, domain, &keypair.sk);
@@ -82,7 +82,7 @@ fn invalid_block_signature() {
#[test]
fn invalid_randao_reveal_signature() {
let spec = FoundationEthSpec::spec();
let spec = FoundationEthSpec::default_spec();
let builder = get_builder(&spec);
// sign randao reveal with random keypair
@@ -104,7 +104,8 @@ fn get_builder(spec: &ChainSpec) -> (BlockProcessingBuilder<FoundationEthSpec>)
let mut builder = BlockProcessingBuilder::new(VALIDATOR_COUNT, &spec);
// Set the state and block to be in the last slot of the 4th epoch.
let last_slot_of_epoch = (spec.genesis_epoch + 4).end_slot(spec.slots_per_epoch);
let last_slot_of_epoch =
(spec.genesis_epoch + 4).end_slot(FoundationEthSpec::slots_per_epoch());
builder.set_slot(last_slot_of_epoch, &spec);
builder.build_caches(&spec);

View File

@@ -68,7 +68,7 @@ fn validate_attestation_parametric<T: EthSpec>(
}
);
verify!(
state.slot <= attestation_slot + spec.slots_per_epoch,
state.slot <= attestation_slot + T::slots_per_epoch(),
Invalid::IncludedTooLate {
state: state.slot,
attestation: attestation_slot

View File

@@ -21,8 +21,8 @@ pub fn verify_proposer_slashing<T: EthSpec>(
})?;
verify!(
proposer_slashing.header_1.slot.epoch(spec.slots_per_epoch)
== proposer_slashing.header_2.slot.epoch(spec.slots_per_epoch),
proposer_slashing.header_1.slot.epoch(T::slots_per_epoch())
== proposer_slashing.header_2.slot.epoch(T::slots_per_epoch()),
Invalid::ProposalEpochMismatch(
proposer_slashing.header_1.slot,
proposer_slashing.header_2.slot
@@ -40,7 +40,7 @@ pub fn verify_proposer_slashing<T: EthSpec>(
);
verify!(
verify_header_signature(
verify_header_signature::<T>(
&proposer_slashing.header_1,
&proposer.pubkey,
&state.fork,
@@ -49,7 +49,7 @@ pub fn verify_proposer_slashing<T: EthSpec>(
Invalid::BadProposal1Signature
);
verify!(
verify_header_signature(
verify_header_signature::<T>(
&proposer_slashing.header_2,
&proposer.pubkey,
&state.fork,
@@ -66,7 +66,7 @@ pub fn verify_proposer_slashing<T: EthSpec>(
/// Returns `true` if the signature is valid.
///
/// Spec v0.6.1
fn verify_header_signature(
fn verify_header_signature<T: EthSpec>(
header: &BeaconBlockHeader,
pubkey: &PublicKey,
fork: &Fork,
@@ -74,7 +74,7 @@ fn verify_header_signature(
) -> bool {
let message = header.signed_root();
let domain = spec.get_domain(
header.slot.epoch(spec.slots_per_epoch),
header.slot.epoch(T::slots_per_epoch()),
Domain::BeaconProposer,
fork,
);

View File

@@ -101,7 +101,7 @@ fn verify_transfer_parametric<T: EthSpec>(
.get(transfer.sender as usize)
.ok_or_else(|| Error::Invalid(Invalid::FromValidatorUnknown(transfer.sender)))?;
let epoch = state.slot.epoch(spec.slots_per_epoch);
let epoch = state.slot.epoch(T::slots_per_epoch());
// Ensure one of the following is met:
//
@@ -136,7 +136,7 @@ fn verify_transfer_parametric<T: EthSpec>(
// Verify the transfer signature.
let message = transfer.signed_root();
let domain = spec.get_domain(
transfer.slot.epoch(spec.slots_per_epoch),
transfer.slot.epoch(T::slots_per_epoch()),
Domain::Transfer,
&state.fork,
);

View File

@@ -237,7 +237,7 @@ pub fn process_final_updates<T: EthSpec>(
state.slot -= 1;
}
if next_epoch.as_u64() % (T::SlotsPerHistoricalRoot::to_u64() / spec.slots_per_epoch) == 0 {
if next_epoch.as_u64() % (T::SlotsPerHistoricalRoot::to_u64() / T::slots_per_epoch()) == 0 {
let historical_batch = state.historical_batch();
state
.historical_roots

View File

@@ -8,12 +8,12 @@ use types::*;
fn runs_without_error() {
Builder::from_env(Env::default().default_filter_or("error")).init();
let spec = FewValidatorsEthSpec::spec();
let spec = FewValidatorsEthSpec::default_spec();
let mut builder: TestingBeaconStateBuilder<FewValidatorsEthSpec> =
TestingBeaconStateBuilder::from_deterministic_keypairs(8, &spec);
let target_slot = (spec.genesis_epoch + 4).end_slot(spec.slots_per_epoch);
let target_slot = (spec.genesis_epoch + 4).end_slot(FewValidatorsEthSpec::slots_per_epoch());
builder.teleport_to_slot(target_slot, &spec);
let (mut state, _keypairs) = builder.build();

View File

@@ -233,7 +233,7 @@ impl ValidatorStatuses {
let attestation_slot = state.get_attestation_slot(&a.data)?;
let inclusion_slot = attestation_slot + a.inclusion_delay;
let relative_epoch =
RelativeEpoch::from_slot(state.slot, inclusion_slot, spec.slots_per_epoch)?;
RelativeEpoch::from_slot(state.slot, inclusion_slot, T::slots_per_epoch())?;
status.inclusion_info = Some(InclusionInfo {
slot: inclusion_slot,
distance: a.inclusion_delay,
@@ -297,7 +297,7 @@ impl ValidatorStatuses {
spec: &ChainSpec,
) -> Result<(), BeaconStateError> {
// Loop through each slot in the previous epoch.
for slot in state.previous_epoch().slot_iter(spec.slots_per_epoch) {
for slot in state.previous_epoch().slot_iter(T::slots_per_epoch()) {
let crosslink_committees_at_slot = state.get_crosslink_committees_at_slot(slot)?;
// Loop through each committee in the slot.
@@ -338,7 +338,7 @@ fn target_matches_epoch_start_block<T: EthSpec>(
epoch: Epoch,
spec: &ChainSpec,
) -> Result<bool, BeaconStateError> {
let slot = epoch.start_slot(spec.slots_per_epoch);
let slot = epoch.start_slot(T::slots_per_epoch());
let state_boundary_root = *state.get_block_root(slot)?;
Ok(a.data.target_root == state_boundary_root)

View File

@@ -16,7 +16,7 @@ pub fn per_slot_processing<T: EthSpec>(
) -> Result<(), Error> {
cache_state(state, spec)?;
if (state.slot > spec.genesis_slot) && ((state.slot + 1) % spec.slots_per_epoch == 0) {
if (state.slot > spec.genesis_slot) && ((state.slot + 1) % T::slots_per_epoch() == 0) {
per_epoch_processing(state, spec)?;
}