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-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 05:23:40 +00:00
func checkAttestations ( state : BeaconState , blck : BeaconBlock ) :
seq [ ProcessedAttestation ] =
discard
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
2018-11-28 04:23:27 +00:00
2018-11-29 05:23:40 +00:00
# XXX: 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 # XXX Not!! can skip slots...
2018-11-28 04:23:27 +00:00
# TODO actually get parent block, which means fixing up BeaconState refs above;
# there's no distinction between active/crystallized state anymore, etc.
2018-11-29 05:23:40 +00:00
state . recent_block_hashes =
append_to_recent_block_hashes ( state . recent_block_hashes , parent_slot , slot ,
parent_hash )
state . pending_attestations . add checkAttestations ( state , blck )
doAssert blck . attestations . len < = MAX_ATTESTATION_COUNT
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)
debugEcho " Aggregate sig verify message: " , attestation . aggregate_sig . verifyMessage ( msg , agg_pubkey )
2018-08-21 16:21:45 +00:00
2018-11-29 05:23:40 +00:00
return some ( state )
2018-08-21 16:21:45 +00:00
# Extend the list of AttestationRecord objects in the active_state, ordering the new additions in the same order as they came in the block.
# TODO
2018-11-29 05:23:40 +00:00
# Verify that the slot % len(get_indices_for_slot(state, slot-1)[0])'th attester in get_indices_for_slot(state, slot-1)[0]is part of at least one of the AttestationRecord objects; this attester can be considered to be the proposer of the block.
2018-08-21 16:21:45 +00:00
# TODO