2018-11-23 23:58:49 +00:00
|
|
|
import
|
2019-03-18 03:54:08 +00:00
|
|
|
std_shims/[os_shims, objects], net, sequtils, options, tables, osproc, random,
|
2019-03-20 00:05:10 +00:00
|
|
|
chronos, chronicles, confutils, serialization/errors,
|
2019-03-20 20:01:48 +00:00
|
|
|
spec/[bitfield, datatypes, digest, crypto, beaconstate, helpers, validator],
|
|
|
|
conf, time,
|
2018-12-28 16:51:40 +00:00
|
|
|
state_transition, fork_choice, ssz, beacon_chain_db, validator_pool, extras,
|
2019-03-13 22:59:20 +00:00
|
|
|
attestation_pool, block_pool, eth2_network, beacon_node_types,
|
2019-03-18 03:54:08 +00:00
|
|
|
mainchain_monitor, trusted_state_snapshots, version,
|
2019-03-13 22:59:20 +00:00
|
|
|
eth/trie/db, eth/trie/backends/rocksdb_backend
|
2018-11-23 23:58:49 +00:00
|
|
|
|
|
|
|
const
|
2018-11-26 13:33:06 +00:00
|
|
|
topicBeaconBlocks = "ethereum/2.1/beacon_chain/blocks"
|
|
|
|
topicAttestations = "ethereum/2.1/beacon_chain/attestations"
|
2019-02-28 21:21:29 +00:00
|
|
|
topicfetchBlocks = "ethereum/2.1/beacon_chain/fetch"
|
2019-02-07 21:14:08 +00:00
|
|
|
|
2019-03-18 03:54:08 +00:00
|
|
|
dataDirValidators = "validators"
|
|
|
|
networkMetadataFile = "network.json"
|
|
|
|
genesisFile = "genesis.json"
|
2019-03-19 17:22:17 +00:00
|
|
|
testnetsBaseUrl = "https://serenity-testnets.status.im"
|
2019-03-18 03:54:08 +00:00
|
|
|
|
2019-03-12 15:03:14 +00:00
|
|
|
# #################################################
|
|
|
|
# Careful handling of beacon_node <-> sync_protocol
|
|
|
|
# to avoid recursive dependencies
|
2019-02-18 10:34:39 +00:00
|
|
|
proc onBeaconBlock*(node: BeaconNode, blck: BeaconBlock) {.gcsafe.}
|
2019-03-12 15:03:14 +00:00
|
|
|
# Forward decl for sync_protocol
|
2019-02-18 10:34:39 +00:00
|
|
|
import sync_protocol
|
2019-03-12 15:03:14 +00:00
|
|
|
# #################################################
|
2019-02-18 10:34:39 +00:00
|
|
|
|
2019-01-25 17:35:22 +00:00
|
|
|
func shortValidatorKey(node: BeaconNode, validatorIdx: int): string =
|
2019-02-28 21:21:29 +00:00
|
|
|
($node.state.data.validator_registry[validatorIdx].pubkey)[0..7]
|
|
|
|
|
2019-03-18 03:54:08 +00:00
|
|
|
func localValidatorsDir(conf: BeaconNodeConf): string =
|
|
|
|
conf.dataDir / "validators"
|
|
|
|
|
|
|
|
func databaseDir(conf: BeaconNodeConf): string =
|
|
|
|
conf.dataDir / "db"
|
|
|
|
|
2019-02-28 21:21:29 +00:00
|
|
|
func slotStart(node: BeaconNode, slot: Slot): Timestamp =
|
|
|
|
node.state.data.slotStart(slot)
|
2019-01-05 21:01:26 +00:00
|
|
|
|
2019-03-18 03:54:08 +00:00
|
|
|
template `//`(url, fragment: string): string =
|
|
|
|
url & "/" & fragment
|
|
|
|
|
|
|
|
proc downloadFile(url: string): Future[string] {.async.} =
|
2019-03-20 00:05:10 +00:00
|
|
|
let (fileContents, errorCode) = execCmdEx("curl --fail " & url, options = {poUsePath})
|
2019-03-18 03:54:08 +00:00
|
|
|
if errorCode != 0:
|
|
|
|
raise newException(IOError, "Failed to download URL: " & url)
|
|
|
|
return fileContents
|
|
|
|
|
2019-03-19 17:22:17 +00:00
|
|
|
proc updateTestnetMetadata(conf: BeaconNodeConf): Future[NetworkMetadata] {.async.} =
|
2019-03-19 19:50:22 +00:00
|
|
|
let latestMetadata = await downloadFile(testnetsBaseUrl // $conf.network //
|
|
|
|
netBackendName & "-" & networkMetadataFile)
|
2019-03-18 03:54:08 +00:00
|
|
|
|
2019-03-20 00:05:10 +00:00
|
|
|
result = Json.decode(latestMetadata, NetworkMetadata)
|
|
|
|
|
2019-03-18 03:54:08 +00:00
|
|
|
let localMetadataFile = conf.dataDir / networkMetadataFile
|
|
|
|
if fileExists(localMetadataFile) and readFile(localMetadataFile).string == latestMetadata:
|
|
|
|
return
|
|
|
|
|
2019-03-19 17:22:17 +00:00
|
|
|
info "New testnet genesis data received. Starting with a fresh database."
|
2019-03-18 03:54:08 +00:00
|
|
|
removeDir conf.databaseDir
|
|
|
|
writeFile localMetadataFile, latestMetadata
|
|
|
|
|
|
|
|
let newGenesis = await downloadFile(testnetsBaseUrl // $conf.network // genesisFile)
|
|
|
|
writeFile conf.dataDir / genesisFile, newGenesis
|
|
|
|
|
|
|
|
proc obtainTestnetKey(conf: BeaconNodeConf): Future[ValidatorPrivKey] {.async.} =
|
|
|
|
let
|
2019-03-19 17:22:17 +00:00
|
|
|
metadata = await updateTestnetMetadata(conf)
|
2019-03-18 03:54:08 +00:00
|
|
|
privKeyName = validatorFileBaseName(rand(metadata.userValidatorsRange)) & ".privkey"
|
|
|
|
privKeyContent = await downloadFile(testnetsBaseUrl // $conf.network // privKeyName)
|
|
|
|
|
|
|
|
return ValidatorPrivKey.init(privKeyContent)
|
|
|
|
|
|
|
|
proc saveValidatorKey(key: ValidatorPrivKey, conf: BeaconNodeConf) =
|
2019-03-19 21:04:22 +00:00
|
|
|
let validatorsDir = conf.dataDir / dataDirValidators
|
|
|
|
createDir validatorsDir
|
|
|
|
writeFile(validatorsDir / $key.pubKey, $key)
|
2019-03-18 03:54:08 +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
|
2018-12-19 12:58:53 +00:00
|
|
|
|
2019-03-19 17:22:17 +00:00
|
|
|
template fail(args: varargs[untyped]) =
|
|
|
|
stderr.write args, "\n"
|
|
|
|
quit 1
|
|
|
|
|
|
|
|
case conf.network
|
|
|
|
of "mainnet":
|
|
|
|
fail "The Serenity mainnet hasn't been launched yet"
|
|
|
|
of "testnet0", "testnet1":
|
|
|
|
result.networkMetadata = await updateTestnetMetadata(conf)
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
result.networkMetadata = Json.loadFile(conf.network, NetworkMetadata)
|
|
|
|
except:
|
|
|
|
fail "Failed to load network metadata: ", getCurrentExceptionMsg()
|
|
|
|
|
|
|
|
var metadataErrorMsg = ""
|
|
|
|
|
|
|
|
template checkCompatibility(metadataField, LOCAL_CONSTANT) =
|
|
|
|
let metadataValue = metadataField
|
|
|
|
if metadataValue != LOCAL_CONSTANT:
|
|
|
|
metadataErrorMsg.add " -d:" & astToStr(LOCAL_CONSTANT) & "=" & $metadataValue
|
|
|
|
|
|
|
|
checkCompatibility result.networkMetadata.numShards , SHARD_COUNT
|
|
|
|
checkCompatibility result.networkMetadata.slotDuration , SECONDS_PER_SLOT
|
|
|
|
checkCompatibility result.networkMetadata.slotsPerEpoch , SLOTS_PER_EPOCH
|
|
|
|
|
|
|
|
if metadataErrorMsg.len > 0:
|
2019-03-20 00:05:10 +00:00
|
|
|
fail "To connect to the ", conf.network, " network, please compile with", metadataErrorMsg
|
2019-03-18 03:54:08 +00:00
|
|
|
|
2018-12-28 16:51:40 +00:00
|
|
|
result.attachedValidators = ValidatorPool.init
|
|
|
|
init result.mainchainMonitor, "", Port(0) # TODO: specify geth address and port
|
|
|
|
|
2019-03-18 03:54:08 +00:00
|
|
|
let trieDB = trieDB newChainDb(string 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-02-28 21:21:29 +00:00
|
|
|
let headBlock = result.db.getHeadBlock()
|
|
|
|
if headBlock.isNone():
|
2019-03-18 03:54:08 +00:00
|
|
|
var snapshotFile = conf.dataDir / genesisFile
|
|
|
|
if conf.stateSnapshot.isSome:
|
|
|
|
snapshotFile = conf.stateSnapshot.get.string
|
|
|
|
elif not fileExists(snapshotFile):
|
|
|
|
error "Nimbus database not initialized. Please specify the initial state snapshot file."
|
|
|
|
quit 1
|
|
|
|
|
2019-03-20 00:05:10 +00:00
|
|
|
try:
|
|
|
|
info "Importing snapshot file", path = snapshotFile
|
|
|
|
|
|
|
|
let
|
|
|
|
tailState = Json.loadFile(snapshotFile, BeaconState)
|
|
|
|
tailBlock = get_initial_beacon_block(tailState)
|
|
|
|
blockRoot = hash_tree_root_final(tailBlock)
|
|
|
|
|
|
|
|
notice "Creating new database from snapshot",
|
|
|
|
blockRoot = shortLog(blockRoot),
|
|
|
|
stateRoot = shortLog(tailBlock.state_root),
|
|
|
|
fork = tailState.fork,
|
|
|
|
validators = tailState.validator_registry.len()
|
|
|
|
|
|
|
|
result.db.putState(tailState)
|
|
|
|
result.db.putBlock(tailBlock)
|
|
|
|
result.db.putTailBlock(blockRoot)
|
|
|
|
result.db.putHeadBlock(blockRoot)
|
|
|
|
|
|
|
|
except SerializationError as err:
|
|
|
|
stderr.write "Failed to import ", snapshotFile, "\n"
|
|
|
|
stderr.write err.formatMsg(snapshotFile), "\n"
|
|
|
|
quit 1
|
|
|
|
except:
|
|
|
|
stderr.write "Failed to initialize database\n"
|
|
|
|
stderr.write getCurrentExceptionMsg(), "\n"
|
|
|
|
quit 1
|
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-03-18 03:54:08 +00:00
|
|
|
result.network = await createEth2Node(conf)
|
2018-12-19 12:58:53 +00:00
|
|
|
|
2019-03-18 03:54:08 +00:00
|
|
|
let sync = result.network.protocolState(BeaconSync)
|
2019-03-19 17:22:17 +00:00
|
|
|
sync.networkId = result.networkMetadata.networkId
|
2019-03-18 03:54:08 +00:00
|
|
|
sync.node = result
|
|
|
|
sync.db = result.db
|
2019-02-28 21:21:29 +00:00
|
|
|
|
2019-03-05 22:54:08 +00:00
|
|
|
let head = result.blockPool.get(result.db.getHeadBlock().get())
|
2019-02-28 21:21:29 +00:00
|
|
|
|
|
|
|
result.state = result.blockPool.loadTailState()
|
2018-11-23 23:58:49 +00:00
|
|
|
|
2019-03-05 22:54:08 +00:00
|
|
|
let addressFile = string(conf.dataDir) / "beacon_node.address"
|
|
|
|
result.network.saveConnectionAddressFile(addressFile)
|
2018-12-19 12:58:53 +00:00
|
|
|
|
|
|
|
proc connectToNetwork(node: BeaconNode) {.async.} =
|
2019-03-20 01:18:49 +00:00
|
|
|
let localKeys = ensureNetworkKeys(node.config)
|
|
|
|
var bootstrapNodes = newSeq[BootstrapAddr]()
|
|
|
|
|
|
|
|
for bootNode in node.networkMetadata.bootstrapNodes:
|
|
|
|
if bootNode.pubkey == localKeys.pubKey:
|
|
|
|
node.isBootstrapNode = true
|
|
|
|
else:
|
|
|
|
bootstrapNodes.add bootNode
|
2018-12-19 12:58:53 +00:00
|
|
|
|
2019-03-19 17:22:17 +00:00
|
|
|
for bootNode in node.config.bootstrapNodes:
|
|
|
|
bootstrapNodes.add BootstrapAddr.init(bootNode)
|
2018-12-19 12:58:53 +00:00
|
|
|
|
|
|
|
let bootstrapFile = string node.config.bootstrapNodesFile
|
|
|
|
if bootstrapFile.len > 0:
|
|
|
|
for ln in lines(bootstrapFile):
|
2019-03-05 22:54:08 +00:00
|
|
|
bootstrapNodes.add BootstrapAddr.init(string ln)
|
2018-12-19 12:58:53 +00:00
|
|
|
|
|
|
|
if bootstrapNodes.len > 0:
|
2018-12-28 16:51:40 +00:00
|
|
|
info "Connecting to bootstrap nodes", 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-02-27 08:15:24 +00:00
|
|
|
await node.network.connectToNetwork(bootstrapNodes)
|
2018-12-19 12:58:53 +00:00
|
|
|
|
2018-11-23 23:58:49 +00:00
|
|
|
proc sync*(node: BeaconNode): Future[bool] {.async.} =
|
2019-02-28 21:21:29 +00:00
|
|
|
if node.state.data.slotDistanceFromNow() > WEAK_SUBJECTVITY_PERIOD.int64:
|
|
|
|
# node.state.data = await obtainTrustedStateSnapshot(node.db)
|
|
|
|
return false
|
2018-11-23 23:58:49 +00:00
|
|
|
else:
|
2019-02-28 21:21:29 +00:00
|
|
|
# TODO waiting for genesis should probably be moved elsewhere.. it has
|
|
|
|
# little to do with syncing..
|
2019-01-16 23:01:15 +00:00
|
|
|
let t = now()
|
2019-02-28 21:21:29 +00:00
|
|
|
if t < node.state.data.genesis_time * 1000:
|
|
|
|
notice "Waiting for genesis",
|
|
|
|
fromNow = int(node.state.data.genesis_time * 1000 - t) div 1000
|
|
|
|
await sleepAsync int(node.state.data.genesis_time * 1000 - t)
|
|
|
|
|
|
|
|
let
|
|
|
|
targetSlot = node.state.data.getSlotFromTime()
|
2019-01-16 23:01:15 +00:00
|
|
|
|
2019-02-15 16:33:32 +00:00
|
|
|
# TODO: change this to a full sync / block download
|
|
|
|
info "Syncing state from remote peers",
|
2019-02-28 21:21:29 +00:00
|
|
|
finalized_epoch = humaneEpochNum(node.state.data.finalized_epoch),
|
2019-02-15 16:33:32 +00:00
|
|
|
target_slot_epoch = humaneEpochNum(targetSlot.slot_to_epoch)
|
|
|
|
|
2019-02-21 04:42:17 +00:00
|
|
|
# TODO: sync is called at the beginning of the program, but doing this kind
|
|
|
|
# of catching up here is wrong - if we fall behind on processing
|
|
|
|
# for whatever reason, we want to be safe against the damage that
|
|
|
|
# might cause regardless if we just started or have been running for
|
|
|
|
# long. A classic example where this might happen is when the
|
|
|
|
# computer goes to sleep - when waking up, we'll be in the middle of
|
|
|
|
# processing, but behind everyone else.
|
2019-02-28 21:21:29 +00:00
|
|
|
# TOOD we now detect during epoch scheduling if we're very far behind -
|
|
|
|
# that would potentially be a good place to run the sync (?)
|
2019-02-21 04:42:17 +00:00
|
|
|
# while node.beaconState.finalized_epoch < targetSlot.slot_to_epoch:
|
|
|
|
# var (peer, changeLog) = await node.network.getValidatorChangeLog(
|
|
|
|
# node.beaconState.validator_registry_delta_chain_tip)
|
|
|
|
|
|
|
|
# if peer == nil:
|
|
|
|
# error "Failed to sync with any peer"
|
|
|
|
# return false
|
|
|
|
|
|
|
|
# if applyValidatorChangeLog(changeLog, node.beaconState):
|
|
|
|
# node.db.persistState(node.beaconState)
|
|
|
|
# node.db.persistBlock(changeLog.signedBlock)
|
|
|
|
# else:
|
|
|
|
# warn "Ignoring invalid validator change log", sentFrom = peer
|
2018-11-23 23:58:49 +00:00
|
|
|
|
|
|
|
return true
|
|
|
|
|
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-03-18 03:54:08 +00:00
|
|
|
proc addLocalValidator(node: BeaconNode, validatorKey: ValidatorPrivKey) =
|
|
|
|
let pubKey = validatorKey.pubKey()
|
2018-12-19 12:58:53 +00:00
|
|
|
|
2019-03-18 03:54:08 +00:00
|
|
|
let idx = node.state.data.validator_registry.findIt(it.pubKey == pubKey)
|
|
|
|
if idx == -1:
|
|
|
|
warn "Validator not in registry", pubKey
|
|
|
|
else:
|
|
|
|
debug "Attaching validator", validator = shortValidatorKey(node, idx),
|
|
|
|
idx, pubKey
|
|
|
|
node.attachedValidators.addLocalValidator(idx, pubKey, validatorKey)
|
|
|
|
|
|
|
|
proc addLocalValidators(node: BeaconNode) =
|
|
|
|
for validatorKeyFile in node.config.validators:
|
|
|
|
node.addLocalValidator validatorKeyFile.load
|
|
|
|
|
|
|
|
for kind, file in walkDir(node.config.localValidatorsDir):
|
|
|
|
if kind in {pcFile, pcLinkToFile}:
|
|
|
|
node.addLocalValidator ValidatorPrivKey.init(readFile(file).string)
|
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
|
|
|
|
2018-11-26 13:33:06 +00:00
|
|
|
proc getAttachedValidator(node: BeaconNode, idx: int): AttachedValidator =
|
2019-02-28 21:21:29 +00:00
|
|
|
let validatorKey = node.state.data.validator_registry[idx].pubkey
|
2018-11-26 13:33:06 +00:00
|
|
|
return node.attachedValidators.getValidator(validatorKey)
|
|
|
|
|
2019-03-13 22:59:20 +00:00
|
|
|
proc updateHead(node: BeaconNode): BlockRef =
|
|
|
|
# TODO move all of this logic to BlockPool
|
2019-03-22 11:33:10 +00:00
|
|
|
info "Preparing for fork choice",
|
|
|
|
connectedPeers = node.network.connectedPeers
|
|
|
|
|
2019-03-13 22:59:20 +00:00
|
|
|
let
|
|
|
|
justifiedHead = node.blockPool.latestJustifiedBlock()
|
2019-02-28 21:21:29 +00:00
|
|
|
|
2019-03-14 13:33:56 +00:00
|
|
|
# TODO slot number is wrong here, it should be the start of the epoch that
|
|
|
|
# got finalized:
|
|
|
|
# https://github.com/ethereum/eth2.0-specs/issues/768
|
|
|
|
node.blockPool.updateState(node.state, justifiedHead, justifiedHead.slot)
|
2019-02-28 21:21:29 +00:00
|
|
|
|
2019-03-13 22:59:20 +00:00
|
|
|
let newHead = lmdGhost(node.attestationPool, node.state.data, justifiedHead)
|
|
|
|
node.blockPool.updateHead(node.state, newHead)
|
|
|
|
newHead
|
2019-02-28 21:21:29 +00:00
|
|
|
|
2018-11-29 01:08:34 +00:00
|
|
|
proc makeAttestation(node: BeaconNode,
|
2018-12-28 16:51:40 +00:00
|
|
|
validator: AttachedValidator,
|
2019-02-20 01:33:58 +00:00
|
|
|
slot: Slot,
|
2018-12-28 16:51:40 +00:00
|
|
|
shard: uint64,
|
|
|
|
committeeLen: int,
|
|
|
|
indexInCommittee: int) {.async.} =
|
|
|
|
doAssert node != nil
|
|
|
|
doAssert validator != nil
|
|
|
|
|
2019-02-28 21:21:29 +00:00
|
|
|
# It's time to make an attestation. To do so, we must determine what we
|
|
|
|
# consider to be the head block - this is done by the fork choice rule.
|
|
|
|
# TODO this lazy update of the head is good because it delays head resolution
|
|
|
|
# until the very latest moment - on the other hand, if it takes long, the
|
|
|
|
# attestation might be late!
|
2019-03-14 13:33:56 +00:00
|
|
|
let
|
|
|
|
head = node.updateHead()
|
|
|
|
|
|
|
|
if slot + MIN_ATTESTATION_INCLUSION_DELAY < head.slot:
|
|
|
|
# What happened here is that we're being really slow or there's something
|
|
|
|
# really fishy going on with the slot - let's not send out any attestations
|
|
|
|
# just in case...
|
|
|
|
# TODO is this the right cutoff?
|
|
|
|
notice "Skipping attestation, head is too recent",
|
|
|
|
headSlot = humaneSlotNum(head.slot),
|
|
|
|
slot = humaneSlotNum(slot)
|
|
|
|
return
|
2019-03-13 22:59:20 +00:00
|
|
|
|
2019-03-14 13:33:56 +00:00
|
|
|
let attestationHead = head.findAncestorBySlot(slot)
|
|
|
|
if head != attestationHead:
|
|
|
|
# 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?",
|
|
|
|
headSlot = humaneSlotNum(head.slot),
|
|
|
|
attestationHeadSlot = humaneSlotNum(attestationHead.slot),
|
|
|
|
attestationSlot = humaneSlotNum(slot)
|
|
|
|
|
|
|
|
# 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.
|
|
|
|
node.blockPool.updateState(node.state, attestationHead, slot)
|
2019-02-28 21:21:29 +00:00
|
|
|
|
|
|
|
# Check pending attestations - maybe we found some blocks for them
|
|
|
|
node.attestationPool.resolve(node.state.data)
|
|
|
|
|
2019-03-09 04:23:14 +00:00
|
|
|
let
|
2019-03-13 22:59:20 +00:00
|
|
|
attestationData =
|
|
|
|
makeAttestationData(node.state.data, shard, node.state.blck.root)
|
2019-03-14 13:33:56 +00:00
|
|
|
|
|
|
|
# Careful - after await. node.state (etc) might have changed in async race
|
2019-02-19 23:35:02 +00:00
|
|
|
validatorSignature = await validator.signAttestation(attestationData)
|
2018-12-28 16:51:40 +00:00
|
|
|
|
2019-03-20 20:01:48 +00:00
|
|
|
var aggregationBitfield = BitField.init(committeeLen)
|
|
|
|
set_bitfield_bit(aggregationBitfield, indexInCommittee)
|
2018-12-28 16:51:40 +00:00
|
|
|
|
|
|
|
var attestation = Attestation(
|
|
|
|
data: attestationData,
|
|
|
|
aggregate_signature: validatorSignature,
|
2019-03-09 04:23:14 +00:00
|
|
|
aggregation_bitfield: aggregationBitfield,
|
2019-02-12 22:50:02 +00:00
|
|
|
# Stub in phase0
|
2019-03-20 20:01:48 +00:00
|
|
|
custody_bitfield: BitField.init(committeeLen)
|
2019-02-12 22:50:02 +00:00
|
|
|
)
|
2018-11-26 13:33:06 +00:00
|
|
|
|
2019-02-28 21:21:29 +00:00
|
|
|
# TODO what are we waiting for here? broadcast should never block, and never
|
|
|
|
# fail...
|
2018-11-29 01:08:34 +00:00
|
|
|
await node.network.broadcast(topicAttestations, attestation)
|
2018-11-26 13:33:06 +00:00
|
|
|
|
2019-02-19 23:35:02 +00:00
|
|
|
info "Attestation sent",
|
2019-03-20 20:01:48 +00:00
|
|
|
attestationData = shortLog(attestationData),
|
2019-02-19 23:35:02 +00:00
|
|
|
validator = shortValidatorKey(node, validator.idx),
|
2019-03-20 20:01:48 +00:00
|
|
|
signature = shortLog(validatorSignature)
|
2018-12-28 16:51:40 +00:00
|
|
|
|
2018-11-29 01:08:34 +00:00
|
|
|
proc proposeBlock(node: BeaconNode,
|
|
|
|
validator: AttachedValidator,
|
2019-02-20 01:33:58 +00:00
|
|
|
slot: Slot) {.async.} =
|
2018-12-28 16:51:40 +00:00
|
|
|
doAssert node != nil
|
|
|
|
doAssert validator != nil
|
2019-02-28 21:21:29 +00:00
|
|
|
doAssert validator.idx < node.state.data.validator_registry.len
|
2018-12-28 16:51:40 +00:00
|
|
|
|
2019-02-28 21:21:29 +00:00
|
|
|
# To propose a block, we should know what the head is, because that's what
|
|
|
|
# we'll be building the next block upon..
|
2019-03-13 22:59:20 +00:00
|
|
|
let head = node.updateHead()
|
|
|
|
|
2019-03-14 13:33:56 +00:00
|
|
|
if head.slot > slot:
|
|
|
|
notice "Skipping proposal, we've already selected a newer head",
|
|
|
|
headSlot = humaneSlotNum(head.slot),
|
|
|
|
headBlockRoot = shortLog(head.root),
|
|
|
|
slot = humaneSlotNum(slot)
|
|
|
|
|
|
|
|
if head.slot == slot:
|
|
|
|
# Weird, we should never see as head the same slot as we're proposing a
|
|
|
|
# block for - did someone else steal our slot? why didn't we discard it?
|
|
|
|
warn "Found head at same slot as we're supposed to propose for!",
|
|
|
|
headSlot = humaneSlotNum(head.slot),
|
|
|
|
headBlockRoot = shortLog(head.root)
|
|
|
|
# TODO investigate how and when this happens.. maybe it shouldn't be an
|
|
|
|
# assert?
|
|
|
|
doAssert false, "head slot matches proposal slot (!)"
|
|
|
|
# return
|
|
|
|
|
|
|
|
# There might be gaps between our proposal and what we think is the head -
|
|
|
|
# make sure the state we get takes that into account: we want it to point
|
|
|
|
# to the slot just before our proposal.
|
|
|
|
node.blockPool.updateState(node.state, head, slot - 1)
|
2019-01-05 21:01:26 +00:00
|
|
|
|
2019-02-28 21:21:29 +00:00
|
|
|
# To create a block, we'll first apply a partial block to the state, skipping
|
|
|
|
# some validations.
|
2018-12-28 16:51:40 +00:00
|
|
|
var blockBody = BeaconBlockBody(
|
2019-03-16 19:52:37 +00:00
|
|
|
randao_reveal: validator.genRandaoReveal(node.state.data, slot),
|
|
|
|
eth1_data: node.mainchainMonitor.getBeaconBlockRef(),
|
2019-02-28 21:21:29 +00:00
|
|
|
attestations: node.attestationPool.getAttestationsForBlock(slot))
|
2018-12-28 16:51:40 +00:00
|
|
|
|
|
|
|
var newBlock = BeaconBlock(
|
|
|
|
slot: slot,
|
2019-03-16 19:52:37 +00:00
|
|
|
previous_block_root: node.state.blck.root,
|
2019-03-11 15:33:24 +00:00
|
|
|
body: blockBody,
|
2018-12-28 16:51:40 +00:00
|
|
|
signature: ValidatorSig(), # we need the rest of the block first!
|
2019-03-11 15:33:24 +00:00
|
|
|
)
|
2018-12-28 16:51:40 +00:00
|
|
|
|
2019-02-19 23:35:02 +00:00
|
|
|
let ok =
|
2019-03-13 22:59:20 +00:00
|
|
|
updateState(
|
|
|
|
node.state.data, node.state.blck.root, newBlock, {skipValidation})
|
2018-12-28 16:51:40 +00:00
|
|
|
doAssert ok # TODO: err, could this fail somehow?
|
2019-03-13 22:59:20 +00:00
|
|
|
node.state.root = hash_tree_root_final(node.state.data)
|
2018-11-26 13:33:06 +00:00
|
|
|
|
2019-03-13 22:59:20 +00:00
|
|
|
newBlock.state_root = node.state.root
|
2018-11-26 13:33:06 +00:00
|
|
|
|
2019-03-11 15:33:24 +00:00
|
|
|
let proposal = Proposal(
|
2019-03-12 22:21:32 +00:00
|
|
|
slot: slot.uint64,
|
2019-03-18 15:42:42 +00:00
|
|
|
block_root: Eth2Digest(data: signed_root(newBlock)),
|
2019-03-11 15:33:24 +00:00
|
|
|
signature: ValidatorSig(),
|
|
|
|
)
|
2019-03-13 22:59:20 +00:00
|
|
|
newBlock.signature =
|
|
|
|
await validator.signBlockProposal(node.state.data.fork, proposal)
|
2018-11-26 13:33:06 +00:00
|
|
|
|
2019-02-28 21:21:29 +00:00
|
|
|
# TODO what are we waiting for here? broadcast should never block, and never
|
|
|
|
# fail...
|
2018-12-28 16:51:40 +00:00
|
|
|
await node.network.broadcast(topicBeaconBlocks, newBlock)
|
2018-11-26 13:33:06 +00:00
|
|
|
|
2019-02-19 23:35:02 +00:00
|
|
|
info "Block proposed",
|
2019-03-20 20:01:48 +00:00
|
|
|
blck = shortLog(newBlock),
|
|
|
|
blockRoot = shortLog(proposal.block_root),
|
2019-02-19 23:35:02 +00:00
|
|
|
validator = shortValidatorKey(node, validator.idx),
|
|
|
|
idx = validator.idx
|
2018-11-26 13:33:06 +00:00
|
|
|
|
2018-12-28 16:51:40 +00:00
|
|
|
proc scheduleBlockProposal(node: BeaconNode,
|
2019-02-20 01:33:58 +00:00
|
|
|
slot: Slot,
|
2018-12-28 16:51:40 +00:00
|
|
|
validator: AttachedValidator) =
|
|
|
|
# TODO:
|
|
|
|
# This function exists only to hide a bug with Nim's closures.
|
2019-01-09 01:01:07 +00:00
|
|
|
# If you inline it in `scheduleEpochActions`, you'll see the
|
2018-12-28 16:51:40 +00:00
|
|
|
# internal `doAssert` starting to fail.
|
|
|
|
doAssert validator != nil
|
|
|
|
|
2019-02-19 23:35:02 +00:00
|
|
|
let
|
2019-02-28 21:21:29 +00:00
|
|
|
at = node.slotStart(slot)
|
2019-02-19 23:35:02 +00:00
|
|
|
now = fastEpochTime()
|
|
|
|
|
|
|
|
if now > at:
|
|
|
|
warn "Falling behind on block proposals", at, now, slot
|
2019-01-25 17:35:22 +00:00
|
|
|
|
|
|
|
info "Scheduling block proposal",
|
|
|
|
validator = shortValidatorKey(node, validator.idx),
|
2019-02-12 15:56:58 +00:00
|
|
|
idx = validator.idx,
|
2019-02-07 21:14:08 +00:00
|
|
|
slot = humaneSlotNum(slot),
|
2019-02-19 23:35:02 +00:00
|
|
|
fromNow = (at - now) div 1000
|
2019-01-25 17:35:22 +00:00
|
|
|
|
2019-02-19 23:35:02 +00:00
|
|
|
addTimer(at) do (x: pointer) {.gcsafe.}:
|
2019-01-25 17:35:22 +00:00
|
|
|
# TODO timers are generally not accurate / guaranteed to fire at the right
|
|
|
|
# time - need to guard here against early / late firings
|
2018-12-28 16:51:40 +00:00
|
|
|
doAssert validator != nil
|
2019-02-18 18:54:05 +00:00
|
|
|
asyncCheck proposeBlock(node, validator, slot)
|
2018-12-28 16:51:40 +00:00
|
|
|
|
|
|
|
proc scheduleAttestation(node: BeaconNode,
|
|
|
|
validator: AttachedValidator,
|
2019-02-20 01:33:58 +00:00
|
|
|
slot: Slot,
|
2018-12-28 16:51:40 +00:00
|
|
|
shard: uint64,
|
|
|
|
committeeLen: int,
|
|
|
|
indexInCommittee: int) =
|
|
|
|
# TODO:
|
|
|
|
# This function exists only to hide a bug with Nim's closures.
|
2019-01-09 01:01:07 +00:00
|
|
|
# If you inline it in `scheduleEpochActions`, you'll see the
|
2018-12-28 16:51:40 +00:00
|
|
|
# internal `doAssert` starting to fail.
|
|
|
|
doAssert validator != nil
|
|
|
|
|
2019-02-19 23:35:02 +00:00
|
|
|
let
|
2019-02-28 21:21:29 +00:00
|
|
|
at = node.slotStart(slot)
|
2019-02-19 23:35:02 +00:00
|
|
|
now = fastEpochTime()
|
|
|
|
|
|
|
|
if now > at:
|
|
|
|
warn "Falling behind on attestations", at, now, slot
|
|
|
|
|
|
|
|
debug "Scheduling attestation",
|
|
|
|
validator = shortValidatorKey(node, validator.idx),
|
|
|
|
fromNow = (at - now) div 1000,
|
|
|
|
slot = humaneSlotNum(slot),
|
|
|
|
shard
|
|
|
|
|
|
|
|
addTimer(at) do (p: pointer) {.gcsafe.}:
|
2018-12-28 16:51:40 +00:00
|
|
|
doAssert validator != nil
|
2019-02-18 18:54:05 +00:00
|
|
|
asyncCheck makeAttestation(node, validator, slot,
|
2018-12-28 16:51:40 +00:00
|
|
|
shard, committeeLen, indexInCommittee)
|
2018-11-26 13:33:06 +00:00
|
|
|
|
2019-02-20 01:33:58 +00:00
|
|
|
proc scheduleEpochActions(node: BeaconNode, epoch: Epoch) =
|
2018-11-23 23:58:49 +00:00
|
|
|
## This schedules the required block proposals and
|
|
|
|
## attestations from our attached validators.
|
2018-12-28 16:51:40 +00:00
|
|
|
doAssert node != nil
|
2019-02-19 23:35:02 +00:00
|
|
|
doAssert epoch >= GENESIS_EPOCH,
|
2019-02-28 21:21:29 +00:00
|
|
|
"Epoch: " & $epoch & ", humane epoch: " & $humaneEpochNum(epoch)
|
|
|
|
|
2019-03-14 13:33:56 +00:00
|
|
|
# In case some late blocks dropped in..
|
2019-03-13 22:59:20 +00:00
|
|
|
let head = node.updateHead()
|
2019-03-08 16:40:17 +00:00
|
|
|
|
2019-02-28 21:21:29 +00:00
|
|
|
# Sanity check - verify that the current head block is not too far behind
|
2019-03-14 13:33:56 +00:00
|
|
|
# TODO what if the head block is too far ahead? that would be.. weird.
|
|
|
|
if head.slot.slot_to_epoch() + 1 < epoch:
|
2019-03-08 16:40:17 +00:00
|
|
|
# We're hopelessly behind!
|
|
|
|
#
|
|
|
|
# There's a few ways this can happen:
|
|
|
|
#
|
|
|
|
# * we receive no attestations or blocks for an extended period of time
|
|
|
|
# * all the attestations we receive are bogus - maybe we're connected to
|
|
|
|
# the wrong network?
|
|
|
|
# * we just started and still haven't synced
|
|
|
|
#
|
|
|
|
# TODO make an effort to find other nodes and sync? A worst case scenario
|
|
|
|
# here is that the network stalls because nobody is sending out
|
|
|
|
# attestations because nobody is scheduling them, in a vicious
|
|
|
|
# circle
|
|
|
|
# TODO diagnose the various scenarios and do something smart...
|
|
|
|
|
|
|
|
let
|
|
|
|
expectedSlot = node.state.data.getSlotFromTime()
|
|
|
|
nextSlot = expectedSlot + 1
|
|
|
|
at = node.slotStart(nextSlot)
|
|
|
|
|
|
|
|
notice "Delaying epoch scheduling, head too old - scheduling new attempt",
|
2019-03-14 13:33:56 +00:00
|
|
|
headSlot = humaneSlotNum(head.slot),
|
2019-03-08 16:40:17 +00:00
|
|
|
expectedEpoch = humaneEpochNum(epoch),
|
|
|
|
expectedSlot = humaneSlotNum(expectedSlot),
|
|
|
|
fromNow = (at - fastEpochTime()) div 1000
|
|
|
|
|
|
|
|
addTimer(at) do (p: pointer):
|
|
|
|
node.scheduleEpochActions(nextSlot.slot_to_epoch())
|
|
|
|
return
|
2019-02-28 21:21:29 +00:00
|
|
|
|
2019-03-14 13:33:56 +00:00
|
|
|
|
|
|
|
updateState(node.blockPool, node.state, head, epoch.get_epoch_start_slot())
|
|
|
|
|
2019-02-28 21:21:29 +00:00
|
|
|
# TODO: is this necessary with the new shuffling?
|
|
|
|
# see get_beacon_proposer_index
|
|
|
|
var nextState = node.state.data
|
|
|
|
|
|
|
|
# TODO we don't need to do anything at slot 0 - what about slots we missed
|
|
|
|
# if we got delayed above?
|
2019-02-12 15:56:58 +00:00
|
|
|
let start = if epoch == GENESIS_EPOCH: 1.uint64 else: 0.uint64
|
|
|
|
|
2019-02-20 01:33:58 +00:00
|
|
|
for i in start ..< SLOTS_PER_EPOCH:
|
2019-03-12 22:21:32 +00:00
|
|
|
let slot = (epoch * SLOTS_PER_EPOCH + i).Slot
|
2019-02-19 23:35:02 +00:00
|
|
|
nextState.slot = slot # ugly trick, see get_beacon_proposer_index
|
|
|
|
|
|
|
|
block: # Schedule block proposals
|
|
|
|
let proposerIdx = get_beacon_proposer_index(nextState, slot)
|
|
|
|
let validator = node.getAttachedValidator(proposerIdx)
|
|
|
|
|
|
|
|
if validator != nil:
|
|
|
|
# TODO:
|
|
|
|
# Warm-up the proposer earlier to try to obtain previous
|
|
|
|
# missing blocks if necessary
|
|
|
|
scheduleBlockProposal(node, slot, validator)
|
|
|
|
|
|
|
|
block: # Schedule attestations
|
|
|
|
for crosslink_committee in get_crosslink_committees_at_slot(
|
|
|
|
nextState, slot):
|
|
|
|
for i, validatorIdx in crosslink_committee.committee:
|
|
|
|
let validator = node.getAttachedValidator(validatorIdx)
|
|
|
|
if validator != nil:
|
|
|
|
scheduleAttestation(
|
|
|
|
node, validator, slot, crosslink_committee.shard,
|
|
|
|
crosslink_committee.committee.len, i)
|
2018-12-28 16:51:40 +00:00
|
|
|
|
2019-01-25 17:35:22 +00:00
|
|
|
let
|
2019-03-14 13:33:56 +00:00
|
|
|
# TODO we need to readjust here for wall clock time, in case computer
|
|
|
|
# goes to sleep for example, so that we don't walk epochs one by one
|
|
|
|
# to catch up.. we should also check the current head most likely to
|
|
|
|
# see if we're suspiciously off, in terms of wall clock vs head time.
|
2019-02-18 15:58:34 +00:00
|
|
|
nextEpoch = epoch + 1
|
2019-02-28 21:21:29 +00:00
|
|
|
at = node.slotStart(nextEpoch.get_epoch_start_slot())
|
2018-11-23 23:58:49 +00:00
|
|
|
|
2019-01-25 17:35:22 +00:00
|
|
|
info "Scheduling next epoch update",
|
|
|
|
fromNow = (at - fastEpochTime()) div 1000,
|
2019-02-18 15:58:34 +00:00
|
|
|
epoch = humaneEpochNum(nextEpoch)
|
2019-01-25 17:35:22 +00:00
|
|
|
|
|
|
|
addTimer(at) do (p: pointer):
|
2019-02-21 04:42:17 +00:00
|
|
|
node.scheduleEpochActions(nextEpoch)
|
2018-11-23 23:58:49 +00:00
|
|
|
|
2019-02-28 21:21:29 +00:00
|
|
|
proc fetchBlocks(node: BeaconNode, roots: seq[Eth2Digest]) =
|
|
|
|
if roots.len == 0: return
|
|
|
|
|
|
|
|
# TODO shouldn't send to all!
|
|
|
|
# TODO should never fail - asyncCheck is wrong here..
|
|
|
|
asyncCheck node.network.broadcast(topicfetchBlocks, roots)
|
|
|
|
|
|
|
|
proc onFetchBlocks(node: BeaconNode, roots: seq[Eth2Digest]) =
|
|
|
|
# TODO placeholder logic for block recovery
|
|
|
|
debug "fetchBlocks received",
|
|
|
|
roots = roots.len
|
|
|
|
for root in roots:
|
|
|
|
if (let blck = node.db.getBlock(root); blck.isSome()):
|
|
|
|
# TODO should never fail - asyncCheck is wrong here..
|
|
|
|
# TODO should obviously not spam, but rather send it back to the requester
|
|
|
|
asyncCheck node.network.broadcast(topicBeaconBlocks, blck.get())
|
|
|
|
|
|
|
|
proc scheduleSlotStartActions(node: BeaconNode, slot: Slot) =
|
|
|
|
# TODO in this setup, we retry fetching blocks at the beginning of every slot,
|
|
|
|
# hoping that we'll get some before it's time to attest or propose - is
|
|
|
|
# there a better time to do this?
|
|
|
|
let missingBlocks = node.blockPool.checkUnresolved()
|
|
|
|
node.fetchBlocks(missingBlocks)
|
2019-01-31 16:06:48 +00:00
|
|
|
|
2019-02-28 21:21:29 +00:00
|
|
|
let
|
|
|
|
nextSlot = slot + 1
|
|
|
|
at = node.slotStart(nextSlot)
|
|
|
|
|
|
|
|
info "Scheduling next slot start action block",
|
|
|
|
fromNow = (at - fastEpochTime()) div 1000,
|
|
|
|
slot = humaneSlotNum(nextSlot)
|
|
|
|
|
|
|
|
addTimer(at) do (p: pointer):
|
|
|
|
node.scheduleSlotStartActions(nextSlot)
|
2019-02-21 04:42:17 +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
|
|
|
|
debug "Attestation received",
|
2019-03-20 20:01:48 +00:00
|
|
|
attestationData = shortLog(attestation.data),
|
2019-02-28 21:21:29 +00:00
|
|
|
signature = shortLog(attestation.aggregate_signature)
|
2019-02-21 04:42:17 +00:00
|
|
|
|
2019-02-28 21:21:29 +00:00
|
|
|
node.attestationPool.add(node.state.data, attestation)
|
2019-02-21 04:42:17 +00:00
|
|
|
|
|
|
|
proc onBeaconBlock(node: BeaconNode, blck: BeaconBlock) =
|
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.
|
|
|
|
let blockRoot = hash_tree_root_final(blck)
|
|
|
|
debug "Block received",
|
2019-03-20 20:01:48 +00:00
|
|
|
blck = shortLog(blck),
|
|
|
|
blockRoot = shortLog(blockRoot)
|
2019-02-28 21:21:29 +00:00
|
|
|
|
2019-03-13 22:59:20 +00:00
|
|
|
if not node.blockPool.add(node.state, blockRoot, blck):
|
2019-02-28 21:21:29 +00:00
|
|
|
# TODO the fact that add returns a bool that causes the parent block to be
|
|
|
|
# pre-emptively fetched is quite ugly - fix.
|
2019-03-16 19:52:37 +00:00
|
|
|
node.fetchBlocks(@[blck.previous_block_root])
|
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-02-21 04:42:17 +00:00
|
|
|
for attestation in blck.body.attestations:
|
|
|
|
# TODO attestation pool needs to be taught to deal with overlapping
|
|
|
|
# attestations!
|
|
|
|
discard # node.onAttestation(attestation)
|
|
|
|
|
|
|
|
proc run*(node: BeaconNode) =
|
2019-03-05 22:54:08 +00:00
|
|
|
waitFor node.network.subscribe(topicBeaconBlocks) do (blck: BeaconBlock):
|
2019-02-21 04:42:17 +00:00
|
|
|
node.onBeaconBlock(blck)
|
|
|
|
|
2019-03-05 22:54:08 +00:00
|
|
|
waitFor node.network.subscribe(topicAttestations) do (attestation: Attestation):
|
2019-02-21 04:42:17 +00:00
|
|
|
node.onAttestation(attestation)
|
2018-12-28 16:51:40 +00:00
|
|
|
|
2019-03-05 22:54:08 +00:00
|
|
|
waitFor node.network.subscribe(topicfetchBlocks) do (roots: seq[Eth2Digest]):
|
2019-02-28 21:21:29 +00:00
|
|
|
node.onFetchBlocks(roots)
|
|
|
|
|
|
|
|
let nowSlot = node.state.data.getSlotFromTime()
|
|
|
|
|
|
|
|
node.scheduleEpochActions(nowSlot.slot_to_epoch())
|
|
|
|
node.scheduleSlotStartActions(nowSlot)
|
2018-12-28 16:51:40 +00:00
|
|
|
|
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
|
|
|
|
writeFile filename, $getCurrentProcessId()
|
|
|
|
gPidFile = filename
|
|
|
|
addQuitProc proc {.noconv.} = removeFile gPidFile
|
|
|
|
|
2018-11-23 23:58:49 +00:00
|
|
|
when isMainModule:
|
2019-03-18 03:54:08 +00:00
|
|
|
let config = BeaconNodeConf.load(version = fullVersionStr())
|
|
|
|
|
2019-01-21 19:42:37 +00:00
|
|
|
if config.logLevel != LogLevel.NONE:
|
|
|
|
setLogLevel(config.logLevel)
|
2019-01-16 23:01:15 +00:00
|
|
|
|
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]
|
|
|
|
for i in config.firstValidator.int ..< config.numValidators.int:
|
|
|
|
let depositFile = config.validatorsDir /
|
|
|
|
validatorFileBaseName(i) & ".deposit.json"
|
|
|
|
deposits.add Json.loadFile(depositFile, Deposit)
|
|
|
|
|
|
|
|
let initialState = get_genesis_beacon_state(
|
|
|
|
deposits,
|
|
|
|
uint64(int(fastEpochTime() div 1000) + config.genesisOffset),
|
|
|
|
Eth1Data(), {})
|
|
|
|
|
|
|
|
Json.saveFile(config.outputGenesis.string, initialState, pretty = true)
|
|
|
|
echo "Wrote ", config.outputGenesis.string
|
|
|
|
|
|
|
|
var
|
|
|
|
bootstrapAddress = getPersistenBootstrapAddr(
|
|
|
|
config, parseIpAddress(config.bootstrapAddress), Port config.bootstrapPort)
|
|
|
|
|
|
|
|
testnetMetadata = NetworkMetadata(
|
|
|
|
networkId: config.networkId,
|
|
|
|
genesisRoot: hash_tree_root_final(initialState),
|
|
|
|
bootstrapNodes: @[bootstrapAddress],
|
|
|
|
numShards: SHARD_COUNT,
|
|
|
|
slotDuration: SECONDS_PER_SLOT,
|
|
|
|
slotsPerEpoch: SLOTS_PER_EPOCH,
|
|
|
|
totalValidators: config.numValidators,
|
|
|
|
firstUserValidator: config.firstUserValidator)
|
|
|
|
|
|
|
|
Json.saveFile(config.outputNetwork.string, testnetMetadata, pretty = true)
|
|
|
|
echo "Wrote ", config.outputNetwork.string
|
|
|
|
|
|
|
|
of updateTestnet:
|
|
|
|
discard waitFor updateTestnetMetadata(config)
|
2018-12-19 12:58:53 +00:00
|
|
|
|
2019-03-19 19:50:22 +00:00
|
|
|
of importValidators:
|
2019-03-18 03:54:08 +00:00
|
|
|
template reportFailureFor(keyExpr) =
|
|
|
|
error "Failed to import validator key", key = keyExpr
|
|
|
|
programResult = 1
|
|
|
|
|
2019-03-19 19:50:22 +00:00
|
|
|
for key in config.keys:
|
2019-03-18 03:54:08 +00:00
|
|
|
try:
|
2019-03-19 19:50:22 +00:00
|
|
|
ValidatorPrivKey.init(key).saveValidatorKey(config)
|
2019-03-18 03:54:08 +00:00
|
|
|
except:
|
2019-03-19 19:50:22 +00:00
|
|
|
reportFailureFor key
|
2019-03-18 03:54:08 +00:00
|
|
|
|
2019-03-19 19:50:22 +00:00
|
|
|
for keyFile in config.keyFiles:
|
2019-03-18 03:54:08 +00:00
|
|
|
try:
|
2019-03-19 19:50:22 +00:00
|
|
|
keyFile.load.saveValidatorKey(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
|
|
|
|
2019-03-19 19:50:22 +00:00
|
|
|
if (config.keys.len + config.keyFiles.len) == 0:
|
2019-03-19 17:22:17 +00:00
|
|
|
if config.network in ["testnet0", "testnet1"]:
|
2019-03-18 03:54:08 +00:00
|
|
|
try:
|
2019-03-19 17:22:17 +00:00
|
|
|
let key = waitFor obtainTestnetKey(config)
|
|
|
|
saveValidatorKey(key, config)
|
|
|
|
info "Imported validator", pubkey = key.pubKey
|
2019-03-18 03:54:08 +00:00
|
|
|
except:
|
|
|
|
error "Failed to download key", err = getCurrentExceptionMsg()
|
|
|
|
quit 1
|
|
|
|
else:
|
|
|
|
echo "Validator keys can be downloaded only for testnets"
|
|
|
|
quit 1
|
|
|
|
|
2018-12-19 12:58:53 +00:00
|
|
|
of noCommand:
|
2019-01-25 17:35:22 +00:00
|
|
|
waitFor synchronizeClock()
|
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)
|
2018-12-19 12:58:53 +00:00
|
|
|
|
2019-01-25 17:35:22 +00:00
|
|
|
dynamicLogScope(node = node.config.tcpPort - 50000):
|
2019-02-21 04:42:17 +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.
|
2019-01-25 17:35:22 +00:00
|
|
|
waitFor node.connectToNetwork()
|
|
|
|
|
|
|
|
if not waitFor node.sync():
|
|
|
|
quit 1
|
2018-12-19 12:58:53 +00:00
|
|
|
|
2019-01-25 17:35:22 +00:00
|
|
|
info "Starting beacon node",
|
2019-02-28 21:21:29 +00:00
|
|
|
slotsSinceFinalization = node.state.data.slotDistanceFromNow(),
|
|
|
|
stateSlot = humaneSlotNum(node.state.data.slot),
|
2019-02-14 19:32:33 +00:00
|
|
|
SHARD_COUNT,
|
2019-02-20 01:33:58 +00:00
|
|
|
SLOTS_PER_EPOCH,
|
2019-02-19 23:07:56 +00:00
|
|
|
SECONDS_PER_SLOT,
|
2019-02-14 19:32:33 +00:00
|
|
|
SPEC_VERSION
|
2018-11-23 23:58:49 +00:00
|
|
|
|
2019-01-25 17:35:22 +00:00
|
|
|
node.addLocalValidators()
|
2019-02-21 04:42:17 +00:00
|
|
|
node.run()
|