2018-07-23 12:58:41 +00:00
|
|
|
# beacon_chain
|
2024-01-03 22:36:05 +00:00
|
|
|
# Copyright (c) 2018-2024 Status Research & Development GmbH
|
2018-07-23 12:58:41 +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).
|
2018-07-23 12:58:41 +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: [].}
|
2020-04-22 05:53:02 +00:00
|
|
|
|
2024-02-18 01:16:49 +00:00
|
|
|
# Uncategorized helper functions from the spec
|
|
|
|
|
2019-09-25 17:07:08 +00:00
|
|
|
import
|
2021-10-14 08:28:22 +00:00
|
|
|
# Status libraries
|
2024-07-06 20:25:31 +00:00
|
|
|
stew/[byteutils, endians2, objects],
|
2022-07-10 15:26:29 +00:00
|
|
|
chronicles,
|
2023-04-18 09:21:15 +00:00
|
|
|
eth/common/[eth_types, eth_types_rlp],
|
2022-11-28 13:41:25 +00:00
|
|
|
eth/rlp, eth/trie/[db, hexary],
|
2019-09-25 17:07:08 +00:00
|
|
|
# Internal
|
2021-11-05 07:34:34 +00:00
|
|
|
"."/[eth2_merkleization, forks, ssz_codec]
|
2021-08-12 13:08:20 +00:00
|
|
|
|
2021-08-18 18:57:58 +00:00
|
|
|
# TODO although eth2_merkleization already exports ssz_codec, *sometimes* code
|
2023-02-13 17:15:16 +00:00
|
|
|
# fails to compile if the export is not done here also. Exporting rlp avoids a
|
|
|
|
# generics sandwich where rlp/writer.append() is not seen, by a caller outside
|
|
|
|
# this module via compute_execution_block_hash() called from block_processor.
|
2021-08-18 18:57:58 +00:00
|
|
|
export
|
2023-02-13 17:15:16 +00:00
|
|
|
eth2_merkleization, forks, rlp, ssz_codec
|
2018-07-23 12:58:41 +00:00
|
|
|
|
2024-06-16 00:59:25 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.3/specs/phase0/weak-subjectivity.md#constants
|
2024-03-19 13:22:07 +00:00
|
|
|
const ETH_TO_GWEI = 1_000_000_000.Gwei
|
|
|
|
|
2023-05-10 08:23:59 +00:00
|
|
|
func toEther*(gwei: Gwei): Ether =
|
|
|
|
(gwei div ETH_TO_GWEI).Ether
|
|
|
|
|
2024-03-19 13:22:07 +00:00
|
|
|
func toGwei*(eth: Ether): Gwei =
|
|
|
|
distinctBase(eth) * ETH_TO_GWEI
|
|
|
|
|
2022-08-19 10:28:42 +00:00
|
|
|
type
|
2023-08-07 12:23:44 +00:00
|
|
|
ExecutionHash256* = eth_types.Hash256
|
2023-08-25 09:29:39 +00:00
|
|
|
ExecutionTransaction* = eth_types.Transaction
|
2023-08-28 15:56:40 +00:00
|
|
|
ExecutionReceipt* = eth_types.Receipt
|
2023-09-04 18:44:03 +00:00
|
|
|
ExecutionWithdrawal* = eth_types.Withdrawal
|
2024-07-05 08:18:50 +00:00
|
|
|
ExecutionDepositRequest* = eth_types.DepositRequest
|
|
|
|
ExecutionWithdrawalRequest* = eth_types.WithdrawalRequest
|
|
|
|
ExecutionConsolidationRequest* = eth_types.ConsolidationRequest
|
2023-08-07 12:23:44 +00:00
|
|
|
ExecutionBlockHeader* = eth_types.BlockHeader
|
2022-08-19 10:28:42 +00:00
|
|
|
|
|
|
|
FinalityCheckpoints* = object
|
|
|
|
justified*: Checkpoint
|
|
|
|
finalized*: Checkpoint
|
2022-07-10 15:26:29 +00:00
|
|
|
|
|
|
|
func shortLog*(v: FinalityCheckpoints): auto =
|
|
|
|
(
|
|
|
|
justified: shortLog(v.justified),
|
|
|
|
finalized: shortLog(v.finalized)
|
|
|
|
)
|
|
|
|
|
|
|
|
chronicles.formatIt FinalityCheckpoints: it.shortLog
|
|
|
|
|
2024-02-17 09:02:50 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.7/specs/phase0/beacon-chain.md#integer_squareroot
|
2018-12-19 04:36:10 +00:00
|
|
|
func integer_squareroot*(n: SomeInteger): SomeInteger =
|
2020-09-08 08:54:55 +00:00
|
|
|
## Return the largest integer ``x`` such that ``x**2 <= n``.
|
2019-03-16 19:52:37 +00:00
|
|
|
doAssert n >= 0'u64
|
|
|
|
|
2024-02-17 09:02:50 +00:00
|
|
|
if n == high(uint64):
|
|
|
|
return 4294967295'u64
|
|
|
|
|
2018-12-03 21:41:24 +00:00
|
|
|
var
|
|
|
|
x = n
|
|
|
|
y = (x + 1) div 2
|
|
|
|
while y < x:
|
|
|
|
x = y
|
|
|
|
y = (x + n div x) div 2
|
|
|
|
x
|
|
|
|
|
2024-02-22 02:42:57 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.7/specs/phase0/beacon-chain.md#is_active_validator
|
2019-02-20 01:33:58 +00:00
|
|
|
func is_active_validator*(validator: Validator, epoch: Epoch): bool =
|
2023-05-05 20:37:56 +00:00
|
|
|
## Check if ``validator`` is active.
|
2019-01-29 03:22:22 +00:00
|
|
|
validator.activation_epoch <= epoch and epoch < validator.exit_epoch
|
2019-01-16 11:07:41 +00:00
|
|
|
|
2021-12-20 19:20:31 +00:00
|
|
|
func is_exited_validator*(validator: Validator, epoch: Epoch): bool =
|
2023-05-05 20:37:56 +00:00
|
|
|
## Check if ``validator`` is exited.
|
2021-12-20 19:20:31 +00:00
|
|
|
validator.exit_epoch <= epoch
|
|
|
|
|
|
|
|
func is_withdrawable_validator*(validator: Validator, epoch: Epoch): bool =
|
|
|
|
epoch >= validator.withdrawable_epoch
|
|
|
|
|
2024-01-20 11:19:47 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.6/specs/phase0/beacon-chain.md#get_active_validator_indices
|
2021-11-05 07:34:34 +00:00
|
|
|
iterator get_active_validator_indices*(state: ForkyBeaconState, epoch: Epoch):
|
2021-08-24 20:09:03 +00:00
|
|
|
ValidatorIndex =
|
2022-05-23 23:39:08 +00:00
|
|
|
for vidx in state.validators.vindices:
|
|
|
|
if is_active_validator(state.validators[vidx], epoch):
|
|
|
|
yield vidx
|
2021-08-24 20:09:03 +00:00
|
|
|
|
2021-11-05 07:34:34 +00:00
|
|
|
func get_active_validator_indices*(state: ForkyBeaconState, epoch: Epoch):
|
2019-05-09 12:27:37 +00:00
|
|
|
seq[ValidatorIndex] =
|
2020-09-08 08:54:55 +00:00
|
|
|
## Return the sequence of active validator indices at ``epoch``.
|
2021-08-24 20:09:03 +00:00
|
|
|
var res = newSeqOfCap[ValidatorIndex](state.validators.len)
|
2022-05-23 23:39:08 +00:00
|
|
|
for vidx in get_active_validator_indices(state, epoch):
|
|
|
|
res.add vidx
|
2021-08-24 20:09:03 +00:00
|
|
|
res
|
2019-05-09 12:27:37 +00:00
|
|
|
|
2021-11-05 07:34:34 +00:00
|
|
|
func get_active_validator_indices_len*(state: ForkyBeaconState, epoch: Epoch):
|
2021-05-28 15:25:58 +00:00
|
|
|
uint64 =
|
2022-05-23 23:39:08 +00:00
|
|
|
for vidx in state.validators.vindices:
|
2022-05-30 13:30:42 +00:00
|
|
|
if is_active_validator(state.validators.item(vidx), epoch):
|
2020-09-22 20:42:42 +00:00
|
|
|
inc result
|
|
|
|
|
2021-11-05 07:34:34 +00:00
|
|
|
func get_active_validator_indices_len*(
|
|
|
|
state: ForkedHashedBeaconState; epoch: Epoch): uint64 =
|
|
|
|
withState(state):
|
2022-08-26 14:14:18 +00:00
|
|
|
get_active_validator_indices_len(forkyState.data, epoch)
|
2021-11-05 07:34:34 +00:00
|
|
|
|
2024-04-18 01:00:04 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.4.0/specs/phase0/beacon-chain.md#get_current_epoch
|
2021-11-05 07:34:34 +00:00
|
|
|
func get_current_epoch*(state: ForkyBeaconState): Epoch =
|
2020-09-08 08:54:55 +00:00
|
|
|
## Return the current epoch.
|
2022-01-11 10:01:54 +00:00
|
|
|
state.slot.epoch
|
2019-01-29 03:22:22 +00:00
|
|
|
|
2024-04-17 03:51:16 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.4.0/specs/phase0/beacon-chain.md#get_current_epoch
|
2021-11-05 07:34:34 +00:00
|
|
|
func get_current_epoch*(state: ForkedHashedBeaconState): Epoch =
|
|
|
|
## Return the current epoch.
|
2022-08-26 14:14:18 +00:00
|
|
|
withState(state): get_current_epoch(forkyState.data)
|
2021-11-05 07:34:34 +00:00
|
|
|
|
2024-01-20 11:19:47 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.6/specs/phase0/beacon-chain.md#get_previous_epoch
|
2022-07-06 10:33:02 +00:00
|
|
|
func get_previous_epoch*(
|
|
|
|
state: ForkyBeaconState | ForkedHashedBeaconState): Epoch =
|
|
|
|
## Return the previous epoch (unless the current epoch is ``GENESIS_EPOCH``).
|
|
|
|
get_previous_epoch(get_current_epoch(state))
|
|
|
|
|
2024-04-17 03:51:16 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.4.0/specs/phase0/beacon-chain.md#get_randao_mix
|
2021-11-05 07:34:34 +00:00
|
|
|
func get_randao_mix*(state: ForkyBeaconState, epoch: Epoch): Eth2Digest =
|
2023-05-05 20:37:56 +00:00
|
|
|
## Return the randao mix at a recent ``epoch``.
|
2020-07-13 14:44:58 +00:00
|
|
|
state.randao_mixes[epoch mod EPOCHS_PER_HISTORICAL_VECTOR]
|
2019-01-29 03:22:22 +00:00
|
|
|
|
2022-12-08 02:07:41 +00:00
|
|
|
func bytes_to_uint32*(data: openArray[byte]): uint32 =
|
|
|
|
doAssert data.len == 4
|
|
|
|
|
|
|
|
# Little-endian data representation
|
|
|
|
uint32.fromBytesLE(data)
|
|
|
|
|
2020-10-28 18:35:31 +00:00
|
|
|
func bytes_to_uint64*(data: openArray[byte]): uint64 =
|
2019-03-13 23:04:43 +00:00
|
|
|
doAssert data.len == 8
|
2019-02-13 10:26:32 +00:00
|
|
|
|
|
|
|
# Little-endian data representation
|
2021-02-08 15:13:02 +00:00
|
|
|
uint64.fromBytesLE(data)
|
2019-02-13 10:26:32 +00:00
|
|
|
|
2022-01-08 20:06:34 +00:00
|
|
|
func uint_to_bytes*(x: uint64): array[8, byte] = toBytesLE(x)
|
|
|
|
func uint_to_bytes*(x: uint32): array[4, byte] = toBytesLE(x)
|
|
|
|
func uint_to_bytes*(x: uint16): array[2, byte] = toBytesLE(x)
|
|
|
|
func uint_to_bytes*(x: uint8): array[1, byte] = toBytesLE(x)
|
2019-02-13 10:26:32 +00:00
|
|
|
|
2024-02-22 02:42:57 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.7/specs/phase0/beacon-chain.md#compute_domain
|
2019-09-01 15:02:49 +00:00
|
|
|
func compute_domain*(
|
|
|
|
domain_type: DomainType,
|
2020-07-07 23:02:14 +00:00
|
|
|
fork_version: Version,
|
2021-03-19 02:22:45 +00:00
|
|
|
genesis_validators_root: Eth2Digest = ZERO_HASH): Eth2Domain =
|
2020-09-08 08:54:55 +00:00
|
|
|
## Return the domain for the ``domain_type`` and ``fork_version``.
|
2022-07-12 17:50:12 +00:00
|
|
|
#
|
2023-01-30 19:21:51 +00:00
|
|
|
# TODO toOpenArray can't be used from JavaScript backend
|
2022-07-12 17:50:12 +00:00
|
|
|
# https://github.com/nim-lang/Nim/issues/15952
|
2020-03-22 16:03:07 +00:00
|
|
|
let fork_data_root =
|
|
|
|
compute_fork_data_root(fork_version, genesis_validators_root)
|
2022-01-08 20:06:34 +00:00
|
|
|
result[0..3] = domain_type.data
|
2020-08-27 06:32:51 +00:00
|
|
|
result[4..31] = fork_data_root.data.toOpenArray(0, 27)
|
2019-06-14 16:21:04 +00:00
|
|
|
|
2024-06-22 05:28:19 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.3/specs/phase0/beacon-chain.md#get_domain
|
2019-03-18 15:42:42 +00:00
|
|
|
func get_domain*(
|
2021-03-19 02:22:45 +00:00
|
|
|
fork: Fork,
|
|
|
|
domain_type: DomainType,
|
|
|
|
epoch: Epoch,
|
|
|
|
genesis_validators_root: Eth2Digest): Eth2Domain =
|
2019-04-29 16:48:30 +00:00
|
|
|
## Return the signature domain (fork version concatenated with domain type)
|
|
|
|
## of a message.
|
2020-03-30 11:31:44 +00:00
|
|
|
let fork_version =
|
|
|
|
if epoch < fork.epoch:
|
|
|
|
fork.previous_version
|
|
|
|
else:
|
|
|
|
fork.current_version
|
|
|
|
compute_domain(domain_type, fork_version, genesis_validators_root)
|
2019-03-18 15:42:42 +00:00
|
|
|
|
2019-11-21 09:57:59 +00:00
|
|
|
func get_domain*(
|
2021-11-05 07:34:34 +00:00
|
|
|
state: ForkyBeaconState, domain_type: DomainType, epoch: Epoch): Eth2Domain =
|
2019-11-21 09:57:59 +00:00
|
|
|
## Return the signature domain (fork version concatenated with domain type)
|
|
|
|
## of a message.
|
2020-04-22 23:35:55 +00:00
|
|
|
get_domain(state.fork, domain_type, epoch, state.genesis_validators_root)
|
2019-04-29 16:48:30 +00:00
|
|
|
|
2024-01-20 11:19:47 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.6/specs/phase0/beacon-chain.md#compute_signing_root
|
2021-03-19 02:22:45 +00:00
|
|
|
func compute_signing_root*(ssz_object: auto, domain: Eth2Domain): Eth2Digest =
|
2023-05-05 20:37:56 +00:00
|
|
|
## Return the signing root for the corresponding signing data.
|
2020-06-29 18:08:58 +00:00
|
|
|
let domain_wrapped_object = SigningData(
|
|
|
|
object_root: hash_tree_root(ssz_object),
|
|
|
|
domain: domain
|
|
|
|
)
|
2020-01-30 14:03:26 +00:00
|
|
|
hash_tree_root(domain_wrapped_object)
|
|
|
|
|
2024-02-22 02:42:57 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.7/specs/phase0/beacon-chain.md#get_seed
|
2023-05-15 15:41:30 +00:00
|
|
|
func get_seed*(
|
|
|
|
state: ForkyBeaconState, epoch: Epoch, domain_type: DomainType,
|
|
|
|
mix: Eth2Digest): Eth2Digest =
|
2020-09-08 08:54:55 +00:00
|
|
|
## Return the seed at ``epoch``.
|
2019-11-10 00:03:41 +00:00
|
|
|
var seed_input : array[4+8+32, byte]
|
2022-01-08 20:06:34 +00:00
|
|
|
seed_input[0..3] = domain_type.data
|
|
|
|
seed_input[4..11] = uint_to_bytes(epoch.uint64)
|
2023-05-15 15:41:30 +00:00
|
|
|
seed_input[12..43] = mix.data
|
2020-06-16 12:16:43 +00:00
|
|
|
eth2digest(seed_input)
|
2021-04-04 16:24:45 +00:00
|
|
|
|
2023-05-15 15:41:30 +00:00
|
|
|
func get_seed*(state: ForkyBeaconState, epoch: Epoch, domain_type: DomainType):
|
|
|
|
Eth2Digest =
|
|
|
|
# Detect potential underflow
|
|
|
|
static: doAssert EPOCHS_PER_HISTORICAL_VECTOR > MIN_SEED_LOOKAHEAD
|
|
|
|
let mix = get_randao_mix(state, # Avoid underflow
|
|
|
|
epoch + EPOCHS_PER_HISTORICAL_VECTOR - MIN_SEED_LOOKAHEAD - 1)
|
|
|
|
state.get_seed(epoch, domain_type, mix)
|
|
|
|
|
2024-03-14 06:26:36 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.4.0/specs/altair/beacon-chain.md#add_flag
|
2023-08-02 23:03:40 +00:00
|
|
|
func add_flag*(flags: ParticipationFlags, flag_index: TimelyFlag): ParticipationFlags =
|
|
|
|
let flag = ParticipationFlags(1'u8 shl ord(flag_index))
|
2021-04-04 16:24:45 +00:00
|
|
|
flags or flag
|
|
|
|
|
2024-03-14 06:26:36 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.4.0/specs/altair/beacon-chain.md#has_flag
|
2023-08-02 23:03:40 +00:00
|
|
|
func has_flag*(flags: ParticipationFlags, flag_index: TimelyFlag): bool =
|
|
|
|
let flag = ParticipationFlags(1'u8 shl ord(flag_index))
|
2021-04-04 16:24:45 +00:00
|
|
|
(flags and flag) == flag
|
2021-09-08 16:57:00 +00:00
|
|
|
|
2024-01-18 14:45:10 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.4/specs/deneb/p2p-interface.md#check_blob_sidecar_inclusion_proof
|
|
|
|
func verify_blob_sidecar_inclusion_proof*(
|
|
|
|
blob_sidecar: BlobSidecar): Result[void, string] =
|
|
|
|
let gindex = kzg_commitment_inclusion_proof_gindex(blob_sidecar.index)
|
|
|
|
if not is_valid_merkle_branch(
|
|
|
|
hash_tree_root(blob_sidecar.kzg_commitment),
|
|
|
|
blob_sidecar.kzg_commitment_inclusion_proof,
|
|
|
|
KZG_COMMITMENT_INCLUSION_PROOF_DEPTH,
|
|
|
|
get_subtree_index(gindex),
|
|
|
|
blob_sidecar.signed_block_header.message.body_root):
|
|
|
|
return err("BlobSidecar: inclusion proof not valid")
|
|
|
|
ok()
|
|
|
|
|
2023-11-06 06:48:43 +00:00
|
|
|
func create_blob_sidecars*(
|
2024-02-26 02:38:21 +00:00
|
|
|
forkyBlck: deneb.SignedBeaconBlock | electra.SignedBeaconBlock,
|
2023-11-06 06:48:43 +00:00
|
|
|
kzg_proofs: KzgProofs,
|
|
|
|
blobs: Blobs): seq[BlobSidecar] =
|
|
|
|
template kzg_commitments: untyped =
|
|
|
|
forkyBlck.message.body.blob_kzg_commitments
|
|
|
|
doAssert kzg_proofs.len == blobs.len
|
|
|
|
doAssert kzg_proofs.len == kzg_commitments.len
|
|
|
|
|
|
|
|
var res = newSeqOfCap[BlobSidecar](blobs.len)
|
|
|
|
let signedBlockHeader = forkyBlck.toSignedBeaconBlockHeader()
|
|
|
|
for i in 0 ..< blobs.lenu64:
|
|
|
|
var sidecar = BlobSidecar(
|
|
|
|
index: i,
|
|
|
|
blob: blobs[i],
|
|
|
|
kzg_commitment: kzg_commitments[i],
|
|
|
|
kzg_proof: kzg_proofs[i],
|
|
|
|
signed_block_header: signedBlockHeader)
|
|
|
|
forkyBlck.message.body.build_proof(
|
|
|
|
kzg_commitment_inclusion_proof_gindex(i),
|
|
|
|
sidecar.kzg_commitment_inclusion_proof).expect("Valid gindex")
|
|
|
|
res.add(sidecar)
|
|
|
|
res
|
|
|
|
|
2024-06-22 05:28:19 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.3/specs/altair/light-client/sync-protocol.md#is_sync_committee_update
|
2023-01-14 21:19:50 +00:00
|
|
|
template is_sync_committee_update*(update: SomeForkyLightClientUpdate): bool =
|
|
|
|
when update is SomeForkyLightClientUpdateWithSyncCommittee:
|
|
|
|
update.next_sync_committee_branch !=
|
2023-09-06 14:05:12 +00:00
|
|
|
static(default(typeof(update.next_sync_committee_branch)))
|
2022-05-23 12:02:54 +00:00
|
|
|
else:
|
|
|
|
false
|
|
|
|
|
2023-12-05 02:34:45 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.5/specs/altair/light-client/sync-protocol.md#is_finality_update
|
2023-01-14 21:19:50 +00:00
|
|
|
template is_finality_update*(update: SomeForkyLightClientUpdate): bool =
|
|
|
|
when update is SomeForkyLightClientUpdateWithFinality:
|
2023-09-06 14:05:12 +00:00
|
|
|
update.finality_branch !=
|
|
|
|
static(default(typeof(update.finality_branch)))
|
2022-05-23 12:02:54 +00:00
|
|
|
else:
|
|
|
|
false
|
|
|
|
|
2024-06-16 00:59:25 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.3/specs/altair/light-client/sync-protocol.md#is_next_sync_committee_known
|
2023-01-14 21:19:50 +00:00
|
|
|
template is_next_sync_committee_known*(store: ForkyLightClientStore): bool =
|
2023-09-06 14:05:12 +00:00
|
|
|
store.next_sync_committee !=
|
|
|
|
static(default(typeof(store.next_sync_committee)))
|
2022-05-23 12:02:54 +00:00
|
|
|
|
2023-12-05 02:34:45 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.5/specs/altair/light-client/sync-protocol.md#get_safety_threshold
|
2023-01-14 21:19:50 +00:00
|
|
|
func get_safety_threshold*(store: ForkyLightClientStore): uint64 =
|
2022-05-23 12:02:54 +00:00
|
|
|
max(
|
|
|
|
store.previous_max_active_participants,
|
|
|
|
store.current_max_active_participants
|
|
|
|
) div 2
|
|
|
|
|
2023-12-05 02:34:45 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.5/specs/altair/light-client/sync-protocol.md#is_better_update
|
2022-05-23 12:02:54 +00:00
|
|
|
type LightClientUpdateMetadata* = object
|
|
|
|
attested_slot*, finalized_slot*, signature_slot*: Slot
|
|
|
|
has_sync_committee*, has_finality*: bool
|
|
|
|
num_active_participants*: uint64
|
|
|
|
|
2023-01-14 21:19:50 +00:00
|
|
|
func toMeta*(update: SomeForkyLightClientUpdate): LightClientUpdateMetadata =
|
2022-05-23 12:02:54 +00:00
|
|
|
var meta {.noinit.}: LightClientUpdateMetadata
|
|
|
|
meta.attested_slot =
|
2023-01-13 15:46:35 +00:00
|
|
|
update.attested_header.beacon.slot
|
2022-05-23 12:02:54 +00:00
|
|
|
meta.finalized_slot =
|
2023-01-14 21:19:50 +00:00
|
|
|
when update is SomeForkyLightClientUpdateWithFinality:
|
2023-01-13 15:46:35 +00:00
|
|
|
update.finalized_header.beacon.slot
|
2022-05-23 12:02:54 +00:00
|
|
|
else:
|
|
|
|
GENESIS_SLOT
|
|
|
|
meta.signature_slot =
|
|
|
|
update.signature_slot
|
|
|
|
meta.has_sync_committee =
|
2023-01-14 21:19:50 +00:00
|
|
|
when update is SomeForkyLightClientUpdateWithSyncCommittee:
|
|
|
|
update.is_sync_committee_update
|
2022-05-23 12:02:54 +00:00
|
|
|
else:
|
|
|
|
false
|
|
|
|
meta.has_finality =
|
2023-01-14 21:19:50 +00:00
|
|
|
when update is SomeForkyLightClientUpdateWithFinality:
|
|
|
|
update.is_finality_update
|
2022-05-23 12:02:54 +00:00
|
|
|
else:
|
|
|
|
false
|
|
|
|
meta.num_active_participants =
|
2023-01-10 11:26:25 +00:00
|
|
|
update.sync_aggregate.num_active_participants.uint64
|
2022-05-23 12:02:54 +00:00
|
|
|
meta
|
|
|
|
|
2024-01-03 22:36:05 +00:00
|
|
|
template toMeta*(
|
|
|
|
update: SomeForkedLightClientUpdate): LightClientUpdateMetadata =
|
|
|
|
withForkyObject(update):
|
2023-01-14 21:19:50 +00:00
|
|
|
when lcDataFork > LightClientDataFork.None:
|
2024-01-03 22:36:05 +00:00
|
|
|
forkyObject.toMeta()
|
2023-01-12 17:11:38 +00:00
|
|
|
else:
|
|
|
|
default(LightClientUpdateMetadata)
|
|
|
|
|
2022-05-23 12:02:54 +00:00
|
|
|
func is_better_data*(new_meta, old_meta: LightClientUpdateMetadata): bool =
|
|
|
|
# Compare supermajority (> 2/3) sync committee participation
|
|
|
|
let
|
|
|
|
new_has_supermajority =
|
2023-11-17 03:57:15 +00:00
|
|
|
hasSupermajoritySyncParticipation(new_meta.num_active_participants)
|
2022-05-23 12:02:54 +00:00
|
|
|
old_has_supermajority =
|
2023-11-17 03:57:15 +00:00
|
|
|
hasSupermajoritySyncParticipation(old_meta.num_active_participants)
|
2022-05-23 12:02:54 +00:00
|
|
|
if new_has_supermajority != old_has_supermajority:
|
|
|
|
return new_has_supermajority > old_has_supermajority
|
|
|
|
if not new_has_supermajority:
|
|
|
|
if new_meta.num_active_participants != old_meta.num_active_participants:
|
|
|
|
return new_meta.num_active_participants > old_meta.num_active_participants
|
|
|
|
|
|
|
|
# Compare presence of relevant sync committee
|
|
|
|
let
|
|
|
|
new_has_relevant_sync_committee = new_meta.has_sync_committee and
|
|
|
|
new_meta.attested_slot.sync_committee_period ==
|
|
|
|
new_meta.signature_slot.sync_committee_period
|
|
|
|
old_has_relevant_sync_committee = old_meta.has_sync_committee and
|
|
|
|
old_meta.attested_slot.sync_committee_period ==
|
|
|
|
old_meta.signature_slot.sync_committee_period
|
|
|
|
if new_has_relevant_sync_committee != old_has_relevant_sync_committee:
|
|
|
|
return new_has_relevant_sync_committee > old_has_relevant_sync_committee
|
|
|
|
|
|
|
|
# Compare indication of any finality
|
|
|
|
if new_meta.has_finality != old_meta.has_finality:
|
|
|
|
return new_meta.has_finality > old_meta.has_finality
|
|
|
|
|
|
|
|
# Compare sync committee finality
|
|
|
|
if new_meta.has_finality:
|
|
|
|
let
|
|
|
|
new_has_sync_committee_finality =
|
|
|
|
new_meta.finalized_slot.sync_committee_period ==
|
|
|
|
new_meta.attested_slot.sync_committee_period
|
|
|
|
old_has_sync_committee_finality =
|
|
|
|
old_meta.finalized_slot.sync_committee_period ==
|
|
|
|
old_meta.attested_slot.sync_committee_period
|
|
|
|
if new_has_sync_committee_finality != old_has_sync_committee_finality:
|
|
|
|
return new_has_sync_committee_finality > old_has_sync_committee_finality
|
|
|
|
|
|
|
|
# Tiebreaker 1: Sync committee participation beyond supermajority
|
|
|
|
if new_meta.num_active_participants != old_meta.num_active_participants:
|
|
|
|
return new_meta.num_active_participants > old_meta.num_active_participants
|
|
|
|
|
|
|
|
# Tiebreaker 2: Prefer older data (fewer changes to best data)
|
|
|
|
new_meta.attested_slot < old_meta.attested_slot
|
|
|
|
|
2023-01-12 17:11:38 +00:00
|
|
|
template is_better_update*[
|
2023-01-14 21:19:50 +00:00
|
|
|
A, B: SomeForkyLightClientUpdate | ForkedLightClientUpdate](
|
2022-05-23 12:02:54 +00:00
|
|
|
new_update: A, old_update: B): bool =
|
|
|
|
is_better_data(toMeta(new_update), toMeta(old_update))
|
|
|
|
|
2023-12-05 02:34:45 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.5/specs/altair/light-client/p2p-interface.md#getlightclientbootstrap
|
2023-01-14 21:19:50 +00:00
|
|
|
func contextEpoch*(bootstrap: ForkyLightClientBootstrap): Epoch =
|
2023-01-13 15:46:35 +00:00
|
|
|
bootstrap.header.beacon.slot.epoch
|
2022-10-04 11:38:09 +00:00
|
|
|
|
2023-12-05 02:34:45 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.5/specs/altair/light-client/p2p-interface.md#lightclientupdatesbyrange
|
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.5/specs/altair/light-client/p2p-interface.md#getlightclientfinalityupdate
|
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.5/specs/altair/light-client/p2p-interface.md#getlightclientoptimisticupdate
|
2023-01-14 21:19:50 +00:00
|
|
|
func contextEpoch*(update: SomeForkyLightClientUpdate): Epoch =
|
2023-01-13 15:46:35 +00:00
|
|
|
update.attested_header.beacon.slot.epoch
|
2022-10-04 11:38:09 +00:00
|
|
|
|
2024-06-16 00:59:25 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.3/specs/bellatrix/beacon-chain.md#is_merge_transition_complete
|
2022-10-27 06:29:24 +00:00
|
|
|
func is_merge_transition_complete*(
|
2024-02-26 02:38:21 +00:00
|
|
|
state: bellatrix.BeaconState | capella.BeaconState | deneb.BeaconState |
|
|
|
|
electra.BeaconState): bool =
|
2022-10-27 06:29:24 +00:00
|
|
|
const defaultExecutionPayloadHeader =
|
|
|
|
default(typeof(state.latest_execution_payload_header))
|
2022-02-16 07:16:01 +00:00
|
|
|
state.latest_execution_payload_header != defaultExecutionPayloadHeader
|
2021-09-27 14:22:58 +00:00
|
|
|
|
2024-06-16 00:59:25 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.3/sync/optimistic.md#helpers
|
2022-07-04 20:35:33 +00:00
|
|
|
func is_execution_block*(blck: SomeForkyBeaconBlock): bool =
|
2023-09-27 15:10:28 +00:00
|
|
|
when typeof(blck).kind >= ConsensusFork.Bellatrix:
|
2022-07-04 20:35:33 +00:00
|
|
|
const defaultExecutionPayload =
|
|
|
|
default(typeof(blck.body.execution_payload))
|
|
|
|
blck.body.execution_payload != defaultExecutionPayload
|
|
|
|
else:
|
|
|
|
false
|
2022-06-10 14:16:37 +00:00
|
|
|
|
2024-06-16 00:59:25 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.3/specs/bellatrix/beacon-chain.md#is_merge_transition_block
|
2021-12-17 06:56:33 +00:00
|
|
|
func is_merge_transition_block(
|
2024-02-26 02:38:21 +00:00
|
|
|
state: bellatrix.BeaconState | capella.BeaconState | deneb.BeaconState |
|
|
|
|
electra.BeaconState,
|
2022-01-06 11:25:35 +00:00
|
|
|
body: bellatrix.BeaconBlockBody | bellatrix.TrustedBeaconBlockBody |
|
2022-11-07 18:37:48 +00:00
|
|
|
bellatrix.SigVerifiedBeaconBlockBody |
|
|
|
|
capella.BeaconBlockBody | capella.TrustedBeaconBlockBody |
|
2022-12-08 02:07:41 +00:00
|
|
|
capella.SigVerifiedBeaconBlockBody |
|
2023-02-22 13:10:00 +00:00
|
|
|
deneb.BeaconBlockBody | deneb.TrustedBeaconBlockBody |
|
2024-02-26 02:38:21 +00:00
|
|
|
deneb.SigVerifiedBeaconBlockBody |
|
|
|
|
electra.BeaconBlockBody | electra.TrustedBeaconBlockBody |
|
|
|
|
electra.SigVerifiedBeaconBlockBody): bool =
|
2022-11-07 18:37:48 +00:00
|
|
|
const defaultExecutionPayload = default(typeof(body.execution_payload))
|
2021-12-14 21:02:29 +00:00
|
|
|
not is_merge_transition_complete(state) and
|
2022-11-07 18:37:48 +00:00
|
|
|
body.execution_payload != defaultExecutionPayload
|
2021-09-27 14:22:58 +00:00
|
|
|
|
2024-06-16 00:59:25 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.3/specs/bellatrix/beacon-chain.md#is_execution_enabled
|
2021-09-27 14:22:58 +00:00
|
|
|
func is_execution_enabled*(
|
2024-02-26 02:38:21 +00:00
|
|
|
state: bellatrix.BeaconState | capella.BeaconState | deneb.BeaconState |
|
|
|
|
electra.BeaconState,
|
2022-01-06 11:25:35 +00:00
|
|
|
body: bellatrix.BeaconBlockBody | bellatrix.TrustedBeaconBlockBody |
|
2022-11-07 18:37:48 +00:00
|
|
|
bellatrix.SigVerifiedBeaconBlockBody |
|
|
|
|
capella.BeaconBlockBody | capella.TrustedBeaconBlockBody |
|
2022-12-08 02:07:41 +00:00
|
|
|
capella.SigVerifiedBeaconBlockBody |
|
2023-02-22 13:10:00 +00:00
|
|
|
deneb.BeaconBlockBody | deneb.TrustedBeaconBlockBody |
|
2024-02-26 02:38:21 +00:00
|
|
|
deneb.SigVerifiedBeaconBlockBody |
|
|
|
|
electra.BeaconBlockBody | electra.TrustedBeaconBlockBody |
|
|
|
|
electra.SigVerifiedBeaconBlockBody): bool =
|
2021-12-17 06:56:33 +00:00
|
|
|
is_merge_transition_block(state, body) or is_merge_transition_complete(state)
|
2021-09-27 14:22:58 +00:00
|
|
|
|
2024-06-16 00:59:25 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.3/specs/bellatrix/beacon-chain.md#compute_timestamp_at_slot
|
2021-11-05 07:34:34 +00:00
|
|
|
func compute_timestamp_at_slot*(state: ForkyBeaconState, slot: Slot): uint64 =
|
2021-09-27 14:22:58 +00:00
|
|
|
# Note: This function is unsafe with respect to overflows and underflows.
|
|
|
|
let slots_since_genesis = slot - GENESIS_SLOT
|
|
|
|
state.genesis_time + slots_since_genesis * SECONDS_PER_SLOT
|
2022-04-14 20:15:34 +00:00
|
|
|
|
2022-12-20 08:24:33 +00:00
|
|
|
proc computeTransactionsTrieRoot*(
|
2023-08-07 12:23:44 +00:00
|
|
|
payload: ForkyExecutionPayload): ExecutionHash256 =
|
2022-12-20 08:24:33 +00:00
|
|
|
if payload.transactions.len == 0:
|
|
|
|
return EMPTY_ROOT_HASH
|
|
|
|
|
|
|
|
var tr = initHexaryTrie(newMemoryDB())
|
|
|
|
for i, transaction in payload.transactions:
|
|
|
|
try:
|
2024-07-03 01:43:16 +00:00
|
|
|
# Transactions are already RLP encoded
|
|
|
|
tr.put(rlp.encode(i.uint), distinctBase(transaction))
|
2022-12-20 08:24:33 +00:00
|
|
|
except RlpError as exc:
|
2024-07-03 01:43:16 +00:00
|
|
|
raiseAssert "HexaryTrie.put failed: " & $exc.msg
|
2022-12-20 08:24:33 +00:00
|
|
|
tr.rootHash()
|
|
|
|
|
2022-11-28 13:41:25 +00:00
|
|
|
func toExecutionWithdrawal*(
|
|
|
|
withdrawal: capella.Withdrawal): ExecutionWithdrawal =
|
|
|
|
ExecutionWithdrawal(
|
|
|
|
index: withdrawal.index,
|
2022-12-20 19:00:56 +00:00
|
|
|
validatorIndex: withdrawal.validator_index,
|
2022-11-28 13:41:25 +00:00
|
|
|
address: EthAddress withdrawal.address.data,
|
2024-03-19 13:22:07 +00:00
|
|
|
amount: distinctBase(withdrawal.amount))
|
2022-11-28 13:41:25 +00:00
|
|
|
|
|
|
|
# https://eips.ethereum.org/EIPS/eip-4895
|
|
|
|
proc computeWithdrawalsTrieRoot*(
|
2024-02-26 02:38:21 +00:00
|
|
|
payload: capella.ExecutionPayload | deneb.ExecutionPayload |
|
|
|
|
electra.ExecutionPayload): ExecutionHash256 =
|
2022-11-28 13:41:25 +00:00
|
|
|
if payload.withdrawals.len == 0:
|
|
|
|
return EMPTY_ROOT_HASH
|
|
|
|
|
|
|
|
var tr = initHexaryTrie(newMemoryDB())
|
|
|
|
for i, withdrawal in payload.withdrawals:
|
|
|
|
try:
|
2024-07-03 01:43:16 +00:00
|
|
|
tr.put(rlp.encode(i.uint), rlp.encode(toExecutionWithdrawal(withdrawal)))
|
2022-11-28 13:41:25 +00:00
|
|
|
except RlpError as exc:
|
2024-07-03 01:43:16 +00:00
|
|
|
raiseAssert "HexaryTrie.put failed: " & $exc.msg
|
2022-11-28 13:41:25 +00:00
|
|
|
tr.rootHash()
|
|
|
|
|
2024-07-05 08:18:50 +00:00
|
|
|
func toExecutionDepositRequest*(
|
|
|
|
request: electra.DepositRequest): ExecutionDepositRequest =
|
|
|
|
ExecutionDepositRequest(
|
|
|
|
pubkey: request.pubkey.blob,
|
|
|
|
withdrawalCredentials: request.withdrawal_credentials.data,
|
|
|
|
amount: distinctBase(request.amount),
|
|
|
|
signature: request.signature.blob,
|
|
|
|
index: request.index)
|
|
|
|
|
|
|
|
func toExecutionWithdrawalRequest*(
|
|
|
|
request: electra.WithdrawalRequest): ExecutionWithdrawalRequest =
|
|
|
|
ExecutionWithdrawalRequest(
|
|
|
|
sourceAddress: request.source_address.data,
|
|
|
|
validatorPubkey: request.validator_pubkey.blob,
|
|
|
|
amount: distinctBase(request.amount))
|
|
|
|
|
|
|
|
func toExecutionConsolidationRequest*(
|
|
|
|
request: electra.ConsolidationRequest): ExecutionConsolidationRequest =
|
|
|
|
ExecutionConsolidationRequest(
|
|
|
|
sourceAddress: request.source_address.data,
|
|
|
|
sourcePubkey: request.source_pubkey.blob,
|
|
|
|
targetPubkey: request.target_pubkey.blob)
|
|
|
|
|
|
|
|
# https://eips.ethereum.org/EIPS/eip-7685
|
|
|
|
proc computeRequestsTrieRoot*(
|
|
|
|
payload: electra.ExecutionPayload): ExecutionHash256 =
|
|
|
|
if payload.deposit_requests.len == 0 and
|
|
|
|
payload.withdrawal_requests.len == 0 and
|
|
|
|
payload.consolidation_requests.len == 0:
|
|
|
|
return EMPTY_ROOT_HASH
|
|
|
|
|
|
|
|
var
|
|
|
|
tr = initHexaryTrie(newMemoryDB())
|
|
|
|
i = 0'u64
|
|
|
|
|
|
|
|
static:
|
|
|
|
doAssert DEPOSIT_REQUEST_TYPE < WITHDRAWAL_REQUEST_TYPE
|
|
|
|
doAssert WITHDRAWAL_REQUEST_TYPE < CONSOLIDATION_REQUEST_TYPE
|
|
|
|
|
|
|
|
# EIP-6110
|
|
|
|
for request in payload.deposit_requests:
|
|
|
|
try:
|
|
|
|
tr.put(rlp.encode(i.uint), rlp.encode(
|
|
|
|
toExecutionDepositRequest(request)))
|
|
|
|
except RlpError as exc:
|
|
|
|
raiseAssert "HexaryTree.put failed: " & $exc.msg
|
|
|
|
inc i
|
|
|
|
|
|
|
|
# EIP-7002
|
|
|
|
for request in payload.withdrawal_requests:
|
|
|
|
try:
|
|
|
|
tr.put(rlp.encode(i.uint), rlp.encode(
|
|
|
|
toExecutionWithdrawalRequest(request)))
|
|
|
|
except RlpError as exc:
|
|
|
|
raiseAssert "HexaryTree.put failed: " & $exc.msg
|
|
|
|
inc i
|
|
|
|
|
|
|
|
# EIP-7251
|
|
|
|
for request in payload.consolidation_requests:
|
|
|
|
try:
|
|
|
|
tr.put(rlp.encode(i.uint), rlp.encode(
|
|
|
|
toExecutionConsolidationRequest(request)))
|
|
|
|
except RlpError as exc:
|
|
|
|
raiseAssert "HexaryTree.put failed: " & $exc.msg
|
|
|
|
inc i
|
|
|
|
|
|
|
|
tr.rootHash()
|
|
|
|
|
2023-08-19 08:38:17 +00:00
|
|
|
proc blockToBlockHeader*(blck: ForkyBeaconBlock): ExecutionBlockHeader =
|
|
|
|
template payload: auto = blck.body.execution_payload
|
|
|
|
|
2022-11-28 13:41:25 +00:00
|
|
|
static: # `GasInt` is signed. We only use it for hashing.
|
|
|
|
doAssert sizeof(GasInt) == sizeof(payload.gas_limit)
|
|
|
|
doAssert sizeof(GasInt) == sizeof(payload.gas_used)
|
|
|
|
|
|
|
|
let
|
2022-12-20 08:24:33 +00:00
|
|
|
txRoot = payload.computeTransactionsTrieRoot()
|
2022-11-28 13:41:25 +00:00
|
|
|
withdrawalsRoot =
|
2023-09-27 15:10:28 +00:00
|
|
|
when typeof(payload).kind >= ConsensusFork.Capella:
|
2024-06-12 01:46:39 +00:00
|
|
|
Opt.some payload.computeWithdrawalsTrieRoot()
|
2022-12-20 19:00:56 +00:00
|
|
|
else:
|
2024-06-12 01:46:39 +00:00
|
|
|
Opt.none(ExecutionHash256)
|
2023-08-02 22:07:57 +00:00
|
|
|
blobGasUsed =
|
2023-09-27 15:10:28 +00:00
|
|
|
when typeof(payload).kind >= ConsensusFork.Deneb:
|
2024-06-12 01:46:39 +00:00
|
|
|
Opt.some payload.blob_gas_used
|
2023-06-03 21:55:08 +00:00
|
|
|
else:
|
2024-06-12 01:46:39 +00:00
|
|
|
Opt.none(uint64)
|
2023-08-02 22:07:57 +00:00
|
|
|
excessBlobGas =
|
2023-09-27 15:10:28 +00:00
|
|
|
when typeof(payload).kind >= ConsensusFork.Deneb:
|
2024-06-12 01:46:39 +00:00
|
|
|
Opt.some payload.excess_blob_gas
|
2022-11-28 13:41:25 +00:00
|
|
|
else:
|
2024-06-12 01:46:39 +00:00
|
|
|
Opt.none(uint64)
|
2023-08-19 08:38:17 +00:00
|
|
|
parentBeaconBlockRoot =
|
2023-09-27 15:10:28 +00:00
|
|
|
when typeof(payload).kind >= ConsensusFork.Deneb:
|
2024-06-12 01:46:39 +00:00
|
|
|
Opt.some ExecutionHash256(data: blck.parent_root.data)
|
2023-08-19 08:38:17 +00:00
|
|
|
else:
|
2024-06-12 01:46:39 +00:00
|
|
|
Opt.none(ExecutionHash256)
|
2024-07-05 08:18:50 +00:00
|
|
|
requestsRoot =
|
|
|
|
when typeof(payload).kind >= ConsensusFork.Electra:
|
|
|
|
Opt.some payload.computeRequestsTrieRoot()
|
|
|
|
else:
|
|
|
|
Opt.none(ExecutionHash256)
|
2022-11-28 13:41:25 +00:00
|
|
|
|
2022-08-19 10:28:42 +00:00
|
|
|
ExecutionBlockHeader(
|
2023-08-19 08:38:17 +00:00
|
|
|
parentHash : payload.parent_hash,
|
|
|
|
ommersHash : EMPTY_UNCLE_HASH,
|
|
|
|
coinbase : EthAddress payload.fee_recipient.data,
|
|
|
|
stateRoot : payload.state_root,
|
|
|
|
txRoot : txRoot,
|
2024-06-12 01:46:39 +00:00
|
|
|
receiptsRoot : payload.receipts_root,
|
|
|
|
logsBloom : payload.logs_bloom.data,
|
2023-08-19 08:38:17 +00:00
|
|
|
difficulty : default(DifficultyInt),
|
2024-06-12 01:46:39 +00:00
|
|
|
number : payload.block_number,
|
2024-07-06 20:25:31 +00:00
|
|
|
gasLimit : payload.gas_limit,
|
|
|
|
gasUsed : payload.gas_used,
|
2024-06-13 13:25:59 +00:00
|
|
|
timestamp : EthTime(payload.timestamp),
|
2023-08-19 08:38:17 +00:00
|
|
|
extraData : payload.extra_data.asSeq,
|
2024-06-12 01:46:39 +00:00
|
|
|
mixHash : payload.prev_randao, # EIP-4399 `mixHash` -> `prevRandao`
|
2023-08-19 08:38:17 +00:00
|
|
|
nonce : default(BlockNonce),
|
2024-06-12 01:46:39 +00:00
|
|
|
baseFeePerGas : Opt.some payload.base_fee_per_gas,
|
2023-08-19 08:38:17 +00:00
|
|
|
withdrawalsRoot : withdrawalsRoot,
|
|
|
|
blobGasUsed : blobGasUsed, # EIP-4844
|
|
|
|
excessBlobGas : excessBlobGas, # EIP-4844
|
2024-07-05 08:18:50 +00:00
|
|
|
parentBeaconBlockRoot : parentBeaconBlockRoot, # EIP-4788
|
|
|
|
requestsRoot : requestsRoot) # EIP-7685
|
2023-08-19 08:38:17 +00:00
|
|
|
|
|
|
|
proc compute_execution_block_hash*(blck: ForkyBeaconBlock): Eth2Digest =
|
|
|
|
rlpHash blockToBlockHeader(blck)
|
2024-07-25 01:59:24 +00:00
|
|
|
|
|
|
|
from std/math import exp, ln
|
|
|
|
from std/sequtils import foldl
|
|
|
|
|
|
|
|
func ln_binomial(n, k: int): float64 =
|
|
|
|
if k > n:
|
|
|
|
low(float64)
|
|
|
|
else:
|
|
|
|
template ln_factorial(n: int): float64 =
|
|
|
|
(2 .. n).foldl(a + ln(b.float64), 0.0)
|
|
|
|
ln_factorial(n) - ln_factorial(k) - ln_factorial(n - k)
|
|
|
|
|
|
|
|
func hypergeom_cdf*(k: int, population: int, successes: int, draws: int):
|
|
|
|
float64 =
|
|
|
|
if k < draws + successes - population:
|
|
|
|
0.0
|
|
|
|
elif k >= min(successes, draws):
|
|
|
|
1.0
|
|
|
|
else:
|
|
|
|
let ln_denom = ln_binomial(population, draws)
|
|
|
|
(0 .. k).foldl(a + exp(
|
|
|
|
ln_binomial(successes, b) +
|
|
|
|
ln_binomial(population - successes, draws - b) - ln_denom), 0.0)
|