Skip to content

Commit

Permalink
feat: custom upgrade module (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
j75689 authored Jan 5, 2023
1 parent f8ae8d6 commit 4c7b018
Show file tree
Hide file tree
Showing 81 changed files with 834 additions and 4,245 deletions.
2 changes: 1 addition & 1 deletion baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ func (app *BaseApp) createQueryContext(height int64, prove bool) (sdk.Context, e

// branch the commit-multistore for safety
ctx := sdk.NewContext(
cacheMS, app.checkState.ctx.BlockHeader(), true, app.logger,
cacheMS, app.checkState.ctx.BlockHeader(), true, app.upgradeChecker, app.logger,
).WithMinGasPrices(app.minGasPrices).WithBlockHeight(height)

return ctx, nil
Expand Down
31 changes: 28 additions & 3 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
dbm "github.com/tendermint/tm-db"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
serverconfig "github.com/cosmos/cosmos-sdk/server/config"
"github.com/cosmos/cosmos-sdk/snapshots"
"github.com/cosmos/cosmos-sdk/store"
"github.com/cosmos/cosmos-sdk/store/rootmulti"
Expand Down Expand Up @@ -59,6 +60,9 @@ type BaseApp struct { // nolint: maligned
abciData
moduleRouter

appConfig serverconfig.Config
chainID string

// volatile states:
//
// checkState is set on InitChain and reset on Commit
Expand Down Expand Up @@ -111,6 +115,9 @@ type BaseApp struct { // nolint: maligned
// abciListeners for hooking into the ABCI message processing of the BaseApp
// and exposing the requests and responses to external consumers
abciListeners []ABCIListener

// upgradeChecker is a hook function from the upgrade module to check upgrade is executed or not.
upgradeChecker func(ctx sdk.Context, name string) bool
}

type appStore struct {
Expand Down Expand Up @@ -390,6 +397,14 @@ func (app *BaseApp) setIndexEvents(ie []string) {
}
}

func (app *BaseApp) setAppConfig(config serverconfig.Config) {
app.appConfig = config
}

func (app *BaseApp) setChainID(chainID string) {
app.chainID = chainID
}

// Router returns the legacy router of the BaseApp.
func (app *BaseApp) Router() sdk.Router {
if app.sealed {
Expand Down Expand Up @@ -418,7 +433,7 @@ func (app *BaseApp) setCheckState(header tmproto.Header) {
ms := app.cms.CacheMultiStore()
app.checkState = &state{
ms: ms,
ctx: sdk.NewContext(ms, header, true, app.logger).WithMinGasPrices(app.minGasPrices),
ctx: sdk.NewContext(ms, header, true, app.upgradeChecker, app.logger).WithMinGasPrices(app.minGasPrices),
}
}

Expand All @@ -430,7 +445,7 @@ func (app *BaseApp) setDeliverState(header tmproto.Header) {
ms := app.cms.CacheMultiStore()
app.deliverState = &state{
ms: ms,
ctx: sdk.NewContext(ms, header, false, app.logger),
ctx: sdk.NewContext(ms, header, false, app.upgradeChecker, app.logger),
}
}

Expand Down Expand Up @@ -467,6 +482,16 @@ func (app *BaseApp) GetConsensusParams(ctx sdk.Context) *abci.ConsensusParams {
return cp
}

// AppConfig returns the AppConfig.
func (app *BaseApp) AppConfig() serverconfig.Config {
return app.appConfig
}

// ChainID returns the chain id.
func (app *BaseApp) ChainID() string {
return app.chainID
}

// AddRunTxRecoveryHandler adds custom app.runTx method panic handlers.
func (app *BaseApp) AddRunTxRecoveryHandler(handlers ...RecoveryHandler) {
for _, h := range handlers {
Expand Down Expand Up @@ -557,7 +582,7 @@ func validateBasicTxMsgs(msgs []sdk.Msg) error {
return nil
}

// Returns the applications's deliverState if app is in runTxModeDeliver,
// GetState returns the applications's deliverState if app is in runTxModeDeliver,
// otherwise it returns the application's checkstate.
func (app *BaseApp) getState(mode runTxMode) *state {
if mode == runTxModeDeliver {
Expand Down
2 changes: 1 addition & 1 deletion baseapp/block_gas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestBaseApp_BlockGas(t *testing.T) {
encCfg.InterfaceRegistry.RegisterImplementations((*sdk.Msg)(nil),
&testdata.TestMsg{},
)
app = simapp.NewSimApp(log.NewNopLogger(), dbm.NewMemDB(), nil, true, map[int64]bool{}, "", 0, encCfg, simapp.EmptyAppOptions{}, routerOpt)
app = simapp.NewSimApp(log.NewNopLogger(), dbm.NewMemDB(), nil, true, "", 0, encCfg, simapp.EmptyAppOptions{}, routerOpt)
genState := simapp.GenesisStateWithSingleValidator(t, app)
stateBytes, err := tmjson.MarshalIndent(genState, "", " ")
require.NoError(t, err)
Expand Down
16 changes: 16 additions & 0 deletions baseapp/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/cosmos/cosmos-sdk/codec/types"
pruningtypes "github.com/cosmos/cosmos-sdk/pruning/types"
serverconfig "github.com/cosmos/cosmos-sdk/server/config"
"github.com/cosmos/cosmos-sdk/snapshots"
snapshottypes "github.com/cosmos/cosmos-sdk/snapshots/types"
"github.com/cosmos/cosmos-sdk/store"
Expand Down Expand Up @@ -80,6 +81,16 @@ func SetSnapshot(snapshotStore *snapshots.Store, opts snapshottypes.SnapshotOpti
return func(app *BaseApp) { app.SetSnapshot(snapshotStore, opts) }
}

// SetAppConfig sets the server/config.Config.
func SetAppConfig(config serverconfig.Config) func(*BaseApp) {
return func(app *BaseApp) { app.setAppConfig(config) }
}

// SetAppConfig sets the chain id.
func SetChainID(chainID string) func(*BaseApp) {
return func(app *BaseApp) { app.setChainID(chainID) }
}

func (app *BaseApp) SetName(name string) {
if app.sealed {
panic("SetName() on sealed BaseApp")
Expand Down Expand Up @@ -243,3 +254,8 @@ func (app *BaseApp) SetStreamingService(s StreamingService) {
// BaseApp will pass BeginBlock, DeliverTx, and EndBlock requests and responses to the streaming services to update their ABCI context
app.abciListeners = append(app.abciListeners, s)
}

// SetUpgradeChecker is used to set a upgrade checker from the upgrade module
func (app *BaseApp) SetUpgradeChecker(checker func(sdk.Context, string) bool) {
app.upgradeChecker = checker
}
6 changes: 3 additions & 3 deletions baseapp/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ func (app *BaseApp) SimDeliver(txEncoder sdk.TxEncoder, tx sdk.Tx) (sdk.GasInfo,
// Context with current {check, deliver}State of the app used by tests.
func (app *BaseApp) NewContext(isCheckTx bool, header tmproto.Header) sdk.Context {
if isCheckTx {
return sdk.NewContext(app.checkState.ms, header, true, app.logger).
return sdk.NewContext(app.checkState.ms, header, true, app.upgradeChecker, app.logger).
WithMinGasPrices(app.minGasPrices)
}

return sdk.NewContext(app.deliverState.ms, header, false, app.logger)
return sdk.NewContext(app.deliverState.ms, header, false, app.upgradeChecker, app.logger)
}

func (app *BaseApp) NewUncachedContext(isCheckTx bool, header tmproto.Header) sdk.Context {
return sdk.NewContext(app.cms, header, isCheckTx, app.logger)
return sdk.NewContext(app.cms, header, isCheckTx, app.upgradeChecker, app.logger)
}

func (app *BaseApp) GetContextForDeliverTx(txBytes []byte) sdk.Context {
Expand Down
9 changes: 8 additions & 1 deletion client/pruning/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/flags"
pruningtypes "github.com/cosmos/cosmos-sdk/pruning/types"
"github.com/cosmos/cosmos-sdk/server"
serverconfig "github.com/cosmos/cosmos-sdk/server/config"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
"github.com/cosmos/cosmos-sdk/store/rootmulti"
"github.com/tendermint/tendermint/libs/log"
Expand Down Expand Up @@ -62,7 +63,13 @@ func PruningCmd(appCreator servertypes.AppCreator) *cobra.Command {
}

logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
app := appCreator(logger, db, nil, vp)

config, err := serverconfig.GetConfig(vp)
if err != nil {
return err
}

app := appCreator(logger, db, nil, config, "", vp)
cms := app.CommitMultiStore()

rootMultiStore, ok := cms.(*rootmulti.Store)
Expand Down
9 changes: 1 addition & 8 deletions proto/cosmos/upgrade/v1beta1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,6 @@ service Query {
rpc ModuleVersions(QueryModuleVersionsRequest) returns (QueryModuleVersionsResponse) {
option (google.api.http).get = "/cosmos/upgrade/v1beta1/module_versions";
}

// Returns the account with authority to conduct upgrades
//
// Since: cosmos-sdk 0.46
rpc Authority(QueryAuthorityRequest) returns (QueryAuthorityResponse) {
option (google.api.http).get = "/cosmos/upgrade/v1beta1/authority";
}
}

// QueryCurrentPlanRequest is the request type for the Query/CurrentPlan RPC
Expand All @@ -52,7 +45,7 @@ message QueryCurrentPlanRequest {}
// method.
message QueryCurrentPlanResponse {
// plan is the current upgrade plan.
Plan plan = 1;
repeated Plan plan = 1;
}

// QueryCurrentPlanRequest is the request type for the Query/AppliedPlan RPC
Expand Down
56 changes: 0 additions & 56 deletions proto/cosmos/upgrade/v1beta1/tx.proto

This file was deleted.

46 changes: 2 additions & 44 deletions proto/cosmos/upgrade/v1beta1/upgrade.proto
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
syntax = "proto3";
package cosmos.upgrade.v1beta1;

import "google/protobuf/any.proto";
import "gogoproto/gogo.proto";
import "google/protobuf/timestamp.proto";
import "cosmos_proto/cosmos.proto";

option go_package = "github.com/cosmos/cosmos-sdk/x/upgrade/types";
option (gogoproto.goproto_getters_all) = false;
Expand All @@ -23,52 +20,13 @@ message Plan {
// reached and the software will exit.
string name = 1;

// Deprecated: Time based upgrades have been deprecated. Time based upgrade logic
// has been removed from the SDK.
// If this field is not empty, an error will be thrown.
google.protobuf.Timestamp time = 2 [deprecated = true, (gogoproto.stdtime) = true, (gogoproto.nullable) = false];

// The height at which the upgrade must be performed.
// Only used if Time is not set.
int64 height = 3;
int64 height = 2;

// Any application specific upgrade info to be included on-chain
// such as a git commit that validators could automatically upgrade to
string info = 4;

// Deprecated: UpgradedClientState field has been deprecated. IBC upgrade logic has been
// moved to the IBC module in the sub module 02-client.
// If this field is not empty, an error will be thrown.
google.protobuf.Any upgraded_client_state = 5 [deprecated = true];
}

// SoftwareUpgradeProposal is a gov Content type for initiating a software
// upgrade.
// Deprecated: This legacy proposal is deprecated in favor of Msg-based gov
// proposals, see MsgSoftwareUpgrade.
message SoftwareUpgradeProposal {
option deprecated = true;
option (cosmos_proto.implements_interface) = "cosmos.gov.v1beta1.Content";
option (gogoproto.equal) = true;
option (gogoproto.goproto_stringer) = false;

string title = 1;
string description = 2;
Plan plan = 3 [(gogoproto.nullable) = false];
}

// CancelSoftwareUpgradeProposal is a gov Content type for cancelling a software
// upgrade.
// Deprecated: This legacy proposal is deprecated in favor of Msg-based gov
// proposals, see MsgCancelUpgrade.
message CancelSoftwareUpgradeProposal {
option deprecated = true;
option (cosmos_proto.implements_interface) = "cosmos.gov.v1beta1.Content";
option (gogoproto.equal) = true;
option (gogoproto.goproto_stringer) = false;

string title = 1;
string description = 2;
string info = 3;
}

// ModuleVersion specifies a module and its consensus version.
Expand Down
11 changes: 11 additions & 0 deletions server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,18 @@ type StateSyncConfig struct {
SnapshotKeepRecent uint32 `mapstructure:"snapshot-keep-recent"`
}

// UpgradeConfig defines the upgrading configuration.
type UpgradeConfig struct {
Name string `mapstructure:"name"`
Height int64 `mapstructure:"height"`
Info string `mapstructure:"info"`
}

// Config defines the server's top level configuration
type Config struct {
BaseConfig `mapstructure:",squash"`

Upgrade []UpgradeConfig `mapstructure:"upgrade"`
// Telemetry defines the application telemetry configuration
Telemetry telemetry.Config `mapstructure:"telemetry"`
API APIConfig `mapstructure:"api"`
Expand Down Expand Up @@ -309,6 +317,8 @@ func GetConfig(v *viper.Viper) (Config, error) {
}
}

var upgrade []UpgradeConfig
v.UnmarshalKey("upgrade", &upgrade)
return Config{
BaseConfig: BaseConfig{
MinGasPrices: v.GetString("minimum-gas-prices"),
Expand All @@ -324,6 +334,7 @@ func GetConfig(v *viper.Viper) (Config, error) {
IAVLDisableFastNode: v.GetBool("iavl-disable-fastnode"),
AppDBBackend: v.GetString("app-db-backend"),
},
Upgrade: upgrade,
Telemetry: telemetry.Config{
ServiceName: v.GetString("telemetry.service-name"),
Enabled: v.GetBool("telemetry.enabled"),
Expand Down
15 changes: 15 additions & 0 deletions server/config/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,21 @@ iavl-disable-fastnode = {{ .BaseConfig.IAVLDisableFastNode }}
# Second fallback (if the types.DBBackend also isn't set), is the db-backend value set in Tendermint's config.toml.
app-db-backend = "{{ .BaseConfig.AppDBBackend }}"
###############################################################################
### Upgrade Configuration ###
###############################################################################
# Example:
# [[upgrade]]
# name = "BEP111"
# height = 100
# info = "https://github.com/bnb-chain/BEPs/pull/111"
# [[upgrade]]
# name = "BEP112"
# height = 120
# info = "https://github.com/bnb-chain/BEPs/pull/112"
###############################################################################
### Telemetry Configuration ###
###############################################################################
Expand Down
Loading

0 comments on commit 4c7b018

Please sign in to comment.