Skip to content

Commit

Permalink
withState: state -> forkyState (#4038)
Browse files Browse the repository at this point in the history
  • Loading branch information
tersec authored Aug 26, 2022
1 parent 4e90e9f commit b60456f
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 28 deletions.
2 changes: 1 addition & 1 deletion beacon_chain/consensus_object_pools/block_clearance.nim
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ proc advanceClearanceState*(dag: ChainDAGRef) =
# Notably, we use the clearance state here because that's where the block will
# first be seen - later, this state will be copied to the head state!
let advanced = withState(dag.clearanceState):
state.data.slot > state.data.latest_block_header.slot
forkyState.data.slot > forkyState.data.latest_block_header.slot
if not advanced:
let next = getStateField(dag.clearanceState, slot) + 1

Expand Down
38 changes: 21 additions & 17 deletions beacon_chain/consensus_object_pools/blockchain_dag.nim
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ template is_merge_transition_complete(
stateParam: ForkedHashedBeaconState): bool =
withState(stateParam):
when stateFork >= BeaconStateFork.Bellatrix:
is_merge_transition_complete(state.data)
is_merge_transition_complete(forkyState.data)
else:
false

Expand Down Expand Up @@ -366,7 +366,7 @@ func init*(
dependent_epoch =
if epoch < 1: Epoch(0) else: epoch - 1
attester_dependent_root =
withState(state): state.dependent_root(dependent_epoch)
withState(state): forkyState.dependent_root(dependent_epoch)

ShufflingRef(
epoch: epoch,
Expand All @@ -380,13 +380,15 @@ func init*(
cache: var StateCache): T =
let
epoch = state.get_current_epoch()
proposer_dependent_root = withState(state): state.proposer_dependent_root
proposer_dependent_root = withState(state):
forkyState.proposer_dependent_root
shufflingRef = dag.findShufflingRef(state.latest_block_id, epoch).valueOr:
let tmp = ShufflingRef.init(state, cache, epoch)
dag.putShufflingRef(tmp)
tmp

attester_dependent_root = withState(state): state.attester_dependent_root
attester_dependent_root = withState(state):
forkyState.attester_dependent_root
epochRef = EpochRef(
key: dag.epochAncestor(state.latest_block_id, epoch),

Expand Down Expand Up @@ -703,7 +705,7 @@ proc putState(dag: ChainDAGRef, state: ForkedHashedBeaconState, bid: BlockId) =
# transaction to prevent database inconsistencies, but the state loading code
# is resilient against one or the other going missing
withState(state):
dag.db.putState(state)
dag.db.putState(forkyState)

debug "Stored state", putStateDur = Moment.now() - startTick

Expand Down Expand Up @@ -733,9 +735,10 @@ proc advanceSlots*(
# in the monitor. This may be inaccurate during a deep reorg (>1 epoch)
# which is an acceptable tradeoff for monitoring.
withState(state):
let postEpoch = state.data.slot.epoch
let postEpoch = forkyState.data.slot.epoch
if preEpoch != postEpoch:
dag.validatorMonitor[].registerEpochInfo(postEpoch, info, state.data)
dag.validatorMonitor[].registerEpochInfo(
postEpoch, info, forkyState.data)

proc applyBlock(
dag: ChainDAGRef, state: var ForkedHashedBeaconState, bid: BlockId,
Expand Down Expand Up @@ -1233,7 +1236,7 @@ proc updateState*(
let
startTick = Moment.now()
current {.used.} = withState(state):
BlockSlotId.init(state.latest_block_id, state.data.slot)
BlockSlotId.init(forkyState.latest_block_id, forkyState.data.slot)

var
ancestors: seq[BlockId]
Expand Down Expand Up @@ -1361,7 +1364,7 @@ proc updateState*(
let
assignTick = Moment.now()
ancestor {.used.} = withState(state):
BlockSlotId.init(state.latest_block_id, state.data.slot)
BlockSlotId.init(forkyState.latest_block_id, forkyState.data.slot)
ancestorRoot {.used.} = getStateRoot(state)

var info: ForkedEpochInfo
Expand Down Expand Up @@ -1576,7 +1579,7 @@ func syncCommitteeParticipants*(dag: ChainDAGRef,
when stateFork >= BeaconStateFork.Altair:
let
period = sync_committee_period(slot)
curPeriod = sync_committee_period(state.data.slot)
curPeriod = sync_committee_period(forkyState.data.slot)

if period == curPeriod:
@(dag.headSyncCommittees.current_sync_committee)
Expand Down Expand Up @@ -1606,7 +1609,7 @@ func getSubcommitteePositions*(
when stateFork >= BeaconStateFork.Altair:
let
period = sync_committee_period(slot)
curPeriod = sync_committee_period(state.data.slot)
curPeriod = sync_committee_period(forkyState.data.slot)

template search(syncCommittee: openArray[ValidatorIndex]): seq[uint64] =
dag.getSubcommitteePositionsAux(
Expand Down Expand Up @@ -1769,7 +1772,7 @@ proc updateHead*(

withState(dag.headState):
when stateFork >= BeaconStateFork.Altair:
dag.headSyncCommittees = state.data.get_sync_committee_cache(cache)
dag.headSyncCommittees = forkyState.data.get_sync_committee_cache(cache)

let
finalized_checkpoint =
Expand Down Expand Up @@ -1822,8 +1825,9 @@ proc updateHead*(
if not(isNil(dag.onHeadChanged)):
let
currentEpoch = epoch(newHead.slot)
depRoot = withState(dag.headState): state.proposer_dependent_root
prevDepRoot = withState(dag.headState): state.attester_dependent_root
depRoot = withState(dag.headState): forkyState.proposer_dependent_root
prevDepRoot = withState(dag.headState):
forkyState.attester_dependent_root
epochTransition = (finalizedHead != dag.finalizedHead)
# TODO (cheatfate): Proper implementation required
data = HeadChangeInfoObject.init(dag.head.slot, dag.head.root,
Expand All @@ -1837,7 +1841,7 @@ proc updateHead*(
# state-related metrics change - notify the validator monitor.
# Doing this update during head update ensures there's a reasonable number
# of such updates happening - at most once per valid block.
dag.validatorMonitor[].registerState(state.data)
dag.validatorMonitor[].registerState(forkyState.data)

if finalizedHead != dag.finalizedHead:
debug "Reached new finalization checkpoint",
Expand Down Expand Up @@ -2180,10 +2184,10 @@ proc rebuildIndex*(dag: ChainDAGRef) =
dag.updateFlags).expect("process_slots shouldn't fail when state slot is correct")

withState(state[]):
dag.db.putState(state)
dag.db.putState(forkyState)
dag.db.checkpoint()

state_root = state.root
state_root = forkyState.root

# Now that we have states all the way to genesis, we can adjust the tail
# and readjust the in-memory indices to what they would look like if we had
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ proc syncCommitteeRootForPeriod(
dag.withUpdatedExistingState(tmpState, bsi) do:
withState(state):
when stateFork >= BeaconStateFork.Altair:
ok state.syncCommitteeRoot
ok forkyState.syncCommitteeRoot
else: raiseAssert "Unreachable"
do: err()

Expand Down Expand Up @@ -208,7 +208,7 @@ proc initLightClientBootstrapForPeriod(
continue
let branch = withState(tmpState[]):
when stateFork >= BeaconStateFork.Altair:
state.data.build_proof(altair.CURRENT_SYNC_COMMITTEE_INDEX).get
forkyState.data.build_proof(altair.CURRENT_SYNC_COMMITTEE_INDEX).get
else: raiseAssert "Unreachable"
dag.lcDataStore.db.putCurrentSyncCommitteeBranch(bid.slot, branch)
res
Expand Down Expand Up @@ -322,7 +322,7 @@ proc initLightClientUpdateForPeriod(
dag.withUpdatedExistingState(tmpState[], attestedBid.atSlot) do:
withState(state):
when stateFork >= BeaconStateFork.Altair:
state.data.finalized_checkpoint.epoch
forkyState.data.finalized_checkpoint.epoch
else: raiseAssert "Unreachable"
do:
dag.handleUnexpectedLightClientError(attestedBid.slot)
Expand Down Expand Up @@ -615,7 +615,7 @@ proc initLightClientDataCache*(dag: ChainDAGRef) =
return
withState(dag.headState):
when stateFork >= BeaconStateFork.Altair:
dag.cacheLightClientData(state, dag.head.bid)
dag.cacheLightClientData(forkyState, dag.head.bid)
else: raiseAssert "Unreachable" # `tailSlot` cannot be before Altair
if dag.lcDataStore.importMode == LightClientDataImportMode.OnlyNew:
return
Expand Down Expand Up @@ -748,7 +748,7 @@ proc processHeadChangeForLightClient*(dag: ChainDAGRef) =
period, dag.lcDataStore.cache.pendingBest.getOrDefault(key))
withState(dag.headState): # Common case separate to avoid `tmpState` copy
when stateFork >= BeaconStateFork.Altair:
let key = (headPeriod, state.syncCommitteeRoot)
let key = (headPeriod, forkyState.syncCommitteeRoot)
dag.lcDataStore.db.putBestUpdate(
headPeriod, dag.lcDataStore.cache.pendingBest.getOrDefault(key))
else: raiseAssert "Unreachable" # `tailSlot` cannot be before Altair
Expand Down Expand Up @@ -845,7 +845,8 @@ proc getLightClientBootstrap*(
dag.withUpdatedExistingState(tmpState[], bsi) do:
branch = withState(state):
when stateFork >= BeaconStateFork.Altair:
state.data.build_proof(altair.CURRENT_SYNC_COMMITTEE_INDEX).get
forkyState.data.build_proof(
altair.CURRENT_SYNC_COMMITTEE_INDEX).get
else: raiseAssert "Unreachable"
do: return err()
dag.lcDataStore.db.putCurrentSyncCommitteeBranch(slot, branch)
Expand Down
2 changes: 1 addition & 1 deletion beacon_chain/spec/state_transition_block.nim
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ proc check_attester_slashing*(
state: var ForkedHashedBeaconState; attester_slashing: SomeAttesterSlashing;
flags: UpdateFlags): Result[seq[ValidatorIndex], cstring] =
withState(state):
check_attester_slashing(state.data, attester_slashing, flags)
check_attester_slashing(forkyState.data, attester_slashing, flags)

# https://github.com/ethereum/consensus-specs/blob/v1.2.0-rc.3/specs/phase0/beacon-chain.md#attester-slashings
proc process_attester_slashing*(
Expand Down
4 changes: 2 additions & 2 deletions research/simutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ proc loadGenesis*(validators: Natural, validate: bool):
cfg, readAllBytes(genesisFn).tryGet()))

withState(res[]):
if state.data.slot != GENESIS_SLOT:
if forkyState.data.slot != GENESIS_SLOT:
echo "Can only start from genesis state"
quit 1

if state.data.validators.len != validators:
if forkyState.data.validators.len != validators:
echo &"Supplied genesis file has {state.data.validators.len} validators, while {validators} where requested, running anyway"

echo &"Loaded {genesisFn}..."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ proc block_for_next_slot(

let attestations =
if withAttestations:
let block_root = withState(forked): state.latest_block_root
let block_root = withState(forked): forkyState.latest_block_root
makeFullAttestations(forked, block_root, state.slot, cache)
else:
@[]
Expand Down

0 comments on commit b60456f

Please sign in to comment.