From dfcd22519be13d77407b588563a4da5e09958307 Mon Sep 17 00:00:00 2001 From: Zahary Karadjov Date: Thu, 7 Feb 2019 16:38:46 +0100 Subject: [PATCH] Fix for getting the current epoch number --- beacon_chain/beacon_node.nim | 4 ++-- beacon_chain/time.nim | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/beacon_chain/beacon_node.nim b/beacon_chain/beacon_node.nim index 66ead101a..a3d01e9d3 100644 --- a/beacon_chain/beacon_node.nim +++ b/beacon_chain/beacon_node.nim @@ -93,7 +93,7 @@ proc sync*(node: BeaconNode): Future[bool] {.async.} = node.beaconState = await obtainTrustedStateSnapshot(node.db) else: node.beaconState = persistedState - var targetSlot = toSlot timeSinceGenesis(node.beaconState) + var targetSlot = node.beaconState.getSlotFromTime() let t = now() if t < node.beaconState.genesisTime * 1000: @@ -388,7 +388,7 @@ proc processBlocks*(node: BeaconNode) = node.attestationPool.add(a, node.beaconState) - let epoch = node.beaconState.timeSinceGenesis().toSlot div EPOCH_LENGTH + let epoch = node.beaconState.getSlotFromTime div EPOCH_LENGTH node.scheduleEpochActions(epoch) runForever() diff --git a/beacon_chain/time.nim b/beacon_chain/time.nim index c062bef36..d2e3fd34a 100644 --- a/beacon_chain/time.nim +++ b/beacon_chain/time.nim @@ -16,8 +16,9 @@ proc timeSinceGenesis*(s: BeaconState): Timestamp = Timestamp(int64(fastEpochTime() - s.genesis_time * 1000) - detectedClockDrift) -template toSlot*(t: Timestamp): uint64 = - t div (SLOT_DURATION * 1000) +proc getSlotFromTime*(s: BeaconState, t = now()): SlotNumber = + GENESIS_SLOT + uint64((int64(t - s.genesis_time * 1000) - detectedClockDrift) div + int64(SLOT_DURATION * 1000)) template slotStart*(s: BeaconState, slot: uint64): Timestamp = (s.genesis_time + (slot * SLOT_DURATION)) * 1000