2018-11-29 22:11:05 +00:00
|
|
|
# beacon_chain
|
2021-10-14 08:28:22 +00:00
|
|
|
# Copyright (c) 2018, 2021 Status Research & Development GmbH
|
2018-11-29 22:11:05 +00:00
|
|
|
# Licensed and distributed under either of
|
2019-11-25 15:30:02 +00:00
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
2018-11-29 22:11:05 +00:00
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2019-11-14 10:47:55 +00:00
|
|
|
{.used.}
|
|
|
|
|
2018-11-29 22:11:05 +00:00
|
|
|
import
|
2021-10-14 08:28:22 +00:00
|
|
|
# Standard library
|
|
|
|
sequtils,
|
2021-09-29 13:02:34 +00:00
|
|
|
# Status libraries
|
|
|
|
stew/bitops2,
|
|
|
|
# Beacon chain internals
|
2021-10-13 14:24:36 +00:00
|
|
|
../beacon_chain/spec/[forks, helpers, state_transition],
|
2021-09-29 13:02:34 +00:00
|
|
|
# Test utilities
|
|
|
|
./unittest2, mocking/mock_genesis
|
2018-11-29 22:11:05 +00:00
|
|
|
|
2021-04-28 16:41:02 +00:00
|
|
|
suite "Spec helpers":
|
|
|
|
test "integer_squareroot":
|
2018-12-05 13:07:42 +00:00
|
|
|
check:
|
0.6.2 updates (#275)
* update process_justification_and_finalization to 0.6.2; mark AttesterSlashing as 0.6.2
* replace get_effective_balance(...) with state.validator_registry[idx].effective_balance; rm get_effective_balance, process_ejections, should_update_validator_registry, update_validator_registry, and update_registry_and_shuffling_data; update get_total_balance to 0.6.2; implement process_registry_updates
* rm exit_validator; implement is_slashable_attestation_data; partly update processAttesterSlashings
* mark HistoricalBatch and Eth1Data as 0.6.2; implement get_shard_delta(...); replace 0.5 finish_epoch_update with 0.6 process_final_updates
* mark increase_balance, decrease_balance, get_delayed_activation_exit_epoch, bls_aggregate_pubkeys, bls_verify_multiple, Attestation, Transfer, slot_to_epoch, Crosslink, get_current_epoch, int_to_bytes*, various constants, processEth1Data, processTransfers, and verifyStateRoot as 0.6.2; rm is_double_vote and is_surround_vote
* mark get_bitfield_bit, verify_bitfield, ProposerSlashing, DepositData, VoluntaryExit, PendingAttestation, Fork, integer_squareroot, get_epoch_start_slot, is_active_validator, generate_seed, some constants to 0.6.2; rename MIN_PENALTY_QUOTIENT to MIN_SLASHING_PENALTY_QUOTIENT
* rm get_previous_total_balance, get_current_epoch_boundary_attestations, get_previous_epoch_boundary_attestations, and get_previous_epoch_matching_head_attestations
* update BeaconState to 0.6.2; simplify legacy get_crosslink_committees_at_slot infrastructure a bit by noting that registry_change is always false; reimplment 0.5 get_crosslink_committees_at_slot in terms of 0.6 get_crosslink_committee
* mark process_deposit(...), get_block_root_at_slot(...), get_block_root(...), Deposit, BeaconBlockHeader, BeaconBlockBody, hash(...), get_active_index_root(...), various constants, get_shard_delta(...), get_epoch_start_shard(...), get_crosslink_committee(...), processRandao(...), processVoluntaryExits(...), cacheState(...) as 0.6.2
* rm removed-since-0.5 split(...), is_power_of_2(...), get_shuffling(...); rm 0.5 versions of get_active_validator_indices and get_epoch_committee_count; add a few tests for integer_squareroot
* mark bytes_to_int(...) and advanceState(...) as 0.6.2
* rm 0.5 get_attesting_indices; update get_attesting_balance to 0.6.2
* another tiny commit to poke AppVeyor to maybe not timeout at connecting to GitHub partway through CI: mark get_churn_limit(...), initiate_validator_exit(...), and Validator as 0.6.2
* mark get_attestation_slot(...), AttestationDataAndCustodyBit, and BeaconBlock as 0.6.2
2019-06-03 10:31:04 +00:00
|
|
|
integer_squareroot(0'u64) == 0'u64
|
|
|
|
integer_squareroot(1'u64) == 1'u64
|
|
|
|
integer_squareroot(2'u64) == 1'u64
|
|
|
|
integer_squareroot(3'u64) == 1'u64
|
|
|
|
integer_squareroot(4'u64) == 2'u64
|
|
|
|
integer_squareroot(5'u64) == 2'u64
|
2021-09-29 13:02:34 +00:00
|
|
|
|
|
|
|
test "build_proof - BeaconState":
|
|
|
|
var
|
|
|
|
forked = newClone(initGenesisState())
|
|
|
|
cache = StateCache()
|
2021-10-13 14:24:36 +00:00
|
|
|
info = ForkedEpochInfo()
|
2022-01-17 11:19:58 +00:00
|
|
|
process_slots(
|
|
|
|
defaultRuntimeConfig, forked[], Slot(100), cache, info,
|
|
|
|
flags = {}).expect("no failure")
|
2021-09-29 13:02:34 +00:00
|
|
|
|
|
|
|
let
|
2021-10-18 16:37:27 +00:00
|
|
|
state = forked[].phase0Data.data
|
2021-09-29 13:02:34 +00:00
|
|
|
root = state.hash_tree_root()
|
|
|
|
|
|
|
|
func numLeaves(obj: object): GeneralizedIndex =
|
|
|
|
nextPow2(typeof(obj).totalSerializedFields.uint64).GeneralizedIndex
|
|
|
|
|
|
|
|
proc process(anchor: object, index: GeneralizedIndex) =
|
|
|
|
var i = index
|
|
|
|
anchor.enumInstanceSerializedFields(fieldNameVar, fieldVar):
|
|
|
|
let depth = log2trunc(i)
|
|
|
|
var proof = newSeq[Eth2Digest](depth)
|
|
|
|
build_proof(state, i, proof)
|
2021-10-13 14:24:36 +00:00
|
|
|
check: is_valid_merkle_branch(hash_tree_root(fieldVar), proof,
|
2021-09-29 13:02:34 +00:00
|
|
|
depth, get_subtree_index(i), root)
|
|
|
|
when fieldVar is object and not (fieldVar is Eth2Digest):
|
2021-10-13 14:24:36 +00:00
|
|
|
let
|
2021-09-29 13:02:34 +00:00
|
|
|
numChildLeaves = fieldVar.numLeaves
|
|
|
|
childDepth = log2trunc(numChildLeaves)
|
|
|
|
process(fieldVar, i shl childDepth)
|
|
|
|
i += 1
|
|
|
|
process(state, state.numLeaves)
|
2021-10-14 08:28:22 +00:00
|
|
|
|
|
|
|
test "get_branch_indices":
|
|
|
|
check:
|
|
|
|
toSeq(get_branch_indices(1.GeneralizedIndex)) == []
|
|
|
|
toSeq(get_branch_indices(0b101010.GeneralizedIndex)) ==
|
|
|
|
[
|
|
|
|
0b101011.GeneralizedIndex,
|
|
|
|
0b10100.GeneralizedIndex,
|
|
|
|
0b1011.GeneralizedIndex,
|
|
|
|
0b100.GeneralizedIndex,
|
|
|
|
0b11.GeneralizedIndex
|
|
|
|
]
|
|
|
|
|
|
|
|
test "get_path_indices":
|
|
|
|
check:
|
|
|
|
toSeq(get_path_indices(1.GeneralizedIndex)) == []
|
|
|
|
toSeq(get_path_indices(0b101010.GeneralizedIndex)) ==
|
|
|
|
[
|
|
|
|
0b101010.GeneralizedIndex,
|
|
|
|
0b10101.GeneralizedIndex,
|
|
|
|
0b1010.GeneralizedIndex,
|
|
|
|
0b101.GeneralizedIndex,
|
|
|
|
0b10.GeneralizedIndex
|
|
|
|
]
|
|
|
|
|
|
|
|
test "get_helper_indices":
|
|
|
|
check:
|
|
|
|
get_helper_indices(
|
|
|
|
[
|
|
|
|
8.GeneralizedIndex,
|
|
|
|
9.GeneralizedIndex,
|
|
|
|
14.GeneralizedIndex]) ==
|
|
|
|
[
|
|
|
|
15.GeneralizedIndex,
|
|
|
|
6.GeneralizedIndex,
|
|
|
|
5.GeneralizedIndex
|
|
|
|
]
|
|
|
|
|
|
|
|
test "verify_merkle_multiproof":
|
|
|
|
var nodes: array[16, Eth2Digest]
|
|
|
|
for i in countdown(15, 8):
|
|
|
|
nodes[i] = eth2digest([i.byte])
|
|
|
|
for i in countdown(7, 1):
|
|
|
|
nodes[i] = withEth2Hash:
|
|
|
|
h.update nodes[2 * i + 0].data
|
|
|
|
h.update nodes[2 * i + 1].data
|
|
|
|
|
|
|
|
proc verify(indices_int: openArray[int]) =
|
|
|
|
let
|
|
|
|
indices = indices_int.mapIt(it.GeneralizedIndex)
|
|
|
|
helper_indices = get_helper_indices(indices)
|
|
|
|
leaves = indices.mapIt(nodes[it])
|
|
|
|
proof = helper_indices.mapIt(nodes[it])
|
|
|
|
root = nodes[1]
|
|
|
|
checkpoint "Verifying " & $indices & "---" & $helper_indices
|
|
|
|
check:
|
|
|
|
verify_merkle_multiproof(leaves, proof, indices, root)
|
|
|
|
|
|
|
|
verify([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
|
|
|
|
|
|
|
|
for a in 1 .. 15:
|
|
|
|
verify([a])
|
|
|
|
for b in 1 .. 15:
|
|
|
|
verify([a, b])
|
|
|
|
for c in 1 .. 15:
|
|
|
|
verify([a, b, c])
|
|
|
|
for d in 8 .. 15:
|
|
|
|
verify([a, b, c, d])
|
|
|
|
for e in 1 .. 7:
|
|
|
|
verify([a, b, c, d, e])
|
2021-12-10 15:56:26 +00:00
|
|
|
|
|
|
|
test "is_valid_merkle_branch":
|
|
|
|
type TestCase = object
|
|
|
|
root: string
|
|
|
|
proof: seq[string]
|
|
|
|
leaf: string
|
|
|
|
index: uint64
|
|
|
|
valid: bool
|
|
|
|
|
|
|
|
let testCases = @[
|
|
|
|
TestCase(
|
|
|
|
root:
|
|
|
|
"2a23ef2b7a7221eaac2ffb3842a506a981c009ca6c2fcbf20adbc595e56f1a93",
|
|
|
|
proof: @[
|
|
|
|
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
|
|
|
"f5a5fd42d16a20302798ef6ed309979b43003d2320d9f0e8ea9831a92759fb4b"
|
|
|
|
],
|
|
|
|
leaf:
|
|
|
|
"0100000000000000000000000000000000000000000000000000000000000000",
|
|
|
|
index: 4,
|
|
|
|
valid: true
|
|
|
|
),
|
|
|
|
TestCase(
|
|
|
|
root:
|
|
|
|
"2a23ef2b7a7221eaac2ffb3842a506a981c009ca6c2fcbf20adbc595e56f1a93",
|
|
|
|
proof: @[
|
|
|
|
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
|
|
|
"f5a5fd42d16a20302798ef6ed309979b43003d2320d9f0e8ea9831a92759fb4b"
|
|
|
|
],
|
|
|
|
leaf:
|
|
|
|
"0100000000000000000000000000000000000000000000000000000000000000",
|
|
|
|
index: 6,
|
|
|
|
valid: false
|
|
|
|
),
|
|
|
|
TestCase(
|
|
|
|
root:
|
|
|
|
"2a23ef2b7a7221eaac2ffb3842a506a981c009ca6c2fcbf20adbc595e56f1a93",
|
|
|
|
proof: @[
|
|
|
|
"0100000000000000000000000000000000000000000000000000000000000000",
|
|
|
|
"f5a5fd42d16a20302798ef6ed309979b43003d2320d9f0e8ea9831a92759fb4b"
|
|
|
|
],
|
|
|
|
leaf:
|
|
|
|
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
|
|
|
|
index: 5,
|
|
|
|
valid: true
|
|
|
|
),
|
|
|
|
TestCase(
|
|
|
|
root:
|
|
|
|
"f1824b0084956084591ff4c91c11bcc94a40be82da280e5171932b967dd146e9",
|
|
|
|
proof: @[
|
|
|
|
"35210d64853aee79d03f30cf0f29c1398706cbbcacaf05ab9524f00070aec91e",
|
|
|
|
"f38a181470ef1eee90a29f0af0a9dba6b7e5d48af3c93c29b4f91fa11b777582"
|
|
|
|
],
|
|
|
|
leaf:
|
|
|
|
"0100000000000000000000000000000000000000000000000000000000000000",
|
|
|
|
index: 7,
|
|
|
|
valid: true
|
|
|
|
),
|
|
|
|
TestCase(
|
|
|
|
root:
|
|
|
|
"f1824b0084956084591ff4c91c11bcc94a40be82da280e5171932b967dd146e9",
|
|
|
|
proof: @[
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000000",
|
|
|
|
"0000000000000000000000000000000000000000000000000000000000000000",
|
|
|
|
"f5a5fd42d16a20302798ef6ed309979b43003d2320d9f0e8ea9831a92759fb4b",
|
|
|
|
"0100000000000000000000000000000000000000000000000000000000000000",
|
|
|
|
"f38a181470ef1eee90a29f0af0a9dba6b7e5d48af3c93c29b4f91fa11b777582"
|
|
|
|
],
|
|
|
|
leaf:
|
|
|
|
"6001000000000000000000000000000000000000000000000000000000000000",
|
|
|
|
index: 49,
|
|
|
|
valid: true
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
|
|
|
for testCase in testCases:
|
|
|
|
let
|
|
|
|
root = Eth2Digest.fromHex(testCase.root)
|
|
|
|
proof = mapIt(testCase.proof, Eth2Digest.fromHex(it))
|
|
|
|
leaf = Eth2Digest.fromHex(testCase.leaf)
|
|
|
|
index = testCase.index.GeneralizedIndex
|
|
|
|
valid = is_valid_merkle_branch(leaf, proof,
|
|
|
|
log2trunc(index),
|
|
|
|
get_subtree_index(index),
|
|
|
|
root)
|
|
|
|
if testCase.valid:
|
|
|
|
check valid
|
|
|
|
else:
|
|
|
|
check (not valid)
|