feat!: connect app version with consensus params in end block (#16244)

Co-authored-by: Marko <marko@baricevic.me>
Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
Co-authored-by: marbar3778 <marbar3778@yahoo.com>
This commit is contained in:
Callum Waters
2023-08-29 10:32:53 +00:00
committed by GitHub
co-authored by Marko Aleksandr Bezobchuk marbar3778
parent 6601713eb6
commit 79cc75b1db
18 changed files with 245 additions and 168 deletions
+12 -1
View File
@@ -149,11 +149,22 @@ func (app *BaseApp) InitChain(req *abci.RequestInitChain) (*abci.ResponseInitCha
func (app *BaseApp) Info(req *abci.RequestInfo) (*abci.ResponseInfo, error) {
lastCommitID := app.cms.LastCommitID()
appVersion := InitialAppVersion
if lastCommitID.Version > 0 {
ctx, err := app.CreateQueryContext(lastCommitID.Version, false)
if err != nil {
return nil, fmt.Errorf("failed creating query context: %w", err)
}
appVersion, err = app.AppVersion(ctx)
if err != nil {
return nil, fmt.Errorf("failed getting app version: %w", err)
}
}
return &abci.ResponseInfo{
Data: app.name,
Version: app.version,
AppVersion: app.appVersion,
AppVersion: appVersion,
LastBlockHeight: lastCommitID.Version,
LastBlockAppHash: lastCommitID.Hash,
}, nil
+16 -1
View File
@@ -17,6 +17,7 @@ import (
"github.com/cometbft/cometbft/crypto/secp256k1"
cmtprotocrypto "github.com/cometbft/cometbft/proto/tendermint/crypto"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
cmttypes "github.com/cometbft/cometbft/types"
dbm "github.com/cosmos/cosmos-db"
protoio "github.com/cosmos/gogoproto/io"
"github.com/cosmos/gogoproto/jsonpb"
@@ -44,6 +45,9 @@ import (
func TestABCI_Info(t *testing.T) {
suite := NewBaseAppSuite(t)
ctx := suite.baseApp.NewContext(true)
err := suite.baseApp.StoreConsensusParams(ctx, cmttypes.DefaultConsensusParams().ToProto())
require.NoError(t, err)
reqInfo := abci.RequestInfo{}
res, err := suite.baseApp.Info(&reqInfo)
@@ -53,7 +57,18 @@ func TestABCI_Info(t *testing.T) {
require.Equal(t, t.Name(), res.GetData())
require.Equal(t, int64(0), res.LastBlockHeight)
require.Equal(t, []uint8(nil), res.LastBlockAppHash)
require.Equal(t, suite.baseApp.AppVersion(), res.AppVersion)
appVersion, err := suite.baseApp.AppVersion(ctx)
require.NoError(t, err)
require.Equal(t, appVersion, res.AppVersion)
_, err = suite.baseApp.FinalizeBlock(&abci.RequestFinalizeBlock{Height: 1})
require.NoError(t, err)
_, err = suite.baseApp.Commit()
require.NoError(t, err)
require.NoError(t, suite.baseApp.SetAppVersion(ctx, 1))
res, err = suite.baseApp.Info(&reqInfo)
require.NoError(t, err)
require.Equal(t, uint64(1), res.AppVersion)
}
func TestABCI_First_block_Height(t *testing.T) {
+13 -9
View File
@@ -167,10 +167,6 @@ type BaseApp struct {
// application's version string
version string
// application's protocol version that increments on every upgrade
// if BaseApp is passed to the upgrade keeper's NewKeeper method.
appVersion uint64
// recovery handler for app.runTx method
runTxRecoveryMiddleware recoveryMiddleware
@@ -249,8 +245,19 @@ func (app *BaseApp) Name() string {
}
// AppVersion returns the application's protocol version.
func (app *BaseApp) AppVersion() uint64 {
return app.appVersion
func (app *BaseApp) AppVersion(ctx context.Context) (uint64, error) {
if app.paramStore == nil {
return 0, errors.New("app.paramStore is nil")
}
cp, err := app.paramStore.Get(ctx)
if err != nil {
return 0, fmt.Errorf("failed to get consensus params: %w", err)
}
if cp.Version == nil {
return 0, nil
}
return cp.Version.App, nil
}
// Version returns the application's version string.
@@ -522,9 +529,6 @@ func (app *BaseApp) GetConsensusParams(ctx sdk.Context) cmtproto.ConsensusParams
// StoreConsensusParams sets the consensus parameters to the BaseApp's param
// store.
//
// NOTE: We're explicitly not storing the CometBFT app_version in the param store.
// It's stored instead in the x/upgrade store, with its own bump logic.
func (app *BaseApp) StoreConsensusParams(ctx sdk.Context, cp cmtproto.ConsensusParams) error {
if app.paramStore == nil {
return errors.New("cannot store consensus params with no params store set")
+21 -3
View File
@@ -1,6 +1,8 @@
package baseapp
import (
"context"
"errors"
"fmt"
"io"
"math"
@@ -129,9 +131,25 @@ func (app *BaseApp) SetVersion(v string) {
app.version = v
}
// SetProtocolVersion sets the application's protocol version
func (app *BaseApp) SetProtocolVersion(v uint64) {
app.appVersion = v
// SetAppVersion sets the application's version this is used as part of the
// header in blocks and is returned to the consensus engine in EndBlock.
func (app *BaseApp) SetAppVersion(ctx context.Context, v uint64) error {
if app.paramStore == nil {
return errors.New("param store must be set to set app version")
}
cp, err := app.paramStore.Get(ctx)
if err != nil {
return fmt.Errorf("failed to get consensus params: %w", err)
}
if cp.Version == nil {
return errors.New("version is not set in param store")
}
cp.Version.App = v
if err := app.paramStore.Set(ctx, cp); err != nil {
return err
}
return nil
}
func (app *BaseApp) SetDB(db dbm.DB) {
+11
View File
@@ -6,6 +6,8 @@ import (
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
)
const InitialAppVersion uint64 = 0
// ParamStore defines the interface the parameter store used by the BaseApp must
// fulfill.
type ParamStore interface {
@@ -13,3 +15,12 @@ type ParamStore interface {
Has(ctx context.Context) (bool, error)
Set(ctx context.Context, cp cmtproto.ConsensusParams) error
}
// AppVersionModifier defines the interface fulfilled by BaseApp
// which allows getting and setting it's appVersion field. This
// in turn updates the consensus params that are sent to the
// consensus engine in EndBlock
type AppVersionModifier interface {
SetAppVersion(context.Context, uint64) error
AppVersion(context.Context) (uint64, error)
}