From 9a11f5a7d869d9effae3819b4b7f49d2e5702570 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mamy=20Andr=C3=A9-Ratsimbazafy?= Date: Mon, 11 Feb 2019 19:43:02 +0100 Subject: [PATCH] debugging asserts --- beacon_chain/spec/beaconstate.nim | 15 +++++++++++++++ beacon_chain/spec/helpers.nim | 2 ++ 2 files changed, 17 insertions(+) diff --git a/beacon_chain/spec/beaconstate.nim b/beacon_chain/spec/beaconstate.nim index 1dc58b453..5f0da5198 100644 --- a/beacon_chain/spec/beaconstate.nim +++ b/beacon_chain/spec/beaconstate.nim @@ -375,6 +375,21 @@ proc checkAttestation*( it.shard == attestation.data.shard), it.committee)[0] + # Extra checks not in specs + # https://github.com/status-im/nim-beacon-chain/pull/105#issuecomment-462432544 + assert attestation.aggregation_bitfield.len == ( + crosslink_committee.len + 7) div 8, ( + "Error: got " & $attestation.aggregation_bitfield.len & + " but expected " & $((crosslink_committee.len + 7) div 8) + ) + + assert attestation.custody_bitfield.len == ( + crosslink_committee.len + 7) div 8, ( + "Error: got " & $attestation.custody_bitfield.len & + " but expected " & $((crosslink_committee.len + 7) div 8) + ) + # End extra checks + assert allIt(0 ..< len(crosslink_committee), if get_bitfield_bit(attestation.aggregation_bitfield, it) == 0b0: # Should always be true in phase 0, because of above assertion diff --git a/beacon_chain/spec/helpers.nim b/beacon_chain/spec/helpers.nim index c4c7436e8..1b2ad200c 100644 --- a/beacon_chain/spec/helpers.nim +++ b/beacon_chain/spec/helpers.nim @@ -17,6 +17,8 @@ func bitSet*(bitfield: var openArray[byte], index: int) = # https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#get_bitfield_bit func get_bitfield_bit*(bitfield: openarray[byte], i: int): byte = # Extract the bit in ``bitfield`` at position ``i``. + assert 0 <= i div 8, "i: " & $i & " i div 8: " & $(i div 8) + assert i div 8 < bitfield.len, "i: " & $i & " i div 8: " & $(i div 8) (bitfield[i div 8] shr (7 - (i mod 8))) mod 2 # https://github.com/ethereum/eth2.0-specs/blob/v0.1/specs/core/0_beacon-chain.md#verify_bitfield