Skip to content

Commit 5d1c3ea

Browse files
committed
go/consensus/cometbft: Rename block height to last height
1 parent 4e68963 commit 5d1c3ea

11 files changed

Lines changed: 67 additions & 64 deletions

File tree

.changelog/6303.trivial.md

Whitespace-only changes.

go/consensus/cometbft/abci/mux.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ type ApplicationConfig struct {
7575
ReadOnlyStorage bool
7676

7777
// InitialHeight is the height of the initial block.
78-
InitialHeight uint64
78+
InitialHeight int64
7979

8080
// ChainContext is the chain context for the network.
8181
ChainContext string
@@ -273,7 +273,7 @@ func (mux *abciMux) registerHaltHook(hook api.HaltHook) {
273273
func (mux *abciMux) Info(types.RequestInfo) types.ResponseInfo {
274274
return types.ResponseInfo{
275275
AppVersion: version.CometBFTAppVersion,
276-
LastBlockHeight: mux.state.BlockHeight(),
276+
LastBlockHeight: mux.state.LastHeight(),
277277
LastBlockAppHash: mux.state.StateRootHash(),
278278
}
279279
}
@@ -298,7 +298,7 @@ func (mux *abciMux) InitChain(req types.RequestInitChain) types.ResponseInitChai
298298
})
299299
mux.state.blockLock.Unlock()
300300

301-
if st.Height != req.InitialHeight || uint64(st.Height) != mux.state.initialHeight {
301+
if st.Height != req.InitialHeight || st.Height != mux.state.initialHeight {
302302
panic(fmt.Errorf("mux: inconsistent initial height (genesis: %d abci: %d state: %d)", st.Height, req.InitialHeight, mux.state.initialHeight))
303303
}
304304

@@ -570,12 +570,12 @@ func (mux *abciMux) BeginBlock(req types.RequestBeginBlock) types.ResponseBeginB
570570
return *mux.state.proposal.resultsBeginBlock
571571
}
572572

573-
blockHeight := mux.state.BlockHeight()
573+
lastHeight := mux.state.LastHeight()
574574

575575
mux.logger.Debug("BeginBlock",
576576
"req", req,
577577
"hash", hex.EncodeToString(req.Hash),
578-
"block_height", blockHeight,
578+
"height", lastHeight,
579579
)
580580

581581
params := mux.state.ConsensusParameters()
@@ -607,20 +607,20 @@ func (mux *abciMux) BeginBlock(req types.RequestBeginBlock) types.ResponseBeginB
607607
// checks must run on each block to make sure that any pending upgrade descriptors are cleared
608608
// after consensus upgrade is performed.
609609
if upgrader := mux.state.Upgrader(); upgrader != nil {
610-
switch err := upgrader.ConsensusUpgrade(ctx, currentEpoch, blockHeight); err {
610+
switch err := upgrader.ConsensusUpgrade(ctx, currentEpoch, lastHeight); err {
611611
case nil:
612612
// Everything ok.
613613
case upgrade.ErrStopForUpgrade:
614614
// Signal graceful stop for upgrade.
615-
mux.haltForUpgrade(blockHeight, currentEpoch, true)
615+
mux.haltForUpgrade(lastHeight, currentEpoch, true)
616616
default:
617617
panic(fmt.Errorf("mux: error while trying to perform consensus upgrade: %w", err))
618618
}
619619
}
620620

621621
// Check if we need to halt based on local configuration.
622-
if mux.state.shouldLocalHalt(blockHeight+1, currentEpoch) {
623-
mux.haltForUpgrade(blockHeight, currentEpoch, true)
622+
if mux.state.shouldLocalHalt(lastHeight+1, currentEpoch) {
623+
mux.haltForUpgrade(lastHeight, currentEpoch, true)
624624
}
625625

626626
// Dispatch BeginBlock to all applications.
@@ -632,7 +632,7 @@ func (mux *abciMux) BeginBlock(req types.RequestBeginBlock) types.ResponseBeginB
632632
)
633633

634634
if errors.Is(err, upgrade.ErrStopForUpgrade) {
635-
mux.haltForUpgrade(blockHeight, currentEpoch, true)
635+
mux.haltForUpgrade(lastHeight, currentEpoch, true)
636636
}
637637
panic(fmt.Errorf("mux: BeginBlock: fatal error in application: '%s': %w", app.Name(), err))
638638
}
@@ -642,7 +642,7 @@ func (mux *abciMux) BeginBlock(req types.RequestBeginBlock) types.ResponseBeginB
642642

643643
// During the first block, also collect and prepend application events generated during
644644
// InitChain to BeginBlock events.
645-
if mux.state.BlockHeight() == 0 {
645+
if mux.state.LastHeight() == 0 {
646646
response.Events = append(response.Events, mux.state.initEvents...)
647647
}
648648

@@ -761,7 +761,7 @@ func (mux *abciMux) EndBlock(req types.RequestEndBlock) types.ResponseEndBlock {
761761

762762
mux.logger.Debug("EndBlock",
763763
"req", req,
764-
"block_height", mux.state.BlockHeight(),
764+
"height", mux.state.LastHeight(),
765765
)
766766

767767
ctx := mux.state.NewContext(api.ContextEndBlock)
@@ -829,7 +829,7 @@ func (mux *abciMux) Commit() types.ResponseCommit {
829829
}
830830

831831
mux.logger.Debug("Commit",
832-
"block_height", mux.state.BlockHeight(),
832+
"height", mux.state.LastHeight(),
833833
"state_root_hash", hex.EncodeToString(mux.state.StateRootHash()),
834834
"last_retained_version", lastRetainedVersion,
835835
)
@@ -936,7 +936,7 @@ func (mux *abciMux) checkDependencies() error {
936936
}
937937

938938
func (mux *abciMux) finishInitialization() error {
939-
if mux.state.BlockHeight() >= mux.state.InitialHeight() {
939+
if mux.state.LastHeight() >= mux.state.InitialHeight() {
940940
mux.state.resetProposal()
941941
defer mux.state.closeProposal()
942942

@@ -996,7 +996,7 @@ func newABCIMux(ctx context.Context, upgrader upgrade.Backend, cfg *ApplicationC
996996
mux.md.Subscribe(api.MessageExecuteSubcall, mux)
997997

998998
mux.logger.Debug("ABCI multiplexer initialized",
999-
"block_height", state.BlockHeight(),
999+
"height", state.LastHeight(),
10001000
"state_root_hash", hex.EncodeToString(state.StateRootHash()),
10011001
)
10021002

go/consensus/cometbft/abci/state.go

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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
@@ -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-
lastHeight = 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:
@@ -217,7 +217,7 @@ func (s *applicationState) NewContext(mode api.ContextMode) *api.Context {
217217
state,
218218
blockCtx,
219219
lastHeight,
220-
int64(s.initialHeight),
220+
s.initialHeight,
221221
)
222222
}
223223

@@ -234,25 +234,26 @@ func (s *applicationState) Checkpointer() checkpoint.Checkpointer {
234234
}
235235

236236
func (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

251251
func (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

281282
func (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

311312
func (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
},

go/consensus/cometbft/abci/transaction.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ func (mux *abciMux) EstimateGas(caller signature.PublicKey, tx *transaction.Tran
156156

157157
// Certain modules, in particular the beacon require InitChain or BeginBlock
158158
// to have completed before initialization is complete.
159-
if mux.state.BlockHeight() == 0 {
159+
if mux.state.LastHeight() == 0 {
160160
return 0, consensus.ErrNoCommittedBlocks
161161
}
162162

go/consensus/cometbft/abci/upgrade.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func (mux *abciMux) maybeHaltForUpgrade() {
2424
)
2525
return
2626
}
27-
height := mux.state.BlockHeight()
27+
height := mux.state.LastHeight()
2828

2929
// Check if we need to halt for an upgrade -- but do not actually run the upgrade.
3030
switch err := upgrader.ConsensusUpgrade(nil, epoch, height); err {

go/consensus/cometbft/api/state.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ type ApplicationQueryState interface {
8383
// This may be nil in case checkpoints are disabled.
8484
Checkpointer() checkpoint.Checkpointer
8585

86-
// BlockHeight returns the last committed block height.
87-
BlockHeight() int64
86+
// LastHeight returns the last committed block height.
87+
LastHeight() int64
8888

8989
// GetEpoch returns epoch at block height.
9090
GetEpoch(ctx context.Context, blockHeight int64) (beacon.EpochTime, error)
@@ -104,7 +104,7 @@ type MockApplicationState interface {
104104

105105
// MockApplicationStateConfig is the configuration for the mock application state.
106106
type MockApplicationStateConfig struct {
107-
BlockHeight int64
107+
LastHeight int64
108108
StateRootHash []byte
109109

110110
BaseEpoch beacon.EpochTime
@@ -155,8 +155,8 @@ func (ms *mockApplicationState) InitialHeight() int64 {
155155
return ms.cfg.Genesis.Height
156156
}
157157

158-
func (ms *mockApplicationState) BlockHeight() int64 {
159-
return ms.cfg.BlockHeight
158+
func (ms *mockApplicationState) LastHeight() int64 {
159+
return ms.cfg.LastHeight
160160
}
161161

162162
func (ms *mockApplicationState) StateRootHash() []byte {
@@ -214,7 +214,7 @@ func (ms *mockApplicationState) NewContext(mode ContextMode) *Context {
214214
gasAccountant: NewNopGasAccountant(),
215215
state: ms.tree,
216216
appState: ms,
217-
lastHeight: ms.cfg.BlockHeight,
217+
lastHeight: ms.cfg.LastHeight,
218218
blockCtx: ms.blockCtx,
219219
initialHeight: ms.InitialHeight(),
220220
logger: logging.GetLogger("consensus/cometbft/abci").With("mode", mode),
@@ -273,14 +273,14 @@ func NewImmutableStateAt(ctx context.Context, state ApplicationQueryState, versi
273273
}
274274

275275
// Handle a regular (external) query where we need to create a new tree.
276-
if state.BlockHeight() == 0 {
276+
if state.LastHeight() == 0 {
277277
return nil, consensus.ErrNoCommittedBlocks
278278
}
279-
if version > state.BlockHeight() {
279+
if version > state.LastHeight() {
280280
return nil, consensus.ErrVersionNotFound
281281
}
282282
if version <= 0 {
283-
version = state.BlockHeight()
283+
version = state.LastHeight()
284284
}
285285

286286
ndb := state.Storage().NodeDB()

go/consensus/cometbft/apps/governance/genesis_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ func TestGenesis(t *testing.T) {
279279
currentEpoch := beacon.EpochTime(80)
280280

281281
appState := abciAPI.NewMockApplicationState(&abciAPI.MockApplicationStateConfig{
282-
BlockHeight: 1000,
282+
LastHeight: 1000,
283283
BaseEpoch: 1,
284284
CurrentEpoch: currentEpoch,
285285
})

go/consensus/cometbft/full/archive.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ func NewArchive(ctx context.Context, cfg ArchiveConfig) (consensusAPI.Service, e
178178
},
179179
Identity: cfg.Identity,
180180
DisableCheckpointer: true,
181-
InitialHeight: uint64(srv.genesisHeight),
181+
InitialHeight: srv.genesisHeight,
182182
// ReadOnly should actually be preferable for archive but there is a badger issue with read-only:
183183
// https://discuss.dgraph.io/t/read-only-log-truncate-required-to-run-db/16444/2
184184
ReadOnlyStorage: false,

go/consensus/cometbft/full/common.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ func (n *commonNode) heightToCometBFTHeight(height int64) (int64, error) {
511511
// Do not let CometBFT determine the latest height (e.g., by passing nil) as that
512512
// completely ignores ABCI processing so it can return a block for which local state does
513513
// not yet exist. Use our mux notion of latest height instead.
514-
tmHeight := n.mux.State().BlockHeight()
514+
tmHeight := n.mux.State().LastHeight()
515515
if tmHeight == 0 {
516516
// No committed blocks yet.
517517
return 0, consensusAPI.ErrNoCommittedBlocks

go/consensus/cometbft/full/full.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ func (t *fullService) lazyInit() error { // nolint: gocyclo
571571
DisableCheckpointer: config.GlobalConfig.Consensus.Checkpointer.Disabled,
572572
CheckpointerCheckInterval: config.GlobalConfig.Consensus.Checkpointer.CheckInterval,
573573
ChunkerThreads: threads,
574-
InitialHeight: uint64(t.genesisHeight),
574+
InitialHeight: t.genesisHeight,
575575
ChainContext: t.chainContext,
576576
}
577577
t.mux, err = abci.NewApplicationServer(t.ctx, t.upgrader, appConfig)

0 commit comments

Comments
 (0)