From c64cd2f275e336e4cf0321e83cea94746351b4b8 Mon Sep 17 00:00:00 2001 From: Jacek Sieka Date: Mon, 18 Feb 2019 12:54:05 -0600 Subject: [PATCH] a few simple SlotNumber cleanups --- beacon_chain/beacon_chain_db.nim | 4 ++-- beacon_chain/beacon_node.nim | 16 ++++++++-------- beacon_chain/fork_choice.nim | 16 +++++++++------- beacon_chain/spec/beaconstate.nim | 2 +- 4 files changed, 20 insertions(+), 18 deletions(-) diff --git a/beacon_chain/beacon_chain_db.nim b/beacon_chain/beacon_chain_db.nim index 5d17e0634..d1cb28b6a 100644 --- a/beacon_chain/beacon_chain_db.nim +++ b/beacon_chain/beacon_chain_db.nim @@ -23,11 +23,11 @@ proc hashToBlockKey(h: Eth2Digest): array[32 + 1, byte] = result[0] = byte ord(kHashToBlock) result[1 .. ^1] = h.data -proc slotToBlockHashKey(s: uint64): array[sizeof(uint64) + 1, byte] = +proc slotToBlockHashKey(s: SlotNumber): array[sizeof(SlotNumber) + 1, byte] = result[0] = byte ord(kSlotToBlockHash) copyMem(addr result[1], unsafeAddr(s), sizeof(s)) -proc slotToStateKey(s: uint64): array[sizeof(uint64) + 1, byte] = +proc slotToStateKey(s: SlotNumber): array[sizeof(SlotNumber) + 1, byte] = result[0] = byte ord(kSlotToState) copyMem(addr result[1], unsafeAddr(s), sizeof(s)) diff --git a/beacon_chain/beacon_node.nim b/beacon_chain/beacon_node.nim index dd78ebaac..501e2d541 100644 --- a/beacon_chain/beacon_node.nim +++ b/beacon_chain/beacon_node.nim @@ -16,7 +16,7 @@ type attachedValidators: ValidatorPool attestationPool: AttestationPool mainchainMonitor: MainchainMonitor - lastScheduledEpoch: uint64 + lastScheduledEpoch: EpochNumber headBlock: BeaconBlock headBlockRoot: Eth2Digest blocksChildren: Table[Eth2Digest, seq[Eth2Digest]] @@ -153,7 +153,7 @@ proc getAttachedValidator(node: BeaconNode, idx: int): AttachedValidator = proc makeAttestation(node: BeaconNode, validator: AttachedValidator, - slot: uint64, + slot: SlotNumber, shard: uint64, committeeLen: int, indexInCommittee: int) {.async.} = @@ -199,7 +199,7 @@ proc makeAttestation(node: BeaconNode, proc proposeBlock(node: BeaconNode, validator: AttachedValidator, - slot: uint64) {.async.} = + slot: SlotNumber) {.async.} = doAssert node != nil doAssert validator != nil doAssert validator.idx < node.beaconState.validator_registry.len @@ -245,7 +245,7 @@ proc proposeBlock(node: BeaconNode, idx = validator.idx proc scheduleBlockProposal(node: BeaconNode, - slot: uint64, + slot: SlotNumber, validator: AttachedValidator) = # TODO: # This function exists only to hide a bug with Nim's closures. @@ -265,11 +265,11 @@ proc scheduleBlockProposal(node: BeaconNode, # TODO timers are generally not accurate / guaranteed to fire at the right # time - need to guard here against early / late firings doAssert validator != nil - asyncCheck proposeBlock(node, validator, slot.uint64) + asyncCheck proposeBlock(node, validator, slot) proc scheduleAttestation(node: BeaconNode, validator: AttachedValidator, - slot: uint64, + slot: SlotNumber, shard: uint64, committeeLen: int, indexInCommittee: int) = @@ -281,10 +281,10 @@ proc scheduleAttestation(node: BeaconNode, addTimer(node.beaconState.slotMiddle(slot)) do (p: pointer) {.gcsafe.}: doAssert validator != nil - asyncCheck makeAttestation(node, validator, slot.uint64, + asyncCheck makeAttestation(node, validator, slot, shard, committeeLen, indexInCommittee) -proc scheduleEpochActions(node: BeaconNode, epoch: uint64) = +proc scheduleEpochActions(node: BeaconNode, epoch: EpochNumber) = ## This schedules the required block proposals and ## attestations from our attached validators. doAssert node != nil diff --git a/beacon_chain/fork_choice.nim b/beacon_chain/fork_choice.nim index 5efc736a6..13fc8d458 100644 --- a/beacon_chain/fork_choice.nim +++ b/beacon_chain/fork_choice.nim @@ -15,7 +15,7 @@ type # shard number. When we haven't received an attestation for a particular # shard yet, the Option value will be `none` attestations: Deque[array[SHARD_COUNT, Option[Attestation]]] - startingSlot: uint64 + startingSlot: SlotNumber # TODO: # The compilicated Deque above is not needed. @@ -37,7 +37,7 @@ type # substantial difficulties in network layer aggregation, then adding bits to # aid in supporting overlaps is one potential solution -proc init*(T: type AttestationPool, startingSlot: uint64): T = +proc init*(T: type AttestationPool, startingSlot: SlotNumber): T = result.attestations = initDeque[array[SHARD_COUNT, Option[Attestation]]]() result.startingSlot = startingSlot @@ -94,14 +94,14 @@ proc add*(pool: var AttestationPool, proc getAttestationsForBlock*(pool: AttestationPool, lastState: BeaconState, - newBlockSlot: uint64): seq[Attestation] = + newBlockSlot: SlotNumber): seq[Attestation] = if newBlockSlot < MIN_ATTESTATION_INCLUSION_DELAY or pool.attestations.len == 0: return doAssert newBlockSlot > lastState.slot var - firstSlot = 0.uint64 + firstSlot = 0.SlotNumber lastSlot = newBlockSlot - MIN_ATTESTATION_INCLUSION_DELAY if pool.startingSlot + MIN_ATTESTATION_INCLUSION_DELAY <= lastState.slot: @@ -115,7 +115,7 @@ proc getAttestationsForBlock*(pool: AttestationPool, if pool.attestations[slotDequeIdx][s.shard].isSome: result.add pool.attestations[slotDequeIdx][s.shard].get -proc discardHistoryToSlot*(pool: var AttestationPool, slot: uint64) = +proc discardHistoryToSlot*(pool: var AttestationPool, slot: SlotNumber) = ## The index is treated inclusively let slot = slot - MIN_ATTESTATION_INCLUSION_DELAY if slot < pool.startingSlot: @@ -170,7 +170,8 @@ func getAttestationCandidate*(attestation: Attestation): AttestationCandidate = proc get_parent(db: BeaconChainDB, blck: Eth2Digest): Eth2Digest = db.getBlock(blck).parent_root -proc get_ancestor(store: BeaconChainDB, blck: Eth2Digest, slot: uint64): Eth2Digest = +proc get_ancestor( + store: BeaconChainDB, blck: Eth2Digest, slot: SlotNumber): Eth2Digest = ## Find the ancestor with a specific slot number let blk = store.getBlock(blck) if blk.slot == slot: @@ -187,7 +188,8 @@ func getVoteCount(aggregation_bitfield: openarray[byte]): int = for validatorIdx in 0 ..< aggregation_bitfield.len * 8: result += int aggregation_bitfield.get_bitfield_bit(validatorIdx) -func getAttestationVoteCount(pool: AttestationPool, current_slot: uint64): CountTable[Eth2Digest] = +func getAttestationVoteCount( + pool: AttestationPool, current_slot: SlotNumber): CountTable[Eth2Digest] = ## Returns all blocks more recent that the current slot ## that were attested and their vote count # This replaces: diff --git a/beacon_chain/spec/beaconstate.nim b/beacon_chain/spec/beaconstate.nim index 2017ad510..6fe4ac8f1 100644 --- a/beacon_chain/spec/beaconstate.nim +++ b/beacon_chain/spec/beaconstate.nim @@ -230,7 +230,7 @@ func get_initial_beacon_state*( # https://github.com/ethereum/eth2.0-specs/blob/v0.2.0/specs/core/0_beacon-chain.md#get_block_root func get_block_root*(state: BeaconState, - slot: uint64): Eth2Digest = + slot: SlotNumber): Eth2Digest = # Return the block root at a recent ``slot``. doAssert state.slot <= slot + LATEST_BLOCK_ROOTS_LENGTH