nimbus-eth2/tests/test_helpers.nim

79 lines
2.8 KiB
Nim
Raw Normal View History

# beacon_chain
# Copyright (c) 2018-2023 Status Research & Development GmbH
# Licensed and distributed under either of
# * 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).
# at your option. This file may not be copied, modified, or distributed except according to those terms.
{.used.}
import
# Status libraries
stew/bitops2,
eth/common/eth_types as commonEthTypes, eth/common/eth_types_rlp,
web3/ethtypes,
# Beacon chain internals
../beacon_chain/spec/[forks, helpers, state_transition],
../beacon_chain/spec/datatypes/[bellatrix, capella],
# Test utilities
./unittest2, mocking/mock_genesis
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
test "build_proof - BeaconState":
var
forked = newClone(initGenesisState())
cache = StateCache()
info = ForkedEpochInfo()
process_slots(
defaultRuntimeConfig, forked[], Slot(100), cache, info,
flags = {}).expect("no failure")
let
state = forked[].phase0Data.data
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)
state.build_proof(i, proof).get
check:
hash_tree_root(fieldVar) == hash_tree_root(state, i).get
is_valid_merkle_branch(hash_tree_root(fieldVar), proof,
depth, get_subtree_index(i), root)
when fieldVar is object and not (fieldVar is Eth2Digest):
let
numChildLeaves = fieldVar.numLeaves
childDepth = log2trunc(numChildLeaves)
process(fieldVar, i shl childDepth)
i += 1
process(state, state.numLeaves)
test "build_empty_execution_payload - Bellatrix":
var cfg = defaultRuntimeConfig
cfg.ALTAIR_FORK_EPOCH = GENESIS_EPOCH
cfg.BELLATRIX_FORK_EPOCH = GENESIS_EPOCH
let state = newClone(initGenesisState(cfg = cfg).bellatrixData)
proc testCase(recipient: Eth1Address) =
Support for driving multiple EL nodes from a single Nimbus BN (#4465) * Support for driving multiple EL nodes from a single Nimbus BN Full list of changes: * Eth1Monitor has been renamed to ELManager to match its current responsibilities better. * The ELManager is no longer optional in the code (it won't have a nil value under any circumstances). * The support for subscribing for headers was removed as it only worked with WebSockets and contributed significant complexity while bringing only a very minor advantage. * The `--web3-url` parameter has been deprecated in favor of a new `--el` parameter. The new parameter has a reasonable default value and supports specifying a different JWT for each connection. Each connection can also be configured with a different set of responsibilities (e.g. download deposits, validate blocks and/or produce blocks). On the command-line, these properties can be configured through URL properties stored in the #anchor part of the URL. In TOML files, they come with a very natural syntax (althrough the URL scheme is also supported). * The previously scattered EL-related state and logic is now moved to `eth1_monitor.nim` (this module will be renamed to `el_manager.nim` in a follow-up commit). State is assigned properly either to the `ELManager` or the to individual `ELConnection` objects where appropriate. The ELManager executes all Engine API requests against all attached EL nodes, in parallel. It compares their results and if there is a disagreement regarding the validity of a certain payload, this is detected and the beacon node is protected from publishing a block with a potential execution layer consensus bug in it. The BN provides metrics per EL node for the number of successful or failed requests for each type Engine API requests. If an EL node goes offline and connectivity is resoted later, we report the problem and the remedy in edge-triggered fashion. * More progress towards implementing Deneb block production in the VC and comparing the value of blocks produced by the EL and the builder API. * Adds a Makefile target for the zhejiang testnet
2023-03-05 01:40:21 +00:00
let payload = build_empty_execution_payload(state[].data, recipient).executionPayload
check payload.fee_recipient ==
bellatrix.ExecutionAddress(data: distinctBase(recipient))
testCase default(Eth1Address)
testCase Eth1Address.fromHex("0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b")