2019-04-02 14:50:24 +00:00
|
|
|
import
|
|
|
|
# Status libs
|
|
|
|
blscurve, nimcrypto, byteutils,
|
|
|
|
eth/common, serialization, json_serialization,
|
|
|
|
# Beacon chain internals
|
2019-04-08 08:29:44 +00:00
|
|
|
# submodule in nim-beacon-chain/tests/official/fixtures/
|
2019-04-29 16:10:01 +00:00
|
|
|
../../beacon_chain/spec/[datatypes, crypto, digest],
|
2019-05-23 07:41:17 +00:00
|
|
|
../../beacon_chain/ssz,
|
|
|
|
# Workarounds
|
|
|
|
endians # parseHex into uint64
|
2019-04-02 14:50:24 +00:00
|
|
|
|
|
|
|
export nimcrypto.toHex
|
|
|
|
|
|
|
|
type
|
|
|
|
# TODO: use ref object to avoid allocating
|
|
|
|
# so much on the stack - pending https://github.com/status-im/nim-json-serialization/issues/3
|
2019-05-10 08:14:01 +00:00
|
|
|
StateTests* = object
|
2019-04-02 14:50:24 +00:00
|
|
|
title*: string
|
|
|
|
summary*: string
|
|
|
|
test_suite*: string
|
|
|
|
fork*: string
|
2019-05-10 08:14:01 +00:00
|
|
|
test_cases*: seq[StateTestCase]
|
2019-04-02 14:50:24 +00:00
|
|
|
|
|
|
|
TestConstants* = object
|
2019-05-10 08:14:01 +00:00
|
|
|
# TODO - 0.5.1 constants
|
2019-04-02 14:50:24 +00:00
|
|
|
SHARD_COUNT*: int
|
|
|
|
TARGET_COMMITTEE_SIZE*: int
|
|
|
|
MAX_BALANCE_CHURN_QUOTIENT*: int
|
2019-04-29 16:48:30 +00:00
|
|
|
MAX_INDICES_PER_ATTESTATION*: int
|
|
|
|
MIN_PER_EPOCH_CHURN_LIMIT*: int
|
2019-04-02 14:50:24 +00:00
|
|
|
SHUFFLE_ROUND_COUNT*: int
|
|
|
|
DEPOSIT_CONTRACT_TREE_DEPTH*: int
|
|
|
|
MIN_DEPOSIT_AMOUNT*: uint64
|
2019-04-29 16:48:30 +00:00
|
|
|
MAX_EFFECTIVE_BALANCE*: uint64
|
2019-04-02 14:50:24 +00:00
|
|
|
FORK_CHOICE_BALANCE_INCREMENT*: uint64
|
|
|
|
EJECTION_BALANCE*: uint64
|
|
|
|
GENESIS_FORK_VERSION*: uint32
|
|
|
|
GENESIS_SLOT*: Slot
|
|
|
|
GENESIS_EPOCH*: Epoch
|
|
|
|
GENESIS_START_SHARD*: uint64
|
|
|
|
BLS_WITHDRAWAL_PREFIX_BYTE*: array[1, byte]
|
|
|
|
SECONDS_PER_SLOT*: uint64
|
|
|
|
MIN_ATTESTATION_INCLUSION_DELAY*: uint64
|
|
|
|
SLOTS_PER_EPOCH*: int
|
|
|
|
MIN_SEED_LOOKAHEAD*: int
|
|
|
|
ACTIVATION_EXIT_DELAY*: int
|
|
|
|
EPOCHS_PER_ETH1_VOTING_PERIOD*: uint64
|
|
|
|
SLOTS_PER_HISTORICAL_ROOT*: int
|
|
|
|
MIN_VALIDATOR_WITHDRAWABILITY_DELAY*: uint64
|
|
|
|
PERSISTENT_COMMITTEE_PERIOD*: uint64
|
|
|
|
LATEST_RANDAO_MIXES_LENGTH*: int
|
|
|
|
LATEST_ACTIVE_INDEX_ROOTS_LENGTH*: int
|
|
|
|
LATEST_SLASHED_EXIT_LENGTH*: int
|
|
|
|
BASE_REWARD_QUOTIENT*: uint64
|
More 0.6.1 updates (#274)
* implement get_churn_limit from 0.6.1; update initiate_validator_exit and get_previous_epoch to 0.6.1; remove obsoleted/non-spec reduce_balance, replaced by decrease_balance; mark BeaconBlock as 0.6.1
* rename get_block_root to get_block_root_at_slot and introduce epoch-oriented get_block_root; implement get_attestation_slot, get_matching_source_attestations, get_matching_target_attestations, get_matching_head_attestations, 0.6.1 get_attesting_indices, get_unslashed_attesting_indices, get_attestation_deltas, process_rewards_and_penalties; rm get_inactivity_penalty
* update Validator and processVoluntaryExits to 0.6.1; rm obsolete inclusion_slots/inclusion_distances
* rm removed activate_validator; mark DepositData, misc values, Gwei values, Randao processing, state caching as 0.6.1; update GENESIS_SLOT to 64, since it doesn't quite yet work at 0
* mark BeaconBlockHeader as 0.6.1; update BeaconBlockBody to 0.6.1; rename WHISTLEBLOWER_REWARD_QUOTIENT to WHISTLEBLOWING_REWARD_QUOTIENT; update slash_validator to 0.6.1
* implement 0.6.2 is_slashable_validator; update processProposerSlashings to 0.6.2; mark state caching as 0.6.2
* mark get_total_active_balance and process_slashings as 0.6.2; update get_base_reward to 0.6.2
* rm prepare_validator_for_withdrawal, process_exit_queue; mark mainnet misc constants as 0.6.2; update process block header to 0.6.2
* address mratsim's code review comment
2019-05-29 10:08:56 +00:00
|
|
|
WHISTLEBLOWING_REWARD_QUOTIENT*: uint64
|
2019-04-29 16:48:30 +00:00
|
|
|
PROPOSER_REWARD_QUOTIENT*: uint64
|
2019-04-02 14:50:24 +00:00
|
|
|
INACTIVITY_PENALTY_QUOTIENT*: uint64
|
|
|
|
MIN_PENALTY_QUOTIENT*: int
|
|
|
|
MAX_PROPOSER_SLASHINGS*: int
|
|
|
|
MAX_ATTESTER_SLASHINGS*: int
|
|
|
|
MAX_ATTESTATIONS*: int
|
|
|
|
MAX_DEPOSITS*: int
|
|
|
|
MAX_VOLUNTARY_EXITS*: int
|
|
|
|
MAX_TRANSFERS*: int
|
2019-04-29 16:48:30 +00:00
|
|
|
DOMAIN_BEACON_PROPOSER*: SignatureDomain
|
2019-04-02 14:50:24 +00:00
|
|
|
DOMAIN_RANDAO*: SignatureDomain
|
|
|
|
DOMAIN_ATTESTATION*: SignatureDomain
|
|
|
|
DOMAIN_DEPOSIT*: SignatureDomain
|
|
|
|
DOMAIN_VOLUNTARY_EXIT*: SignatureDomain
|
|
|
|
DOMAIN_TRANSFER*: SignatureDomain
|
|
|
|
|
2019-05-10 08:14:01 +00:00
|
|
|
StateTestCase* = object
|
2019-04-02 14:50:24 +00:00
|
|
|
name*: string
|
|
|
|
config*: TestConstants
|
|
|
|
verify_signatures*: bool
|
|
|
|
initial_state*: BeaconState
|
|
|
|
blocks*: seq[BeaconBlock]
|
2019-04-29 16:10:01 +00:00
|
|
|
expected_state*: BeaconState
|
2019-05-10 08:14:01 +00:00
|
|
|
|
2019-05-23 07:41:17 +00:00
|
|
|
Tests*[T] = object
|
2019-05-10 08:14:01 +00:00
|
|
|
title*: string
|
|
|
|
summary*: string
|
|
|
|
forks_timeline*: string
|
|
|
|
forks*: seq[string]
|
|
|
|
config*: string
|
|
|
|
runner*: string
|
|
|
|
handler*: string
|
2019-05-23 07:41:17 +00:00
|
|
|
test_cases*: seq[T]
|
2019-05-10 08:14:01 +00:00
|
|
|
|
2019-05-23 07:41:17 +00:00
|
|
|
Shuffling* = object
|
2019-05-10 08:14:01 +00:00
|
|
|
seed*: Eth2Digest
|
|
|
|
count*: uint64
|
|
|
|
shuffled*: seq[ValidatorIndex]
|
|
|
|
|
2019-05-23 07:41:17 +00:00
|
|
|
# # TODO - but already tested in nim-blscurve
|
|
|
|
# BLSUncompressedG2 = object
|
|
|
|
# input*: tuple[
|
|
|
|
# message: seq[byte],
|
|
|
|
# domain: array[1, byte]
|
|
|
|
# ]
|
|
|
|
# output*: ECP2_BLS381
|
|
|
|
|
|
|
|
# # TODO - but already tested in nim-blscurve
|
|
|
|
# BLSCompressedG2 = object
|
|
|
|
# input*: tuple[
|
|
|
|
# message: seq[byte],
|
|
|
|
# domain: array[1, byte]
|
|
|
|
# ]
|
|
|
|
# output*: ECP2_BLS381
|
|
|
|
|
|
|
|
Domain = distinct uint64
|
|
|
|
## Domains have custom hex serialization
|
|
|
|
|
|
|
|
BLSPrivToPub* = object
|
|
|
|
input*: ValidatorPrivKey
|
|
|
|
output*: ValidatorPubKey
|
|
|
|
|
|
|
|
BLSSignMsgInput = object
|
|
|
|
privkey*: ValidatorPrivKey
|
|
|
|
message*: seq[byte]
|
|
|
|
domain*: Domain
|
|
|
|
|
|
|
|
BLSSignMsg* = object
|
|
|
|
input*: BLSSignMsgInput
|
|
|
|
output*: Signature
|
|
|
|
|
|
|
|
BLSAggSig* = object
|
|
|
|
input*: seq[Signature]
|
|
|
|
output*: Signature
|
|
|
|
|
|
|
|
BLSAggPubKey* = object
|
|
|
|
input*: seq[ValidatorPubKey]
|
|
|
|
output*: ValidatorPubKey
|
2019-04-02 14:50:24 +00:00
|
|
|
|
|
|
|
# #######################
|
|
|
|
# Default init
|
|
|
|
proc default*(T: typedesc): T = discard
|
|
|
|
|
|
|
|
# #######################
|
|
|
|
# JSON deserialization
|
|
|
|
|
|
|
|
proc readValue*[N: static int](r: var JsonReader, a: var array[N, byte]) {.inline.} =
|
|
|
|
# Needed for;
|
|
|
|
# - BLS_WITHDRAWAL_PREFIX_BYTE
|
2019-04-29 16:10:01 +00:00
|
|
|
# - Fork datatypes
|
2019-04-02 14:50:24 +00:00
|
|
|
# TODO: are all bytes and bytearray serialized as hex?
|
|
|
|
# if so export that to nim-eth
|
|
|
|
hexToByteArray(r.readValue(string), a)
|
|
|
|
|
2019-05-10 08:14:01 +00:00
|
|
|
proc readValue*(r: var JsonReader, a: var ValidatorIndex) {.inline.} =
|
|
|
|
a = r.readValue(uint32)
|
|
|
|
|
2019-05-23 07:41:17 +00:00
|
|
|
proc readValue*(r: var JsonReader, a: var Domain) {.inline.} =
|
|
|
|
## Custom deserializer for Domain
|
|
|
|
## They are uint64 stored in hex values
|
|
|
|
# Furthermore Nim parseHex doesn't support uint
|
|
|
|
# until https://github.com/nim-lang/Nim/pull/11067
|
|
|
|
# (0.20)
|
|
|
|
let be_uint = hexToPaddedByteArray[8](r.readValue(string))
|
|
|
|
bigEndian64(a.addr, be_uint.unsafeAddr)
|
|
|
|
|
|
|
|
proc readValue*(r: var JsonReader, a: var seq[byte]) {.inline.} =
|
|
|
|
## Custom deserializer for seq[byte]
|
|
|
|
a = hexToSeqByte(r.readValue(string))
|
|
|
|
|
|
|
|
template parseTestsImpl(T: untyped) {.dirty.} =
|
|
|
|
# TODO: workaround typedesc/generics
|
|
|
|
# being broken with nim-serialization
|
|
|
|
# - https://github.com/status-im/nim-serialization/issues/4
|
|
|
|
# - https://github.com/status-im/nim-serialization/issues/5
|
2019-04-02 14:50:24 +00:00
|
|
|
try:
|
2019-05-23 07:41:17 +00:00
|
|
|
result = Json.loadFile(jsonPath, T)
|
2019-04-02 14:50:24 +00:00
|
|
|
except SerializationError as err:
|
|
|
|
writeStackTrace()
|
|
|
|
stderr.write "Json load issue for file \"", jsonPath, "\"\n"
|
|
|
|
stderr.write err.formatMsg(jsonPath), "\n"
|
2019-04-29 16:10:01 +00:00
|
|
|
quit 1
|
|
|
|
|
2019-05-23 07:41:17 +00:00
|
|
|
proc parseTestsShuffling*(jsonPath: string): Tests[Shuffling] =
|
|
|
|
parseTestsImpl(Tests[Shuffling])
|
|
|
|
|
|
|
|
proc parseTestsBLSPrivToPub*(jsonPath: string): Tests[BLSPrivToPub] =
|
|
|
|
parseTestsImpl(Tests[BLSPrivToPub])
|
|
|
|
|
|
|
|
proc parseTestsBLSSignMsg*(jsonPath: string): Tests[BLSSignMsg] =
|
|
|
|
parseTestsImpl(Tests[BLSSignMsg])
|
|
|
|
|
|
|
|
proc parseTestsBLSAggSig*(jsonPath: string): Tests[BLSAggSig] =
|
|
|
|
parseTestsImpl(Tests[BLSAggSig])
|
|
|
|
|
|
|
|
proc parseTestsBLSAggPubKey*(jsonPath: string): Tests[BLSAggPubKey] =
|
|
|
|
parseTestsImpl(Tests[BLSAggPubKey])
|
|
|
|
|
2019-04-29 16:10:01 +00:00
|
|
|
# #######################
|
|
|
|
# Mocking helpers
|
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/75f0af45bb0613bb406fc72d10266cee4cfb402a/tests/phase0/helpers.py#L107
|
|
|
|
|
|
|
|
proc build_empty_block_for_next_slot*(state: BeaconState): BeaconBlock =
|
|
|
|
## TODO: why can the official spec get away with a simple proc
|
|
|
|
|
|
|
|
# result.slot = state.slot + 1
|
|
|
|
# var previous_block_header = state.latest_block_header
|
|
|
|
# if previous_block_header.state_root == ZERO_HASH:
|
|
|
|
# previous_block_header.state_root = state.hash_tree_root()
|
2019-05-09 12:27:37 +00:00
|
|
|
# result.previous_block_root = signing_root(previous_block_header)
|
2019-04-29 16:10:01 +00:00
|
|
|
|
|
|
|
## TODO: `makeBlock` from testutil.nim
|
|
|
|
## doesn't work either due to use of fake private keys
|
|
|
|
|
|
|
|
# let prev_root = block:
|
|
|
|
# if state.latest_block_header.state_root == ZERO_HASH:
|
|
|
|
# state.hash_tree_root()
|
|
|
|
# else: state.latest_block_header.state_root
|
|
|
|
# result = makeBlock(
|
|
|
|
# state,
|
|
|
|
# prev_root,
|
|
|
|
# BeaconBlockBody()
|
|
|
|
# )
|
|
|
|
{.error: "Not implemented".}
|