Merge latest master in v0.2.0

This commit is contained in:
Age Manning
2020-04-08 16:46:37 +10:00
144 changed files with 2603 additions and 1308 deletions

View File

@@ -140,6 +140,8 @@ pub struct HeadInfo {
pub current_justified_checkpoint: types::Checkpoint,
pub finalized_checkpoint: types::Checkpoint,
pub fork: Fork,
pub genesis_time: u64,
pub genesis_validators_root: Hash256,
}
pub trait BeaconChainTypes: Send + Sync + 'static {
@@ -176,6 +178,8 @@ pub struct BeaconChain<T: BeaconChainTypes> {
pub(crate) canonical_head: TimeoutRwLock<BeaconSnapshot<T::EthSpec>>,
/// The root of the genesis block.
pub genesis_block_root: Hash256,
/// The root of the list of genesis validators, used during syncing.
pub genesis_validators_root: Hash256,
/// A state-machine that is updated with information from the network and chooses a canonical
/// head block.
pub fork_choice: ForkChoice<T>,
@@ -468,6 +472,8 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
current_justified_checkpoint: head.beacon_state.current_justified_checkpoint.clone(),
finalized_checkpoint: head.beacon_state.finalized_checkpoint.clone(),
fork: head.beacon_state.fork.clone(),
genesis_time: head.beacon_state.genesis_time,
genesis_validators_root: head.beacon_state.genesis_validators_root,
})
}
@@ -814,7 +820,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
root: target_root,
},
},
signature: AggregateSignature::new(),
signature: AggregateSignature::empty_signature(),
})
}
@@ -1084,11 +1090,16 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
.try_read_for(VALIDATOR_PUBKEY_CACHE_LOCK_TIMEOUT)
.ok_or_else(|| Error::ValidatorPubkeyCacheLockTimeout)?;
let fork = self
let (fork, genesis_validators_root) = self
.canonical_head
.try_read_for(HEAD_LOCK_TIMEOUT)
.ok_or_else(|| Error::CanonicalHeadLockTimeout)
.map(|head| head.beacon_state.fork.clone())?;
.map(|head| {
(
head.beacon_state.fork.clone(),
head.beacon_state.genesis_validators_root,
)
})?;
let signature_set = indexed_attestation_signature_set_from_pubkeys(
|validator_index| {
@@ -1099,6 +1110,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
&attestation.signature,
&indexed_attestation,
&fork,
genesis_validators_root,
&self.spec,
)
.map_err(Error::SignatureSetError)?;
@@ -1175,10 +1187,12 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
let index = attestation.data.index;
let slot = attestation.data.slot;
match self
.op_pool
.insert_attestation(attestation, &fork, &self.spec)
{
match self.op_pool.insert_attestation(
attestation,
&fork,
genesis_validators_root,
&self.spec,
) {
Ok(_) => {}
Err(e) => {
error!(
@@ -1674,6 +1688,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
let mut block = SignedBeaconBlock {
message: BeaconBlock {
slot: state.slot,
proposer_index: state.get_beacon_proposer_index(state.slot, &self.spec)? as u64,
parent_root,
state_root: Hash256::zero(),
body: BeaconBlockBody {
@@ -2014,7 +2029,8 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
// If we are unable to read the slot clock we assume that it is prior to genesis and
// therefore use the genesis slot.
let slot = self.slot().unwrap_or_else(|_| self.spec.genesis_slot);
self.spec.enr_fork_id(slot)
self.spec.enr_fork_id(slot, self.genesis_validators_root)
}
/// Calculates the `Duration` to the next fork, if one exists.

View File

@@ -187,6 +187,19 @@ where
.map_err(|e| format!("DB error whilst reading eth1 cache: {:?}", e))
}
/// Returns true if `self.store` contains a persisted beacon chain.
pub fn store_contains_beacon_chain(&self) -> Result<bool, String> {
let store = self
.store
.clone()
.ok_or_else(|| "load_from_store requires a store.".to_string())?;
Ok(store
.get::<PersistedBeaconChain>(&Hash256::from_slice(&BEACON_CHAIN_DB_KEY))
.map_err(|e| format!("DB error when reading persisted beacon chain: {:?}", e))?
.is_some())
}
/// Attempt to load an existing chain from the builder's `Store`.
///
/// May initialize several components; including the op_pool and finalized checkpoints.
@@ -418,6 +431,7 @@ where
// TODO: allow for persisting and loading the pool from disk.
naive_aggregation_pool: <_>::default(),
eth1_chain: self.eth1_chain,
genesis_validators_root: canonical_head.beacon_state.genesis_validators_root,
canonical_head: TimeoutRwLock::new(canonical_head.clone()),
genesis_block_root: self
.genesis_block_root

View File

@@ -274,11 +274,12 @@ mod tests {
a
}
fn sign(a: &mut Attestation<E>, i: usize) {
fn sign(a: &mut Attestation<E>, i: usize, genesis_validators_root: Hash256) {
a.sign(
&generate_deterministic_keypair(i).sk,
i,
&Fork::default(),
genesis_validators_root,
&E::default_spec(),
)
.expect("should sign attestation");
@@ -302,7 +303,7 @@ mod tests {
"should not accept attestation without any signatures"
);
sign(&mut a, 0);
sign(&mut a, 0, Hash256::random());
assert_eq!(
pool.insert(&a),
@@ -324,7 +325,7 @@ mod tests {
"retrieved attestation should equal the one inserted"
);
sign(&mut a, 1);
sign(&mut a, 1, Hash256::random());
assert_eq!(
pool.insert(&a),
@@ -338,8 +339,9 @@ mod tests {
let mut a_0 = get_attestation(Slot::new(0));
let mut a_1 = a_0.clone();
sign(&mut a_0, 0);
sign(&mut a_1, 1);
let genesis_validators_root = Hash256::random();
sign(&mut a_0, 0, genesis_validators_root);
sign(&mut a_1, 1, genesis_validators_root);
let pool = NaiveAggregationPool::default();
@@ -374,7 +376,7 @@ mod tests {
let mut a_different = a_0.clone();
let different_root = Hash256::from_low_u64_be(1337);
unset_bit(&mut a_different, 0);
sign(&mut a_different, 2);
sign(&mut a_different, 2, genesis_validators_root);
assert!(a_different.data.beacon_block_root != different_root);
a_different.data.beacon_block_root = different_root;
@@ -396,7 +398,7 @@ mod tests {
#[test]
fn auto_pruning() {
let mut base = get_attestation(Slot::new(0));
sign(&mut base, 0);
sign(&mut base, 0, Hash256::random());
let pool = NaiveAggregationPool::default();
@@ -450,7 +452,7 @@ mod tests {
#[test]
fn max_attestations() {
let mut base = get_attestation(Slot::new(0));
sign(&mut base, 0);
sign(&mut base, 0, Hash256::random());
let pool = NaiveAggregationPool::default();

View File

@@ -307,7 +307,9 @@ where
let randao_reveal = {
let epoch = slot.epoch(E::slots_per_epoch());
let domain = self.spec.get_domain(epoch, Domain::Randao, fork);
let domain =
self.spec
.get_domain(epoch, Domain::Randao, fork, state.genesis_validators_root);
let message = epoch.signing_root(domain);
Signature::new(message.as_bytes(), sk)
};
@@ -317,7 +319,7 @@ where
.produce_block_on_state(state, slot, randao_reveal)
.expect("should produce block");
let signed_block = block.sign(sk, &state.fork, &self.spec);
let signed_block = block.sign(sk, &state.fork, state.genesis_validators_root, &self.spec);
(signed_block, state)
}
@@ -402,6 +404,7 @@ where
attestation.data.target.epoch,
Domain::BeaconAttester,
fork,
state.genesis_validators_root,
);
let message = attestation.data.signing_root(domain);