From 255980c9f6593318bd8ca2b7dd8670eb104e5ed7 Mon Sep 17 00:00:00 2001 From: Dustin Brody Date: Wed, 2 Oct 2019 11:08:21 +0200 Subject: [PATCH] fix issue #367 and remove too-small range type usage for ValidatorIndex --- beacon_chain/beacon_node.nim | 2 +- beacon_chain/spec/beaconstate.nim | 2 +- beacon_chain/spec/datatypes.nim | 27 ++++++++++++++++++++++- beacon_chain/ssz.nim | 2 +- tests/official/test_fixture_shuffling.nim | 6 ++--- 5 files changed, 32 insertions(+), 7 deletions(-) diff --git a/beacon_chain/beacon_node.nim b/beacon_chain/beacon_node.nim index c0afb57c9..aec70ecc5 100644 --- a/beacon_chain/beacon_node.nim +++ b/beacon_chain/beacon_node.nim @@ -299,7 +299,7 @@ proc addLocalValidators(node: BeaconNode, state: BeaconState) = info "Local validators attached ", count = node.attachedValidators.count proc getAttachedValidator( - node: BeaconNode, state: BeaconState, idx: int): AttachedValidator = + node: BeaconNode, state: BeaconState, idx: ValidatorIndex): AttachedValidator = let validatorKey = state.validators[idx].pubkey node.attachedValidators.getValidator(validatorKey) diff --git a/beacon_chain/spec/beaconstate.nim b/beacon_chain/spec/beaconstate.nim index 335a61c22..635eb04a4 100644 --- a/beacon_chain/spec/beaconstate.nim +++ b/beacon_chain/spec/beaconstate.nim @@ -621,7 +621,7 @@ proc process_attestation*( data: attestation.data, aggregation_bits: attestation.aggregation_bits, inclusion_delay: state.slot - attestation_slot, - proposer_index: get_beacon_proposer_index(state, stateCache), + proposer_index: get_beacon_proposer_index(state, stateCache).uint64, ) if attestation.data.target.epoch == get_current_epoch(state): diff --git a/beacon_chain/spec/datatypes.nim b/beacon_chain/spec/datatypes.nim index 94bd7fd9f..0e5d5952c 100644 --- a/beacon_chain/spec/datatypes.nim +++ b/beacon_chain/spec/datatypes.nim @@ -73,7 +73,14 @@ template maxSize*(n: int) {.pragma.} type Bytes = seq[byte] - ValidatorIndex* = range[0'u32 .. 0xFFFFFF'u32] # TODO: wrap-around + # https://github.com/nim-lang/Nim/issues/574 and be consistent across + # 32-bit and 64-bit word platforms. + # TODO VALIDATOR_REGISTRY_LIMIT is 1 shl 40 in 0.8.3, and + # proc newSeq(typ: PNimType, len: int): pointer {.compilerRtl.} + # in Nim/lib/system/gc.nim quite tightly ties seq addressibility + # to the system wordsize. This lifts smaller, and now incorrect, + # range-limit. + ValidatorIndex* = distinct uint32 Shard* = uint64 Gwei* = uint64 @@ -517,6 +524,24 @@ template ethTimeUnit(typ: type) {.dirty.} = proc `%`*(i: uint64): JsonNode = % int(i) +# `ValidatorIndex` seq handling. +proc max*(a: ValidatorIndex, b: int) : auto = + max(a.int, b) + +proc `[]`*[T](a: var seq[T], b: ValidatorIndex): var T = + a[b.int] + +proc `[]`*[T](a: seq[T], b: ValidatorIndex): auto = + a[b.int] + +proc `[]=`*[T](a: var seq[T], b: ValidatorIndex, c: T) = + a[b.int] = c + +# `ValidatorIndex` Nim integration +proc `==`*(x, y: ValidatorIndex) : bool {.borrow.} +proc hash*(x: ValidatorIndex): Hash {.borrow.} +proc `$`*(x: ValidatorIndex): auto = $(x.int64) + ethTimeUnit Slot ethTimeUnit Epoch diff --git a/beacon_chain/ssz.nim b/beacon_chain/ssz.nim index 4f36be105..28d5bd6f9 100644 --- a/beacon_chain/ssz.nim +++ b/beacon_chain/ssz.nim @@ -39,7 +39,7 @@ type SszWriter* = object stream: OutputStreamVar - BasicType = char|bool|SomeUnsignedInt|StUint + BasicType = char|bool|SomeUnsignedInt|StUint|ValidatorIndex SszChunksMerkelizer = ref object of RootObj combinedChunks: array[maxChunkTreeDepth, Eth2Digest] diff --git a/tests/official/test_fixture_shuffling.nim b/tests/official/test_fixture_shuffling.nim index 2572e7305..4a77b084a 100644 --- a/tests/official/test_fixture_shuffling.nim +++ b/tests/official/test_fixture_shuffling.nim @@ -7,7 +7,7 @@ import # Standard library - os, unittest, + os, unittest, sequtils, # Beacon chain internals ../../beacon_chain/spec/[datatypes, validator, digest], # Test utilities @@ -18,7 +18,7 @@ type Shuffling* = object seed*: Eth2Digest count*: uint64 - mapping*: seq[ValidatorIndex] + mapping*: seq[uint64] const ShufflingDir = JsonTestsDir/const_preset/"phase0"/"shuffling"/"core"/"shuffle" @@ -27,4 +27,4 @@ suite "Official - Shuffling tests [Preset: " & preset(): for file in walkDirRec(ShufflingDir): let t = parseTest(file, Json, Shuffling) let implResult = get_shuffled_seq(t.seed, t.count) - check: implResult == t.mapping + check: implResult == mapIt(t.mapping, it.ValidatorIndex)