2019-08-28 12:07:00 +00:00
|
|
|
# beacon_chain
|
2020-04-10 13:59:17 +00:00
|
|
|
# Copyright (c) 2018-2020 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.
|
|
|
|
|
|
|
|
# Mocking deposits and genesis deposits
|
|
|
|
# ---------------------------------------------------------------
|
|
|
|
|
|
|
|
import
|
|
|
|
# Standard library
|
2020-11-16 16:10:51 +00:00
|
|
|
math,
|
2020-07-07 23:02:14 +00:00
|
|
|
|
2019-08-28 12:07:00 +00:00
|
|
|
# Specs
|
2020-07-07 23:02:14 +00:00
|
|
|
../../beacon_chain/spec/[datatypes, crypto, digest,
|
|
|
|
keystore, signatures, presets],
|
|
|
|
|
2019-08-28 12:07:00 +00:00
|
|
|
# Internals
|
2021-03-03 06:23:05 +00:00
|
|
|
../../beacon_chain/extras,
|
|
|
|
../../beacon_chain/ssz,
|
|
|
|
../../beacon_chain/eth1/merkle_minimal,
|
2020-07-07 23:02:14 +00:00
|
|
|
|
2019-08-28 12:07:00 +00:00
|
|
|
# Mocking procs
|
2020-04-15 07:59:47 +00:00
|
|
|
./mock_validator_keys
|
2019-08-28 12:07:00 +00:00
|
|
|
|
|
|
|
func mockDepositData(
|
|
|
|
pubkey: ValidatorPubKey,
|
|
|
|
amount: uint64,
|
2020-06-16 05:45:04 +00:00
|
|
|
): DepositData =
|
2019-08-28 12:07:00 +00:00
|
|
|
# Insecurely use pubkey as withdrawal key
|
2020-06-16 05:45:04 +00:00
|
|
|
DepositData(
|
|
|
|
pubkey: pubkey,
|
|
|
|
withdrawal_credentials: makeWithdrawalCredentials(pubkey),
|
|
|
|
amount: amount,
|
|
|
|
)
|
2019-08-28 12:07:00 +00:00
|
|
|
|
|
|
|
func mockDepositData(
|
|
|
|
pubkey: ValidatorPubKey,
|
|
|
|
privkey: ValidatorPrivKey,
|
|
|
|
amount: uint64,
|
|
|
|
# withdrawal_credentials: Eth2Digest,
|
|
|
|
flags: UpdateFlags = {}
|
2020-06-16 05:45:04 +00:00
|
|
|
): DepositData =
|
|
|
|
var ret = mockDepositData(pubkey, amount)
|
2020-03-05 12:52:10 +00:00
|
|
|
if skipBlsValidation notin flags:
|
2020-07-07 23:02:14 +00:00
|
|
|
ret.signature = defaultRuntimePreset.get_deposit_signature(ret, privkey)
|
2020-06-16 05:45:04 +00:00
|
|
|
ret
|
2019-08-28 12:07:00 +00:00
|
|
|
|
|
|
|
template mockGenesisDepositsImpl(
|
2020-11-14 21:43:27 +00:00
|
|
|
result: seq[DepositData],
|
2019-08-28 12:07:00 +00:00
|
|
|
validatorCount: uint64,
|
|
|
|
amount: untyped,
|
|
|
|
flags: UpdateFlags = {},
|
|
|
|
updateAmount: untyped,
|
|
|
|
) =
|
|
|
|
# Genesis deposits with varying amounts
|
|
|
|
|
2020-06-12 16:03:46 +00:00
|
|
|
# NOTE: prefer to er on the side of caution and generate a valid Deposit
|
|
|
|
# (it can still be skipped later).
|
2020-03-05 12:52:10 +00:00
|
|
|
if skipBlsValidation in flags:
|
2019-08-28 12:07:00 +00:00
|
|
|
# 1st loop - build deposit data
|
2020-07-26 18:55:48 +00:00
|
|
|
for valIdx in 0 ..< validatorCount:
|
2019-08-28 12:07:00 +00:00
|
|
|
# Directly build the Deposit in-place for speed
|
|
|
|
result.setLen(valIdx + 1)
|
|
|
|
|
|
|
|
updateAmount
|
|
|
|
|
|
|
|
# DepositData
|
2020-11-14 21:43:27 +00:00
|
|
|
result[valIdx] = mockDepositData(MockPubKeys[valIdx], amount)
|
2019-08-28 12:07:00 +00:00
|
|
|
else: # With signing
|
|
|
|
var depositsDataHash: seq[Eth2Digest]
|
|
|
|
var depositsData: seq[DepositData]
|
|
|
|
|
|
|
|
# 1st loop - build deposit data
|
2020-07-26 18:55:48 +00:00
|
|
|
for valIdx in 0 ..< validatorCount:
|
2019-08-28 12:07:00 +00:00
|
|
|
# Directly build the Deposit in-place for speed
|
|
|
|
result.setLen(valIdx + 1)
|
|
|
|
|
|
|
|
updateAmount
|
|
|
|
|
|
|
|
# DepositData
|
2020-11-14 21:43:27 +00:00
|
|
|
result[valIdx] = mockDepositData(
|
2020-06-16 05:45:04 +00:00
|
|
|
MockPubKeys[valIdx], MockPrivKeys[valIdx], amount, flags)
|
2019-08-28 12:07:00 +00:00
|
|
|
|
2020-11-14 21:43:27 +00:00
|
|
|
depositsData.add result[valIdx]
|
|
|
|
depositsDataHash.add hash_tree_root(result[valIdx])
|
2019-08-28 12:07:00 +00:00
|
|
|
|
|
|
|
proc mockGenesisBalancedDeposits*(
|
|
|
|
validatorCount: uint64,
|
|
|
|
amountInEth: Positive,
|
|
|
|
flags: UpdateFlags = {}
|
2020-11-14 21:43:27 +00:00
|
|
|
): seq[DepositData] =
|
2019-08-28 12:07:00 +00:00
|
|
|
## The amount should be strictly positive
|
|
|
|
## - 1 is the minimum deposit amount (MIN_DEPOSIT_AMOUNT)
|
|
|
|
## - 16 is the ejection balance (EJECTION_BALANCE)
|
|
|
|
## - 32 is the max effective balance (MAX_EFFECTIVE_BALANCE)
|
|
|
|
## ETH beyond do not contribute more for staking.
|
|
|
|
##
|
|
|
|
## Only validators with 32 ETH will be active at genesis
|
|
|
|
|
|
|
|
let amount = amountInEth.uint64 * 10'u64^9
|
|
|
|
mockGenesisDepositsImpl(result, validatorCount,amount,flags):
|
|
|
|
discard
|
|
|
|
|
|
|
|
proc mockUpdateStateForNewDeposit*(
|
|
|
|
state: var BeaconState,
|
|
|
|
validator_index: uint64,
|
|
|
|
amount: uint64,
|
|
|
|
# withdrawal_credentials: Eth2Digest
|
|
|
|
flags: UpdateFlags
|
|
|
|
): Deposit =
|
|
|
|
|
2019-11-18 16:52:02 +00:00
|
|
|
|
2019-08-28 12:07:00 +00:00
|
|
|
# TODO withdrawal credentials
|
|
|
|
|
2020-06-16 05:45:04 +00:00
|
|
|
result.data = mockDepositData(
|
2019-08-28 12:07:00 +00:00
|
|
|
MockPubKeys[validator_index],
|
|
|
|
MockPrivKeys[validator_index],
|
|
|
|
amount,
|
|
|
|
# withdrawal_credentials: Eth2Digest
|
|
|
|
flags
|
|
|
|
)
|
|
|
|
|
2020-04-10 13:59:17 +00:00
|
|
|
var result_seq = @[result]
|
|
|
|
attachMerkleProofs(result_seq)
|
|
|
|
result.proof = result_seq[0].proof
|
2019-08-28 12:07:00 +00:00
|
|
|
|
|
|
|
# TODO: this logic from the eth2.0-specs test suite seems strange
|
|
|
|
# but confirmed by running it
|
|
|
|
state.eth1_deposit_index = 0
|
2020-04-10 13:59:17 +00:00
|
|
|
state.eth1_data.deposit_root =
|
2020-05-18 17:49:22 +00:00
|
|
|
hash_tree_root(List[DepositData, 2'i64^DEPOSIT_CONTRACT_TREE_DEPTH](@[result.data]))
|
2019-08-28 12:07:00 +00:00
|
|
|
state.eth1_data.deposit_count = 1
|