Skip to content

Commit

Permalink
refactor: decouple comet from modules (backport #21382) (#21650)
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored Sep 11, 2024
1 parent 20f7cd7 commit b2feec1
Show file tree
Hide file tree
Showing 20 changed files with 202 additions and 149 deletions.
20 changes: 19 additions & 1 deletion simapp/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import (
"fmt"

cmtproto "github.com/cometbft/cometbft/api/cometbft/types/v1"
cmttypes "github.com/cometbft/cometbft/types"

"cosmossdk.io/collections"
storetypes "cosmossdk.io/store/types"
slashingtypes "cosmossdk.io/x/slashing/types"
"cosmossdk.io/x/staking"
stakingtypes "cosmossdk.io/x/staking/types"

cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
Expand Down Expand Up @@ -41,9 +43,25 @@ func (app *SimApp) ExportAppStateAndValidators(forZeroHeight bool, jailAllowedAd
}

validators, err := staking.WriteValidators(ctx, app.StakingKeeper)
cmtValidators := []cmttypes.GenesisValidator{}
for _, val := range validators {
cmtPk, err := cryptocodec.ToCmtPubKeyInterface(val.PubKey)
if err != nil {
return servertypes.ExportedApp{}, err
}
cmtVal := cmttypes.GenesisValidator{
Address: val.Address.Bytes(),
PubKey: cmtPk,
Power: val.Power,
Name: val.Name,
}

cmtValidators = append(cmtValidators, cmtVal)
}

return servertypes.ExportedApp{
AppState: appState,
Validators: validators,
Validators: cmtValidators,
Height: height,
ConsensusParams: app.BaseApp.GetConsensusParams(ctx),
}, err
Expand Down
81 changes: 79 additions & 2 deletions testutil/cli/cmt_mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ func NewMockCometRPC(respQuery abci.QueryResponse) MockCometRPC {
return MockCometRPC{responseQuery: respQuery}
}

// NewMockCometRPCWithValue returns a mock CometBFT RPC implementation with value only.
// NewMockCometRPCWithResponseQueryValue returns a mock CometBFT RPC implementation with value only.
// It is used for CLI testing.
func NewMockCometRPCWithValue(bz []byte) MockCometRPC {
func NewMockCometRPCWithResponseQueryValue(bz []byte) MockCometRPC {
return MockCometRPC{responseQuery: abci.QueryResponse{
Value: bz,
}}
Expand All @@ -47,3 +47,80 @@ func (m MockCometRPC) ABCIQueryWithOptions(
) (*coretypes.ResultABCIQuery, error) {
return &coretypes.ResultABCIQuery{Response: m.responseQuery}, nil
}

type FilterTxsFn = func(query string, start, end int) ([][]byte, error)

type MockCometTxSearchRPC struct {
rpcclientmock.Client

txConfig client.TxConfig
txs []cmttypes.Tx
filterTxsFn FilterTxsFn
}

func (m MockCometTxSearchRPC) Txs() []cmttypes.Tx {
return m.txs
}

// accept [][]byte so that module that use this for testing dont have to import comet directly
func (m *MockCometTxSearchRPC) WithTxs(txs [][]byte) {
cmtTxs := make([]cmttypes.Tx, len(txs))
for i, tx := range txs {
cmtTxs[i] = tx
}
m.txs = cmtTxs
}

func (m *MockCometTxSearchRPC) WithTxConfig(cfg client.TxConfig) {
m.txConfig = cfg
}

func (m *MockCometTxSearchRPC) WithFilterTxsFn(fn FilterTxsFn) {
m.filterTxsFn = fn
}

func (MockCometTxSearchRPC) BroadcastTxSync(context.Context, cmttypes.Tx) (*coretypes.ResultBroadcastTx, error) {
return &coretypes.ResultBroadcastTx{Code: 0}, nil
}

func (mock MockCometTxSearchRPC) TxSearch(ctx context.Context, query string, prove bool, page, perPage *int, orderBy string) (*coretypes.ResultTxSearch, error) {
if page == nil {
*page = 0
}

if perPage == nil {
*perPage = 0
}

start, end := client.Paginate(len(mock.txs), *page, *perPage, 100)
if start < 0 || end < 0 {
// nil result with nil error crashes utils.QueryTxsByEvents
return &coretypes.ResultTxSearch{}, nil
}

var txs []cmttypes.Tx
if mock.filterTxsFn != nil {
filterTxs, err := mock.filterTxsFn(query, start, end)
if err != nil {
return nil, err
}

cmtTxs := make([]cmttypes.Tx, len(filterTxs))
for i, tx := range filterTxs {
cmtTxs[i] = tx
}
txs = append(txs, cmtTxs...)
} else {
txs = mock.txs[start:end]
}

rst := &coretypes.ResultTxSearch{Txs: make([]*coretypes.ResultTx, len(txs)), TotalCount: len(txs)}
for i := range txs {
rst.Txs[i] = &coretypes.ResultTx{Tx: txs[i]}
}
return rst, nil
}

func (mock MockCometTxSearchRPC) Block(ctx context.Context, height *int64) (*coretypes.ResultBlock, error) {
return &coretypes.ResultBlock{Block: &cmttypes.Block{}}, nil
}
2 changes: 1 addition & 1 deletion x/accounts/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (s *CLITestSuite) TestTxInitCmd() {
Response: sdk.MsgTypeURL(&types.Empty{})[1:],
},
})
c := clitestutil.NewMockCometRPCWithValue(bz)
c := clitestutil.NewMockCometRPCWithResponseQueryValue(bz)
return s.baseCtx.WithClient(c)
}
s.clientCtx = ctxGen()
Expand Down
3 changes: 1 addition & 2 deletions x/auth/ante/testutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"testing"

abci "github.com/cometbft/cometbft/api/cometbft/abci/v1"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -117,7 +116,7 @@ func SetupTestSuite(t *testing.T, isCheckTx bool) *AnteTestSuite {

suite.clientCtx = client.Context{}.
WithTxConfig(suite.encCfg.TxConfig).
WithClient(clitestutil.NewMockCometRPC(abci.QueryResponse{}))
WithClient(clitestutil.NewMockCometRPCWithResponseQueryValue(nil))

anteHandler, err := ante.NewAnteHandler(
ante.HandlerOptions{
Expand Down
2 changes: 1 addition & 1 deletion x/authz/client/cli/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (s *CLITestSuite) SetupSuite() {

ctxGen := func() client.Context {
bz, _ := s.encCfg.Codec.Marshal(&sdk.TxResponse{})
c := clitestutil.NewMockCometRPCWithValue(bz)
c := clitestutil.NewMockCometRPCWithResponseQueryValue(bz)
return s.baseCtx.WithClient(c)
}
s.clientCtx = ctxGen()
Expand Down
3 changes: 1 addition & 2 deletions x/bank/client/cli/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io"
"testing"

rpcclientmock "github.com/cometbft/cometbft/rpc/client/mock"
"github.com/stretchr/testify/suite"

sdkmath "cosmossdk.io/math"
Expand Down Expand Up @@ -44,7 +43,7 @@ func (s *CLITestSuite) SetupSuite() {
WithKeyring(s.kr).
WithTxConfig(s.encCfg.TxConfig).
WithCodec(s.encCfg.Codec).
WithClient(clitestutil.MockCometRPC{Client: rpcclientmock.Client{}}).
WithClient(clitestutil.MockCometRPC{}).
WithAccountRetriever(client.MockAccountRetriever{}).
WithOutput(io.Discard).
WithAddressCodec(addresscodec.NewBech32Codec("cosmos")).
Expand Down
2 changes: 1 addition & 1 deletion x/bank/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
cosmossdk.io/math v1.3.0
cosmossdk.io/store v1.1.1-0.20240909133312-50288938d1b6 // main
github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 // indirect
github.com/cometbft/cometbft v1.0.0-rc1.0.20240908111210-ab0be101882f
github.com/cometbft/cometbft v1.0.0-rc1.0.20240908111210-ab0be101882f // indirect
github.com/cosmos/cosmos-proto v1.0.0-beta.5
github.com/cosmos/cosmos-sdk v0.52.0
github.com/cosmos/gogoproto v1.7.0
Expand Down
8 changes: 2 additions & 6 deletions x/feegrant/client/cli/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"testing"
"time"

abci "github.com/cometbft/cometbft/api/cometbft/abci/v1"
rpcclientmock "github.com/cometbft/cometbft/rpc/client/mock"
"github.com/cosmos/gogoproto/proto"
"github.com/stretchr/testify/suite"

Expand Down Expand Up @@ -69,7 +67,7 @@ func (s *CLITestSuite) SetupSuite() {
WithKeyring(s.kr).
WithTxConfig(s.encCfg.TxConfig).
WithCodec(s.encCfg.Codec).
WithClient(clitestutil.MockCometRPC{Client: rpcclientmock.Client{}}).
WithClient(clitestutil.MockCometRPC{}).
WithAccountRetriever(client.MockAccountRetriever{}).
WithOutput(io.Discard).
WithChainID("test-chain").
Expand All @@ -79,9 +77,7 @@ func (s *CLITestSuite) SetupSuite() {

ctxGen := func() client.Context {
bz, _ := s.encCfg.Codec.Marshal(&sdk.TxResponse{})
c := clitestutil.NewMockCometRPC(abci.QueryResponse{
Value: bz,
})
c := clitestutil.NewMockCometRPCWithResponseQueryValue(bz)

return s.baseCtx.WithClient(c)
}
Expand Down
4 changes: 2 additions & 2 deletions x/feegrant/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
cosmossdk.io/store v1.1.1-0.20240909133312-50288938d1b6
cosmossdk.io/x/bank v0.0.0-20240226161501-23359a0b6d91
cosmossdk.io/x/gov v0.0.0-20230925135524-a1bc045b3190
github.com/cometbft/cometbft v1.0.0-rc1.0.20240908111210-ab0be101882f
github.com/cometbft/cometbft v1.0.0-rc1.0.20240908111210-ab0be101882f // indirect
github.com/cosmos/cosmos-proto v1.0.0-beta.5
github.com/cosmos/cosmos-sdk v0.52.0
github.com/cosmos/gogoproto v1.7.0
Expand Down Expand Up @@ -58,7 +58,7 @@ require (
github.com/cockroachdb/redact v1.1.5 // indirect
github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 // indirect
github.com/cometbft/cometbft-db v0.15.0 // indirect
github.com/cometbft/cometbft/api v1.0.0-rc.1
github.com/cometbft/cometbft/api v1.0.0-rc.1 // indirect
github.com/cosmos/btcutil v1.0.5 // indirect
github.com/cosmos/cosmos-db v1.0.3-0.20240911104526-ddc3f09bfc22 // indirect
github.com/cosmos/crypto v0.1.2 // indirect
Expand Down
8 changes: 2 additions & 6 deletions x/genutil/client/cli/gentx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"path/filepath"
"testing"

abci "github.com/cometbft/cometbft/abci/types"
rpcclientmock "github.com/cometbft/cometbft/rpc/client/mock"
"github.com/stretchr/testify/suite"

sdkmath "cosmossdk.io/math"
Expand Down Expand Up @@ -48,16 +46,14 @@ func (s *CLITestSuite) SetupSuite() {
WithKeyring(s.kr).
WithTxConfig(s.encCfg.TxConfig).
WithCodec(s.encCfg.Codec).
WithClient(clitestutil.MockCometRPC{Client: rpcclientmock.Client{}}).
WithClient(clitestutil.MockCometRPC{}).
WithAccountRetriever(client.MockAccountRetriever{}).
WithOutput(io.Discard).
WithChainID("test-chain")

ctxGen := func() client.Context {
bz, _ := s.encCfg.Codec.Marshal(&sdk.TxResponse{})
c := clitestutil.NewMockCometRPC(abci.QueryResponse{
Value: bz,
})
c := clitestutil.NewMockCometRPCWithResponseQueryValue(bz)
return s.baseCtx.WithClient(c)
}
s.clientCtx = ctxGen()
Expand Down
8 changes: 2 additions & 6 deletions x/gov/client/cli/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"io"
"testing"

abci "github.com/cometbft/cometbft/abci/types"
rpcclientmock "github.com/cometbft/cometbft/rpc/client/mock"
"github.com/stretchr/testify/suite"

sdkmath "cosmossdk.io/math"
Expand Down Expand Up @@ -50,7 +48,7 @@ func (s *CLITestSuite) SetupSuite() {
WithKeyring(s.kr).
WithTxConfig(s.encCfg.TxConfig).
WithCodec(s.encCfg.Codec).
WithClient(clitestutil.MockCometRPC{Client: rpcclientmock.Client{}}).
WithClient(clitestutil.MockCometRPC{}).
WithAccountRetriever(client.MockAccountRetriever{}).
WithOutput(io.Discard).
WithChainID("test-chain").
Expand All @@ -60,9 +58,7 @@ func (s *CLITestSuite) SetupSuite() {

ctxGen := func() client.Context {
bz, _ := s.encCfg.Codec.Marshal(&sdk.TxResponse{})
c := clitestutil.NewMockCometRPC(abci.QueryResponse{
Value: bz,
})
c := clitestutil.NewMockCometRPCWithResponseQueryValue(bz)
return s.baseCtx.WithClient(c)
}
s.clientCtx = ctxGen()
Expand Down
Loading

0 comments on commit b2feec1

Please sign in to comment.