2018-11-23 23:58:49 +00:00
|
|
|
import
|
2019-10-02 12:38:14 +00:00
|
|
|
# Standard library
|
2019-12-10 14:20:40 +00:00
|
|
|
os, net, tables, random, strutils, times,
|
2019-10-02 12:38:14 +00:00
|
|
|
|
|
|
|
# Nimble packages
|
2019-12-10 14:20:40 +00:00
|
|
|
stew/[objects, bitseqs, byteutils], stew/ranges/ptr_arith,
|
2019-09-07 17:48:05 +00:00
|
|
|
chronos, chronicles, confutils, metrics,
|
2019-09-10 21:55:58 +00:00
|
|
|
json_serialization/std/[options, sets], serialization/errors,
|
2019-03-28 11:01:28 +00:00
|
|
|
eth/trie/db, eth/trie/backends/rocksdb_backend, eth/async_utils,
|
2019-10-02 12:38:14 +00:00
|
|
|
|
|
|
|
# Local modules
|
2019-10-25 10:59:56 +00:00
|
|
|
spec/[datatypes, digest, crypto, beaconstate, helpers, validator, network],
|
2019-12-19 13:02:28 +00:00
|
|
|
conf, time, state_transition, beacon_chain_db,
|
2019-06-17 11:08:05 +00:00
|
|
|
validator_pool, extras, attestation_pool, block_pool, eth2_network,
|
2019-11-09 10:46:34 +00:00
|
|
|
beacon_node_types, mainchain_monitor, version, ssz, ssz/dynamic_navigator,
|
2019-10-03 01:51:44 +00:00
|
|
|
sync_protocol, request_manager, validator_keygen, interop, statusbar
|
2018-11-23 23:58:49 +00:00
|
|
|
|
|
|
|
const
|
2019-10-29 16:46:41 +00:00
|
|
|
genesisFile = "genesis.ssz"
|
2019-10-02 12:38:14 +00:00
|
|
|
hasPrompt = not defined(withoutPrompt)
|
2019-12-02 14:42:57 +00:00
|
|
|
maxEmptySlotCount = uint64(24*60*60) div SECONDS_PER_SLOT
|
2019-03-18 03:54:08 +00:00
|
|
|
|
2019-10-24 06:51:27 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-metrics/blob/master/metrics.md#interop-metrics
|
2019-10-25 17:15:12 +00:00
|
|
|
declareGauge beacon_slot,
|
|
|
|
"Latest slot of the beacon chain state"
|
|
|
|
declareGauge beacon_head_slot,
|
|
|
|
"Slot of the head block of the beacon chain"
|
|
|
|
declareGauge beacon_head_root,
|
|
|
|
"Root of the head block of the beacon chain"
|
2019-09-07 17:48:05 +00:00
|
|
|
|
2019-10-24 06:51:27 +00:00
|
|
|
# Metrics for tracking attestation and beacon block loss
|
2019-10-25 17:15:12 +00:00
|
|
|
declareCounter beacon_attestations_sent,
|
|
|
|
"Number of beacon chain attestations sent by this peer"
|
|
|
|
declareCounter beacon_attestations_received,
|
|
|
|
"Number of beacon chain attestations received by this peer"
|
|
|
|
declareCounter beacon_blocks_proposed,
|
|
|
|
"Number of beacon chain blocks sent by this peer"
|
|
|
|
declareCounter beacon_blocks_received,
|
|
|
|
"Number of beacon chain blocks received by this peer"
|
2019-10-24 06:51:27 +00:00
|
|
|
|
2019-09-12 01:45:04 +00:00
|
|
|
logScope: topics = "beacnde"
|
|
|
|
|
2019-11-25 14:36:25 +00:00
|
|
|
type
|
|
|
|
BeaconNode = ref object
|
|
|
|
nickname: string
|
|
|
|
network: Eth2Node
|
|
|
|
forkVersion: array[4, byte]
|
|
|
|
networkIdentity: Eth2NodeIdentity
|
|
|
|
requestManager: RequestManager
|
|
|
|
bootstrapNodes: seq[BootstrapAddr]
|
|
|
|
db: BeaconChainDB
|
|
|
|
config: BeaconNodeConf
|
|
|
|
attachedValidators: ValidatorPool
|
|
|
|
blockPool: BlockPool
|
|
|
|
attestationPool: AttestationPool
|
|
|
|
mainchainMonitor: MainchainMonitor
|
|
|
|
beaconClock: BeaconClock
|
|
|
|
|
2019-12-16 18:08:50 +00:00
|
|
|
proc onBeaconBlock*(node: BeaconNode, blck: SignedBeaconBlock) {.gcsafe.}
|
2019-12-13 11:54:48 +00:00
|
|
|
proc updateHead(node: BeaconNode): BlockRef
|
2019-02-18 10:34:39 +00:00
|
|
|
|
2019-03-25 23:26:11 +00:00
|
|
|
proc saveValidatorKey(keyName, key: string, conf: BeaconNodeConf) =
|
2019-12-10 14:20:40 +00:00
|
|
|
let validatorsDir = conf.localValidatorsDir
|
2019-03-25 23:26:11 +00:00
|
|
|
let outputFile = validatorsDir / keyName
|
2019-03-19 21:04:22 +00:00
|
|
|
createDir validatorsDir
|
2019-03-25 23:26:11 +00:00
|
|
|
writeFile(outputFile, key)
|
|
|
|
info "Imported validator key", file = outputFile
|
2019-03-18 03:54:08 +00:00
|
|
|
|
2019-10-25 14:53:31 +00:00
|
|
|
proc getStateFromSnapshot(node: BeaconNode, state: var BeaconState): bool =
|
|
|
|
template conf: untyped = node.config
|
2019-09-09 18:10:19 +00:00
|
|
|
|
2019-11-09 10:46:34 +00:00
|
|
|
var
|
|
|
|
genesisPath = conf.dataDir/genesisFile
|
|
|
|
snapshotContents: TaintedString
|
|
|
|
writeGenesisFile = false
|
2019-09-09 18:10:19 +00:00
|
|
|
|
2019-11-09 10:46:34 +00:00
|
|
|
if conf.stateSnapshot.isSome:
|
|
|
|
let
|
|
|
|
snapshotPath = conf.stateSnapshot.get.string
|
|
|
|
snapshotExt = splitFile(snapshotPath).ext
|
|
|
|
|
|
|
|
if cmpIgnoreCase(snapshotExt, ".ssz") != 0:
|
|
|
|
error "The supplied state snapshot must be a SSZ file",
|
|
|
|
suppliedPath = snapshotPath
|
|
|
|
quit 1
|
|
|
|
|
|
|
|
snapshotContents = readFile(snapshotPath)
|
|
|
|
if fileExists(genesisPath):
|
|
|
|
let genesisContents = readFile(genesisPath)
|
|
|
|
if snapshotContents != genesisContents:
|
|
|
|
error "Data directory not empty. Existing genesis state differs from supplied snapshot",
|
|
|
|
dataDir = conf.dataDir.string, snapshot = snapshotPath
|
2019-09-09 18:10:19 +00:00
|
|
|
quit 1
|
2019-11-09 10:46:34 +00:00
|
|
|
else:
|
2019-11-25 14:36:25 +00:00
|
|
|
debug "No genesis file in data directory", genesisPath
|
2019-11-09 10:46:34 +00:00
|
|
|
writeGenesisFile = true
|
|
|
|
genesisPath = snapshotPath
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
snapshotContents = readFile(genesisPath)
|
|
|
|
except CatchableError as err:
|
|
|
|
error "Failed to read genesis file", err = err.msg
|
2019-07-18 11:36:53 +00:00
|
|
|
quit 1
|
|
|
|
|
2019-11-09 10:46:34 +00:00
|
|
|
try:
|
|
|
|
state = SSZ.decode(snapshotContents, BeaconState)
|
2019-11-25 14:36:25 +00:00
|
|
|
except SerializationError:
|
2019-11-09 10:46:34 +00:00
|
|
|
error "Failed to import genesis file", path = genesisPath
|
|
|
|
quit 1
|
|
|
|
|
2019-11-25 14:36:25 +00:00
|
|
|
info "Loaded genesis state", path = genesisPath
|
|
|
|
|
2019-11-09 10:46:34 +00:00
|
|
|
if writeGenesisFile:
|
|
|
|
try:
|
2019-11-25 14:36:25 +00:00
|
|
|
notice "Writing genesis to data directory", path = conf.dataDir/genesisFile
|
2019-11-09 10:46:34 +00:00
|
|
|
writeFile(conf.dataDir/genesisFile, snapshotContents.string)
|
|
|
|
except CatchableError as err:
|
2019-11-25 14:36:25 +00:00
|
|
|
error "Failed to persist genesis file to data dir",
|
|
|
|
err = err.msg, genesisFile = conf.dataDir/genesisFile
|
2019-11-09 10:46:34 +00:00
|
|
|
quit 1
|
|
|
|
|
|
|
|
result = true
|
2019-10-25 14:53:31 +00:00
|
|
|
|
|
|
|
proc commitGenesisState(node: BeaconNode, tailState: BeaconState) =
|
2019-07-18 11:36:53 +00:00
|
|
|
info "Got genesis state", hash = hash_tree_root(tailState)
|
2019-08-05 00:00:49 +00:00
|
|
|
node.forkVersion = tailState.fork.current_version
|
2019-07-18 11:36:53 +00:00
|
|
|
try:
|
|
|
|
let tailBlock = get_initial_beacon_block(tailState)
|
|
|
|
BlockPool.preInit(node.db, tailState, tailBlock)
|
|
|
|
|
2019-12-02 15:38:18 +00:00
|
|
|
except CatchableError as e:
|
2019-07-18 11:36:53 +00:00
|
|
|
stderr.write "Failed to initialize database\n"
|
2019-12-02 15:38:18 +00:00
|
|
|
stderr.write e.msg, "\n"
|
2019-07-18 11:36:53 +00:00
|
|
|
quit 1
|
|
|
|
|
2019-10-28 23:04:52 +00:00
|
|
|
proc addBootstrapNode(node: BeaconNode, bootstrapNode: BootstrapAddr) =
|
2019-12-23 15:34:09 +00:00
|
|
|
if not bootstrapNode.isSameNode(node.networkIdentity):
|
2019-10-28 23:04:52 +00:00
|
|
|
node.bootstrapNodes.add bootstrapNode
|
|
|
|
|
2019-12-23 15:34:09 +00:00
|
|
|
proc loadBootstrapFile(node: BeaconNode, bootstrapFile: string) =
|
2019-10-29 13:41:10 +00:00
|
|
|
for ln in lines(bootstrapFile):
|
2019-11-12 19:56:37 +00:00
|
|
|
node.addBootstrapNode BootstrapAddr.initAddress(string ln)
|
2019-10-29 13:41:10 +00:00
|
|
|
|
2019-03-05 22:54:08 +00:00
|
|
|
proc init*(T: type BeaconNode, conf: BeaconNodeConf): Future[BeaconNode] {.async.} =
|
2018-11-23 23:58:49 +00:00
|
|
|
new result
|
|
|
|
result.config = conf
|
2019-05-22 07:13:15 +00:00
|
|
|
result.networkIdentity = getPersistentNetIdentity(conf)
|
2019-10-28 23:04:52 +00:00
|
|
|
result.nickname = if conf.nodeName == "auto": shortForm(result.networkIdentity)
|
|
|
|
else: conf.nodeName
|
2018-12-19 12:58:53 +00:00
|
|
|
|
2019-08-05 00:00:49 +00:00
|
|
|
for bootNode in conf.bootstrapNodes:
|
2019-10-28 23:04:52 +00:00
|
|
|
result.addBootstrapNode BootstrapAddr.init(bootNode)
|
2019-08-05 00:00:49 +00:00
|
|
|
|
|
|
|
let bootstrapFile = string conf.bootstrapNodesFile
|
|
|
|
if bootstrapFile.len > 0:
|
2019-12-23 15:34:09 +00:00
|
|
|
result.loadBootstrapFile(bootstrapFile)
|
2019-10-29 13:41:10 +00:00
|
|
|
|
|
|
|
let siteLocalBootstrapFile = conf.dataDir / "bootstrap_nodes.txt"
|
|
|
|
if fileExists(siteLocalBootstrapFile):
|
2019-12-23 15:34:09 +00:00
|
|
|
result.loadBootstrapFile(siteLocalBootstrapFile)
|
2019-08-05 00:00:49 +00:00
|
|
|
|
2018-12-28 16:51:40 +00:00
|
|
|
result.attachedValidators = ValidatorPool.init
|
|
|
|
|
2019-11-15 11:04:49 +00:00
|
|
|
let trieDB = trieDB newChainDb(conf.databaseDir)
|
2019-01-14 12:19:44 +00:00
|
|
|
result.db = BeaconChainDB.init(trieDB)
|
2019-01-25 14:17:35 +00:00
|
|
|
|
2019-02-28 21:21:29 +00:00
|
|
|
# TODO this is problably not the right place to ensure that db is sane..
|
2019-02-21 04:42:17 +00:00
|
|
|
# TODO does it really make sense to load from DB if a state snapshot has been
|
|
|
|
# specified on command line? potentially, this should be the other way
|
|
|
|
# around...
|
|
|
|
|
2019-10-25 14:53:31 +00:00
|
|
|
var eth1MonitorStartBlock: Eth2Digest
|
2019-07-18 11:36:53 +00:00
|
|
|
if result.db.getHeadBlock().isNone():
|
2019-11-25 14:36:25 +00:00
|
|
|
var state = new BeaconState
|
|
|
|
# TODO getStateFromSnapshot never returns false - it quits..
|
|
|
|
if not result.getStateFromSnapshot(state[]):
|
2019-10-25 14:53:31 +00:00
|
|
|
if conf.depositWeb3Url.len != 0:
|
2019-11-25 14:36:25 +00:00
|
|
|
result.mainchainMonitor = MainchainMonitor.init(
|
|
|
|
conf.depositWeb3Url, conf.depositContractAddress, eth1MonitorStartBlock)
|
2019-10-25 14:53:31 +00:00
|
|
|
result.mainchainMonitor.start()
|
|
|
|
else:
|
|
|
|
stderr.write "No state snapshot (or web3 URL) provided\n"
|
|
|
|
quit 1
|
|
|
|
|
2019-11-25 14:36:25 +00:00
|
|
|
state[] = await result.mainchainMonitor.getGenesis()
|
2019-10-25 14:53:31 +00:00
|
|
|
else:
|
|
|
|
eth1MonitorStartBlock = state.eth1Data.block_hash
|
2019-11-25 14:36:25 +00:00
|
|
|
result.commitGenesisState(state[])
|
2019-10-25 14:53:31 +00:00
|
|
|
|
|
|
|
if result.mainchainMonitor.isNil and conf.depositWeb3Url.len != 0:
|
2019-11-25 14:36:25 +00:00
|
|
|
result.mainchainMonitor = MainchainMonitor.init(
|
|
|
|
conf.depositWeb3Url, conf.depositContractAddress, eth1MonitorStartBlock)
|
2019-10-25 14:53:31 +00:00
|
|
|
result.mainchainMonitor.start()
|
2019-02-28 21:21:29 +00:00
|
|
|
|
|
|
|
result.blockPool = BlockPool.init(result.db)
|
|
|
|
result.attestationPool = AttestationPool.init(result.blockPool)
|
2019-01-25 14:17:35 +00:00
|
|
|
|
2019-08-05 00:00:49 +00:00
|
|
|
result.network = await createEth2Node(conf, result.bootstrapNodes)
|
2019-03-28 14:03:19 +00:00
|
|
|
result.requestManager.init result.network
|
2018-12-19 12:58:53 +00:00
|
|
|
|
2019-03-22 15:49:37 +00:00
|
|
|
# TODO sync is called when a remote peer is connected - is that the right
|
|
|
|
# time to do so?
|
2019-03-18 03:54:08 +00:00
|
|
|
let sync = result.network.protocolState(BeaconSync)
|
2019-12-13 11:54:48 +00:00
|
|
|
let node = result
|
2019-11-25 14:36:25 +00:00
|
|
|
sync.init(
|
|
|
|
result.blockPool, result.forkVersion,
|
2019-12-16 18:08:50 +00:00
|
|
|
proc(signedBlock: SignedBeaconBlock) =
|
|
|
|
if signedBlock.message.slot mod SLOTS_PER_EPOCH == 0:
|
2019-12-13 11:54:48 +00:00
|
|
|
# TODO this is a hack to make sure that lmd ghost is run regularly
|
|
|
|
# while syncing blocks - it's poor form to keep it here though -
|
|
|
|
# the logic should be moved elsewhere
|
|
|
|
# TODO why only when syncing? well, because the way the code is written
|
|
|
|
# we require a connection to a boot node to start, and that boot
|
|
|
|
# node will start syncing as part of connection setup - it looks
|
|
|
|
# like it needs to finish syncing before the slot timer starts
|
|
|
|
# ticking which is a problem: all the synced blocks will be added
|
|
|
|
# to the block pool without any periodic head updates while this
|
|
|
|
# process is ongoing (during a blank start for example), which
|
|
|
|
# leads to an unhealthy buildup of blocks in the non-finalized part
|
|
|
|
# of the block pool
|
|
|
|
# TODO is it a problem that someone sending us a block can force
|
|
|
|
# a potentially expensive head resolution?
|
|
|
|
discard node.updateHead()
|
|
|
|
|
2019-12-16 18:08:50 +00:00
|
|
|
onBeaconBlock(result, signedBlock))
|
2019-02-28 21:21:29 +00:00
|
|
|
|
2019-03-05 22:54:08 +00:00
|
|
|
let addressFile = string(conf.dataDir) / "beacon_node.address"
|
|
|
|
result.network.saveConnectionAddressFile(addressFile)
|
2019-12-19 13:02:28 +00:00
|
|
|
result.beaconClock = BeaconClock.init(result.blockPool.headState.data.data)
|
2019-04-06 07:46:07 +00:00
|
|
|
|
2019-09-07 17:48:05 +00:00
|
|
|
when useInsecureFeatures:
|
|
|
|
if conf.metricsServer:
|
|
|
|
let metricsAddress = conf.metricsServerAddress
|
|
|
|
info "Starting metrics HTTP server", address = metricsAddress, port = conf.metricsServerPort
|
|
|
|
metrics.startHttpServer(metricsAddress, Port(conf.metricsServerPort))
|
|
|
|
|
2018-12-19 12:58:53 +00:00
|
|
|
proc connectToNetwork(node: BeaconNode) {.async.} =
|
2019-08-05 00:00:49 +00:00
|
|
|
if node.bootstrapNodes.len > 0:
|
|
|
|
info "Connecting to bootstrap nodes", bootstrapNodes = node.bootstrapNodes
|
2018-12-19 12:58:53 +00:00
|
|
|
else:
|
2018-12-28 16:51:40 +00:00
|
|
|
info "Waiting for connections"
|
2019-03-05 22:54:08 +00:00
|
|
|
|
2019-08-05 00:00:49 +00:00
|
|
|
await node.network.connectToNetwork(node.bootstrapNodes)
|
2018-12-19 12:58:53 +00:00
|
|
|
|
2018-12-05 13:58:41 +00:00
|
|
|
template findIt(s: openarray, predicate: untyped): int =
|
|
|
|
var res = -1
|
|
|
|
for i, it {.inject.} in s:
|
|
|
|
if predicate:
|
|
|
|
res = i
|
|
|
|
break
|
|
|
|
res
|
|
|
|
|
2019-04-06 07:46:07 +00:00
|
|
|
proc addLocalValidator(
|
|
|
|
node: BeaconNode, state: BeaconState, privKey: ValidatorPrivKey) =
|
|
|
|
let pubKey = privKey.pubKey()
|
2018-12-19 12:58:53 +00:00
|
|
|
|
2019-07-01 09:13:14 +00:00
|
|
|
let idx = state.validators.findIt(it.pubKey == pubKey)
|
2019-03-18 03:54:08 +00:00
|
|
|
if idx == -1:
|
2019-11-25 12:47:29 +00:00
|
|
|
# We allow adding a validator even if its key is not in the state registry:
|
|
|
|
# it might be that the deposit for this validator has not yet been processed
|
|
|
|
warn "Validator not in registry (yet?)", pubKey
|
|
|
|
|
|
|
|
node.attachedValidators.addLocalValidator(pubKey, privKey)
|
2019-03-18 03:54:08 +00:00
|
|
|
|
2019-04-06 07:46:07 +00:00
|
|
|
proc addLocalValidators(node: BeaconNode, state: BeaconState) =
|
2019-12-10 14:20:40 +00:00
|
|
|
for validatorKey in node.config.validatorKeys:
|
|
|
|
node.addLocalValidator state, validatorKey
|
2018-12-05 13:58:41 +00:00
|
|
|
|
2018-12-28 16:51:40 +00:00
|
|
|
info "Local validators attached ", count = node.attachedValidators.count
|
2018-11-23 23:58:49 +00:00
|
|
|
|
2019-11-21 09:15:10 +00:00
|
|
|
func getAttachedValidator(node: BeaconNode,
|
2019-09-29 17:42:53 +00:00
|
|
|
state: BeaconState,
|
|
|
|
idx: ValidatorIndex): AttachedValidator =
|
2019-07-01 09:13:14 +00:00
|
|
|
let validatorKey = state.validators[idx].pubkey
|
2019-04-06 07:46:07 +00:00
|
|
|
node.attachedValidators.getValidator(validatorKey)
|
2018-11-26 13:33:06 +00:00
|
|
|
|
2019-12-02 14:42:57 +00:00
|
|
|
proc isSynced(node: BeaconNode, head: BlockRef): bool =
|
|
|
|
## TODO This function is here as a placeholder for some better heurestics to
|
|
|
|
## determine if we're in sync and should be producing blocks and
|
|
|
|
## attestations. Generally, the problem is that slot time keeps advancing
|
|
|
|
## even when there are no blocks being produced, so there's no way to
|
|
|
|
## distinguish validators geniunely going missing from the node not being
|
|
|
|
## well connected (during a network split or an internet outage for
|
|
|
|
## example). It would generally be correct to simply keep running as if
|
|
|
|
## we were the only legit node left alive, but then we run into issues:
|
|
|
|
## with enough many empty slots, the validator pool is emptied leading
|
|
|
|
## to empty committees and lots of empty slot processing that will be
|
|
|
|
## thrown away as soon as we're synced again.
|
|
|
|
|
|
|
|
let
|
|
|
|
# The slot we should be at, according to the clock
|
|
|
|
beaconTime = node.beaconClock.now()
|
|
|
|
wallSlot = beaconTime.toSlot()
|
|
|
|
|
|
|
|
# TODO if everyone follows this logic, the network will not recover from a
|
|
|
|
# halt: nobody will be producing blocks because everone expects someone
|
|
|
|
# else to do it
|
|
|
|
if wallSlot.afterGenesis and (wallSlot.slot > head.slot) and
|
|
|
|
(wallSlot.slot - head.slot) > maxEmptySlotCount:
|
|
|
|
false
|
|
|
|
else:
|
|
|
|
true
|
|
|
|
|
2019-12-13 11:54:48 +00:00
|
|
|
proc updateHead(node: BeaconNode): BlockRef =
|
2019-08-19 16:41:13 +00:00
|
|
|
# Check pending attestations - maybe we found some blocks for them
|
2019-12-19 13:02:28 +00:00
|
|
|
node.attestationPool.resolve()
|
2019-02-28 21:21:29 +00:00
|
|
|
|
2019-12-19 13:02:28 +00:00
|
|
|
# Grab the new head according to our latest attestation data
|
|
|
|
let newHead = node.attestationPool.selectHead()
|
2019-08-19 16:41:13 +00:00
|
|
|
|
2019-12-19 13:02:28 +00:00
|
|
|
# Store the new head in the block pool - this may cause epochs to be
|
|
|
|
# justified and finalized
|
|
|
|
node.blockPool.updateHead(newHead)
|
2019-09-07 17:48:05 +00:00
|
|
|
beacon_head_root.set newHead.root.toGaugeValue
|
|
|
|
|
2019-03-13 22:59:20 +00:00
|
|
|
newHead
|
2019-02-28 21:21:29 +00:00
|
|
|
|
2019-04-06 07:46:07 +00:00
|
|
|
proc sendAttestation(node: BeaconNode,
|
2019-11-21 09:57:59 +00:00
|
|
|
fork: Fork,
|
2018-12-28 16:51:40 +00:00
|
|
|
validator: AttachedValidator,
|
2019-04-06 07:46:07 +00:00
|
|
|
attestationData: AttestationData,
|
2018-12-28 16:51:40 +00:00
|
|
|
committeeLen: int,
|
|
|
|
indexInCommittee: int) {.async.} =
|
2019-09-12 01:45:04 +00:00
|
|
|
logScope: pcs = "send_attestation"
|
|
|
|
|
2019-03-09 04:23:14 +00:00
|
|
|
let
|
2019-11-21 09:57:59 +00:00
|
|
|
validatorSignature = await validator.signAttestation(attestationData, fork)
|
2018-12-28 16:51:40 +00:00
|
|
|
|
2019-07-03 07:35:05 +00:00
|
|
|
var aggregationBits = CommitteeValidatorsBits.init(committeeLen)
|
2019-12-20 13:25:33 +00:00
|
|
|
aggregationBits.setBit indexInCommittee
|
2018-12-28 16:51:40 +00:00
|
|
|
|
|
|
|
var attestation = Attestation(
|
|
|
|
data: attestationData,
|
2019-06-12 07:48:49 +00:00
|
|
|
signature: validatorSignature,
|
2019-11-13 11:30:11 +00:00
|
|
|
aggregation_bits: aggregationBits
|
2019-02-12 22:50:02 +00:00
|
|
|
)
|
2018-11-26 13:33:06 +00:00
|
|
|
|
2019-03-28 11:01:28 +00:00
|
|
|
node.network.broadcast(topicAttestations, attestation)
|
2018-11-26 13:33:06 +00:00
|
|
|
|
2019-12-03 11:32:27 +00:00
|
|
|
if node.config.dump:
|
|
|
|
SSZ.saveFile(
|
|
|
|
node.config.dumpDir / "att-" & $attestationData.slot & "-" &
|
|
|
|
$attestationData.index & "-" & validator.pubKey.shortLog &
|
|
|
|
".ssz", attestation)
|
|
|
|
|
2019-02-19 23:35:02 +00:00
|
|
|
info "Attestation sent",
|
2019-03-20 20:01:48 +00:00
|
|
|
attestationData = shortLog(attestationData),
|
2019-04-06 07:46:07 +00:00
|
|
|
validator = shortLog(validator),
|
2019-08-14 08:56:32 +00:00
|
|
|
signature = shortLog(validatorSignature),
|
2019-09-12 01:45:04 +00:00
|
|
|
indexInCommittee = indexInCommittee,
|
|
|
|
cat = "consensus"
|
2018-12-28 16:51:40 +00:00
|
|
|
|
2019-10-24 06:51:27 +00:00
|
|
|
beacon_attestations_sent.inc()
|
|
|
|
|
2018-11-29 01:08:34 +00:00
|
|
|
proc proposeBlock(node: BeaconNode,
|
|
|
|
validator: AttachedValidator,
|
2019-03-22 15:49:37 +00:00
|
|
|
head: BlockRef,
|
|
|
|
slot: Slot): Future[BlockRef] {.async.} =
|
2019-09-12 01:45:04 +00:00
|
|
|
logScope: pcs = "block_proposal"
|
|
|
|
|
2019-12-23 15:34:09 +00:00
|
|
|
if head.slot >= slot:
|
|
|
|
# We should normally not have a head newer than the slot we're proposing for
|
|
|
|
# but this can happen if block proposal is delayed
|
|
|
|
warn "Skipping proposal, have newer head already",
|
2019-08-15 16:01:55 +00:00
|
|
|
headSlot = shortLog(head.slot),
|
2019-03-14 13:33:56 +00:00
|
|
|
headBlockRoot = shortLog(head.root),
|
2019-09-12 01:45:04 +00:00
|
|
|
slot = shortLog(slot),
|
|
|
|
cat = "fastforward"
|
2019-03-22 15:49:37 +00:00
|
|
|
return head
|
2019-03-14 13:33:56 +00:00
|
|
|
|
2019-11-25 14:36:25 +00:00
|
|
|
# Advance state to the slot immediately preceding the one we're creating a
|
|
|
|
# block for - potentially we will be processing empty slots along the way.
|
2019-10-07 02:34:45 +00:00
|
|
|
let (nroot, nblck) = node.blockPool.withState(
|
2019-12-23 15:34:09 +00:00
|
|
|
node.blockPool.tmpState, head.atSlot(slot)):
|
2019-11-25 14:36:25 +00:00
|
|
|
let (eth1data, deposits) =
|
|
|
|
if node.mainchainMonitor.isNil:
|
|
|
|
(get_eth1data_stub(
|
|
|
|
state.eth1_deposit_index, slot.compute_epoch_at_slot()),
|
|
|
|
newSeq[Deposit]())
|
|
|
|
else:
|
|
|
|
(node.mainchainMonitor.eth1Data,
|
|
|
|
node.mainchainMonitor.getPendingDeposits())
|
|
|
|
|
2019-04-06 07:46:07 +00:00
|
|
|
# To create a block, we'll first apply a partial block to the state, skipping
|
|
|
|
# some validations.
|
|
|
|
let
|
2019-11-21 09:57:59 +00:00
|
|
|
fork = state.fork
|
2019-04-06 07:46:07 +00:00
|
|
|
blockBody = BeaconBlockBody(
|
2019-11-21 09:57:59 +00:00
|
|
|
randao_reveal: validator.genRandaoReveal(fork, slot),
|
2019-09-09 17:47:33 +00:00
|
|
|
eth1_data: eth1data,
|
2019-04-06 07:46:07 +00:00
|
|
|
attestations:
|
2019-09-09 15:59:02 +00:00
|
|
|
node.attestationPool.getAttestationsForBlock(state, slot),
|
2019-09-09 17:47:33 +00:00
|
|
|
deposits: deposits)
|
2019-03-22 15:49:37 +00:00
|
|
|
|
2019-04-06 07:46:07 +00:00
|
|
|
var
|
2019-12-16 18:08:50 +00:00
|
|
|
newBlock = SignedBeaconBlock(
|
|
|
|
message: BeaconBlock(
|
|
|
|
slot: slot,
|
|
|
|
parent_root: head.root,
|
|
|
|
body: blockBody))
|
2019-08-14 08:56:32 +00:00
|
|
|
tmpState = hashedState
|
2019-12-16 18:08:50 +00:00
|
|
|
discard state_transition(tmpState, newBlock.message, {skipValidation})
|
2019-06-12 07:48:49 +00:00
|
|
|
# TODO only enable in fast-fail debugging situations
|
|
|
|
# otherwise, bad attestations can bring down network
|
|
|
|
# doAssert ok # TODO: err, could this fail somehow?
|
2018-11-26 13:33:06 +00:00
|
|
|
|
2019-12-16 18:08:50 +00:00
|
|
|
newBlock.message.state_root = tmpState.root
|
2018-11-26 13:33:06 +00:00
|
|
|
|
2019-12-16 18:08:50 +00:00
|
|
|
let blockRoot = hash_tree_root(newBlock.message)
|
2018-11-26 13:33:06 +00:00
|
|
|
|
2019-04-06 07:46:07 +00:00
|
|
|
# Careful, state no longer valid after here..
|
2019-11-21 09:57:59 +00:00
|
|
|
# We use the fork from the pre-newBlock state which should be fine because
|
|
|
|
# fork carries two epochs, so even if it's a fork block, the right thing
|
|
|
|
# will happen here
|
2019-04-06 07:46:07 +00:00
|
|
|
newBlock.signature =
|
2019-11-21 09:57:59 +00:00
|
|
|
await validator.signBlockProposal(fork, slot, blockRoot)
|
2019-03-22 15:49:37 +00:00
|
|
|
|
2019-04-06 07:46:07 +00:00
|
|
|
(blockRoot, newBlock)
|
|
|
|
|
2019-12-19 13:02:28 +00:00
|
|
|
let newBlockRef = node.blockPool.add(nroot, nblck)
|
2019-03-27 20:17:01 +00:00
|
|
|
if newBlockRef == nil:
|
|
|
|
warn "Unable to add proposed block to block pool",
|
2019-12-16 18:08:50 +00:00
|
|
|
newBlock = shortLog(newBlock.message),
|
2019-09-12 01:45:04 +00:00
|
|
|
blockRoot = shortLog(blockRoot),
|
|
|
|
cat = "bug"
|
2019-03-27 20:17:01 +00:00
|
|
|
return head
|
2018-11-26 13:33:06 +00:00
|
|
|
|
2019-02-19 23:35:02 +00:00
|
|
|
info "Block proposed",
|
2019-12-16 18:08:50 +00:00
|
|
|
blck = shortLog(newBlock.message),
|
2019-03-22 15:49:37 +00:00
|
|
|
blockRoot = shortLog(newBlockRef.root),
|
2019-09-12 01:45:04 +00:00
|
|
|
validator = shortLog(validator),
|
|
|
|
cat = "consensus"
|
2018-11-26 13:33:06 +00:00
|
|
|
|
2019-12-03 11:32:27 +00:00
|
|
|
if node.config.dump:
|
|
|
|
SSZ.saveFile(
|
2019-12-16 18:08:50 +00:00
|
|
|
node.config.dumpDir / "block-" & $newBlock.message.slot & "-" &
|
2019-12-03 11:32:27 +00:00
|
|
|
shortLog(newBlockRef.root) & ".ssz", newBlock)
|
|
|
|
SSZ.saveFile(
|
|
|
|
node.config.dumpDir / "state-" & $tmpState.data.slot & "-" &
|
|
|
|
shortLog(newBlockRef.root) & "-" & shortLog(tmpState.root) & ".ssz",
|
|
|
|
tmpState.data)
|
|
|
|
|
2019-03-28 11:01:28 +00:00
|
|
|
node.network.broadcast(topicBeaconBlocks, newBlock)
|
2019-01-25 17:35:22 +00:00
|
|
|
|
2019-10-24 06:51:27 +00:00
|
|
|
beacon_blocks_proposed.inc()
|
|
|
|
|
2019-03-22 15:49:37 +00:00
|
|
|
return newBlockRef
|
2018-11-23 23:58:49 +00:00
|
|
|
|
2019-02-28 21:21:29 +00:00
|
|
|
proc onAttestation(node: BeaconNode, attestation: Attestation) =
|
|
|
|
# We received an attestation from the network but don't know much about it
|
|
|
|
# yet - in particular, we haven't verified that it belongs to particular chain
|
|
|
|
# we're on, or that it follows the rules of the protocol
|
2019-09-12 01:45:04 +00:00
|
|
|
logScope: pcs = "on_attestation"
|
|
|
|
|
2019-02-28 21:21:29 +00:00
|
|
|
debug "Attestation received",
|
2019-03-20 20:01:48 +00:00
|
|
|
attestationData = shortLog(attestation.data),
|
2019-09-12 01:45:04 +00:00
|
|
|
signature = shortLog(attestation.signature),
|
|
|
|
cat = "consensus" # Tag "consensus|attestation"?
|
2019-02-21 04:42:17 +00:00
|
|
|
|
2019-12-19 13:02:28 +00:00
|
|
|
let
|
|
|
|
wallSlot = node.beaconClock.now().toSlot()
|
|
|
|
head = node.blockPool.head
|
2019-08-16 11:16:56 +00:00
|
|
|
|
2019-12-19 13:02:28 +00:00
|
|
|
if not wallSlot.afterGenesis or wallSlot.slot < head.blck.slot:
|
|
|
|
warn "Received attestation before genesis or head - clock is wrong?",
|
|
|
|
afterGenesis = wallSlot.afterGenesis,
|
|
|
|
wallSlot = shortLog(wallSlot.slot),
|
|
|
|
headSlot = shortLog(head.blck.slot),
|
|
|
|
cat = "clock_drift" # Tag "attestation|clock_drift"?
|
|
|
|
return
|
|
|
|
|
|
|
|
if attestation.data.slot > head.blck.slot and
|
|
|
|
(attestation.data.slot - head.blck.slot) > maxEmptySlotCount:
|
|
|
|
warn "Ignoring attestation, head block too old (out of sync?)",
|
|
|
|
attestationSlot = attestation.data.slot, headSlot = head.blck.slot
|
|
|
|
return
|
|
|
|
|
|
|
|
node.attestationPool.add(attestation)
|
2019-08-14 08:56:32 +00:00
|
|
|
|
2019-12-16 18:08:50 +00:00
|
|
|
proc onBeaconBlock(node: BeaconNode, blck: SignedBeaconBlock) =
|
2019-02-28 21:21:29 +00:00
|
|
|
# We received a block but don't know much about it yet - in particular, we
|
|
|
|
# don't know if it's part of the chain we're currently building.
|
2019-12-16 18:08:50 +00:00
|
|
|
let blockRoot = hash_tree_root(blck.message)
|
2019-02-28 21:21:29 +00:00
|
|
|
debug "Block received",
|
2019-12-16 18:08:50 +00:00
|
|
|
blck = shortLog(blck.message),
|
2019-09-12 01:45:04 +00:00
|
|
|
blockRoot = shortLog(blockRoot),
|
|
|
|
cat = "block_listener",
|
|
|
|
pcs = "receive_block"
|
2019-02-28 21:21:29 +00:00
|
|
|
|
2019-10-24 06:51:27 +00:00
|
|
|
beacon_blocks_received.inc()
|
|
|
|
|
2019-12-19 13:02:28 +00:00
|
|
|
if node.blockPool.add(blockRoot, blck).isNil:
|
2019-03-22 15:49:37 +00:00
|
|
|
return
|
2019-02-28 21:21:29 +00:00
|
|
|
|
|
|
|
# The block we received contains attestations, and we might not yet know about
|
|
|
|
# all of them. Let's add them to the attestation pool - in case they block
|
|
|
|
# is not yet resolved, neither will the attestations be!
|
2019-11-12 22:53:19 +00:00
|
|
|
# But please note that we only care about recent attestations.
|
2019-06-03 08:26:38 +00:00
|
|
|
# TODO shouldn't add attestations if the block turns out to be invalid..
|
2019-11-12 22:53:19 +00:00
|
|
|
let currentSlot = node.beaconClock.now.toSlot
|
|
|
|
if currentSlot.afterGenesis and
|
2019-12-16 18:08:50 +00:00
|
|
|
blck.message.slot.epoch + 1 >= currentSlot.slot.epoch:
|
|
|
|
for attestation in blck.message.body.attestations:
|
2019-11-12 22:53:19 +00:00
|
|
|
node.onAttestation(attestation)
|
2019-02-21 04:42:17 +00:00
|
|
|
|
2019-03-22 15:49:37 +00:00
|
|
|
proc handleAttestations(node: BeaconNode, head: BlockRef, slot: Slot) =
|
|
|
|
## Perform all attestations that the validators attached to this node should
|
|
|
|
## perform during the given slot
|
2019-09-12 01:45:04 +00:00
|
|
|
logScope: pcs = "on_attestation"
|
2019-03-22 15:49:37 +00:00
|
|
|
|
|
|
|
if slot + SLOTS_PER_EPOCH < head.slot:
|
|
|
|
# The latest block we know about is a lot newer than the slot we're being
|
|
|
|
# asked to attest to - this makes it unlikely that it will be included
|
|
|
|
# at all.
|
|
|
|
# TODO the oldest attestations allowed are those that are older than the
|
|
|
|
# finalized epoch.. also, it seems that posting very old attestations
|
|
|
|
# is risky from a slashing perspective. More work is needed here.
|
|
|
|
notice "Skipping attestation, head is too recent",
|
2019-08-15 16:01:55 +00:00
|
|
|
headSlot = shortLog(head.slot),
|
|
|
|
slot = shortLog(slot)
|
2019-03-22 15:49:37 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
let attestationHead = head.findAncestorBySlot(slot)
|
2019-05-01 09:19:29 +00:00
|
|
|
if head != attestationHead.blck:
|
2019-03-22 15:49:37 +00:00
|
|
|
# In rare cases, such as when we're busy syncing or just slow, we'll be
|
|
|
|
# attesting to a past state - we must then recreate the world as it looked
|
|
|
|
# like back then
|
|
|
|
notice "Attesting to a state in the past, falling behind?",
|
2019-08-15 16:01:55 +00:00
|
|
|
headSlot = shortLog(head.slot),
|
|
|
|
attestationHeadSlot = shortLog(attestationHead.slot),
|
|
|
|
attestationSlot = shortLog(slot)
|
2019-03-22 15:49:37 +00:00
|
|
|
|
2019-09-12 01:45:04 +00:00
|
|
|
trace "Checking attestations",
|
2019-05-01 09:19:29 +00:00
|
|
|
attestationHeadRoot = shortLog(attestationHead.blck.root),
|
2019-09-12 01:45:04 +00:00
|
|
|
attestationSlot = shortLog(slot),
|
|
|
|
cat = "attestation"
|
2019-03-28 06:10:48 +00:00
|
|
|
|
2019-04-06 07:46:07 +00:00
|
|
|
# Collect data to send before node.stateCache grows stale
|
|
|
|
var attestations: seq[tuple[
|
|
|
|
data: AttestationData, committeeLen, indexInCommittee: int,
|
|
|
|
validator: AttachedValidator]]
|
|
|
|
|
2019-03-22 15:49:37 +00:00
|
|
|
# We need to run attestations exactly for the slot that we're attesting to.
|
|
|
|
# In case blocks went missing, this means advancing past the latest block
|
|
|
|
# using empty slots as fillers.
|
2019-10-24 13:36:36 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.8.4/specs/validator/0_beacon-chain-validator.md#validator-assignments
|
2019-08-19 16:41:13 +00:00
|
|
|
# TODO we could cache the validator assignment since it's valid for the entire
|
|
|
|
# epoch since it doesn't change, but that has to be weighed against
|
|
|
|
# the complexity of handling forks correctly - instead, we use an adapted
|
|
|
|
# version here that calculates the committee for a single slot only
|
2019-12-19 13:02:28 +00:00
|
|
|
node.blockPool.withState(node.blockPool.tmpState, attestationHead):
|
2019-06-24 09:21:56 +00:00
|
|
|
var cache = get_empty_per_epoch_cache()
|
2019-11-14 18:48:12 +00:00
|
|
|
let committees_per_slot = get_committee_count_at_slot(state, slot)
|
2019-08-19 16:41:13 +00:00
|
|
|
|
2019-11-14 17:37:51 +00:00
|
|
|
for committee_index in 0'u64..<committees_per_slot:
|
2019-06-24 09:21:56 +00:00
|
|
|
let
|
2019-11-14 17:37:51 +00:00
|
|
|
committee = get_beacon_committee(state, slot, committee_index, cache)
|
2019-08-19 16:41:13 +00:00
|
|
|
|
2019-11-14 17:37:51 +00:00
|
|
|
for index_in_committee, validatorIdx in committee:
|
2019-04-06 07:46:07 +00:00
|
|
|
let validator = node.getAttachedValidator(state, validatorIdx)
|
|
|
|
if validator != nil:
|
2019-11-14 17:37:51 +00:00
|
|
|
let ad = makeAttestationData(state, slot, committee_index, blck.root)
|
|
|
|
attestations.add((ad, committee.len, index_in_committee, validator))
|
2019-04-06 07:46:07 +00:00
|
|
|
|
2019-09-08 18:09:01 +00:00
|
|
|
for a in attestations:
|
|
|
|
traceAsyncErrors sendAttestation(
|
2019-11-21 09:57:59 +00:00
|
|
|
node, state.fork, a.validator, a.data, a.committeeLen, a.indexInCommittee)
|
2019-03-22 15:49:37 +00:00
|
|
|
|
|
|
|
proc handleProposal(node: BeaconNode, head: BlockRef, slot: Slot):
|
|
|
|
Future[BlockRef] {.async.} =
|
|
|
|
## Perform the proposal for the given slot, iff we have a validator attached
|
|
|
|
## that is supposed to do so, given the shuffling in head
|
|
|
|
|
2019-12-23 15:34:09 +00:00
|
|
|
# TODO here we advance the state to the new slot, but later we'll be
|
2019-03-22 15:49:37 +00:00
|
|
|
# proposing for it - basically, we're selecting proposer based on an
|
2019-12-23 15:34:09 +00:00
|
|
|
# empty slot
|
2019-06-24 09:21:56 +00:00
|
|
|
var cache = get_empty_per_epoch_cache()
|
2019-12-23 15:34:09 +00:00
|
|
|
node.blockPool.withState(node.blockPool.tmpState, head.atSlot(slot)):
|
2019-12-04 10:49:59 +00:00
|
|
|
let proposerIdx = get_beacon_proposer_index(state, cache)
|
|
|
|
if proposerIdx.isNone:
|
2019-12-05 11:49:57 +00:00
|
|
|
notice "Missing proposer index",
|
|
|
|
slot=slot,
|
|
|
|
epoch=slot.compute_epoch_at_slot,
|
|
|
|
num_validators=state.validators.len,
|
2019-12-05 12:10:20 +00:00
|
|
|
active_validators=
|
2019-12-05 11:49:57 +00:00
|
|
|
get_active_validator_indices(state, slot.compute_epoch_at_slot),
|
|
|
|
balances=state.balances
|
|
|
|
|
2019-12-04 10:49:59 +00:00
|
|
|
return head
|
|
|
|
|
|
|
|
let validator = node.getAttachedValidator(state, proposerIdx.get)
|
2019-03-22 15:49:37 +00:00
|
|
|
|
2019-04-06 07:46:07 +00:00
|
|
|
if validator != nil:
|
|
|
|
return await proposeBlock(node, validator, head, slot)
|
2019-03-22 15:49:37 +00:00
|
|
|
|
2019-09-12 01:45:04 +00:00
|
|
|
trace "Expecting block proposal",
|
2019-04-06 07:46:07 +00:00
|
|
|
headRoot = shortLog(head.root),
|
2019-08-15 16:01:55 +00:00
|
|
|
slot = shortLog(slot),
|
2019-12-04 10:49:59 +00:00
|
|
|
proposer = shortLog(state.validators[proposerIdx.get].pubKey),
|
2019-09-12 01:45:04 +00:00
|
|
|
cat = "consensus",
|
|
|
|
pcs = "wait_for_proposal"
|
2019-03-22 15:49:37 +00:00
|
|
|
|
|
|
|
return head
|
|
|
|
|
|
|
|
proc onSlotStart(node: BeaconNode, lastSlot, scheduledSlot: Slot) {.gcsafe, async.} =
|
|
|
|
## Called at the beginning of a slot - usually every slot, but sometimes might
|
|
|
|
## skip a few in case we're running late.
|
|
|
|
## lastSlot: the last slot that we sucessfully processed, so we know where to
|
|
|
|
## start work from
|
|
|
|
## scheduledSlot: the slot that we were aiming for, in terms of timing
|
|
|
|
|
2019-09-12 01:45:04 +00:00
|
|
|
logScope: pcs = "slot_start"
|
|
|
|
|
2019-03-22 15:49:37 +00:00
|
|
|
let
|
|
|
|
# The slot we should be at, according to the clock
|
2019-08-16 11:16:56 +00:00
|
|
|
beaconTime = node.beaconClock.now()
|
|
|
|
wallSlot = beaconTime.toSlot()
|
2019-03-22 15:49:37 +00:00
|
|
|
|
2019-12-23 15:34:09 +00:00
|
|
|
info "Slot start",
|
2019-08-15 16:01:55 +00:00
|
|
|
lastSlot = shortLog(lastSlot),
|
|
|
|
scheduledSlot = shortLog(scheduledSlot),
|
2019-09-12 01:45:04 +00:00
|
|
|
beaconTime = shortLog(beaconTime),
|
|
|
|
peers = node.network.peersCount,
|
2019-12-23 15:34:09 +00:00
|
|
|
headSlot = shortLog(node.blockPool.head.blck.slot),
|
|
|
|
headEpoch = shortLog(node.blockPool.head.blck.slot.compute_epoch_at_slot()),
|
|
|
|
headRoot = shortLog(node.blockPool.head.blck.root),
|
|
|
|
finalizedSlot = shortLog(node.blockPool.finalizedHead.blck.slot),
|
|
|
|
finalizedRoot = shortLog(node.blockPool.finalizedHead.blck.root),
|
|
|
|
finalizedSlot = shortLog(node.blockPool.finalizedHead.blck.slot.compute_epoch_at_slot()),
|
2019-09-12 01:45:04 +00:00
|
|
|
cat = "scheduling"
|
2019-03-22 15:49:37 +00:00
|
|
|
|
2019-08-16 11:16:56 +00:00
|
|
|
if not wallSlot.afterGenesis or (wallSlot.slot < lastSlot):
|
2019-12-23 15:34:09 +00:00
|
|
|
let
|
|
|
|
slot =
|
|
|
|
if wallSlot.afterGenesis: wallSlot.slot
|
|
|
|
else: GENESIS_SLOT
|
|
|
|
nextSlot = slot + 1 # At least GENESIS_SLOT + 1!
|
|
|
|
|
2019-03-22 15:49:37 +00:00
|
|
|
# This can happen if the system clock changes time for example, and it's
|
|
|
|
# pretty bad
|
|
|
|
# TODO shut down? time either was or is bad, and PoS relies on accuracy..
|
|
|
|
warn "Beacon clock time moved back, rescheduling slot actions",
|
2019-08-16 11:16:56 +00:00
|
|
|
beaconTime = shortLog(beaconTime),
|
|
|
|
lastSlot = shortLog(lastSlot),
|
2019-09-12 01:45:04 +00:00
|
|
|
scheduledSlot = shortLog(scheduledSlot),
|
2019-12-23 15:34:09 +00:00
|
|
|
nextSlot = shortLog(nextSlot),
|
2019-09-12 01:45:04 +00:00
|
|
|
cat = "clock_drift" # tag "scheduling|clock_drift"?
|
2019-03-22 15:49:37 +00:00
|
|
|
|
|
|
|
addTimer(saturate(node.beaconClock.fromNow(nextSlot))) do (p: pointer):
|
|
|
|
asyncCheck node.onSlotStart(slot, nextSlot)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
2019-08-16 11:16:56 +00:00
|
|
|
let
|
|
|
|
slot = wallSlot.slot # afterGenesis == true!
|
|
|
|
nextSlot = slot + 1
|
|
|
|
|
2019-09-07 17:48:05 +00:00
|
|
|
beacon_slot.set slot.int64
|
|
|
|
|
2019-03-22 15:49:37 +00:00
|
|
|
if slot > lastSlot + SLOTS_PER_EPOCH:
|
|
|
|
# We've fallen behind more than an epoch - there's nothing clever we can
|
|
|
|
# do here really, except skip all the work and try again later.
|
|
|
|
# TODO how long should the period be? Using an epoch because that's roughly
|
|
|
|
# how long attestations remain interesting
|
|
|
|
# TODO should we shut down instead? clearly we're unable to keep up
|
2019-12-23 15:34:09 +00:00
|
|
|
warn "Unable to keep up, skipping ahead",
|
2019-08-15 16:01:55 +00:00
|
|
|
lastSlot = shortLog(lastSlot),
|
|
|
|
slot = shortLog(slot),
|
2019-12-23 15:34:09 +00:00
|
|
|
nextSlot = shortLog(nextSlot),
|
2019-09-12 01:45:04 +00:00
|
|
|
scheduledSlot = shortLog(scheduledSlot),
|
|
|
|
cat = "overload"
|
2019-03-22 15:49:37 +00:00
|
|
|
|
|
|
|
addTimer(saturate(node.beaconClock.fromNow(nextSlot))) do (p: pointer):
|
|
|
|
# We pass the current slot here to indicate that work should be skipped!
|
|
|
|
asyncCheck node.onSlotStart(slot, nextSlot)
|
|
|
|
return
|
|
|
|
|
|
|
|
# Whatever we do during the slot, we need to know the head, because this will
|
|
|
|
# give us a state to work with and thus a shuffling.
|
|
|
|
# TODO typically, what consitutes correct actions stays constant between slot
|
|
|
|
# updates and is stable across some epoch transitions as well - see how
|
|
|
|
# we can avoid recalculating everything here
|
|
|
|
|
2019-12-13 11:54:48 +00:00
|
|
|
var head = node.updateHead()
|
|
|
|
|
|
|
|
# TODO is the slot of the clock or the head block more interestion? provide
|
|
|
|
# rationale in comment
|
|
|
|
beacon_head_slot.set slot.int64
|
2019-03-22 15:49:37 +00:00
|
|
|
|
|
|
|
# TODO if the head is very old, that is indicative of something being very
|
|
|
|
# wrong - us being out of sync or disconnected from the network - need
|
|
|
|
# to consider what to do in that case:
|
|
|
|
# * nothing - the other parts of the application will reconnect and
|
|
|
|
# start listening to broadcasts, learn a new head etc..
|
|
|
|
# risky, because the network might stall if everyone does
|
|
|
|
# this, because no blocks will be produced
|
|
|
|
# * shut down - this allows the user to notice and take action, but is
|
|
|
|
# kind of harsh
|
|
|
|
# * keep going - we create blocks and attestations as usual and send them
|
|
|
|
# out - if network conditions improve, fork choice should
|
|
|
|
# eventually select the correct head and the rest will
|
|
|
|
# disappear naturally - risky because user is not aware,
|
|
|
|
# and might lose stake on canonical chain but "just works"
|
|
|
|
# when reconnected..
|
2019-12-10 09:55:37 +00:00
|
|
|
if node.attachedValidators.count == 0:
|
|
|
|
# There are no validators, thus we don't have any additional work to do
|
|
|
|
# beyond keeping track of the head
|
|
|
|
discard
|
|
|
|
elif not node.isSynced(head):
|
2019-12-02 14:42:57 +00:00
|
|
|
warn "Node out of sync, skipping block and attestation production for this slot",
|
|
|
|
slot, headSlot = head.slot
|
|
|
|
else:
|
|
|
|
var curSlot = lastSlot + 1
|
|
|
|
while curSlot < slot:
|
|
|
|
# Timers may be delayed because we're busy processing, and we might have
|
|
|
|
# more work to do. We'll try to do so in an expedited way.
|
|
|
|
# TODO maybe even collect all work synchronously to avoid unnecessary
|
|
|
|
# state rewinds while waiting for async operations like validator
|
|
|
|
# signature..
|
|
|
|
notice "Catching up",
|
|
|
|
curSlot = shortLog(curSlot),
|
|
|
|
lastSlot = shortLog(lastSlot),
|
|
|
|
slot = shortLog(slot),
|
|
|
|
cat = "overload"
|
|
|
|
|
|
|
|
# For every slot we're catching up, we'll propose then send
|
|
|
|
# attestations - head should normally be advancing along the same branch
|
|
|
|
# in this case
|
|
|
|
# TODO what if we receive blocks / attestations while doing this work?
|
|
|
|
head = await handleProposal(node, head, curSlot)
|
|
|
|
|
|
|
|
# For each slot we missed, we need to send out attestations - if we were
|
|
|
|
# proposing during this time, we'll use the newly proposed head, else just
|
|
|
|
# keep reusing the same - the attestation that goes out will actually
|
|
|
|
# rewind the state to what it looked like at the time of that slot
|
|
|
|
# TODO smells like there's an optimization opportunity here
|
|
|
|
handleAttestations(node, head, curSlot)
|
|
|
|
|
|
|
|
curSlot += 1
|
|
|
|
|
|
|
|
head = await handleProposal(node, head, slot)
|
|
|
|
|
|
|
|
# We've been doing lots of work up until now which took time. Normally, we
|
|
|
|
# send out attestations at the slot mid-point, so we go back to the clock
|
|
|
|
# to see how much time we need to wait.
|
|
|
|
# TODO the beacon clock might jump here also. It's probably easier to complete
|
|
|
|
# the work for the whole slot using a monotonic clock instead, then deal
|
|
|
|
# with any clock discrepancies once only, at the start of slot timer
|
|
|
|
# processing..
|
2019-12-11 13:02:07 +00:00
|
|
|
|
2019-12-20 16:50:09 +00:00
|
|
|
# https://github.com/ethereum/eth2.0-specs/blob/v0.9.4/specs/validator/0_beacon-chain-validator.md#attesting
|
2019-12-11 13:02:07 +00:00
|
|
|
# A validator should create and broadcast the attestation to the
|
|
|
|
# associated attestation subnet one-third of the way through the slot
|
|
|
|
# during which the validator is assigned―that is, SECONDS_PER_SLOT / 3
|
|
|
|
# seconds after the start of slot.
|
2019-12-02 14:42:57 +00:00
|
|
|
let
|
|
|
|
attestationStart = node.beaconClock.fromNow(slot)
|
2019-12-11 13:02:07 +00:00
|
|
|
thirdSlot = seconds(int64(SECONDS_PER_SLOT)) div 3
|
2019-03-22 15:49:37 +00:00
|
|
|
|
2019-12-11 13:02:07 +00:00
|
|
|
if attestationStart.inFuture or attestationStart.offset <= thirdSlot:
|
2019-12-02 14:42:57 +00:00
|
|
|
let fromNow =
|
2019-12-11 13:02:07 +00:00
|
|
|
if attestationStart.inFuture: attestationStart.offset + thirdSlot
|
|
|
|
else: thirdSlot - attestationStart.offset
|
2019-03-22 15:49:37 +00:00
|
|
|
|
2019-12-02 14:42:57 +00:00
|
|
|
trace "Waiting to send attestations",
|
|
|
|
slot = shortLog(slot),
|
|
|
|
fromNow = shortLog(fromNow),
|
|
|
|
cat = "scheduling"
|
2019-03-28 06:10:48 +00:00
|
|
|
|
2019-12-02 14:42:57 +00:00
|
|
|
await sleepAsync(fromNow)
|
2019-03-22 15:49:37 +00:00
|
|
|
|
2019-12-02 14:42:57 +00:00
|
|
|
# Time passed - we might need to select a new head in that case
|
2019-12-13 11:54:48 +00:00
|
|
|
head = node.updateHead()
|
2019-03-22 15:49:37 +00:00
|
|
|
|
2019-12-02 14:42:57 +00:00
|
|
|
handleAttestations(node, head, slot)
|
2019-03-22 15:49:37 +00:00
|
|
|
|
|
|
|
# TODO ... and beacon clock might jump here also. sigh.
|
|
|
|
let
|
|
|
|
nextSlotStart = saturate(node.beaconClock.fromNow(nextSlot))
|
|
|
|
|
2019-12-23 15:34:09 +00:00
|
|
|
info "Slot end",
|
|
|
|
slot = shortLog(slot),
|
|
|
|
nextSlot = shortLog(nextSlot),
|
|
|
|
headSlot = shortLog(node.blockPool.head.blck.slot),
|
|
|
|
headEpoch = shortLog(node.blockPool.head.blck.slot.compute_epoch_at_slot()),
|
|
|
|
headRoot = shortLog(node.blockPool.head.blck.root),
|
|
|
|
finalizedSlot = shortLog(node.blockPool.finalizedHead.blck.slot),
|
|
|
|
finalizedEpoch = shortLog(node.blockPool.finalizedHead.blck.slot.compute_epoch_at_slot()),
|
|
|
|
finalizedRoot = shortLog(node.blockPool.finalizedHead.blck.root),
|
|
|
|
cat = "scheduling"
|
|
|
|
|
2019-03-22 15:49:37 +00:00
|
|
|
addTimer(nextSlotStart) do (p: pointer):
|
|
|
|
asyncCheck node.onSlotStart(slot, nextSlot)
|
|
|
|
|
2019-12-02 14:42:57 +00:00
|
|
|
proc handleMissingBlocks(node: BeaconNode) =
|
2019-04-26 16:38:56 +00:00
|
|
|
let missingBlocks = node.blockPool.checkMissing()
|
2019-03-28 14:03:19 +00:00
|
|
|
if missingBlocks.len > 0:
|
2019-12-02 14:42:57 +00:00
|
|
|
var left = missingBlocks.len
|
|
|
|
|
2019-03-28 14:03:19 +00:00
|
|
|
info "Requesting detected missing blocks", missingBlocks
|
2019-12-16 18:08:50 +00:00
|
|
|
node.requestManager.fetchAncestorBlocks(missingBlocks) do (b: SignedBeaconBlock):
|
2019-12-02 14:42:57 +00:00
|
|
|
onBeaconBlock(node, b)
|
|
|
|
|
|
|
|
# TODO instead of waiting for a full second to try the next missing block
|
|
|
|
# fetching, we'll do it here again in case we get all blocks we asked
|
|
|
|
# for (there might be new parents to fetch). of course, this is not
|
|
|
|
# good because the onSecond fetching also kicks in regardless but
|
|
|
|
# whatever - this is just a quick fix for making the testnet easier
|
|
|
|
# work with while the sync problem is dealt with more systematically
|
|
|
|
dec left
|
|
|
|
if left == 0:
|
|
|
|
addTimer(Moment.now()) do (p: pointer):
|
|
|
|
handleMissingBlocks(node)
|
|
|
|
|
|
|
|
proc onSecond(node: BeaconNode, moment: Moment) {.async.} =
|
|
|
|
node.handleMissingBlocks()
|
2019-03-27 20:17:01 +00:00
|
|
|
|
|
|
|
let nextSecond = max(Moment.now(), moment + chronos.seconds(1))
|
|
|
|
addTimer(nextSecond) do (p: pointer):
|
|
|
|
asyncCheck node.onSecond(nextSecond)
|
|
|
|
|
2019-02-21 04:42:17 +00:00
|
|
|
proc run*(node: BeaconNode) =
|
2019-12-16 18:08:50 +00:00
|
|
|
waitFor node.network.subscribe(topicBeaconBlocks) do (signedBlock: SignedBeaconBlock):
|
|
|
|
onBeaconBlock(node, signedBlock)
|
2019-02-21 04:42:17 +00:00
|
|
|
|
2019-03-05 22:54:08 +00:00
|
|
|
waitFor node.network.subscribe(topicAttestations) do (attestation: Attestation):
|
2019-10-25 14:00:55 +00:00
|
|
|
# Avoid double-counting attestation-topic attestations on shared codepath
|
|
|
|
# when they're reflected through beacon blocks
|
|
|
|
beacon_attestations_received.inc()
|
|
|
|
|
2019-02-21 04:42:17 +00:00
|
|
|
node.onAttestation(attestation)
|
2018-12-28 16:51:40 +00:00
|
|
|
|
2019-03-22 15:49:37 +00:00
|
|
|
let
|
2019-12-23 15:34:09 +00:00
|
|
|
t = node.beaconClock.now().toSlot()
|
|
|
|
curSlot = if t.afterGenesis: t.slot
|
|
|
|
else: GENESIS_SLOT
|
|
|
|
nextSlot = curSlot + 1 # No earlier than GENESIS_SLOT + 1
|
|
|
|
fromNow = saturate(node.beaconClock.fromNow(nextSlot))
|
2019-03-22 15:49:37 +00:00
|
|
|
|
|
|
|
info "Scheduling first slot action",
|
2019-08-16 11:16:56 +00:00
|
|
|
beaconTime = shortLog(node.beaconClock.now()),
|
2019-12-23 15:34:09 +00:00
|
|
|
nextSlot = shortLog(nextSlot),
|
2019-09-12 01:45:04 +00:00
|
|
|
fromNow = shortLog(fromNow),
|
|
|
|
cat = "scheduling"
|
2019-02-28 21:21:29 +00:00
|
|
|
|
2019-03-22 15:49:37 +00:00
|
|
|
addTimer(fromNow) do (p: pointer):
|
2019-12-23 15:34:09 +00:00
|
|
|
asyncCheck node.onSlotStart(curSlot, nextSlot)
|
2018-12-28 16:51:40 +00:00
|
|
|
|
2019-03-27 20:17:01 +00:00
|
|
|
let second = Moment.now() + chronos.seconds(1)
|
|
|
|
addTimer(second) do (p: pointer):
|
|
|
|
asyncCheck node.onSecond(second)
|
|
|
|
|
2019-01-25 17:35:22 +00:00
|
|
|
runForever()
|
2018-11-23 23:58:49 +00:00
|
|
|
|
2018-12-19 12:58:53 +00:00
|
|
|
var gPidFile: string
|
|
|
|
proc createPidFile(filename: string) =
|
|
|
|
createDir splitFile(filename).dir
|
2019-07-07 09:53:58 +00:00
|
|
|
writeFile filename, $os.getCurrentProcessId()
|
2018-12-19 12:58:53 +00:00
|
|
|
gPidFile = filename
|
|
|
|
addQuitProc proc {.noconv.} = removeFile gPidFile
|
|
|
|
|
2019-11-25 12:47:29 +00:00
|
|
|
proc start(node: BeaconNode) =
|
2019-03-20 11:52:30 +00:00
|
|
|
# TODO: while it's nice to cheat by waiting for connections here, we
|
|
|
|
# actually need to make this part of normal application flow -
|
|
|
|
# losing all connections might happen at any time and we should be
|
|
|
|
# prepared to handle it.
|
|
|
|
waitFor node.connectToNetwork()
|
|
|
|
|
2019-11-25 12:47:29 +00:00
|
|
|
let
|
|
|
|
head = node.blockPool.head
|
|
|
|
finalizedHead = node.blockPool.finalizedHead
|
|
|
|
|
2019-03-20 11:52:30 +00:00
|
|
|
info "Starting beacon node",
|
2019-11-12 00:05:35 +00:00
|
|
|
version = fullVersionStr,
|
2019-08-16 11:16:56 +00:00
|
|
|
timeSinceFinalization =
|
2019-11-25 12:47:29 +00:00
|
|
|
int64(finalizedHead.slot.toBeaconTime()) -
|
2019-03-22 15:49:37 +00:00
|
|
|
int64(node.beaconClock.now()),
|
2019-11-25 12:47:29 +00:00
|
|
|
headSlot = shortLog(head.blck.slot),
|
|
|
|
headRoot = shortLog(head.blck.root),
|
|
|
|
finalizedSlot = shortLog(finalizedHead.blck.slot),
|
|
|
|
finalizedRoot = shortLog(finalizedHead.blck.root),
|
2019-03-20 11:52:30 +00:00
|
|
|
SLOTS_PER_EPOCH,
|
|
|
|
SECONDS_PER_SLOT,
|
2019-09-12 01:45:04 +00:00
|
|
|
SPEC_VERSION,
|
2019-10-22 21:18:45 +00:00
|
|
|
dataDir = node.config.dataDir.string,
|
2019-09-12 01:45:04 +00:00
|
|
|
cat = "init",
|
|
|
|
pcs = "start_beacon_node"
|
2019-03-20 11:52:30 +00:00
|
|
|
|
2019-11-25 12:47:29 +00:00
|
|
|
let
|
|
|
|
bs = BlockSlot(blck: head.blck, slot: head.blck.slot)
|
|
|
|
|
2019-12-19 13:02:28 +00:00
|
|
|
node.blockPool.withState(node.blockPool.tmpState, bs):
|
2019-11-25 12:47:29 +00:00
|
|
|
node.addLocalValidators(state)
|
|
|
|
|
2019-03-20 11:52:30 +00:00
|
|
|
node.run()
|
|
|
|
|
2019-10-03 01:51:44 +00:00
|
|
|
func formatGwei(amount: uint64): string =
|
|
|
|
# TODO This is implemented in a quite a silly way.
|
|
|
|
# Better routines for formatting decimal numbers
|
|
|
|
# should exists somewhere else.
|
|
|
|
let
|
|
|
|
eth = amount div 1000000000
|
|
|
|
remainder = amount mod 1000000000
|
|
|
|
|
|
|
|
result = $eth
|
|
|
|
if remainder != 0:
|
|
|
|
result.add '.'
|
|
|
|
result.add $remainder
|
|
|
|
while result[^1] == '0':
|
|
|
|
result.setLen(result.len - 1)
|
|
|
|
|
2019-10-02 12:38:14 +00:00
|
|
|
when hasPrompt:
|
|
|
|
from unicode import Rune
|
|
|
|
import terminal, prompt
|
|
|
|
|
|
|
|
proc providePromptCompletions*(line: seq[Rune], cursorPos: int): seq[string] =
|
|
|
|
# TODO
|
|
|
|
# The completions should be generated with the general-purpose command-line
|
|
|
|
# parsing API of Confutils
|
|
|
|
result = @[]
|
|
|
|
|
2019-10-03 01:51:44 +00:00
|
|
|
proc processPromptCommands(p: ptr Prompt) {.thread.} =
|
|
|
|
while true:
|
|
|
|
var cmd = p[].readLine()
|
|
|
|
case cmd
|
|
|
|
of "quit":
|
|
|
|
quit 0
|
|
|
|
else:
|
|
|
|
p[].writeLine("Unknown command: " & cmd)
|
2019-10-02 12:38:14 +00:00
|
|
|
|
2019-10-07 08:29:39 +00:00
|
|
|
proc slotOrZero(time: BeaconTime): Slot =
|
|
|
|
let exSlot = time.toSlot
|
|
|
|
if exSlot.afterGenesis: exSlot.slot
|
|
|
|
else: Slot(0)
|
|
|
|
|
2019-10-03 01:51:44 +00:00
|
|
|
proc initPrompt(node: BeaconNode) =
|
2019-10-28 23:04:52 +00:00
|
|
|
if isatty(stdout) and node.config.statusBarEnabled:
|
2019-10-03 01:51:44 +00:00
|
|
|
enableTrueColors()
|
|
|
|
|
|
|
|
# TODO: nim-prompt seems to have threading issues at the moment
|
|
|
|
# which result in sporadic crashes. We should introduce a
|
|
|
|
# lock that guards the access to the internal prompt line
|
|
|
|
# variable.
|
|
|
|
#
|
|
|
|
# var p = Prompt.init("nimbus > ", providePromptCompletions)
|
|
|
|
# p.useHistoryFile()
|
|
|
|
|
|
|
|
proc dataResolver(expr: string): string =
|
|
|
|
# TODO:
|
|
|
|
# We should introduce a general API for resolving dot expressions
|
|
|
|
# such as `db.latest_block.slot` or `metrics.connected_peers`.
|
|
|
|
# Such an API can be shared between the RPC back-end, CLI tools
|
|
|
|
# such as ncli, a potential GraphQL back-end and so on.
|
|
|
|
# The status bar feature would allow the user to specify an
|
|
|
|
# arbitrary expression that is resolvable through this API.
|
|
|
|
case expr.toLowerAscii
|
|
|
|
of "connected_peers":
|
|
|
|
$(sync_protocol.libp2p_peers.value.int)
|
|
|
|
|
|
|
|
of "last_finalized_epoch":
|
|
|
|
var head = node.blockPool.finalizedHead
|
|
|
|
# TODO: Should we display a state root instead?
|
2019-10-07 08:29:39 +00:00
|
|
|
$(head.slot.epoch) & " (" & shortLog(head.blck.root) & ")"
|
|
|
|
|
|
|
|
of "epoch":
|
|
|
|
$node.beaconClock.now.slotOrZero.epoch
|
|
|
|
|
|
|
|
of "epoch_slot":
|
|
|
|
$(node.beaconClock.now.slotOrZero mod SLOTS_PER_EPOCH)
|
|
|
|
|
|
|
|
of "slots_per_epoch":
|
|
|
|
$SLOTS_PER_EPOCH
|
|
|
|
|
2019-10-29 19:48:32 +00:00
|
|
|
of "slot":
|
|
|
|
$node.beaconClock.now.slotOrZero
|
|
|
|
|
2019-10-07 08:29:39 +00:00
|
|
|
of "slot_trailing_digits":
|
|
|
|
var slotStr = $node.beaconClock.now.slotOrZero
|
|
|
|
if slotStr.len > 3: slotStr = slotStr[^3..^1]
|
|
|
|
slotStr
|
2019-10-03 01:51:44 +00:00
|
|
|
|
|
|
|
of "attached_validators_balance":
|
|
|
|
var balance = uint64(0)
|
2019-11-25 12:47:29 +00:00
|
|
|
# TODO slow linear scan!
|
2019-12-19 13:02:28 +00:00
|
|
|
for idx, b in node.blockPool.headState.data.data.balances:
|
2019-11-25 12:47:29 +00:00
|
|
|
if node.getAttachedValidator(
|
2019-12-19 13:02:28 +00:00
|
|
|
node.blockPool.headState.data.data, ValidatorIndex(idx)) != nil:
|
2019-11-25 12:47:29 +00:00
|
|
|
balance += b
|
2019-10-03 01:51:44 +00:00
|
|
|
formatGwei(balance)
|
|
|
|
|
|
|
|
else:
|
|
|
|
# We ignore typos for now and just render the expression
|
|
|
|
# as it was written. TODO: come up with a good way to show
|
|
|
|
# an error message to the user.
|
|
|
|
"$" & expr
|
|
|
|
|
|
|
|
var statusBar = StatusBarView.init(
|
2019-10-28 23:04:52 +00:00
|
|
|
node.config.statusBarContents,
|
2019-10-03 01:51:44 +00:00
|
|
|
dataResolver)
|
2019-10-02 12:38:14 +00:00
|
|
|
|
2019-10-29 01:22:06 +00:00
|
|
|
when compiles(defaultChroniclesStream.output.writer):
|
|
|
|
defaultChroniclesStream.output.writer =
|
|
|
|
proc (logLevel: LogLevel, msg: LogOutputStr) {.gcsafe.} =
|
|
|
|
# p.hidePrompt
|
|
|
|
erase statusBar
|
|
|
|
# p.writeLine msg
|
|
|
|
stdout.write msg
|
|
|
|
render statusBar
|
|
|
|
# p.showPrompt
|
2019-10-03 01:51:44 +00:00
|
|
|
|
|
|
|
proc statusBarUpdatesPollingLoop() {.async.} =
|
|
|
|
while true:
|
|
|
|
update statusBar
|
2019-11-25 12:47:29 +00:00
|
|
|
await sleepAsync(chronos.seconds(1))
|
2019-10-03 01:51:44 +00:00
|
|
|
|
|
|
|
traceAsyncErrors statusBarUpdatesPollingLoop()
|
|
|
|
|
|
|
|
# var t: Thread[ptr Prompt]
|
|
|
|
# createThread(t, processPromptCommands, addr p)
|
2019-10-02 12:38:14 +00:00
|
|
|
|
2018-11-23 23:58:49 +00:00
|
|
|
when isMainModule:
|
2019-03-25 23:26:11 +00:00
|
|
|
randomize()
|
2019-03-18 03:54:08 +00:00
|
|
|
|
2019-11-11 14:43:12 +00:00
|
|
|
let config = BeaconNodeConf.load(
|
|
|
|
version = clientId,
|
|
|
|
copyrightBanner = clientId & "\p" & copyrights)
|
2019-11-08 17:35:57 +00:00
|
|
|
|
2019-10-29 03:27:14 +00:00
|
|
|
when compiles(defaultChroniclesStream.output.writer):
|
|
|
|
defaultChroniclesStream.output.writer =
|
|
|
|
proc (logLevel: LogLevel, msg: LogOutputStr) {.gcsafe.} =
|
|
|
|
stdout.write(msg)
|
2019-10-03 01:51:44 +00:00
|
|
|
|
2019-01-21 19:42:37 +00:00
|
|
|
if config.logLevel != LogLevel.NONE:
|
|
|
|
setLogLevel(config.logLevel)
|
2019-01-16 23:01:15 +00:00
|
|
|
|
2019-07-11 02:36:07 +00:00
|
|
|
## Ctrl+C handling
|
|
|
|
proc controlCHandler() {.noconv.} =
|
|
|
|
when defined(windows):
|
|
|
|
# workaround for https://github.com/nim-lang/Nim/issues/4057
|
|
|
|
setupForeignThreadGc()
|
|
|
|
debug "Shutting down after having received SIGINT"
|
2019-11-08 17:35:57 +00:00
|
|
|
quit(QuitFailure)
|
2019-07-11 02:36:07 +00:00
|
|
|
setControlCHook(controlCHandler)
|
|
|
|
|
2018-12-19 12:58:53 +00:00
|
|
|
case config.cmd
|
2019-03-19 17:22:17 +00:00
|
|
|
of createTestnet:
|
|
|
|
var deposits: seq[Deposit]
|
2019-03-27 12:06:06 +00:00
|
|
|
for i in config.firstValidator.int ..< config.totalValidators.int:
|
2019-03-19 17:22:17 +00:00
|
|
|
let depositFile = config.validatorsDir /
|
|
|
|
validatorFileBaseName(i) & ".deposit.json"
|
2019-06-19 13:41:54 +00:00
|
|
|
try:
|
|
|
|
deposits.add Json.loadFile(depositFile, Deposit)
|
|
|
|
except SerializationError as err:
|
|
|
|
stderr.write "Error while loading a deposit file:\n"
|
|
|
|
stderr.write err.formatMsg(depositFile), "\n"
|
2019-09-01 15:02:49 +00:00
|
|
|
stderr.write "Please regenerate the deposit files by running makeDeposits again\n"
|
2019-06-19 13:41:54 +00:00
|
|
|
quit 1
|
2019-03-19 17:22:17 +00:00
|
|
|
|
2019-10-29 16:46:41 +00:00
|
|
|
let
|
2019-09-02 10:31:14 +00:00
|
|
|
startTime = uint64(times.toUnix(times.getTime()) + config.genesisOffset)
|
2019-10-29 16:46:41 +00:00
|
|
|
outGenesis = config.outputGenesis.string
|
|
|
|
eth1Hash = if config.depositWeb3Url.len == 0: eth1BlockHash
|
|
|
|
else: waitFor getLatestEth1BlockHash(config.depositWeb3Url)
|
|
|
|
var
|
2019-09-02 10:31:14 +00:00
|
|
|
initialState = initialize_beacon_state_from_eth1(
|
2019-10-25 14:53:31 +00:00
|
|
|
eth1Hash, startTime, deposits, {skipValidation})
|
2019-09-02 10:31:14 +00:00
|
|
|
|
|
|
|
# https://github.com/ethereum/eth2.0-pm/tree/6e41fcf383ebeb5125938850d8e9b4e9888389b4/interop/mocked_start#create-genesis-state
|
|
|
|
initialState.genesis_time = startTime
|
2019-07-01 13:20:55 +00:00
|
|
|
|
|
|
|
doAssert initialState.validators.len > 0
|
2019-03-19 17:22:17 +00:00
|
|
|
|
2019-10-29 16:46:41 +00:00
|
|
|
let outGenesisExt = splitFile(outGenesis).ext
|
|
|
|
if cmpIgnoreCase(outGenesisExt, ".json") == 0:
|
|
|
|
Json.saveFile(outGenesis, initialState, pretty = true)
|
|
|
|
echo "Wrote ", outGenesis
|
2019-03-19 17:22:17 +00:00
|
|
|
|
2019-10-29 16:46:41 +00:00
|
|
|
let outSszGenesis = outGenesis.changeFileExt "ssz"
|
|
|
|
SSZ.saveFile(outSszGenesis, initialState)
|
|
|
|
echo "Wrote ", outSszGenesis
|
2019-09-08 15:32:38 +00:00
|
|
|
|
2019-03-19 17:22:17 +00:00
|
|
|
var
|
|
|
|
bootstrapAddress = getPersistenBootstrapAddr(
|
|
|
|
config, parseIpAddress(config.bootstrapAddress), Port config.bootstrapPort)
|
|
|
|
|
2019-10-28 23:04:52 +00:00
|
|
|
let bootstrapFile = config.outputBootstrapFile.string
|
2019-09-27 16:05:17 +00:00
|
|
|
if bootstrapFile.len > 0:
|
2019-11-12 19:56:37 +00:00
|
|
|
let bootstrapAddrLine = $bootstrapAddress
|
2019-09-27 16:05:17 +00:00
|
|
|
writeFile(bootstrapFile, bootstrapAddrLine)
|
|
|
|
echo "Wrote ", bootstrapFile
|
|
|
|
|
2019-03-25 23:26:11 +00:00
|
|
|
of importValidator:
|
2019-03-18 03:54:08 +00:00
|
|
|
template reportFailureFor(keyExpr) =
|
|
|
|
error "Failed to import validator key", key = keyExpr
|
|
|
|
programResult = 1
|
|
|
|
|
2019-10-28 23:04:52 +00:00
|
|
|
if config.keyFiles.len == 0:
|
|
|
|
stderr.write "Please specify at least one keyfile to import."
|
|
|
|
quit 1
|
|
|
|
|
2019-03-19 19:50:22 +00:00
|
|
|
for keyFile in config.keyFiles:
|
2019-03-18 03:54:08 +00:00
|
|
|
try:
|
2019-03-25 23:26:11 +00:00
|
|
|
saveValidatorKey(keyFile.string.extractFilename,
|
|
|
|
readFile(keyFile.string), config)
|
2019-03-18 03:54:08 +00:00
|
|
|
except:
|
2019-03-19 19:50:22 +00:00
|
|
|
reportFailureFor keyFile.string
|
2019-03-18 03:54:08 +00:00
|
|
|
|
2018-12-19 12:58:53 +00:00
|
|
|
of noCommand:
|
2018-12-28 16:51:40 +00:00
|
|
|
createPidFile(config.dataDir.string / "beacon_node.pid")
|
2018-12-19 12:58:53 +00:00
|
|
|
|
2019-03-05 22:54:08 +00:00
|
|
|
var node = waitFor BeaconNode.init(config)
|
2019-10-02 12:38:14 +00:00
|
|
|
when hasPrompt: initPrompt(node)
|
2018-12-19 12:58:53 +00:00
|
|
|
|
2019-03-22 14:35:20 +00:00
|
|
|
if node.nickname != "":
|
2019-11-25 12:47:29 +00:00
|
|
|
dynamicLogScope(node = node.nickname): node.start()
|
2019-03-20 11:52:30 +00:00
|
|
|
else:
|
2019-11-25 12:47:29 +00:00
|
|
|
node.start()
|
2019-09-01 15:02:49 +00:00
|
|
|
|
|
|
|
of makeDeposits:
|
2019-11-05 18:16:10 +00:00
|
|
|
createDir(config.depositsDir)
|
|
|
|
|
2019-10-29 02:43:23 +00:00
|
|
|
let
|
|
|
|
quickstartDeposits = generateDeposits(
|
|
|
|
config.totalQuickstartDeposits, config.depositsDir, false)
|
|
|
|
|
|
|
|
randomDeposits = generateDeposits(
|
|
|
|
config.totalRandomDeposits, config.depositsDir, true,
|
|
|
|
firstIdx = config.totalQuickstartDeposits)
|
2019-09-01 15:02:49 +00:00
|
|
|
|
2019-10-29 02:43:23 +00:00
|
|
|
if config.depositWeb3Url.len > 0 and config.depositContractAddress.len > 0:
|
2019-12-02 23:27:59 +00:00
|
|
|
info "Sending deposits",
|
|
|
|
web3 = config.depositWeb3Url,
|
|
|
|
depositContract = config.depositContractAddress
|
|
|
|
|
2019-09-01 15:02:49 +00:00
|
|
|
waitFor sendDeposits(
|
2019-10-29 02:43:23 +00:00
|
|
|
quickstartDeposits & randomDeposits,
|
|
|
|
config.depositWeb3Url,
|
2019-11-05 18:16:10 +00:00
|
|
|
config.depositContractAddress,
|
|
|
|
config.depositPrivateKey)
|
2019-11-09 10:46:34 +00:00
|
|
|
|
|
|
|
of query:
|
|
|
|
case config.queryCmd
|
|
|
|
of QueryCmd.nimQuery:
|
|
|
|
# TODO: This will handle a simple subset of Nim using
|
|
|
|
# dot syntax and `[]` indexing.
|
|
|
|
echo "nim query: ", config.nimQueryExpression
|
|
|
|
|
|
|
|
of QueryCmd.get:
|
|
|
|
let pathFragments = config.getQueryPath.split('/', maxsplit = 1)
|
2019-12-10 14:20:40 +00:00
|
|
|
let bytes =
|
|
|
|
case pathFragments[0]
|
|
|
|
of "genesis_state":
|
|
|
|
readFile(config.dataDir/genesisFile).string.toBytes()
|
|
|
|
else:
|
|
|
|
stderr.write config.getQueryPath & " is not a valid path"
|
|
|
|
quit 1
|
2019-11-09 10:46:34 +00:00
|
|
|
|
2019-12-10 14:20:40 +00:00
|
|
|
let navigator = DynamicSszNavigator.init(bytes, BeaconState)
|
2019-11-09 10:46:34 +00:00
|
|
|
|
|
|
|
echo navigator.navigatePath(pathFragments[1 .. ^1]).toJson
|