@@ -129,7 +129,7 @@ type applicationState struct {
129129 ctx context.Context
130130 cancelCtx context.CancelFunc
131131
132- initialHeight uint64
132+ initialHeight int64
133133
134134 stateRoot storage.Root
135135 storage storage.LocalBackend
@@ -183,7 +183,7 @@ func (s *applicationState) NewContext(mode api.ContextMode) *api.Context {
183183 blockCtx * api.BlockContext
184184 state mkvs.OverlayTree
185185 )
186- blockHeight := int64 (s .stateRoot .Version )
186+ lastHeight := int64 (s .stateRoot .Version )
187187 now := s .blockTime
188188 switch mode {
189189 case api .ContextInitChain :
@@ -193,7 +193,7 @@ func (s *applicationState) NewContext(mode api.ContextMode) *api.Context {
193193 s .initState = mkvs .NewOverlay (mkvs .New (nil , nil , storage .RootTypeState , mkvs .WithoutWriteLog ()))
194194 state = s .initState
195195 // Configure block height so that current height will be correctly computed.
196- blockHeight = int64 ( s .initialHeight ) - 1
196+ lastHeight = s .initialHeight - 1
197197 case api .ContextCheckTx :
198198 state = mkvs .NewOverlayWrapper (s .checkState )
199199 case api .ContextDeliverTx , api .ContextBeginBlock , api .ContextEndBlock :
@@ -215,9 +215,9 @@ func (s *applicationState) NewContext(mode api.ContextMode) *api.Context {
215215 api .NewNopGasAccountant (),
216216 s ,
217217 state ,
218- blockHeight ,
219218 blockCtx ,
220- int64 (s .initialHeight ),
219+ lastHeight ,
220+ s .initialHeight ,
221221 )
222222}
223223
@@ -234,25 +234,26 @@ func (s *applicationState) Checkpointer() checkpoint.Checkpointer {
234234}
235235
236236func (s * applicationState ) InitialHeight () int64 {
237- return int64 ( s .initialHeight )
237+ return s .initialHeight
238238}
239239
240- func (s * applicationState ) BlockHeight () int64 {
240+ func (s * applicationState ) LastHeight () int64 {
241241 s .blockLock .RLock ()
242242 defer s .blockLock .RUnlock ()
243243
244- height := s .stateRoot .Version
244+ height := int64 ( s .stateRoot .Version )
245245 if height < s .initialHeight {
246246 height = 0
247247 }
248- return int64 ( height )
248+ return height
249249}
250250
251251func (s * applicationState ) StateRootHash () []byte {
252252 s .blockLock .RLock ()
253253 defer s .blockLock .RUnlock ()
254254
255- if s .stateRoot .Version < s .initialHeight {
255+ height := int64 (s .stateRoot .Version )
256+ if height < s .initialHeight {
256257 // CometBFT expects a nil hash when there is no state otherwise it will panic.
257258 return nil
258259 }
@@ -279,12 +280,12 @@ func (s *applicationState) GetEpoch(ctx context.Context, blockHeight int64) (bea
279280}
280281
281282func (s * applicationState ) GetCurrentEpoch (ctx context.Context ) (beacon.EpochTime , error ) {
282- blockHeight := s .BlockHeight ()
283- if blockHeight == 0 {
283+ lastHeight := s .LastHeight ()
284+ if lastHeight == 0 {
284285 return beacon .EpochInvalid , nil
285286 }
286287
287- latestHeight := blockHeight
288+ latestHeight := lastHeight
288289 if abciCtx := api .FromCtx (ctx ); abciCtx != nil {
289290 // If request was made from an ABCI application context, then use blockHeight + 1, to fetch
290291 // the epoch at current (future) height. See cometbft/api.NewImmutableState for details.
@@ -297,23 +298,23 @@ func (s *applicationState) GetCurrentEpoch(ctx context.Context) (beacon.EpochTim
297298 if err != nil {
298299 return beacon .EpochInvalid , fmt .Errorf ("failed to get future epoch for height %d: %w" , latestHeight , err )
299300 }
300- if future != nil && future .Height == blockHeight + 1 {
301+ if future != nil && future .Height == lastHeight + 1 {
301302 return future .Epoch , nil
302303 }
303304
304305 currentEpoch , err := s .timeSource .GetEpoch (ctx , latestHeight )
305306 if err != nil {
306- return beacon .EpochInvalid , fmt .Errorf ("failed to get epoch for height %d: %w" , blockHeight + 1 , err )
307+ return beacon .EpochInvalid , fmt .Errorf ("failed to get epoch for height %d: %w" , lastHeight + 1 , err )
307308 }
308309 return currentEpoch , nil
309310}
310311
311312func (s * applicationState ) EpochChanged (ctx * api.Context ) (bool , beacon.EpochTime ) {
312- blockHeight := s .BlockHeight ()
313- if blockHeight == 0 {
313+ lastHeight := s .LastHeight ()
314+ if lastHeight == 0 {
314315 return false , beacon .EpochInvalid
315316 }
316- latestHeight := blockHeight
317+ latestHeight := lastHeight
317318 if abciCtx := api .FromCtx (ctx ); abciCtx != nil {
318319 // If request was made from an ABCI application context, then use blockHeight + 1, to fetch
319320 // the epoch at current (future) height. See cometbft/api.NewImmutableState for details.
@@ -328,7 +329,7 @@ func (s *applicationState) EpochChanged(ctx *api.Context) (bool, beacon.EpochTim
328329 return false , beacon .EpochInvalid
329330 }
330331
331- if uint64 ( blockHeight ) == s .initialHeight {
332+ if lastHeight == s .initialHeight {
332333 // There is no block before the first block. For historic reasons, this is defined as not
333334 // having had a transition.
334335 return false , currentEpoch
@@ -380,13 +381,14 @@ func (s *applicationState) doInitChain() error {
380381
381382 // We use the height before the initial height for the state before the first block. Note that
382383 // this tree is not persisted, we only need it to compute the root hash.
383- _ , stateRootHash , err := s .canonicalState .Commit (s .ctx , s .stateRoot .Namespace , s .initialHeight - 1 , mkvs .NoPersist ())
384+ version := uint64 (s .initialHeight - 1 )
385+ _ , stateRootHash , err := s .canonicalState .Commit (s .ctx , s .stateRoot .Namespace , version , mkvs .NoPersist ())
384386 if err != nil {
385387 return fmt .Errorf ("failed to commit: %w" , err )
386388 }
387389
388390 s .stateRoot .Hash = stateRootHash
389- s .stateRoot .Version = s . initialHeight - 1
391+ s .stateRoot .Version = version
390392
391393 return s .doCommitOrInitChainLocked ()
392394}
@@ -622,7 +624,7 @@ func (s *applicationState) pruneWorker() {
622624 if err := s .statePruner .Prune (version ); err != nil {
623625 s .logger .Warn ("failed to prune state" ,
624626 "err" , err ,
625- "block_height " , version ,
627+ "height " , version ,
626628 )
627629 }
628630 }
@@ -756,7 +758,8 @@ func newApplicationState(ctx context.Context, upgrader upgrade.Backend, cfg *App
756758 }
757759
758760 // Refresh consensus parameters when loading state if we are past genesis.
759- if latestVersion >= s .initialHeight {
761+ initialVersion := uint64 (s .initialHeight )
762+ if latestVersion >= initialVersion {
760763 if err = s .doCommitOrInitChainLocked (); err != nil {
761764 return nil , fmt .Errorf ("state: failed to run initial state commit hook: %w" , err )
762765 }
@@ -774,7 +777,7 @@ func newApplicationState(ctx context.Context, upgrader upgrade.Backend, cfg *App
774777 Interval : params .StateCheckpointInterval ,
775778 NumKept : params .StateCheckpointNumKept ,
776779 ChunkSize : params .StateCheckpointChunkSize ,
777- InitialVersion : cfg . InitialHeight ,
780+ InitialVersion : initialVersion ,
778781 ChunkerThreads : cfg .ChunkerThreads ,
779782 }, nil
780783 },
0 commit comments