Skip to content

Commit

Permalink
feat: unify version modifier for v2 (#21508)
Browse files Browse the repository at this point in the history
(cherry picked from commit dce0365)

# Conflicts:
#	baseapp/utils_test.go
#	core/server/app.go
#	runtime/v2/module.go
#	server/v2/stf/core_branch_service.go
#	server/v2/stf/core_branch_service_test.go
  • Loading branch information
randygrok authored and mergify[bot] committed Sep 6, 2024
1 parent 889becd commit 1dde9de
Show file tree
Hide file tree
Showing 12 changed files with 644 additions and 31 deletions.
15 changes: 5 additions & 10 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package baseapp

import (
"context"
"cosmossdk.io/core/server"
"errors"
"fmt"
"maps"
Expand Down Expand Up @@ -89,6 +90,7 @@ type BaseApp struct {
verifyVoteExt sdk.VerifyVoteExtensionHandler // ABCI VerifyVoteExtension handler
prepareCheckStater sdk.PrepareCheckStater // logic to run during commit using the checkState
precommiter sdk.Precommiter // logic to run during commit using the deliverState
versionModifier server.VersionModifier // interface to get and set the app version

addrPeerFilter sdk.PeerFilter // filter peers by address and port
idPeerFilter sdk.PeerFilter // filter peers by node ID
Expand Down Expand Up @@ -249,18 +251,11 @@ func (app *BaseApp) Name() string {

// AppVersion returns the application's protocol version.
func (app *BaseApp) AppVersion(ctx context.Context) (uint64, error) {
if app.paramStore == nil {
return 0, errors.New("app.paramStore is nil")
if app.versionModifier == nil {
return 0, errors.New("app.versionModifier 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
return app.versionModifier.AppVersion(ctx)
}

// Version returns the application's version string.
Expand Down
1 change: 1 addition & 0 deletions baseapp/baseapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func NewBaseAppSuite(t *testing.T, opts ...func(*baseapp.BaseApp)) *BaseAppSuite
app.SetParamStore(paramStore{db: dbm.NewMemDB()})
app.SetTxDecoder(txConfig.TxDecoder())
app.SetTxEncoder(txConfig.TxEncoder())
app.SetVersionModifier(newMockedVersionModifier(0))

// mount stores and seal
require.Nil(t, app.LoadLatestVersion())
Expand Down
27 changes: 13 additions & 14 deletions baseapp/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package baseapp

import (
"context"
"cosmossdk.io/core/server"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -146,22 +147,11 @@ func (app *BaseApp) SetVersion(v string) {
// 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")
if app.versionModifier == nil {
return errors.New("version modifier 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
return app.versionModifier.SetAppVersion(ctx, v)
}

func (app *BaseApp) SetDB(db corestore.KVStoreWithBatch) {
Expand Down Expand Up @@ -323,6 +313,15 @@ func (app *BaseApp) SetTxEncoder(txEncoder sdk.TxEncoder) {
app.txEncoder = txEncoder
}

// SetVersionModifier sets the version modifier for the BaseApp that allows to set the app version.
func (app *BaseApp) SetVersionModifier(versionModifier server.VersionModifier) {
if app.sealed {
panic("SetVersionModifier() on sealed BaseApp")
}

app.versionModifier = versionModifier
}

// SetQueryMultiStore set a alternative MultiStore implementation to support grpc query service.
//
// Ref: https://github.com/cosmos/cosmos-sdk/issues/13317
Expand Down
90 changes: 90 additions & 0 deletions baseapp/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package baseapp_test
import (
"bytes"
"context"
"cosmossdk.io/core/server"
"encoding/binary"
"encoding/json"
"errors"
Expand Down Expand Up @@ -375,3 +376,92 @@ func wonkyMsg(t *testing.T, cfg client.TxConfig, tx signing.Tx) signing.Tx {
require.NoError(t, err)
return builder.GetTx()
}
<<<<<<< HEAD
=======

type SendServerImpl struct {
gas uint64
}

func (s SendServerImpl) Send(ctx context.Context, send *baseapptestutil.MsgSend) (*baseapptestutil.MsgSendResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
if send.From == "" {
return nil, errors.New("from address cannot be empty")
}
if send.To == "" {
return nil, errors.New("to address cannot be empty")
}

_, err := sdk.ParseCoinNormalized(send.Amount)
if err != nil {
return nil, err
}
gas := s.gas
if gas == 0 {
gas = 5
}
sdkCtx.GasMeter().ConsumeGas(gas, "send test")
return &baseapptestutil.MsgSendResponse{}, nil
}

type NestedMessgesServerImpl struct {
gas uint64
}

func (n NestedMessgesServerImpl) Check(ctx context.Context, message *baseapptestutil.MsgNestedMessages) (*baseapptestutil.MsgCreateNestedMessagesResponse, error) {
sdkCtx := sdk.UnwrapSDKContext(ctx)
cdc := codectestutil.CodecOptions{}.NewCodec()
baseapptestutil.RegisterInterfaces(cdc.InterfaceRegistry())

signer, _, err := cdc.GetMsgSigners(message)
if err != nil {
return nil, err
}
if len(signer) != 1 {
return nil, fmt.Errorf("expected 1 signer, got %d", len(signer))
}

msgs, err := message.GetMsgs()
if err != nil {
return nil, err
}

for _, msg := range msgs {
s, _, err := cdc.GetMsgSigners(msg)
if err != nil {
return nil, err
}
if len(s) != 1 {
return nil, fmt.Errorf("expected 1 signer, got %d", len(s))
}
if !bytes.Equal(signer[0], s[0]) {
return nil, errors.New("signer does not match")
}

}

gas := n.gas
if gas == 0 {
gas = 5
}
sdkCtx.GasMeter().ConsumeGas(gas, "nested messages test")
return nil, nil
}

func newMockedVersionModifier(startingVersion uint64) server.VersionModifier {
return &mockedVersionModifier{version: startingVersion}
}

type mockedVersionModifier struct {
version uint64
}

func (m *mockedVersionModifier) SetAppVersion(ctx context.Context, u uint64) error {
m.version = u
return nil
}

func (m *mockedVersionModifier) AppVersion(ctx context.Context) (uint64, error) {
return m.version, nil
}
>>>>>>> dce0365c2 (feat: unify version modifier for v2 (#21508))
59 changes: 59 additions & 0 deletions core/server/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package server

import (
"context"
"time"

appmodulev2 "cosmossdk.io/core/appmodule/v2"
"cosmossdk.io/core/event"
"cosmossdk.io/core/transaction"
)

// BlockRequest defines the request structure for a block coming from consensus server to the state transition function.
type BlockRequest[T transaction.Tx] struct {
Height uint64
Time time.Time
Hash []byte
ChainId string
AppHash []byte
Txs []T

// IsGenesis indicates if this block is the first block of the chain.
IsGenesis bool
}

// BlockResponse defines the response structure for a block coming from the state transition function to consensus server.
type BlockResponse struct {
ValidatorUpdates []appmodulev2.ValidatorUpdate
PreBlockEvents []event.Event
BeginBlockEvents []event.Event
TxResults []TxResult
EndBlockEvents []event.Event
}

// TxResult defines the result of a transaction execution.
type TxResult struct {
// Events produced by the transaction.
Events []event.Event
// Response messages produced by the transaction.
Resp []transaction.Msg
// Error produced by the transaction.
Error error
// Code produced by the transaction.
// A non-zero code is an error that is either define by the module via the cosmossdk.io/errors/v2 package
// or injected through the antehandler along the execution of the transaction.
Code uint32
// GasWanted is the maximum units of work we allow this tx to perform.
GasWanted uint64
// GasUsed is the amount of gas actually consumed.
GasUsed uint64
}

// VersionModifier defines the interface fulfilled by BaseApp
// which allows getting and setting its appVersion field. This
// in turn updates the consensus params that are sent to the
// consensus engine in EndBlock
type VersionModifier interface {
SetAppVersion(context.Context, uint64) error
AppVersion(context.Context) (uint64, error)
}
6 changes: 0 additions & 6 deletions runtime/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
"cosmossdk.io/core/appmodule"
"cosmossdk.io/core/comet"
"cosmossdk.io/core/registry"
"cosmossdk.io/core/server"
"cosmossdk.io/core/store"
"cosmossdk.io/depinject"
"cosmossdk.io/depinject/appconfig"
Expand Down Expand Up @@ -103,7 +102,6 @@ func init() {
ProvideEnvironment,
ProvideTransientStoreService,
ProvideModuleManager,
ProvideAppVersionModifier,
ProvideCometService,
),
appconfig.Invoke(SetupAppBuilder),
Expand Down Expand Up @@ -293,10 +291,6 @@ func ProvideTransientStoreService(
return transientStoreService{key: storeKey}
}

func ProvideAppVersionModifier(app *AppBuilder) server.VersionModifier {
return app.app
}

func ProvideCometService() comet.Service {
return NewContextAwareCometInfoService()
}
Loading

0 comments on commit 1dde9de

Please sign in to comment.