2018-11-29 22:11:05 +00:00
|
|
|
# beacon_chain
|
2019-02-27 13:58:07 +00:00
|
|
|
# Copyright (c) 2018-2019 Status Research & Development GmbH
|
2018-11-29 22:11:05 +00:00
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * MIT license (license terms in the root directory or at http://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at http://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
|
2019-02-05 16:13:29 +00:00
|
|
|
options, sequtils,
|
2019-03-28 12:00:21 +00:00
|
|
|
chronicles, eth/trie/[db],
|
2019-03-28 06:10:48 +00:00
|
|
|
../beacon_chain/[beacon_chain_db, block_pool, extras, ssz, state_transition,
|
|
|
|
validator_pool, beacon_node_types],
|
|
|
|
../beacon_chain/spec/[beaconstate, bitfield, crypto, datatypes, digest,
|
|
|
|
helpers, validator]
|
2018-11-29 22:11:05 +00:00
|
|
|
|
2019-05-27 12:48:13 +00:00
|
|
|
func preset*(): string =
|
|
|
|
" [Preset: " & const_preset & ']'
|
|
|
|
|
2019-03-07 13:59:28 +00:00
|
|
|
func makeFakeValidatorPrivKey*(i: int): ValidatorPrivKey =
|
2018-12-13 16:00:55 +00:00
|
|
|
var i = i + 1 # 0 does not work, as private key...
|
|
|
|
copyMem(result.x[0].addr, i.addr, min(sizeof(result.x), sizeof(i)))
|
|
|
|
|
2018-12-19 12:58:53 +00:00
|
|
|
func makeFakeHash*(i: int): Eth2Digest =
|
2018-12-13 16:00:55 +00:00
|
|
|
copyMem(result.data[0].addr, i.unsafeAddr, min(sizeof(result.data), sizeof(i)))
|
|
|
|
|
2019-01-17 18:27:11 +00:00
|
|
|
func hackPrivKey(v: Validator): ValidatorPrivKey =
|
2018-12-13 16:00:55 +00:00
|
|
|
## Extract private key, per above hack
|
|
|
|
var i: int
|
|
|
|
copyMem(
|
|
|
|
i.addr, v.withdrawal_credentials.data[0].unsafeAddr,
|
|
|
|
min(sizeof(v.withdrawal_credentials.data), sizeof(i)))
|
2019-03-07 13:59:28 +00:00
|
|
|
makeFakeValidatorPrivKey(i)
|
2018-12-13 16:00:55 +00:00
|
|
|
|
2018-12-27 23:40:22 +00:00
|
|
|
func makeDeposit(i: int, flags: UpdateFlags): Deposit =
|
2018-12-13 16:00:55 +00:00
|
|
|
## 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
|
2019-03-07 13:59:28 +00:00
|
|
|
privkey = makeFakeValidatorPrivKey(i)
|
2019-02-05 16:13:29 +00:00
|
|
|
pubkey = privkey.pubKey()
|
2018-12-13 16:00:55 +00:00
|
|
|
withdrawal_credentials = makeFakeHash(i)
|
2019-05-09 12:27:37 +00:00
|
|
|
domain = 3'u64
|
2018-12-27 23:40:22 +00:00
|
|
|
|
2019-05-09 12:27:37 +00:00
|
|
|
result = Deposit(
|
2019-03-18 15:42:42 +00:00
|
|
|
index: i.uint64,
|
2019-05-09 12:27:37 +00:00
|
|
|
data: DepositData(
|
|
|
|
pubkey: pubkey,
|
|
|
|
withdrawal_credentials: withdrawal_credentials,
|
2019-04-29 16:48:30 +00:00
|
|
|
amount: MAX_EFFECTIVE_BALANCE,
|
2018-12-13 16:00:55 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2019-05-09 12:27:37 +00:00
|
|
|
if skipValidation notin flags:
|
|
|
|
result.data.signature =
|
|
|
|
bls_sign(privkey, signing_root(result.data).data,
|
|
|
|
domain)
|
|
|
|
|
2018-12-27 23:40:22 +00:00
|
|
|
func makeInitialDeposits*(
|
2019-02-20 01:33:58 +00:00
|
|
|
n = SLOTS_PER_EPOCH, flags: UpdateFlags = {}): seq[Deposit] =
|
2018-12-11 17:55:45 +00:00
|
|
|
for i in 0..<n.int:
|
2019-05-09 12:27:37 +00:00
|
|
|
result.add makeDeposit(i, flags)
|
2018-12-13 16:00:55 +00:00
|
|
|
|
2019-01-29 04:15:00 +00:00
|
|
|
func getNextBeaconProposerIndex*(state: BeaconState): ValidatorIndex =
|
2018-12-27 20:14:37 +00:00
|
|
|
# TODO: This is a special version of get_beacon_proposer_index that takes into
|
|
|
|
# account the partial update done at the start of slot processing -
|
|
|
|
# see get_shard_committees_index
|
2018-12-14 16:12:39 +00:00
|
|
|
var next_state = state
|
|
|
|
next_state.slot += 1
|
2019-05-23 11:13:02 +00:00
|
|
|
get_beacon_proposer_index(next_state)
|
2018-12-19 04:36:10 +00:00
|
|
|
|
2018-12-21 22:37:46 +00:00
|
|
|
proc addBlock*(
|
|
|
|
state: var BeaconState, previous_block_root: Eth2Digest,
|
2018-12-27 23:40:22 +00:00
|
|
|
body: BeaconBlockBody, flags: UpdateFlags = {}): BeaconBlock =
|
2018-12-21 22:37:46 +00:00
|
|
|
# Create and add a block to state - state will advance by one slot!
|
|
|
|
# This is the equivalent of running
|
|
|
|
# updateState(state, prev_block, makeBlock(...), {skipValidation})
|
|
|
|
# but avoids some slow block copies
|
|
|
|
|
|
|
|
state.slot += 1
|
2019-05-23 11:13:02 +00:00
|
|
|
let proposer_index = get_beacon_proposer_index(state)
|
2018-12-21 22:37:46 +00:00
|
|
|
state.slot -= 1
|
2018-12-13 16:00:55 +00:00
|
|
|
|
2019-02-27 13:58:07 +00:00
|
|
|
# Ferret out remaining GENESIS_EPOCH == 0 assumptions in test code
|
|
|
|
doAssert allIt(
|
|
|
|
body.attestations,
|
2019-03-18 15:42:42 +00:00
|
|
|
it.data.previous_crosslink.epoch >= GENESIS_EPOCH)
|
2019-02-27 13:58:07 +00:00
|
|
|
|
2018-12-19 04:36:10 +00:00
|
|
|
let
|
|
|
|
# Index from the new state, but registry from the old state.. hmm...
|
2018-12-27 20:14:37 +00:00
|
|
|
proposer = state.validator_registry[proposer_index]
|
2019-02-07 20:13:10 +00:00
|
|
|
privKey = hackPrivKey(proposer)
|
2018-12-19 04:36:10 +00:00
|
|
|
|
2019-03-16 19:52:37 +00:00
|
|
|
# TODO ugly hack; API needs rethinking
|
|
|
|
var new_body = body
|
|
|
|
new_body.randao_reveal = privKey.genRandaoReveal(state, state.slot + 1)
|
|
|
|
new_body.eth1_data = Eth1Data()
|
|
|
|
|
2018-12-19 04:36:10 +00:00
|
|
|
var
|
|
|
|
# In order to reuse the state transition function, we first create a dummy
|
|
|
|
# block that has some fields set, and use that to generate the state as it
|
|
|
|
# would look with the new block applied.
|
|
|
|
new_block = BeaconBlock(
|
|
|
|
slot: state.slot + 1,
|
2019-03-16 19:52:37 +00:00
|
|
|
previous_block_root: previous_block_root,
|
2018-12-19 04:36:10 +00:00
|
|
|
state_root: Eth2Digest(), # we need the new state first
|
2019-03-27 01:32:35 +00:00
|
|
|
body: new_body,
|
2018-12-19 04:36:10 +00:00
|
|
|
signature: ValidatorSig(), # we need the rest of the block first!
|
2018-12-13 16:00:55 +00:00
|
|
|
)
|
|
|
|
|
2019-03-27 01:32:35 +00:00
|
|
|
let block_ok = updateState(state, new_block, {skipValidation})
|
2019-03-13 23:04:43 +00:00
|
|
|
doAssert block_ok
|
2018-12-19 04:36:10 +00:00
|
|
|
|
|
|
|
# Ok, we have the new state as it would look with the block applied - now we
|
|
|
|
# can set the state root in order to be able to create a valid signature
|
2019-03-25 16:46:31 +00:00
|
|
|
new_block.state_root = hash_tree_root(state)
|
2018-12-19 04:36:10 +00:00
|
|
|
|
2019-03-22 18:33:12 +00:00
|
|
|
let proposerPrivkey = hackPrivKey(proposer)
|
2019-03-13 23:04:43 +00:00
|
|
|
doAssert proposerPrivkey.pubKey() == proposer.pubkey,
|
2018-12-19 04:36:10 +00:00
|
|
|
"signature key should be derived from private key! - wrong privkey?"
|
2018-12-13 16:00:55 +00:00
|
|
|
|
2018-12-27 23:40:22 +00:00
|
|
|
if skipValidation notin flags:
|
2019-05-09 12:27:37 +00:00
|
|
|
let block_root = signing_root(new_block)
|
2018-12-27 23:40:22 +00:00
|
|
|
# We have a signature - put it in the block and we should be done!
|
|
|
|
new_block.signature =
|
2019-03-27 01:32:35 +00:00
|
|
|
bls_sign(proposerPrivkey, block_root.data,
|
2019-04-29 16:48:30 +00:00
|
|
|
get_domain(state, DOMAIN_BEACON_PROPOSER, slot_to_epoch(new_block.slot)))
|
2018-12-13 16:00:55 +00:00
|
|
|
|
2019-03-13 23:04:43 +00:00
|
|
|
doAssert bls_verify(
|
2018-12-27 23:40:22 +00:00
|
|
|
proposer.pubkey,
|
2019-03-27 01:32:35 +00:00
|
|
|
block_root.data, new_block.signature,
|
2019-04-29 16:48:30 +00:00
|
|
|
get_domain(state, DOMAIN_BEACON_PROPOSER, slot_to_epoch(new_block.slot))),
|
2018-12-27 23:40:22 +00:00
|
|
|
"we just signed this message - it should pass verification!"
|
2018-12-13 16:00:55 +00:00
|
|
|
|
|
|
|
new_block
|
2018-12-21 22:37:46 +00:00
|
|
|
|
|
|
|
proc makeBlock*(
|
|
|
|
state: BeaconState, previous_block_root: Eth2Digest,
|
|
|
|
body: BeaconBlockBody): BeaconBlock =
|
|
|
|
# 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.
|
|
|
|
var next_state = state
|
|
|
|
addBlock(next_state, previous_block_root, body)
|
2018-12-21 23:47:55 +00:00
|
|
|
|
|
|
|
proc find_shard_committee(
|
2019-02-06 19:37:25 +00:00
|
|
|
sacs: openArray[CrosslinkCommittee], validator_index: ValidatorIndex): CrosslinkCommittee =
|
2018-12-21 23:47:55 +00:00
|
|
|
for sac in sacs:
|
|
|
|
if validator_index in sac.committee: return sac
|
|
|
|
doAssert false
|
|
|
|
|
|
|
|
proc makeAttestation*(
|
|
|
|
state: BeaconState, beacon_block_root: Eth2Digest,
|
2019-01-29 04:15:00 +00:00
|
|
|
validator_index: ValidatorIndex, flags: UpdateFlags = {}): Attestation =
|
2018-12-21 23:47:55 +00:00
|
|
|
let
|
|
|
|
sac = find_shard_committee(
|
2019-02-06 19:37:25 +00:00
|
|
|
get_crosslink_committees_at_slot(state, state.slot), validator_index)
|
2018-12-21 23:47:55 +00:00
|
|
|
validator = state.validator_registry[validator_index]
|
|
|
|
sac_index = sac.committee.find(validator_index)
|
2019-03-09 04:23:14 +00:00
|
|
|
data = makeAttestationData(state, sac.shard, beacon_block_root)
|
2018-12-21 23:47:55 +00:00
|
|
|
|
2019-03-13 23:04:43 +00:00
|
|
|
doAssert sac_index != -1, "find_shard_committe should guarantee this"
|
2018-12-21 23:47:55 +00:00
|
|
|
|
|
|
|
var
|
2019-03-20 20:01:48 +00:00
|
|
|
aggregation_bitfield = BitField.init(sac.committee.len)
|
|
|
|
set_bitfield_bit(aggregation_bitfield, sac_index)
|
2018-12-21 23:47:55 +00:00
|
|
|
|
|
|
|
let
|
2019-03-25 16:46:31 +00:00
|
|
|
msg = hash_tree_root(
|
2019-03-09 04:23:14 +00:00
|
|
|
AttestationDataAndCustodyBit(data: data, custody_bit: false))
|
2018-12-27 23:40:22 +00:00
|
|
|
sig =
|
|
|
|
if skipValidation notin flags:
|
2019-02-28 21:24:43 +00:00
|
|
|
bls_sign(
|
|
|
|
hackPrivKey(validator), @(msg.data),
|
|
|
|
get_domain(
|
2019-04-29 16:48:30 +00:00
|
|
|
state,
|
|
|
|
DOMAIN_ATTESTATION,
|
|
|
|
slot_to_epoch(state.slot)))
|
2018-12-27 23:40:22 +00:00
|
|
|
else:
|
|
|
|
ValidatorSig()
|
2018-12-21 23:47:55 +00:00
|
|
|
|
|
|
|
Attestation(
|
|
|
|
data: data,
|
2019-02-07 16:55:48 +00:00
|
|
|
aggregation_bitfield: aggregation_bitfield,
|
2019-02-12 17:07:44 +00:00
|
|
|
aggregate_signature: sig,
|
2019-03-20 20:01:48 +00:00
|
|
|
custody_bitfield: BitField.init(sac.committee.len)
|
2018-12-21 23:47:55 +00:00
|
|
|
)
|
2019-02-28 21:21:29 +00:00
|
|
|
|
|
|
|
proc makeTestDB*(tailState: BeaconState, tailBlock: BeaconBlock): BeaconChainDB =
|
|
|
|
let
|
2019-05-09 12:27:37 +00:00
|
|
|
tailRoot = signing_root(tailBlock)
|
2019-02-28 21:21:29 +00:00
|
|
|
|
|
|
|
result = init(BeaconChainDB, newMemoryDB())
|
2019-03-28 06:10:48 +00:00
|
|
|
BlockPool.preInit(result, tailState, tailBlock)
|