Skip to content

Commit

Permalink
Allow the deposit contract deployment block to be specified as a numb…
Browse files Browse the repository at this point in the history
…er (needed for Medalla)
  • Loading branch information
zah committed Jul 28, 2020
1 parent b45de82 commit 0c60e45
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 14 deletions.
22 changes: 18 additions & 4 deletions beacon_chain/beacon_node.nim
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,28 @@ proc init*(T: type BeaconNode,
fatal "Deposit contract deployment block not specified"
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
# that would do only this - see Paul's proposal for this.
mainchainMonitor = MainchainMonitor.init(
conf.runtimePreset,
web3Provider(conf.web3Url),
web3,
conf.depositContractAddress.get,
Eth1Data(block_hash: conf.depositContractDeployedAt.get.asEth2Digest,
deposit_count: 0))
Eth1Data(block_hash: deployedAtAsHash.asEth2Digest, deposit_count: 0))

mainchainMonitor.start()

genesisState = await mainchainMonitor.waitGenesis()
Expand Down Expand Up @@ -1097,7 +1111,7 @@ programMain:
startTime = uint64(times.toUnix(times.getTime()) + config.genesisOffset)
outGenesis = config.outputGenesis.string
eth1Hash = if config.web3Url.len == 0: eth1BlockHash
else: waitFor getLatestEth1BlockHash(config.web3Url)
else: (waitFor getEth1BlockHash(config.web3Url, blockId("latest"))).asEth2Digest
var
initialState = initialize_beacon_state_from_eth1(
defaultRuntimePreset, eth1Hash, startTime, deposits, {skipBlsValidation})
Expand Down
4 changes: 2 additions & 2 deletions beacon_chain/conf.nim
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ type
name: "deposit-contract" }: Option[Eth1Address]

depositContractDeployedAt* {.
desc: "The Eth1 block hash where the deposit contract has been deployed"
name: "deposit-contract-block" }: Option[Eth1BlockHash]
desc: "The Eth1 block number or hash where the deposit contract has been deployed"
name: "deposit-contract-block" }: Option[string]

nonInteractive* {.
desc: "Do not display interative prompts. Quit on missing configuration"
Expand Down
6 changes: 3 additions & 3 deletions beacon_chain/mainchain_monitor.nim
Original file line number Diff line number Diff line change
Expand Up @@ -798,11 +798,11 @@ proc start(m: MainchainMonitor, delayBeforeStart: Duration) =
proc start*(m: MainchainMonitor) {.inline.} =
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)
try:
let blk = await web3.provider.eth_getBlockByNumber("latest", false)
return Eth2Digest(data: array[32, byte](blk.hash))
let blk = await web3.provider.eth_getBlockByNumber(blockId, false)
return blk.hash
finally:
await web3.close()

Expand Down
8 changes: 4 additions & 4 deletions beacon_chain/network_metadata.nim
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type
bootstrapNodes*: seq[string]

depositContractAddress*: Eth1Address
depositContractDeployedAt*: Eth1BlockHash
depositContractDeployedAt*: string

# Please note that we are using `string` here because SSZ.decode
# is not currently usable at compile time and we want to load the
Expand Down Expand Up @@ -110,9 +110,9 @@ proc loadEth2NetworkMetadata*(path: string): Eth2NetworkMetadata
default(Eth1Address)

depositContractBlock = if fileExists(depositContractBlockPath):
Eth1BlockHash.fromHex readFile(depositContractBlockPath).strip
readFile(depositContractBlockPath).strip
else:
default(Eth1BlockHash)
""

bootstrapNodes = if fileExists(bootstrapNodesPath):
readFile(bootstrapNodesPath).splitLines()
Expand Down Expand Up @@ -145,7 +145,7 @@ const
# TODO The values below are just placeholders for now
bootstrapNodes: @[],
depositContractAddress: Eth1Address.fromHex "0x1234567890123456789012345678901234567890",
depositContractDeployedAt: Eth1BlockHash.fromHex "0x73056f16a59bf70abad5b4365438e8a7d646aa0d7f56d22c3d9e4c6000d8e176",
depositContractDeployedAt: "0",
genesisData: "")
else:
Eth2NetworkMetadata(
Expand Down
2 changes: 1 addition & 1 deletion vendor/nim-web3
Submodule nim-web3 updated 1 files
+2 −2 web3/ethtypes.nim

0 comments on commit 0c60e45

Please sign in to comment.