2019-08-28 12:07:00 +00:00
|
|
|
# beacon_chain
|
2023-01-09 22:44:44 +00:00
|
|
|
# Copyright (c) 2018-2023 Status Research & Development GmbH
|
2019-08-28 12:07:00 +00:00
|
|
|
# Licensed and distributed under either of
|
2019-11-25 15:30:02 +00:00
|
|
|
# * MIT license (license terms in the root directory or at https://opensource.org/licenses/MIT).
|
|
|
|
# * Apache v2 license (license terms in the root directory or at https://www.apache.org/licenses/LICENSE-2.0).
|
2019-08-28 12:07:00 +00:00
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2023-01-20 14:14:37 +00:00
|
|
|
{.push raises: [].}
|
2021-03-26 06:52:01 +00:00
|
|
|
|
2023-09-21 18:06:51 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.2/tests/core/pyspec/eth2spec/utils/merkle_minimal.py
|
2020-04-08 14:06:30 +00:00
|
|
|
|
2019-08-28 12:07:00 +00:00
|
|
|
# Merkle tree helpers
|
|
|
|
# ---------------------------------------------------------------
|
|
|
|
|
|
|
|
import
|
2020-10-26 08:55:10 +00:00
|
|
|
sequtils,
|
2020-10-21 16:24:36 +00:00
|
|
|
stew/endians2,
|
2019-08-28 12:07:00 +00:00
|
|
|
# Specs
|
2021-08-18 18:57:58 +00:00
|
|
|
../spec/[eth2_merkleization, digest],
|
|
|
|
../spec/datatypes/base
|
2019-08-28 12:07:00 +00:00
|
|
|
|
2023-05-11 08:45:55 +00:00
|
|
|
template getProof*(
|
|
|
|
proofs: seq[Eth2Digest], idxParam: int): openArray[Eth2Digest] =
|
2020-10-26 08:55:10 +00:00
|
|
|
let
|
|
|
|
idx = idxParam
|
2020-12-02 18:15:36 +00:00
|
|
|
## TODO: It's surprising that we have to do +1 here.
|
|
|
|
## It seems that `depositContractLimit` is set too high.
|
|
|
|
startIdx = idx * (DEPOSIT_CONTRACT_TREE_DEPTH + 1)
|
2020-10-26 08:55:10 +00:00
|
|
|
endIdx = startIdx + DEPOSIT_CONTRACT_TREE_DEPTH - 1
|
|
|
|
proofs.toOpenArray(startIdx, endIdx)
|
|
|
|
|
2023-05-11 08:45:55 +00:00
|
|
|
func attachMerkleProofs*(deposits: var openArray[Deposit]): Eth2Digest =
|
|
|
|
let depositsRoots = mapIt(deposits, hash_tree_root(it.data))
|
|
|
|
|
|
|
|
var merkleizer = createMerkleizer(DEPOSIT_CONTRACT_LIMIT)
|
|
|
|
let proofs = merkleizer.addChunksAndGenMerkleProofs(depositsRoots)
|
|
|
|
for i in 0 ..< depositsRoots.len:
|
|
|
|
deposits[i].proof[0 ..< DEPOSIT_CONTRACT_TREE_DEPTH] = getProof(proofs, i)
|
|
|
|
deposits[i].proof[DEPOSIT_CONTRACT_TREE_DEPTH] = default(Eth2Digest)
|
|
|
|
deposits[i].proof[DEPOSIT_CONTRACT_TREE_DEPTH].data[0..7] =
|
|
|
|
toBytesLE deposits.lenu64
|
|
|
|
|
|
|
|
mixInLength(merkleizer.getFinalHash(), deposits.len)
|