Allow the deposit contract deployment block to be specified as a number (needed for Medalla)

This commit is contained in:
Zahary Karadjov 2020-07-28 16:36:11 +03:00 committed by zah
parent b45de824a4
commit 0c60e452e7
5 changed files with 28 additions and 14 deletions

View File

@ -165,14 +165,28 @@ proc init*(T: type BeaconNode,
fatal "Deposit contract deployment block not specified" fatal "Deposit contract deployment block not specified"
quit 1 quit 1
let web3 = web3Provider(conf.web3Url)
let deployedAtAsHash =
if conf.depositContractDeployedAt.get.startsWith "0x":
try: BlockHash.fromHex conf.depositContractDeployedAt.get
except ValueError:
fatal "Invalid hex value specified for deposit-contract-block"
quit 1
else:
let blockNum = try: parseBiggestUInt conf.depositContractDeployedAt.get
except ValueError:
fatal "Invalid nummeric value for deposit-contract-block"
quit 1
await getEth1BlockHash(conf.web3Url, blockId blockNum)
# TODO Could move this to a separate "GenesisMonitor" process or task # TODO Could move this to a separate "GenesisMonitor" process or task
# that would do only this - see Paul's proposal for this. # that would do only this - see Paul's proposal for this.
mainchainMonitor = MainchainMonitor.init( mainchainMonitor = MainchainMonitor.init(
conf.runtimePreset, conf.runtimePreset,
web3Provider(conf.web3Url), web3,
conf.depositContractAddress.get, conf.depositContractAddress.get,
Eth1Data(block_hash: conf.depositContractDeployedAt.get.asEth2Digest, Eth1Data(block_hash: deployedAtAsHash.asEth2Digest, deposit_count: 0))
deposit_count: 0))
mainchainMonitor.start() mainchainMonitor.start()
genesisState = await mainchainMonitor.waitGenesis() genesisState = await mainchainMonitor.waitGenesis()
@ -1097,7 +1111,7 @@ programMain:
startTime = uint64(times.toUnix(times.getTime()) + config.genesisOffset) startTime = uint64(times.toUnix(times.getTime()) + config.genesisOffset)
outGenesis = config.outputGenesis.string outGenesis = config.outputGenesis.string
eth1Hash = if config.web3Url.len == 0: eth1BlockHash eth1Hash = if config.web3Url.len == 0: eth1BlockHash
else: waitFor getLatestEth1BlockHash(config.web3Url) else: (waitFor getEth1BlockHash(config.web3Url, blockId("latest"))).asEth2Digest
var var
initialState = initialize_beacon_state_from_eth1( initialState = initialize_beacon_state_from_eth1(
defaultRuntimePreset, eth1Hash, startTime, deposits, {skipBlsValidation}) defaultRuntimePreset, eth1Hash, startTime, deposits, {skipBlsValidation})

View File

@ -68,8 +68,8 @@ type
name: "deposit-contract" }: Option[Eth1Address] name: "deposit-contract" }: Option[Eth1Address]
depositContractDeployedAt* {. depositContractDeployedAt* {.
desc: "The Eth1 block hash where the deposit contract has been deployed" desc: "The Eth1 block number or hash where the deposit contract has been deployed"
name: "deposit-contract-block" }: Option[Eth1BlockHash] name: "deposit-contract-block" }: Option[string]
nonInteractive* {. nonInteractive* {.
desc: "Do not display interative prompts. Quit on missing configuration" desc: "Do not display interative prompts. Quit on missing configuration"

View File

@ -798,11 +798,11 @@ proc start(m: MainchainMonitor, delayBeforeStart: Duration) =
proc start*(m: MainchainMonitor) {.inline.} = proc start*(m: MainchainMonitor) {.inline.} =
m.start(0.seconds) m.start(0.seconds)
proc getLatestEth1BlockHash*(url: string): Future[Eth2Digest] {.async.} = proc getEth1BlockHash*(url: string, blockId: RtBlockIdentifier): Future[BlockHash] {.async.} =
let web3 = await newWeb3(url) let web3 = await newWeb3(url)
try: try:
let blk = await web3.provider.eth_getBlockByNumber("latest", false) let blk = await web3.provider.eth_getBlockByNumber(blockId, false)
return Eth2Digest(data: array[32, byte](blk.hash)) return blk.hash
finally: finally:
await web3.close() await web3.close()

View File

@ -38,7 +38,7 @@ type
bootstrapNodes*: seq[string] bootstrapNodes*: seq[string]
depositContractAddress*: Eth1Address depositContractAddress*: Eth1Address
depositContractDeployedAt*: Eth1BlockHash depositContractDeployedAt*: string
# Please note that we are using `string` here because SSZ.decode # Please note that we are using `string` here because SSZ.decode
# is not currently usable at compile time and we want to load the # is not currently usable at compile time and we want to load the
@ -110,9 +110,9 @@ proc loadEth2NetworkMetadata*(path: string): Eth2NetworkMetadata
default(Eth1Address) default(Eth1Address)
depositContractBlock = if fileExists(depositContractBlockPath): depositContractBlock = if fileExists(depositContractBlockPath):
Eth1BlockHash.fromHex readFile(depositContractBlockPath).strip readFile(depositContractBlockPath).strip
else: else:
default(Eth1BlockHash) ""
bootstrapNodes = if fileExists(bootstrapNodesPath): bootstrapNodes = if fileExists(bootstrapNodesPath):
readFile(bootstrapNodesPath).splitLines() readFile(bootstrapNodesPath).splitLines()
@ -145,7 +145,7 @@ const
# TODO The values below are just placeholders for now # TODO The values below are just placeholders for now
bootstrapNodes: @[], bootstrapNodes: @[],
depositContractAddress: Eth1Address.fromHex "0x1234567890123456789012345678901234567890", depositContractAddress: Eth1Address.fromHex "0x1234567890123456789012345678901234567890",
depositContractDeployedAt: Eth1BlockHash.fromHex "0x73056f16a59bf70abad5b4365438e8a7d646aa0d7f56d22c3d9e4c6000d8e176", depositContractDeployedAt: "0",
genesisData: "") genesisData: "")
else: else:
Eth2NetworkMetadata( Eth2NetworkMetadata(

2
vendor/nim-web3 vendored

@ -1 +1 @@
Subproject commit 4fe1a81ce7e16413b01467a2f88058127941170e Subproject commit 9fb271b87697bafff9f27c73d5695d9aba648921