2019-12-03 16:45:12 +00:00
|
|
|
# beacon_chain
|
2020-04-15 07:59:47 +00:00
|
|
|
# Copyright (c) 2018-2020 Status Research & Development GmbH
|
2019-12-03 16:45:12 +00:00
|
|
|
# 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.
|
|
|
|
|
|
|
|
import
|
2020-04-15 07:59:47 +00:00
|
|
|
options, stew/endians2,
|
2019-12-03 16:45:12 +00:00
|
|
|
chronicles, eth/trie/[db],
|
2020-04-15 07:59:47 +00:00
|
|
|
../beacon_chain/[beacon_chain_db, block_pool, extras, merkle_minimal, ssz,
|
|
|
|
state_transition, validator_pool],
|
2019-12-03 16:45:12 +00:00
|
|
|
../beacon_chain/spec/[beaconstate, crypto, datatypes, digest,
|
2020-03-19 23:48:03 +00:00
|
|
|
helpers, validator, state_transition_block]
|
2019-12-03 16:45:12 +00:00
|
|
|
|
2020-03-04 21:27:11 +00:00
|
|
|
func makeFakeValidatorPrivKey(i: int): ValidatorPrivKey =
|
|
|
|
# 0 is not a valid BLS private key - 1000 helps interop with rust BLS library,
|
|
|
|
# lighthouse.
|
|
|
|
# TODO: switch to https://github.com/ethereum/eth2.0-pm/issues/60
|
|
|
|
var bytes = uint64(i + 1000).toBytesLE()
|
|
|
|
copyMem(addr result, addr bytes[0], sizeof(bytes))
|
2019-12-03 16:45:12 +00:00
|
|
|
|
|
|
|
func makeFakeHash(i: int): Eth2Digest =
|
|
|
|
var bytes = uint64(i).toBytesLE()
|
|
|
|
static: doAssert sizeof(bytes) <= sizeof(result.data)
|
|
|
|
copyMem(addr result.data[0], addr bytes[0], sizeof(bytes))
|
|
|
|
|
2020-05-01 15:51:24 +00:00
|
|
|
func hackPrivKey*(v: Validator): ValidatorPrivKey =
|
2019-12-03 16:45:12 +00:00
|
|
|
## Extract private key, per above hack
|
|
|
|
var bytes: array[8, byte]
|
|
|
|
static: doAssert sizeof(bytes) <= sizeof(v.withdrawal_credentials.data)
|
|
|
|
|
|
|
|
copyMem(
|
|
|
|
addr bytes, unsafeAddr v.withdrawal_credentials.data[0], sizeof(bytes))
|
|
|
|
let i = int(uint64.fromBytesLE(bytes))
|
|
|
|
makeFakeValidatorPrivKey(i)
|
|
|
|
|
|
|
|
func makeDeposit(i: int, flags: UpdateFlags): Deposit =
|
|
|
|
## Ugly hack for now: we stick the private key in withdrawal_credentials
|
|
|
|
## which means we can repro private key and randao reveal from this data,
|
|
|
|
## for testing :)
|
|
|
|
let
|
|
|
|
privkey = makeFakeValidatorPrivKey(i)
|
2020-04-11 08:51:07 +00:00
|
|
|
pubkey = privkey.toPubKey()
|
2019-12-03 16:45:12 +00:00
|
|
|
withdrawal_credentials = makeFakeHash(i)
|
2020-05-11 18:08:52 +00:00
|
|
|
domain = compute_domain(DOMAIN_DEPOSIT, Version(GENESIS_FORK_VERSION))
|
2019-12-03 16:45:12 +00:00
|
|
|
|
|
|
|
result = Deposit(
|
|
|
|
data: DepositData(
|
|
|
|
pubkey: pubkey,
|
|
|
|
withdrawal_credentials: withdrawal_credentials,
|
|
|
|
amount: MAX_EFFECTIVE_BALANCE,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2020-03-04 21:27:11 +00:00
|
|
|
if skipBLSValidation notin flags:
|
|
|
|
let signing_root = compute_signing_root(result.getDepositMessage, domain)
|
|
|
|
result.data.signature = bls_sign(privkey, signing_root.data)
|
2019-12-03 16:45:12 +00:00
|
|
|
|
2020-04-10 13:59:17 +00:00
|
|
|
proc makeInitialDeposits*(
|
2019-12-03 16:45:12 +00:00
|
|
|
n = SLOTS_PER_EPOCH, flags: UpdateFlags = {}): seq[Deposit] =
|
|
|
|
for i in 0..<n.int:
|
|
|
|
result.add makeDeposit(i, flags)
|
|
|
|
|
2020-04-10 13:59:17 +00:00
|
|
|
# This needs to be done as a batch, since the Merkle proof of the i'th
|
|
|
|
# deposit depends on the deposit (data) of the 0th through (i-1)st, of
|
|
|
|
# deposits. Computing partial hash_tree_root sequences of DepositData,
|
|
|
|
# and ideally (but not yet) efficiently only once calculating a Merkle
|
|
|
|
# tree utilizing as much of the shared substructure as feasible, means
|
|
|
|
# attaching proofs all together, as a separate step.
|
|
|
|
if skipMerkleValidation notin flags:
|
|
|
|
attachMerkleProofs(result)
|
|
|
|
|
2020-04-09 09:41:02 +00:00
|
|
|
func signBlock*(
|
|
|
|
fork: Fork, genesis_validators_root: Eth2Digest, blck: BeaconBlock,
|
|
|
|
privKey: ValidatorPrivKey, flags: UpdateFlags = {}): SignedBeaconBlock =
|
|
|
|
SignedBeaconBlock(
|
|
|
|
message: blck,
|
|
|
|
signature:
|
|
|
|
if skipBlsValidation notin flags:
|
|
|
|
get_block_signature(
|
|
|
|
fork, genesis_validators_root, blck.slot,
|
|
|
|
hash_tree_root(blck), privKey)
|
|
|
|
else:
|
|
|
|
ValidatorSig()
|
|
|
|
)
|
|
|
|
|
2020-03-19 23:48:03 +00:00
|
|
|
proc addTestBlock*(
|
2020-05-22 14:21:22 +00:00
|
|
|
state: var HashedBeaconState,
|
2020-03-19 23:48:03 +00:00
|
|
|
parent_root: Eth2Digest,
|
|
|
|
eth1_data = Eth1Data(),
|
|
|
|
attestations = newSeq[Attestation](),
|
|
|
|
deposits = newSeq[Deposit](),
|
|
|
|
graffiti = Eth2Digest(),
|
|
|
|
flags: set[UpdateFlag] = {}): SignedBeaconBlock =
|
2019-12-03 16:45:12 +00:00
|
|
|
# Create and add a block to state - state will advance by one slot!
|
2020-05-22 14:21:22 +00:00
|
|
|
advance_slot(state, err(Opt[Eth2Digest]), flags)
|
2020-03-19 23:48:03 +00:00
|
|
|
|
2019-12-03 16:45:12 +00:00
|
|
|
var cache = get_empty_per_epoch_cache()
|
|
|
|
let
|
2020-05-22 14:21:22 +00:00
|
|
|
proposer_index = get_beacon_proposer_index(state.data, cache)
|
|
|
|
privKey = hackPrivKey(state.data.validators[proposer_index.get])
|
2020-03-19 23:48:03 +00:00
|
|
|
randao_reveal =
|
|
|
|
if skipBlsValidation notin flags:
|
2020-03-30 11:31:44 +00:00
|
|
|
privKey.genRandaoReveal(
|
2020-05-22 14:21:22 +00:00
|
|
|
state.data.fork, state.data.genesis_validators_root, state.data.slot)
|
2020-03-19 23:48:03 +00:00
|
|
|
else:
|
|
|
|
ValidatorSig()
|
2019-12-03 16:45:12 +00:00
|
|
|
|
2020-03-19 23:48:03 +00:00
|
|
|
let
|
|
|
|
message = makeBeaconBlock(
|
|
|
|
state,
|
2020-05-22 14:21:22 +00:00
|
|
|
proposer_index.get(),
|
2020-03-19 23:48:03 +00:00
|
|
|
parent_root,
|
|
|
|
randao_reveal,
|
2020-04-17 22:09:34 +00:00
|
|
|
# Keep deposit counts internally consistent.
|
|
|
|
Eth1Data(
|
|
|
|
deposit_root: eth1_data.deposit_root,
|
2020-05-22 14:21:22 +00:00
|
|
|
deposit_count: state.data.eth1_deposit_index + deposits.len.uint64,
|
2020-04-17 22:09:34 +00:00
|
|
|
block_hash: eth1_data.block_hash),
|
2020-03-19 23:48:03 +00:00
|
|
|
graffiti,
|
|
|
|
attestations,
|
2020-05-22 14:21:22 +00:00
|
|
|
deposits,
|
|
|
|
noRollback)
|
2020-01-21 09:22:13 +00:00
|
|
|
|
2020-03-19 23:48:03 +00:00
|
|
|
doAssert message.isSome(), "Should have created a valid block!"
|
2019-12-03 16:45:12 +00:00
|
|
|
|
2020-04-09 09:41:02 +00:00
|
|
|
let
|
|
|
|
new_block = signBlock(
|
2020-05-22 14:21:22 +00:00
|
|
|
state.data.fork,
|
|
|
|
state.data.genesis_validators_root, message.get(), privKey, flags)
|
2019-12-03 16:45:12 +00:00
|
|
|
|
|
|
|
new_block
|
|
|
|
|
2020-03-19 23:48:03 +00:00
|
|
|
proc makeTestBlock*(
|
2020-05-22 14:21:22 +00:00
|
|
|
state: HashedBeaconState,
|
2020-03-19 23:48:03 +00:00
|
|
|
parent_root: Eth2Digest,
|
|
|
|
eth1_data = Eth1Data(),
|
|
|
|
attestations = newSeq[Attestation](),
|
|
|
|
deposits = newSeq[Deposit](),
|
|
|
|
graffiti = Eth2Digest(),
|
|
|
|
flags: set[UpdateFlag] = {}): SignedBeaconBlock =
|
2019-12-03 16:45:12 +00:00
|
|
|
# Create a block for `state.slot + 1` - like a block proposer would do!
|
|
|
|
# It's a bit awkward - in order to produce a block for N+1, we need to
|
|
|
|
# calculate what the state will look like after that block has been applied,
|
|
|
|
# because the block includes the state root.
|
2020-04-23 18:58:54 +00:00
|
|
|
var tmpState = newClone(state)
|
2020-03-19 23:48:03 +00:00
|
|
|
addTestBlock(
|
2020-04-23 18:58:54 +00:00
|
|
|
tmpState[], parent_root, eth1_data, attestations, deposits, graffiti, flags)
|
2019-12-03 16:45:12 +00:00
|
|
|
|
|
|
|
proc makeAttestation*(
|
|
|
|
state: BeaconState, beacon_block_root: Eth2Digest,
|
|
|
|
committee: seq[ValidatorIndex], slot: Slot, index: uint64,
|
|
|
|
validator_index: auto, cache: var StateCache,
|
|
|
|
flags: UpdateFlags = {}): Attestation =
|
|
|
|
# Avoids state_sim silliness; as it's responsible for all validators,
|
|
|
|
# transforming, from monotonic enumerable index -> committee index ->
|
|
|
|
# montonoic enumerable index, is wasteful and slow. Most test callers
|
|
|
|
# want ValidatorIndex, so that's supported too.
|
|
|
|
let
|
|
|
|
validator = state.validators[validator_index]
|
|
|
|
sac_index = committee.find(validator_index)
|
|
|
|
data = makeAttestationData(state, slot, index, beacon_block_root)
|
|
|
|
|
|
|
|
doAssert sac_index != -1, "find_beacon_committee should guarantee this"
|
|
|
|
|
|
|
|
var aggregation_bits = CommitteeValidatorsBits.init(committee.len)
|
2019-12-20 13:25:33 +00:00
|
|
|
aggregation_bits.setBit sac_index
|
2019-12-03 16:45:12 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
sig =
|
2020-03-04 21:27:11 +00:00
|
|
|
if skipBLSValidation notin flags:
|
2020-03-30 11:31:44 +00:00
|
|
|
get_attestation_signature(state.fork, state.genesis_validators_root,
|
|
|
|
data, hackPrivKey(validator))
|
2019-12-03 16:45:12 +00:00
|
|
|
else:
|
|
|
|
ValidatorSig()
|
|
|
|
|
|
|
|
Attestation(
|
|
|
|
data: data,
|
|
|
|
aggregation_bits: aggregation_bits,
|
|
|
|
signature: sig
|
|
|
|
)
|
|
|
|
|
|
|
|
proc find_beacon_committee(
|
|
|
|
state: BeaconState, validator_index: ValidatorIndex,
|
|
|
|
cache: var StateCache): auto =
|
|
|
|
let epoch = compute_epoch_at_slot(state.slot)
|
|
|
|
for epoch_committee_index in 0'u64 ..< get_committee_count_at_slot(
|
|
|
|
state, epoch.compute_start_slot_at_epoch) * SLOTS_PER_EPOCH:
|
|
|
|
let
|
|
|
|
slot = ((epoch_committee_index mod SLOTS_PER_EPOCH) +
|
|
|
|
epoch.compute_start_slot_at_epoch.uint64).Slot
|
|
|
|
index = epoch_committee_index div SLOTS_PER_EPOCH
|
2020-04-15 09:01:36 +00:00
|
|
|
committee = get_beacon_committee(state, slot, index.CommitteeIndex, cache)
|
2019-12-03 16:45:12 +00:00
|
|
|
if validator_index in committee:
|
|
|
|
return (committee, slot, index)
|
|
|
|
doAssert false
|
|
|
|
|
|
|
|
proc makeAttestation*(
|
|
|
|
state: BeaconState, beacon_block_root: Eth2Digest,
|
|
|
|
validator_index: ValidatorIndex, cache: var StateCache,
|
|
|
|
flags: UpdateFlags = {}): Attestation =
|
|
|
|
let (committee, slot, index) =
|
|
|
|
find_beacon_committee(state, validator_index, cache)
|
|
|
|
makeAttestation(state, beacon_block_root, committee, slot, index,
|
|
|
|
validator_index, cache, flags)
|
2020-01-21 09:22:13 +00:00
|
|
|
|
|
|
|
proc makeFullAttestations*(
|
|
|
|
state: BeaconState, beacon_block_root: Eth2Digest, slot: Slot,
|
|
|
|
cache: var StateCache,
|
|
|
|
flags: UpdateFlags = {}): seq[Attestation] =
|
|
|
|
# Create attestations in which the full committee participates for each shard
|
|
|
|
# that should be attested to during a particular slot
|
|
|
|
let
|
|
|
|
count = get_committee_count_at_slot(state, slot)
|
|
|
|
|
|
|
|
for index in 0..<count:
|
|
|
|
let
|
2020-04-15 09:01:36 +00:00
|
|
|
committee = get_beacon_committee(
|
|
|
|
state, slot, index.CommitteeIndex, cache)
|
2020-01-21 09:22:13 +00:00
|
|
|
data = makeAttestationData(state, slot, index, beacon_block_root)
|
2020-03-04 21:27:11 +00:00
|
|
|
|
|
|
|
doAssert committee.len() >= 1
|
|
|
|
# Initial attestation
|
|
|
|
var attestation = Attestation(
|
|
|
|
aggregation_bits: CommitteeValidatorsBits.init(committee.len),
|
|
|
|
data: data,
|
2020-03-19 23:48:03 +00:00
|
|
|
signature: get_attestation_signature(
|
2020-03-30 11:31:44 +00:00
|
|
|
state.fork, state.genesis_validators_root, data,
|
2020-03-19 23:48:03 +00:00
|
|
|
hackPrivKey(state.validators[committee[0]]))
|
2020-03-04 21:27:11 +00:00
|
|
|
)
|
|
|
|
# Aggregate the remainder
|
|
|
|
attestation.aggregation_bits.setBit 0
|
|
|
|
for j in 1 ..< committee.len():
|
2020-01-21 09:22:13 +00:00
|
|
|
attestation.aggregation_bits.setBit j
|
2020-03-04 21:27:11 +00:00
|
|
|
if skipBLSValidation notin flags:
|
2020-03-19 23:48:03 +00:00
|
|
|
attestation.signature.aggregate(get_attestation_signature(
|
2020-03-30 11:31:44 +00:00
|
|
|
state.fork, state.genesis_validators_root, data,
|
2020-03-19 23:48:03 +00:00
|
|
|
hackPrivKey(state.validators[committee[j]])
|
2020-03-04 21:27:11 +00:00
|
|
|
))
|
2020-01-21 09:22:13 +00:00
|
|
|
|
|
|
|
result.add attestation
|