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
… (backport #21525) (#21720)

Co-authored-by: cool-developer <51834436+cool-develope@users.noreply.github.com>
Co-authored-by: Julien Robert <julien@rbrt.fr>
  • Loading branch information
3 people authored Sep 13, 2024
1 parent 64beb97 commit 150d17c
Show file tree
Hide file tree
Showing 84 changed files with 576 additions and 523 deletions.
38 changes: 19 additions & 19 deletions baseapp/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ 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"
gogotypes "github.com/cosmos/gogoproto/types"
"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 @@ -102,7 +102,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 @@ -202,7 +202,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 @@ -219,7 +219,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 @@ -241,7 +241,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 @@ -308,7 +308,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 @@ -326,7 +326,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 @@ -391,7 +391,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 @@ -404,7 +404,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 @@ -524,7 +524,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 @@ -535,7 +535,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 @@ -553,7 +553,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 @@ -564,7 +564,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 @@ -1253,10 +1253,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 @@ -1345,7 +1345,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 @@ -1366,7 +1366,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 @@ -1389,7 +1389,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 @@ -2061,7 +2061,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 150d17c

Please sign in to comment.