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.
|
|
|
|
|
2022-07-29 10:53:42 +00:00
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
else:
|
|
|
|
{.push raises: [].}
|
2021-03-26 06:52:01 +00:00
|
|
|
|
2023-01-09 22:44:44 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.3.0-rc.0/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
|
|
|
|
2020-10-28 18:35:31 +00:00
|
|
|
func attachMerkleProofs*(deposits: var openArray[Deposit]) =
|
2020-10-21 16:24:36 +00:00
|
|
|
let depositsRoots = mapIt(deposits, hash_tree_root(it.data))
|
|
|
|
|
2022-06-29 16:53:59 +00:00
|
|
|
var incrementalMerkleProofs = createMerkleizer(DEPOSIT_CONTRACT_LIMIT)
|
2020-10-25 18:47:25 +00:00
|
|
|
|
2020-10-21 16:24:36 +00:00
|
|
|
for i in 0 ..< depositsRoots.len:
|
|
|
|
incrementalMerkleProofs.addChunkAndGenMerkleProof(depositsRoots[i], deposits[i].proof)
|
2020-11-03 01:21:07 +00:00
|
|
|
deposits[i].proof[32] = default(Eth2Digest)
|
2020-10-21 16:24:36 +00:00
|
|
|
deposits[i].proof[32].data[0..7] = toBytesLE uint64(i + 1)
|
|
|
|
|
2020-10-28 18:35:31 +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)
|
|
|
|
|