Skip to content

Commit

Permalink
feat: replace the cosmos-db usecases in the tests with core/testing (
Browse files Browse the repository at this point in the history
  • Loading branch information
cool-develope authored Sep 13, 2024
1 parent 7d6ff0d commit aaf72f2
Show file tree
Hide file tree
Showing 96 changed files with 1,849 additions and 613 deletions.
38 changes: 19 additions & 19 deletions baseapp/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
cmtproto "github.com/cometbft/cometbft/api/cometbft/types/v1"
"github.com/cometbft/cometbft/crypto/secp256k1"
cmttypes "github.com/cometbft/cometbft/types"
dbm "github.com/cosmos/cosmos-db"
protoio "github.com/cosmos/gogoproto/io"
"github.com/cosmos/gogoproto/jsonpb"
"github.com/cosmos/gogoproto/proto"
Expand All @@ -28,6 +27,7 @@ import (
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"

coretesting "cosmossdk.io/core/testing"
errorsmod "cosmossdk.io/errors"
"cosmossdk.io/log"
pruningtypes "cosmossdk.io/store/pruning/types"
Expand Down Expand Up @@ -105,7 +105,7 @@ func TestABCI_First_block_Height(t *testing.T) {

func TestABCI_InitChain(t *testing.T) {
name := t.Name()
db := dbm.NewMemDB()
db := coretesting.NewMemDB()
logger := log.NewTestLogger(t)
app := baseapp.NewBaseApp(name, logger, db, nil, baseapp.SetChainID("test-chain-id"))

Expand Down Expand Up @@ -205,7 +205,7 @@ func TestABCI_InitChain(t *testing.T) {

func TestABCI_InitChain_WithInitialHeight(t *testing.T) {
name := t.Name()
db := dbm.NewMemDB()
db := coretesting.NewMemDB()
app := baseapp.NewBaseApp(name, log.NewTestLogger(t), db, nil)

_, err := app.InitChain(
Expand All @@ -222,7 +222,7 @@ func TestABCI_InitChain_WithInitialHeight(t *testing.T) {

func TestABCI_FinalizeBlock_WithInitialHeight(t *testing.T) {
name := t.Name()
db := dbm.NewMemDB()
db := coretesting.NewMemDB()
app := baseapp.NewBaseApp(name, log.NewTestLogger(t), db, nil)

_, err := app.InitChain(
Expand All @@ -244,7 +244,7 @@ func TestABCI_FinalizeBlock_WithInitialHeight(t *testing.T) {

func TestABCI_FinalizeBlock_WithBeginAndEndBlocker(t *testing.T) {
name := t.Name()
db := dbm.NewMemDB()
db := coretesting.NewMemDB()
app := baseapp.NewBaseApp(name, log.NewTestLogger(t), db, nil)

app.SetBeginBlocker(func(ctx sdk.Context) (sdk.BeginBlock, error) {
Expand Down Expand Up @@ -311,7 +311,7 @@ func TestABCI_FinalizeBlock_WithBeginAndEndBlocker(t *testing.T) {

func TestABCI_ExtendVote(t *testing.T) {
name := t.Name()
db := dbm.NewMemDB()
db := coretesting.NewMemDB()
app := baseapp.NewBaseApp(name, log.NewTestLogger(t), db, nil)

app.SetExtendVoteHandler(func(ctx sdk.Context, req *abci.ExtendVoteRequest) (*abci.ExtendVoteResponse, error) {
Expand All @@ -329,7 +329,7 @@ func TestABCI_ExtendVote(t *testing.T) {
return &abci.VerifyVoteExtensionResponse{Status: abci.VERIFY_VOTE_EXTENSION_STATUS_ACCEPT}, nil
})

app.SetParamStore(&paramStore{db: dbm.NewMemDB()})
app.SetParamStore(&paramStore{db: coretesting.NewMemDB()})
_, err := app.InitChain(
&abci.InitChainRequest{
InitialHeight: 1,
Expand Down Expand Up @@ -394,7 +394,7 @@ func TestABCI_ExtendVote(t *testing.T) {
// without having called ExtendVote before.
func TestABCI_OnlyVerifyVoteExtension(t *testing.T) {
name := t.Name()
db := dbm.NewMemDB()
db := coretesting.NewMemDB()
app := baseapp.NewBaseApp(name, log.NewTestLogger(t), db, nil)

app.SetVerifyVoteExtensionHandler(func(ctx sdk.Context, req *abci.VerifyVoteExtensionRequest) (*abci.VerifyVoteExtensionResponse, error) {
Expand All @@ -407,7 +407,7 @@ func TestABCI_OnlyVerifyVoteExtension(t *testing.T) {
return &abci.VerifyVoteExtensionResponse{Status: abci.VERIFY_VOTE_EXTENSION_STATUS_ACCEPT}, nil
})

app.SetParamStore(&paramStore{db: dbm.NewMemDB()})
app.SetParamStore(&paramStore{db: coretesting.NewMemDB()})
_, err := app.InitChain(
&abci.InitChainRequest{
InitialHeight: 1,
Expand Down Expand Up @@ -527,7 +527,7 @@ func TestABCI_P2PQuery(t *testing.T) {
}

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

Expand All @@ -538,7 +538,7 @@ func TestBaseApp_PrepareCheckState(t *testing.T) {
}

app := baseapp.NewBaseApp(name, logger, db, nil)
app.SetParamStore(&paramStore{db: dbm.NewMemDB()})
app.SetParamStore(&paramStore{db: coretesting.NewMemDB()})
_, err := app.InitChain(&abci.InitChainRequest{
ConsensusParams: cp,
})
Expand All @@ -556,7 +556,7 @@ func TestBaseApp_PrepareCheckState(t *testing.T) {
}

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

Expand All @@ -567,7 +567,7 @@ func TestBaseApp_Precommit(t *testing.T) {
}

app := baseapp.NewBaseApp(name, logger, db, nil)
app.SetParamStore(&paramStore{db: dbm.NewMemDB()})
app.SetParamStore(&paramStore{db: coretesting.NewMemDB()})
_, err := app.InitChain(&abci.InitChainRequest{
ConsensusParams: cp,
})
Expand Down Expand Up @@ -1499,10 +1499,10 @@ func TestABCI_Query(t *testing.T) {

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

snapshotStore, err := snapshots.NewStore(dbm.NewMemDB(), testutil.GetTempDir(t))
snapshotStore, err := snapshots.NewStore(coretesting.NewMemDB(), testutil.GetTempDir(t))
require.NoError(t, err)

testCases := map[string]struct {
Expand Down Expand Up @@ -1591,7 +1591,7 @@ func TestABCI_GetBlockRetentionHeight(t *testing.T) {
for name, tc := range testCases {
tc := tc

tc.bapp.SetParamStore(&paramStore{db: dbm.NewMemDB()})
tc.bapp.SetParamStore(&paramStore{db: coretesting.NewMemDB()})
_, err := tc.bapp.InitChain(&abci.InitChainRequest{
ConsensusParams: &cmtproto.ConsensusParams{
Evidence: &cmtproto.EvidenceParams{
Expand All @@ -1612,7 +1612,7 @@ func TestPrepareCheckStateCalledWithCheckState(t *testing.T) {
t.Parallel()

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

Expand All @@ -1635,7 +1635,7 @@ func TestPrecommiterCalledWithDeliverState(t *testing.T) {
t.Parallel()

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

Expand Down Expand Up @@ -2307,7 +2307,7 @@ func TestABCI_HaltChain(t *testing.T) {
}

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

Expand Down
4 changes: 2 additions & 2 deletions baseapp/abci_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
cmtproto "github.com/cometbft/cometbft/api/cometbft/types/v1"
cmtsecp256k1 "github.com/cometbft/cometbft/crypto/secp256k1"
cmttypes "github.com/cometbft/cometbft/types"
dbm "github.com/cosmos/cosmos-db"
protoio "github.com/cosmos/gogoproto/io"
"github.com/cosmos/gogoproto/proto"
gogotypes "github.com/cosmos/gogoproto/types"
Expand All @@ -21,6 +20,7 @@ import (
"cosmossdk.io/core/address"
"cosmossdk.io/core/comet"
"cosmossdk.io/core/header"
coretesting "cosmossdk.io/core/testing"
"cosmossdk.io/log"

"github.com/cosmos/cosmos-sdk/baseapp"
Expand Down Expand Up @@ -478,7 +478,7 @@ func (s *ABCIUtilsTestSuite) TestDefaultProposalHandler_NoOpMempoolTxSelection()

// create a baseapp along with a tx config for tx generation
txConfig := authtx.NewTxConfig(cdc, signingCtx.AddressCodec(), signingCtx.ValidatorAddressCodec(), authtx.DefaultSignModes)
app := baseapp.NewBaseApp(s.T().Name(), log.NewNopLogger(), dbm.NewMemDB(), txConfig.TxDecoder())
app := baseapp.NewBaseApp(s.T().Name(), log.NewNopLogger(), coretesting.NewMemDB(), txConfig.TxDecoder())

// create a proposal handler
ph := baseapp.NewDefaultProposalHandler(mempool.NoOpMempool{}, app)
Expand Down
24 changes: 12 additions & 12 deletions baseapp/baseapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (

abci "github.com/cometbft/cometbft/api/cometbft/abci/v1"
cmtproto "github.com/cometbft/cometbft/api/cometbft/types/v1"
dbm "github.com/cosmos/cosmos-db"
"github.com/stretchr/testify/require"

"cosmossdk.io/core/address"
corestore "cosmossdk.io/core/store"
coretesting "cosmossdk.io/core/testing"
errorsmod "cosmossdk.io/errors"
"cosmossdk.io/log"
"cosmossdk.io/store/metrics"
Expand Down Expand Up @@ -70,7 +70,7 @@ func NewBaseAppSuite(t *testing.T, opts ...func(*baseapp.BaseApp)) *BaseAppSuite
signingCtx := cdc.InterfaceRegistry().SigningContext()

txConfig := authtx.NewTxConfig(cdc, signingCtx.AddressCodec(), signingCtx.ValidatorAddressCodec(), authtx.DefaultSignModes)
db := dbm.NewMemDB()
db := coretesting.NewMemDB()
logBuffer := new(bytes.Buffer)
logger := log.NewLogger(logBuffer, log.ColorOption(false))

Expand All @@ -80,7 +80,7 @@ func NewBaseAppSuite(t *testing.T, opts ...func(*baseapp.BaseApp)) *BaseAppSuite
app.SetInterfaceRegistry(cdc.InterfaceRegistry())
app.MsgServiceRouter().SetInterfaceRegistry(cdc.InterfaceRegistry())
app.MountStores(capKey1, capKey2)
app.SetParamStore(paramStore{db: dbm.NewMemDB()})
app.SetParamStore(paramStore{db: coretesting.NewMemDB()})
app.SetTxDecoder(txConfig.TxDecoder())
app.SetTxEncoder(txConfig.TxEncoder())
app.SetVersionModifier(newMockedVersionModifier(0))
Expand All @@ -100,7 +100,7 @@ func NewBaseAppSuite(t *testing.T, opts ...func(*baseapp.BaseApp)) *BaseAppSuite
func getQueryBaseapp(t *testing.T) *baseapp.BaseApp {
t.Helper()

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

Expand All @@ -120,7 +120,7 @@ func getQueryBaseapp(t *testing.T) *baseapp.BaseApp {
func NewBaseAppSuiteWithSnapshots(t *testing.T, cfg SnapshotsConfig, opts ...func(*baseapp.BaseApp)) *BaseAppSuite {
t.Helper()
snapshotTimeout := 1 * time.Minute
snapshotStore, err := snapshots.NewStore(dbm.NewMemDB(), testutil.GetTempDir(t))
snapshotStore, err := snapshots.NewStore(coretesting.NewMemDB(), testutil.GetTempDir(t))
require.NoError(t, err)

suite := NewBaseAppSuite(
Expand Down Expand Up @@ -242,7 +242,7 @@ func TestAnteHandlerGasMeter(t *testing.T) {
func TestLoadVersion(t *testing.T) {
logger := log.NewTestLogger(t)
pruningOpt := baseapp.SetPruning(pruningtypes.NewPruningOptions(pruningtypes.PruningNothing))
db := dbm.NewMemDB()
db := coretesting.NewMemDB()
name := t.Name()
app := baseapp.NewBaseApp(name, logger, db, nil, pruningOpt)

Expand Down Expand Up @@ -365,7 +365,7 @@ func TestSetLoader(t *testing.T) {
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
// prepare a db with some data
db := dbm.NewMemDB()
db := coretesting.NewMemDB()
initStore(t, db, tc.origStoreKey, k, v)

// load the app with the existing db
Expand Down Expand Up @@ -394,7 +394,7 @@ func TestSetLoader(t *testing.T) {

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

Expand All @@ -417,7 +417,7 @@ func TestVersionSetterGetter(t *testing.T) {
func TestLoadVersionInvalid(t *testing.T) {
logger := log.NewNopLogger()
pruningOpt := baseapp.SetPruning(pruningtypes.NewPruningOptions(pruningtypes.PruningNothing))
db := dbm.NewMemDB()
db := coretesting.NewMemDB()
name := t.Name()
app := baseapp.NewBaseApp(name, logger, db, nil, pruningOpt)

Expand Down Expand Up @@ -454,7 +454,7 @@ func TestOptionFunction(t *testing.T) {
}
}

db := dbm.NewMemDB()
db := coretesting.NewMemDB()
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 @@ -703,7 +703,7 @@ func TestBaseAppPostHandler(t *testing.T) {
func TestABCI_CreateQueryContext(t *testing.T) {
t.Parallel()

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

Expand Down Expand Up @@ -849,7 +849,7 @@ func TestLoadVersionPruning(t *testing.T) {
logger := log.NewNopLogger()
pruningOptions := pruningtypes.NewCustomPruningOptions(10, 15)
pruningOpt := baseapp.SetPruning(pruningOptions)
db := dbm.NewMemDB()
db := coretesting.NewMemDB()
name := t.Name()
app := baseapp.NewBaseApp(name, logger, db, nil, pruningOpt)

Expand Down
4 changes: 2 additions & 2 deletions baseapp/grpcrouter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"sync"
"testing"

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

coretesting "cosmossdk.io/core/testing"
"cosmossdk.io/depinject"
"cosmossdk.io/log"

Expand Down Expand Up @@ -107,7 +107,7 @@ func TestRegisterQueryServiceTwice(t *testing.T) {
),
&appBuilder)
require.NoError(t, err)
db := dbm.NewMemDB()
db := coretesting.NewMemDB()
app := appBuilder.Build(db, nil)

// First time registering service shouldn't panic.
Expand Down
10 changes: 5 additions & 5 deletions baseapp/msg_service_router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"testing"

abci "github.com/cometbft/cometbft/api/cometbft/abci/v1"
dbm "github.com/cosmos/cosmos-db"
"github.com/stretchr/testify/require"

coretesting "cosmossdk.io/core/testing"
"cosmossdk.io/depinject"
"cosmossdk.io/log"

Expand All @@ -33,7 +33,7 @@ func TestRegisterMsgService(t *testing.T) {
depinject.Supply(log.NewTestLogger(t)),
), &appBuilder, &registry)
require.NoError(t, err)
app := appBuilder.Build(dbm.NewMemDB(), nil)
app := appBuilder.Build(coretesting.NewMemDB(), nil)

require.Panics(t, func() {
testdata.RegisterMsgServer(
Expand Down Expand Up @@ -65,7 +65,7 @@ func TestRegisterMsgServiceTwice(t *testing.T) {
depinject.Supply(log.NewTestLogger(t)),
), &appBuilder, &registry)
require.NoError(t, err)
db := dbm.NewMemDB()
db := coretesting.NewMemDB()
app := appBuilder.Build(db, nil)
testdata.RegisterInterfaces(registry)

Expand Down Expand Up @@ -98,7 +98,7 @@ func TestHybridHandlerByMsgName(t *testing.T) {
depinject.Supply(log.NewTestLogger(t)),
), &appBuilder, &registry)
require.NoError(t, err)
db := dbm.NewMemDB()
db := coretesting.NewMemDB()
app := appBuilder.Build(db, nil)
testdata.RegisterInterfaces(registry)

Expand Down Expand Up @@ -135,7 +135,7 @@ func TestMsgService(t *testing.T) {
depinject.Supply(log.NewNopLogger()),
), &appBuilder, &cdc, &interfaceRegistry)
require.NoError(t, err)
app := appBuilder.Build(dbm.NewMemDB(), nil)
app := appBuilder.Build(coretesting.NewMemDB(), nil)
signingCtx := interfaceRegistry.SigningContext()

// patch in TxConfig instead of using an output from x/auth/tx
Expand Down
4 changes: 2 additions & 2 deletions baseapp/regression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package baseapp
import (
"testing"

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

coretesting "cosmossdk.io/core/testing"
"cosmossdk.io/log"
"cosmossdk.io/store"
storemetrics "cosmossdk.io/store/metrics"
Expand All @@ -27,7 +27,7 @@ func TestNilCmsCheckBeforeSeal(t *testing.T) {

// 2. Now that we've figured out and gotten back an error, let's rectify the problem.
// and we should be able to set the commit multistore then reinvoke app.Init successfully!
db := dbm.NewMemDB()
db := coretesting.NewMemDB()
logger := log.NewTestLogger(t)
app.cms = store.NewCommitMultiStore(db, logger, storemetrics.NewNoOpMetrics())
err := app.Init()
Expand Down
Loading

0 comments on commit aaf72f2

Please sign in to comment.