abstract over more BeaconState usage (#2496)

This commit is contained in:
tersec 2021-04-14 09:34:35 +00:00 committed by GitHub
parent 3e3137ec14
commit 050e3ac48b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 24 deletions

View File

@ -167,9 +167,9 @@ proc installBeaconApiHandlers*(router: var RestRouter, node: BeaconNode) =
node.withStateForBlockSlot(bslot): node.withStateForBlockSlot(bslot):
return RestApiResponse.jsonResponse( return RestApiResponse.jsonResponse(
( (
previous_version: state().fork.previous_version, previous_version: getStateField(stateData, fork).previous_version,
current_version: state().fork.current_version, current_version: getStateField(stateData, fork).current_version,
epoch: state().fork.epoch epoch: getStateField(stateData, fork).epoch
) )
) )
return RestApiResponse.jsonError(Http500, InternalServerError) return RestApiResponse.jsonError(Http500, InternalServerError)
@ -191,9 +191,11 @@ proc installBeaconApiHandlers*(router: var RestRouter, node: BeaconNode) =
node.withStateForBlockSlot(bslot): node.withStateForBlockSlot(bslot):
return RestApiResponse.jsonResponse( return RestApiResponse.jsonResponse(
( (
previous_justified: state().previous_justified_checkpoint, previous_justified:
current_justified: state().current_justified_checkpoint, getStateField(stateData, previous_justified_checkpoint),
finalized: state().finalized_checkpoint current_justified:
getStateField(stateData, current_justified_checkpoint),
finalized: getStateField(stateData, finalized_checkpoint)
) )
) )
return RestApiResponse.jsonError(Http500, InternalServerError) return RestApiResponse.jsonError(Http500, InternalServerError)
@ -268,7 +270,7 @@ proc installBeaconApiHandlers*(router: var RestRouter, node: BeaconNode) =
node.withStateForBlockSlot(bslot): node.withStateForBlockSlot(bslot):
let current_epoch = get_current_epoch(node.chainDag.headState.data.data) let current_epoch = get_current_epoch(node.chainDag.headState.data.data)
var res: seq[RestValidatorTuple] var res: seq[RestValidatorTuple]
for index, validator in state().validators.pairs(): for index, validator in getStateField(stateData, validators).pairs():
let includeFlag = let includeFlag =
(len(keySet) == 0) and (len(indexSet) == 0) or (len(keySet) == 0) and (len(indexSet) == 0) or
(len(indexSet) > 0 and (ValidatorIndex(index) in indexSet)) or (len(indexSet) > 0 and (ValidatorIndex(index) in indexSet)) or
@ -280,7 +282,8 @@ proc installBeaconApiHandlers*(router: var RestRouter, node: BeaconNode) =
if includeFlag and statusFlag: if includeFlag and statusFlag:
res.add(( res.add((
index: ValidatorIndex(index), index: ValidatorIndex(index),
balance: Base10.toString(state().balances[index]), balance:
Base10.toString(getStateField(stateData, balances)[index]),
status: toString(vstatus), status: toString(vstatus),
validator: validator validator: validator
)) ))
@ -310,14 +313,15 @@ proc installBeaconApiHandlers*(router: var RestRouter, node: BeaconNode) =
let vid = validator_id.get() let vid = validator_id.get()
case vid.kind case vid.kind
of ValidatorQueryKind.Key: of ValidatorQueryKind.Key:
for index, validator in state().validators.pairs(): for index, validator in getStateField(stateData, validators).pairs():
if validator.pubkey == vid.key: if validator.pubkey == vid.key:
let sres = validator.getStatus(current_epoch) let sres = validator.getStatus(current_epoch)
if sres.isOk(): if sres.isOk():
return RestApiResponse.jsonResponse( return RestApiResponse.jsonResponse(
( (
index: ValidatorIndex(index), index: ValidatorIndex(index),
balance: Base10.toString(state().balances[index]), balance:
Base10.toString(getStateField(stateData, balances)[index]),
status: toString(sres.get()), status: toString(sres.get()),
validator: validator validator: validator
) )
@ -340,15 +344,15 @@ proc installBeaconApiHandlers*(router: var RestRouter, node: BeaconNode) =
UnsupportedValidatorIndexValueError) UnsupportedValidatorIndexValueError)
vres.get() vres.get()
if uint64(vindex) >= uint64(len(state().validators)): if uint64(vindex) >= uint64(len(getStateField(stateData, validators))):
return RestApiResponse.jsonError(Http404, ValidatorNotFoundError) return RestApiResponse.jsonError(Http404, ValidatorNotFoundError)
let validator = state().validators[vindex] let validator = getStateField(stateData, validators)[vindex]
let sres = validator.getStatus(current_epoch) let sres = validator.getStatus(current_epoch)
if sres.isOk(): if sres.isOk():
return RestApiResponse.jsonResponse( return RestApiResponse.jsonResponse(
( (
index: vindex, index: vindex,
balance: Base10.toString(state().balances[vindex]), balance: Base10.toString(getStateField(stateData, balances)[vindex]),
status: toString(sres.get()), status: toString(sres.get()),
validator: validator validator: validator
) )
@ -414,7 +418,7 @@ proc installBeaconApiHandlers*(router: var RestRouter, node: BeaconNode) =
node.withStateForBlockSlot(bslot): node.withStateForBlockSlot(bslot):
let current_epoch = get_current_epoch(node.chainDag.headState.data.data) let current_epoch = get_current_epoch(node.chainDag.headState.data.data)
var res: seq[RestValidatorBalanceTuple] var res: seq[RestValidatorBalanceTuple]
for index, validator in state().validators.pairs(): for index, validator in getStateField(stateData, validators).pairs():
let includeFlag = let includeFlag =
(len(keySet) == 0) and (len(indexSet) == 0) or (len(keySet) == 0) and (len(indexSet) == 0) or
(len(indexSet) > 0 and (ValidatorIndex(index) in indexSet)) or (len(indexSet) > 0 and (ValidatorIndex(index) in indexSet)) or
@ -425,7 +429,8 @@ proc installBeaconApiHandlers*(router: var RestRouter, node: BeaconNode) =
if includeFlag: if includeFlag:
res.add(( res.add((
index: ValidatorIndex(index), index: ValidatorIndex(index),
balance: Base10.toString(state().balances[index]), balance:
Base10.toString(getStateField(stateData, balances)[index]),
)) ))
return RestApiResponse.jsonResponse(res) return RestApiResponse.jsonResponse(res)
@ -500,7 +505,7 @@ proc installBeaconApiHandlers*(router: var RestRouter, node: BeaconNode) =
var res: seq[RestBeaconStatesCommitteesTuple] var res: seq[RestBeaconStatesCommitteesTuple]
let qepoch = let qepoch =
if vepoch.isNone: if vepoch.isNone:
compute_epoch_at_slot(state().slot) compute_epoch_at_slot(getStateField(stateData, slot))
else: else:
vepoch.get() vepoch.get()

View File

@ -105,7 +105,7 @@ proc installNimbusApiHandlers*(rpcServer: RpcServer, node: BeaconNode) {.
let proposalState = assignClone(node.chainDag.headState) let proposalState = assignClone(node.chainDag.headState)
node.chainDag.withState(proposalState[], head.atSlot(wallSlot)): node.chainDag.withState(proposalState[], head.atSlot(wallSlot)):
return node.getBlockProposalEth1Data(state) return node.getBlockProposalEth1Data(stateData)
rpcServer.rpc("debug_getChronosFutures") do () -> seq[FutureInfo]: rpcServer.rpc("debug_getChronosFutures") do () -> seq[FutureInfo]:
when defined(chronosFutureTracking): when defined(chronosFutureTracking):

View File

@ -185,7 +185,8 @@ proc installNimbusApiHandlers*(router: var RestRouter, node: BeaconNode) =
res.get() res.get()
let proposalState = assignClone(node.chainDag.headState) let proposalState = assignClone(node.chainDag.headState)
node.chainDag.withState(proposalState[], head.atSlot(wallSlot)): node.chainDag.withState(proposalState[], head.atSlot(wallSlot)):
return RestApiResponse.jsonResponse(node.getBlockProposalEth1Data(state)) return RestApiResponse.jsonResponse(
node.getBlockProposalEth1Data(stateData))
router.api(MethodGet, "/api/nimbus/v1/debug/chronos/futures") do ( router.api(MethodGet, "/api/nimbus/v1/debug/chronos/futures") do (
) -> RestApiResponse: ) -> RestApiResponse:

View File

@ -231,18 +231,20 @@ proc createAndSendAttestation(node: BeaconNode,
beacon_attestation_sent_delay.observe(delayMillis) beacon_attestation_sent_delay.observe(delayMillis)
proc getBlockProposalEth1Data*(node: BeaconNode, proc getBlockProposalEth1Data*(node: BeaconNode,
state: BeaconState): BlockProposalEth1Data = stateData: StateData): BlockProposalEth1Data =
if node.eth1Monitor.isNil: if node.eth1Monitor.isNil:
var pendingDepositsCount = state.eth1_data.deposit_count - var pendingDepositsCount =
state.eth1_deposit_index getStateField(stateData, eth1_data).deposit_count -
getStateField(stateData, eth1_deposit_index)
if pendingDepositsCount > 0: if pendingDepositsCount > 0:
result.hasMissingDeposits = true result.hasMissingDeposits = true
else: else:
result.vote = state.eth1_data result.vote = getStateField(stateData, eth1_data)
else: else:
let finalizedEpochRef = node.chainDag.getFinalizedEpochRef() let finalizedEpochRef = node.chainDag.getFinalizedEpochRef()
result = node.eth1Monitor.getBlockProposalData( result = node.eth1Monitor.getBlockProposalData(
state, finalizedEpochRef.eth1_data, finalizedEpochRef.eth1_deposit_index) stateData.data.data, finalizedEpochRef.eth1_data,
finalizedEpochRef.eth1_deposit_index)
proc makeBeaconBlockForHeadAndSlot*(node: BeaconNode, proc makeBeaconBlockForHeadAndSlot*(node: BeaconNode,
randao_reveal: ValidatorSig, randao_reveal: ValidatorSig,
@ -258,7 +260,7 @@ proc makeBeaconBlockForHeadAndSlot*(node: BeaconNode,
node.chainDag.withState(proposalState[], head.atSlot(slot)): node.chainDag.withState(proposalState[], head.atSlot(slot)):
let let
eth1Proposal = node.getBlockProposalEth1Data(state) eth1Proposal = node.getBlockProposalEth1Data(stateData)
poolPtr = unsafeAddr node.chainDag # safe because restore is short-lived poolPtr = unsafeAddr node.chainDag # safe because restore is short-lived
if eth1Proposal.hasMissingDeposits: if eth1Proposal.hasMissingDeposits: