Add Eth2Config to runtime

This commit is contained in:
Paul Hauner
2019-06-08 13:17:03 -04:00
parent fd6766c268
commit d8fc5f31d8
13 changed files with 196 additions and 101 deletions

View File

@@ -1,7 +1,7 @@
use crate::*;
use int_to_bytes::int_to_bytes4;
use serde_derive::{Deserialize, Serialize};
use test_utils::u8_from_hex_str;
use test_utils::{u8_from_hex_str, u8_to_hex_str};
/// Each of the BLS signature domains.
///
@@ -48,17 +48,19 @@ pub struct ChainSpec {
* Initial Values
*/
pub genesis_slot: Slot,
// Skipped because serde TOML can't handle u64::max_value, the typical value for this field.
#[serde(skip_serializing)]
pub far_future_epoch: Epoch,
pub zero_hash: Hash256,
#[serde(deserialize_with = "u8_from_hex_str")]
#[serde(deserialize_with = "u8_from_hex_str", serialize_with = "u8_to_hex_str")]
pub bls_withdrawal_prefix_byte: u8,
/*
* Time parameters
*/
pub genesis_time: u64,
pub seconds_per_slot: u64,
pub min_attestation_inclusion_delay: u64,
//pub slots_per_epoch: u64,
pub min_seed_lookahead: Epoch,
pub activation_exit_delay: u64,
pub slots_per_eth1_voting_period: u64,
@@ -172,9 +174,9 @@ impl ChainSpec {
/*
* Time parameters
*/
genesis_time: u32::max_value() as u64,
seconds_per_slot: 6,
min_attestation_inclusion_delay: 4,
// slots_per_epoch: 64,
min_seed_lookahead: Epoch::new(1),
activation_exit_delay: 4,
slots_per_eth1_voting_period: 1_024,

View File

@@ -23,6 +23,7 @@ use std::iter::Iterator;
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Rem, Sub, SubAssign};
#[derive(Eq, Debug, Clone, Copy, Default, Serialize, Deserialize)]
#[serde(transparent)]
pub struct Slot(u64);
#[derive(Eq, Debug, Clone, Copy, Default, Serialize, Deserialize)]

View File

@@ -184,7 +184,7 @@ macro_rules! impl_display {
key: slog::Key,
serializer: &mut slog::Serializer,
) -> slog::Result {
self.0.serialize(record, key, serializer)
slog::Value::serialize(&self.0, record, key, serializer)
}
}
};

View File

@@ -6,7 +6,6 @@ use dirs;
use log::debug;
use rayon::prelude::*;
use std::path::{Path, PathBuf};
use std::time::SystemTime;
pub const KEYPAIRS_FILE: &str = "keypairs.raw_keypairs";
@@ -123,20 +122,8 @@ impl<T: EthSpec> TestingBeaconStateBuilder<T> {
})
.collect();
// TODO: Testing only. Burn with fire later.
// set genesis to the last 30 minute block.
// this is used for testing only. Allows multiple nodes to connect within a 30min window
// and agree on a genesis
let now = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap()
.as_secs();
let secs_after_last_period = now.checked_rem(30 * 60).unwrap_or(0);
// genesis is now the last 30 minute block.
let genesis_time = now - secs_after_last_period;
let mut state = BeaconState::genesis(
genesis_time,
spec.genesis_time,
Eth1Data {
deposit_root: Hash256::zero(),
deposit_count: 0,
@@ -172,8 +159,8 @@ impl<T: EthSpec> TestingBeaconStateBuilder<T> {
}
/// Sets the `BeaconState` to be in a slot, calling `teleport_to_epoch` to update the epoch.
pub fn teleport_to_slot(&mut self, slot: Slot, spec: &ChainSpec) {
self.teleport_to_epoch(slot.epoch(T::slots_per_epoch()), spec);
pub fn teleport_to_slot(&mut self, slot: Slot) {
self.teleport_to_epoch(slot.epoch(T::slots_per_epoch()));
self.state.slot = slot;
}
@@ -181,7 +168,7 @@ impl<T: EthSpec> TestingBeaconStateBuilder<T> {
///
/// Sets all justification/finalization parameters to be be as "perfect" as possible (i.e.,
/// highest justified and finalized slots, full justification bitfield, etc).
fn teleport_to_epoch(&mut self, epoch: Epoch, spec: &ChainSpec) {
fn teleport_to_epoch(&mut self, epoch: Epoch) {
let state = &mut self.state;
let slot = epoch.start_slot(T::slots_per_epoch());

View File

@@ -14,5 +14,5 @@ pub use rand::{
RngCore,
{prng::XorShiftRng, SeedableRng},
};
pub use serde_utils::{fork_from_hex_str, graffiti_from_hex_str, u8_from_hex_str};
pub use serde_utils::{fork_from_hex_str, graffiti_from_hex_str, u8_from_hex_str, u8_to_hex_str};
pub use test_random::TestRandom;

View File

@@ -1,5 +1,5 @@
use serde::de::Error;
use serde::{Deserialize, Deserializer};
use serde::{Deserialize, Deserializer, Serializer};
pub const FORK_BYTES_LEN: usize = 4;
pub const GRAFFITI_BYTES_LEN: usize = 32;
@@ -13,6 +13,16 @@ where
u8::from_str_radix(&s.as_str()[2..], 16).map_err(D::Error::custom)
}
pub fn u8_to_hex_str<S>(byte: &u8, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut hex: String = "0x".to_string();
hex.push_str(&hex::encode(&[*byte]));
serializer.serialize_str(&hex)
}
pub fn fork_from_hex_str<'de, D>(deserializer: D) -> Result<[u8; FORK_BYTES_LEN], D::Error>
where
D: Deserializer<'de>,