Skip to content

Commit

Permalink
Fix app based tests
Browse files Browse the repository at this point in the history
  • Loading branch information
avalkov committed Nov 22, 2024
1 parent e3f37c7 commit 0b737c1
Show file tree
Hide file tree
Showing 49 changed files with 477 additions and 657 deletions.
18 changes: 3 additions & 15 deletions baseapp/grpcrouter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@ import (
"sync"
"testing"

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

"cosmossdk.io/depinject"
"cosmossdk.io/log"

hApp "github.com/0xPolygon/heimdall-v2/app"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/runtime"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
testdata_pulsar "github.com/cosmos/cosmos-sdk/testutil/testdata/testpb"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -99,16 +95,8 @@ func TestGRPCRouterHybridHandlers(t *testing.T) {

func TestRegisterQueryServiceTwice(t *testing.T) {
// Setup baseapp.
var appBuilder *runtime.AppBuilder
err := depinject.Inject(
depinject.Configs(
makeMinimalConfig(),
depinject.Supply(log.NewTestLogger(t)),
),
&appBuilder)
require.NoError(t, err)
db := dbm.NewMemDB()
app := appBuilder.Build(db, nil)
app, _, _ := hApp.SetupApp(t, 1)
hApp.RequestFinalizeBlock(t, app, app.LastBlockHeight()+1)

// First time registering service shouldn't panic.
require.NotPanics(t, func() {
Expand Down
107 changes: 37 additions & 70 deletions baseapp/msg_service_router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,26 @@ package baseapp_test

import (
"context"
"math/big"
"testing"

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

"cosmossdk.io/depinject"
"cosmossdk.io/log"

hApp "github.com/0xPolygon/heimdall-v2/app"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/runtime"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
authTypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/bank/testutil"
)

func TestRegisterMsgService(t *testing.T) {
// Setup baseapp.
var (
appBuilder *runtime.AppBuilder
registry codectypes.InterfaceRegistry
)
err := depinject.Inject(
depinject.Configs(
makeMinimalConfig(),
depinject.Supply(log.NewTestLogger(t)),
), &appBuilder, &registry)
require.NoError(t, err)
app := appBuilder.Build(dbm.NewMemDB(), nil)
app, _, _ := hApp.SetupApp(t, 1)
hApp.RequestFinalizeBlock(t, app, app.LastBlockHeight()+1)

require.Panics(t, func() {
testdata.RegisterMsgServer(
Expand All @@ -43,7 +31,7 @@ func TestRegisterMsgService(t *testing.T) {
})

// Register testdata Msg services, and rerun `RegisterMsgService`.
testdata.RegisterInterfaces(registry)
testdata.RegisterInterfaces(app.InterfaceRegistry())

require.NotPanics(t, func() {
testdata.RegisterMsgServer(
Expand All @@ -55,19 +43,10 @@ func TestRegisterMsgService(t *testing.T) {

func TestRegisterMsgServiceTwice(t *testing.T) {
// Setup baseapp.
var (
appBuilder *runtime.AppBuilder
registry codectypes.InterfaceRegistry
)
err := depinject.Inject(
depinject.Configs(
makeMinimalConfig(),
depinject.Supply(log.NewTestLogger(t)),
), &appBuilder, &registry)
require.NoError(t, err)
db := dbm.NewMemDB()
app := appBuilder.Build(db, nil)
testdata.RegisterInterfaces(registry)
app, _, _ := hApp.SetupApp(t, 1)
hApp.RequestFinalizeBlock(t, app, app.LastBlockHeight()+1)

testdata.RegisterInterfaces(app.InterfaceRegistry())

// First time registering service shouldn't panic.
require.NotPanics(t, func() {
Expand All @@ -88,19 +67,10 @@ func TestRegisterMsgServiceTwice(t *testing.T) {

func TestHybridHandlerByMsgName(t *testing.T) {
// Setup baseapp and router.
var (
appBuilder *runtime.AppBuilder
registry codectypes.InterfaceRegistry
)
err := depinject.Inject(
depinject.Configs(
makeMinimalConfig(),
depinject.Supply(log.NewTestLogger(t)),
), &appBuilder, &registry)
require.NoError(t, err)
db := dbm.NewMemDB()
app := appBuilder.Build(db, nil)
testdata.RegisterInterfaces(registry)
app, _, _ := hApp.SetupApp(t, 1)
hApp.RequestFinalizeBlock(t, app, app.LastBlockHeight()+1)

testdata.RegisterInterfaces(app.InterfaceRegistry())

testdata.RegisterMsgServer(
app.MsgServiceRouter(),
Expand All @@ -110,10 +80,9 @@ func TestHybridHandlerByMsgName(t *testing.T) {
handler := app.MsgServiceRouter().HybridHandlerByMsgName("testpb.MsgCreateDog")

require.NotNil(t, handler)
require.NoError(t, app.Init())
ctx := app.NewContext(true)
resp := new(testdata.MsgCreateDogResponse)
err = handler(ctx, &testdata.MsgCreateDog{
err := handler(ctx, &testdata.MsgCreateDog{
Dog: &testdata.Dog{Name: "Spot"},
Owner: "me",
}, resp)
Expand All @@ -124,36 +93,33 @@ func TestHybridHandlerByMsgName(t *testing.T) {
func TestMsgService(t *testing.T) {
priv, _, _ := testdata.KeyTestPubAddr()

var (
appBuilder *runtime.AppBuilder
cdc codec.Codec
interfaceRegistry codectypes.InterfaceRegistry
)
err := depinject.Inject(
depinject.Configs(
makeMinimalConfig(),
depinject.Supply(log.NewNopLogger()),
), &appBuilder, &cdc, &interfaceRegistry)
require.NoError(t, err)
app := appBuilder.Build(dbm.NewMemDB(), nil)
app, _, _ := hApp.SetupApp(t, 1)
hApp.RequestFinalizeBlock(t, app, app.LastBlockHeight()+1)

ctx := app.NewContext(false)

addr := sdk.AccAddress(priv.PubKey().Address())
acc := authTypes.NewBaseAccount(addr, priv.PubKey(), 1337, 0)
require.NoError(t, testutil.FundAccount(ctx, app.BankKeeper, addr, sdk.NewCoins(sdk.NewInt64Coin("pol", 43*defaultFeeAmount))))

app.AccountKeeper.SetAccount(ctx, acc)

// patch in TxConfig instead of using an output from x/auth/tx
txConfig := authtx.NewTxConfig(cdc, authtx.DefaultSignModes)
txConfig := authtx.NewTxConfig(app.AppCodec(), authtx.DefaultSignModes)
// set the TxDecoder in the BaseApp for minimal tx simulations
app.SetTxDecoder(txConfig.TxDecoder())

defaultSignMode, err := authsigning.APISignModeToInternal(txConfig.SignModeHandler().DefaultMode())
require.NoError(t, err)

testdata.RegisterInterfaces(interfaceRegistry)
testdata.RegisterInterfaces(app.InterfaceRegistry())
testdata.RegisterMsgServer(
app.MsgServiceRouter(),
testdata.MsgServerImpl{},
)
_, err = app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: 1})
require.NoError(t, err)

_, _, addr := testdata.KeyTestPubAddr()
hApp.RequestFinalizeBlock(t, app, app.LastBlockHeight()+1)

msg := testdata.MsgCreateDog{
Dog: &testdata.Dog{Name: "Spot"},
Owner: addr.String(),
Expand Down Expand Up @@ -181,8 +147,8 @@ func TestMsgService(t *testing.T) {

// Second round: all signer infos are set, so each signer can sign.
signerData := authsigning.SignerData{
ChainID: "test",
AccountNumber: 0,
ChainID: "",
AccountNumber: 1337,
Sequence: 0,
PubKey: priv.PubKey(),
}
Expand All @@ -196,7 +162,8 @@ func TestMsgService(t *testing.T) {
// Send the tx to the app
txBytes, err := txConfig.TxEncoder()(txBuilder.GetTx())
require.NoError(t, err)
res, err := app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: 1, Txs: [][]byte{txBytes}})
require.NoError(t, err)
require.Equal(t, abci.CodeTypeOK, res.TxResults[0].Code, "res=%+v", res)
res := hApp.RequestFinalizeBlockWithTxs(t, app, app.LastBlockHeight()+1, txBytes)
require.Equal(t, abci.CodeTypeOK, res.TxResults[1].Code, "res=%+v", res)
}

var defaultFeeAmount = big.NewInt(10).Exp(big.NewInt(10), big.NewInt(15), nil).Int64()
85 changes: 16 additions & 69 deletions client/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,18 @@ import (
"context"
"testing"

abci "github.com/cometbft/cometbft/abci/types"
cmtjson "github.com/cometbft/cometbft/libs/json"
dbm "github.com/cosmos/cosmos-db"
"github.com/stretchr/testify/suite"
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"

"cosmossdk.io/depinject"
"cosmossdk.io/log"
"cosmossdk.io/math"
hApp "github.com/0xPolygon/heimdall-v2/app"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/runtime"
"github.com/cosmos/cosmos-sdk/testutil/sims"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth/testutil"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
"github.com/cosmos/cosmos-sdk/x/bank/types"
)

Expand All @@ -42,70 +32,27 @@ type IntegrationTestSuite struct {

func (s *IntegrationTestSuite) SetupSuite() {
s.T().Log("setting up integration test suite")
var (
interfaceRegistry codectypes.InterfaceRegistry
bankKeeper bankkeeper.BaseKeeper
appBuilder *runtime.AppBuilder
cdc codec.Codec
)

// 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(
depinject.Configs(
testutil.AppConfig,
depinject.Supply(log.NewNopLogger()),
),
&interfaceRegistry, &bankKeeper, &appBuilder, &cdc)
s.NoError(err)

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

valSet, err := sims.CreateRandomValidatorSet()
s.NoError(err)

// generate genesis account
s.genesisAccountBalance = 100000000000000
senderPrivKey := secp256k1.GenPrivKey()
acc := authtypes.NewBaseAccount(senderPrivKey.PubKey().Address().Bytes(), senderPrivKey.PubKey(), 0, 0)
balance := types.Balance{
Address: acc.GetAddress().String(),
Coins: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, math.NewInt(s.genesisAccountBalance))),
}

genesisState, err := sims.GenesisStateWithValSet(cdc, app.DefaultGenesis(), valSet, []authtypes.GenesisAccount{acc}, balance)
s.NoError(err)

stateBytes, err := cmtjson.MarshalIndent(genesisState, "", " ")
s.NoError(err)

// init chain will set the validator set and initialize the genesis accounts
app.InitChain(&abci.RequestInitChain{
Validators: []abci.ValidatorUpdate{},
ConsensusParams: sims.DefaultConsensusParams,
AppStateBytes: stateBytes,
},
)

app.FinalizeBlock(&abci.RequestFinalizeBlock{
Height: app.LastBlockHeight() + 1,
Hash: app.LastCommitID().Hash,
NextValidatorsHash: valSet.Hash(),
})

// end of app init
app, _, _ := hApp.SetupApp(s.T(), 1)
hApp.RequestFinalizeBlock(s.T(), app, app.LastBlockHeight()+1)

s.ctx = app.BaseApp.NewContext(false)
s.cdc = cdc
queryHelper := baseapp.NewQueryServerTestHelper(s.ctx, interfaceRegistry)
types.RegisterQueryServer(queryHelper, bankKeeper)
s.cdc = app.AppCodec()
queryHelper := baseapp.NewQueryServerTestHelper(s.ctx, app.InterfaceRegistry())
types.RegisterQueryServer(queryHelper, app.BankKeeper)
testdata.RegisterQueryServer(queryHelper, testdata.QueryImpl{})
s.bankClient = types.NewQueryClient(queryHelper)
s.testClient = testdata.NewQueryClient(queryHelper)
s.genesisAccount = acc

accounts := app.AccountKeeper.GetAllAccounts(s.ctx)
for _, acc := range accounts {
if acc.GetPubKey() != nil {
s.genesisAccount = acc.(*authtypes.BaseAccount)
break
}
}
genesisCoins := app.BankKeeper.GetBalance(s.ctx, s.genesisAccount.GetAddress(), sdk.DefaultBondDenom)
s.genesisAccountBalance = genesisCoins.Amount.Int64()
}

func (s *IntegrationTestSuite) TearDownSuite() {
Expand Down
Loading

0 comments on commit 0b737c1

Please sign in to comment.