Fix for getting the current epoch number

This commit is contained in:
Zahary Karadjov 2019-02-07 16:38:46 +01:00
parent bc44744acd
commit dfcd22519b
No known key found for this signature in database
GPG Key ID: C8936F8A3073D609
2 changed files with 5 additions and 4 deletions

View File

@ -93,7 +93,7 @@ proc sync*(node: BeaconNode): Future[bool] {.async.} =
node.beaconState = await obtainTrustedStateSnapshot(node.db) node.beaconState = await obtainTrustedStateSnapshot(node.db)
else: else:
node.beaconState = persistedState node.beaconState = persistedState
var targetSlot = toSlot timeSinceGenesis(node.beaconState) var targetSlot = node.beaconState.getSlotFromTime()
let t = now() let t = now()
if t < node.beaconState.genesisTime * 1000: if t < node.beaconState.genesisTime * 1000:
@ -388,7 +388,7 @@ proc processBlocks*(node: BeaconNode) =
node.attestationPool.add(a, node.beaconState) 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) node.scheduleEpochActions(epoch)
runForever() runForever()

View File

@ -16,8 +16,9 @@ proc timeSinceGenesis*(s: BeaconState): Timestamp =
Timestamp(int64(fastEpochTime() - s.genesis_time * 1000) - Timestamp(int64(fastEpochTime() - s.genesis_time * 1000) -
detectedClockDrift) detectedClockDrift)
template toSlot*(t: Timestamp): uint64 = proc getSlotFromTime*(s: BeaconState, t = now()): SlotNumber =
t div (SLOT_DURATION * 1000) GENESIS_SLOT + uint64((int64(t - s.genesis_time * 1000) - detectedClockDrift) div
int64(SLOT_DURATION * 1000))
template slotStart*(s: BeaconState, slot: uint64): Timestamp = template slotStart*(s: BeaconState, slot: uint64): Timestamp =
(s.genesis_time + (slot * SLOT_DURATION)) * 1000 (s.genesis_time + (slot * SLOT_DURATION)) * 1000