Restore the build support of the -d:has_genesis_detection feature
This commit is contained in:
parent
6b4f32ae23
commit
a860cd6250
|
@ -17,7 +17,7 @@ import
|
||||||
eth/async_utils, stew/[objects, byteutils, shims/hashes],
|
eth/async_utils, stew/[objects, byteutils, shims/hashes],
|
||||||
# Local modules:
|
# Local modules:
|
||||||
../spec/[eth2_merkleization, forks, helpers],
|
../spec/[eth2_merkleization, forks, helpers],
|
||||||
../spec/datatypes/[base, merge],
|
../spec/datatypes/[base, phase0, merge],
|
||||||
../networking/network_metadata,
|
../networking/network_metadata,
|
||||||
../consensus_object_pools/block_pools_types,
|
../consensus_object_pools/block_pools_types,
|
||||||
".."/[beacon_chain_db, beacon_node_status],
|
".."/[beacon_chain_db, beacon_node_status],
|
||||||
|
@ -60,7 +60,7 @@ type
|
||||||
Eth1BlockTimestamp* = uint64
|
Eth1BlockTimestamp* = uint64
|
||||||
Eth1BlockHeader = web3Types.BlockHeader
|
Eth1BlockHeader = web3Types.BlockHeader
|
||||||
|
|
||||||
Database* = object
|
GenesisStateRef = ref phase0.BeaconState
|
||||||
|
|
||||||
Eth1Block* = ref object
|
Eth1Block* = ref object
|
||||||
number*: Eth1BlockNumber
|
number*: Eth1BlockNumber
|
||||||
|
@ -110,7 +110,7 @@ type
|
||||||
when hasGenesisDetection:
|
when hasGenesisDetection:
|
||||||
genesisValidators: seq[ImmutableValidatorData]
|
genesisValidators: seq[ImmutableValidatorData]
|
||||||
genesisValidatorKeyToIndex: Table[ValidatorPubKey, ValidatorIndex]
|
genesisValidatorKeyToIndex: Table[ValidatorPubKey, ValidatorIndex]
|
||||||
genesisState: NilableBeaconStateRef
|
genesisState: GenesisStateRef
|
||||||
genesisStateFut: Future[void]
|
genesisStateFut: Future[void]
|
||||||
|
|
||||||
Web3DataProvider* = object
|
Web3DataProvider* = object
|
||||||
|
@ -168,18 +168,17 @@ func depositCountU64(s: DepositContractState): uint64 =
|
||||||
|
|
||||||
uint64.fromBytesBE s.deposit_count[24..31]
|
uint64.fromBytesBE s.deposit_count[24..31]
|
||||||
|
|
||||||
|
template cfg(m: Eth1Monitor): auto =
|
||||||
|
m.depositsChain.cfg
|
||||||
|
|
||||||
when hasGenesisDetection:
|
when hasGenesisDetection:
|
||||||
import spec/[beaconstate, signatures]
|
import ../spec/[beaconstate, signatures]
|
||||||
|
|
||||||
template hasEnoughValidators(m: Eth1Monitor, blk: Eth1Block): bool =
|
template hasEnoughValidators(m: Eth1Monitor, blk: Eth1Block): bool =
|
||||||
blk.activeValidatorsCount >= m.cfg.MIN_GENESIS_ACTIVE_VALIDATOR_COUNT
|
blk.activeValidatorsCount >= m.cfg.MIN_GENESIS_ACTIVE_VALIDATOR_COUNT
|
||||||
|
|
||||||
func chainHasEnoughValidators(m: Eth1Monitor): bool =
|
func chainHasEnoughValidators(m: Eth1Monitor): bool =
|
||||||
if m.depositsChain.blocks.len > 0:
|
m.depositsChain.blocks.len > 0 and m.hasEnoughValidators(m.depositsChain.blocks[^1])
|
||||||
m.hasEnoughValidators(m.depositsChain.blocks[^1])
|
|
||||||
else:
|
|
||||||
m.knownStart.depositContractState.depositCountU64 >=
|
|
||||||
m.cfg.MIN_GENESIS_ACTIVE_VALIDATOR_COUNT
|
|
||||||
|
|
||||||
func isAfterMinGenesisTime(m: Eth1Monitor, blk: Eth1Block): bool =
|
func isAfterMinGenesisTime(m: Eth1Monitor, blk: Eth1Block): bool =
|
||||||
doAssert blk.timestamp != 0
|
doAssert blk.timestamp != 0
|
||||||
|
@ -189,10 +188,10 @@ when hasGenesisDetection:
|
||||||
func isGenesisCandidate(m: Eth1Monitor, blk: Eth1Block): bool =
|
func isGenesisCandidate(m: Eth1Monitor, blk: Eth1Block): bool =
|
||||||
m.hasEnoughValidators(blk) and m.isAfterMinGenesisTime(blk)
|
m.hasEnoughValidators(blk) and m.isAfterMinGenesisTime(blk)
|
||||||
|
|
||||||
func findGenesisBlockInRange(m: Eth1Monitor, startBlock, endBlock: Eth1Block):
|
proc findGenesisBlockInRange(m: Eth1Monitor, startBlock, endBlock: Eth1Block):
|
||||||
Future[Eth1Block] {.async, gcsafe.}
|
Future[Eth1Block] {.gcsafe.}
|
||||||
|
|
||||||
func signalGenesis(m: Eth1Monitor, genesisState: BeaconStateRef) =
|
proc signalGenesis(m: Eth1Monitor, genesisState: GenesisStateRef) =
|
||||||
m.genesisState = genesisState
|
m.genesisState = genesisState
|
||||||
|
|
||||||
if not m.genesisStateFut.isNil:
|
if not m.genesisStateFut.isNil:
|
||||||
|
@ -200,10 +199,10 @@ when hasGenesisDetection:
|
||||||
m.genesisStateFut = nil
|
m.genesisStateFut = nil
|
||||||
|
|
||||||
func allGenesisDepositsUpTo(m: Eth1Monitor, totalDeposits: uint64): seq[DepositData] =
|
func allGenesisDepositsUpTo(m: Eth1Monitor, totalDeposits: uint64): seq[DepositData] =
|
||||||
for i in 0'u64 ..< totalDeposits:
|
for i in 0 ..< int64(totalDeposits):
|
||||||
result.add m.db.genesisDeposits.get(i)
|
result.add m.depositsChain.db.genesisDeposits.get(i)
|
||||||
|
|
||||||
func createGenesisState(m: Eth1Monitor, eth1Block: Eth1Block): BeaconStateRef =
|
proc createGenesisState(m: Eth1Monitor, eth1Block: Eth1Block): GenesisStateRef =
|
||||||
notice "Generating genesis state",
|
notice "Generating genesis state",
|
||||||
blockNum = eth1Block.number,
|
blockNum = eth1Block.number,
|
||||||
blockHash = eth1Block.voteData.block_hash,
|
blockHash = eth1Block.voteData.block_hash,
|
||||||
|
@ -222,7 +221,7 @@ when hasGenesisDetection:
|
||||||
if eth1Block.activeValidatorsCount != 0:
|
if eth1Block.activeValidatorsCount != 0:
|
||||||
doAssert result.validators.lenu64 == eth1Block.activeValidatorsCount
|
doAssert result.validators.lenu64 == eth1Block.activeValidatorsCount
|
||||||
|
|
||||||
func produceDerivedData(m: Eth1Monitor, deposit: DepositData) =
|
proc produceDerivedData(m: Eth1Monitor, deposit: DepositData) =
|
||||||
let htr = hash_tree_root(deposit)
|
let htr = hash_tree_root(deposit)
|
||||||
|
|
||||||
if verify_deposit_signature(m.cfg, deposit):
|
if verify_deposit_signature(m.cfg, deposit):
|
||||||
|
@ -234,16 +233,13 @@ when hasGenesisDetection:
|
||||||
withdrawal_credentials: deposit.withdrawal_credentials)
|
withdrawal_credentials: deposit.withdrawal_credentials)
|
||||||
m.genesisValidatorKeyToIndex[pubkey] = idx
|
m.genesisValidatorKeyToIndex[pubkey] = idx
|
||||||
|
|
||||||
func processGenesisDeposit*(m: Eth1Monitor, newDeposit: DepositData) =
|
proc processGenesisDeposit*(m: Eth1Monitor, newDeposit: DepositData) =
|
||||||
m.db.genesisDeposits.add newDeposit
|
m.depositsChain.db.genesisDeposits.add newDeposit
|
||||||
m.produceDerivedData(newDeposit)
|
m.produceDerivedData(newDeposit)
|
||||||
|
|
||||||
template blocks*(m: Eth1Monitor): Deque[Eth1Block] =
|
template blocks*(m: Eth1Monitor): Deque[Eth1Block] =
|
||||||
m.depositsChain.blocks
|
m.depositsChain.blocks
|
||||||
|
|
||||||
template cfg(m: Eth1Monitor): auto =
|
|
||||||
m.depositsChain.cfg
|
|
||||||
|
|
||||||
template finalizedDepositsMerkleizer(m: Eth1Monitor): auto =
|
template finalizedDepositsMerkleizer(m: Eth1Monitor): auto =
|
||||||
m.depositsChain.finalizedDepositsMerkleizer
|
m.depositsChain.finalizedDepositsMerkleizer
|
||||||
|
|
||||||
|
@ -1014,7 +1010,7 @@ proc syncBlockRange(m: Eth1Monitor,
|
||||||
eth1Block: blocksWithDeposits[^1].voteData.block_hash,
|
eth1Block: blocksWithDeposits[^1].voteData.block_hash,
|
||||||
depositContractState: merkleizer[].toDepositContractState)
|
depositContractState: merkleizer[].toDepositContractState)
|
||||||
|
|
||||||
m.db.putEth2FinalizedTo depositContractState
|
m.depositsChain.db.putEth2FinalizedTo depositContractState
|
||||||
|
|
||||||
if m.genesisStateFut != nil and m.chainHasEnoughValidators:
|
if m.genesisStateFut != nil and m.chainHasEnoughValidators:
|
||||||
let lastIdx = m.depositsChain.blocks.len - 1
|
let lastIdx = m.depositsChain.blocks.len - 1
|
||||||
|
@ -1255,17 +1251,16 @@ proc testWeb3Provider*(web3Url: Uri,
|
||||||
|
|
||||||
when hasGenesisDetection:
|
when hasGenesisDetection:
|
||||||
proc init*(T: type Eth1Monitor,
|
proc init*(T: type Eth1Monitor,
|
||||||
db: BeaconChainDB,
|
|
||||||
cfg: RuntimeConfig,
|
cfg: RuntimeConfig,
|
||||||
|
db: BeaconChainDB,
|
||||||
web3Urls: seq[string],
|
web3Urls: seq[string],
|
||||||
depositContractAddress: Eth1Address,
|
|
||||||
depositContractDeployedAt: BlockHashOrNumber,
|
depositContractDeployedAt: BlockHashOrNumber,
|
||||||
eth1Network: Option[Eth1Network],
|
eth1Network: Option[Eth1Network],
|
||||||
forcePolling: bool): Future[Result[T, string]] {.async.} =
|
forcePolling: bool): Future[Result[T, string]] {.async.} =
|
||||||
doAssert web3Urls.len > 0
|
doAssert web3Urls.len > 0
|
||||||
try:
|
try:
|
||||||
var urlIdx = 0
|
var urlIdx = 0
|
||||||
let dataProviderRes = await Web3DataProvider.new(depositContractAddress, web3Urls[urlIdx])
|
let dataProviderRes = await Web3DataProvider.new(cfg.DEPOSIT_CONTRACT_ADDRESS, web3Urls[urlIdx])
|
||||||
if dataProviderRes.isErr:
|
if dataProviderRes.isErr:
|
||||||
return err(dataProviderRes.error)
|
return err(dataProviderRes.error)
|
||||||
var dataProvider = dataProviderRes.get
|
var dataProvider = dataProviderRes.get
|
||||||
|
@ -1292,7 +1287,7 @@ when hasGenesisDetection:
|
||||||
# the Eth1Monitor will be restarted.
|
# the Eth1Monitor will be restarted.
|
||||||
inc urlIdx
|
inc urlIdx
|
||||||
dataProvider = tryGet(
|
dataProvider = tryGet(
|
||||||
await Web3DataProvider.new(depositContractAddress,
|
await Web3DataProvider.new(cfg.DEPOSIT_CONTRACT_ADDRESS,
|
||||||
web3Urls[urlIdx mod web3Urls.len]))
|
web3Urls[urlIdx mod web3Urls.len]))
|
||||||
blk.hash.asEth2Digest
|
blk.hash.asEth2Digest
|
||||||
|
|
||||||
|
@ -1300,10 +1295,9 @@ when hasGenesisDetection:
|
||||||
eth1Block: knownStartBlockHash)
|
eth1Block: knownStartBlockHash)
|
||||||
|
|
||||||
var monitor = Eth1Monitor.init(
|
var monitor = Eth1Monitor.init(
|
||||||
db,
|
|
||||||
cfg,
|
cfg,
|
||||||
|
db,
|
||||||
web3Urls,
|
web3Urls,
|
||||||
depositContractAddress,
|
|
||||||
depositContractSnapshot,
|
depositContractSnapshot,
|
||||||
eth1Network,
|
eth1Network,
|
||||||
forcePolling)
|
forcePolling)
|
||||||
|
@ -1362,7 +1356,7 @@ when hasGenesisDetection:
|
||||||
|
|
||||||
return endBlock
|
return endBlock
|
||||||
|
|
||||||
proc waitGenesis*(m: Eth1Monitor): Future[BeaconStateRef] {.async.} =
|
proc waitGenesis*(m: Eth1Monitor): Future[GenesisStateRef] {.async.} =
|
||||||
if m.genesisState.isNil:
|
if m.genesisState.isNil:
|
||||||
m.start()
|
m.start()
|
||||||
|
|
||||||
|
@ -1377,4 +1371,4 @@ when hasGenesisDetection:
|
||||||
return m.genesisState
|
return m.genesisState
|
||||||
else:
|
else:
|
||||||
doAssert bnStatus == BeaconNodeStatus.Stopping
|
doAssert bnStatus == BeaconNodeStatus.Stopping
|
||||||
return new BeaconStateRef # cannot return nil...
|
return nil
|
||||||
|
|
|
@ -206,7 +206,7 @@ proc init(T: type BeaconNode,
|
||||||
|
|
||||||
if genesisStateContents.len == 0 and checkpointState == nil:
|
if genesisStateContents.len == 0 and checkpointState == nil:
|
||||||
when hasGenesisDetection:
|
when hasGenesisDetection:
|
||||||
if genesisDepositsSnapshotContents != nil:
|
if genesisDepositsSnapshotContents.len > 0:
|
||||||
fatal "A deposits snapshot cannot be provided without also providing a matching beacon state snapshot"
|
fatal "A deposits snapshot cannot be provided without also providing a matching beacon state snapshot"
|
||||||
quit 1
|
quit 1
|
||||||
|
|
||||||
|
@ -236,7 +236,11 @@ proc init(T: type BeaconNode,
|
||||||
else:
|
else:
|
||||||
eth1Monitor = eth1MonitorRes.get
|
eth1Monitor = eth1MonitorRes.get
|
||||||
|
|
||||||
genesisState = waitFor eth1Monitor.waitGenesis()
|
let phase0Genesis = waitFor eth1Monitor.waitGenesis()
|
||||||
|
genesisState = newClone ForkedHashedBeaconState.init(
|
||||||
|
phase0.HashedBeaconState(data: phase0Genesis[],
|
||||||
|
root: hash_tree_root(phase0Genesis[])))
|
||||||
|
|
||||||
if bnStatus == BeaconNodeStatus.Stopping:
|
if bnStatus == BeaconNodeStatus.Stopping:
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
|
@ -244,9 +248,9 @@ proc init(T: type BeaconNode,
|
||||||
tailBlock = get_initial_beacon_block(genesisState[])
|
tailBlock = get_initial_beacon_block(genesisState[])
|
||||||
|
|
||||||
notice "Eth2 genesis state detected",
|
notice "Eth2 genesis state detected",
|
||||||
genesisTime = genesisState.genesisTime,
|
genesisTime = phase0Genesis.genesisTime,
|
||||||
eth1Block = genesisState.eth1_data.block_hash,
|
eth1Block = phase0Genesis.eth1_data.block_hash,
|
||||||
totalDeposits = genesisState.eth1_data.deposit_count
|
totalDeposits = phase0Genesis.eth1_data.deposit_count
|
||||||
else:
|
else:
|
||||||
fatal "No database and no genesis snapshot found: supply a genesis.ssz " &
|
fatal "No database and no genesis snapshot found: supply a genesis.ssz " &
|
||||||
"with the network configuration, or compile the beacon node with " &
|
"with the network configuration, or compile the beacon node with " &
|
||||||
|
|
|
@ -320,7 +320,7 @@ func get_initial_beacon_block*(state: merge.HashedBeaconState):
|
||||||
# The genesis block is implicitly trusted
|
# The genesis block is implicitly trusted
|
||||||
let message = merge.TrustedBeaconBlock(
|
let message = merge.TrustedBeaconBlock(
|
||||||
slot: state.data.slot,
|
slot: state.data.slot,
|
||||||
state_root: state.root,)
|
state_root: state.root)
|
||||||
# parent_root, randao_reveal, eth1_data, signature, and body automatically
|
# parent_root, randao_reveal, eth1_data, signature, and body automatically
|
||||||
# initialized to default values.
|
# initialized to default values.
|
||||||
merge.TrustedSignedBeaconBlock(
|
merge.TrustedSignedBeaconBlock(
|
||||||
|
|
Loading…
Reference in New Issue