add validators even if they're not in startup state

This commit is contained in:
Jacek Sieka 2019-11-25 13:47:29 +01:00 committed by zah
parent 1494bcc262
commit a82869c03d
3 changed files with 30 additions and 17 deletions

View File

@ -275,9 +275,11 @@ proc addLocalValidator(
let idx = state.validators.findIt(it.pubKey == pubKey) let idx = state.validators.findIt(it.pubKey == pubKey)
if idx == -1: if idx == -1:
warn "Validator not in registry", pubKey # We allow adding a validator even if its key is not in the state registry:
else: # it might be that the deposit for this validator has not yet been processed
node.attachedValidators.addLocalValidator(ValidatorIndex(idx), pubKey, privKey) warn "Validator not in registry (yet?)", pubKey
node.attachedValidators.addLocalValidator(pubKey, privKey)
proc addLocalValidators(node: BeaconNode, state: BeaconState) = proc addLocalValidators(node: BeaconNode, state: BeaconState) =
for validatorKeyFile in node.config.validators: for validatorKeyFile in node.config.validators:
@ -812,19 +814,26 @@ proc createPidFile(filename: string) =
gPidFile = filename gPidFile = filename
addQuitProc proc {.noconv.} = removeFile gPidFile addQuitProc proc {.noconv.} = removeFile gPidFile
proc start(node: BeaconNode, headState: BeaconState) = proc start(node: BeaconNode) =
# TODO: while it's nice to cheat by waiting for connections here, we # TODO: while it's nice to cheat by waiting for connections here, we
# actually need to make this part of normal application flow - # actually need to make this part of normal application flow -
# losing all connections might happen at any time and we should be # losing all connections might happen at any time and we should be
# prepared to handle it. # prepared to handle it.
waitFor node.connectToNetwork() waitFor node.connectToNetwork()
let
head = node.blockPool.head
finalizedHead = node.blockPool.finalizedHead
info "Starting beacon node", info "Starting beacon node",
version = fullVersionStr, version = fullVersionStr,
timeSinceFinalization = timeSinceFinalization =
int64(node.blockPool.finalizedHead.slot.toBeaconTime()) - int64(finalizedHead.slot.toBeaconTime()) -
int64(node.beaconClock.now()), int64(node.beaconClock.now()),
stateSlot = shortLog(headState.slot), headSlot = shortLog(head.blck.slot),
headRoot = shortLog(head.blck.root),
finalizedSlot = shortLog(finalizedHead.blck.slot),
finalizedRoot = shortLog(finalizedHead.blck.root),
SLOTS_PER_EPOCH, SLOTS_PER_EPOCH,
SECONDS_PER_SLOT, SECONDS_PER_SLOT,
SPEC_VERSION, SPEC_VERSION,
@ -832,7 +841,12 @@ proc start(node: BeaconNode, headState: BeaconState) =
cat = "init", cat = "init",
pcs = "start_beacon_node" pcs = "start_beacon_node"
node.addLocalValidators(headState) let
bs = BlockSlot(blck: head.blck, slot: head.blck.slot)
node.blockPool.withState(node.stateCache, bs):
node.addLocalValidators(state)
node.run() node.run()
func formatGwei(amount: uint64): string = func formatGwei(amount: uint64): string =
@ -922,8 +936,11 @@ when hasPrompt:
of "attached_validators_balance": of "attached_validators_balance":
var balance = uint64(0) var balance = uint64(0)
for _, validator in node.attachedValidators.validators: # TODO slow linear scan!
balance += node.stateCache.data.data.balances[validator.idx] for idx, b in node.stateCache.data.data.balances:
if node.getAttachedValidator(
node.stateCache.data.data, ValidatorIndex(idx)) != nil:
balance += b
formatGwei(balance) formatGwei(balance)
else: else:
@ -949,7 +966,7 @@ when hasPrompt:
proc statusBarUpdatesPollingLoop() {.async.} = proc statusBarUpdatesPollingLoop() {.async.} =
while true: while true:
update statusBar update statusBar
await sleepAsync(1000) await sleepAsync(chronos.seconds(1))
traceAsyncErrors statusBarUpdatesPollingLoop() traceAsyncErrors statusBarUpdatesPollingLoop()
@ -1053,11 +1070,10 @@ when isMainModule:
var node = waitFor BeaconNode.init(config) var node = waitFor BeaconNode.init(config)
when hasPrompt: initPrompt(node) when hasPrompt: initPrompt(node)
# TODO slightly ugly to rely on node.stateCache state here..
if node.nickname != "": if node.nickname != "":
dynamicLogScope(node = node.nickname): node.start(node.stateCache.data.data) dynamicLogScope(node = node.nickname): node.start()
else: else:
node.start(node.stateCache.data.data) node.start()
of makeDeposits: of makeDeposits:
createDir(config.depositsDir) createDir(config.depositsDir)

View File

@ -191,7 +191,6 @@ type
ValidatorConnection* = object ValidatorConnection* = object
AttachedValidator* = ref object AttachedValidator* = ref object
idx*: ValidatorIndex
pubKey*: ValidatorPubKey pubKey*: ValidatorPubKey
case kind*: ValidatorKind case kind*: ValidatorKind

View File

@ -11,11 +11,9 @@ template count*(pool: ValidatorPool): int =
pool.validators.len pool.validators.len
proc addLocalValidator*(pool: var ValidatorPool, proc addLocalValidator*(pool: var ValidatorPool,
idx: ValidatorIndex,
pubKey: ValidatorPubKey, pubKey: ValidatorPubKey,
privKey: ValidatorPrivKey) = privKey: ValidatorPrivKey) =
let v = AttachedValidator(idx: idx, let v = AttachedValidator(pubKey: pubKey,
pubKey: pubKey,
kind: inProcess, kind: inProcess,
privKey: privKey) privKey: privKey)
pool.validators[pubKey] = v pool.validators[pubKey] = v