Skip to content

Commit

Permalink
fix: allow application to set and query app version (#395)
Browse files Browse the repository at this point in the history
* allow application to query and set app version

* Apply suggestions from code review

Co-authored-by: Rootul P <[email protected]>
  • Loading branch information
cmwaters and rootulp authored Apr 29, 2024
1 parent 02244ae commit d2720e3
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 34 deletions.
22 changes: 9 additions & 13 deletions baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,8 @@ func (app *BaseApp) SetOption(req abci.RequestSetOption) (res abci.ResponseSetOp

// Info implements the ABCI interface.
func (app *BaseApp) Info(req abci.RequestInfo) abci.ResponseInfo {
// get the latest height and app hash
lastCommitID := app.cms.LastCommitID()
// load the app version for a non zero height and zero app hash
if lastCommitID.Version > 0 && app.appVersion == 0 {
ctx, err := app.createQueryContext(lastCommitID.Version, false)
if err != nil {
panic(err)
}
// initialise the app version by checking if it is already in state
app.InitAppVersion(ctx)
}
return abci.ResponseInfo{
Data: app.name,
Version: app.version,
Expand Down Expand Up @@ -621,7 +613,7 @@ func (app *BaseApp) ApplySnapshotChunk(req abci.RequestApplySnapshotChunk) abci.
}

func (app *BaseApp) handleQueryGRPC(handler GRPCQueryHandler, req abci.RequestQuery) abci.ResponseQuery {
ctx, err := app.createQueryContext(req.Height, req.Prove)
ctx, err := app.CreateQueryContext(req.Height, req.Prove)
if err != nil {
return sdkerrors.QueryResult(err, app.trace)
}
Expand Down Expand Up @@ -667,9 +659,9 @@ func checkNegativeHeight(height int64) error {
return nil
}

// createQueryContext creates a new sdk.Context for a query, taking as args
// CreateQueryContext creates a new sdk.Context for a query, taking as args
// the block height and whether the query needs a proof or not.
func (app *BaseApp) createQueryContext(height int64, prove bool) (sdk.Context, error) {
func (app *BaseApp) CreateQueryContext(height int64, prove bool) (sdk.Context, error) {
if err := checkNegativeHeight(height); err != nil {
return sdk.Context{}, err
}
Expand Down Expand Up @@ -711,6 +703,10 @@ func (app *BaseApp) createQueryContext(height int64, prove bool) (sdk.Context, e
)
}

if app.checkState == nil {
app.setCheckState(tmproto.Header{Height: height})
}

// branch the commit-multistore for safety
ctx := sdk.NewContext(
cacheMS, app.checkState.ctx.BlockHeader(), true, app.logger,
Expand Down Expand Up @@ -892,7 +888,7 @@ func handleQueryCustom(app *BaseApp, path []string, req abci.RequestQuery) abci.
return sdkerrors.QueryResult(sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "no custom querier found for route %s", path[1]), app.trace)
}

ctx, err := app.createQueryContext(req.Height, req.Prove)
ctx, err := app.CreateQueryContext(req.Height, req.Prove)
if err != nil {
return sdkerrors.QueryResult(err, app.trace)
}
Expand Down
2 changes: 1 addition & 1 deletion baseapp/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func TestBaseAppCreateQueryContext(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
_, err := app.createQueryContext(tc.height, tc.prove)
_, err := app.CreateQueryContext(tc.height, tc.prove)
if tc.expErr {
require.Error(t, err)
} else {
Expand Down
25 changes: 13 additions & 12 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,21 @@ func (app *BaseApp) Name() string {
return app.name
}

func (app *BaseApp) InitAppVersion(ctx sdk.Context) {
if app.appVersion == 0 && app.paramStore.Has(ctx, ParamStoreKeyVersionParams) {
// GetAppVersionFromParamStore returns the app version from the param store.
func (app *BaseApp) GetAppVersionFromParamStore(ctx sdk.Context) uint64 {
if app.paramStore.Has(ctx, ParamStoreKeyVersionParams) {
var vp tmproto.VersionParams
app.paramStore.Get(ctx, ParamStoreKeyVersionParams, &vp)
// set the app version
app.appVersion = vp.AppVersion
return vp.AppVersion
}
return 0
}

// SetInitialAppVersionInConsensusParams sets the initial app version
// in the consensus params if it has not yet been set.
func (app *BaseApp) SetInitialAppVersionInConsensusParams(ctx sdk.Context, version uint64) {
if !app.paramStore.Has(ctx, ParamStoreKeyVersionParams) {
app.paramStore.Set(ctx, ParamStoreKeyVersionParams, &tmproto.VersionParams{AppVersion: version})
}
}

Expand Down Expand Up @@ -546,14 +555,6 @@ func (app *BaseApp) StoreConsensusParams(ctx sdk.Context, cp *abci.ConsensusPara
}
}

// SetInitialAppVersionInConsensusParams sets the initial app version
// in the consensus params if it has not yet been set.
func (app *BaseApp) SetInitialAppVersionInConsensusParams(ctx sdk.Context, version uint64) {
if !app.paramStore.Has(ctx, ParamStoreKeyVersionParams) {
app.paramStore.Set(ctx, ParamStoreKeyVersionParams, &tmproto.VersionParams{AppVersion: version})
}
}

// getMaximumBlockGas gets the maximum gas from the consensus params. It panics
// if maximum block gas is less than negative one and returns zero if negative
// one.
Expand Down
2 changes: 1 addition & 1 deletion baseapp/grpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (app *BaseApp) RegisterGRPCServer(server gogogrpc.Server) {

// Create the sdk.Context. Passing false as 2nd arg, as we can't
// actually support proofs with gRPC right now.
sdkCtx, err := app.createQueryContext(height, false)
sdkCtx, err := app.CreateQueryContext(height, false)
if err != nil {
return nil, err
}
Expand Down
7 changes: 0 additions & 7 deletions baseapp/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,6 @@ func (app *BaseApp) GetName() string {
return app.name
}

// CreateQueryContext calls app's createQueryContext.
//
// This method is only accessible in baseapp tests.
func (app *BaseApp) CreateQueryContext(height int64, prove bool) (types.Context, error) {
return app.createQueryContext(height, prove)
}

// MinGasPrices returns minGasPrices.
//
// This method is only accessible in baseapp tests.
Expand Down

0 comments on commit d2720e3

Please sign in to comment.