height-based merkleizer
This commit is contained in:
Jacek Sieka 2024-01-11 18:34:44 +01:00 committed by GitHub
parent a5297a352c
commit e4a1ae67df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 20 additions and 30 deletions

View File

@ -32,7 +32,7 @@ template getProof*(
func attachMerkleProofs*(deposits: var openArray[Deposit]): Eth2Digest = func attachMerkleProofs*(deposits: var openArray[Deposit]): Eth2Digest =
let depositsRoots = mapIt(deposits, hash_tree_root(it.data)) let depositsRoots = mapIt(deposits, hash_tree_root(it.data))
var merkleizer = createMerkleizer(DEPOSIT_CONTRACT_LIMIT) var merkleizer = createMerkleizer2(DEPOSIT_CONTRACT_TREE_DEPTH + 1)
let proofs = merkleizer.addChunksAndGenMerkleProofs(depositsRoots) let proofs = merkleizer.addChunksAndGenMerkleProofs(depositsRoots)
for i in 0 ..< depositsRoots.len: for i in 0 ..< depositsRoots.len:
deposits[i].proof[0 ..< DEPOSIT_CONTRACT_TREE_DEPTH] = getProof(proofs, i) deposits[i].proof[0 ..< DEPOSIT_CONTRACT_TREE_DEPTH] = getProof(proofs, i)

View File

@ -878,6 +878,14 @@ func get_next_sync_committee*(
res.aggregate_pubkey = finish(attestersAgg).toPubKey() res.aggregate_pubkey = finish(attestersAgg).toPubKey()
res res
func compute_deposit_root(deposits: openArray[DepositData]): Eth2Digest =
var merkleizer = createMerkleizer2(DEPOSIT_CONTRACT_TREE_DEPTH + 1)
for i, deposit in deposits:
let htr = hash_tree_root(deposit)
merkleizer.addChunk(htr.data)
mixInLength(merkleizer.getFinalHash(), deposits.len)
# https://github.com/ethereum/consensus-specs/blob/v1.4.0-alpha.3/specs/phase0/beacon-chain.md#genesis # https://github.com/ethereum/consensus-specs/blob/v1.4.0-alpha.3/specs/phase0/beacon-chain.md#genesis
proc initialize_beacon_state_from_eth1( proc initialize_beacon_state_from_eth1(
cfg: RuntimeConfig, cfg: RuntimeConfig,
@ -906,8 +914,11 @@ proc initialize_beacon_state_from_eth1(
state = phase0.BeaconState( state = phase0.BeaconState(
fork: genesisFork(cfg), fork: genesisFork(cfg),
genesis_time: genesis_time_from_eth1_timestamp(cfg, eth1_timestamp), genesis_time: genesis_time_from_eth1_timestamp(cfg, eth1_timestamp),
eth1_data: eth1_data:Eth1Data(
Eth1Data(block_hash: eth1_block_hash, deposit_count: uint64(len(deposits))), deposit_count: deposits.lenu64,
deposit_root: compute_deposit_root(deposits),
block_hash: eth1_block_hash),
eth1_deposit_index: deposits.lenu64,
latest_block_header: latest_block_header:
BeaconBlockHeader( BeaconBlockHeader(
body_root: hash_tree_root(default(phase0.BeaconBlockBody)))) body_root: hash_tree_root(default(phase0.BeaconBlockBody))))
@ -915,17 +926,6 @@ proc initialize_beacon_state_from_eth1(
# Seed RANDAO with Eth1 entropy # Seed RANDAO with Eth1 entropy
state.randao_mixes.fill(eth1_block_hash) state.randao_mixes.fill(eth1_block_hash)
var merkleizer = createMerkleizer(DEPOSIT_CONTRACT_LIMIT)
for i, deposit in deposits:
let htr = hash_tree_root(deposit)
merkleizer.addChunk(htr.data)
# This is already known in the Eth1 monitor, but it would be too
# much work to refactor all the existing call sites in the test suite
state.eth1_data.deposit_root = mixInLength(merkleizer.getFinalHash(),
deposits.len)
state.eth1_deposit_index = deposits.lenu64
var pubkeyToIndex = initTable[ValidatorPubKey, ValidatorIndex]() var pubkeyToIndex = initTable[ValidatorPubKey, ValidatorIndex]()
for idx, deposit in deposits: for idx, deposit in deposits:
let let
@ -1020,24 +1020,16 @@ proc initialize_beacon_state_from_eth1*(
fork: fork, fork: fork,
genesis_time: genesis_time_from_eth1_timestamp(cfg, eth1_timestamp), genesis_time: genesis_time_from_eth1_timestamp(cfg, eth1_timestamp),
eth1_data: Eth1Data( eth1_data: Eth1Data(
block_hash: eth1_block_hash, deposit_count: uint64(len(deposits))), deposit_count: deposits.lenu64,
deposit_root: compute_deposit_root(deposits),
block_hash: eth1_block_hash),
eth1_deposit_index: deposits.lenu64,
latest_block_header: BeaconBlockHeader( latest_block_header: BeaconBlockHeader(
body_root: hash_tree_root(default consensusFork.BeaconBlockBody))) body_root: hash_tree_root(default consensusFork.BeaconBlockBody)))
# Seed RANDAO with Eth1 entropy # Seed RANDAO with Eth1 entropy
state.randao_mixes.data.fill(eth1_block_hash) state.randao_mixes.data.fill(eth1_block_hash)
var merkleizer = createMerkleizer(DEPOSIT_CONTRACT_LIMIT)
for i, deposit in deposits:
let htr = hash_tree_root(deposit)
merkleizer.addChunk(htr.data)
# This is already known in the Eth1 monitor, but it would be too
# much work to refactor all the existing call sites in the test suite
state.eth1_data.deposit_root = mixInLength(merkleizer.getFinalHash(),
deposits.len)
state.eth1_deposit_index = deposits.lenu64
var pubkeyToIndex = initTable[ValidatorPubKey, ValidatorIndex]() var pubkeyToIndex = initTable[ValidatorPubKey, ValidatorIndex]()
for idx, deposit in deposits: for idx, deposit in deposits:
let let

View File

@ -87,8 +87,6 @@ const
DEPOSIT_CONTRACT_TREE_DEPTH* = 32 DEPOSIT_CONTRACT_TREE_DEPTH* = 32
BASE_REWARDS_PER_EPOCH* = 4 BASE_REWARDS_PER_EPOCH* = 4
DEPOSIT_CONTRACT_LIMIT* = Limit(1'u64 shl DEPOSIT_CONTRACT_TREE_DEPTH)
template maxSize*(n: int) {.pragma.} template maxSize*(n: int) {.pragma.}
# Block validation flow # Block validation flow

View File

@ -22,7 +22,7 @@ from ./datatypes/capella import HashedBeaconState, SignedBeaconBlock
export ssz_codec, merkleization, proofs export ssz_codec, merkleization, proofs
type type
DepositsMerkleizer* = SszMerkleizer[DEPOSIT_CONTRACT_LIMIT] DepositsMerkleizer* = SszMerkleizer2[DEPOSIT_CONTRACT_TREE_DEPTH + 1]
# Can't use `ForkyHashedBeaconState`/`ForkyHashedSignedBeaconBlock` without # Can't use `ForkyHashedBeaconState`/`ForkyHashedSignedBeaconBlock` without
# creating recursive module dependency through `forks`. # creating recursive module dependency through `forks`.

@ -1 +1 @@
Subproject commit f87c99be04f3051fe2d9b4facf5e6e71251e8555 Subproject commit 66de36a9ecc67c98ee858faf555b8a8dd2ea2b5f