mirror of
https://github.com/sigp/lighthouse.git
synced 2026-04-27 01:33:33 +00:00
Fix issues with old state information
This commit is contained in:
@@ -134,14 +134,23 @@ impl<T: SlotClock, U: BeaconNode, V: DutiesReader, W: Signer> BlockProducer<T, U
|
||||
/// slashing.
|
||||
fn produce_block(&mut self, slot: u64) -> Result<PollOutcome, Error> {
|
||||
let randao_reveal = {
|
||||
/*
|
||||
* TODO:
|
||||
* https://github.com/ethereum/eth2.0-specs/pull/496
|
||||
*
|
||||
let producer_nonce = self.beacon_node.proposer_nonce(&self.pubkey)?;
|
||||
// TODO: add domain, etc to this message.
|
||||
let message = ssz_encode(&producer_nonce);
|
||||
*/
|
||||
// TODO: add domain, etc to this message.
|
||||
let message = ssz_encode(&slot);
|
||||
println!("validator randao: {:?}", &message);
|
||||
match self.signer.bls_sign(&message) {
|
||||
None => return Ok(PollOutcome::SignerRejection(slot)),
|
||||
Some(signature) => signature,
|
||||
}
|
||||
};
|
||||
println!("validator pubkey: {:?}", &self.pubkey);
|
||||
|
||||
if let Some(block) = self
|
||||
.beacon_node
|
||||
@@ -169,21 +178,7 @@ impl<T: SlotClock, U: BeaconNode, V: DutiesReader, W: Signer> BlockProducer<T, U
|
||||
fn sign_block(&mut self, mut block: BeaconBlock) -> Option<BeaconBlock> {
|
||||
self.store_produce(&block);
|
||||
|
||||
let proposal_root = {
|
||||
let block_without_signature_root = {
|
||||
let mut block_without_signature = block.clone();
|
||||
block_without_signature.signature = self.spec.empty_signature.clone();
|
||||
block_without_signature.canonical_root()
|
||||
};
|
||||
let proposal = ProposalSignedData {
|
||||
slot: block.slot,
|
||||
shard: self.spec.beacon_chain_shard_number,
|
||||
block_root: block_without_signature_root,
|
||||
};
|
||||
hash_tree_root(&proposal)
|
||||
};
|
||||
|
||||
match self.signer.bls_sign(&proposal_root[..]) {
|
||||
match self.signer.bls_sign(&block.proposal_root(&self.spec)[..]) {
|
||||
None => None,
|
||||
Some(signature) => {
|
||||
block.signature = signature;
|
||||
@@ -214,11 +209,6 @@ impl<T: SlotClock, U: BeaconNode, V: DutiesReader, W: Signer> BlockProducer<T, U
|
||||
}
|
||||
}
|
||||
|
||||
fn hash_tree_root<T>(_input: &T) -> Hash256 {
|
||||
// TODO: stubbed out.
|
||||
Hash256::zero()
|
||||
}
|
||||
|
||||
impl From<BeaconNodeError> for Error {
|
||||
fn from(e: BeaconNodeError) -> Error {
|
||||
Error::BeaconNodeError(e)
|
||||
|
||||
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()
|
||||
}
|
||||
@@ -6,3 +6,4 @@ edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
tiny-keccak = "1.4.2"
|
||||
ssz = { path = "../ssz" }
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
extern crate tiny_keccak;
|
||||
|
||||
use ssz::{ssz_encode, Encodable as SszEncodable};
|
||||
use tiny_keccak::Keccak;
|
||||
|
||||
pub fn canonical_hash(input: &[u8]) -> Vec<u8> {
|
||||
@@ -10,6 +9,10 @@ pub fn canonical_hash(input: &[u8]) -> Vec<u8> {
|
||||
result
|
||||
}
|
||||
|
||||
pub fn hash_tree_root<T: SszEncodable>(input: &T) -> Vec<u8> {
|
||||
canonical_hash(&ssz_encode(input))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
Reference in New Issue
Block a user