more interop fixes

* fix eth1 interop block hash
* update initialize_beacon_state_from_eth1 to 0.8.3
This commit is contained in:
Jacek Sieka 2019-09-02 12:31:14 +02:00 committed by zah
parent 7b73b40bab
commit 9cfbdb5e16
14 changed files with 65 additions and 52 deletions

View File

@ -794,10 +794,14 @@ when isMainModule:
stderr.write "Please regenerate the deposit files by running makeDeposits again\n"
quit 1
let initialState = initialize_beacon_state_from_eth1(
deposits,
uint64(times.toUnix(times.getTime()) + config.genesisOffset),
get_eth1data_stub(deposits.len().uint64, 0.Epoch), {skipValidation})
var
startTime = uint64(times.toUnix(times.getTime()) + config.genesisOffset)
initialState = initialize_beacon_state_from_eth1(
eth1BlockHash, startTime, deposits, {skipValidation})
# https://github.com/ethereum/eth2.0-pm/tree/6e41fcf383ebeb5125938850d8e9b4e9888389b4/interop/mocked_start#create-genesis-state
initialState.genesis_time = startTime
doAssert initialState.validators.len > 0

View File

@ -266,4 +266,3 @@ else:
shuffle peers
if peers.len > maxPeers: peers.setLen(maxPeers)
for p in peers: yield p

View File

@ -35,6 +35,11 @@ else:
ValidatorPrivKey.init(key)
const eth1BlockHash* = block:
var x: Eth2Digest
for v in x.data.mitems: v = 0x42
x
func makeWithdrawalCredentials*(k: ValidatorPubKey): Eth2Digest =
# https://github.com/ethereum/eth2.0-specs/blob/v0.8.3/specs/core/0_deposit-contract.md#withdrawal-credentials
var bytes = eth2hash(k.getBytes())

View File

@ -200,11 +200,11 @@ func get_compact_committees_root*(state: BeaconState, epoch: Epoch): Eth2Digest
hash_tree_root(committees)
# https://github.com/ethereum/eth2.0-specs/blob/v0.7.1/specs/core/0_beacon-chain.md#genesis
# https://github.com/ethereum/eth2.0-specs/blob/v0.8.3/specs/core/0_beacon-chain.md#genesis
func initialize_beacon_state_from_eth1*(
genesis_validator_deposits: openArray[Deposit],
genesis_time: uint64,
genesis_eth1_data: Eth1Data,
eth1_block_hash: Eth2Digest,
eth1_timestamp: uint64,
deposits: openArray[Deposit],
flags: UpdateFlags = {}): BeaconState =
## Get the genesis ``BeaconState``.
##
@ -220,42 +220,53 @@ func initialize_beacon_state_from_eth1*(
# validators - there needs to be at least one member in each committee -
# good to know for testing, though arguably the system is not that useful at
# at that point :)
doAssert genesis_validator_deposits.len >= SLOTS_PER_EPOCH
doAssert deposits.len >= SLOTS_PER_EPOCH
const SECONDS_PER_DAY = uint64(60*60*24)
var state = BeaconState(
genesis_time: genesis_time,
genesis_time:
eth1_timestamp + 2'u64 * SECONDS_PER_DAY -
(eth1_timestamp mod SECONDS_PER_DAY),
eth1_data:
Eth1Data(block_hash: eth1_block_hash, deposit_count: uint64(len(deposits))),
latest_block_header:
BeaconBlockHeader(body_root: hash_tree_root(BeaconBlockBody())),
eth1_data: genesis_eth1_data,
)
# Process deposits
for deposit in genesis_validator_deposits:
let leaves = deposits.mapIt(it.data)
for i, deposit in deposits:
let deposit_data_list = leaves[0..i]
state.eth1_data.deposit_root = hash_tree_root(
sszList(deposit_data_list, int64(2^DEPOSIT_CONTRACT_TREE_DEPTH)))
discard process_deposit(state, deposit, flags)
# Process activations
for validator_index in 0 ..< state.validators.len:
let validator = addr state.validators[validator_index]
if validator.effective_balance >= MAX_EFFECTIVE_BALANCE:
let
balance = state.balances[validator_index]
validator = addr state.validators[validator_index]
validator.effective_balance = min(
balance - balance mod EFFECTIVE_BALANCE_INCREMENT, MAX_EFFECTIVE_BALANCE)
if validator.effective_balance == MAX_EFFECTIVE_BALANCE:
validator.activation_eligibility_epoch = GENESIS_EPOCH
validator.activation_epoch = GENESIS_EPOCH
# Populate active_index_roots and compact_committees_roots
let active_index_root = hash_tree_root(
get_active_validator_indices(state, GENESIS_EPOCH))
sszList(
get_active_validator_indices(state, GENESIS_EPOCH),
VALIDATOR_REGISTRY_LIMIT))
let committee_root = get_compact_committees_root(state, GENESIS_EPOCH)
for index in 0 ..< EPOCHS_PER_HISTORICAL_VECTOR:
state.active_index_roots[index] = active_index_root
state.compact_committees_roots[index] = committee_root
state
proc initialize_beacon_state_from_eth1*(eth1_block_hash: Eth2Digest,
eth1_timestamp: uint64,
deposits: openarray[Deposit],
flags: UpdateFlags = {}): BeaconState =
# TODO: Revisit
initialize_beacon_state_from_eth1(deposits, eth1_timestamp, Eth1Data(deposit_count: deposits.len.uint64, deposit_root: eth1_block_hash), flags)
proc is_valid_genesis_state*(state: BeaconState): bool =
if state.genesis_time < MIN_GENESIS_TIME:
return false

View File

@ -70,4 +70,3 @@ func hash*(x: Eth2Digest): Hash =
# We just slice the first 4 or 8 bytes of the block hash
# depending of if we are on a 32 or 64-bit platform
result = cast[ptr Hash](unsafeAddr x)[]

View File

@ -33,5 +33,10 @@ rm -rf data ; ./start.sh
less data/state_snapshot.json
# Lighthouse genesis state
curl localhost:5052/beacon/state?slot=0 | jq . | sed 's/"0x/"/' | /tmp/lighthouse_state.json
curl localhost:5052/beacon/state?slot=0 | python -m json.tool | sed 's/"0x/"/' > /tmp/lighthouse_state.json
# Format nimbus the same
cat data/state_snapshot.json | python -m json.tool | sed 's/"0x/"/' > /tmp/nimbus_state.json
diff -uw /tmp/nimbus_state.json /tmp/lighthouse_state.json
```

View File

@ -29,4 +29,4 @@ cd target/debug
# fresh start!
rm -rf ~/.lighthouse
./beacon_node --libp2p-addresses="/ip4/127.0.0.1/tcp/50000" --api --rpc testnet --spec minimal quick 16 $genesis_time
./beacon_node --libp2p-addresses="/ip4/127.0.0.1/tcp/50000" testnet --spec minimal quick 16 $genesis_time

View File

@ -6,8 +6,8 @@ import
proc stateSize(deposits: int, maxContent = false) =
var state = initialize_beacon_state_from_eth1(
makeInitialDeposits(
deposits, {skipValidation}), 0, Eth1Data(), {skipValidation})
Eth2Digest(), 0,
makeInitialDeposits(deposits, {skipValidation}), {skipValidation})
if maxContent:
# TODO: state.latest_attestations was removed

View File

@ -46,7 +46,8 @@ cli do(slots = 448,
let
flags = if validate: {} else: {skipValidation}
genesisState = initialize_beacon_state_from_eth1(
makeInitialDeposits(validators, flags), 0, Eth1Data(), flags)
Eth2Digest(), 0,
makeInitialDeposits(validators, flags), flags)
genesisBlock = get_initial_beacon_block(genesisState)
var

View File

@ -12,7 +12,7 @@ import
# Specs
../../beacon_chain/spec/[datatypes, beaconstate, digest],
# Internals
../../beacon_chain/extras,
../../beacon_chain/[extras, interop],
# Mocking procs
./mock_deposits,
# Helpers
@ -20,28 +20,14 @@ import
proc initGenesisState*(num_validators: uint64, genesis_time: uint64 = 0): BeaconState =
# EF magic number (similar to https://en.wikipedia.org/wiki/Magic_number_(programming))
const deposit_root = [byte 0x42] * 32
let eth1_data = Eth1Data(
deposit_root: deposit_root,
deposit_count: num_validators,
block_hash: ZERO_HASH
)
let deposits = mockGenesisBalancedDeposits(
validatorCount = num_validators,
amountInEth = 32, # We create canonical validators with 32 Eth
flags = {skipValidation}
)
result = initialize_beacon_state_from_eth1(
genesis_validator_deposits = deposits,
genesis_time = 0,
genesis_eth1_data = eth1_data,
flags = {skipValidation}
)
initialize_beacon_state_from_eth1(
eth1BlockHash, 0, deposits, {skipValidation})
when isMainModule:
# Smoke test

View File

@ -30,7 +30,8 @@ suite "Attestation pool processing" & preset():
# Genesis state that results in 2 members per committee
let
genState = initialize_beacon_state_from_eth1(
makeInitialDeposits(SLOTS_PER_EPOCH * 2, {skipValidation}), 0, Eth1Data(),
Eth2Digest(), 0,
makeInitialDeposits(SLOTS_PER_EPOCH * 2, {skipValidation}),
{skipValidation})
genBlock = get_initial_beacon_block(genState)

View File

@ -14,5 +14,6 @@ import
suite "Beacon state" & preset():
test "Smoke test initialize_beacon_state_from_eth1" & preset():
let state = initialize_beacon_state_from_eth1(
makeInitialDeposits(SLOTS_PER_EPOCH, {}), 0, Eth1Data(), {})
Eth2Digest(), 0,
makeInitialDeposits(SLOTS_PER_EPOCH, {}), {})
check: state.validators.len == SLOTS_PER_EPOCH

View File

@ -14,8 +14,8 @@ import
suite "Block pool processing" & preset():
let
genState = initialize_beacon_state_from_eth1(
makeInitialDeposits(flags = {skipValidation}), 0, Eth1Data(),
{skipValidation})
Eth2Digest(), 0,
makeInitialDeposits(flags = {skipValidation}), {skipValidation})
genBlock = get_initial_beacon_block(genState)
test "loadTailState gets genesis block on first load" & preset():

View File

@ -19,7 +19,8 @@ suite "Block processing" & preset():
# Genesis state with minimal number of deposits
# TODO bls verification is a bit of a bottleneck here
genesisState = initialize_beacon_state_from_eth1(
makeInitialDeposits(), 0, Eth1Data(), {})
Eth2Digest(), 0,
makeInitialDeposits(), {})
genesisBlock = get_initial_beacon_block(genesisState)
genesisRoot = signing_root(genesisBlock)