2022-01-22 09:25:30 +00:00
|
|
|
# beacon_chain
|
2024-01-06 14:26:56 +00:00
|
|
|
# Copyright (c) 2022-2024 Status Research & Development GmbH
|
2022-01-22 09:25:30 +00:00
|
|
|
# Licensed and distributed under either of
|
|
|
|
# * 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).
|
|
|
|
# at your option. This file may not be copied, modified, or distributed except according to those terms.
|
|
|
|
|
2024-02-29 13:24:08 +00:00
|
|
|
{.push raises: [].}
|
|
|
|
|
2022-01-22 09:25:30 +00:00
|
|
|
# `wss_sim` loads a state and a set of validator keys, then simulates a
|
|
|
|
# beacon chain running with the given validators producing blocks
|
|
|
|
# and attesting when they're supposed to.
|
|
|
|
|
|
|
|
import
|
2023-03-31 20:46:47 +00:00
|
|
|
std/[strformat, sequtils, tables],
|
2022-01-22 09:25:30 +00:00
|
|
|
chronicles,
|
|
|
|
confutils,
|
|
|
|
stew/io2,
|
|
|
|
../tests/testblockutil,
|
2024-04-08 13:28:46 +00:00
|
|
|
../beacon_chain/el/el_manager,
|
2022-01-22 09:25:30 +00:00
|
|
|
../beacon_chain/networking/network_metadata,
|
|
|
|
../beacon_chain/[beacon_clock, sszdump],
|
|
|
|
../beacon_chain/spec/eth2_apis/eth2_rest_serialization,
|
|
|
|
../beacon_chain/spec/datatypes/[phase0, altair, bellatrix],
|
|
|
|
../beacon_chain/spec/[
|
2024-04-08 13:28:46 +00:00
|
|
|
beaconstate, crypto, engine_authentication, forks, helpers,
|
|
|
|
signatures, state_transition],
|
2022-01-22 09:25:30 +00:00
|
|
|
../beacon_chain/validators/[keystore_management, validator_pool]
|
|
|
|
|
2024-04-08 13:28:46 +00:00
|
|
|
from ../beacon_chain/gossip_processing/block_processor import
|
|
|
|
newExecutionPayload
|
|
|
|
|
2022-01-22 09:25:30 +00:00
|
|
|
template findIt*(s: openArray, predicate: untyped): int =
|
|
|
|
var res = -1
|
|
|
|
for i, it {.inject.} in s:
|
|
|
|
if predicate:
|
|
|
|
res = i
|
|
|
|
break
|
|
|
|
res
|
|
|
|
|
2024-03-01 04:59:34 +00:00
|
|
|
func findValidator(validators: seq[Validator], pubkey: ValidatorPubKey):
|
2022-08-19 21:51:30 +00:00
|
|
|
Opt[ValidatorIndex] =
|
2024-03-01 04:59:34 +00:00
|
|
|
let idx = validators.findIt(it.pubkey == pubkey)
|
2022-01-22 09:25:30 +00:00
|
|
|
if idx == -1:
|
2022-08-19 21:51:30 +00:00
|
|
|
Opt.none ValidatorIndex
|
2022-01-22 09:25:30 +00:00
|
|
|
else:
|
2022-08-19 21:51:30 +00:00
|
|
|
Opt.some idx.ValidatorIndex
|
2022-01-22 09:25:30 +00:00
|
|
|
|
2022-11-11 14:37:43 +00:00
|
|
|
from ../beacon_chain/spec/datatypes/capella import SignedBeaconBlock
|
2023-03-09 00:34:17 +00:00
|
|
|
from ../beacon_chain/spec/datatypes/deneb import SignedBeaconBlock
|
2022-11-11 14:37:43 +00:00
|
|
|
|
2022-01-22 09:25:30 +00:00
|
|
|
cli do(validatorsDir: string, secretsDir: string,
|
2024-04-08 13:28:46 +00:00
|
|
|
startState: string, startBlock: string,
|
|
|
|
network: string, elUrl: string, jwtSecret: string,
|
|
|
|
suggestedFeeRecipient: string, graffiti = "insecura"):
|
2022-01-22 09:25:30 +00:00
|
|
|
let
|
2024-04-08 13:28:46 +00:00
|
|
|
metadata = getMetadataForNetwork(network)
|
|
|
|
cfg = metadata.cfg
|
|
|
|
state = block:
|
|
|
|
let data = readAllBytes(startState)
|
|
|
|
if data.isErr:
|
|
|
|
fatal "failed to read hashed beacon state", err = $data.error
|
|
|
|
quit QuitFailure
|
|
|
|
try:
|
|
|
|
newClone(readSszForkedHashedBeaconState(cfg, data.get))
|
|
|
|
except SerializationError as exc:
|
|
|
|
fatal "failed to parse hashed beacon state", err = exc.msg
|
|
|
|
quit QuitFailure
|
|
|
|
blck = block:
|
|
|
|
let data = readAllBytes(startBlock)
|
|
|
|
if data.isErr:
|
|
|
|
fatal "failed to read signed beacon block", err = $data.error
|
|
|
|
quit QuitFailure
|
|
|
|
try:
|
|
|
|
newClone(readSszForkedSignedBeaconBlock(cfg, data.get))
|
|
|
|
except SerializationError as exc:
|
|
|
|
fatal "failed to parse signed beacon block", err = exc.msg
|
|
|
|
quit QuitFailure
|
|
|
|
engineApiUrl = block:
|
|
|
|
let
|
|
|
|
jwtSecretFile =
|
|
|
|
try:
|
|
|
|
InputFile.parseCmdArg(jwtSecret)
|
|
|
|
except ValueError as exc:
|
|
|
|
fatal "failed to read JWT secret file", err = exc.msg
|
|
|
|
quit QuitFailure
|
|
|
|
jwtSecret = loadJwtSecretFile(jwtSecretFile)
|
|
|
|
if jwtSecret.isErr:
|
|
|
|
fatal "failed to parse JWT secret file", err = jwtSecret.error
|
|
|
|
quit QuitFailure
|
|
|
|
let finalUrl = EngineApiUrlConfigValue(url: elUrl)
|
|
|
|
.toFinalUrl(Opt.some jwtSecret.get)
|
|
|
|
if finalUrl.isErr:
|
|
|
|
fatal "failed to read EL URL", err = finalUrl.error
|
|
|
|
quit QuitFailure
|
|
|
|
finalUrl.get
|
|
|
|
elManager = ELManager.new(
|
|
|
|
cfg,
|
|
|
|
metadata.depositContractBlock,
|
|
|
|
metadata.depositContractBlockHash,
|
|
|
|
db = nil,
|
|
|
|
@[engineApiUrl],
|
|
|
|
metadata.eth1Network)
|
|
|
|
feeRecipient =
|
2024-02-24 05:08:22 +00:00
|
|
|
try:
|
2024-04-08 13:28:46 +00:00
|
|
|
Address.fromHex(suggestedFeeRecipient)
|
|
|
|
except ValueError as exc:
|
|
|
|
fatal "failed to parse suggested fee recipient", err = exc.msg
|
|
|
|
quit QuitFailure
|
|
|
|
graffitiValue =
|
|
|
|
try:
|
|
|
|
GraffitiBytes.init(graffiti)
|
|
|
|
except ValueError as exc:
|
|
|
|
fatal "failed to parse graffiti", err = exc.msg
|
|
|
|
quit QuitFailure
|
|
|
|
|
|
|
|
# Sync EL to initial state. Note that to construct the new branch, the EL
|
|
|
|
# should not have advanced to a later block via `engine_forkchoiceUpdated`.
|
|
|
|
# The EL may otherwise refuse to produce new heads
|
|
|
|
elManager.start(syncChain = false)
|
|
|
|
withBlck(blck[]):
|
|
|
|
when consensusFork >= ConsensusFork.Bellatrix:
|
|
|
|
if forkyBlck.message.is_execution_block:
|
|
|
|
template payload(): auto = forkyBlck.message.body.execution_payload
|
|
|
|
if not payload.block_hash.isZero:
|
|
|
|
notice "Syncing EL", elUrl, jwtSecret
|
|
|
|
while true:
|
|
|
|
waitFor noCancel sleepAsync(chronos.seconds(2))
|
|
|
|
(waitFor noCancel elManager
|
|
|
|
.newExecutionPayload(forkyBlck.message)).isOkOr:
|
|
|
|
continue
|
|
|
|
|
|
|
|
let (status, _) = waitFor noCancel elManager.forkchoiceUpdated(
|
|
|
|
headBlockHash = payload.block_hash,
|
|
|
|
safeBlockHash = payload.block_hash,
|
|
|
|
finalizedBlockHash = ZERO_HASH,
|
2024-06-12 01:46:39 +00:00
|
|
|
payloadAttributes = Opt.none(consensusFork.PayloadAttributes))
|
2024-04-08 13:28:46 +00:00
|
|
|
if status != PayloadExecutionStatus.valid:
|
|
|
|
continue
|
|
|
|
|
|
|
|
notice "EL synced", elUrl, jwtSecret
|
|
|
|
break
|
2022-01-22 09:25:30 +00:00
|
|
|
|
|
|
|
var
|
2024-01-06 14:26:56 +00:00
|
|
|
clock = BeaconClock.init(getStateField(state[], genesis_time)).valueOr:
|
|
|
|
error "Invalid genesis time in state"
|
|
|
|
quit 1
|
2022-01-22 09:25:30 +00:00
|
|
|
validators: Table[ValidatorIndex, ValidatorPrivKey]
|
|
|
|
validatorKeys: Table[ValidatorPubKey, ValidatorPrivKey]
|
|
|
|
|
2022-02-07 20:36:09 +00:00
|
|
|
for item in listLoadableKeystores(validatorsDir, secretsDir, true,
|
2023-02-16 17:25:48 +00:00
|
|
|
{KeystoreKind.Local}, nil):
|
2022-01-22 09:25:30 +00:00
|
|
|
let
|
|
|
|
pubkey = item.privateKey.toPubKey().toPubKey()
|
|
|
|
idx = findValidator(getStateField(state[], validators).toSeq, pubkey)
|
|
|
|
if idx.isSome():
|
|
|
|
notice "Loaded validator", pubkey
|
|
|
|
validators[idx.get()] = item.privateKey
|
|
|
|
validatorKeys[pubkey] = item.privateKey
|
|
|
|
else:
|
2024-04-08 13:28:46 +00:00
|
|
|
warn "Unknown validator", pubkey
|
2022-01-22 09:25:30 +00:00
|
|
|
|
|
|
|
var
|
2022-09-16 13:35:00 +00:00
|
|
|
blockRoot = withState(state[]): forkyState.latest_block_root
|
2022-01-22 09:25:30 +00:00
|
|
|
cache: StateCache
|
|
|
|
info: ForkedEpochInfo
|
2024-04-17 20:44:29 +00:00
|
|
|
aggregates: seq[phase0.Attestation]
|
2022-01-22 09:25:30 +00:00
|
|
|
syncAggregate = SyncAggregate.init()
|
|
|
|
|
|
|
|
let
|
|
|
|
genesis_validators_root = getStateField(state[], genesis_validators_root)
|
|
|
|
|
|
|
|
block:
|
|
|
|
let
|
|
|
|
active = withState(state[]):
|
2022-09-16 13:35:00 +00:00
|
|
|
get_active_validator_indices_len(
|
|
|
|
forkyState.data, forkyState.data.slot.epoch)
|
2022-01-22 09:25:30 +00:00
|
|
|
|
|
|
|
notice "Let's play",
|
|
|
|
validators = validators.len(),
|
|
|
|
active
|
|
|
|
|
|
|
|
while true:
|
|
|
|
# Move to slot
|
|
|
|
let
|
|
|
|
slot = getStateField(state[], slot) + 1
|
|
|
|
process_slots(cfg, state[], slot, cache, info, {}).expect("works")
|
|
|
|
|
|
|
|
if start_beacon_time(slot) > clock.now():
|
|
|
|
notice "Ran out of time",
|
|
|
|
epoch = slot.epoch
|
|
|
|
break
|
|
|
|
|
|
|
|
var exited: seq[ValidatorIndex]
|
|
|
|
for k, v in validators:
|
|
|
|
if getStateField(state[], validators).asSeq[k].exit_epoch != FAR_FUTURE_EPOCH:
|
|
|
|
exited.add k
|
|
|
|
for k in exited:
|
|
|
|
warn "Validator exited", k
|
|
|
|
|
|
|
|
validatorKeys.del(getStateField(state[], validators).asSeq[k].pubkey)
|
|
|
|
validators.del(k)
|
|
|
|
|
|
|
|
if slot.epoch != (slot - 1).epoch:
|
|
|
|
let
|
|
|
|
active = withState(state[]):
|
2022-09-16 13:35:00 +00:00
|
|
|
get_active_validator_indices_len(forkyState.data, slot.epoch)
|
2022-01-22 09:25:30 +00:00
|
|
|
balance = block:
|
2024-03-19 13:22:07 +00:00
|
|
|
var b: Gwei
|
2022-01-22 09:25:30 +00:00
|
|
|
for k, _ in validators:
|
|
|
|
if is_active_validator(getStateField(state[], validators).asSeq[k], slot.epoch):
|
|
|
|
b += getStateField(state[], balances).asSeq[k]
|
|
|
|
b
|
|
|
|
validators = block:
|
|
|
|
var b: int
|
|
|
|
for k, _ in validators:
|
|
|
|
if is_active_validator(getStateField(state[], validators).asSeq[k], slot.epoch):
|
|
|
|
b += 1
|
|
|
|
b
|
|
|
|
avgBalance = balance.int64 div validators.int64
|
|
|
|
|
|
|
|
notice "Processing epoch",
|
|
|
|
epoch = slot.epoch,
|
|
|
|
active,
|
|
|
|
epochsSinceFinality =
|
|
|
|
slot.epoch - getStateField(state[], finalized_checkpoint).epoch,
|
|
|
|
balance,
|
|
|
|
validators,
|
|
|
|
avgBalance
|
|
|
|
|
|
|
|
if slot.epoch mod 32 == 0:
|
2022-09-16 13:35:00 +00:00
|
|
|
withState(state[]): dump(".", forkyState)
|
2022-01-22 09:25:30 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
fork = getStateField(state[], fork)
|
|
|
|
proposer = get_beacon_proposer_index(state[], cache, slot).get()
|
|
|
|
|
|
|
|
if proposer in validators:
|
|
|
|
let
|
|
|
|
blockAggregates = aggregates.filterIt(
|
|
|
|
it.data.slot + MIN_ATTESTATION_INCLUSION_DELAY <= slot and
|
|
|
|
slot <= it.data.slot + SLOTS_PER_EPOCH)
|
2022-06-29 16:53:59 +00:00
|
|
|
randao_reveal = get_epoch_signature(
|
|
|
|
fork, genesis_validators_root, slot.epoch,
|
2024-02-24 05:08:22 +00:00
|
|
|
# should never fall back to default value
|
|
|
|
validators.getOrDefault(
|
|
|
|
proposer, default(ValidatorPrivKey))).toValidatorSig()
|
2024-04-08 13:28:46 +00:00
|
|
|
withState(state[]):
|
|
|
|
let
|
|
|
|
payload =
|
|
|
|
when consensusFork >= ConsensusFork.Bellatrix:
|
|
|
|
let
|
|
|
|
executionHead =
|
|
|
|
forkyState.data.latest_execution_payload_header.block_hash
|
|
|
|
withdrawals =
|
|
|
|
when consensusFork >= ConsensusFork.Capella:
|
|
|
|
get_expected_withdrawals(forkyState.data)
|
|
|
|
else:
|
|
|
|
newSeq[Withdrawal]()
|
|
|
|
|
|
|
|
var pl: consensusFork.ExecutionPayloadForSigning
|
|
|
|
while true:
|
|
|
|
pl = (waitFor noCancel elManager.getPayload(
|
|
|
|
consensusFork.ExecutionPayloadForSigning,
|
|
|
|
consensusHead = forkyState.latest_block_root,
|
|
|
|
headBlock = executionHead,
|
|
|
|
safeBlock = executionHead,
|
|
|
|
finalizedBlock = ZERO_HASH,
|
|
|
|
timestamp = compute_timestamp_at_slot(
|
|
|
|
forkyState.data, forkyState.data.slot),
|
|
|
|
randomData = get_randao_mix(
|
|
|
|
forkyState.data, get_current_epoch(forkyState.data)),
|
|
|
|
suggestedFeeRecipient = feeRecipient,
|
|
|
|
withdrawals = withdrawals)).valueOr:
|
|
|
|
waitFor noCancel sleepAsync(chronos.seconds(2))
|
|
|
|
continue
|
|
|
|
break
|
|
|
|
pl
|
|
|
|
else:
|
|
|
|
default(bellatrix.ExecutionPayloadForSigning)
|
|
|
|
message = makeBeaconBlock(
|
|
|
|
cfg,
|
|
|
|
state[],
|
|
|
|
proposer,
|
|
|
|
randao_reveal,
|
|
|
|
forkyState.data.eth1_data,
|
|
|
|
graffitiValue,
|
2024-04-22 09:00:38 +00:00
|
|
|
when typeof(payload).kind == ConsensusFork.Electra:
|
2024-09-13 08:07:06 +00:00
|
|
|
default(seq[electra.Attestation])
|
2024-04-22 09:00:38 +00:00
|
|
|
else:
|
|
|
|
blockAggregates,
|
2024-04-08 13:28:46 +00:00
|
|
|
@[],
|
|
|
|
BeaconBlockValidatorChanges(),
|
|
|
|
syncAggregate,
|
|
|
|
payload,
|
|
|
|
noRollback,
|
|
|
|
cache).get()
|
|
|
|
|
|
|
|
blockRoot = message.forky(consensusFork).hash_tree_root()
|
|
|
|
let
|
|
|
|
proposerPrivkey =
|
|
|
|
try:
|
|
|
|
validators[proposer]
|
|
|
|
except KeyError as exc:
|
|
|
|
raiseAssert "Proposer key not available: " & exc.msg
|
|
|
|
signedBlock = consensusFork.SignedBeaconBlock(
|
|
|
|
message: message.forky(consensusFork),
|
2024-04-04 03:17:31 +00:00
|
|
|
root: blockRoot,
|
|
|
|
signature: get_block_signature(
|
|
|
|
fork, genesis_validators_root, slot, blockRoot,
|
2024-04-08 13:28:46 +00:00
|
|
|
proposerPrivkey).toValidatorSig())
|
|
|
|
|
|
|
|
dump(".", signedBlock)
|
|
|
|
when consensusFork >= ConsensusFork.Deneb:
|
|
|
|
let blobs = signedBlock.create_blob_sidecars(
|
|
|
|
payload.blobsBundle.proofs, payload.blobsBundle.blobs)
|
|
|
|
for blob in blobs:
|
|
|
|
dump(".", blob)
|
|
|
|
|
|
|
|
notice "Block proposed", message, blockRoot
|
|
|
|
|
|
|
|
when consensusFork >= ConsensusFork.Bellatrix:
|
|
|
|
while true:
|
|
|
|
let status = waitFor noCancel elManager
|
|
|
|
.newExecutionPayload(signedBlock.message)
|
|
|
|
if status.isNone:
|
|
|
|
waitFor noCancel sleepAsync(chronos.seconds(2))
|
|
|
|
continue
|
|
|
|
doAssert status.get in [
|
|
|
|
PayloadExecutionStatus.valid,
|
|
|
|
PayloadExecutionStatus.accepted,
|
|
|
|
PayloadExecutionStatus.syncing]
|
|
|
|
break
|
2022-01-22 09:25:30 +00:00
|
|
|
|
|
|
|
aggregates.setLen(0)
|
|
|
|
|
|
|
|
syncAggregate = SyncAggregate.init()
|
|
|
|
|
|
|
|
withState(state[]):
|
|
|
|
let committees_per_slot = get_committee_count_per_slot(
|
2022-09-16 13:35:00 +00:00
|
|
|
forkyState.data, slot.epoch, cache)
|
2022-01-22 09:25:30 +00:00
|
|
|
for committee_index in get_committee_indices(committees_per_slot):
|
2022-09-16 13:35:00 +00:00
|
|
|
let committee = get_beacon_committee(
|
|
|
|
forkyState.data, slot, committee_index, cache)
|
2022-01-22 09:25:30 +00:00
|
|
|
|
|
|
|
var
|
2024-04-17 20:44:29 +00:00
|
|
|
attestation = phase0.Attestation(
|
2022-01-22 09:25:30 +00:00
|
|
|
data: makeAttestationData(
|
2022-09-16 13:35:00 +00:00
|
|
|
forkyState.data, slot, committee_index, blockRoot),
|
2022-01-22 09:25:30 +00:00
|
|
|
aggregation_bits: CommitteeValidatorsBits.init(committee.len))
|
|
|
|
agg: AggregateSignature
|
|
|
|
|
|
|
|
for index_in_committee, validator_index in committee:
|
|
|
|
if validator_index notin validators:
|
|
|
|
continue
|
|
|
|
|
|
|
|
let
|
|
|
|
signature = get_attestation_signature(
|
|
|
|
fork, genesis_validators_root, attestation.data,
|
2024-02-24 05:08:22 +00:00
|
|
|
validators.getOrDefault(validator_index, default(ValidatorPrivKey)))
|
2023-05-17 04:55:55 +00:00
|
|
|
if attestation.aggregation_bits.isZeros:
|
2022-01-22 09:25:30 +00:00
|
|
|
agg = AggregateSignature.init(signature)
|
|
|
|
else:
|
|
|
|
agg.aggregate(signature)
|
|
|
|
attestation.aggregation_bits.setBit(index_in_committee)
|
|
|
|
|
2023-05-17 04:55:55 +00:00
|
|
|
if not attestation.aggregation_bits.isZeros:
|
2022-01-22 09:25:30 +00:00
|
|
|
attestation.signature = agg.finish().toValidatorSig()
|
|
|
|
|
|
|
|
if aggregates.len == 128:
|
|
|
|
aggregates.delete(0)
|
|
|
|
|
|
|
|
aggregates.add(attestation)
|
|
|
|
|
2023-03-11 00:35:52 +00:00
|
|
|
when consensusFork >= ConsensusFork.Altair:
|
2022-01-22 09:25:30 +00:00
|
|
|
let
|
|
|
|
nextSlot = slot + 1
|
|
|
|
pubkeys =
|
|
|
|
if slot.sync_committee_period == nextSlot.sync_committee_period:
|
2022-09-16 13:35:00 +00:00
|
|
|
forkyState.data.current_sync_committee.pubkeys
|
2022-01-22 09:25:30 +00:00
|
|
|
else:
|
2022-09-16 13:35:00 +00:00
|
|
|
forkyState.data.next_sync_committee.pubkeys
|
2022-01-22 09:25:30 +00:00
|
|
|
|
|
|
|
var
|
|
|
|
agg: AggregateSignature
|
|
|
|
inited = false
|
|
|
|
|
2022-05-10 10:03:40 +00:00
|
|
|
for i, pubkey in pubkeys:
|
2022-01-22 09:25:30 +00:00
|
|
|
validatorKeys.withValue(pubkey, privkey):
|
|
|
|
let sig = get_sync_committee_message_signature(
|
|
|
|
fork, genesis_validators_root, slot, blockRoot, privkey[])
|
|
|
|
|
|
|
|
if inited:
|
|
|
|
agg.aggregate(sig)
|
|
|
|
else:
|
|
|
|
agg = AggregateSignature.init(sig)
|
|
|
|
inited = true
|
2022-04-15 12:46:56 +00:00
|
|
|
syncAggregate.sync_committee_bits.setBit(i)
|
2022-01-22 09:25:30 +00:00
|
|
|
|
|
|
|
if inited:
|
2024-04-17 20:44:29 +00:00
|
|
|
syncAggregate.sync_committee_signature = finish(agg).toValidatorSig()
|