2021-03-26 06:52:01 +00:00
|
|
|
# beacon_chain
|
2022-01-12 14:50:30 +00:00
|
|
|
# Copyright (c) 2018-2022 Status Research & Development GmbH
|
2021-03-26 06:52:01 +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.
|
|
|
|
|
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
|
|
|
|
2018-11-26 13:33:06 +00:00
|
|
|
import
|
2021-12-03 15:04:58 +00:00
|
|
|
std/[deques, options, strformat, strutils, sequtils, tables,
|
2021-09-29 19:26:27 +00:00
|
|
|
typetraits, uri, json],
|
2020-12-09 22:44:59 +00:00
|
|
|
# Nimble packages:
|
2022-06-09 14:30:13 +00:00
|
|
|
chronos, metrics, chronicles/timings, stint/endians2,
|
2021-10-14 11:29:02 +00:00
|
|
|
web3, web3/ethtypes as web3Types, web3/ethhexstrings, web3/engine_api,
|
2021-09-29 19:26:27 +00:00
|
|
|
eth/common/eth_types,
|
2022-03-25 11:40:10 +00:00
|
|
|
eth/async_utils, stew/[byteutils, objects, shims/hashes],
|
2020-12-09 22:44:59 +00:00
|
|
|
# Local modules:
|
2021-08-18 18:57:58 +00:00
|
|
|
../spec/[eth2_merkleization, forks, helpers],
|
2022-01-12 14:50:30 +00:00
|
|
|
../spec/datatypes/[base, phase0, bellatrix],
|
2021-03-05 13:12:00 +00:00
|
|
|
../networking/network_metadata,
|
2021-05-21 09:23:28 +00:00
|
|
|
../consensus_object_pools/block_pools_types,
|
2022-03-25 11:40:10 +00:00
|
|
|
".."/[beacon_chain_db, beacon_node_status, beacon_clock],
|
2021-03-03 06:23:05 +00:00
|
|
|
./merkle_minimal
|
2018-11-26 13:33:06 +00:00
|
|
|
|
2022-03-31 14:43:05 +00:00
|
|
|
from std/times import getTime, inSeconds, initTime, `-`
|
|
|
|
from ../spec/engine_authentication import getSignedIatToken
|
|
|
|
|
2020-06-19 17:42:28 +00:00
|
|
|
export
|
2022-08-12 13:52:06 +00:00
|
|
|
web3Types, deques, base,
|
2022-07-13 01:45:48 +00:00
|
|
|
beacon_chain_db.DepositContractSnapshot
|
2020-06-19 17:42:28 +00:00
|
|
|
|
2020-11-19 17:19:03 +00:00
|
|
|
logScope:
|
|
|
|
topics = "eth1"
|
|
|
|
|
2021-10-14 12:33:55 +00:00
|
|
|
type
|
|
|
|
PubKeyBytes = DynamicBytes[48, 48]
|
|
|
|
WithdrawalCredentialsBytes = DynamicBytes[32, 32]
|
|
|
|
SignatureBytes = DynamicBytes[96, 96]
|
|
|
|
Int64LeBytes = DynamicBytes[8, 8]
|
|
|
|
|
2020-03-24 11:13:07 +00:00
|
|
|
contract(DepositContract):
|
2021-10-14 12:33:55 +00:00
|
|
|
proc deposit(pubkey: PubKeyBytes,
|
|
|
|
withdrawalCredentials: WithdrawalCredentialsBytes,
|
|
|
|
signature: SignatureBytes,
|
2020-03-24 11:13:07 +00:00
|
|
|
deposit_data_root: FixedBytes[32])
|
|
|
|
|
|
|
|
proc get_deposit_root(): FixedBytes[32]
|
2021-10-14 12:33:55 +00:00
|
|
|
proc get_deposit_count(): Int64LeBytes
|
2020-03-24 11:13:07 +00:00
|
|
|
|
2021-10-14 12:33:55 +00:00
|
|
|
proc DepositEvent(pubkey: PubKeyBytes,
|
|
|
|
withdrawalCredentials: WithdrawalCredentialsBytes,
|
|
|
|
amount: Int64LeBytes,
|
|
|
|
signature: SignatureBytes,
|
|
|
|
index: Int64LeBytes) {.event.}
|
2020-03-24 11:13:07 +00:00
|
|
|
|
2020-11-24 21:21:47 +00:00
|
|
|
const
|
2020-11-24 21:28:20 +00:00
|
|
|
web3Timeouts = 60.seconds
|
2020-12-15 23:09:19 +00:00
|
|
|
hasDepositRootChecks = defined(has_deposit_root_checks)
|
|
|
|
hasGenesisDetection* = defined(has_genesis_detection)
|
2020-11-24 21:21:47 +00:00
|
|
|
|
2022-08-12 13:51:33 +00:00
|
|
|
targetBlocksPerLogsRequest = 5000'u64 # This is roughly a day of Eth1 blocks
|
|
|
|
|
2018-11-26 13:33:06 +00:00
|
|
|
type
|
2020-03-24 11:13:07 +00:00
|
|
|
Eth1BlockNumber* = uint64
|
|
|
|
Eth1BlockTimestamp* = uint64
|
2020-07-02 15:14:11 +00:00
|
|
|
Eth1BlockHeader = web3Types.BlockHeader
|
2020-03-24 11:13:07 +00:00
|
|
|
|
2021-12-23 14:58:54 +00:00
|
|
|
GenesisStateRef = ref phase0.BeaconState
|
2020-10-12 01:07:20 +00:00
|
|
|
|
2020-03-24 11:13:07 +00:00
|
|
|
Eth1Block* = ref object
|
2022-08-10 12:31:10 +00:00
|
|
|
hash*: Eth2Digest
|
2020-03-24 11:13:07 +00:00
|
|
|
number*: Eth1BlockNumber
|
|
|
|
timestamp*: Eth1BlockTimestamp
|
2022-08-10 12:31:10 +00:00
|
|
|
## Basic properties of the block
|
|
|
|
## These must be initialized in the constructor
|
|
|
|
|
2020-11-24 21:21:47 +00:00
|
|
|
deposits*: seq[DepositData]
|
2022-08-10 12:31:10 +00:00
|
|
|
## Deposits inside this particular block
|
|
|
|
|
|
|
|
depositRoot*: Eth2Digest
|
|
|
|
depositCount*: uint64
|
|
|
|
## Global deposits count and hash tree root of the entire sequence
|
|
|
|
## These are computed when the block is added to the chain (see `addBlock`)
|
2020-11-24 21:21:47 +00:00
|
|
|
|
|
|
|
when hasGenesisDetection:
|
|
|
|
activeValidatorsCount*: uint64
|
2020-06-25 23:33:06 +00:00
|
|
|
|
2020-03-24 11:13:07 +00:00
|
|
|
Eth1Chain* = object
|
2020-12-03 04:30:35 +00:00
|
|
|
db: BeaconChainDB
|
Implement split preset/config support (#2710)
* Implement split preset/config support
This is the initial bulk refactor to introduce runtime config values in
a number of places, somewhat replacing the existing mechanism of loading
network metadata.
It still needs more work, this is the initial refactor that introduces
runtime configuration in some of the places that need it.
The PR changes the way presets and constants work, to match the spec. In
particular, a "preset" now refers to the compile-time configuration
while a "cfg" or "RuntimeConfig" is the dynamic part.
A single binary can support either mainnet or minimal, but not both.
Support for other presets has been removed completely (can be readded,
in case there's need).
There's a number of outstanding tasks:
* `SECONDS_PER_SLOT` still needs fixing
* loading custom runtime configs needs redoing
* checking constants against YAML file
* yeerongpilly support
`build/nimbus_beacon_node --network=yeerongpilly --discv5:no --log-level=DEBUG`
* load fork epoch from config
* fix fork digest sent in status
* nicer error string for request failures
* fix tools
* one more
* fixup
* fixup
* fixup
* use "standard" network definition folder in local testnet
Files are loaded from their standard locations, including genesis etc,
to conform to the format used in the `eth2-networks` repo.
* fix launch scripts, allow unknown config values
* fix base config of rest test
* cleanups
* bundle mainnet config using common loader
* fix spec links and names
* only include supported preset in binary
* drop yeerongpilly, add altair-devnet-0, support boot_enr.yaml
2021-07-12 13:01:38 +00:00
|
|
|
cfg: RuntimeConfig
|
2020-12-03 04:30:35 +00:00
|
|
|
finalizedBlockHash: Eth2Digest
|
|
|
|
finalizedDepositsMerkleizer: DepositsMerkleizer
|
2022-01-03 12:22:56 +00:00
|
|
|
## The latest block that reached a 50% majority vote from
|
|
|
|
## the Eth2 validators according to the follow distance and
|
|
|
|
## the ETH1_VOTING_PERIOD
|
|
|
|
|
2022-08-12 13:52:06 +00:00
|
|
|
blocks*: Deque[Eth1Block]
|
2022-01-03 12:22:56 +00:00
|
|
|
## A non-forkable chain of blocks ending at the block with
|
|
|
|
## ETH1_FOLLOW_DISTANCE offset from the head.
|
|
|
|
|
|
|
|
blocksByHash: Table[BlockHash, Eth1Block]
|
|
|
|
|
2022-08-09 21:32:34 +00:00
|
|
|
headMerkleizer: DepositsMerkleizer
|
|
|
|
## Merkleizer state after applying all `blocks`
|
|
|
|
|
2021-01-29 21:21:44 +00:00
|
|
|
hasConsensusViolation: bool
|
|
|
|
## The local chain contradicts the observed consensus on the network
|
2020-03-24 11:13:07 +00:00
|
|
|
|
2020-12-15 21:59:29 +00:00
|
|
|
Eth1MonitorState = enum
|
|
|
|
Initialized
|
|
|
|
Started
|
2022-01-31 17:28:26 +00:00
|
|
|
ReadyToRestartToPrimary
|
2020-12-15 21:59:29 +00:00
|
|
|
Failed
|
|
|
|
Stopping
|
|
|
|
Stopped
|
|
|
|
|
2020-11-03 01:21:07 +00:00
|
|
|
Eth1Monitor* = ref object
|
2020-12-15 21:59:29 +00:00
|
|
|
state: Eth1MonitorState
|
2021-04-06 21:42:59 +00:00
|
|
|
startIdx: int
|
|
|
|
web3Urls: seq[string]
|
2020-12-15 21:59:29 +00:00
|
|
|
eth1Network: Option[Eth1Network]
|
|
|
|
depositContractAddress*: Eth1Address
|
2021-11-25 16:51:51 +00:00
|
|
|
forcePolling: bool
|
2022-03-31 14:43:05 +00:00
|
|
|
jwtSecret: Option[seq[byte]]
|
2022-08-12 13:51:33 +00:00
|
|
|
blocksPerLogsRequest: uint64
|
2020-10-14 14:04:08 +00:00
|
|
|
|
|
|
|
dataProvider: Web3DataProviderRef
|
2021-11-25 16:51:51 +00:00
|
|
|
latestEth1Block: Option[FullBlockId]
|
2020-11-03 01:21:07 +00:00
|
|
|
|
2021-11-15 12:01:47 +00:00
|
|
|
depositsChain: Eth1Chain
|
2020-11-03 01:21:07 +00:00
|
|
|
eth1Progress: AsyncEvent
|
|
|
|
|
2022-09-08 08:57:26 +00:00
|
|
|
exchangedConfiguration*: bool
|
2022-06-15 02:38:27 +00:00
|
|
|
terminalBlockHash*: Option[BlockHash]
|
|
|
|
|
2019-11-22 13:16:07 +00:00
|
|
|
runFut: Future[void]
|
2020-12-15 21:59:29 +00:00
|
|
|
stopFut: Future[void]
|
2022-03-31 14:43:05 +00:00
|
|
|
getBeaconTime: GetBeaconTimeFn
|
2019-11-22 13:16:07 +00:00
|
|
|
|
2022-09-20 06:17:25 +00:00
|
|
|
ttdReachedField: bool
|
|
|
|
|
2020-11-24 21:21:47 +00:00
|
|
|
when hasGenesisDetection:
|
2020-12-15 21:59:29 +00:00
|
|
|
genesisValidators: seq[ImmutableValidatorData]
|
|
|
|
genesisValidatorKeyToIndex: Table[ValidatorPubKey, ValidatorIndex]
|
2021-12-23 14:58:54 +00:00
|
|
|
genesisState: GenesisStateRef
|
2020-11-24 21:21:47 +00:00
|
|
|
genesisStateFut: Future[void]
|
|
|
|
|
2020-10-14 14:04:08 +00:00
|
|
|
Web3DataProvider* = object
|
2020-03-24 11:13:07 +00:00
|
|
|
url: string
|
|
|
|
web3: Web3
|
|
|
|
ns: Sender[DepositContract]
|
2020-06-27 12:01:19 +00:00
|
|
|
blockHeadersSubscription: Subscription
|
2020-03-24 11:13:07 +00:00
|
|
|
|
|
|
|
Web3DataProviderRef* = ref Web3DataProvider
|
|
|
|
|
2021-11-25 16:51:51 +00:00
|
|
|
FullBlockId* = object
|
|
|
|
number: Eth1BlockNumber
|
|
|
|
hash: BlockHash
|
|
|
|
|
2021-05-28 12:42:39 +00:00
|
|
|
DataProviderFailure* = object of CatchableError
|
|
|
|
CorruptDataProvider* = object of DataProviderFailure
|
|
|
|
DataProviderTimeout* = object of DataProviderFailure
|
2020-03-24 11:13:07 +00:00
|
|
|
|
|
|
|
DisconnectHandler* = proc () {.gcsafe, raises: [Defect].}
|
|
|
|
|
|
|
|
DepositEventHandler* = proc (
|
2021-10-14 12:33:55 +00:00
|
|
|
pubkey: PubKeyBytes,
|
|
|
|
withdrawalCredentials: WithdrawalCredentialsBytes,
|
|
|
|
amount: Int64LeBytes,
|
|
|
|
signature: SignatureBytes,
|
|
|
|
merkleTreeIndex: Int64LeBytes,
|
|
|
|
j: JsonNode) {.gcsafe, raises: [Defect].}
|
2020-03-24 11:13:07 +00:00
|
|
|
|
2020-11-19 17:19:03 +00:00
|
|
|
BlockProposalEth1Data* = object
|
|
|
|
vote*: Eth1Data
|
|
|
|
deposits*: seq[Deposit]
|
2020-11-24 21:21:47 +00:00
|
|
|
hasMissingDeposits*: bool
|
2020-11-19 17:19:03 +00:00
|
|
|
|
2020-12-09 22:44:59 +00:00
|
|
|
declareCounter failed_web3_requests,
|
|
|
|
"Failed web3 requests"
|
|
|
|
|
|
|
|
declareGauge eth1_latest_head,
|
|
|
|
"The highest Eth1 block number observed on the network"
|
|
|
|
|
|
|
|
declareGauge eth1_synced_head,
|
|
|
|
"Block number of the highest synchronized block according to follow distance"
|
|
|
|
|
|
|
|
declareGauge eth1_finalized_head,
|
|
|
|
"Block number of the highest Eth1 block finalized by Eth2 consensus"
|
|
|
|
|
|
|
|
declareGauge eth1_finalized_deposits,
|
|
|
|
"Number of deposits that were finalized by the Eth2 consensus"
|
|
|
|
|
|
|
|
declareGauge eth1_chain_len,
|
|
|
|
"The length of the in-memory chain of Eth1 blocks"
|
|
|
|
|
2022-09-20 06:17:25 +00:00
|
|
|
func ttdReached*(m: Eth1Monitor): bool =
|
|
|
|
m.ttdReachedField
|
|
|
|
|
2021-12-23 14:58:54 +00:00
|
|
|
template cfg(m: Eth1Monitor): auto =
|
|
|
|
m.depositsChain.cfg
|
|
|
|
|
2020-12-15 21:59:29 +00:00
|
|
|
when hasGenesisDetection:
|
2021-12-23 14:58:54 +00:00
|
|
|
import ../spec/[beaconstate, signatures]
|
2020-12-15 21:59:29 +00:00
|
|
|
|
|
|
|
template hasEnoughValidators(m: Eth1Monitor, blk: Eth1Block): bool =
|
Implement split preset/config support (#2710)
* Implement split preset/config support
This is the initial bulk refactor to introduce runtime config values in
a number of places, somewhat replacing the existing mechanism of loading
network metadata.
It still needs more work, this is the initial refactor that introduces
runtime configuration in some of the places that need it.
The PR changes the way presets and constants work, to match the spec. In
particular, a "preset" now refers to the compile-time configuration
while a "cfg" or "RuntimeConfig" is the dynamic part.
A single binary can support either mainnet or minimal, but not both.
Support for other presets has been removed completely (can be readded,
in case there's need).
There's a number of outstanding tasks:
* `SECONDS_PER_SLOT` still needs fixing
* loading custom runtime configs needs redoing
* checking constants against YAML file
* yeerongpilly support
`build/nimbus_beacon_node --network=yeerongpilly --discv5:no --log-level=DEBUG`
* load fork epoch from config
* fix fork digest sent in status
* nicer error string for request failures
* fix tools
* one more
* fixup
* fixup
* fixup
* use "standard" network definition folder in local testnet
Files are loaded from their standard locations, including genesis etc,
to conform to the format used in the `eth2-networks` repo.
* fix launch scripts, allow unknown config values
* fix base config of rest test
* cleanups
* bundle mainnet config using common loader
* fix spec links and names
* only include supported preset in binary
* drop yeerongpilly, add altair-devnet-0, support boot_enr.yaml
2021-07-12 13:01:38 +00:00
|
|
|
blk.activeValidatorsCount >= m.cfg.MIN_GENESIS_ACTIVE_VALIDATOR_COUNT
|
2020-12-15 21:59:29 +00:00
|
|
|
|
|
|
|
func chainHasEnoughValidators(m: Eth1Monitor): bool =
|
2021-12-23 14:58:54 +00:00
|
|
|
m.depositsChain.blocks.len > 0 and m.hasEnoughValidators(m.depositsChain.blocks[^1])
|
2020-12-15 21:59:29 +00:00
|
|
|
|
|
|
|
func isAfterMinGenesisTime(m: Eth1Monitor, blk: Eth1Block): bool =
|
|
|
|
doAssert blk.timestamp != 0
|
Implement split preset/config support (#2710)
* Implement split preset/config support
This is the initial bulk refactor to introduce runtime config values in
a number of places, somewhat replacing the existing mechanism of loading
network metadata.
It still needs more work, this is the initial refactor that introduces
runtime configuration in some of the places that need it.
The PR changes the way presets and constants work, to match the spec. In
particular, a "preset" now refers to the compile-time configuration
while a "cfg" or "RuntimeConfig" is the dynamic part.
A single binary can support either mainnet or minimal, but not both.
Support for other presets has been removed completely (can be readded,
in case there's need).
There's a number of outstanding tasks:
* `SECONDS_PER_SLOT` still needs fixing
* loading custom runtime configs needs redoing
* checking constants against YAML file
* yeerongpilly support
`build/nimbus_beacon_node --network=yeerongpilly --discv5:no --log-level=DEBUG`
* load fork epoch from config
* fix fork digest sent in status
* nicer error string for request failures
* fix tools
* one more
* fixup
* fixup
* fixup
* use "standard" network definition folder in local testnet
Files are loaded from their standard locations, including genesis etc,
to conform to the format used in the `eth2-networks` repo.
* fix launch scripts, allow unknown config values
* fix base config of rest test
* cleanups
* bundle mainnet config using common loader
* fix spec links and names
* only include supported preset in binary
* drop yeerongpilly, add altair-devnet-0, support boot_enr.yaml
2021-07-12 13:01:38 +00:00
|
|
|
let t = genesis_time_from_eth1_timestamp(m.cfg, uint64 blk.timestamp)
|
|
|
|
t >= m.cfg.MIN_GENESIS_TIME
|
2020-12-15 21:59:29 +00:00
|
|
|
|
|
|
|
func isGenesisCandidate(m: Eth1Monitor, blk: Eth1Block): bool =
|
|
|
|
m.hasEnoughValidators(blk) and m.isAfterMinGenesisTime(blk)
|
|
|
|
|
2021-12-23 14:58:54 +00:00
|
|
|
proc findGenesisBlockInRange(m: Eth1Monitor, startBlock, endBlock: Eth1Block):
|
|
|
|
Future[Eth1Block] {.gcsafe.}
|
2020-12-15 21:59:29 +00:00
|
|
|
|
2021-12-23 14:58:54 +00:00
|
|
|
proc signalGenesis(m: Eth1Monitor, genesisState: GenesisStateRef) =
|
2020-12-15 21:59:29 +00:00
|
|
|
m.genesisState = genesisState
|
2020-10-14 14:04:08 +00:00
|
|
|
|
2020-12-15 21:59:29 +00:00
|
|
|
if not m.genesisStateFut.isNil:
|
|
|
|
m.genesisStateFut.complete()
|
|
|
|
m.genesisStateFut = nil
|
|
|
|
|
2021-11-25 10:53:31 +00:00
|
|
|
func allGenesisDepositsUpTo(m: Eth1Monitor, totalDeposits: uint64): seq[DepositData] =
|
2021-12-23 14:58:54 +00:00
|
|
|
for i in 0 ..< int64(totalDeposits):
|
|
|
|
result.add m.depositsChain.db.genesisDeposits.get(i)
|
2020-12-15 21:59:29 +00:00
|
|
|
|
2021-12-23 14:58:54 +00:00
|
|
|
proc createGenesisState(m: Eth1Monitor, eth1Block: Eth1Block): GenesisStateRef =
|
2020-12-15 21:59:29 +00:00
|
|
|
notice "Generating genesis state",
|
|
|
|
blockNum = eth1Block.number,
|
2022-08-10 12:31:10 +00:00
|
|
|
blockHash = eth1Block.hash,
|
2020-12-15 21:59:29 +00:00
|
|
|
blockTimestamp = eth1Block.timestamp,
|
2022-08-10 12:31:10 +00:00
|
|
|
totalDeposits = eth1Block.depositCount,
|
2020-12-15 21:59:29 +00:00
|
|
|
activeValidators = eth1Block.activeValidatorsCount
|
|
|
|
|
2022-08-10 12:31:10 +00:00
|
|
|
var deposits = m.allGenesisDepositsUpTo(eth1Block.depositCount)
|
2020-12-15 21:59:29 +00:00
|
|
|
|
2021-11-18 12:02:43 +00:00
|
|
|
result = newClone(initialize_beacon_state_from_eth1(
|
Implement split preset/config support (#2710)
* Implement split preset/config support
This is the initial bulk refactor to introduce runtime config values in
a number of places, somewhat replacing the existing mechanism of loading
network metadata.
It still needs more work, this is the initial refactor that introduces
runtime configuration in some of the places that need it.
The PR changes the way presets and constants work, to match the spec. In
particular, a "preset" now refers to the compile-time configuration
while a "cfg" or "RuntimeConfig" is the dynamic part.
A single binary can support either mainnet or minimal, but not both.
Support for other presets has been removed completely (can be readded,
in case there's need).
There's a number of outstanding tasks:
* `SECONDS_PER_SLOT` still needs fixing
* loading custom runtime configs needs redoing
* checking constants against YAML file
* yeerongpilly support
`build/nimbus_beacon_node --network=yeerongpilly --discv5:no --log-level=DEBUG`
* load fork epoch from config
* fix fork digest sent in status
* nicer error string for request failures
* fix tools
* one more
* fixup
* fixup
* fixup
* use "standard" network definition folder in local testnet
Files are loaded from their standard locations, including genesis etc,
to conform to the format used in the `eth2-networks` repo.
* fix launch scripts, allow unknown config values
* fix base config of rest test
* cleanups
* bundle mainnet config using common loader
* fix spec links and names
* only include supported preset in binary
* drop yeerongpilly, add altair-devnet-0, support boot_enr.yaml
2021-07-12 13:01:38 +00:00
|
|
|
m.cfg,
|
2022-08-10 12:31:10 +00:00
|
|
|
eth1Block.hash,
|
2020-12-15 21:59:29 +00:00
|
|
|
eth1Block.timestamp.uint64,
|
2021-11-18 12:02:43 +00:00
|
|
|
deposits, {}))
|
2020-12-15 21:59:29 +00:00
|
|
|
|
|
|
|
if eth1Block.activeValidatorsCount != 0:
|
|
|
|
doAssert result.validators.lenu64 == eth1Block.activeValidatorsCount
|
|
|
|
|
2021-12-23 14:58:54 +00:00
|
|
|
proc produceDerivedData(m: Eth1Monitor, deposit: DepositData) =
|
2020-12-15 21:59:29 +00:00
|
|
|
let htr = hash_tree_root(deposit)
|
|
|
|
|
Implement split preset/config support (#2710)
* Implement split preset/config support
This is the initial bulk refactor to introduce runtime config values in
a number of places, somewhat replacing the existing mechanism of loading
network metadata.
It still needs more work, this is the initial refactor that introduces
runtime configuration in some of the places that need it.
The PR changes the way presets and constants work, to match the spec. In
particular, a "preset" now refers to the compile-time configuration
while a "cfg" or "RuntimeConfig" is the dynamic part.
A single binary can support either mainnet or minimal, but not both.
Support for other presets has been removed completely (can be readded,
in case there's need).
There's a number of outstanding tasks:
* `SECONDS_PER_SLOT` still needs fixing
* loading custom runtime configs needs redoing
* checking constants against YAML file
* yeerongpilly support
`build/nimbus_beacon_node --network=yeerongpilly --discv5:no --log-level=DEBUG`
* load fork epoch from config
* fix fork digest sent in status
* nicer error string for request failures
* fix tools
* one more
* fixup
* fixup
* fixup
* use "standard" network definition folder in local testnet
Files are loaded from their standard locations, including genesis etc,
to conform to the format used in the `eth2-networks` repo.
* fix launch scripts, allow unknown config values
* fix base config of rest test
* cleanups
* bundle mainnet config using common loader
* fix spec links and names
* only include supported preset in binary
* drop yeerongpilly, add altair-devnet-0, support boot_enr.yaml
2021-07-12 13:01:38 +00:00
|
|
|
if verify_deposit_signature(m.cfg, deposit):
|
2020-12-15 21:59:29 +00:00
|
|
|
let pubkey = deposit.pubkey
|
|
|
|
if pubkey notin m.genesisValidatorKeyToIndex:
|
|
|
|
let idx = ValidatorIndex m.genesisValidators.len
|
|
|
|
m.genesisValidators.add ImmutableValidatorData(
|
|
|
|
pubkey: pubkey,
|
|
|
|
withdrawal_credentials: deposit.withdrawal_credentials)
|
2021-05-17 16:37:26 +00:00
|
|
|
m.genesisValidatorKeyToIndex[pubkey] = idx
|
2020-12-15 21:59:29 +00:00
|
|
|
|
2021-12-23 14:58:54 +00:00
|
|
|
proc processGenesisDeposit*(m: Eth1Monitor, newDeposit: DepositData) =
|
|
|
|
m.depositsChain.db.genesisDeposits.add newDeposit
|
2020-12-15 21:59:29 +00:00
|
|
|
m.produceDerivedData(newDeposit)
|
2020-10-14 14:04:08 +00:00
|
|
|
|
2022-01-03 12:22:56 +00:00
|
|
|
template depositChainBlocks*(m: Eth1Monitor): Deque[Eth1Block] =
|
2021-11-15 12:01:47 +00:00
|
|
|
m.depositsChain.blocks
|
2020-11-19 17:19:03 +00:00
|
|
|
|
2020-12-03 04:30:35 +00:00
|
|
|
template finalizedDepositsMerkleizer(m: Eth1Monitor): auto =
|
2021-11-15 12:01:47 +00:00
|
|
|
m.depositsChain.finalizedDepositsMerkleizer
|
2020-12-03 04:30:35 +00:00
|
|
|
|
2022-08-09 21:32:34 +00:00
|
|
|
template headMerkleizer(m: Eth1Monitor): auto =
|
|
|
|
m.depositsChain.headMerkleizer
|
|
|
|
|
2020-11-12 13:49:13 +00:00
|
|
|
proc fixupWeb3Urls*(web3Url: var string) =
|
2022-01-15 07:11:17 +00:00
|
|
|
var normalizedUrl = toLowerAscii(web3Url)
|
|
|
|
if not (normalizedUrl.startsWith("https://") or
|
|
|
|
normalizedUrl.startsWith("http://") or
|
|
|
|
normalizedUrl.startsWith("wss://") or
|
|
|
|
normalizedUrl.startsWith("ws://")):
|
|
|
|
warn "The Web3 URL does not specify a protocol. Assuming a WebSocket server", web3Url
|
2022-06-16 05:46:43 +00:00
|
|
|
web3Url = "ws://" & web3Url
|
2020-11-05 23:11:06 +00:00
|
|
|
|
2020-12-09 22:44:59 +00:00
|
|
|
template toGaugeValue(x: Quantity): int64 =
|
|
|
|
toGaugeValue(distinctBase x)
|
|
|
|
|
Implement split preset/config support (#2710)
* Implement split preset/config support
This is the initial bulk refactor to introduce runtime config values in
a number of places, somewhat replacing the existing mechanism of loading
network metadata.
It still needs more work, this is the initial refactor that introduces
runtime configuration in some of the places that need it.
The PR changes the way presets and constants work, to match the spec. In
particular, a "preset" now refers to the compile-time configuration
while a "cfg" or "RuntimeConfig" is the dynamic part.
A single binary can support either mainnet or minimal, but not both.
Support for other presets has been removed completely (can be readded,
in case there's need).
There's a number of outstanding tasks:
* `SECONDS_PER_SLOT` still needs fixing
* loading custom runtime configs needs redoing
* checking constants against YAML file
* yeerongpilly support
`build/nimbus_beacon_node --network=yeerongpilly --discv5:no --log-level=DEBUG`
* load fork epoch from config
* fix fork digest sent in status
* nicer error string for request failures
* fix tools
* one more
* fixup
* fixup
* fixup
* use "standard" network definition folder in local testnet
Files are loaded from their standard locations, including genesis etc,
to conform to the format used in the `eth2-networks` repo.
* fix launch scripts, allow unknown config values
* fix base config of rest test
* cleanups
* bundle mainnet config using common loader
* fix spec links and names
* only include supported preset in binary
* drop yeerongpilly, add altair-devnet-0, support boot_enr.yaml
2021-07-12 13:01:38 +00:00
|
|
|
# TODO: Add cfg validation
|
2020-07-07 23:02:14 +00:00
|
|
|
# MIN_GENESIS_ACTIVE_VALIDATOR_COUNT should be larger than SLOTS_PER_EPOCH
|
Implement split preset/config support (#2710)
* Implement split preset/config support
This is the initial bulk refactor to introduce runtime config values in
a number of places, somewhat replacing the existing mechanism of loading
network metadata.
It still needs more work, this is the initial refactor that introduces
runtime configuration in some of the places that need it.
The PR changes the way presets and constants work, to match the spec. In
particular, a "preset" now refers to the compile-time configuration
while a "cfg" or "RuntimeConfig" is the dynamic part.
A single binary can support either mainnet or minimal, but not both.
Support for other presets has been removed completely (can be readded,
in case there's need).
There's a number of outstanding tasks:
* `SECONDS_PER_SLOT` still needs fixing
* loading custom runtime configs needs redoing
* checking constants against YAML file
* yeerongpilly support
`build/nimbus_beacon_node --network=yeerongpilly --discv5:no --log-level=DEBUG`
* load fork epoch from config
* fix fork digest sent in status
* nicer error string for request failures
* fix tools
* one more
* fixup
* fixup
* fixup
* use "standard" network definition folder in local testnet
Files are loaded from their standard locations, including genesis etc,
to conform to the format used in the `eth2-networks` repo.
* fix launch scripts, allow unknown config values
* fix base config of rest test
* cleanups
* bundle mainnet config using common loader
* fix spec links and names
* only include supported preset in binary
* drop yeerongpilly, add altair-devnet-0, support boot_enr.yaml
2021-07-12 13:01:38 +00:00
|
|
|
# doAssert SECONDS_PER_ETH1_BLOCK * cfg.ETH1_FOLLOW_DISTANCE < GENESIS_DELAY,
|
2020-07-07 23:02:14 +00:00
|
|
|
# "Invalid configuration: GENESIS_DELAY is set too low"
|
2020-03-24 11:13:07 +00:00
|
|
|
|
2022-11-08 02:37:28 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.3.0-alpha.0/specs/phase0/validator.md#get_eth1_data
|
2021-06-11 17:51:46 +00:00
|
|
|
func compute_time_at_slot(genesis_time: uint64, slot: Slot): uint64 =
|
|
|
|
genesis_time + slot * SECONDS_PER_SLOT
|
2020-03-24 11:13:07 +00:00
|
|
|
|
2022-11-08 02:37:28 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.3.0-alpha.0/specs/phase0/validator.md#get_eth1_data
|
2021-12-05 17:32:41 +00:00
|
|
|
func voting_period_start_time(state: ForkedHashedBeaconState): uint64 =
|
2020-06-15 09:38:05 +00:00
|
|
|
let eth1_voting_period_start_slot =
|
2021-05-21 09:23:28 +00:00
|
|
|
getStateField(state, slot) - getStateField(state, slot) mod
|
|
|
|
SLOTS_PER_ETH1_VOTING_PERIOD.uint64
|
2021-06-11 17:51:46 +00:00
|
|
|
compute_time_at_slot(
|
|
|
|
getStateField(state, genesis_time), eth1_voting_period_start_slot)
|
2020-03-24 11:13:07 +00:00
|
|
|
|
2022-11-08 02:37:28 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.3.0-alpha.0/specs/phase0/validator.md#get_eth1_data
|
Implement split preset/config support (#2710)
* Implement split preset/config support
This is the initial bulk refactor to introduce runtime config values in
a number of places, somewhat replacing the existing mechanism of loading
network metadata.
It still needs more work, this is the initial refactor that introduces
runtime configuration in some of the places that need it.
The PR changes the way presets and constants work, to match the spec. In
particular, a "preset" now refers to the compile-time configuration
while a "cfg" or "RuntimeConfig" is the dynamic part.
A single binary can support either mainnet or minimal, but not both.
Support for other presets has been removed completely (can be readded,
in case there's need).
There's a number of outstanding tasks:
* `SECONDS_PER_SLOT` still needs fixing
* loading custom runtime configs needs redoing
* checking constants against YAML file
* yeerongpilly support
`build/nimbus_beacon_node --network=yeerongpilly --discv5:no --log-level=DEBUG`
* load fork epoch from config
* fix fork digest sent in status
* nicer error string for request failures
* fix tools
* one more
* fixup
* fixup
* fixup
* use "standard" network definition folder in local testnet
Files are loaded from their standard locations, including genesis etc,
to conform to the format used in the `eth2-networks` repo.
* fix launch scripts, allow unknown config values
* fix base config of rest test
* cleanups
* bundle mainnet config using common loader
* fix spec links and names
* only include supported preset in binary
* drop yeerongpilly, add altair-devnet-0, support boot_enr.yaml
2021-07-12 13:01:38 +00:00
|
|
|
func is_candidate_block(cfg: RuntimeConfig,
|
2020-11-20 14:05:37 +00:00
|
|
|
blk: Eth1Block,
|
|
|
|
period_start: uint64): bool =
|
Implement split preset/config support (#2710)
* Implement split preset/config support
This is the initial bulk refactor to introduce runtime config values in
a number of places, somewhat replacing the existing mechanism of loading
network metadata.
It still needs more work, this is the initial refactor that introduces
runtime configuration in some of the places that need it.
The PR changes the way presets and constants work, to match the spec. In
particular, a "preset" now refers to the compile-time configuration
while a "cfg" or "RuntimeConfig" is the dynamic part.
A single binary can support either mainnet or minimal, but not both.
Support for other presets has been removed completely (can be readded,
in case there's need).
There's a number of outstanding tasks:
* `SECONDS_PER_SLOT` still needs fixing
* loading custom runtime configs needs redoing
* checking constants against YAML file
* yeerongpilly support
`build/nimbus_beacon_node --network=yeerongpilly --discv5:no --log-level=DEBUG`
* load fork epoch from config
* fix fork digest sent in status
* nicer error string for request failures
* fix tools
* one more
* fixup
* fixup
* fixup
* use "standard" network definition folder in local testnet
Files are loaded from their standard locations, including genesis etc,
to conform to the format used in the `eth2-networks` repo.
* fix launch scripts, allow unknown config values
* fix base config of rest test
* cleanups
* bundle mainnet config using common loader
* fix spec links and names
* only include supported preset in binary
* drop yeerongpilly, add altair-devnet-0, support boot_enr.yaml
2021-07-12 13:01:38 +00:00
|
|
|
(blk.timestamp + cfg.SECONDS_PER_ETH1_BLOCK * cfg.ETH1_FOLLOW_DISTANCE <= period_start) and
|
|
|
|
(blk.timestamp + cfg.SECONDS_PER_ETH1_BLOCK * cfg.ETH1_FOLLOW_DISTANCE * 2 >= period_start)
|
2020-03-24 11:13:07 +00:00
|
|
|
|
2020-06-19 17:42:28 +00:00
|
|
|
func asEth2Digest*(x: BlockHash): Eth2Digest =
|
2020-03-24 11:13:07 +00:00
|
|
|
Eth2Digest(data: array[32, byte](x))
|
|
|
|
|
2022-03-25 11:40:10 +00:00
|
|
|
template asBlockHash*(x: Eth2Digest): BlockHash =
|
2020-03-24 11:13:07 +00:00
|
|
|
BlockHash(x.data)
|
|
|
|
|
2022-01-03 12:22:56 +00:00
|
|
|
func asConsensusExecutionPayload*(rpcExecutionPayload: ExecutionPayloadV1):
|
2022-01-12 14:50:30 +00:00
|
|
|
bellatrix.ExecutionPayload =
|
2022-04-15 12:46:56 +00:00
|
|
|
template getTransaction(tt: TypedTransaction): bellatrix.Transaction =
|
|
|
|
bellatrix.Transaction.init(tt.distinctBase)
|
2022-01-03 12:22:56 +00:00
|
|
|
|
2022-01-12 14:50:30 +00:00
|
|
|
bellatrix.ExecutionPayload(
|
2022-01-03 12:22:56 +00:00
|
|
|
parent_hash: rpcExecutionPayload.parentHash.asEth2Digest,
|
|
|
|
feeRecipient:
|
|
|
|
ExecutionAddress(data: rpcExecutionPayload.feeRecipient.distinctBase),
|
|
|
|
state_root: rpcExecutionPayload.stateRoot.asEth2Digest,
|
|
|
|
receipts_root: rpcExecutionPayload.receiptsRoot.asEth2Digest,
|
|
|
|
logs_bloom: BloomLogs(data: rpcExecutionPayload.logsBloom.distinctBase),
|
2022-03-03 16:08:14 +00:00
|
|
|
prev_randao: rpcExecutionPayload.prevRandao.asEth2Digest,
|
2022-01-03 12:22:56 +00:00
|
|
|
block_number: rpcExecutionPayload.blockNumber.uint64,
|
|
|
|
gas_limit: rpcExecutionPayload.gasLimit.uint64,
|
|
|
|
gas_used: rpcExecutionPayload.gasUsed.uint64,
|
|
|
|
timestamp: rpcExecutionPayload.timestamp.uint64,
|
|
|
|
extra_data:
|
|
|
|
List[byte, MAX_EXTRA_DATA_BYTES].init(
|
|
|
|
rpcExecutionPayload.extraData.distinctBase),
|
2022-06-01 12:57:28 +00:00
|
|
|
base_fee_per_gas: rpcExecutionPayload.baseFeePerGas,
|
2022-01-03 12:22:56 +00:00
|
|
|
block_hash: rpcExecutionPayload.blockHash.asEth2Digest,
|
2022-01-12 14:50:30 +00:00
|
|
|
transactions: List[bellatrix.Transaction, MAX_TRANSACTIONS_PER_PAYLOAD].init(
|
2022-01-03 12:22:56 +00:00
|
|
|
mapIt(rpcExecutionPayload.transactions, it.getTransaction)))
|
|
|
|
|
2022-01-12 14:50:30 +00:00
|
|
|
func asEngineExecutionPayload*(executionPayload: bellatrix.ExecutionPayload):
|
2022-01-03 12:22:56 +00:00
|
|
|
ExecutionPayloadV1 =
|
2022-04-15 12:46:56 +00:00
|
|
|
template getTypedTransaction(tt: bellatrix.Transaction): TypedTransaction =
|
|
|
|
TypedTransaction(tt.distinctBase)
|
2022-01-03 12:22:56 +00:00
|
|
|
|
|
|
|
engine_api.ExecutionPayloadV1(
|
|
|
|
parentHash: executionPayload.parent_hash.asBlockHash,
|
2022-04-08 16:22:49 +00:00
|
|
|
feeRecipient: Address(executionPayload.fee_recipient.data),
|
2022-01-03 12:22:56 +00:00
|
|
|
stateRoot: executionPayload.state_root.asBlockHash,
|
|
|
|
receiptsRoot: executionPayload.receipts_root.asBlockHash,
|
|
|
|
logsBloom:
|
|
|
|
FixedBytes[BYTES_PER_LOGS_BLOOM](executionPayload.logs_bloom.data),
|
2022-03-03 16:08:14 +00:00
|
|
|
prevRandao: executionPayload.prev_randao.asBlockHash,
|
2022-01-03 12:22:56 +00:00
|
|
|
blockNumber: Quantity(executionPayload.block_number),
|
|
|
|
gasLimit: Quantity(executionPayload.gas_limit),
|
|
|
|
gasUsed: Quantity(executionPayload.gas_used),
|
|
|
|
timestamp: Quantity(executionPayload.timestamp),
|
|
|
|
extraData:
|
|
|
|
DynamicBytes[0, MAX_EXTRA_DATA_BYTES](executionPayload.extra_data),
|
2022-06-01 12:57:28 +00:00
|
|
|
baseFeePerGas: executionPayload.base_fee_per_gas,
|
2022-01-03 12:22:56 +00:00
|
|
|
blockHash: executionPayload.block_hash.asBlockHash,
|
|
|
|
transactions: mapIt(executionPayload.transactions, it.getTypedTransaction))
|
|
|
|
|
2020-09-06 08:39:25 +00:00
|
|
|
func shortLog*(b: Eth1Block): string =
|
2021-03-26 06:52:01 +00:00
|
|
|
try:
|
2022-08-10 12:31:10 +00:00
|
|
|
&"{b.number}:{shortLog b.hash}(deposits = {b.depositCount})"
|
2021-03-26 06:52:01 +00:00
|
|
|
except ValueError as exc: raiseAssert exc.msg
|
2020-06-27 12:01:19 +00:00
|
|
|
|
2021-12-05 17:32:41 +00:00
|
|
|
template findBlock(chain: Eth1Chain, eth1Data: Eth1Data): Eth1Block =
|
2020-12-03 04:30:35 +00:00
|
|
|
getOrDefault(chain.blocksByHash, asBlockHash(eth1Data.block_hash), nil)
|
2020-03-24 11:13:07 +00:00
|
|
|
|
2020-11-19 17:19:03 +00:00
|
|
|
func makeSuccessorWithoutDeposits(existingBlock: Eth1Block,
|
2022-04-08 16:22:49 +00:00
|
|
|
successor: BlockObject): Eth1Block =
|
2020-11-24 21:21:47 +00:00
|
|
|
result = Eth1Block(
|
2022-08-10 12:31:10 +00:00
|
|
|
hash: successor.hash.asEth2Digest,
|
2020-11-19 17:19:03 +00:00
|
|
|
number: Eth1BlockNumber successor.number,
|
2022-08-10 12:31:10 +00:00
|
|
|
timestamp: Eth1BlockTimestamp successor.timestamp)
|
2020-11-24 21:21:47 +00:00
|
|
|
|
|
|
|
when hasGenesisDetection:
|
|
|
|
result.activeValidatorsCount = existingBlock.activeValidatorsCount
|
2020-11-19 17:19:03 +00:00
|
|
|
|
2020-12-03 04:30:35 +00:00
|
|
|
func latestCandidateBlock(chain: Eth1Chain, periodStart: uint64): Eth1Block =
|
|
|
|
for i in countdown(chain.blocks.len - 1, 0):
|
|
|
|
let blk = chain.blocks[i]
|
Implement split preset/config support (#2710)
* Implement split preset/config support
This is the initial bulk refactor to introduce runtime config values in
a number of places, somewhat replacing the existing mechanism of loading
network metadata.
It still needs more work, this is the initial refactor that introduces
runtime configuration in some of the places that need it.
The PR changes the way presets and constants work, to match the spec. In
particular, a "preset" now refers to the compile-time configuration
while a "cfg" or "RuntimeConfig" is the dynamic part.
A single binary can support either mainnet or minimal, but not both.
Support for other presets has been removed completely (can be readded,
in case there's need).
There's a number of outstanding tasks:
* `SECONDS_PER_SLOT` still needs fixing
* loading custom runtime configs needs redoing
* checking constants against YAML file
* yeerongpilly support
`build/nimbus_beacon_node --network=yeerongpilly --discv5:no --log-level=DEBUG`
* load fork epoch from config
* fix fork digest sent in status
* nicer error string for request failures
* fix tools
* one more
* fixup
* fixup
* fixup
* use "standard" network definition folder in local testnet
Files are loaded from their standard locations, including genesis etc,
to conform to the format used in the `eth2-networks` repo.
* fix launch scripts, allow unknown config values
* fix base config of rest test
* cleanups
* bundle mainnet config using common loader
* fix spec links and names
* only include supported preset in binary
* drop yeerongpilly, add altair-devnet-0, support boot_enr.yaml
2021-07-12 13:01:38 +00:00
|
|
|
if is_candidate_block(chain.cfg, blk, periodStart):
|
2020-03-24 11:13:07 +00:00
|
|
|
return blk
|
|
|
|
|
2020-12-03 04:30:35 +00:00
|
|
|
proc popFirst(chain: var Eth1Chain) =
|
|
|
|
let removed = chain.blocks.popFirst
|
2022-08-10 12:31:10 +00:00
|
|
|
chain.blocksByHash.del removed.hash.asBlockHash
|
2020-12-03 04:30:35 +00:00
|
|
|
eth1_chain_len.set chain.blocks.len.int64
|
2020-06-27 12:01:19 +00:00
|
|
|
|
2022-08-10 12:31:10 +00:00
|
|
|
func getDepositsRoot*(m: DepositsMerkleizer): Eth2Digest =
|
|
|
|
mixInLength(m.getFinalHash, int m.totalChunks)
|
|
|
|
|
2020-12-03 04:30:35 +00:00
|
|
|
proc addBlock*(chain: var Eth1Chain, newBlock: Eth1Block) =
|
2022-08-10 12:31:10 +00:00
|
|
|
for deposit in newBlock.deposits:
|
|
|
|
chain.headMerkleizer.addChunk hash_tree_root(deposit).data
|
|
|
|
|
|
|
|
newBlock.depositCount = chain.headMerkleizer.getChunkCount
|
|
|
|
newBlock.depositRoot = chain.headMerkleizer.getDepositsRoot
|
|
|
|
|
2020-12-03 04:30:35 +00:00
|
|
|
chain.blocks.addLast newBlock
|
2022-08-10 12:31:10 +00:00
|
|
|
chain.blocksByHash[newBlock.hash.asBlockHash] = newBlock
|
|
|
|
|
2020-12-03 04:30:35 +00:00
|
|
|
eth1_chain_len.set chain.blocks.len.int64
|
2020-03-24 11:13:07 +00:00
|
|
|
|
2022-08-10 12:31:10 +00:00
|
|
|
func toVoteData(blk: Eth1Block): Eth1Data =
|
|
|
|
Eth1Data(
|
|
|
|
deposit_root: blk.depositRoot,
|
|
|
|
deposit_count: blk.depositCount,
|
|
|
|
block_hash: blk.hash)
|
|
|
|
|
2021-12-08 17:29:22 +00:00
|
|
|
func hash*(x: Eth1Data): Hash =
|
2021-12-03 15:04:58 +00:00
|
|
|
hash(x.block_hash)
|
2020-11-19 17:19:03 +00:00
|
|
|
|
2021-05-28 12:42:39 +00:00
|
|
|
template awaitWithRetries*[T](lazyFutExpr: Future[T],
|
|
|
|
retries = 3,
|
|
|
|
timeout = web3Timeouts): untyped =
|
2020-12-01 21:20:28 +00:00
|
|
|
const
|
|
|
|
reqType = astToStr(lazyFutExpr)
|
|
|
|
var
|
|
|
|
retryDelayMs = 16000
|
|
|
|
f: Future[T]
|
|
|
|
attempts = 0
|
|
|
|
|
|
|
|
while true:
|
|
|
|
f = lazyFutExpr
|
|
|
|
yield f or sleepAsync(timeout)
|
|
|
|
if not f.finished:
|
|
|
|
await cancelAndWait(f)
|
|
|
|
elif f.failed:
|
2022-06-21 19:01:45 +00:00
|
|
|
when not (f.error of CatchableError):
|
|
|
|
static: doAssert false, "f.error not CatchableError"
|
2022-04-08 16:22:49 +00:00
|
|
|
debug "Web3 request failed", req = reqType, err = f.error.msg
|
|
|
|
inc failed_web3_requests
|
2020-12-01 21:20:28 +00:00
|
|
|
else:
|
|
|
|
break
|
|
|
|
|
|
|
|
inc attempts
|
|
|
|
if attempts >= retries:
|
2020-12-09 23:45:49 +00:00
|
|
|
var errorMsg = reqType & " failed " & $retries & " times"
|
|
|
|
if f.failed: errorMsg &= ". Last error: " & f.error.msg
|
|
|
|
raise newException(DataProviderFailure, errorMsg)
|
2020-12-01 21:20:28 +00:00
|
|
|
|
|
|
|
await sleepAsync(chronos.milliseconds(retryDelayMs))
|
|
|
|
retryDelayMs *= 2
|
|
|
|
|
|
|
|
read(f)
|
|
|
|
|
2021-12-05 17:32:41 +00:00
|
|
|
proc close(p: Web3DataProviderRef): Future[void] {.async.} =
|
2020-06-27 12:01:19 +00:00
|
|
|
if p.blockHeadersSubscription != nil:
|
2020-12-15 21:59:29 +00:00
|
|
|
try:
|
|
|
|
awaitWithRetries(p.blockHeadersSubscription.unsubscribe())
|
|
|
|
except CatchableError:
|
|
|
|
debug "Failed to clean up block headers subscription properly"
|
2020-06-27 12:01:19 +00:00
|
|
|
|
2022-07-25 19:23:02 +00:00
|
|
|
awaitWithTimeout(p.web3.close(), 30.seconds):
|
|
|
|
debug "Failed to close data provider in time"
|
2020-06-27 12:01:19 +00:00
|
|
|
|
2021-12-05 17:32:41 +00:00
|
|
|
proc getBlockByHash(p: Web3DataProviderRef, hash: BlockHash):
|
|
|
|
Future[BlockObject] =
|
2020-06-27 12:01:19 +00:00
|
|
|
return p.web3.provider.eth_getBlockByHash(hash, false)
|
|
|
|
|
2022-02-04 12:12:19 +00:00
|
|
|
proc getBlockByNumber*(p: Web3DataProviderRef,
|
|
|
|
number: Eth1BlockNumber): Future[BlockObject] =
|
2021-04-01 12:43:03 +00:00
|
|
|
let hexNumber = try: &"0x{number:X}" # No leading 0's!
|
|
|
|
except ValueError as exc: raiseAssert exc.msg # Never fails
|
|
|
|
p.web3.provider.eth_getBlockByNumber(hexNumber, false)
|
2020-06-27 12:01:19 +00:00
|
|
|
|
2022-04-05 08:40:59 +00:00
|
|
|
proc getPayload*(p: Eth1Monitor,
|
2022-01-12 14:50:30 +00:00
|
|
|
payloadId: bellatrix.PayloadID): Future[engine_api.ExecutionPayloadV1] =
|
2022-03-25 11:40:10 +00:00
|
|
|
# Eth1 monitor can recycle connections without (external) warning; at least,
|
|
|
|
# don't crash.
|
2022-04-26 09:25:19 +00:00
|
|
|
if p.isNil or p.dataProvider.isNil:
|
2022-08-25 18:07:29 +00:00
|
|
|
let epr = newFuture[engine_api.ExecutionPayloadV1]("getPayload")
|
2022-03-25 11:40:10 +00:00
|
|
|
epr.complete(default(engine_api.ExecutionPayloadV1))
|
|
|
|
return epr
|
|
|
|
|
2022-04-05 08:40:59 +00:00
|
|
|
p.dataProvider.web3.provider.engine_getPayloadV1(FixedBytes[8] payloadId)
|
2021-09-29 19:26:27 +00:00
|
|
|
|
2022-03-25 11:40:10 +00:00
|
|
|
proc newPayload*(p: Eth1Monitor, payload: engine_api.ExecutionPayloadV1):
|
|
|
|
Future[PayloadStatusV1] =
|
|
|
|
# Eth1 monitor can recycle connections without (external) warning; at least,
|
|
|
|
# don't crash.
|
|
|
|
if p.dataProvider.isNil:
|
2022-08-25 18:07:29 +00:00
|
|
|
let epr = newFuture[PayloadStatusV1]("newPayload")
|
2022-03-25 11:40:10 +00:00
|
|
|
epr.complete(PayloadStatusV1(status: PayloadExecutionStatus.syncing))
|
|
|
|
return epr
|
2021-09-29 19:26:27 +00:00
|
|
|
|
2022-03-25 11:40:10 +00:00
|
|
|
p.dataProvider.web3.provider.engine_newPayloadV1(payload)
|
|
|
|
|
|
|
|
proc forkchoiceUpdated*(p: Eth1Monitor,
|
2022-08-25 23:34:02 +00:00
|
|
|
headBlock, safeBlock, finalizedBlock: Eth2Digest):
|
2021-12-17 12:23:32 +00:00
|
|
|
Future[engine_api.ForkchoiceUpdatedResponse] =
|
2022-03-25 11:40:10 +00:00
|
|
|
# Eth1 monitor can recycle connections without (external) warning; at least,
|
|
|
|
# don't crash.
|
2022-04-26 09:25:19 +00:00
|
|
|
if p.isNil or p.dataProvider.isNil:
|
2022-08-25 18:07:29 +00:00
|
|
|
let fcuR =
|
|
|
|
newFuture[engine_api.ForkchoiceUpdatedResponse]("forkchoiceUpdated")
|
2022-03-25 11:40:10 +00:00
|
|
|
fcuR.complete(engine_api.ForkchoiceUpdatedResponse(
|
|
|
|
payloadStatus: PayloadStatusV1(status: PayloadExecutionStatus.syncing)))
|
|
|
|
return fcuR
|
|
|
|
|
|
|
|
p.dataProvider.web3.provider.engine_forkchoiceUpdatedV1(
|
2021-12-17 12:23:32 +00:00
|
|
|
ForkchoiceStateV1(
|
|
|
|
headBlockHash: headBlock.asBlockHash,
|
2022-08-25 23:34:02 +00:00
|
|
|
safeBlockHash: safeBlock.asBlockHash,
|
2021-12-17 12:23:32 +00:00
|
|
|
finalizedBlockHash: finalizedBlock.asBlockHash),
|
|
|
|
none(engine_api.PayloadAttributesV1))
|
|
|
|
|
2022-04-05 08:40:59 +00:00
|
|
|
proc forkchoiceUpdated*(p: Eth1Monitor,
|
2022-08-25 23:34:02 +00:00
|
|
|
headBlock, safeBlock, finalizedBlock: Eth2Digest,
|
2021-11-10 11:41:02 +00:00
|
|
|
timestamp: uint64,
|
|
|
|
randomData: array[32, byte],
|
2021-12-07 14:30:16 +00:00
|
|
|
suggestedFeeRecipient: Eth1Address):
|
2021-11-09 02:43:51 +00:00
|
|
|
Future[engine_api.ForkchoiceUpdatedResponse] =
|
2022-03-25 11:40:10 +00:00
|
|
|
# Eth1 monitor can recycle connections without (external) warning; at least,
|
|
|
|
# don't crash.
|
2022-04-26 09:25:19 +00:00
|
|
|
if p.isNil or p.dataProvider.isNil:
|
2022-08-25 18:07:29 +00:00
|
|
|
let fcuR =
|
|
|
|
newFuture[engine_api.ForkchoiceUpdatedResponse]("forkchoiceUpdated")
|
2022-03-25 11:40:10 +00:00
|
|
|
fcuR.complete(engine_api.ForkchoiceUpdatedResponse(
|
|
|
|
payloadStatus: PayloadStatusV1(status: PayloadExecutionStatus.syncing)))
|
|
|
|
return fcuR
|
|
|
|
|
2022-04-05 08:40:59 +00:00
|
|
|
p.dataProvider.web3.provider.engine_forkchoiceUpdatedV1(
|
2021-11-09 02:43:51 +00:00
|
|
|
ForkchoiceStateV1(
|
|
|
|
headBlockHash: headBlock.asBlockHash,
|
2022-08-25 23:34:02 +00:00
|
|
|
safeBlockHash: safeBlock.asBlockHash,
|
2021-11-09 02:43:51 +00:00
|
|
|
finalizedBlockHash: finalizedBlock.asBlockHash),
|
2021-11-10 11:41:02 +00:00
|
|
|
some(engine_api.PayloadAttributesV1(
|
|
|
|
timestamp: Quantity timestamp,
|
2022-02-24 17:01:48 +00:00
|
|
|
prevRandao: FixedBytes[32] randomData,
|
2021-12-07 14:30:16 +00:00
|
|
|
suggestedFeeRecipient: suggestedFeeRecipient)))
|
2021-05-20 10:44:13 +00:00
|
|
|
|
2022-06-15 02:38:27 +00:00
|
|
|
# TODO can't be defined within exchangeTransitionConfiguration
|
|
|
|
proc `==`(x, y: Quantity): bool {.borrow, noSideEffect.}
|
|
|
|
|
2022-08-22 19:44:40 +00:00
|
|
|
type
|
|
|
|
EtcStatus {.pure.} = enum
|
|
|
|
exchangeError
|
|
|
|
mismatch
|
|
|
|
match
|
|
|
|
|
|
|
|
proc exchangeTransitionConfiguration*(p: Eth1Monitor): Future[EtcStatus] {.async.} =
|
2022-06-15 02:38:27 +00:00
|
|
|
# Eth1 monitor can recycle connections without (external) warning; at least,
|
|
|
|
# don't crash.
|
|
|
|
if p.isNil:
|
|
|
|
debug "exchangeTransitionConfiguration: nil Eth1Monitor"
|
2022-09-08 07:36:53 +00:00
|
|
|
return EtcStatus.exchangeError
|
2022-06-15 02:38:27 +00:00
|
|
|
|
2022-09-08 07:36:53 +00:00
|
|
|
let dataProvider = p.dataProvider
|
|
|
|
if dataProvider.isNil:
|
2022-08-22 19:44:40 +00:00
|
|
|
return EtcStatus.exchangeError
|
2022-06-15 02:38:27 +00:00
|
|
|
|
2022-09-16 13:33:22 +00:00
|
|
|
# https://github.com/ethereum/execution-apis/blob/v1.0.0-beta.1/src/engine/specification.md#engine_exchangetransitionconfigurationv1
|
2022-08-22 19:44:40 +00:00
|
|
|
let consensusCfg = TransitionConfigurationV1(
|
2022-06-15 02:38:27 +00:00
|
|
|
terminalTotalDifficulty: p.depositsChain.cfg.TERMINAL_TOTAL_DIFFICULTY,
|
2022-09-16 13:33:22 +00:00
|
|
|
terminalBlockHash: p.depositsChain.cfg.TERMINAL_BLOCK_HASH,
|
|
|
|
terminalBlockNumber: Quantity 0)
|
2022-08-22 19:44:40 +00:00
|
|
|
let executionCfg =
|
2022-07-25 19:23:02 +00:00
|
|
|
try:
|
|
|
|
awaitWithRetries(
|
2022-09-08 07:36:53 +00:00
|
|
|
dataProvider.web3.provider.engine_exchangeTransitionConfigurationV1(
|
2022-08-22 19:44:40 +00:00
|
|
|
consensusCfg),
|
2022-07-25 19:23:02 +00:00
|
|
|
timeout = 1.seconds)
|
|
|
|
except CatchableError as err:
|
2022-09-16 13:33:22 +00:00
|
|
|
warn "Failed to exchange transition configuration", err = err.msg
|
2022-08-22 19:44:40 +00:00
|
|
|
return EtcStatus.exchangeError
|
|
|
|
|
2022-09-16 13:33:22 +00:00
|
|
|
return
|
|
|
|
if consensusCfg.terminalTotalDifficulty != executionCfg.terminalTotalDifficulty:
|
|
|
|
error "Engine API configured with different terminal total difficulty",
|
|
|
|
engineAPI_value = executionCfg.terminalTotalDifficulty,
|
|
|
|
localValue = consensusCfg.terminalTotalDifficulty
|
|
|
|
EtcStatus.mismatch
|
|
|
|
elif consensusCfg.terminalBlockNumber != executionCfg.terminalBlockNumber:
|
2022-08-22 19:44:40 +00:00
|
|
|
warn "Engine API reporting different terminal block number",
|
|
|
|
engineAPI_value = executionCfg.terminalBlockNumber.uint64,
|
|
|
|
localValue = consensusCfg.terminalBlockNumber.uint64
|
2022-09-16 13:33:22 +00:00
|
|
|
EtcStatus.mismatch
|
|
|
|
elif consensusCfg.terminalBlockHash != executionCfg.terminalBlockHash:
|
2022-08-22 19:44:40 +00:00
|
|
|
warn "Engine API reporting different terminal block hash",
|
|
|
|
engineAPI_value = executionCfg.terminalBlockHash,
|
|
|
|
localValue = consensusCfg.terminalBlockHash
|
2022-09-16 13:33:22 +00:00
|
|
|
EtcStatus.mismatch
|
|
|
|
else:
|
|
|
|
if not p.exchangedConfiguration:
|
|
|
|
# Log successful engine configuration exchange once at startup
|
|
|
|
p.exchangedConfiguration = true
|
|
|
|
info "Exchanged engine configuration",
|
|
|
|
terminalTotalDifficulty = executionCfg.terminalTotalDifficulty,
|
|
|
|
terminalBlockHash = executionCfg.terminalBlockHash,
|
|
|
|
terminalBlockNumber = executionCfg.terminalBlockNumber.uint64
|
|
|
|
EtcStatus.match
|
2022-06-15 02:38:27 +00:00
|
|
|
|
2020-11-03 01:21:07 +00:00
|
|
|
template readJsonField(j: JsonNode, fieldName: string, ValueType: type): untyped =
|
2020-06-27 12:01:19 +00:00
|
|
|
var res: ValueType
|
|
|
|
fromJson(j[fieldName], fieldName, res)
|
|
|
|
res
|
|
|
|
|
2021-10-14 12:33:55 +00:00
|
|
|
template init[N: static int](T: type DynamicBytes[N, N]): T =
|
|
|
|
T newSeq[byte](N)
|
|
|
|
|
2022-08-10 12:31:10 +00:00
|
|
|
proc fetchTimestampWithRetries(blkParam: Eth1Block, p: Web3DataProviderRef) {.async.} =
|
|
|
|
let blk = blkParam
|
|
|
|
let web3block = awaitWithRetries(
|
|
|
|
p.getBlockByHash(blk.hash.asBlockHash))
|
|
|
|
blk.timestamp = Eth1BlockTimestamp web3block.timestamp
|
|
|
|
|
2021-11-25 10:53:31 +00:00
|
|
|
func depositEventsToBlocks(depositsList: JsonNode): seq[Eth1Block] {.
|
2021-03-26 06:52:01 +00:00
|
|
|
raises: [Defect, CatchableError].} =
|
2020-06-27 12:01:19 +00:00
|
|
|
if depositsList.kind != JArray:
|
|
|
|
raise newException(CatchableError,
|
|
|
|
"Web3 provider didn't return a list of deposit events")
|
|
|
|
|
|
|
|
var lastEth1Block: Eth1Block
|
|
|
|
|
|
|
|
for logEvent in depositsList:
|
|
|
|
let
|
|
|
|
blockNumber = Eth1BlockNumber readJsonField(logEvent, "blockNumber", Quantity)
|
|
|
|
blockHash = readJsonField(logEvent, "blockHash", BlockHash)
|
|
|
|
logData = strip0xPrefix(logEvent["data"].getStr)
|
|
|
|
|
|
|
|
if lastEth1Block == nil or lastEth1Block.number != blockNumber:
|
|
|
|
lastEth1Block = Eth1Block(
|
2022-08-10 12:31:10 +00:00
|
|
|
hash: blockHash.asEth2Digest,
|
|
|
|
number: blockNumber
|
|
|
|
# The `timestamp` is set in `syncBlockRange` immediately
|
|
|
|
# after calling this function, because we don't want to
|
|
|
|
# make this function `async`
|
|
|
|
)
|
2020-06-27 12:01:19 +00:00
|
|
|
|
|
|
|
result.add lastEth1Block
|
|
|
|
|
|
|
|
var
|
2021-10-14 12:33:55 +00:00
|
|
|
pubkey = init PubKeyBytes
|
|
|
|
withdrawalCredentials = init WithdrawalCredentialsBytes
|
|
|
|
amount = init Int64LeBytes
|
|
|
|
signature = init SignatureBytes
|
|
|
|
index = init Int64LeBytes
|
2020-06-27 12:01:19 +00:00
|
|
|
|
|
|
|
var offset = 0
|
|
|
|
offset += decode(logData, offset, pubkey)
|
|
|
|
offset += decode(logData, offset, withdrawalCredentials)
|
|
|
|
offset += decode(logData, offset, amount)
|
|
|
|
offset += decode(logData, offset, signature)
|
|
|
|
offset += decode(logData, offset, index)
|
|
|
|
|
2021-10-14 12:33:55 +00:00
|
|
|
if pubkey.len != 48 or
|
|
|
|
withdrawalCredentials.len != 32 or
|
|
|
|
amount.len != 8 or
|
|
|
|
signature.len != 96 or
|
|
|
|
index.len != 8:
|
|
|
|
raise newException(CorruptDataProvider, "Web3 provider supplied invalid deposit logs")
|
|
|
|
|
2020-11-24 21:21:47 +00:00
|
|
|
lastEth1Block.deposits.add DepositData(
|
2021-10-14 12:33:55 +00:00
|
|
|
pubkey: ValidatorPubKey.init(pubkey.toArray),
|
|
|
|
withdrawal_credentials: Eth2Digest(data: withdrawalCredentials.toArray),
|
|
|
|
amount: bytes_to_uint64(amount.toArray),
|
|
|
|
signature: ValidatorSig.init(signature.toArray))
|
2020-06-27 12:01:19 +00:00
|
|
|
|
2020-12-01 11:14:32 +00:00
|
|
|
type
|
|
|
|
DepositContractDataStatus = enum
|
|
|
|
Fetched
|
|
|
|
VerifiedCorrect
|
|
|
|
DepositRootIncorrect
|
|
|
|
DepositRootUnavailable
|
|
|
|
DepositCountIncorrect
|
|
|
|
DepositCountUnavailable
|
2020-11-03 01:21:07 +00:00
|
|
|
|
2022-07-25 19:23:02 +00:00
|
|
|
template awaitOrRaiseOnTimeout[T](fut: Future[T],
|
|
|
|
timeout: Duration): T =
|
|
|
|
awaitWithTimeout(fut, timeout):
|
|
|
|
raise newException(DataProviderTimeout, "Timeout")
|
|
|
|
|
2020-12-01 11:14:32 +00:00
|
|
|
when hasDepositRootChecks:
|
2020-11-24 21:21:47 +00:00
|
|
|
const
|
2022-07-25 19:23:02 +00:00
|
|
|
contractCallTimeout = 60.seconds
|
2020-11-24 21:21:47 +00:00
|
|
|
|
2022-08-09 21:32:34 +00:00
|
|
|
proc fetchDepositContractData(p: Web3DataProviderRef, blk: Eth1Block):
|
2020-11-03 01:21:07 +00:00
|
|
|
Future[DepositContractDataStatus] {.async.} =
|
|
|
|
let
|
|
|
|
depositRoot = p.ns.get_deposit_root.call(blockNumber = blk.number)
|
|
|
|
rawCount = p.ns.get_deposit_count.call(blockNumber = blk.number)
|
2020-10-21 13:25:53 +00:00
|
|
|
|
2020-11-03 01:21:07 +00:00
|
|
|
try:
|
2020-11-24 21:21:47 +00:00
|
|
|
let fetchedRoot = asEth2Digest(
|
|
|
|
awaitOrRaiseOnTimeout(depositRoot, contractCallTimeout))
|
2022-08-10 12:31:10 +00:00
|
|
|
if blk.depositRoot.isZero:
|
|
|
|
blk.depositRoot = fetchedRoot
|
2020-11-03 01:21:07 +00:00
|
|
|
result = Fetched
|
2022-08-10 12:31:10 +00:00
|
|
|
elif blk.depositRoot == fetchedRoot:
|
2020-11-03 01:21:07 +00:00
|
|
|
result = VerifiedCorrect
|
|
|
|
else:
|
|
|
|
result = DepositRootIncorrect
|
|
|
|
except CatchableError as err:
|
|
|
|
debug "Failed to fetch deposits root",
|
|
|
|
blockNumber = blk.number,
|
|
|
|
err = err.msg
|
|
|
|
result = DepositRootUnavailable
|
2020-10-21 13:25:53 +00:00
|
|
|
|
2020-11-03 01:21:07 +00:00
|
|
|
try:
|
2022-08-09 21:32:34 +00:00
|
|
|
let fetchedCount = bytes_to_uint64(
|
|
|
|
awaitOrRaiseOnTimeout(rawCount, contractCallTimeout).toArray)
|
2022-08-10 12:31:10 +00:00
|
|
|
if blk.depositCount == 0:
|
|
|
|
blk.depositCount = fetchedCount
|
|
|
|
elif blk.depositCount != fetchedCount:
|
2020-11-03 01:21:07 +00:00
|
|
|
result = DepositCountIncorrect
|
|
|
|
except CatchableError as err:
|
|
|
|
debug "Failed to fetch deposits count",
|
|
|
|
blockNumber = blk.number,
|
|
|
|
err = err.msg
|
|
|
|
result = DepositCountUnavailable
|
2020-06-27 12:01:19 +00:00
|
|
|
|
2021-12-05 17:32:41 +00:00
|
|
|
proc onBlockHeaders(p: Web3DataProviderRef,
|
|
|
|
blockHeaderHandler: BlockHeaderHandler,
|
|
|
|
errorHandler: SubscriptionErrorHandler) {.async.} =
|
2020-10-12 01:07:20 +00:00
|
|
|
info "Waiting for new Eth1 block headers"
|
2020-06-27 12:01:19 +00:00
|
|
|
|
2020-12-01 21:20:28 +00:00
|
|
|
p.blockHeadersSubscription = awaitWithRetries(
|
|
|
|
p.web3.subscribeForBlockHeaders(blockHeaderHandler, errorHandler))
|
2020-06-27 12:01:19 +00:00
|
|
|
|
2020-12-03 04:30:35 +00:00
|
|
|
proc pruneOldBlocks(chain: var Eth1Chain, depositIndex: uint64) =
|
|
|
|
let initialChunks = chain.finalizedDepositsMerkleizer.getChunkCount
|
2020-11-24 21:21:47 +00:00
|
|
|
var lastBlock: Eth1Block
|
|
|
|
|
2020-12-03 04:30:35 +00:00
|
|
|
while chain.blocks.len > 0:
|
|
|
|
let blk = chain.blocks.peekFirst
|
2022-08-10 12:31:10 +00:00
|
|
|
if blk.depositCount >= depositIndex:
|
2020-11-24 21:21:47 +00:00
|
|
|
break
|
|
|
|
else:
|
|
|
|
for deposit in blk.deposits:
|
2020-12-03 04:30:35 +00:00
|
|
|
chain.finalizedDepositsMerkleizer.addChunk hash_tree_root(deposit).data
|
|
|
|
chain.popFirst()
|
2020-11-24 21:21:47 +00:00
|
|
|
lastBlock = blk
|
|
|
|
|
2020-12-03 04:30:35 +00:00
|
|
|
if chain.finalizedDepositsMerkleizer.getChunkCount > initialChunks:
|
2022-08-10 12:31:10 +00:00
|
|
|
chain.finalizedBlockHash = lastBlock.hash
|
2020-12-03 04:30:35 +00:00
|
|
|
chain.db.putEth2FinalizedTo DepositContractSnapshot(
|
2022-08-10 12:31:10 +00:00
|
|
|
eth1Block: lastBlock.hash,
|
2020-12-03 04:30:35 +00:00
|
|
|
depositContractState: chain.finalizedDepositsMerkleizer.toDepositContractState)
|
2020-11-24 21:21:47 +00:00
|
|
|
|
2020-12-09 22:44:59 +00:00
|
|
|
eth1_finalized_head.set lastBlock.number.toGaugeValue
|
2022-08-10 12:31:10 +00:00
|
|
|
eth1_finalized_deposits.set lastBlock.depositCount.toGaugeValue
|
2020-12-09 22:44:59 +00:00
|
|
|
|
2020-11-30 23:59:35 +00:00
|
|
|
debug "Eth1 blocks pruned",
|
2022-08-10 12:31:10 +00:00
|
|
|
newTailBlock = lastBlock.hash,
|
|
|
|
depositsCount = lastBlock.depositCount
|
2020-11-30 23:59:35 +00:00
|
|
|
|
2021-11-25 10:53:31 +00:00
|
|
|
func advanceMerkleizer(chain: Eth1Chain,
|
2020-11-24 21:21:47 +00:00
|
|
|
merkleizer: var DepositsMerkleizer,
|
|
|
|
depositIndex: uint64): bool =
|
2020-12-03 04:30:35 +00:00
|
|
|
if chain.blocks.len == 0:
|
2020-11-24 21:21:47 +00:00
|
|
|
return depositIndex == merkleizer.getChunkCount
|
|
|
|
|
2022-08-10 12:31:10 +00:00
|
|
|
if chain.blocks.peekLast.depositCount < depositIndex:
|
2020-11-24 21:21:47 +00:00
|
|
|
return false
|
|
|
|
|
|
|
|
let
|
2020-12-03 04:30:35 +00:00
|
|
|
firstBlock = chain.blocks[0]
|
2022-08-10 12:31:10 +00:00
|
|
|
depositsInLastPrunedBlock = firstBlock.depositCount -
|
2020-11-24 21:21:47 +00:00
|
|
|
firstBlock.deposits.lenu64
|
|
|
|
|
|
|
|
# advanceMerkleizer should always be called shortly after prunning the chain
|
|
|
|
doAssert depositsInLastPrunedBlock == merkleizer.getChunkCount
|
|
|
|
|
2020-12-03 04:30:35 +00:00
|
|
|
for blk in chain.blocks:
|
2020-11-24 21:21:47 +00:00
|
|
|
for deposit in blk.deposits:
|
|
|
|
if merkleizer.getChunkCount < depositIndex:
|
|
|
|
merkleizer.addChunk hash_tree_root(deposit).data
|
|
|
|
else:
|
|
|
|
return true
|
|
|
|
|
|
|
|
return merkleizer.getChunkCount == depositIndex
|
|
|
|
|
2022-08-12 13:52:06 +00:00
|
|
|
iterator getDepositsRange*(chain: Eth1Chain, first, last: uint64): DepositData =
|
2020-11-24 21:21:47 +00:00
|
|
|
# TODO It's possible to make this faster by performing binary search that
|
|
|
|
# will locate the blocks holding the `first` and `last` indices.
|
|
|
|
# TODO There is an assumption here that the requested range will be present
|
2022-08-12 13:52:06 +00:00
|
|
|
# in the Eth1Chain. This should hold true at the call sites right now,
|
|
|
|
# but we need to guard the pre-conditions better.
|
2020-12-03 04:30:35 +00:00
|
|
|
for blk in chain.blocks:
|
2022-08-10 12:31:10 +00:00
|
|
|
if blk.depositCount <= first:
|
2020-11-24 21:21:47 +00:00
|
|
|
continue
|
|
|
|
|
2022-08-10 12:31:10 +00:00
|
|
|
let firstDepositIdxInBlk = blk.depositCount - blk.deposits.lenu64
|
2020-11-24 21:21:47 +00:00
|
|
|
if firstDepositIdxInBlk >= last:
|
2022-08-12 13:52:06 +00:00
|
|
|
break
|
2020-11-24 21:21:47 +00:00
|
|
|
|
|
|
|
for i in 0 ..< blk.deposits.lenu64:
|
|
|
|
let globalIdx = firstDepositIdxInBlk + i
|
|
|
|
if globalIdx >= first and globalIdx < last:
|
2022-08-12 13:52:06 +00:00
|
|
|
yield blk.deposits[i]
|
2020-11-24 21:21:47 +00:00
|
|
|
|
2021-11-25 10:53:31 +00:00
|
|
|
func lowerBound(chain: Eth1Chain, depositCount: uint64): Eth1Block =
|
2020-11-30 23:59:35 +00:00
|
|
|
# TODO: This can be replaced with a proper binary search in the
|
|
|
|
# future, but the `algorithm` module currently requires an
|
|
|
|
# `openArray`, which the `deques` module can't provide yet.
|
|
|
|
for eth1Block in chain.blocks:
|
2022-08-10 12:31:10 +00:00
|
|
|
if eth1Block.depositCount > depositCount:
|
2020-11-30 23:59:35 +00:00
|
|
|
return
|
|
|
|
result = eth1Block
|
|
|
|
|
2021-12-05 17:32:41 +00:00
|
|
|
proc trackFinalizedState(chain: var Eth1Chain,
|
|
|
|
finalizedEth1Data: Eth1Data,
|
2022-01-31 17:28:26 +00:00
|
|
|
finalizedStateDepositIndex: uint64,
|
|
|
|
blockProposalExpected = false): bool =
|
2022-08-10 12:31:10 +00:00
|
|
|
## This function will return true if the Eth1Monitor is synced
|
|
|
|
## to the finalization point.
|
|
|
|
|
2020-12-03 04:30:35 +00:00
|
|
|
if chain.blocks.len == 0:
|
2020-11-30 23:59:35 +00:00
|
|
|
debug "Eth1 chain not initialized"
|
|
|
|
return false
|
|
|
|
|
2020-12-03 04:30:35 +00:00
|
|
|
let latest = chain.blocks.peekLast
|
2022-08-10 12:31:10 +00:00
|
|
|
if latest.depositCount < finalizedEth1Data.deposit_count:
|
2022-01-31 17:28:26 +00:00
|
|
|
if blockProposalExpected:
|
|
|
|
error "The Eth1 chain is not synced",
|
2022-08-10 12:31:10 +00:00
|
|
|
ourDepositsCount = latest.depositCount,
|
2022-01-31 17:28:26 +00:00
|
|
|
targetDepositsCount = finalizedEth1Data.deposit_count
|
2020-11-30 23:59:35 +00:00
|
|
|
return false
|
|
|
|
|
2020-12-03 04:30:35 +00:00
|
|
|
let matchingBlock = chain.lowerBound(finalizedEth1Data.deposit_count)
|
2020-11-30 23:59:35 +00:00
|
|
|
result = if matchingBlock != nil:
|
2022-08-10 12:31:10 +00:00
|
|
|
if matchingBlock.depositRoot == finalizedEth1Data.deposit_root:
|
2020-11-24 21:21:47 +00:00
|
|
|
true
|
|
|
|
else:
|
|
|
|
error "Corrupted deposits history detected",
|
2022-08-10 12:31:10 +00:00
|
|
|
ourDepositsCount = matchingBlock.depositCount,
|
2020-12-03 04:30:35 +00:00
|
|
|
taretDepositsCount = finalizedEth1Data.deposit_count,
|
2022-08-10 12:31:10 +00:00
|
|
|
ourDepositsRoot = matchingBlock.depositRoot,
|
2020-12-03 04:30:35 +00:00
|
|
|
targetDepositsRoot = finalizedEth1Data.deposit_root
|
2021-01-29 21:21:44 +00:00
|
|
|
chain.hasConsensusViolation = true
|
2020-11-04 09:06:58 +00:00
|
|
|
false
|
|
|
|
else:
|
2020-11-30 23:59:35 +00:00
|
|
|
error "The Eth1 chain is in inconsistent state",
|
2020-11-24 21:21:47 +00:00
|
|
|
checkpointHash = finalizedEth1Data.block_hash,
|
|
|
|
checkpointDeposits = finalizedEth1Data.deposit_count,
|
2020-12-03 04:30:35 +00:00
|
|
|
localChainStart = shortLog(chain.blocks.peekFirst),
|
|
|
|
localChainEnd = shortLog(chain.blocks.peekLast)
|
2021-01-29 21:21:44 +00:00
|
|
|
chain.hasConsensusViolation = true
|
2020-11-04 09:06:58 +00:00
|
|
|
false
|
2020-10-26 08:55:10 +00:00
|
|
|
|
2020-11-30 23:59:35 +00:00
|
|
|
if result:
|
2020-12-03 04:30:35 +00:00
|
|
|
chain.pruneOldBlocks(finalizedStateDepositIndex)
|
|
|
|
|
|
|
|
template trackFinalizedState*(m: Eth1Monitor,
|
|
|
|
finalizedEth1Data: Eth1Data,
|
|
|
|
finalizedStateDepositIndex: uint64): bool =
|
2021-11-15 12:01:47 +00:00
|
|
|
trackFinalizedState(m.depositsChain, finalizedEth1Data, finalizedStateDepositIndex)
|
2020-11-24 21:21:47 +00:00
|
|
|
|
2022-11-08 02:37:28 +00:00
|
|
|
# https://github.com/ethereum/consensus-specs/blob/v1.3.0-alpha.0/specs/phase0/validator.md#get_eth1_data
|
2020-12-03 04:30:35 +00:00
|
|
|
proc getBlockProposalData*(chain: var Eth1Chain,
|
2021-06-11 17:51:46 +00:00
|
|
|
state: ForkedHashedBeaconState,
|
2020-11-30 23:59:35 +00:00
|
|
|
finalizedEth1Data: Eth1Data,
|
|
|
|
finalizedStateDepositIndex: uint64): BlockProposalEth1Data =
|
|
|
|
let
|
|
|
|
periodStart = voting_period_start_time(state)
|
2020-12-03 04:30:35 +00:00
|
|
|
hasLatestDeposits = chain.trackFinalizedState(finalizedEth1Data,
|
2022-01-31 17:28:26 +00:00
|
|
|
finalizedStateDepositIndex,
|
|
|
|
blockProposalExpected = true)
|
2020-03-24 11:13:07 +00:00
|
|
|
|
2020-11-19 17:19:03 +00:00
|
|
|
var otherVotesCountTable = initCountTable[Eth1Data]()
|
2021-05-21 09:23:28 +00:00
|
|
|
for vote in getStateField(state, eth1_data_votes):
|
2020-12-03 04:30:35 +00:00
|
|
|
let eth1Block = chain.findBlock(vote)
|
2021-04-30 23:00:03 +00:00
|
|
|
if eth1Block != nil and
|
2022-08-10 12:31:10 +00:00
|
|
|
eth1Block.depositRoot == vote.deposit_root and
|
2021-05-21 09:23:28 +00:00
|
|
|
vote.deposit_count >= getStateField(state, eth1_data).deposit_count and
|
Implement split preset/config support (#2710)
* Implement split preset/config support
This is the initial bulk refactor to introduce runtime config values in
a number of places, somewhat replacing the existing mechanism of loading
network metadata.
It still needs more work, this is the initial refactor that introduces
runtime configuration in some of the places that need it.
The PR changes the way presets and constants work, to match the spec. In
particular, a "preset" now refers to the compile-time configuration
while a "cfg" or "RuntimeConfig" is the dynamic part.
A single binary can support either mainnet or minimal, but not both.
Support for other presets has been removed completely (can be readded,
in case there's need).
There's a number of outstanding tasks:
* `SECONDS_PER_SLOT` still needs fixing
* loading custom runtime configs needs redoing
* checking constants against YAML file
* yeerongpilly support
`build/nimbus_beacon_node --network=yeerongpilly --discv5:no --log-level=DEBUG`
* load fork epoch from config
* fix fork digest sent in status
* nicer error string for request failures
* fix tools
* one more
* fixup
* fixup
* fixup
* use "standard" network definition folder in local testnet
Files are loaded from their standard locations, including genesis etc,
to conform to the format used in the `eth2-networks` repo.
* fix launch scripts, allow unknown config values
* fix base config of rest test
* cleanups
* bundle mainnet config using common loader
* fix spec links and names
* only include supported preset in binary
* drop yeerongpilly, add altair-devnet-0, support boot_enr.yaml
2021-07-12 13:01:38 +00:00
|
|
|
is_candidate_block(chain.cfg, eth1Block, periodStart):
|
2020-11-19 17:19:03 +00:00
|
|
|
otherVotesCountTable.inc vote
|
2020-11-20 14:05:37 +00:00
|
|
|
else:
|
2021-04-30 23:00:03 +00:00
|
|
|
debug "Ignoring eth1 vote",
|
|
|
|
root = vote.block_hash,
|
|
|
|
deposits = vote.deposit_count,
|
|
|
|
depositsRoot = vote.deposit_root,
|
2021-05-21 09:23:28 +00:00
|
|
|
localDeposits = getStateField(state, eth1_data).deposit_count
|
2020-03-24 11:13:07 +00:00
|
|
|
|
2022-04-27 09:39:41 +00:00
|
|
|
let
|
|
|
|
stateDepositIdx = getStateField(state, eth1_deposit_index)
|
|
|
|
stateDepositsCount = getStateField(state, eth1_data).deposit_count
|
|
|
|
|
|
|
|
# A valid state should never have this condition, but it doesn't hurt
|
|
|
|
# to be extra defensive here because we are working with uint types
|
|
|
|
var pendingDepositsCount = if stateDepositsCount > stateDepositIdx:
|
|
|
|
stateDepositsCount - stateDepositIdx
|
|
|
|
else:
|
|
|
|
0
|
|
|
|
|
2020-03-24 11:13:07 +00:00
|
|
|
if otherVotesCountTable.len > 0:
|
2020-11-19 17:19:03 +00:00
|
|
|
let (winningVote, votes) = otherVotesCountTable.largest
|
2020-11-20 14:05:37 +00:00
|
|
|
debug "Voting on eth1 head with majority", votes
|
2020-11-19 17:19:03 +00:00
|
|
|
result.vote = winningVote
|
2020-10-12 01:07:20 +00:00
|
|
|
if uint64((votes + 1) * 2) > SLOTS_PER_ETH1_VOTING_PERIOD:
|
2022-04-27 09:39:41 +00:00
|
|
|
pendingDepositsCount = winningVote.deposit_count - stateDepositIdx
|
|
|
|
|
2020-03-24 11:13:07 +00:00
|
|
|
else:
|
2020-12-03 04:30:35 +00:00
|
|
|
let latestBlock = chain.latestCandidateBlock(periodStart)
|
2020-10-12 01:07:20 +00:00
|
|
|
if latestBlock == nil:
|
2020-11-20 14:05:37 +00:00
|
|
|
debug "No acceptable eth1 votes and no recent candidates. Voting no change"
|
2021-05-21 09:23:28 +00:00
|
|
|
result.vote = getStateField(state, eth1_data)
|
2020-10-12 01:07:20 +00:00
|
|
|
else:
|
2020-11-20 14:05:37 +00:00
|
|
|
debug "No acceptable eth1 votes. Voting for latest candidate"
|
2022-08-10 12:31:10 +00:00
|
|
|
result.vote = latestBlock.toVoteData
|
2020-10-15 17:30:33 +00:00
|
|
|
|
2020-11-24 21:21:47 +00:00
|
|
|
if pendingDepositsCount > 0:
|
|
|
|
if hasLatestDeposits:
|
2020-12-02 18:15:36 +00:00
|
|
|
let
|
|
|
|
totalDepositsInNewBlock = min(MAX_DEPOSITS, pendingDepositsCount)
|
2022-04-27 09:39:41 +00:00
|
|
|
postStateDepositIdx = stateDepositIdx + pendingDepositsCount
|
2022-08-12 13:52:06 +00:00
|
|
|
var
|
|
|
|
deposits = newSeqOfCap[DepositData](totalDepositsInNewBlock)
|
|
|
|
depositRoots = newSeqOfCap[Eth2Digest](pendingDepositsCount)
|
|
|
|
for data in chain.getDepositsRange(stateDepositIdx, postStateDepositIdx):
|
|
|
|
if deposits.lenu64 < totalDepositsInNewBlock:
|
|
|
|
deposits.add data
|
|
|
|
depositRoots.add hash_tree_root(data)
|
2020-11-24 21:21:47 +00:00
|
|
|
|
2020-12-03 04:30:35 +00:00
|
|
|
var scratchMerkleizer = copy chain.finalizedDepositsMerkleizer
|
2022-04-27 09:39:41 +00:00
|
|
|
if chain.advanceMerkleizer(scratchMerkleizer, stateDepositIdx):
|
2020-11-24 21:21:47 +00:00
|
|
|
let proofs = scratchMerkleizer.addChunksAndGenMerkleProofs(depositRoots)
|
2020-12-02 18:15:36 +00:00
|
|
|
for i in 0 ..< totalDepositsInNewBlock:
|
|
|
|
var proof: array[33, Eth2Digest]
|
|
|
|
proof[0..31] = proofs.getProof(i.int)
|
|
|
|
proof[32] = default(Eth2Digest)
|
2022-04-27 09:39:41 +00:00
|
|
|
proof[32].data[0..7] = toBytesLE uint64(postStateDepositIdx)
|
2020-12-02 18:15:36 +00:00
|
|
|
result.deposits.add Deposit(data: deposits[i], proof: proof)
|
2020-11-24 21:21:47 +00:00
|
|
|
else:
|
|
|
|
error "The Eth1 chain is in inconsistent state" # This should not really happen
|
|
|
|
result.hasMissingDeposits = true
|
|
|
|
else:
|
|
|
|
result.hasMissingDeposits = true
|
2020-03-24 11:13:07 +00:00
|
|
|
|
2020-12-03 04:30:35 +00:00
|
|
|
template getBlockProposalData*(m: Eth1Monitor,
|
2021-06-11 17:51:46 +00:00
|
|
|
state: ForkedHashedBeaconState,
|
2020-12-03 04:30:35 +00:00
|
|
|
finalizedEth1Data: Eth1Data,
|
2022-03-31 14:43:05 +00:00
|
|
|
finalizedStateDepositIndex: uint64):
|
|
|
|
BlockProposalEth1Data =
|
|
|
|
getBlockProposalData(
|
|
|
|
m.depositsChain, state, finalizedEth1Data, finalizedStateDepositIndex)
|
|
|
|
|
|
|
|
proc getJsonRpcRequestHeaders(jwtSecret: Option[seq[byte]]):
|
|
|
|
auto =
|
|
|
|
if jwtSecret.isSome:
|
|
|
|
let secret = jwtSecret.get
|
|
|
|
(proc(): seq[(string, string)] =
|
|
|
|
# https://www.rfc-editor.org/rfc/rfc6750#section-6.1.1
|
|
|
|
@[("Authorization", "Bearer " & getSignedIatToken(
|
|
|
|
secret, (getTime() - initTime(0, 0)).inSeconds))])
|
|
|
|
else:
|
|
|
|
(proc(): seq[(string, string)] = @[])
|
2020-12-03 04:30:35 +00:00
|
|
|
|
2021-10-13 14:08:50 +00:00
|
|
|
proc new*(T: type Web3DataProvider,
|
|
|
|
depositContractAddress: Eth1Address,
|
2022-03-31 14:43:05 +00:00
|
|
|
web3Url: string,
|
|
|
|
jwtSecret: Option[seq[byte]]):
|
|
|
|
Future[Result[Web3DataProviderRef, string]] {.async.} =
|
|
|
|
let web3Fut = newWeb3(web3Url, getJsonRpcRequestHeaders(jwtSecret))
|
2022-07-25 19:23:02 +00:00
|
|
|
yield web3Fut or sleepAsync(10.seconds)
|
2020-11-17 19:50:07 +00:00
|
|
|
if (not web3Fut.finished) or web3Fut.failed:
|
|
|
|
await cancelAndWait(web3Fut)
|
2022-01-08 13:35:36 +00:00
|
|
|
if web3Fut.failed:
|
|
|
|
return err "Failed to setup web3 connection: " & web3Fut.readError.msg
|
|
|
|
else:
|
|
|
|
return err "Failed to setup web3 connection"
|
2020-11-17 19:50:07 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
web3 = web3Fut.read
|
|
|
|
ns = web3.contractSender(DepositContract, depositContractAddress)
|
|
|
|
|
|
|
|
return ok Web3DataProviderRef(url: web3Url, web3: web3, ns: ns)
|
|
|
|
|
2020-12-03 04:30:35 +00:00
|
|
|
proc putInitialDepositContractSnapshot*(db: BeaconChainDB,
|
|
|
|
s: DepositContractSnapshot) =
|
|
|
|
let existingStart = db.getEth2FinalizedTo()
|
|
|
|
if not existingStart.isOk:
|
|
|
|
db.putEth2FinalizedTo(s)
|
|
|
|
|
|
|
|
template getOrDefault[T, E](r: Result[T, E]): T =
|
|
|
|
type TT = T
|
|
|
|
get(r, default(TT))
|
|
|
|
|
Implement split preset/config support (#2710)
* Implement split preset/config support
This is the initial bulk refactor to introduce runtime config values in
a number of places, somewhat replacing the existing mechanism of loading
network metadata.
It still needs more work, this is the initial refactor that introduces
runtime configuration in some of the places that need it.
The PR changes the way presets and constants work, to match the spec. In
particular, a "preset" now refers to the compile-time configuration
while a "cfg" or "RuntimeConfig" is the dynamic part.
A single binary can support either mainnet or minimal, but not both.
Support for other presets has been removed completely (can be readded,
in case there's need).
There's a number of outstanding tasks:
* `SECONDS_PER_SLOT` still needs fixing
* loading custom runtime configs needs redoing
* checking constants against YAML file
* yeerongpilly support
`build/nimbus_beacon_node --network=yeerongpilly --discv5:no --log-level=DEBUG`
* load fork epoch from config
* fix fork digest sent in status
* nicer error string for request failures
* fix tools
* one more
* fixup
* fixup
* fixup
* use "standard" network definition folder in local testnet
Files are loaded from their standard locations, including genesis etc,
to conform to the format used in the `eth2-networks` repo.
* fix launch scripts, allow unknown config values
* fix base config of rest test
* cleanups
* bundle mainnet config using common loader
* fix spec links and names
* only include supported preset in binary
* drop yeerongpilly, add altair-devnet-0, support boot_enr.yaml
2021-07-12 13:01:38 +00:00
|
|
|
proc init*(T: type Eth1Chain, cfg: RuntimeConfig, db: BeaconChainDB): T =
|
2022-06-29 16:53:59 +00:00
|
|
|
let
|
2022-07-14 04:07:40 +00:00
|
|
|
finalizedDeposits =
|
|
|
|
if db != nil:
|
|
|
|
db.getEth2FinalizedTo().getOrDefault()
|
|
|
|
else:
|
|
|
|
default(DepositContractSnapshot)
|
2022-06-29 16:53:59 +00:00
|
|
|
m = DepositsMerkleizer.init(finalizedDeposits.depositContractState)
|
2020-12-03 04:30:35 +00:00
|
|
|
|
|
|
|
T(db: db,
|
Implement split preset/config support (#2710)
* Implement split preset/config support
This is the initial bulk refactor to introduce runtime config values in
a number of places, somewhat replacing the existing mechanism of loading
network metadata.
It still needs more work, this is the initial refactor that introduces
runtime configuration in some of the places that need it.
The PR changes the way presets and constants work, to match the spec. In
particular, a "preset" now refers to the compile-time configuration
while a "cfg" or "RuntimeConfig" is the dynamic part.
A single binary can support either mainnet or minimal, but not both.
Support for other presets has been removed completely (can be readded,
in case there's need).
There's a number of outstanding tasks:
* `SECONDS_PER_SLOT` still needs fixing
* loading custom runtime configs needs redoing
* checking constants against YAML file
* yeerongpilly support
`build/nimbus_beacon_node --network=yeerongpilly --discv5:no --log-level=DEBUG`
* load fork epoch from config
* fix fork digest sent in status
* nicer error string for request failures
* fix tools
* one more
* fixup
* fixup
* fixup
* use "standard" network definition folder in local testnet
Files are loaded from their standard locations, including genesis etc,
to conform to the format used in the `eth2-networks` repo.
* fix launch scripts, allow unknown config values
* fix base config of rest test
* cleanups
* bundle mainnet config using common loader
* fix spec links and names
* only include supported preset in binary
* drop yeerongpilly, add altair-devnet-0, support boot_enr.yaml
2021-07-12 13:01:38 +00:00
|
|
|
cfg: cfg,
|
2020-12-03 04:30:35 +00:00
|
|
|
finalizedBlockHash: finalizedDeposits.eth1Block,
|
2022-08-10 12:31:10 +00:00
|
|
|
finalizedDepositsMerkleizer: m,
|
|
|
|
headMerkleizer: copy m)
|
2020-12-03 04:30:35 +00:00
|
|
|
|
2022-01-21 10:59:09 +00:00
|
|
|
proc createInitialDepositSnapshot*(
|
|
|
|
depositContractAddress: Eth1Address,
|
|
|
|
depositContractDeployedAt: BlockHashOrNumber,
|
2022-03-31 14:43:05 +00:00
|
|
|
web3Url: string,
|
|
|
|
jwtSecret: Option[seq[byte]]): Future[Result[DepositContractSnapshot, string]]
|
|
|
|
{.async.} =
|
2022-01-21 10:59:09 +00:00
|
|
|
|
|
|
|
let dataProviderRes =
|
2022-03-31 14:43:05 +00:00
|
|
|
await Web3DataProvider.new(depositContractAddress, web3Url, jwtSecret)
|
2022-01-21 10:59:09 +00:00
|
|
|
if dataProviderRes.isErr:
|
|
|
|
return err(dataProviderRes.error)
|
|
|
|
var dataProvider = dataProviderRes.get
|
|
|
|
|
|
|
|
let knownStartBlockHash =
|
|
|
|
if depositContractDeployedAt.isHash:
|
|
|
|
depositContractDeployedAt.hash
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
var blk = awaitWithRetries(
|
|
|
|
dataProvider.getBlockByNumber(depositContractDeployedAt.number))
|
|
|
|
blk.hash.asEth2Digest
|
|
|
|
except CatchableError as err:
|
|
|
|
return err(err.msg)
|
|
|
|
|
|
|
|
return ok DepositContractSnapshot(eth1Block: knownStartBlockHash)
|
|
|
|
|
2022-06-15 02:38:27 +00:00
|
|
|
proc currentEpoch(m: Eth1Monitor): Epoch =
|
|
|
|
if m.getBeaconTime != nil:
|
|
|
|
m.getBeaconTime().slotOrZero.epoch
|
|
|
|
else:
|
|
|
|
Epoch 0
|
|
|
|
|
2020-11-03 01:21:07 +00:00
|
|
|
proc init*(T: type Eth1Monitor,
|
Implement split preset/config support (#2710)
* Implement split preset/config support
This is the initial bulk refactor to introduce runtime config values in
a number of places, somewhat replacing the existing mechanism of loading
network metadata.
It still needs more work, this is the initial refactor that introduces
runtime configuration in some of the places that need it.
The PR changes the way presets and constants work, to match the spec. In
particular, a "preset" now refers to the compile-time configuration
while a "cfg" or "RuntimeConfig" is the dynamic part.
A single binary can support either mainnet or minimal, but not both.
Support for other presets has been removed completely (can be readded,
in case there's need).
There's a number of outstanding tasks:
* `SECONDS_PER_SLOT` still needs fixing
* loading custom runtime configs needs redoing
* checking constants against YAML file
* yeerongpilly support
`build/nimbus_beacon_node --network=yeerongpilly --discv5:no --log-level=DEBUG`
* load fork epoch from config
* fix fork digest sent in status
* nicer error string for request failures
* fix tools
* one more
* fixup
* fixup
* fixup
* use "standard" network definition folder in local testnet
Files are loaded from their standard locations, including genesis etc,
to conform to the format used in the `eth2-networks` repo.
* fix launch scripts, allow unknown config values
* fix base config of rest test
* cleanups
* bundle mainnet config using common loader
* fix spec links and names
* only include supported preset in binary
* drop yeerongpilly, add altair-devnet-0, support boot_enr.yaml
2021-07-12 13:01:38 +00:00
|
|
|
cfg: RuntimeConfig,
|
2020-12-03 04:30:35 +00:00
|
|
|
db: BeaconChainDB,
|
2022-03-31 14:43:05 +00:00
|
|
|
getBeaconTime: GetBeaconTimeFn,
|
2021-04-06 21:42:59 +00:00
|
|
|
web3Urls: seq[string],
|
2022-01-21 10:59:09 +00:00
|
|
|
depositContractSnapshot: Option[DepositContractSnapshot],
|
2021-11-25 16:51:51 +00:00
|
|
|
eth1Network: Option[Eth1Network],
|
2022-02-27 16:55:02 +00:00
|
|
|
forcePolling: bool,
|
2022-09-20 06:17:25 +00:00
|
|
|
jwtSecret: Option[seq[byte]],
|
|
|
|
ttdReached: bool): T =
|
2021-04-06 21:42:59 +00:00
|
|
|
doAssert web3Urls.len > 0
|
|
|
|
var web3Urls = web3Urls
|
|
|
|
for url in mitems(web3Urls):
|
|
|
|
fixupWeb3Urls url
|
2020-11-05 23:11:06 +00:00
|
|
|
|
2022-01-21 10:59:09 +00:00
|
|
|
if depositContractSnapshot.isSome:
|
|
|
|
putInitialDepositContractSnapshot(db, depositContractSnapshot.get)
|
2020-12-03 04:30:35 +00:00
|
|
|
|
2020-12-15 21:59:29 +00:00
|
|
|
T(state: Initialized,
|
2021-11-15 12:01:47 +00:00
|
|
|
depositsChain: Eth1Chain.init(cfg, db),
|
Implement split preset/config support (#2710)
* Implement split preset/config support
This is the initial bulk refactor to introduce runtime config values in
a number of places, somewhat replacing the existing mechanism of loading
network metadata.
It still needs more work, this is the initial refactor that introduces
runtime configuration in some of the places that need it.
The PR changes the way presets and constants work, to match the spec. In
particular, a "preset" now refers to the compile-time configuration
while a "cfg" or "RuntimeConfig" is the dynamic part.
A single binary can support either mainnet or minimal, but not both.
Support for other presets has been removed completely (can be readded,
in case there's need).
There's a number of outstanding tasks:
* `SECONDS_PER_SLOT` still needs fixing
* loading custom runtime configs needs redoing
* checking constants against YAML file
* yeerongpilly support
`build/nimbus_beacon_node --network=yeerongpilly --discv5:no --log-level=DEBUG`
* load fork epoch from config
* fix fork digest sent in status
* nicer error string for request failures
* fix tools
* one more
* fixup
* fixup
* fixup
* use "standard" network definition folder in local testnet
Files are loaded from their standard locations, including genesis etc,
to conform to the format used in the `eth2-networks` repo.
* fix launch scripts, allow unknown config values
* fix base config of rest test
* cleanups
* bundle mainnet config using common loader
* fix spec links and names
* only include supported preset in binary
* drop yeerongpilly, add altair-devnet-0, support boot_enr.yaml
2021-07-12 13:01:38 +00:00
|
|
|
depositContractAddress: cfg.DEPOSIT_CONTRACT_ADDRESS,
|
2022-03-31 14:43:05 +00:00
|
|
|
getBeaconTime: getBeaconTime,
|
2021-04-06 21:42:59 +00:00
|
|
|
web3Urls: web3Urls,
|
2020-12-15 21:59:29 +00:00
|
|
|
eth1Network: eth1Network,
|
2021-11-25 16:51:51 +00:00
|
|
|
eth1Progress: newAsyncEvent(),
|
2022-02-27 16:55:02 +00:00
|
|
|
forcePolling: forcePolling,
|
2022-08-12 13:51:33 +00:00
|
|
|
jwtSecret: jwtSecret,
|
2022-09-20 06:17:25 +00:00
|
|
|
blocksPerLogsRequest: targetBlocksPerLogsRequest,
|
|
|
|
ttdReachedField: ttdReached)
|
2019-09-09 15:59:02 +00:00
|
|
|
|
2020-09-28 15:19:57 +00:00
|
|
|
proc safeCancel(fut: var Future[void]) =
|
|
|
|
if not fut.isNil and not fut.finished:
|
|
|
|
fut.cancel()
|
2020-11-03 01:21:07 +00:00
|
|
|
fut = nil
|
2020-09-28 15:19:57 +00:00
|
|
|
|
2021-11-25 10:53:31 +00:00
|
|
|
func clear(chain: var Eth1Chain) =
|
2020-11-24 21:21:47 +00:00
|
|
|
chain.blocks.clear()
|
|
|
|
chain.blocksByHash.clear()
|
2022-08-10 12:31:10 +00:00
|
|
|
chain.headMerkleizer = copy chain.finalizedDepositsMerkleizer
|
2021-01-29 21:21:44 +00:00
|
|
|
chain.hasConsensusViolation = false
|
2020-11-24 21:21:47 +00:00
|
|
|
|
2022-01-31 17:28:26 +00:00
|
|
|
proc detectPrimaryProviderComingOnline(m: Eth1Monitor) {.async.} =
|
2022-07-25 19:23:02 +00:00
|
|
|
const checkInterval = 30.seconds
|
2022-01-31 17:28:26 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
web3Url = m.web3Urls[0]
|
|
|
|
initialRunFut = m.runFut
|
|
|
|
|
|
|
|
# This is a way to detect that the monitor was restarted. When this
|
|
|
|
# happens, this function will just return terminating the "async thread"
|
|
|
|
while m.runFut == initialRunFut:
|
|
|
|
let tempProviderRes = await Web3DataProvider.new(
|
|
|
|
m.depositContractAddress,
|
2022-03-31 14:43:05 +00:00
|
|
|
web3Url,
|
|
|
|
m.jwtSecret)
|
2022-01-31 17:28:26 +00:00
|
|
|
|
|
|
|
if tempProviderRes.isErr:
|
|
|
|
await sleepAsync(checkInterval)
|
|
|
|
continue
|
|
|
|
|
|
|
|
var tempProvider = tempProviderRes.get
|
2022-06-17 06:32:52 +00:00
|
|
|
|
|
|
|
# Use one of the get/request-type methods from
|
2022-08-26 10:44:50 +00:00
|
|
|
# https://github.com/ethereum/execution-apis/blob/v1.0.0-beta.1/src/engine/specification.md#underlying-protocol
|
|
|
|
# which doesn't take parameters and returns a small structure, to ensure
|
2022-06-27 15:02:12 +00:00
|
|
|
# this works with engine API endpoints.
|
2022-06-17 06:32:52 +00:00
|
|
|
let testRequest = tempProvider.web3.provider.eth_syncing()
|
2022-01-31 17:28:26 +00:00
|
|
|
|
2022-02-15 19:10:04 +00:00
|
|
|
yield testRequest or sleepAsync(web3Timeouts)
|
2022-01-31 17:28:26 +00:00
|
|
|
|
2022-02-15 19:10:04 +00:00
|
|
|
traceAsyncErrors tempProvider.close()
|
2019-09-09 15:59:02 +00:00
|
|
|
|
2022-02-15 19:10:04 +00:00
|
|
|
if testRequest.completed and m.state == Started:
|
2022-01-31 17:28:26 +00:00
|
|
|
m.state = ReadyToRestartToPrimary
|
|
|
|
return
|
2022-02-15 19:10:04 +00:00
|
|
|
else:
|
|
|
|
await sleepAsync(checkInterval)
|
2022-01-31 17:28:26 +00:00
|
|
|
|
|
|
|
proc doStop(m: Eth1Monitor) {.async.} =
|
|
|
|
safeCancel m.runFut
|
2020-11-24 21:21:47 +00:00
|
|
|
|
2021-02-04 15:24:34 +00:00
|
|
|
if m.dataProvider != nil:
|
2022-07-25 19:23:02 +00:00
|
|
|
awaitWithTimeout(m.dataProvider.close(), 30.seconds):
|
|
|
|
debug "Failed to close data provider in time"
|
2021-02-04 15:24:34 +00:00
|
|
|
m.dataProvider = nil
|
2020-12-15 21:59:29 +00:00
|
|
|
|
2022-01-21 10:59:09 +00:00
|
|
|
proc ensureDataProvider*(m: Eth1Monitor) {.async.} =
|
2022-04-14 20:15:34 +00:00
|
|
|
if m.isNil or not m.dataProvider.isNil:
|
2022-01-21 10:59:09 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
let web3Url = m.web3Urls[m.startIdx mod m.web3Urls.len]
|
|
|
|
inc m.startIdx
|
|
|
|
|
|
|
|
m.dataProvider = block:
|
2022-03-31 14:43:05 +00:00
|
|
|
let v = await Web3DataProvider.new(
|
|
|
|
m.depositContractAddress, web3Url, m.jwtSecret)
|
2022-01-21 10:59:09 +00:00
|
|
|
if v.isErr():
|
|
|
|
raise (ref CatchableError)(msg: v.error())
|
2022-08-29 20:11:09 +00:00
|
|
|
info "Established connection to execution layer", url = web3Url
|
2022-01-21 10:59:09 +00:00
|
|
|
v.get()
|
|
|
|
|
2021-12-05 17:32:41 +00:00
|
|
|
proc stop(m: Eth1Monitor) {.async.} =
|
2022-01-31 17:28:26 +00:00
|
|
|
if m.state in {Started, ReadyToRestartToPrimary}:
|
2020-12-15 21:59:29 +00:00
|
|
|
m.state = Stopping
|
2022-01-31 17:28:26 +00:00
|
|
|
m.stopFut = m.doStop()
|
2020-12-15 21:59:29 +00:00
|
|
|
await m.stopFut
|
|
|
|
m.state = Stopped
|
|
|
|
elif m.state == Stopping:
|
|
|
|
await m.stopFut
|
|
|
|
|
2020-11-19 17:19:03 +00:00
|
|
|
const
|
2020-11-24 21:21:47 +00:00
|
|
|
votedBlocksSafetyMargin = 50
|
2020-11-19 17:19:03 +00:00
|
|
|
|
2021-11-25 16:51:51 +00:00
|
|
|
func latestEth1BlockNumber(m: Eth1Monitor): Eth1BlockNumber =
|
|
|
|
if m.latestEth1Block.isSome:
|
|
|
|
Eth1BlockNumber m.latestEth1Block.get.number
|
|
|
|
else:
|
|
|
|
Eth1BlockNumber 0
|
|
|
|
|
2021-11-25 10:53:31 +00:00
|
|
|
func earliestBlockOfInterest(m: Eth1Monitor): Eth1BlockNumber =
|
Implement split preset/config support (#2710)
* Implement split preset/config support
This is the initial bulk refactor to introduce runtime config values in
a number of places, somewhat replacing the existing mechanism of loading
network metadata.
It still needs more work, this is the initial refactor that introduces
runtime configuration in some of the places that need it.
The PR changes the way presets and constants work, to match the spec. In
particular, a "preset" now refers to the compile-time configuration
while a "cfg" or "RuntimeConfig" is the dynamic part.
A single binary can support either mainnet or minimal, but not both.
Support for other presets has been removed completely (can be readded,
in case there's need).
There's a number of outstanding tasks:
* `SECONDS_PER_SLOT` still needs fixing
* loading custom runtime configs needs redoing
* checking constants against YAML file
* yeerongpilly support
`build/nimbus_beacon_node --network=yeerongpilly --discv5:no --log-level=DEBUG`
* load fork epoch from config
* fix fork digest sent in status
* nicer error string for request failures
* fix tools
* one more
* fixup
* fixup
* fixup
* use "standard" network definition folder in local testnet
Files are loaded from their standard locations, including genesis etc,
to conform to the format used in the `eth2-networks` repo.
* fix launch scripts, allow unknown config values
* fix base config of rest test
* cleanups
* bundle mainnet config using common loader
* fix spec links and names
* only include supported preset in binary
* drop yeerongpilly, add altair-devnet-0, support boot_enr.yaml
2021-07-12 13:01:38 +00:00
|
|
|
m.latestEth1BlockNumber - (2 * m.cfg.ETH1_FOLLOW_DISTANCE) - votedBlocksSafetyMargin
|
2020-11-19 17:19:03 +00:00
|
|
|
|
|
|
|
proc syncBlockRange(m: Eth1Monitor,
|
|
|
|
fromBlock, toBlock,
|
2020-12-15 21:59:29 +00:00
|
|
|
fullSyncFromBlock: Eth1BlockNumber) {.gcsafe, async.} =
|
2022-09-08 07:36:53 +00:00
|
|
|
doAssert m.dataProvider != nil, "close not called concurrently"
|
2022-08-09 21:32:34 +00:00
|
|
|
doAssert m.depositsChain.blocks.len > 0
|
2020-11-19 17:19:03 +00:00
|
|
|
|
2020-11-03 01:21:07 +00:00
|
|
|
var currentBlock = fromBlock
|
|
|
|
while currentBlock <= toBlock:
|
2020-11-14 20:51:50 +00:00
|
|
|
var
|
|
|
|
depositLogs: JsonNode = nil
|
|
|
|
maxBlockNumberRequested: Eth1BlockNumber
|
2020-12-01 23:35:07 +00:00
|
|
|
backoff = 100
|
2020-03-24 11:13:07 +00:00
|
|
|
|
2020-11-03 01:21:07 +00:00
|
|
|
while true:
|
2022-08-12 13:51:33 +00:00
|
|
|
maxBlockNumberRequested =
|
|
|
|
min(toBlock, currentBlock + m.blocksPerLogsRequest - 1)
|
2020-03-24 11:13:07 +00:00
|
|
|
|
2020-11-24 21:21:47 +00:00
|
|
|
debug "Obtaining deposit log events",
|
|
|
|
fromBlock = currentBlock,
|
2020-12-01 23:35:07 +00:00
|
|
|
toBlock = maxBlockNumberRequested,
|
|
|
|
backoff
|
2020-02-07 07:13:38 +00:00
|
|
|
|
2020-11-24 21:21:47 +00:00
|
|
|
debug.logTime "Deposit logs obtained":
|
2020-12-01 11:14:32 +00:00
|
|
|
# Reduce all request rate until we have a more general solution
|
|
|
|
# for dealing with Infura's rate limits
|
2020-12-01 23:35:07 +00:00
|
|
|
await sleepAsync(milliseconds(backoff))
|
2020-11-24 21:21:47 +00:00
|
|
|
let jsonLogsFut = m.dataProvider.ns.getJsonLogs(
|
|
|
|
DepositEvent,
|
|
|
|
fromBlock = some blockId(currentBlock),
|
|
|
|
toBlock = some blockId(maxBlockNumberRequested))
|
|
|
|
|
|
|
|
depositLogs = try:
|
2022-08-12 13:51:33 +00:00
|
|
|
# Downloading large amounts of deposits may take several minutes
|
2020-12-15 21:59:29 +00:00
|
|
|
awaitWithTimeout(jsonLogsFut, web3Timeouts):
|
2022-08-12 13:51:33 +00:00
|
|
|
raise newException(DataProviderTimeout,
|
2020-11-24 21:21:47 +00:00
|
|
|
"Request time out while obtaining json logs")
|
|
|
|
except CatchableError as err:
|
2020-12-01 23:35:07 +00:00
|
|
|
debug "Request for deposit logs failed", err = err.msg
|
2020-12-09 22:44:59 +00:00
|
|
|
inc failed_web3_requests
|
2020-12-01 23:35:07 +00:00
|
|
|
backoff = (backoff * 3) div 2
|
2022-08-12 13:51:33 +00:00
|
|
|
m.blocksPerLogsRequest = m.blocksPerLogsRequest div 2
|
|
|
|
if m.blocksPerLogsRequest == 0:
|
|
|
|
m.blocksPerLogsRequest = 1
|
|
|
|
raise err
|
|
|
|
continue
|
|
|
|
m.blocksPerLogsRequest = min(
|
|
|
|
(m.blocksPerLogsRequest * 3 + 1) div 2,
|
|
|
|
targetBlocksPerLogsRequest)
|
2020-11-03 01:21:07 +00:00
|
|
|
|
2020-11-24 21:21:47 +00:00
|
|
|
currentBlock = maxBlockNumberRequested + 1
|
|
|
|
break
|
|
|
|
|
2020-12-01 11:14:32 +00:00
|
|
|
let blocksWithDeposits = depositEventsToBlocks(depositLogs)
|
2020-11-24 21:21:47 +00:00
|
|
|
|
|
|
|
for i in 0 ..< blocksWithDeposits.len:
|
|
|
|
let blk = blocksWithDeposits[i]
|
2022-08-10 12:31:10 +00:00
|
|
|
await blk.fetchTimestampWithRetries(m.dataProvider)
|
2020-11-03 01:21:07 +00:00
|
|
|
|
2020-11-19 17:19:03 +00:00
|
|
|
if blk.number > fullSyncFromBlock:
|
2021-11-15 12:01:47 +00:00
|
|
|
let lastBlock = m.depositsChain.blocks.peekLast
|
2020-11-19 17:19:03 +00:00
|
|
|
for n in max(lastBlock.number + 1, fullSyncFromBlock) ..< blk.number:
|
2020-12-01 23:35:07 +00:00
|
|
|
debug "Obtaining block without deposits", blockNum = n
|
2020-12-01 21:20:28 +00:00
|
|
|
let blockWithoutDeposits = awaitWithRetries(
|
|
|
|
m.dataProvider.getBlockByNumber(n))
|
|
|
|
|
2021-11-15 12:01:47 +00:00
|
|
|
m.depositsChain.addBlock(
|
2020-11-19 17:19:03 +00:00
|
|
|
lastBlock.makeSuccessorWithoutDeposits(blockWithoutDeposits))
|
2020-12-09 22:44:59 +00:00
|
|
|
eth1_synced_head.set blockWithoutDeposits.number.toGaugeValue
|
2020-11-19 17:19:03 +00:00
|
|
|
|
2021-11-15 12:01:47 +00:00
|
|
|
m.depositsChain.addBlock blk
|
2020-12-09 22:44:59 +00:00
|
|
|
eth1_synced_head.set blk.number.toGaugeValue
|
2020-11-24 21:21:47 +00:00
|
|
|
|
|
|
|
if blocksWithDeposits.len > 0:
|
|
|
|
let lastIdx = blocksWithDeposits.len - 1
|
|
|
|
template lastBlock: auto = blocksWithDeposits[lastIdx]
|
2020-11-03 01:21:07 +00:00
|
|
|
|
2020-11-24 21:21:47 +00:00
|
|
|
let status = when hasDepositRootChecks:
|
2020-12-01 21:20:28 +00:00
|
|
|
awaitWithRetries m.dataProvider.fetchDepositContractData(lastBlock)
|
2020-11-24 21:21:47 +00:00
|
|
|
else:
|
|
|
|
DepositRootUnavailable
|
2020-11-03 01:21:07 +00:00
|
|
|
|
|
|
|
when hasDepositRootChecks:
|
2020-11-24 21:21:47 +00:00
|
|
|
debug "Deposit contract state verified",
|
|
|
|
status = $status,
|
2022-08-10 12:31:10 +00:00
|
|
|
ourCount = lastBlock.depositCount,
|
|
|
|
ourRoot = lastBlock.depositRoot
|
2020-11-03 01:21:07 +00:00
|
|
|
|
2020-11-24 21:21:47 +00:00
|
|
|
case status
|
|
|
|
of DepositRootIncorrect, DepositCountIncorrect:
|
|
|
|
raise newException(CorruptDataProvider,
|
|
|
|
"The deposit log events disagree with the deposit contract state")
|
|
|
|
else:
|
|
|
|
discard
|
2020-11-03 01:21:07 +00:00
|
|
|
|
2022-01-03 21:18:49 +00:00
|
|
|
info "Eth1 sync progress",
|
2020-11-24 21:21:47 +00:00
|
|
|
blockNumber = lastBlock.number,
|
2022-08-10 12:31:10 +00:00
|
|
|
depositsProcessed = lastBlock.depositCount
|
2020-11-17 19:50:07 +00:00
|
|
|
|
2020-11-24 21:21:47 +00:00
|
|
|
when hasGenesisDetection:
|
2020-12-15 21:59:29 +00:00
|
|
|
if blocksWithDeposits.len > 0:
|
2020-11-24 21:21:47 +00:00
|
|
|
for blk in blocksWithDeposits:
|
|
|
|
for deposit in blk.deposits:
|
2020-12-15 21:59:29 +00:00
|
|
|
m.processGenesisDeposit(deposit)
|
|
|
|
blk.activeValidatorsCount = m.genesisValidators.lenu64
|
|
|
|
|
|
|
|
let depositContractState = DepositContractSnapshot(
|
2022-08-10 12:31:10 +00:00
|
|
|
eth1Block: blocksWithDeposits[^1].hash,
|
2022-08-09 21:32:34 +00:00
|
|
|
depositContractState: m.headMerkleizer.toDepositContractState)
|
2020-11-24 21:21:47 +00:00
|
|
|
|
2021-12-23 14:58:54 +00:00
|
|
|
m.depositsChain.db.putEth2FinalizedTo depositContractState
|
2020-11-24 21:21:47 +00:00
|
|
|
|
|
|
|
if m.genesisStateFut != nil and m.chainHasEnoughValidators:
|
2021-11-15 12:01:47 +00:00
|
|
|
let lastIdx = m.depositsChain.blocks.len - 1
|
|
|
|
template lastBlock: auto = m.depositsChain.blocks[lastIdx]
|
2020-11-24 21:21:47 +00:00
|
|
|
|
|
|
|
if maxBlockNumberRequested == toBlock and
|
2021-11-15 12:01:47 +00:00
|
|
|
(m.depositsChain.blocks.len == 0 or lastBlock.number != toBlock):
|
2020-12-01 21:20:28 +00:00
|
|
|
let web3Block = awaitWithRetries(
|
|
|
|
m.dataProvider.getBlockByNumber(toBlock))
|
2020-11-24 21:21:47 +00:00
|
|
|
|
|
|
|
debug "Latest block doesn't hold deposits. Obtaining it",
|
|
|
|
ts = web3Block.timestamp.uint64,
|
|
|
|
number = web3Block.number.uint64
|
|
|
|
|
2021-11-15 12:01:47 +00:00
|
|
|
m.depositsChain.addBlock lastBlock.makeSuccessorWithoutDeposits(web3Block)
|
2020-11-24 21:21:47 +00:00
|
|
|
else:
|
2022-08-10 12:31:10 +00:00
|
|
|
await lastBlock.fetchTimestampWithRetries(m.dataProvider)
|
2020-11-24 21:21:47 +00:00
|
|
|
|
2021-11-15 12:01:47 +00:00
|
|
|
var genesisBlockIdx = m.depositsChain.blocks.len - 1
|
|
|
|
if m.isAfterMinGenesisTime(m.depositsChain.blocks[genesisBlockIdx]):
|
2020-12-15 21:59:29 +00:00
|
|
|
for i in 1 ..< blocksWithDeposits.len:
|
2021-11-15 12:01:47 +00:00
|
|
|
let idx = (m.depositsChain.blocks.len - 1) - i
|
|
|
|
let blk = m.depositsChain.blocks[idx]
|
2022-08-10 12:31:10 +00:00
|
|
|
await blk.fetchTimestampWithRetries(m.dataProvider)
|
2020-11-24 21:21:47 +00:00
|
|
|
if m.isGenesisCandidate(blk):
|
|
|
|
genesisBlockIdx = idx
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
# We have a candidate state on our hands, but our current Eth1Chain
|
|
|
|
# may consist only of blocks that have deposits attached to them
|
|
|
|
# while the real genesis may have happened in a block without any
|
|
|
|
# deposits (triggered by MIN_GENESIS_TIME).
|
|
|
|
#
|
|
|
|
# This can happen when the beacon node is launched after the genesis
|
|
|
|
# event. We take a short cut when constructing the initial Eth1Chain
|
|
|
|
# by downloading only deposit log entries. Thus, we'll see all the
|
|
|
|
# blocks with deposits, but not the regular blocks in between.
|
|
|
|
#
|
|
|
|
# We'll handle this special case below by examing whether we are in
|
|
|
|
# this potential scenario and we'll use a fast guessing algorith to
|
|
|
|
# discover the ETh1 block with minimal valid genesis time.
|
2021-11-15 12:01:47 +00:00
|
|
|
var genesisBlock = m.depositsChain.blocks[genesisBlockIdx]
|
2020-11-24 21:21:47 +00:00
|
|
|
if genesisBlockIdx > 0:
|
2021-11-15 12:01:47 +00:00
|
|
|
let genesisParent = m.depositsChain.blocks[genesisBlockIdx - 1]
|
2020-11-24 21:21:47 +00:00
|
|
|
if genesisParent.timestamp == 0:
|
2022-08-10 12:31:10 +00:00
|
|
|
await genesisParent.fetchTimestampWithRetries(m.dataProvider)
|
2020-11-24 21:21:47 +00:00
|
|
|
if m.hasEnoughValidators(genesisParent) and
|
|
|
|
genesisBlock.number - genesisParent.number > 1:
|
2020-12-01 21:20:28 +00:00
|
|
|
genesisBlock = awaitWithRetries(
|
|
|
|
m.findGenesisBlockInRange(genesisParent, genesisBlock))
|
|
|
|
|
2020-11-24 21:21:47 +00:00
|
|
|
m.signalGenesis m.createGenesisState(genesisBlock)
|
|
|
|
|
2021-11-25 16:51:51 +00:00
|
|
|
func init(T: type FullBlockId, blk: Eth1BlockHeader|BlockObject): T =
|
|
|
|
FullBlockId(number: Eth1BlockNumber blk.number, hash: blk.hash)
|
|
|
|
|
2022-08-12 19:44:55 +00:00
|
|
|
func isNewLastBlock(m: Eth1Monitor, blk: Eth1BlockHeader|BlockObject): bool =
|
2022-09-18 05:45:51 +00:00
|
|
|
m.latestEth1Block.isNone or blk.number.uint64 > m.latestEth1BlockNumber
|
2022-08-12 19:44:55 +00:00
|
|
|
|
2020-12-15 21:59:29 +00:00
|
|
|
proc startEth1Syncing(m: Eth1Monitor, delayBeforeStart: Duration) {.async.} =
|
2022-02-15 19:10:04 +00:00
|
|
|
if m.state == Started:
|
2022-01-31 17:28:26 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
let isFirstRun = m.state == Initialized
|
2022-02-15 19:10:04 +00:00
|
|
|
let needsReset = m.state in {Failed, ReadyToRestartToPrimary}
|
|
|
|
|
|
|
|
m.state = Started
|
2020-12-15 21:59:29 +00:00
|
|
|
|
|
|
|
if delayBeforeStart != ZeroDuration:
|
|
|
|
await sleepAsync(delayBeforeStart)
|
|
|
|
|
2022-01-31 17:28:26 +00:00
|
|
|
# If the monitor died with an exception, the web3 provider may be in
|
|
|
|
# an arbitary state, so we better reset it (not doing this has resulted
|
|
|
|
# in resource leaks historically).
|
2022-02-15 19:10:04 +00:00
|
|
|
if not m.dataProvider.isNil and needsReset:
|
2022-01-31 17:28:26 +00:00
|
|
|
# We introduce a local var to eliminate the risk of scheduling two
|
|
|
|
# competing calls to `close` below.
|
|
|
|
let provider = m.dataProvider
|
|
|
|
m.dataProvider = nil
|
|
|
|
await provider.close()
|
|
|
|
|
2022-01-21 10:59:09 +00:00
|
|
|
await m.ensureDataProvider()
|
2022-09-08 07:36:53 +00:00
|
|
|
doAssert m.dataProvider != nil, "close not called concurrently"
|
2022-01-31 17:28:26 +00:00
|
|
|
|
|
|
|
# We might need to reset the chain if the new provider disagrees
|
|
|
|
# with the previous one regarding the history of the chain or if
|
|
|
|
# we have detected a conensus violation - our view disagreeing with
|
|
|
|
# the majority of the validators in the network.
|
|
|
|
#
|
|
|
|
# Consensus violations happen in practice because the web3 providers
|
|
|
|
# sometimes return incomplete or incorrect deposit log events even
|
|
|
|
# when they don't indicate any errors in the response. When this
|
|
|
|
# happens, we are usually able to download the data successfully
|
|
|
|
# on the second attempt.
|
|
|
|
if m.latestEth1Block.isSome and m.depositsChain.blocks.len > 0:
|
|
|
|
let needsReset = m.depositsChain.hasConsensusViolation or (block:
|
|
|
|
let
|
|
|
|
lastKnownBlock = m.depositsChain.blocks.peekLast
|
|
|
|
matchingBlockAtNewProvider = awaitWithRetries(
|
|
|
|
m.dataProvider.getBlockByNumber lastKnownBlock.number)
|
|
|
|
|
2022-08-10 12:31:10 +00:00
|
|
|
lastKnownBlock.hash.asBlockHash != matchingBlockAtNewProvider.hash)
|
2022-01-31 17:28:26 +00:00
|
|
|
|
|
|
|
if needsReset:
|
|
|
|
m.depositsChain.clear()
|
|
|
|
m.latestEth1Block = none(FullBlockId)
|
|
|
|
|
|
|
|
template web3Url: string = m.dataProvider.url
|
|
|
|
|
|
|
|
if web3Url != m.web3Urls[0]:
|
|
|
|
asyncSpawn m.detectPrimaryProviderComingOnline()
|
2021-04-06 21:42:59 +00:00
|
|
|
|
2020-12-15 21:59:29 +00:00
|
|
|
info "Starting Eth1 deposit contract monitoring",
|
2022-01-31 17:28:26 +00:00
|
|
|
contract = $m.depositContractAddress
|
2020-12-15 21:59:29 +00:00
|
|
|
|
2022-01-31 17:28:26 +00:00
|
|
|
if isFirstRun and m.eth1Network.isSome:
|
2022-06-27 15:02:12 +00:00
|
|
|
try:
|
|
|
|
let
|
|
|
|
providerChain =
|
|
|
|
awaitWithRetries m.dataProvider.web3.provider.eth_chainId()
|
|
|
|
|
|
|
|
# https://eips.ethereum.org/EIPS/eip-155#list-of-chain-ids
|
|
|
|
expectedChain = case m.eth1Network.get
|
|
|
|
of mainnet: 1.Quantity
|
|
|
|
of ropsten: 3.Quantity
|
|
|
|
of rinkeby: 4.Quantity
|
|
|
|
of goerli: 5.Quantity
|
|
|
|
of sepolia: 11155111.Quantity # https://chainid.network/
|
|
|
|
if expectedChain != providerChain:
|
|
|
|
fatal "The specified Web3 provider serves data for a different chain",
|
|
|
|
expectedChain = distinctBase(expectedChain),
|
|
|
|
providerChain = distinctBase(providerChain)
|
|
|
|
quit 1
|
|
|
|
except CatchableError as exc:
|
|
|
|
# Typically because it's not synced through EIP-155, assuming this Web3
|
|
|
|
# endpoint has been otherwise working.
|
|
|
|
debug "startEth1Syncing: eth_chainId failed: ",
|
|
|
|
error = exc.msg
|
2020-12-15 21:59:29 +00:00
|
|
|
|
2021-11-25 16:51:51 +00:00
|
|
|
var mustUsePolling = m.forcePolling or
|
|
|
|
web3Url.startsWith("http://") or
|
|
|
|
web3Url.startsWith("https://")
|
|
|
|
|
|
|
|
if not mustUsePolling:
|
|
|
|
proc newBlockHeadersHandler(blk: Eth1BlockHeader)
|
|
|
|
{.raises: [Defect], gcsafe.} =
|
|
|
|
try:
|
2022-08-12 19:44:55 +00:00
|
|
|
if m.isNewLastBlock(blk):
|
2021-11-25 16:51:51 +00:00
|
|
|
eth1_latest_head.set blk.number.toGaugeValue
|
|
|
|
m.latestEth1Block = some FullBlockId.init(blk)
|
|
|
|
m.eth1Progress.fire()
|
|
|
|
except Exception:
|
|
|
|
# TODO Investigate why this exception is being raised
|
|
|
|
raiseAssert "AsyncEvent.fire should not raise exceptions"
|
|
|
|
|
|
|
|
proc subscriptionErrorHandler(err: CatchableError)
|
|
|
|
{.raises: [Defect], gcsafe.} =
|
|
|
|
warn "Failed to subscribe for block headers. Switching to polling",
|
2022-01-31 17:28:26 +00:00
|
|
|
err = err.msg
|
2021-11-25 16:51:51 +00:00
|
|
|
mustUsePolling = true
|
|
|
|
|
|
|
|
await m.dataProvider.onBlockHeaders(newBlockHeadersHandler,
|
|
|
|
subscriptionErrorHandler)
|
2020-12-15 21:59:29 +00:00
|
|
|
|
2022-06-15 02:38:27 +00:00
|
|
|
let shouldProcessDeposits = not m.depositContractAddress.isZeroMemory
|
|
|
|
var eth1SyncedTo: Eth1BlockNumber
|
2022-08-12 13:48:33 +00:00
|
|
|
if shouldProcessDeposits:
|
|
|
|
if m.depositsChain.blocks.len == 0:
|
|
|
|
let startBlock = awaitWithRetries(
|
|
|
|
m.dataProvider.getBlockByHash(
|
|
|
|
m.depositsChain.finalizedBlockHash.asBlockHash))
|
2020-11-19 17:19:03 +00:00
|
|
|
|
2022-08-12 13:48:33 +00:00
|
|
|
m.depositsChain.addBlock Eth1Block(
|
|
|
|
hash: m.depositsChain.finalizedBlockHash,
|
|
|
|
number: Eth1BlockNumber startBlock.number,
|
|
|
|
timestamp: Eth1BlockTimestamp startBlock.timestamp)
|
2020-11-19 17:19:03 +00:00
|
|
|
|
2022-08-12 13:48:33 +00:00
|
|
|
eth1SyncedTo = Eth1BlockNumber m.depositsChain.blocks[^1].number
|
2022-06-15 02:38:27 +00:00
|
|
|
|
|
|
|
eth1_synced_head.set eth1SyncedTo.toGaugeValue
|
|
|
|
eth1_finalized_head.set eth1SyncedTo.toGaugeValue
|
|
|
|
eth1_finalized_deposits.set(
|
|
|
|
m.depositsChain.finalizedDepositsMerkleizer.getChunkCount.toGaugeValue)
|
2020-12-09 22:44:59 +00:00
|
|
|
|
2022-08-12 13:48:33 +00:00
|
|
|
debug "Starting Eth1 syncing", `from` = shortLog(m.depositsChain.blocks[^1])
|
2020-11-30 23:59:35 +00:00
|
|
|
|
2022-09-18 05:45:51 +00:00
|
|
|
let shouldCheckForMergeTransition = block:
|
|
|
|
const FAR_FUTURE_TOTAL_DIFFICULTY =
|
|
|
|
u256"115792089237316195423570985008687907853269984665640564039457584007913129638912"
|
2022-09-20 06:17:25 +00:00
|
|
|
(not m.ttdReachedField) and
|
|
|
|
(m.cfg.TERMINAL_TOTAL_DIFFICULTY != FAR_FUTURE_TOTAL_DIFFICULTY)
|
2022-09-18 05:45:51 +00:00
|
|
|
|
2022-08-12 19:44:55 +00:00
|
|
|
var didPollOnce = false
|
2020-11-03 01:21:07 +00:00
|
|
|
while true:
|
|
|
|
if bnStatus == BeaconNodeStatus.Stopping:
|
2020-11-24 21:21:47 +00:00
|
|
|
when hasGenesisDetection:
|
|
|
|
if not m.genesisStateFut.isNil:
|
|
|
|
m.genesisStateFut.complete()
|
|
|
|
m.genesisStateFut = nil
|
2020-12-15 21:59:29 +00:00
|
|
|
await m.stop()
|
2020-11-03 01:21:07 +00:00
|
|
|
return
|
2020-10-14 14:04:08 +00:00
|
|
|
|
2021-11-15 12:01:47 +00:00
|
|
|
if m.depositsChain.hasConsensusViolation:
|
2021-01-29 21:21:44 +00:00
|
|
|
raise newException(CorruptDataProvider, "Eth1 chain contradicts Eth2 consensus")
|
|
|
|
|
2022-01-31 17:28:26 +00:00
|
|
|
if m.state == ReadyToRestartToPrimary:
|
|
|
|
info "Primary web3 provider is back online. Restarting the Eth1 monitor"
|
|
|
|
m.startIdx = 0
|
|
|
|
return
|
|
|
|
|
2022-08-12 19:44:55 +00:00
|
|
|
let nextBlock = if mustUsePolling or not didPollOnce:
|
2021-11-25 16:51:51 +00:00
|
|
|
let blk = awaitWithRetries(
|
|
|
|
m.dataProvider.web3.provider.eth_getBlockByNumber(blockId("latest"), false))
|
|
|
|
|
2022-08-12 19:44:55 +00:00
|
|
|
# Same as when handling events, minus `m.eth1Progress` round trip
|
|
|
|
if m.isNewLastBlock(blk):
|
|
|
|
eth1_latest_head.set blk.number.toGaugeValue
|
|
|
|
m.latestEth1Block = some FullBlockId.init(blk)
|
|
|
|
elif mustUsePolling:
|
2021-11-25 16:51:51 +00:00
|
|
|
await sleepAsync(m.cfg.SECONDS_PER_ETH1_BLOCK.int.seconds)
|
|
|
|
continue
|
2022-08-12 19:44:55 +00:00
|
|
|
else:
|
|
|
|
doAssert not didPollOnce
|
2021-11-25 16:51:51 +00:00
|
|
|
|
2022-08-12 19:44:55 +00:00
|
|
|
didPollOnce = true
|
2022-06-15 02:38:27 +00:00
|
|
|
blk
|
2021-11-25 16:51:51 +00:00
|
|
|
else:
|
|
|
|
awaitWithTimeout(m.eth1Progress.wait(), 5.minutes):
|
|
|
|
raise newException(CorruptDataProvider, "No eth1 chain progress for too long")
|
2021-02-04 15:01:47 +00:00
|
|
|
|
2021-11-25 16:51:51 +00:00
|
|
|
m.eth1Progress.clear()
|
2020-03-24 11:13:07 +00:00
|
|
|
|
2022-08-12 19:44:55 +00:00
|
|
|
doAssert m.latestEth1Block.isSome
|
2022-06-15 02:38:27 +00:00
|
|
|
awaitWithRetries m.dataProvider.getBlockByHash(m.latestEth1Block.get.hash)
|
|
|
|
|
2022-09-16 13:33:22 +00:00
|
|
|
# TODO when a terminal block has is configured in cfg.TERMINAL_BLOCK_HASH,
|
|
|
|
# we should try to fetch that block from the EL - this facility is not
|
|
|
|
# in use on any current network, but should be implemented for full
|
|
|
|
# compliance
|
2022-09-18 05:45:51 +00:00
|
|
|
if m.terminalBlockHash.isNone and shouldCheckForMergeTransition:
|
2022-06-15 02:38:27 +00:00
|
|
|
var terminalBlockCandidate = nextBlock
|
|
|
|
|
2022-09-06 23:41:55 +00:00
|
|
|
debug "startEth1Syncing: checking for merge terminal block",
|
2022-06-15 02:38:27 +00:00
|
|
|
currentEpoch = m.currentEpoch,
|
|
|
|
BELLATRIX_FORK_EPOCH = m.cfg.BELLATRIX_FORK_EPOCH,
|
2022-06-17 14:16:03 +00:00
|
|
|
totalDifficulty = $nextBlock.totalDifficulty,
|
|
|
|
ttd = $m.cfg.TERMINAL_TOTAL_DIFFICULTY,
|
2022-09-15 13:00:23 +00:00
|
|
|
terminalBlockHash = m.terminalBlockHash,
|
|
|
|
candidateBlockHash = terminalBlockCandidate.hash,
|
|
|
|
candidateBlockNumber = distinctBase(terminalBlockCandidate.number)
|
2022-06-15 02:38:27 +00:00
|
|
|
|
|
|
|
if terminalBlockCandidate.totalDifficulty >= m.cfg.TERMINAL_TOTAL_DIFFICULTY:
|
|
|
|
while not terminalBlockCandidate.parentHash.isZeroMemory:
|
|
|
|
var parentBlock = awaitWithRetries(
|
|
|
|
m.dataProvider.getBlockByHash(terminalBlockCandidate.parentHash))
|
|
|
|
if parentBlock.totalDifficulty < m.cfg.TERMINAL_TOTAL_DIFFICULTY:
|
|
|
|
break
|
|
|
|
terminalBlockCandidate = parentBlock
|
|
|
|
m.terminalBlockHash = some terminalBlockCandidate.hash
|
2022-09-20 06:17:25 +00:00
|
|
|
m.ttdReachedField = true
|
2022-06-15 02:38:27 +00:00
|
|
|
|
2022-09-15 13:00:23 +00:00
|
|
|
debug "startEth1Syncing: found merge terminal block",
|
|
|
|
currentEpoch = m.currentEpoch,
|
|
|
|
BELLATRIX_FORK_EPOCH = m.cfg.BELLATRIX_FORK_EPOCH,
|
|
|
|
totalDifficulty = $nextBlock.totalDifficulty,
|
|
|
|
ttd = $m.cfg.TERMINAL_TOTAL_DIFFICULTY,
|
|
|
|
terminalBlockHash = m.terminalBlockHash,
|
|
|
|
candidateBlockHash = terminalBlockCandidate.hash,
|
|
|
|
candidateBlockNumber = distinctBase(terminalBlockCandidate.number)
|
|
|
|
|
2022-08-09 21:32:34 +00:00
|
|
|
if shouldProcessDeposits:
|
2022-06-15 02:38:27 +00:00
|
|
|
if m.latestEth1BlockNumber <= m.cfg.ETH1_FOLLOW_DISTANCE:
|
|
|
|
continue
|
2020-03-24 11:13:07 +00:00
|
|
|
|
2022-06-15 02:38:27 +00:00
|
|
|
let targetBlock = m.latestEth1BlockNumber - m.cfg.ETH1_FOLLOW_DISTANCE
|
|
|
|
if targetBlock <= eth1SyncedTo:
|
|
|
|
continue
|
|
|
|
|
|
|
|
let earliestBlockOfInterest = m.earliestBlockOfInterest()
|
2022-08-09 21:32:34 +00:00
|
|
|
await m.syncBlockRange(eth1SyncedTo + 1,
|
2022-06-15 02:38:27 +00:00
|
|
|
targetBlock,
|
|
|
|
earliestBlockOfInterest)
|
|
|
|
eth1SyncedTo = targetBlock
|
|
|
|
eth1_synced_head.set eth1SyncedTo.toGaugeValue
|
2019-11-22 13:16:07 +00:00
|
|
|
|
2022-02-25 08:19:12 +00:00
|
|
|
proc start(m: Eth1Monitor, delayBeforeStart: Duration) {.gcsafe.} =
|
2019-11-22 13:16:07 +00:00
|
|
|
if m.runFut.isNil:
|
2020-12-15 21:59:29 +00:00
|
|
|
let runFut = m.startEth1Syncing(delayBeforeStart)
|
2019-11-22 13:16:07 +00:00
|
|
|
m.runFut = runFut
|
2022-02-25 08:19:12 +00:00
|
|
|
runFut.addCallback do (p: pointer) {.gcsafe.}:
|
2020-03-24 11:13:07 +00:00
|
|
|
if runFut.failed:
|
2022-02-25 08:19:12 +00:00
|
|
|
if runFut == m.runFut:
|
|
|
|
warn "Eth1 chain monitoring failure, restarting", err = runFut.error.msg
|
|
|
|
m.state = Failed
|
2019-11-22 13:16:07 +00:00
|
|
|
|
2020-12-15 21:59:29 +00:00
|
|
|
safeCancel m.runFut
|
2020-11-19 17:19:03 +00:00
|
|
|
m.start(5.seconds)
|
|
|
|
|
2020-11-20 10:00:22 +00:00
|
|
|
proc start*(m: Eth1Monitor) =
|
2019-11-22 13:16:07 +00:00
|
|
|
m.start(0.seconds)
|
|
|
|
|
2022-03-31 14:43:05 +00:00
|
|
|
proc getEth1BlockHash*(
|
|
|
|
url: string, blockId: RtBlockIdentifier, jwtSecret: Option[seq[byte]]):
|
|
|
|
Future[BlockHash] {.async.} =
|
2022-07-25 19:23:02 +00:00
|
|
|
let web3 = awaitOrRaiseOnTimeout(newWeb3(url, getJsonRpcRequestHeaders(jwtSecret)),
|
|
|
|
10.seconds)
|
2020-04-22 23:35:55 +00:00
|
|
|
try:
|
2020-12-01 21:20:28 +00:00
|
|
|
let blk = awaitWithRetries(
|
|
|
|
web3.provider.eth_getBlockByNumber(blockId, false))
|
2020-07-28 13:36:11 +00:00
|
|
|
return blk.hash
|
2020-04-22 23:35:55 +00:00
|
|
|
finally:
|
|
|
|
await web3.close()
|
|
|
|
|
2022-07-25 19:23:02 +00:00
|
|
|
func `$`(x: Quantity): string =
|
|
|
|
$(x.uint64)
|
|
|
|
|
|
|
|
func `$`(x: BlockObject): string =
|
|
|
|
$(x.number) & " [" & $(x.hash) & "]"
|
|
|
|
|
2020-12-04 16:28:42 +00:00
|
|
|
proc testWeb3Provider*(web3Url: Uri,
|
2022-03-31 14:43:05 +00:00
|
|
|
depositContractAddress: Eth1Address,
|
|
|
|
jwtSecret: Option[seq[byte]]) {.async.} =
|
2022-07-25 19:23:02 +00:00
|
|
|
stdout.write "Establishing web3 connection..."
|
|
|
|
var web3: Web3
|
|
|
|
try:
|
|
|
|
web3 = awaitOrRaiseOnTimeout(
|
|
|
|
newWeb3($web3Url, getJsonRpcRequestHeaders(jwtSecret)), 5.seconds)
|
|
|
|
stdout.write "\rEstablishing web3 connection: Connected\n"
|
|
|
|
except CatchableError as err:
|
|
|
|
stdout.write "\rEstablishing web3 connection: Failure(" & err.msg & ")\n"
|
|
|
|
quit 1
|
|
|
|
|
|
|
|
template request(actionDesc: static string,
|
|
|
|
action: untyped): untyped =
|
|
|
|
stdout.write actionDesc & "..."
|
|
|
|
stdout.flushFile()
|
|
|
|
var res: typeof(read action)
|
|
|
|
try:
|
|
|
|
res = awaitWithRetries action
|
|
|
|
stdout.write "\r" & actionDesc & ": " & $res
|
2020-12-04 16:28:42 +00:00
|
|
|
except CatchableError as err:
|
2022-07-25 19:23:02 +00:00
|
|
|
stdout.write "\r" & actionDesc & ": Error(" & err.msg & ")"
|
|
|
|
stdout.write "\n"
|
|
|
|
res
|
2020-12-04 16:28:42 +00:00
|
|
|
|
|
|
|
let
|
2022-07-25 19:23:02 +00:00
|
|
|
clientVersion = request "Client version":
|
|
|
|
web3.provider.web3_clientVersion()
|
2020-12-04 16:28:42 +00:00
|
|
|
|
2022-07-25 19:23:02 +00:00
|
|
|
chainId = request "Chain ID":
|
|
|
|
web3.provider.eth_chainId()
|
2022-06-27 15:02:12 +00:00
|
|
|
|
2022-07-25 19:23:02 +00:00
|
|
|
latestBlock = request "Latest block":
|
|
|
|
web3.provider.eth_getBlockByNumber(blockId("latest"), false)
|
|
|
|
|
|
|
|
syncStatus = request "Sync status":
|
|
|
|
web3.provider.eth_syncing()
|
|
|
|
|
|
|
|
peers = request "Peers":
|
|
|
|
web3.provider.net_peerCount()
|
|
|
|
|
|
|
|
miningStatus = request "Mining status":
|
|
|
|
web3.provider.eth_mining()
|
|
|
|
|
|
|
|
ns = web3.contractSender(DepositContract, depositContractAddress)
|
|
|
|
|
|
|
|
depositRoot = request "Deposit root":
|
|
|
|
ns.get_deposit_root.call(blockNumber = latestBlock.number.uint64)
|
2020-11-24 21:21:47 +00:00
|
|
|
|
2022-02-25 08:22:44 +00:00
|
|
|
when hasGenesisDetection:
|
|
|
|
proc loadPersistedDeposits*(monitor: Eth1Monitor) =
|
|
|
|
for i in 0 ..< monitor.depositsChain.db.genesisDeposits.len:
|
|
|
|
monitor.produceDerivedData monitor.depositsChain.db.genesisDeposits.get(i)
|
2020-11-24 21:21:47 +00:00
|
|
|
|
|
|
|
proc findGenesisBlockInRange(m: Eth1Monitor, startBlock, endBlock: Eth1Block):
|
|
|
|
Future[Eth1Block] {.async.} =
|
2022-09-08 07:36:53 +00:00
|
|
|
doAssert m.dataProvider != nil, "close not called concurrently"
|
2020-11-24 21:21:47 +00:00
|
|
|
doAssert startBlock.timestamp != 0 and not m.isAfterMinGenesisTime(startBlock)
|
|
|
|
doAssert endBlock.timestamp != 0 and m.isAfterMinGenesisTime(endBlock)
|
|
|
|
doAssert m.hasEnoughValidators(startBlock)
|
|
|
|
doAssert m.hasEnoughValidators(endBlock)
|
|
|
|
|
|
|
|
var
|
|
|
|
startBlock = startBlock
|
|
|
|
endBlock = endBlock
|
|
|
|
activeValidatorsCountDuringRange = startBlock.activeValidatorsCount
|
|
|
|
|
|
|
|
while startBlock.number + 1 < endBlock.number:
|
|
|
|
let
|
Implement split preset/config support (#2710)
* Implement split preset/config support
This is the initial bulk refactor to introduce runtime config values in
a number of places, somewhat replacing the existing mechanism of loading
network metadata.
It still needs more work, this is the initial refactor that introduces
runtime configuration in some of the places that need it.
The PR changes the way presets and constants work, to match the spec. In
particular, a "preset" now refers to the compile-time configuration
while a "cfg" or "RuntimeConfig" is the dynamic part.
A single binary can support either mainnet or minimal, but not both.
Support for other presets has been removed completely (can be readded,
in case there's need).
There's a number of outstanding tasks:
* `SECONDS_PER_SLOT` still needs fixing
* loading custom runtime configs needs redoing
* checking constants against YAML file
* yeerongpilly support
`build/nimbus_beacon_node --network=yeerongpilly --discv5:no --log-level=DEBUG`
* load fork epoch from config
* fix fork digest sent in status
* nicer error string for request failures
* fix tools
* one more
* fixup
* fixup
* fixup
* use "standard" network definition folder in local testnet
Files are loaded from their standard locations, including genesis etc,
to conform to the format used in the `eth2-networks` repo.
* fix launch scripts, allow unknown config values
* fix base config of rest test
* cleanups
* bundle mainnet config using common loader
* fix spec links and names
* only include supported preset in binary
* drop yeerongpilly, add altair-devnet-0, support boot_enr.yaml
2021-07-12 13:01:38 +00:00
|
|
|
MIN_GENESIS_TIME = m.cfg.MIN_GENESIS_TIME
|
|
|
|
startBlockTime = genesis_time_from_eth1_timestamp(m.cfg, startBlock.timestamp)
|
2020-11-24 21:21:47 +00:00
|
|
|
secondsPerBlock = float(endBlock.timestamp - startBlock.timestamp) /
|
|
|
|
float(endBlock.number - startBlock.number)
|
|
|
|
blocksToJump = max(float(MIN_GENESIS_TIME - startBlockTime) / secondsPerBlock, 1.0)
|
|
|
|
candidateNumber = min(endBlock.number - 1, startBlock.number + blocksToJump.uint64)
|
2020-12-01 21:20:28 +00:00
|
|
|
candidateBlock = awaitWithRetries(
|
|
|
|
m.dataProvider.getBlockByNumber(candidateNumber))
|
2020-11-24 21:21:47 +00:00
|
|
|
|
2022-08-10 12:31:10 +00:00
|
|
|
var candidateAsEth1Block = Eth1Block(hash: candidateBlock.hash.asEth2Digest,
|
|
|
|
number: candidateBlock.number.uint64,
|
|
|
|
timestamp: candidateBlock.timestamp.uint64)
|
2020-11-24 21:21:47 +00:00
|
|
|
|
|
|
|
let candidateGenesisTime = genesis_time_from_eth1_timestamp(
|
Implement split preset/config support (#2710)
* Implement split preset/config support
This is the initial bulk refactor to introduce runtime config values in
a number of places, somewhat replacing the existing mechanism of loading
network metadata.
It still needs more work, this is the initial refactor that introduces
runtime configuration in some of the places that need it.
The PR changes the way presets and constants work, to match the spec. In
particular, a "preset" now refers to the compile-time configuration
while a "cfg" or "RuntimeConfig" is the dynamic part.
A single binary can support either mainnet or minimal, but not both.
Support for other presets has been removed completely (can be readded,
in case there's need).
There's a number of outstanding tasks:
* `SECONDS_PER_SLOT` still needs fixing
* loading custom runtime configs needs redoing
* checking constants against YAML file
* yeerongpilly support
`build/nimbus_beacon_node --network=yeerongpilly --discv5:no --log-level=DEBUG`
* load fork epoch from config
* fix fork digest sent in status
* nicer error string for request failures
* fix tools
* one more
* fixup
* fixup
* fixup
* use "standard" network definition folder in local testnet
Files are loaded from their standard locations, including genesis etc,
to conform to the format used in the `eth2-networks` repo.
* fix launch scripts, allow unknown config values
* fix base config of rest test
* cleanups
* bundle mainnet config using common loader
* fix spec links and names
* only include supported preset in binary
* drop yeerongpilly, add altair-devnet-0, support boot_enr.yaml
2021-07-12 13:01:38 +00:00
|
|
|
m.cfg, candidateBlock.timestamp.uint64)
|
2020-11-24 21:21:47 +00:00
|
|
|
|
|
|
|
notice "Probing possible genesis block",
|
|
|
|
`block` = candidateBlock.number.uint64,
|
|
|
|
candidateGenesisTime
|
|
|
|
|
|
|
|
if candidateGenesisTime < MIN_GENESIS_TIME:
|
|
|
|
startBlock = candidateAsEth1Block
|
|
|
|
else:
|
|
|
|
endBlock = candidateAsEth1Block
|
|
|
|
|
|
|
|
if endBlock.activeValidatorsCount == 0:
|
|
|
|
endBlock.activeValidatorsCount = activeValidatorsCountDuringRange
|
|
|
|
|
|
|
|
return endBlock
|
|
|
|
|
2021-12-23 14:58:54 +00:00
|
|
|
proc waitGenesis*(m: Eth1Monitor): Future[GenesisStateRef] {.async.} =
|
2020-11-24 21:21:47 +00:00
|
|
|
if m.genesisState.isNil:
|
|
|
|
m.start()
|
|
|
|
|
|
|
|
if m.genesisStateFut.isNil:
|
|
|
|
m.genesisStateFut = newFuture[void]("waitGenesis")
|
|
|
|
|
|
|
|
info "Awaiting genesis event"
|
|
|
|
await m.genesisStateFut
|
|
|
|
m.genesisStateFut = nil
|
|
|
|
|
|
|
|
if m.genesisState != nil:
|
|
|
|
return m.genesisState
|
|
|
|
else:
|
|
|
|
doAssert bnStatus == BeaconNodeStatus.Stopping
|
2021-12-23 14:58:54 +00:00
|
|
|
return nil
|