Add interop eth1 data stub

This commit is contained in:
Paul Hauner 2019-09-02 11:39:28 +10:00
parent 5616e0a239
commit d4bf1390c9
No known key found for this signature in database
GPG Key ID: 5E2CFF9B75FA63DF
2 changed files with 32 additions and 5 deletions

View File

@ -4,6 +4,7 @@ use crate::fork_choice::{Error as ForkChoiceError, ForkChoice};
use crate::iter::{ReverseBlockRootIterator, ReverseStateRootIterator}; use crate::iter::{ReverseBlockRootIterator, ReverseStateRootIterator};
use crate::metrics; use crate::metrics;
use crate::persisted_beacon_chain::{PersistedBeaconChain, BEACON_CHAIN_DB_KEY}; use crate::persisted_beacon_chain::{PersistedBeaconChain, BEACON_CHAIN_DB_KEY};
use eth2_hashing::hash;
use lmd_ghost::LmdGhost; use lmd_ghost::LmdGhost;
use operation_pool::DepositInsertStatus; use operation_pool::DepositInsertStatus;
use operation_pool::{OperationPool, PersistedOperationPool}; use operation_pool::{OperationPool, PersistedOperationPool};
@ -1198,11 +1199,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
body: BeaconBlockBody { body: BeaconBlockBody {
randao_reveal, randao_reveal,
// TODO: replace with real data. // TODO: replace with real data.
eth1_data: Eth1Data { eth1_data: Self::eth1_data_stub(&state),
deposit_count: state.eth1_data.deposit_count,
deposit_root: Hash256::zero(),
block_hash: Hash256::zero(),
},
graffiti, graffiti,
proposer_slashings: proposer_slashings.into(), proposer_slashings: proposer_slashings.into(),
attester_slashings: attester_slashings.into(), attester_slashings: attester_slashings.into(),
@ -1231,6 +1228,22 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
Ok((block, state)) Ok((block, state))
} }
fn eth1_data_stub(state: &BeaconState<T::EthSpec>) -> Eth1Data {
let current_epoch = state.current_epoch();
let slots_per_voting_period = T::EthSpec::slots_per_eth1_voting_period() as u64;
let current_voting_period: u64 = current_epoch.as_u64() / slots_per_voting_period;
// TODO: confirm that `int_to_bytes32` is correct.
let deposit_root = hash(&int_to_bytes32(current_voting_period));
let block_hash = hash(&deposit_root);
Eth1Data {
deposit_root: Hash256::from_slice(&deposit_root),
deposit_count: state.eth1_deposit_index,
block_hash: Hash256::from_slice(&block_hash),
}
}
/// Execute the fork choice algorithm and enthrone the result as the canonical head. /// Execute the fork choice algorithm and enthrone the result as the canonical head.
pub fn fork_choice(&self) -> Result<(), Error> { pub fn fork_choice(&self) -> Result<(), Error> {
metrics::inc_counter(&metrics::FORK_CHOICE_REQUESTS); metrics::inc_counter(&metrics::FORK_CHOICE_REQUESTS);
@ -1426,6 +1439,13 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
} }
} }
/// Returns `int` as little-endian bytes with a length of 32.
fn int_to_bytes32(int: u64) -> Vec<u8> {
let mut vec = int.to_le_bytes().to_vec();
vec.resize(32, 0);
vec
}
impl From<DBError> for Error { impl From<DBError> for Error {
fn from(e: DBError) -> Error { fn from(e: DBError) -> Error {
Error::DBError(e) Error::DBError(e)

View File

@ -120,6 +120,13 @@ pub trait EthSpec: 'static + Default + Sync + Send + Clone + Debug + PartialEq {
fn epochs_per_historical_vector() -> usize { fn epochs_per_historical_vector() -> usize {
Self::EpochsPerHistoricalVector::to_usize() Self::EpochsPerHistoricalVector::to_usize()
} }
/// Returns the `SLOTS_PER_ETH1_VOTING_PERIOD` constant for this specification.
///
/// Spec v0.8.1
fn slots_per_eth1_voting_period() -> usize {
Self::EpochsPerHistoricalVector::to_usize()
}
} }
/// Macro to inherit some type values from another EthSpec. /// Macro to inherit some type values from another EthSpec.