Represent slots in secs instead of millisecs (#2163)

## Issue Addressed

NA

## Proposed Changes

Copied from #2083, changes the config milliseconds_per_slot to seconds_per_slot to avoid errors when slot duration is not a multiple of a second. To avoid deserializing old serialized data (with milliseconds instead of seconds) the Serialize and Deserialize derive got removed from the Spec struct (isn't currently used anyway).

This PR replaces #2083 for the purpose of fixing a merge conflict without requiring the input of @blacktemplar.

## Additional Info

NA


Co-authored-by: blacktemplar <blacktemplar@a1.net>
This commit is contained in:
Paul Hauner
2021-01-19 09:39:51 +00:00
parent 46cb6e204c
commit d9f940613f
24 changed files with 58 additions and 132 deletions

View File

@@ -639,7 +639,7 @@ impl<T: BeaconChainTypes> BeaconChain<T> {
let start_slot = head_state.slot;
let task_start = Instant::now();
let max_task_runtime = Duration::from_millis(self.spec.milliseconds_per_slot);
let max_task_runtime = Duration::from_secs(self.spec.seconds_per_slot);
let head_state_slot = head_state.slot;
let mut state = head_state;

View File

@@ -90,7 +90,7 @@ fn get_sync_status<T: EthSpec>(
let period_start = slot_start_seconds::<T>(
genesis_time,
spec.milliseconds_per_slot,
spec.seconds_per_slot,
voting_period_start_slot,
);
@@ -98,7 +98,7 @@ fn get_sync_status<T: EthSpec>(
} else {
// The number of seconds in an eth1 voting period.
let voting_period_duration =
T::slots_per_eth1_voting_period() as u64 * (spec.milliseconds_per_slot / 1_000);
T::slots_per_eth1_voting_period() as u64 * spec.seconds_per_slot;
let now = SystemTime::now().duration_since(UNIX_EPOCH).ok()?.as_secs();
@@ -454,7 +454,7 @@ impl<T: EthSpec> Eth1ChainBackend<T> for CachingEth1Backend<T> {
let voting_period_start_slot = (state.slot / period) * period;
let voting_period_start_seconds = slot_start_seconds::<T>(
state.genesis_time,
spec.milliseconds_per_slot,
spec.seconds_per_slot,
voting_period_start_slot,
);
@@ -645,10 +645,10 @@ fn int_to_bytes32(int: u64) -> Vec<u8> {
/// Returns the unix-epoch seconds at the start of the given `slot`.
fn slot_start_seconds<T: EthSpec>(
genesis_unix_seconds: u64,
milliseconds_per_slot: u64,
seconds_per_slot: u64,
slot: Slot,
) -> u64 {
genesis_unix_seconds + slot.as_u64() * milliseconds_per_slot / 1_000
genesis_unix_seconds + slot.as_u64() * seconds_per_slot
}
/// Returns a boolean denoting if a given `Eth1Block` is a candidate for `Eth1Data` calculation
@@ -686,7 +686,7 @@ mod test {
let voting_period_start_slot = (state.slot / period) * period;
slot_start_seconds::<E>(
state.genesis_time,
spec.milliseconds_per_slot,
spec.seconds_per_slot,
voting_period_start_slot,
)
}
@@ -696,21 +696,21 @@ mod test {
let zero_sec = 0;
assert_eq!(slot_start_seconds::<E>(100, zero_sec, Slot::new(2)), 100);
let half_sec = 500;
assert_eq!(slot_start_seconds::<E>(100, half_sec, Slot::new(0)), 100);
assert_eq!(slot_start_seconds::<E>(100, half_sec, Slot::new(1)), 100);
assert_eq!(slot_start_seconds::<E>(100, half_sec, Slot::new(2)), 101);
assert_eq!(slot_start_seconds::<E>(100, half_sec, Slot::new(3)), 101);
let one_sec = 1_000;
let one_sec = 1;
assert_eq!(slot_start_seconds::<E>(100, one_sec, Slot::new(0)), 100);
assert_eq!(slot_start_seconds::<E>(100, one_sec, Slot::new(1)), 101);
assert_eq!(slot_start_seconds::<E>(100, one_sec, Slot::new(2)), 102);
let three_sec = 3_000;
let three_sec = 3;
assert_eq!(slot_start_seconds::<E>(100, three_sec, Slot::new(0)), 100);
assert_eq!(slot_start_seconds::<E>(100, three_sec, Slot::new(1)), 103);
assert_eq!(slot_start_seconds::<E>(100, three_sec, Slot::new(2)), 106);
let five_sec = 5;
assert_eq!(slot_start_seconds::<E>(100, five_sec, Slot::new(0)), 100);
assert_eq!(slot_start_seconds::<E>(100, five_sec, Slot::new(1)), 105);
assert_eq!(slot_start_seconds::<E>(100, five_sec, Slot::new(2)), 110);
assert_eq!(slot_start_seconds::<E>(100, five_sec, Slot::new(3)), 115);
}
fn get_eth1_block(timestamp: u64, number: u64) -> Eth1Block {

View File

@@ -322,13 +322,13 @@ where
.beacon_chain
.clone()
.ok_or("node timer requires a beacon chain")?;
let milliseconds_per_slot = self
let seconds_per_slot = self
.chain_spec
.as_ref()
.ok_or("node timer requires a chain spec")?
.milliseconds_per_slot;
.seconds_per_slot;
spawn_timer(context.executor, beacon_chain, milliseconds_per_slot)
spawn_timer(context.executor, beacon_chain, seconds_per_slot)
.map_err(|e| format!("Unable to start node timer: {}", e))?;
Ok(self)
@@ -381,17 +381,17 @@ where
.network_globals
.clone()
.ok_or("slot_notifier requires a libp2p network")?;
let milliseconds_per_slot = self
let seconds_per_slot = self
.chain_spec
.as_ref()
.ok_or("slot_notifier requires a chain spec")?
.milliseconds_per_slot;
.seconds_per_slot;
spawn_notifier(
context.executor,
beacon_chain,
network_globals,
milliseconds_per_slot,
seconds_per_slot,
)
.map_err(|e| format!("Unable to start slot notifier: {}", e))?;
@@ -685,7 +685,7 @@ where
let slot_clock = SystemTimeSlotClock::new(
spec.genesis_slot,
Duration::from_secs(genesis_time),
Duration::from_millis(spec.milliseconds_per_slot),
Duration::from_secs(spec.seconds_per_slot),
);
self.slot_clock = Some(slot_clock);

View File

@@ -25,9 +25,9 @@ pub fn spawn_notifier<T: BeaconChainTypes>(
executor: task_executor::TaskExecutor,
beacon_chain: Arc<BeaconChain<T>>,
network: Arc<NetworkGlobals<T::EthSpec>>,
milliseconds_per_slot: u64,
seconds_per_slot: u64,
) -> Result<(), String> {
let slot_duration = Duration::from_millis(milliseconds_per_slot);
let slot_duration = Duration::from_secs(seconds_per_slot);
let duration_to_next_slot = beacon_chain
.slot_clock
.duration_to_next_slot()

View File

@@ -360,7 +360,7 @@ impl Config {
pub fn set_block_cache_truncation<E: EthSpec>(&mut self, spec: &ChainSpec) {
// Compute the number of eth1 blocks in an eth1 voting period.
let seconds_per_voting_period =
E::SlotsPerEth1VotingPeriod::to_u64() * spec.milliseconds_per_slot / 1000;
E::SlotsPerEth1VotingPeriod::to_u64() * spec.seconds_per_slot;
let eth1_blocks_per_voting_period = seconds_per_voting_period / spec.seconds_per_eth1_block;
// Compute the number of extra blocks we store prior to the voting period start blocks.
@@ -1200,8 +1200,7 @@ mod tests {
let len = config.block_cache_truncation.unwrap();
let seconds_per_voting_period =
<MainnetEthSpec as EthSpec>::SlotsPerEth1VotingPeriod::to_u64()
* (spec.milliseconds_per_slot / 1000);
<MainnetEthSpec as EthSpec>::SlotsPerEth1VotingPeriod::to_u64() * spec.seconds_per_slot;
let eth1_blocks_per_voting_period = seconds_per_voting_period / spec.seconds_per_eth1_block;
let reduce_follow_distance_blocks =
config.follow_distance / ETH1_BLOCK_TIME_TOLERANCE_FACTOR;

View File

@@ -37,7 +37,7 @@ pub struct PeerScoreSettings<TSpec: EthSpec> {
impl<TSpec: EthSpec> PeerScoreSettings<TSpec> {
pub fn new(chain_spec: &ChainSpec, gs_config: &GossipsubConfig) -> PeerScoreSettings<TSpec> {
let slot = Duration::from_millis(chain_spec.milliseconds_per_slot);
let slot = Duration::from_millis(chain_spec.seconds_per_slot);
let beacon_attestation_subnet_weight = 1.0 / chain_spec.attestation_subnet_count as f64;
let max_positive_score = (MAX_IN_MESH_SCORE + MAX_FIRST_MESSAGE_DELIVERIES_SCORE)
* (BEACON_BLOCK_WEIGHT

View File

@@ -32,7 +32,7 @@ const LAST_SEEN_VALIDATOR_TIMEOUT: u32 = 150;
// 30 mins at a 12s slot time
/// The fraction of a slot that we subscribe to a subnet before the required slot.
///
/// Note: The time is calculated as `time = milliseconds_per_slot / ADVANCE_SUBSCRIPTION_TIME`.
/// Note: The time is calculated as `time = seconds_per_slot / ADVANCE_SUBSCRIPTION_TIME`.
const ADVANCE_SUBSCRIBE_TIME: u32 = 3;
/// The default number of slots before items in hash delay sets used by this class should expire.
/// 36s at 12s slot time

View File

@@ -269,7 +269,7 @@ fn spawn_service<T: BeaconChainTypes>(
_ = service.metrics_update.next() => {
// update various network metrics
metric_update_counter +=1;
if metric_update_counter* 1000 % T::EthSpec::default_spec().milliseconds_per_slot == 0 {
if metric_update_counter % T::EthSpec::default_spec().seconds_per_slot == 0 {
// if a slot has occurred, reset the metrics
let _ = metrics::ATTESTATIONS_PUBLISHED_PER_SUBNET_PER_SLOT
.as_ref()

View File

@@ -14,7 +14,7 @@ use tokio::time::{interval_at, Instant};
pub fn spawn_timer<T: BeaconChainTypes>(
executor: task_executor::TaskExecutor,
beacon_chain: Arc<BeaconChain<T>>,
milliseconds_per_slot: u64,
seconds_per_slot: u64,
) -> Result<(), &'static str> {
let log = executor.log();
let start_instant = Instant::now()
@@ -23,8 +23,8 @@ pub fn spawn_timer<T: BeaconChainTypes>(
.duration_to_next_slot()
.ok_or("slot_notifier unable to determine time to next slot")?;
// Warning: `interval_at` panics if `milliseconds_per_slot` = 0.
let mut interval = interval_at(start_instant, Duration::from_millis(milliseconds_per_slot));
// Warning: `interval_at` panics if `seconds_per_slot` = 0.
let mut interval = interval_at(start_instant, Duration::from_secs(seconds_per_slot));
let timer_future = async move {
while interval.next().await.is_some() {
beacon_chain.per_slot_task();