Skip to content

Commit

Permalink
feat: apply the changes of lbm-sdk v0.46.0-rc8 (#95)
Browse files Browse the repository at this point in the history
* feat: apply the changes of lbm-sdk v0.46.0-rc8 from v0.46.0-rc6
- and add app_test

Signed-off-by: zemyblue <zemyblue@gmail.com>

* fix: fix lint warning and add changelog

Signed-off-by: zemyblue <zemyblue@gmail.com>

* fix: lint warning

Signed-off-by: zemyblue <zemyblue@gmail.com>

* fix: lint warning

Signed-off-by: zemyblue <zemyblue@gmail.com>

Signed-off-by: zemyblue <zemyblue@gmail.com>
  • Loading branch information
zemyblue authored Sep 15, 2022
1 parent 8d5195f commit 54fb309
Show file tree
Hide file tree
Showing 11 changed files with 495 additions and 593 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Bug Fixes
* [\#83](https://github.com/line/lbm/pull/83) enable tests on CI

### Improvements
* [\#95](https://github.com/line/lbm/pull/95) apply the changes of lbm-sdk v0.46.0-rc8

### Breaking Changes
* [\#87](https://github.com/line/lbm/pull/87) remove unused modules from app

Expand Down
46 changes: 28 additions & 18 deletions app/app.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package app

import (
"fmt"

"io"
stdlog "log"
"net/http"
Expand Down Expand Up @@ -92,7 +94,8 @@ import (
upgradetypes "github.com/line/lbm-sdk/x/upgrade/types"
"github.com/line/lbm-sdk/x/wasm"
wasmclient "github.com/line/lbm-sdk/x/wasm/client"

wasmkeeper "github.com/line/lbm-sdk/x/wasm/keeper"
wasmlbmtypes "github.com/line/lbm-sdk/x/wasm/lbmtypes"
appparams "github.com/line/lbm/app/params"

// unnamed import of statik for swagger UI support
Expand Down Expand Up @@ -201,6 +204,9 @@ type LinkApp struct { // nolint: golint

// simulation manager
sm *module.SimulationManager

// the configurator
configurator module.Configurator
}

func init() {
Expand Down Expand Up @@ -354,7 +360,7 @@ func NewLinkApp(
AddRoute(distrtypes.RouterKey, distr.NewCommunityPoolSpendProposalHandler(app.DistrKeeper)).
AddRoute(upgradetypes.RouterKey, upgrade.NewSoftwareUpgradeProposalHandler(app.UpgradeKeeper)).
AddRoute(foundation.RouterKey, foundationkeeper.NewProposalHandler(app.FoundationKeeper)).
AddRoute(wasm.RouterKey, wasm.NewWasmProposalHandler(app.WasmKeeper, wasm.EnableAllProposals))
AddRoute(wasm.RouterKey, wasmkeeper.NewWasmProposalHandler(app.WasmKeeper, wasmlbmtypes.EnableAllProposals))

govKeeper := govkeeper.NewKeeper(
appCodec, keys[govtypes.StoreKey], app.GetSubspace(govtypes.ModuleName), app.AccountKeeper, app.BankKeeper,
Expand Down Expand Up @@ -482,27 +488,18 @@ func NewLinkApp(

app.mm.RegisterInvariants(&app.CrisisKeeper)
app.mm.RegisterRoutes(app.Router(), app.QueryRouter(), encodingConfig.Amino)
app.mm.RegisterServices(module.NewConfigurator(app.appCodec, app.MsgServiceRouter(), app.GRPCQueryRouter()))
app.configurator = module.NewConfigurator(app.appCodec, app.MsgServiceRouter(), app.GRPCQueryRouter())
app.mm.RegisterServices(app.configurator)

// create the simulation manager and define the order of the modules for deterministic simulations
//
// NOTE: this is not required apps that don't use the simulator for fuzz testing
// transactions
app.sm = module.NewSimulationManager(
auth.NewAppModule(appCodec, app.AccountKeeper, authsims.RandomGenesisAccounts),
bankplus.NewAppModule(appCodec, app.BankKeeper, app.AccountKeeper),
capability.NewAppModule(appCodec, *app.CapabilityKeeper),
feegrantmodule.NewAppModule(appCodec, app.AccountKeeper, app.BankKeeper, app.FeeGrantKeeper, app.interfaceRegistry),
gov.NewAppModule(appCodec, app.GovKeeper, app.AccountKeeper, app.BankKeeper),
mint.NewAppModule(appCodec, app.MintKeeper, app.AccountKeeper),
staking.NewAppModule(appCodec, app.StakingKeeper, app.AccountKeeper, app.BankKeeper),
distr.NewAppModule(appCodec, app.DistrKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper),
slashing.NewAppModule(appCodec, app.SlashingKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper),
params.NewAppModule(app.ParamsKeeper),
wasm.NewAppModule(appCodec, &app.WasmKeeper, app.StakingKeeper, app.AccountKeeper, app.BankKeeper),
evidence.NewAppModule(app.EvidenceKeeper),
authzmodule.NewAppModule(appCodec, app.AuthzKeeper, app.AccountKeeper, app.BankKeeper, app.interfaceRegistry),
)
overrideModules := map[string]module.AppModuleSimulation{
authtypes.ModuleName: auth.NewAppModule(app.appCodec, app.AccountKeeper, authsims.RandomGenesisAccounts),
stakingtypes.ModuleName: staking.NewAppModule(app.appCodec, app.StakingKeeper, app.AccountKeeper, app.BankKeeper),
}
app.sm = module.NewSimulationManagerFromAppModules(app.mm.Modules, overrideModules)

app.sm.RegisterStoreDecoders()

Expand Down Expand Up @@ -530,6 +527,18 @@ func NewLinkApp(
app.SetAnteHandler(anteHandler)
app.SetEndBlocker(app.EndBlocker)

// must be before loading version
// requires the snapshot store to be created and registered as a BaseAppOptions
// see cmd/lbm/cmd/root.go: 257-265
if manager := app.SnapshotManager(); manager != nil {
err := manager.RegisterExtensions(
wasmkeeper.NewWasmSnapshotter(app.CommitMultiStore(), &app.WasmKeeper),
)
if err != nil {
panic(fmt.Errorf("failed to register snapshot extension: %s", err))
}
}

if loadLatest {
if err := app.LoadLatestVersion(); err != nil {
ostos.Exit(err.Error())
Expand Down Expand Up @@ -576,6 +585,7 @@ func (app *LinkApp) InitChainer(ctx sdk.Context, req abci.RequestInitChain) abci
if err := ostjson.Unmarshal(req.AppStateBytes, &genesisState); err != nil {
panic(err)
}
app.UpgradeKeeper.SetModuleVersionMap(ctx, app.mm.GetVersionMap())
return app.mm.InitGenesis(ctx, app.appCodec, genesisState)
}

Expand Down
262 changes: 262 additions & 0 deletions app/app_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
package app

import (
"encoding/json"
"os"
"testing"

"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"

abci "github.com/line/ostracon/abci/types"
"github.com/line/ostracon/libs/log"
ocproto "github.com/line/ostracon/proto/ostracon/types"
dbm "github.com/tendermint/tm-db"

"github.com/line/lbm-sdk/baseapp"
"github.com/line/lbm-sdk/simapp"
"github.com/line/lbm-sdk/tests/mocks"
sdk "github.com/line/lbm-sdk/types"
"github.com/line/lbm-sdk/types/module"
"github.com/line/lbm-sdk/x/auth"
"github.com/line/lbm-sdk/x/auth/vesting"
authz_m "github.com/line/lbm-sdk/x/authz/module"
"github.com/line/lbm-sdk/x/bank"
banktypes "github.com/line/lbm-sdk/x/bank/types"
"github.com/line/lbm-sdk/x/capability"
"github.com/line/lbm-sdk/x/crisis"
"github.com/line/lbm-sdk/x/distribution"
"github.com/line/lbm-sdk/x/evidence"
feegrantmodule "github.com/line/lbm-sdk/x/feegrant/module"
foundationmodule "github.com/line/lbm-sdk/x/foundation/module"
"github.com/line/lbm-sdk/x/genutil"
"github.com/line/lbm-sdk/x/gov"
"github.com/line/lbm-sdk/x/ibc/applications/transfer"
ibc "github.com/line/lbm-sdk/x/ibc/core"
"github.com/line/lbm-sdk/x/mint"
"github.com/line/lbm-sdk/x/params"
"github.com/line/lbm-sdk/x/slashing"
"github.com/line/lbm-sdk/x/staking"
tokenmodule "github.com/line/lbm-sdk/x/token/module"
"github.com/line/lbm-sdk/x/upgrade"
)

func TestSimAppExportAndBlockedAddrs(t *testing.T) {
encCfg := MakeEncodingConfig()
db := dbm.NewMemDB()
app := NewLinkApp(log.NewOCLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, encCfg, simapp.EmptyAppOptions{}, nil)

for acc := range maccPerms {
require.Equal(t, !allowedReceivingModAcc[acc], app.BankKeeper.BlockedAddr(app.AccountKeeper.GetModuleAddress(acc)),
"ensure that blocked addresses are properly set in bank keeper")
}

genesisState := NewDefaultGenesisState(encCfg.Marshaler)
stateBytes, err := json.MarshalIndent(genesisState, "", " ")
require.NoError(t, err)

// Initialize the chain
app.InitChain(
abci.RequestInitChain{
Validators: []abci.ValidatorUpdate{},
AppStateBytes: stateBytes,
},
)
app.Commit()

// Making a new app object with the db, so that initchain hasn't been called
app2 := NewLinkApp(log.NewOCLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, encCfg, simapp.EmptyAppOptions{}, nil)
_, err = app2.ExportAppStateAndValidators(false, []string{})
require.NoError(t, err, "ExportAppStateAndValidators should not have an error")
}

func TestGetMaccPerms(t *testing.T) {
dup := GetMaccPerms()
require.Equal(t, maccPerms, dup, "duplicated module account permissions differed from actual module account permissions")
}

func TestRunMigrations(t *testing.T) {
db := dbm.NewMemDB()
encCfg := MakeEncodingConfig()
logger := log.NewOCLogger(log.NewSyncWriter(os.Stdout))
app := NewLinkApp(logger, db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, encCfg, simapp.EmptyAppOptions{}, nil)

// Create a new baseapp and configurator for the purpose of this test.
bApp := baseapp.NewBaseApp(appName, logger, db, encCfg.TxConfig.TxDecoder())
bApp.SetCommitMultiStoreTracer(nil)
bApp.SetInterfaceRegistry(encCfg.InterfaceRegistry)
app.BaseApp = bApp
app.configurator = module.NewConfigurator(app.appCodec, app.MsgServiceRouter(), app.GRPCQueryRouter())

// We register all modules on the Configurator, except x/bank. x/bank will
// serve as the test subject on which we run the migration tests.
//
// The loop below is the same as calling `RegisterServices` on
// ModuleManager, except that we skip x/bank.
for _, module := range app.mm.Modules {
if module.Name() == banktypes.ModuleName {
continue
}

module.RegisterServices(app.configurator)
}

// Initialize the chain
app.InitChain(abci.RequestInitChain{})
app.Commit()

testCases := []struct {
name string
moduleName string
expRegErrMsg string
expRunErrMsg string
forVersion uint64
expCalled int
expRegErr bool // errors while registering migration
expRunErr bool // errors while running migration
}{
{
"cannot register migration for version 0",
"bank", "module migration versions should start at 1: invalid version", "",
0, 0,
true, false,
},
}

for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
var err error

// Since it's very hard to test actual in-place store migrations in
// tests (due to the difficulty of maintaining multiple versions of a
// module), we're just testing here that the migration logic is
// called.
called := 0

if tc.moduleName != "" {
// Register migration for module from version `forVersion` to `forVersion+1`.
err = app.configurator.RegisterMigration(tc.moduleName, tc.forVersion, func(sdk.Context) error {
called++

return nil
})

if tc.expRegErr {
require.EqualError(t, err, tc.expRegErrMsg)

return
}
}
require.NoError(t, err)

// Run migrations only for bank. That's why we put the initial
// version for bank as 1, and for all other modules, we put as
// their latest ConsensusVersion.
_, err = app.mm.RunMigrations(
app.NewContext(true, ocproto.Header{Height: app.LastBlockHeight()}), app.configurator,
module.VersionMap{
"bank": 1,
"auth": auth.AppModule{}.ConsensusVersion(),
"authz": authz_m.AppModule{}.ConsensusVersion(),
"staking": staking.AppModule{}.ConsensusVersion(),
"mint": mint.AppModule{}.ConsensusVersion(),
"distribution": distribution.AppModule{}.ConsensusVersion(),
"slashing": slashing.AppModule{}.ConsensusVersion(),
"gov": gov.AppModule{}.ConsensusVersion(),
"params": params.AppModule{}.ConsensusVersion(),
"ibc": ibc.AppModule{}.ConsensusVersion(),
"upgrade": upgrade.AppModule{}.ConsensusVersion(),
"vesting": vesting.AppModule{}.ConsensusVersion(),
"feegrant": feegrantmodule.AppModule{}.ConsensusVersion(),
"transfer": transfer.AppModule{}.ConsensusVersion(),
"evidence": evidence.AppModule{}.ConsensusVersion(),
"crisis": crisis.AppModule{}.ConsensusVersion(),
"genutil": genutil.AppModule{}.ConsensusVersion(),
"capability": capability.AppModule{}.ConsensusVersion(),
"foundation": foundationmodule.AppModule{}.ConsensusVersion(),
"token": tokenmodule.AppModule{}.ConsensusVersion(),
},
)
if tc.expRunErr {
require.EqualError(t, err, tc.expRunErrMsg)
} else {
require.NoError(t, err)
// Make sure bank's migration is called.
require.Equal(t, tc.expCalled, called)
}
})
}
}

func TestInitGenesisOnMigration(t *testing.T) {
db := dbm.NewMemDB()
encCfg := MakeEncodingConfig()
logger := log.NewOCLogger(log.NewSyncWriter(os.Stdout))
app := NewLinkApp(logger, db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, encCfg, simapp.EmptyAppOptions{}, nil)
ctx := app.NewContext(true, ocproto.Header{Height: app.LastBlockHeight()})

// Create a mock module. This module will serve as the new module we're
// adding during a migration.
mockCtrl := gomock.NewController(t)
t.Cleanup(mockCtrl.Finish)
mockModule := mocks.NewMockAppModule(mockCtrl)
mockDefaultGenesis := json.RawMessage(`{"key": "value"}`)
mockModule.EXPECT().DefaultGenesis(gomock.Eq(app.appCodec)).Times(1).Return(mockDefaultGenesis)
mockModule.EXPECT().InitGenesis(gomock.Eq(ctx), gomock.Eq(app.appCodec), gomock.Eq(mockDefaultGenesis)).Times(1).Return(nil)
mockModule.EXPECT().ConsensusVersion().Times(1).Return(uint64(0))

app.mm.Modules["mock"] = mockModule

// Run migrations only for "mock" module. We exclude it from
// the VersionMap to simulate upgrading with a new module.
_, err := app.mm.RunMigrations(ctx, app.configurator,
module.VersionMap{
"bank": bank.AppModule{}.ConsensusVersion(),
"auth": auth.AppModule{}.ConsensusVersion(),
"authz": authz_m.AppModule{}.ConsensusVersion(),
"staking": staking.AppModule{}.ConsensusVersion(),
"mint": mint.AppModule{}.ConsensusVersion(),
"distribution": distribution.AppModule{}.ConsensusVersion(),
"slashing": slashing.AppModule{}.ConsensusVersion(),
"gov": gov.AppModule{}.ConsensusVersion(),
"params": params.AppModule{}.ConsensusVersion(),
"ibc": ibc.AppModule{}.ConsensusVersion(),
"upgrade": upgrade.AppModule{}.ConsensusVersion(),
"vesting": vesting.AppModule{}.ConsensusVersion(),
"transfer": transfer.AppModule{}.ConsensusVersion(),
"evidence": evidence.AppModule{}.ConsensusVersion(),
"crisis": crisis.AppModule{}.ConsensusVersion(),
"genutil": genutil.AppModule{}.ConsensusVersion(),
"capability": capability.AppModule{}.ConsensusVersion(),
"foundation": foundationmodule.AppModule{}.ConsensusVersion(),
"token": tokenmodule.AppModule{}.ConsensusVersion(),
},
)

require.NoError(t, err)
}

func TestUpgradeStateOnGenesis(t *testing.T) {
encCfg := MakeEncodingConfig()
db := dbm.NewMemDB()
app := NewLinkApp(log.NewOCLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{}, DefaultNodeHome, 0, encCfg, simapp.EmptyAppOptions{}, nil)
genesisState := NewDefaultGenesisState(encCfg.Marshaler)
stateBytes, err := json.MarshalIndent(genesisState, "", " ")
require.NoError(t, err)

// Initialize the chain
app.InitChain(
abci.RequestInitChain{
Validators: []abci.ValidatorUpdate{},
AppStateBytes: stateBytes,
},
)

// make sure the upgrade keeper has version map in state
ctx := app.NewContext(false, ocproto.Header{})
vm := app.UpgradeKeeper.GetModuleVersionMap(ctx)
for v, i := range app.mm.Modules {
require.Equal(t, vm[v], i.ConsensusVersion())
}
}
Loading

0 comments on commit 54fb309

Please sign in to comment.