finalizing state_transition (#792)

This commit is contained in:
Joao Gabriel Carvalho 2020-03-10 08:19:00 -03:00 committed by GitHub
parent f29bfef5a7
commit 91d75bdfdd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 5 deletions

View File

@ -373,7 +373,6 @@ proc add*(
# TODO if the block is from the future, we should not be resolving it (yet),
# but maybe we should use it as a hint that our clock is wrong?
updateStateData(pool, pool.tmpState, BlockSlot(blck: parent, slot: blck.slot - 1))
if not state_transition(pool.tmpState.data, signedBlock, {}):
# TODO find a better way to log all this block data
notice "Invalid block",
@ -382,7 +381,6 @@ proc add*(
cat = "filtering"
return
# Careful, tmpState.data has been updated but not blck - we need to create
# the BlockRef first!
pool.tmpState.blck = pool.addResolvedBlock(

View File

@ -151,7 +151,7 @@ func compute_signing_root*(ssz_object: auto, domain: Domain): Eth2Digest =
# object-domain tree.
let domain_wrapped_object = SigningRoot(
object_root: hash_tree_root(ssz_object),
domain: domain
domain: bytes_to_int(domain)
)
hash_tree_root(domain_wrapped_object)

View File

@ -31,9 +31,9 @@
# now.
import
collections/sets, chronicles, sets,
collections/sets, chronicles, sets, options,
./extras, ./ssz, metrics,
./spec/[datatypes, digest, helpers, validator],
./spec/[datatypes, crypto, digest, helpers, validator],
./spec/[state_transition_block, state_transition_epoch],
../nbench/bench_lab
@ -101,6 +101,19 @@ proc process_slots*(state: var BeaconState, slot: Slot) {.nbench.}=
if is_epoch_transition:
beacon_current_validators.set(get_epoch_validator_count(state))
#https://github.com/ethereum/eth2.0-specs/blob/v0.10.1/specs/phase0/beacon-chain.md#verify_block_signature
proc verify_block_signature*(state: var BeaconState, signedBlock: SignedBeaconBlock, stateCache: var StateCache): bool {.nbench.} =
let proposer = state.validators[get_beacon_proposer_index(state, stateCache).get]
let domain = get_domain(state, DOMAIN_BEACON_PROPOSER, compute_epoch_at_slot(signedBlock.message.slot))
# TODO This will need to be changed for:
# ```
# let signing_root = compute_signing_root(signedBlock.message,domain)
# return bls_verify(proposer.pubKey, signing_root.data, signedBlock.signature, domain)
# ```
# when https://github.com/status-im/nim-beacon-chain/pull/780 is merged
return bls_verify(proposer.pubKey, hash_tree_root(signedBlock.message).data, signedBlock.signature, domain)
# https://github.com/ethereum/eth2.0-specs/blob/v0.9.4/specs/core/0_beacon-chain.md#beacon-chain-state-transition-function
proc verifyStateRoot(state: BeaconState, blck: BeaconBlock): bool =
# This is inlined in state_transition(...) in spec.
@ -157,6 +170,10 @@ proc state_transition*(
# https://github.com/ethereum/eth2.0-specs/issues/293
var per_epoch_cache = get_empty_per_epoch_cache()
if skipBLSValidation notin flags and not verify_block_signature(state, signedBlock, per_epoch_cache):
state = old_state
return false
if processBlock(state, signedBlock.message, flags, per_epoch_cache):
# This is a bit awkward - at the end of processing we verify that the
# state we arrive at is what the block producer thought it would be -
@ -224,6 +241,10 @@ proc state_transition*(
process_slots(state, signedBlock.message.slot)
var per_epoch_cache = get_empty_per_epoch_cache()
if skipBLSValidation notin flags and not verify_block_signature(state.data, signedBlock, per_epoch_cache):
state = old_state
return false
if processBlock(state.data, signedBlock.message, flags, per_epoch_cache):
if skipStateRootValidation in flags or verifyStateRoot(state.data, signedBlock.message):
# State root is what it should be - we're done!