2018-08-21 16:21:45 +00:00
|
|
|
# beacon_chain
|
|
|
|
# Copyright (c) 2018 Status Research & Development GmbH
|
|
|
|
# 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.
|
|
|
|
|
2018-11-29 05:23:40 +00:00
|
|
|
# A imcomplete implementation of the state transition function, as described
|
|
|
|
# under "Per-block processing" in https://github.com/ethereum/eth2.0-specs/blob/master/specs/core/0_beacon-chain.md
|
2018-08-21 16:21:45 +00:00
|
|
|
#
|
2018-11-29 05:23:40 +00:00
|
|
|
# The code is here mainly to verify the data types and get an idea about
|
|
|
|
# missing pieces - needs testing throughout
|
2018-08-21 16:21:45 +00:00
|
|
|
|
2018-09-26 16:26:39 +00:00
|
|
|
import
|
2018-11-29 05:23:40 +00:00
|
|
|
options,
|
2018-11-29 22:11:05 +00:00
|
|
|
./extras,
|
2018-11-28 19:49:03 +00:00
|
|
|
./spec/[beaconstate, crypto, datatypes, digest, helpers],
|
2018-11-29 05:23:40 +00:00
|
|
|
./ssz,
|
2018-09-26 16:26:39 +00:00
|
|
|
milagro_crypto # nimble install https://github.com/status-im/nim-milagro-crypto@#master
|
2018-08-21 16:21:45 +00:00
|
|
|
|
2018-11-29 22:11:05 +00:00
|
|
|
# TODO there's an ugly mix of functional and procedural styles here that
|
|
|
|
# is due to how the spec is mixed as well - once we're past the prototype
|
|
|
|
# stage, this will need clearing up and unification.
|
2018-11-29 05:23:40 +00:00
|
|
|
|
2018-11-29 22:11:05 +00:00
|
|
|
func checkAttestations(state: BeaconState,
|
|
|
|
blck: BeaconBlock,
|
|
|
|
parent_slot: uint64): Option[seq[ProcessedAttestation]] =
|
|
|
|
# TODO perf improvement potential..
|
|
|
|
if blck.attestations.len > MAX_ATTESTATION_COUNT:
|
|
|
|
return
|
2018-08-21 16:21:45 +00:00
|
|
|
|
2018-11-29 22:11:05 +00:00
|
|
|
var res: seq[ProcessedAttestation]
|
2018-08-21 16:21:45 +00:00
|
|
|
for attestation in blck.attestations:
|
2018-11-29 05:23:40 +00:00
|
|
|
if attestation.data.slot <= blck.slot - MIN_ATTESTATION_INCLUSION_DELAY:
|
|
|
|
return
|
|
|
|
if attestation.data.slot >= max(parent_slot - CYCLE_LENGTH + 1, 0):
|
|
|
|
return
|
2018-11-28 04:23:27 +00:00
|
|
|
#doAssert attestation.data.justified_slot == justification_source if attestation.data.slot >= state.last_state_recalculation_slot else prev_cycle_justification_source
|
|
|
|
# doAssert attestation.data.justified_block_hash == get_block_hash(state, block, attestation.data.justified_slot).
|
|
|
|
# doAssert either attestation.data.last_crosslink_hash or attestation.data.shard_block_hash equals state.crosslinks[shard].shard_block_hash.
|
2018-08-21 16:21:45 +00:00
|
|
|
|
2018-11-29 05:23:40 +00:00
|
|
|
let attestation_participants = get_attestation_participants(
|
|
|
|
state, attestation.data, attestation.attester_bitfield)
|
|
|
|
|
|
|
|
var
|
|
|
|
agg_pubkey: ValidatorPubKey
|
|
|
|
empty = true
|
|
|
|
|
|
|
|
for attester_idx in attestation_participants:
|
|
|
|
let validator = state.validators[attester_idx]
|
|
|
|
if empty:
|
|
|
|
agg_pubkey = validator.pubkey
|
|
|
|
empty = false
|
|
|
|
else:
|
|
|
|
agg_pubkey.combine(validator.pubkey)
|
|
|
|
|
|
|
|
# Verify that aggregate_sig verifies using the group pubkey.
|
|
|
|
let msg = hashSSZ(attestation.data)
|
2018-10-02 16:09:11 +00:00
|
|
|
|
|
|
|
# For now only check compilation
|
|
|
|
# doAssert attestation.aggregate_sig.verifyMessage(msg, agg_pubkey)
|
2018-11-29 22:11:05 +00:00
|
|
|
debugEcho "Aggregate sig verify message: ",
|
|
|
|
attestation.aggregate_sig.verifyMessage(msg, agg_pubkey)
|
|
|
|
|
|
|
|
res.add ProcessedAttestation(
|
|
|
|
data: attestation.data,
|
|
|
|
attester_bitfield: attestation.attester_bitfield,
|
|
|
|
poc_bitfield: attestation.poc_bitfield,
|
|
|
|
slot_included: blck.slot
|
|
|
|
)
|
|
|
|
|
|
|
|
some(res)
|
|
|
|
|
|
|
|
func verifyProposerSignature(state: BeaconState, blck: BeaconBlock): bool =
|
|
|
|
var blck_without_sig = blck
|
|
|
|
blck_without_sig.proposer_signature = ValidatorSig()
|
|
|
|
|
|
|
|
let
|
|
|
|
proposal_hash = hashSSZ(ProposalSignedData(
|
|
|
|
slot: blck.slot,
|
|
|
|
shard: BEACON_CHAIN_SHARD,
|
|
|
|
block_hash: Eth2Digest(data: hashSSZ(blck_without_sig))
|
|
|
|
))
|
|
|
|
|
|
|
|
verifyMessage(
|
|
|
|
blck.proposer_signature, proposal_hash,
|
|
|
|
state.validators[get_beacon_proposer_index(state, blck.slot).int].pubkey)
|
|
|
|
|
|
|
|
func processRandaoReveal(state: var BeaconState,
|
|
|
|
blck: BeaconBlock,
|
|
|
|
parent_slot: uint64): bool =
|
|
|
|
# Update randao skips
|
|
|
|
for slot in parentslot + 1 ..< blck.slot:
|
|
|
|
let proposer_index = get_beacon_proposer_index(state, slot)
|
|
|
|
state.validators[proposer_index.int].randao_skips.inc()
|
|
|
|
|
|
|
|
var
|
|
|
|
proposer_index = get_beacon_proposer_index(state, blck.slot)
|
|
|
|
proposer = state.validators[proposer_index.int]
|
|
|
|
|
|
|
|
# Check that proposer commit and reveal match
|
|
|
|
if repeat_hash(blck.randao_reveal, proposer.randao_skips + 1) !=
|
|
|
|
proposer.randao_commitment:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Update state and proposer now that we're alright
|
|
|
|
for i, b in state.randao_mix.data:
|
|
|
|
state.randao_mix.data[i] = b xor blck.randao_reveal.data[i]
|
|
|
|
|
|
|
|
proposer.randao_commitment = blck.randao_reveal
|
|
|
|
proposer.randao_skips = 0
|
|
|
|
|
|
|
|
true
|
|
|
|
|
|
|
|
func process_block*(state: BeaconState, blck: BeaconBlock): Option[BeaconState] =
|
|
|
|
## When a new block is received, all participants must verify that the block
|
|
|
|
## makes sense and update their state accordingly. This function will return
|
|
|
|
## the new state, unless something breaks along the way
|
|
|
|
|
|
|
|
# TODO: simplistic way to be able to rollback state
|
|
|
|
var state = state
|
|
|
|
|
|
|
|
let
|
|
|
|
parent_hash = blck.ancestor_hashes[0]
|
|
|
|
slot = blck.slot
|
|
|
|
parent_slot = slot - 1 # TODO Not!! can skip slots...
|
|
|
|
# TODO actually get parent block, which means fixing up BeaconState refs above;
|
|
|
|
# there's no distinction between active/crystallized state anymore, etc.
|
|
|
|
|
|
|
|
state.recent_block_hashes =
|
|
|
|
append_to_recent_block_hashes(state.recent_block_hashes, parent_slot, slot,
|
|
|
|
parent_hash)
|
|
|
|
|
|
|
|
let processed_attestations = checkAttestations(state, blck, parent_slot)
|
|
|
|
if processed_attestations.isNone:
|
|
|
|
return
|
|
|
|
|
|
|
|
state.pending_attestations.add processed_attestations.get()
|
|
|
|
|
|
|
|
if not verifyProposerSignature(state, blck):
|
|
|
|
return
|
2018-08-21 16:21:45 +00:00
|
|
|
|
2018-11-29 22:11:05 +00:00
|
|
|
if not processRandaoReveal(state, blck, parent_slot):
|
|
|
|
return
|
2018-08-21 16:21:45 +00:00
|
|
|
|
2018-11-29 22:11:05 +00:00
|
|
|
some(state) # Looks ok - move on with the updated state
|