From 46c0e176827be48cec31394391f0b981179adff4 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Fri, 21 Jun 2019 11:37:02 +1000 Subject: [PATCH] Add arbitrary attestation for beacon chain harness --- beacon_node/beacon_chain/src/beacon_chain.rs | 55 ++------------------ beacon_node/beacon_chain/src/test_utils.rs | 19 ++++--- 2 files changed, 18 insertions(+), 56 deletions(-) diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index b8fff9611..235ec5f4c 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -425,65 +425,20 @@ impl BeaconChain { } /// Produce an `AttestationData` that is valid for the present `slot` and given `shard`. + /// + /// Attests to the canonical chain. pub fn produce_attestation_data(&self, shard: u64) -> Result { let state = self.state.read(); let head_block_root = self.head().beacon_block_root; let head_block_slot = self.head().beacon_block.slot; self.produce_attestation_data_for_block(shard, head_block_root, head_block_slot, &*state) - /* - let slots_per_epoch = T::EthSpec::slots_per_epoch(); - - self.metrics.attestation_production_requests.inc(); - let timer = self.metrics.attestation_production_times.start_timer(); - - let state = self.state.read(); - - let current_epoch_start_slot = self - .state - .read() - .slot - .epoch(slots_per_epoch) - .start_slot(slots_per_epoch); - - let target_root = if state.slot == current_epoch_start_slot { - // If we're on the first slot of the state's epoch. - if self.head().beacon_block.slot == state.slot { - // If the current head block is from the current slot, use its block root. - self.head().beacon_block_root - } else { - // If the current head block is not from this slot, use the slot from the previous - // epoch. - *self - .state - .read() - .get_block_root(current_epoch_start_slot - slots_per_epoch)? - } - } else { - // If we're not on the first slot of the epoch. - *self.state.read().get_block_root(current_epoch_start_slot)? - }; - - let previous_crosslink_root = - Hash256::from_slice(&state.get_current_crosslink(shard)?.tree_hash_root()); - - self.metrics.attestation_production_successes.inc(); - timer.observe_duration(); - - Ok(AttestationData { - beacon_block_root: self.head().beacon_block_root, - source_epoch: state.current_justified_epoch, - source_root: state.current_justified_root, - target_epoch: state.current_epoch(), - target_root, - shard, - previous_crosslink_root, - crosslink_data_root: Hash256::zero(), - }) - */ } /// Produce an `AttestationData` that attests to the chain denoted by `block_root` and `state`. + /// + /// Permits attesting to any arbitrary chain. Generally, the `produce_attestation_data` + /// function should be used as it attests to the canonical chain. pub fn produce_attestation_data_for_block( &self, shard: u64, diff --git a/beacon_node/beacon_chain/src/test_utils.rs b/beacon_node/beacon_chain/src/test_utils.rs index 8da68bbdc..b6873fa57 100644 --- a/beacon_node/beacon_chain/src/test_utils.rs +++ b/beacon_node/beacon_chain/src/test_utils.rs @@ -134,13 +134,11 @@ where .expect("should not error during block processing"); if let BlockProcessingOutcome::Processed { block_root } = outcome { - // + self.add_attestations_to_op_pool(&new_state, block_root, slot); } else { panic!("block should be successfully processed"); } - self.add_attestations_to_op_pool(); - state = new_state; slot += 1; } @@ -198,8 +196,12 @@ where (block, state) } - fn add_attestations_to_op_pool(&self) { - let state = &self.chain.current_state(); + fn add_attestations_to_op_pool( + &self, + state: &BeaconState, + head_block_root: Hash256, + head_block_slot: Slot, + ) { let spec = &self.spec; let fork = &state.fork; @@ -213,7 +215,12 @@ where for (i, validator_index) in cc.committee.iter().enumerate() { let data = self .chain - .produce_attestation_data(cc.shard) + .produce_attestation_data_for_block( + cc.shard, + head_block_root, + head_block_slot, + state, + ) .expect("should produce attestation data"); let mut aggregation_bitfield = Bitfield::new();