mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-20 21:34:46 +00:00
Fix issues with old state information
This commit is contained in:
19
eth2/types/src/beacon_block/signing.rs
Normal file
19
eth2/types/src/beacon_block/signing.rs
Normal file
@@ -0,0 +1,19 @@
|
||||
use crate::{BeaconBlock, ChainSpec, Hash256, ProposalSignedData};
|
||||
use hashing::hash_tree_root;
|
||||
|
||||
impl BeaconBlock {
|
||||
pub fn proposal_root(&self, spec: &ChainSpec) -> Hash256 {
|
||||
let block_without_signature_root = {
|
||||
let mut block_without_signature = self.clone();
|
||||
block_without_signature.signature = spec.empty_signature.clone();
|
||||
block_without_signature.canonical_root()
|
||||
};
|
||||
|
||||
let proposal = ProposalSignedData {
|
||||
slot: self.slot,
|
||||
shard: spec.beacon_chain_shard_number,
|
||||
block_root: block_without_signature_root,
|
||||
};
|
||||
Hash256::from_slice(&hash_tree_root(&proposal)[..])
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,10 @@ use hashing::canonical_hash;
|
||||
use rand::RngCore;
|
||||
use ssz::{ssz_encode, Decodable, DecodeError, Encodable, SszStream};
|
||||
|
||||
mod slot_advance;
|
||||
|
||||
pub use self::slot_advance::Error as SlotProcessingError;
|
||||
|
||||
// Custody will not be added to the specs until Phase 1 (Sharding Phase) so dummy class used.
|
||||
type CustodyChallenge = usize;
|
||||
|
||||
@@ -203,9 +207,9 @@ impl<T: RngCore> TestRandom<T> for BeaconState {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::ssz::ssz_encode;
|
||||
use super::*;
|
||||
use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng};
|
||||
use ssz::ssz_encode;
|
||||
|
||||
#[test]
|
||||
pub fn test_ssz_round_trip() {
|
||||
45
eth2/types/src/beacon_state/slot_advance.rs
Normal file
45
eth2/types/src/beacon_state/slot_advance.rs
Normal file
@@ -0,0 +1,45 @@
|
||||
use crate::{BeaconState, ChainSpec, Hash256};
|
||||
|
||||
pub enum Error {
|
||||
UnableToDetermineProducer,
|
||||
}
|
||||
|
||||
impl BeaconState {
|
||||
pub fn per_slot_processing(
|
||||
&mut self,
|
||||
previous_block_root: Hash256,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
self.slot += 1;
|
||||
|
||||
let block_proposer = self
|
||||
.get_beacon_proposer_index(self.slot, spec)
|
||||
.ok_or_else(|| Error::UnableToDetermineProducer)?;
|
||||
|
||||
self.validator_registry[block_proposer].proposer_slots += 1;
|
||||
self.latest_randao_mixes[(self.slot % spec.latest_randao_mixes_length) as usize] =
|
||||
self.latest_randao_mixes[((self.slot - 1) % spec.latest_randao_mixes_length) as usize];
|
||||
|
||||
// Block roots.
|
||||
self.latest_block_roots[((self.slot - 1) % spec.latest_block_roots_length) as usize] =
|
||||
previous_block_root;
|
||||
|
||||
if self.slot % spec.latest_block_roots_length == 0 {
|
||||
let root = merkle_root(&self.latest_block_roots[..]);
|
||||
self.batched_block_roots.push(root);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn get_beacon_proposer_index(&self, slot: u64, spec: &ChainSpec) -> Option<usize> {
|
||||
// TODO: this is a stub; implement it properly.
|
||||
//
|
||||
// https://github.com/sigp/lighthouse/pull/148/files
|
||||
let validator_count = self.validator_registry.len();
|
||||
Some((slot as usize) % validator_count)
|
||||
}
|
||||
}
|
||||
|
||||
fn merkle_root(_input: &[Hash256]) -> Hash256 {
|
||||
Hash256::zero()
|
||||
}
|
||||
Reference in New Issue
Block a user