Skip to content

Commit b60456f

Browse files
authored
withState: state -> forkyState (#4038)
1 parent 4e90e9f commit b60456f

File tree

6 files changed

+33
-28
lines changed

6 files changed

+33
-28
lines changed

beacon_chain/consensus_object_pools/block_clearance.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ proc advanceClearanceState*(dag: ChainDAGRef) =
143143
# Notably, we use the clearance state here because that's where the block will
144144
# first be seen - later, this state will be copied to the head state!
145145
let advanced = withState(dag.clearanceState):
146-
state.data.slot > state.data.latest_block_header.slot
146+
forkyState.data.slot > forkyState.data.latest_block_header.slot
147147
if not advanced:
148148
let next = getStateField(dag.clearanceState, slot) + 1
149149

beacon_chain/consensus_object_pools/blockchain_dag.nim

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ template is_merge_transition_complete(
143143
stateParam: ForkedHashedBeaconState): bool =
144144
withState(stateParam):
145145
when stateFork >= BeaconStateFork.Bellatrix:
146-
is_merge_transition_complete(state.data)
146+
is_merge_transition_complete(forkyState.data)
147147
else:
148148
false
149149

@@ -366,7 +366,7 @@ func init*(
366366
dependent_epoch =
367367
if epoch < 1: Epoch(0) else: epoch - 1
368368
attester_dependent_root =
369-
withState(state): state.dependent_root(dependent_epoch)
369+
withState(state): forkyState.dependent_root(dependent_epoch)
370370

371371
ShufflingRef(
372372
epoch: epoch,
@@ -380,13 +380,15 @@ func init*(
380380
cache: var StateCache): T =
381381
let
382382
epoch = state.get_current_epoch()
383-
proposer_dependent_root = withState(state): state.proposer_dependent_root
383+
proposer_dependent_root = withState(state):
384+
forkyState.proposer_dependent_root
384385
shufflingRef = dag.findShufflingRef(state.latest_block_id, epoch).valueOr:
385386
let tmp = ShufflingRef.init(state, cache, epoch)
386387
dag.putShufflingRef(tmp)
387388
tmp
388389

389-
attester_dependent_root = withState(state): state.attester_dependent_root
390+
attester_dependent_root = withState(state):
391+
forkyState.attester_dependent_root
390392
epochRef = EpochRef(
391393
key: dag.epochAncestor(state.latest_block_id, epoch),
392394

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

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

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

740743
proc applyBlock(
741744
dag: ChainDAGRef, state: var ForkedHashedBeaconState, bid: BlockId,
@@ -1233,7 +1236,7 @@ proc updateState*(
12331236
let
12341237
startTick = Moment.now()
12351238
current {.used.} = withState(state):
1236-
BlockSlotId.init(state.latest_block_id, state.data.slot)
1239+
BlockSlotId.init(forkyState.latest_block_id, forkyState.data.slot)
12371240

12381241
var
12391242
ancestors: seq[BlockId]
@@ -1361,7 +1364,7 @@ proc updateState*(
13611364
let
13621365
assignTick = Moment.now()
13631366
ancestor {.used.} = withState(state):
1364-
BlockSlotId.init(state.latest_block_id, state.data.slot)
1367+
BlockSlotId.init(forkyState.latest_block_id, forkyState.data.slot)
13651368
ancestorRoot {.used.} = getStateRoot(state)
13661369

13671370
var info: ForkedEpochInfo
@@ -1576,7 +1579,7 @@ func syncCommitteeParticipants*(dag: ChainDAGRef,
15761579
when stateFork >= BeaconStateFork.Altair:
15771580
let
15781581
period = sync_committee_period(slot)
1579-
curPeriod = sync_committee_period(state.data.slot)
1582+
curPeriod = sync_committee_period(forkyState.data.slot)
15801583

15811584
if period == curPeriod:
15821585
@(dag.headSyncCommittees.current_sync_committee)
@@ -1606,7 +1609,7 @@ func getSubcommitteePositions*(
16061609
when stateFork >= BeaconStateFork.Altair:
16071610
let
16081611
period = sync_committee_period(slot)
1609-
curPeriod = sync_committee_period(state.data.slot)
1612+
curPeriod = sync_committee_period(forkyState.data.slot)
16101613

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

17701773
withState(dag.headState):
17711774
when stateFork >= BeaconStateFork.Altair:
1772-
dag.headSyncCommittees = state.data.get_sync_committee_cache(cache)
1775+
dag.headSyncCommittees = forkyState.data.get_sync_committee_cache(cache)
17731776

17741777
let
17751778
finalized_checkpoint =
@@ -1822,8 +1825,9 @@ proc updateHead*(
18221825
if not(isNil(dag.onHeadChanged)):
18231826
let
18241827
currentEpoch = epoch(newHead.slot)
1825-
depRoot = withState(dag.headState): state.proposer_dependent_root
1826-
prevDepRoot = withState(dag.headState): state.attester_dependent_root
1828+
depRoot = withState(dag.headState): forkyState.proposer_dependent_root
1829+
prevDepRoot = withState(dag.headState):
1830+
forkyState.attester_dependent_root
18271831
epochTransition = (finalizedHead != dag.finalizedHead)
18281832
# TODO (cheatfate): Proper implementation required
18291833
data = HeadChangeInfoObject.init(dag.head.slot, dag.head.root,
@@ -1837,7 +1841,7 @@ proc updateHead*(
18371841
# state-related metrics change - notify the validator monitor.
18381842
# Doing this update during head update ensures there's a reasonable number
18391843
# of such updates happening - at most once per valid block.
1840-
dag.validatorMonitor[].registerState(state.data)
1844+
dag.validatorMonitor[].registerState(forkyState.data)
18411845

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

21822186
withState(state[]):
2183-
dag.db.putState(state)
2187+
dag.db.putState(forkyState)
21842188
dag.db.checkpoint()
21852189

2186-
state_root = state.root
2190+
state_root = forkyState.root
21872191

21882192
# Now that we have states all the way to genesis, we can adjust the tail
21892193
# and readjust the in-memory indices to what they would look like if we had

beacon_chain/consensus_object_pools/blockchain_dag_light_client.nim

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ proc syncCommitteeRootForPeriod(
119119
dag.withUpdatedExistingState(tmpState, bsi) do:
120120
withState(state):
121121
when stateFork >= BeaconStateFork.Altair:
122-
ok state.syncCommitteeRoot
122+
ok forkyState.syncCommitteeRoot
123123
else: raiseAssert "Unreachable"
124124
do: err()
125125

@@ -208,7 +208,7 @@ proc initLightClientBootstrapForPeriod(
208208
continue
209209
let branch = withState(tmpState[]):
210210
when stateFork >= BeaconStateFork.Altair:
211-
state.data.build_proof(altair.CURRENT_SYNC_COMMITTEE_INDEX).get
211+
forkyState.data.build_proof(altair.CURRENT_SYNC_COMMITTEE_INDEX).get
212212
else: raiseAssert "Unreachable"
213213
dag.lcDataStore.db.putCurrentSyncCommitteeBranch(bid.slot, branch)
214214
res
@@ -322,7 +322,7 @@ proc initLightClientUpdateForPeriod(
322322
dag.withUpdatedExistingState(tmpState[], attestedBid.atSlot) do:
323323
withState(state):
324324
when stateFork >= BeaconStateFork.Altair:
325-
state.data.finalized_checkpoint.epoch
325+
forkyState.data.finalized_checkpoint.epoch
326326
else: raiseAssert "Unreachable"
327327
do:
328328
dag.handleUnexpectedLightClientError(attestedBid.slot)
@@ -615,7 +615,7 @@ proc initLightClientDataCache*(dag: ChainDAGRef) =
615615
return
616616
withState(dag.headState):
617617
when stateFork >= BeaconStateFork.Altair:
618-
dag.cacheLightClientData(state, dag.head.bid)
618+
dag.cacheLightClientData(forkyState, dag.head.bid)
619619
else: raiseAssert "Unreachable" # `tailSlot` cannot be before Altair
620620
if dag.lcDataStore.importMode == LightClientDataImportMode.OnlyNew:
621621
return
@@ -748,7 +748,7 @@ proc processHeadChangeForLightClient*(dag: ChainDAGRef) =
748748
period, dag.lcDataStore.cache.pendingBest.getOrDefault(key))
749749
withState(dag.headState): # Common case separate to avoid `tmpState` copy
750750
when stateFork >= BeaconStateFork.Altair:
751-
let key = (headPeriod, state.syncCommitteeRoot)
751+
let key = (headPeriod, forkyState.syncCommitteeRoot)
752752
dag.lcDataStore.db.putBestUpdate(
753753
headPeriod, dag.lcDataStore.cache.pendingBest.getOrDefault(key))
754754
else: raiseAssert "Unreachable" # `tailSlot` cannot be before Altair
@@ -845,7 +845,8 @@ proc getLightClientBootstrap*(
845845
dag.withUpdatedExistingState(tmpState[], bsi) do:
846846
branch = withState(state):
847847
when stateFork >= BeaconStateFork.Altair:
848-
state.data.build_proof(altair.CURRENT_SYNC_COMMITTEE_INDEX).get
848+
forkyState.data.build_proof(
849+
altair.CURRENT_SYNC_COMMITTEE_INDEX).get
849850
else: raiseAssert "Unreachable"
850851
do: return err()
851852
dag.lcDataStore.db.putCurrentSyncCommitteeBranch(slot, branch)

beacon_chain/spec/state_transition_block.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ proc check_attester_slashing*(
236236
state: var ForkedHashedBeaconState; attester_slashing: SomeAttesterSlashing;
237237
flags: UpdateFlags): Result[seq[ValidatorIndex], cstring] =
238238
withState(state):
239-
check_attester_slashing(state.data, attester_slashing, flags)
239+
check_attester_slashing(forkyState.data, attester_slashing, flags)
240240

241241
# https://github.com/ethereum/consensus-specs/blob/v1.2.0-rc.3/specs/phase0/beacon-chain.md#attester-slashings
242242
proc process_attester_slashing*(

research/simutils.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ proc loadGenesis*(validators: Natural, validate: bool):
8282
cfg, readAllBytes(genesisFn).tryGet()))
8383

8484
withState(res[]):
85-
if state.data.slot != GENESIS_SLOT:
85+
if forkyState.data.slot != GENESIS_SLOT:
8686
echo "Can only start from genesis state"
8787
quit 1
8888

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

9292
echo &"Loaded {genesisFn}..."

tests/consensus_spec/altair/test_fixture_light_client_sync_protocol.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ proc block_for_next_slot(
124124

125125
let attestations =
126126
if withAttestations:
127-
let block_root = withState(forked): state.latest_block_root
127+
let block_root = withState(forked): forkyState.latest_block_root
128128
makeFullAttestations(forked, block_root, state.slot, cache)
129129
else:
130130
@[]

0 commit comments

Comments
 (0)