This commit is contained in:
Zahary Karadjov 2020-11-12 21:01:26 +02:00 committed by zah
parent 17d35e1fd9
commit 80ca7a2d9f
5 changed files with 43 additions and 28 deletions

View File

@ -5,6 +5,7 @@ import
chronicles, chronicles/options as chroniclesOptions,
confutils, confutils/defs, confutils/std/net, stew/shims/net as stewNet,
stew/io2, unicodedb/properties, normalize,
eth/common/eth_types as commonEthTypes,
json_serialization, web3/[ethtypes, confutils_defs],
spec/[crypto, keystore, digest, datatypes, network],
network_metadata, filepath
@ -78,7 +79,7 @@ type
depositContractDeployedAt* {.
desc: "The Eth1 block number or hash where the deposit contract has been deployed"
name: "deposit-contract-block" }: Option[string]
name: "deposit-contract-block" }: Option[BlockHashOrNumber]
nonInteractive* {.
desc: "Do not display interative prompts. Quit on missing configuration"
@ -451,6 +452,13 @@ func parseCmdArg*(T: type GraffitiBytes, input: TaintedString): T
func completeCmdArg*(T: type GraffitiBytes, input: TaintedString): seq[string] =
return @[]
func parseCmdArg*(T: type BlockHashOrNumber, input: TaintedString): T
{.raises: [ValueError, Defect].} =
init(BlockHashOrNumber, string input)
func completeCmdArg*(T: type BlockHashOrNumber, input: TaintedString): seq[string] =
return @[]
func parseCmdArg*(T: type Checkpoint, input: TaintedString): T
{.raises: [ValueError, Defect].} =
let sepIdx = find(input.string, ':')

View File

@ -48,6 +48,7 @@ type
Eth1Monitor* = ref object
db: BeaconChainDB
preset: RuntimePreset
depositContractDeployedAt: BlockHashOrNumber
dataProvider: Web3DataProviderRef
@ -395,7 +396,7 @@ proc init*(T: type Eth1Monitor,
preset: RuntimePreset,
web3Url: string,
depositContractAddress: Eth1Address,
depositContractDeployedAt: string,
depositContractDeployedAt: BlockHashOrNumber,
eth1Network: Option[Eth1Network]): Future[Result[T, string]] {.async.} =
var web3Url = web3Url
fixupWeb3Urls web3Url
@ -422,31 +423,12 @@ proc init*(T: type Eth1Monitor,
return err("The specified web3 provider is not attached to the " &
$eth1Network.get & " network")
let
previouslyPersistedTo = db.getEth1PersistedTo()
knownStart = previouslyPersistedTo.get:
# `previouslyPersistedTo` wall null, we start from scratch
let deployedAtHash = if depositContractDeployedAt.startsWith "0x":
try: BlockHash.fromHex depositContractDeployedAt
except ValueError:
return err "Invalid hex value specified for deposit-contract-block"
else:
let blockNum = try: parseBiggestUInt depositContractDeployedAt
except ValueError:
return err "Invalid nummeric value for deposit-contract-block"
try:
let blk = await dataProvider.getBlockByNumber(blockNum)
blk.hash
except CatchableError:
return err("Failed to obtain block hash for block number " & $blockNum)
Eth1Data(block_hash: deployedAtHash.asEth2Digest, deposit_count: 0)
return ok T(
db: db,
preset: preset,
depositContractDeployedAt: depositContractDeployedAt,
dataProvider: dataProvider,
eth1Progress: newAsyncEvent(),
eth1Chain: Eth1Chain(knownStart: knownStart))
eth1Progress: newAsyncEvent())
proc allDepositsUpTo(m: Eth1Monitor, totalDeposits: uint64): seq[Deposit] =
for i in 0'u64 ..< totalDeposits:
@ -643,6 +625,7 @@ proc handleEth1Progress(m: Eth1Monitor) {.async.} =
# If we had the same code embedded in the new block headers event,
# it could easily re-order the steps due to the interruptible
# interleaved execution of async code.
var eth1SyncedTo = await m.dataProvider.getBlockNumber(
m.eth1Chain.knownStart.block_hash.asBlockHash)
@ -686,6 +669,24 @@ proc run(m: Eth1Monitor, delayBeforeStart: Duration) {.async.} =
info "Starting Eth1 deposit contract monitoring",
contract = $m.depositContractAddress, url = m.web3Url
let previouslyPersistedTo = m.db.getEth1PersistedTo()
m.eth1Chain.knownStart = previouslyPersistedTo.get:
# `previouslyPersistedTo` wall null, we start from scratch
let deployedAtHash = if m.depositContractDeployedAt.isHash:
m.depositContractDeployedAt.hash
else:
var blk: BlockObject
while true:
try:
blk = await m.dataProvider.getBlockByNumber(m.depositContractDeployedAt.number)
break
except CatchableError as err:
error "Failed to obtain details for the starting block of the deposit contract sync. " &
"The Web3 provider may still be not fully synced", error = err.msg
await sleepAsync(chronos.seconds(10))
blk.hash.asEth2Digest
Eth1Data(block_hash: deployedAtHash, deposit_count: 0)
await m.dataProvider.onBlockHeaders do (blk: Eth1BlockHeader)
{.raises: [Defect], gcsafe.}:
try:

View File

@ -1,6 +1,7 @@
import
tables, strutils, os,
stew/shims/macros, nimcrypto/hash,
eth/common/eth_types as commonEthTypes,
web3/[ethtypes, conversions],
chronicles,
spec/presets,
@ -47,7 +48,7 @@ type
bootstrapNodes*: seq[string]
depositContractAddress*: Eth1Address
depositContractDeployedAt*: string
depositContractDeployedAt*: BlockHashOrNumber
# Please note that we are using `string` here because SSZ.decode
# is not currently usable at compile time and we want to load the
@ -123,6 +124,11 @@ proc loadEth2NetworkMetadata*(path: string): Eth2NetworkMetadata
else:
""
depositContractDeployedAt = if depositContractBlock.len > 0:
BlockHashOrNumber.init(depositContractBlock)
else:
BlockHashOrNumber(isHash: false, number: 1)
bootstrapNodes = if fileExists(bootstrapNodesPath):
readFile(bootstrapNodesPath).splitLines()
else:
@ -137,7 +143,7 @@ proc loadEth2NetworkMetadata*(path: string): Eth2NetworkMetadata
runtimePreset: runtimePreset,
bootstrapNodes: bootstrapNodes,
depositContractAddress: depositContractAddress,
depositContractDeployedAt: depositContractBlock,
depositContractDeployedAt: depositContractDeployedAt,
genesisData: genesisData)
except PresetIncompatible as err:
@ -154,7 +160,7 @@ const
# TODO(zah) Add bootstrap nodes for mainnet
bootstrapNodes: @[],
depositContractAddress: Eth1Address.fromHex "0x00000000219ab540356cBB839Cbe05303d7705Fa",
depositContractDeployedAt: "11052984",
depositContractDeployedAt: BlockHashOrNumber.init "11052984",
genesisData: "")
else:
Eth2NetworkMetadata(

2
vendor/nim-eth vendored

@ -1 +1 @@
Subproject commit 362675552965c1930346c08cc3dd0a62b990aa8c
Subproject commit 5dff021cbc97f56ea5ef73b74a392b53b20638d0

@ -1 +1 @@
Subproject commit 3b0c9eafa4eb75c6257ec5cd08cf6d25c31119a6
Subproject commit 32f75d93b0762328d1d85ce62cef84ed919ae31e