Skip to content

Commit

Permalink
Son/iavl v1 (#494)
Browse files Browse the repository at this point in the history
* fix newMutableTree

* replace log and dbm

* migrate to cosmos ics23

* fix test

* fix test

* fix logger for test

* fix conflict

* fix test

* fix unit test
  • Loading branch information
sontrinh16 authored Nov 15, 2023
1 parent ae63edb commit 4720227
Show file tree
Hide file tree
Showing 101 changed files with 696 additions and 720 deletions.
11 changes: 6 additions & 5 deletions baseapp/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/cosmos/gogoproto/jsonpb"
"github.com/stretchr/testify/require"

"cosmossdk.io/log"
"github.com/cosmos/cosmos-sdk/baseapp"
baseapptestutil "github.com/cosmos/cosmos-sdk/baseapp/testutil"
"github.com/cosmos/cosmos-sdk/snapshots"
Expand Down Expand Up @@ -43,7 +44,7 @@ func TestABCI_Info(t *testing.T) {
func TestABCI_InitChain(t *testing.T) {
name := t.Name()
db := dbm.NewMemDB()
logger := defaultLogger()
logger := log.NewTestLogger(t)
app := baseapp.NewBaseApp(name, logger, db, nil, baseapp.SetChainID("test-chain-id"))

capKey := sdk.NewKVStoreKey("main")
Expand Down Expand Up @@ -128,7 +129,7 @@ func TestABCI_InitChain(t *testing.T) {
func TestABCI_InitChain_WithInitialHeight(t *testing.T) {
name := t.Name()
db := dbm.NewMemDB()
logger := defaultLogger()
logger := log.NewTestLogger(t)
app := baseapp.NewBaseApp(name, logger, db, nil)

app.InitChain(
Expand All @@ -144,7 +145,7 @@ func TestABCI_InitChain_WithInitialHeight(t *testing.T) {
func TestABCI_BeginBlock_WithInitialHeight(t *testing.T) {
name := t.Name()
db := dbm.NewMemDB()
logger := defaultLogger()
logger := log.NewTestLogger(t)
app := baseapp.NewBaseApp(name, logger, db, nil)

app.InitChain(
Expand Down Expand Up @@ -568,7 +569,7 @@ func TestABCI_ApplySnapshotChunk(t *testing.T) {
func TestABCI_EndBlock(t *testing.T) {
db := dbm.NewMemDB()
name := t.Name()
logger := defaultLogger()
logger := log.NewTestLogger(t)

cp := &tmproto.ConsensusParams{
Block: &tmproto.BlockParams{
Expand Down Expand Up @@ -1213,7 +1214,7 @@ func TestABCI_Query(t *testing.T) {
}

func TestABCI_GetBlockRetentionHeight(t *testing.T) {
logger := defaultLogger()
logger := log.NewTestLogger(t)
db := dbm.NewMemDB()
name := t.Name()

Expand Down
21 changes: 8 additions & 13 deletions baseapp/baseapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,9 @@ func NewBaseAppSuite(t *testing.T, opts ...func(*baseapp.BaseApp)) *BaseAppSuite
baseapptestutil.RegisterInterfaces(cdc.InterfaceRegistry())

txConfig := authtx.NewTxConfig(cdc, authtx.DefaultSignModes)
logger := defaultLogger()
db := dbm.NewMemDB()

app := baseapp.NewBaseApp(t.Name(), logger, db, txConfig.TxDecoder(), opts...)
app := baseapp.NewBaseApp(t.Name(), log.NewTestLogger(t), db, txConfig.TxDecoder(), opts...)
require.Equal(t, t.Name(), app.Name())

app.SetInterfaceRegistry(cdc.InterfaceRegistry())
Expand Down Expand Up @@ -158,11 +157,10 @@ func NewBaseAppSuiteWithSnapshots(t *testing.T, cfg SnapshotsConfig, opts ...fun
}

func TestLoadVersion(t *testing.T) {
logger := defaultLogger()
pruningOpt := baseapp.SetPruning(pruningtypes.NewPruningOptions(pruningtypes.PruningNothing))
db := dbm.NewMemDB()
name := t.Name()
app := baseapp.NewBaseApp(name, logger, db, nil, pruningOpt)
app := baseapp.NewBaseApp(name, log.NewTestLogger(t), db, nil, pruningOpt)

// make a cap key and mount the store
err := app.LoadLatestVersion() // needed to make stores non-nil
Expand All @@ -189,7 +187,7 @@ func TestLoadVersion(t *testing.T) {
commitID2 := storetypes.CommitID{Version: 2, Hash: res.Data}

// reload with LoadLatestVersion
app = baseapp.NewBaseApp(name, logger, db, nil, pruningOpt)
app = baseapp.NewBaseApp(name, log.NewTestLogger(t), db, nil, pruningOpt)
app.MountStores()

err = app.LoadLatestVersion()
Expand All @@ -199,7 +197,7 @@ func TestLoadVersion(t *testing.T) {

// Reload with LoadVersion, see if you can commit the same block and get
// the same result.
app = baseapp.NewBaseApp(name, logger, db, nil, pruningOpt)
app = baseapp.NewBaseApp(name, log.NewTestLogger(t), db, nil, pruningOpt)
err = app.LoadVersion(1)
require.Nil(t, err)

Expand Down Expand Up @@ -283,7 +281,7 @@ func TestSetLoader(t *testing.T) {
if tc.setLoader != nil {
opts = append(opts, tc.setLoader)
}
app := baseapp.NewBaseApp(t.Name(), defaultLogger(), db, nil, opts...)
app := baseapp.NewBaseApp(t.Name(), log.NewTestLogger(t), db, nil, opts...)
app.MountStores(sdk.NewKVStoreKey(tc.loadStoreKey))
err := app.LoadLatestVersion()
require.Nil(t, err)
Expand All @@ -301,11 +299,10 @@ func TestSetLoader(t *testing.T) {
}

func TestVersionSetterGetter(t *testing.T) {
logger := defaultLogger()
pruningOpt := baseapp.SetPruning(pruningtypes.NewPruningOptions(pruningtypes.PruningDefault))
db := dbm.NewMemDB()
name := t.Name()
app := baseapp.NewBaseApp(name, logger, db, nil, pruningOpt)
app := baseapp.NewBaseApp(name, log.NewTestLogger(t), db, nil, pruningOpt)

require.Equal(t, "", app.Version())
res := app.Query(abci.RequestQuery{Path: "app/version"})
Expand Down Expand Up @@ -360,9 +357,8 @@ func TestOptionFunction(t *testing.T) {
}
}

logger := defaultLogger()
db := dbm.NewMemDB()
bap := baseapp.NewBaseApp("starting name", logger, db, nil, testChangeNameHelper("new name"))
bap := baseapp.NewBaseApp("starting name", log.NewTestLogger(t), db, nil, testChangeNameHelper("new name"))
require.Equal(t, bap.Name(), "new name", "BaseApp should have had name changed via option function")
}

Expand Down Expand Up @@ -542,10 +538,9 @@ func TestBaseAppAnteHandler(t *testing.T) {
func TestABCI_CreateQueryContext(t *testing.T) {
t.Parallel()

logger := defaultLogger()
db := dbm.NewMemDB()
name := t.Name()
app := baseapp.NewBaseApp(name, logger, db, nil)
app := baseapp.NewBaseApp(name, log.NewTestLogger(t), db, nil)

app.BeginBlock(abci.RequestBeginBlock{Header: tmproto.Header{Height: 1}})
app.Commit()
Expand Down
7 changes: 4 additions & 3 deletions baseapp/block_gas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"math"
"testing"

"cosmossdk.io/log"
abci "github.com/cometbft/cometbft/abci/types"
tmjson "github.com/cometbft/cometbft/libs/json"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
Expand All @@ -15,6 +14,7 @@ import (

"cosmossdk.io/depinject"

"cosmossdk.io/log"
baseapptestutil "github.com/cosmos/cosmos-sdk/baseapp/testutil"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/tx"
Expand Down Expand Up @@ -81,7 +81,8 @@ func TestBaseApp_BlockGas(t *testing.T) {
err error
)

appConfig := depinject.Configs(makeTestConfig())
appConfig := depinject.Configs(
makeTestConfig(), depinject.Supply(log.NewNopLogger()))

err = depinject.Inject(appConfig,
&bankKeeper,
Expand All @@ -93,7 +94,7 @@ func TestBaseApp_BlockGas(t *testing.T) {
&appBuilder)
require.NoError(t, err)

bapp := appBuilder.Build(log.NewNopLogger(), dbm.NewMemDB(), nil)
bapp := appBuilder.Build(dbm.NewMemDB(), nil)
err = bapp.Load(true)
require.NoError(t, err)

Expand Down
11 changes: 7 additions & 4 deletions baseapp/grpcrouter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ package baseapp_test

import (
"context"
"os"
"sync"
"testing"

"cosmossdk.io/log"
dbm "github.com/cosmos/cosmos-db"
"github.com/stretchr/testify/require"

"cosmossdk.io/depinject"
"cosmossdk.io/log"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/runtime"
Expand Down Expand Up @@ -56,10 +55,14 @@ func TestGRPCQueryRouter(t *testing.T) {
func TestRegisterQueryServiceTwice(t *testing.T) {
// Setup baseapp.
var appBuilder *runtime.AppBuilder
err := depinject.Inject(makeMinimalConfig(), &appBuilder)
err := depinject.Inject(
depinject.Configs(
makeMinimalConfig(),
depinject.Supply(log.NewNopLogger()),
), &appBuilder)
require.NoError(t, err)
db := dbm.NewMemDB()
app := appBuilder.Build(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil)
app := appBuilder.Build(db, nil)

// First time registering service shouldn't panic.
require.NotPanics(t, func() {
Expand Down
27 changes: 19 additions & 8 deletions baseapp/msg_service_router_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package baseapp_test

import (
"os"
"testing"

"cosmossdk.io/log"
abci "github.com/cometbft/cometbft/abci/types"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
dbm "github.com/cosmos/cosmos-db"
"github.com/stretchr/testify/require"

"cosmossdk.io/depinject"
"cosmossdk.io/log"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
Expand All @@ -27,9 +26,13 @@ func TestRegisterMsgService(t *testing.T) {
appBuilder *runtime.AppBuilder
registry codectypes.InterfaceRegistry
)
err := depinject.Inject(makeMinimalConfig(), &appBuilder, &registry)
err := depinject.Inject(
depinject.Configs(
makeMinimalConfig(),
depinject.Supply(log.NewNopLogger()),
), &appBuilder, &registry)
require.NoError(t, err)
app := appBuilder.Build(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), dbm.NewMemDB(), nil)
app := appBuilder.Build(dbm.NewMemDB(), nil)

require.Panics(t, func() {
testdata.RegisterMsgServer(
Expand All @@ -55,10 +58,14 @@ func TestRegisterMsgServiceTwice(t *testing.T) {
appBuilder *runtime.AppBuilder
registry codectypes.InterfaceRegistry
)
err := depinject.Inject(makeMinimalConfig(), &appBuilder, &registry)
err := depinject.Inject(
depinject.Configs(
makeMinimalConfig(),
depinject.Supply(log.NewNopLogger()),
), &appBuilder, &registry)
require.NoError(t, err)
db := dbm.NewMemDB()
app := appBuilder.Build(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil)
app := appBuilder.Build(db, nil)
testdata.RegisterInterfaces(registry)

// First time registering service shouldn't panic.
Expand Down Expand Up @@ -86,9 +93,13 @@ func TestMsgService(t *testing.T) {
cdc codec.ProtoCodecMarshaler
interfaceRegistry codectypes.InterfaceRegistry
)
err := depinject.Inject(makeMinimalConfig(), &appBuilder, &cdc, &interfaceRegistry)
err := depinject.Inject(
depinject.Configs(
makeMinimalConfig(),
depinject.Supply(log.NewNopLogger()),
), &appBuilder, &cdc, &interfaceRegistry)
require.NoError(t, err)
app := appBuilder.Build(log.NewNopLogger(), dbm.NewMemDB(), nil)
app := appBuilder.Build(dbm.NewMemDB(), nil)

// patch in TxConfig instead of using an output from x/auth/tx
txConfig := authtx.NewTxConfig(cdc, authtx.DefaultSignModes)
Expand Down
10 changes: 0 additions & 10 deletions baseapp/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"errors"
"fmt"
"net/url"
"os"
"reflect"
"strconv"
"testing"
Expand All @@ -25,7 +24,6 @@ import (
txconfigv1 "cosmossdk.io/api/cosmos/tx/config/v1"
"cosmossdk.io/core/appconfig"
"cosmossdk.io/depinject"
"cosmossdk.io/log"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
tmtypes "github.com/cometbft/cometbft/types"
dbm "github.com/cosmos/cosmos-db"
Expand Down Expand Up @@ -62,14 +60,6 @@ var (
ParamStoreKey = []byte("paramstore")
)

func defaultLogger() log.Logger {
if testing.Verbose() {
return log.NewTMLogger(log.NewSyncWriter(os.Stdout)).With("module", "baseapp/test")
}

return log.NewNopLogger()
}

// GenesisStateWithSingleValidator initializes GenesisState with a single validator and genesis accounts
// that also act as delegators.
func GenesisStateWithSingleValidator(t *testing.T, codec codec.Codec, builder *runtime.AppBuilder) map[string]json.RawMessage {
Expand Down
9 changes: 6 additions & 3 deletions client/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"testing"

"cosmossdk.io/log"
abci "github.com/cometbft/cometbft/abci/types"
tmjson "github.com/cometbft/cometbft/libs/json"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
Expand All @@ -14,6 +13,7 @@ import (
"google.golang.org/grpc/metadata"

"cosmossdk.io/depinject"
"cosmossdk.io/log"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
Expand Down Expand Up @@ -51,10 +51,13 @@ func (s *IntegrationTestSuite) SetupSuite() {
// TODO duplicated from testutils/sims/app_helpers.go
// need more composable startup options for simapp, this test needed a handle to the closed over genesis account
// to query balances
err := depinject.Inject(testutil.AppConfig, &interfaceRegistry, &bankKeeper, &appBuilder, &cdc)
err := depinject.Inject(depinject.Configs(
testutil.AppConfig,
depinject.Supply(log.NewNopLogger()),
), &interfaceRegistry, &bankKeeper, &appBuilder, &cdc)
s.NoError(err)

app := appBuilder.Build(log.NewNopLogger(), dbm.NewMemDB(), nil)
app := appBuilder.Build(dbm.NewMemDB(), nil)
err = app.Load(true)
s.NoError(err)

Expand Down
18 changes: 4 additions & 14 deletions client/pruning/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package pruning

import (
"fmt"
"os"
"path/filepath"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -80,7 +79,7 @@ Supported app-db-backend types include 'goleveldb', 'rocksdb', 'pebbledb'.`,
return err
}

logger := log.NewTMLogger(log.NewSyncWriter(os.Stdout))
logger := log.NewLogger(cmd.OutOrStdout())
app := appCreator(logger, db, nil, vp)
cms := app.CommitMultiStore()

Expand All @@ -94,19 +93,10 @@ Supported app-db-backend types include 'goleveldb', 'rocksdb', 'pebbledb'.`,
return fmt.Errorf("the database has no valid heights to prune, the latest height: %v", latestHeight)
}

var pruningHeights []int64
for height := int64(1); height < latestHeight; height++ {
if height < latestHeight-int64(pruningOptions.KeepRecent) {
pruningHeights = append(pruningHeights, height)
}
}
if len(pruningHeights) == 0 {
cmd.Println("no heights to prune")
return nil
}
cmd.Printf("pruning heights start from %v, end at %v\n", pruningHeights[0], pruningHeights[len(pruningHeights)-1])
pruningHeight := latestHeight - int64(pruningOptions.KeepRecent)
cmd.Printf("pruning heights up to %v\n", pruningHeight)

if err = rootMultiStore.PruneStores(false, pruningHeights); err != nil {
if err = rootMultiStore.PruneStores(pruningHeight); err != nil {
return err
}

Expand Down
Loading

0 comments on commit 4720227

Please sign in to comment.