Skip to content

Commit

Permalink
a few simple SlotNumber cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
arnetheduck authored and zah committed Feb 18, 2019
1 parent 8e48bac commit c64cd2f
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
4 changes: 2 additions & 2 deletions beacon_chain/beacon_chain_db.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
16 changes: 8 additions & 8 deletions beacon_chain/beacon_node.nim
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type
attachedValidators: ValidatorPool
attestationPool: AttestationPool
mainchainMonitor: MainchainMonitor
lastScheduledEpoch: uint64
lastScheduledEpoch: EpochNumber
headBlock: BeaconBlock
headBlockRoot: Eth2Digest
blocksChildren: Table[Eth2Digest, seq[Eth2Digest]]
Expand Down Expand Up @@ -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.} =
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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) =
Expand All @@ -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
Expand Down
16 changes: 9 additions & 7 deletions beacon_chain/fork_choice.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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

Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion beacon_chain/spec/beaconstate.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c64cd2f

Please sign in to comment.