2019-08-28 12:07:00 +00:00
|
|
|
# beacon_chain
|
2022-01-04 06:08:19 +00:00
|
|
|
# Copyright (c) 2018-2022 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.
|
|
|
|
|
2021-03-26 06:52:01 +00:00
|
|
|
{.push raises: [Defect].}
|
|
|
|
|
2022-01-04 06:08:19 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.1.8/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-11-03 01:21:07 +00:00
|
|
|
const depositContractLimit* = Limit(1'u64 shl DEPOSIT_CONTRACT_TREE_DEPTH)
|
2020-10-26 08:55:10 +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))
|
|
|
|
|
|
|
|
var incrementalMerkleProofs = createMerkleizer(depositContractLimit)
|
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)
|
|
|
|
|