Skip to content

Commit

Permalink
fix: unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Lockwarr committed Nov 11, 2024
1 parent 39a7145 commit e17058b
Show file tree
Hide file tree
Showing 14 changed files with 607 additions and 666 deletions.
38 changes: 19 additions & 19 deletions testutil/contractmanager/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package network

import (
"fmt"
"os"
"testing"
"time"

Expand All @@ -18,12 +19,6 @@ import (
"github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/genutil"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
"github.com/cosmos/cosmos-sdk/x/staking"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

"github.com/Nolus-Protocol/nolus-core/app/params"

"github.com/Nolus-Protocol/nolus-core/app"
)
Expand Down Expand Up @@ -51,14 +46,21 @@ func New(t *testing.T, configs ...network.Config) *network.Network {
return net
}

var tempDir = func() string {
dir, err := os.MkdirTemp("", "nolusd")
if err != nil {
panic("failed to create temp dir: " + err.Error())
}
defer os.RemoveAll(dir)

return dir
}

// DefaultConfig will initialize config for the network with custom application,
// genesis and single validator. All other parameters are inherited from cosmos-sdk/testutil/network.DefaultConfig.
func DefaultConfig() network.Config {
// app doesn't have these modules anymore, but we need them for test setup, which uses gentx and MsgCreateValidator
app.ModuleBasics[genutiltypes.ModuleName] = genutil.AppModuleBasic{}
app.ModuleBasics[stakingtypes.ModuleName] = staking.AppModuleBasic{}

encoding := app.MakeEncodingConfig(app.ModuleBasics)

chainID := "chain-" + tmrand.NewRand().Str(6)
return network.Config{
Codec: encoding.Marshaler,
Expand All @@ -71,7 +73,6 @@ func DefaultConfig() network.Config {
val.GetCtx().Logger, db.NewMemDB(), nil, true, map[int64]bool{}, val.GetCtx().Config.RootDir, 0,
encoding,
sims.EmptyAppOptions{},
nil,
baseapp.SetPruning(pruningtypes.NewPruningOptionsFromString(val.GetAppConfig().Pruning)),
baseapp.SetMinGasPrices(val.GetAppConfig().MinGasPrices),
baseapp.SetChainID(chainID),
Expand All @@ -80,14 +81,13 @@ func DefaultConfig() network.Config {
GenesisState: app.ModuleBasics.DefaultGenesis(encoding.Marshaler),
TimeoutCommit: 2 * time.Second,
ChainID: chainID,
// Some changes are introduced to make the tests run as if nolus is a standalone chain.
// This will only work if NumValidators is set to 1.
NumValidators: 1,
BondDenom: params.DefaultBondDenom,
MinGasPrices: fmt.Sprintf("0.000006%s", params.DefaultBondDenom),
AccountTokens: sdk.TokensFromConsensusPower(1000, sdk.DefaultPowerReduction),
StakingTokens: sdk.TokensFromConsensusPower(500, sdk.DefaultPowerReduction),
BondedTokens: sdk.TokensFromConsensusPower(100, sdk.DefaultPowerReduction),
NumValidators: 1,
BondDenom: sdk.DefaultBondDenom,
MinGasPrices: fmt.Sprintf("0.000006%s", sdk.DefaultBondDenom),
AccountTokens: sdk.TokensFromConsensusPower(1000, sdk.DefaultPowerReduction),
StakingTokens: sdk.TokensFromConsensusPower(500, sdk.DefaultPowerReduction),
BondedTokens: sdk.TokensFromConsensusPower(100, sdk.DefaultPowerReduction),

PruningStrategy: pruningtypes.PruningOptionNothing,
CleanupDir: true,
SigningAlgo: string(hd.Secp256k1Type),
Expand Down
27 changes: 8 additions & 19 deletions testutil/interchainqueries/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ import (
"github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/genutil"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
"github.com/cosmos/cosmos-sdk/x/staking"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

"github.com/Nolus-Protocol/nolus-core/app/params"

"github.com/Nolus-Protocol/nolus-core/app"
)
Expand Down Expand Up @@ -54,11 +48,8 @@ func New(t *testing.T, configs ...network.Config) *network.Network {
// DefaultConfig will initialize config for the network with custom application,
// genesis and single validator. All other parameters are inherited from cosmos-sdk/testutil/network.DefaultConfig.
func DefaultConfig() network.Config {
// app doesn't have these modules anymore, but we need them for test setup, which uses gentx and MsgCreateValidator
app.ModuleBasics[genutiltypes.ModuleName] = genutil.AppModuleBasic{}
app.ModuleBasics[stakingtypes.ModuleName] = staking.AppModuleBasic{}

encoding := app.MakeEncodingConfig(app.ModuleBasics)

chainID := "chain-" + tmrand.NewRand().Str(6)
return network.Config{
Codec: encoding.Marshaler,
Expand All @@ -71,7 +62,6 @@ func DefaultConfig() network.Config {
val.GetCtx().Logger, db.NewMemDB(), nil, true, map[int64]bool{}, val.GetCtx().Config.RootDir, 0,
encoding,
sims.EmptyAppOptions{},
nil,
baseapp.SetPruning(pruningtypes.NewPruningOptionsFromString(val.GetAppConfig().Pruning)),
baseapp.SetMinGasPrices(val.GetAppConfig().MinGasPrices),
baseapp.SetChainID(chainID),
Expand All @@ -80,14 +70,13 @@ func DefaultConfig() network.Config {
GenesisState: app.ModuleBasics.DefaultGenesis(encoding.Marshaler),
TimeoutCommit: 2 * time.Second,
ChainID: chainID,
// Some changes are introduced to make the tests run as if nolus is a standalone chain.
// This will only work if NumValidators is set to 1.
NumValidators: 1,
BondDenom: params.DefaultBondDenom,
MinGasPrices: fmt.Sprintf("0.000006%s", params.DefaultBondDenom),
AccountTokens: sdk.TokensFromConsensusPower(1000, sdk.DefaultPowerReduction),
StakingTokens: sdk.TokensFromConsensusPower(500, sdk.DefaultPowerReduction),
BondedTokens: sdk.TokensFromConsensusPower(100, sdk.DefaultPowerReduction),
NumValidators: 1,
BondDenom: sdk.DefaultBondDenom,
MinGasPrices: fmt.Sprintf("0.000006%s", sdk.DefaultBondDenom),
AccountTokens: sdk.TokensFromConsensusPower(1000, sdk.DefaultPowerReduction),
StakingTokens: sdk.TokensFromConsensusPower(500, sdk.DefaultPowerReduction),
BondedTokens: sdk.TokensFromConsensusPower(100, sdk.DefaultPowerReduction),

PruningStrategy: pruningtypes.PruningOptionNothing,
CleanupDir: true,
SigningAlgo: string(hd.Secp256k1Type),
Expand Down
27 changes: 8 additions & 19 deletions testutil/interchaintxs/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ import (
"github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/genutil"
genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types"
"github.com/cosmos/cosmos-sdk/x/staking"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"

"github.com/Nolus-Protocol/nolus-core/app/params"

"github.com/Nolus-Protocol/nolus-core/app"
)
Expand Down Expand Up @@ -54,11 +48,8 @@ func New(t *testing.T, configs ...network.Config) *network.Network {
// DefaultConfig will initialize config for the network with custom application,
// genesis and single validator. All other parameters are inherited from cosmos-sdk/testutil/network.DefaultConfig.
func DefaultConfig() network.Config {
// app doesn't have these modules anymore, but we need them for test setup, which uses gentx and MsgCreateValidator
app.ModuleBasics[genutiltypes.ModuleName] = genutil.AppModuleBasic{}
app.ModuleBasics[stakingtypes.ModuleName] = staking.AppModuleBasic{}

encoding := app.MakeEncodingConfig(app.ModuleBasics)

chainID := "chain-" + tmrand.NewRand().Str(6)
return network.Config{
Codec: encoding.Marshaler,
Expand All @@ -71,7 +62,6 @@ func DefaultConfig() network.Config {
val.GetCtx().Logger, db.NewMemDB(), nil, true, map[int64]bool{}, val.GetCtx().Config.RootDir, 0,
encoding,
sims.EmptyAppOptions{},
nil,
baseapp.SetPruning(pruningtypes.NewPruningOptionsFromString(val.GetAppConfig().Pruning)),
baseapp.SetMinGasPrices(val.GetAppConfig().MinGasPrices),
baseapp.SetChainID(chainID),
Expand All @@ -80,14 +70,13 @@ func DefaultConfig() network.Config {
GenesisState: app.ModuleBasics.DefaultGenesis(encoding.Marshaler),
TimeoutCommit: 2 * time.Second,
ChainID: chainID,
// Some changes are introduced to make the tests run as if nolus is a standalone chain.
// This will only work if NumValidators is set to 1.
NumValidators: 1,
BondDenom: params.DefaultBondDenom,
MinGasPrices: fmt.Sprintf("0.000006%s", params.DefaultBondDenom),
AccountTokens: sdk.TokensFromConsensusPower(1000, sdk.DefaultPowerReduction),
StakingTokens: sdk.TokensFromConsensusPower(500, sdk.DefaultPowerReduction),
BondedTokens: sdk.TokensFromConsensusPower(100, sdk.DefaultPowerReduction),
NumValidators: 1,
BondDenom: sdk.DefaultBondDenom,
MinGasPrices: fmt.Sprintf("0.000006%s", sdk.DefaultBondDenom),
AccountTokens: sdk.TokensFromConsensusPower(1000, sdk.DefaultPowerReduction),
StakingTokens: sdk.TokensFromConsensusPower(500, sdk.DefaultPowerReduction),
BondedTokens: sdk.TokensFromConsensusPower(100, sdk.DefaultPowerReduction),

PruningStrategy: pruningtypes.PruningOptionNothing,
CleanupDir: true,
SigningAlgo: string(hd.Secp256k1Type),
Expand Down
74 changes: 16 additions & 58 deletions testutil/test_helpers.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package testutil

import (
"context"
"encoding/json"
"fmt"
"os"
"time"

"cosmossdk.io/log"

Expand All @@ -16,21 +14,20 @@ import (
"github.com/CosmWasm/wasmd/x/wasm/keeper"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"

tmrand "github.com/cometbft/cometbft/libs/rand"

db2 "github.com/cosmos/cosmos-db"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/testutil/sims"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
simcli "github.com/cosmos/cosmos-sdk/x/simulation/client/cli"
channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
"github.com/stretchr/testify/suite"

"github.com/Nolus-Protocol/nolus-core/app"
"github.com/Nolus-Protocol/nolus-core/app/params"

tmos "github.com/cometbft/cometbft/libs/os"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"

ictxstypes "github.com/Nolus-Protocol/nolus-core/x/interchaintxs/types"
)

Expand Down Expand Up @@ -90,10 +87,10 @@ func (suite *IBCConnectionTestSuite) SetupTest() {
// suite.Require().Equal("connection-0", path.EndpointA.ClientID)
// suite.Require().Equal("channel-0", path.EndpointA.ClientID)

suite.Path = NewICAPath(suite.ChainA, suite.ChainB)
// suite.Path = NewICAPath(suite.ChainA, suite.ChainB)

suite.ConfigureTransferChannel()
suite.Coordinator.Setup(suite.Path)
// suite.Coordinator.Setup(suite.Path)
}

func (suite *IBCConnectionTestSuite) ConfigureTransferChannel() {
Expand Down Expand Up @@ -220,63 +217,24 @@ func fauxMerkleModeOpt(bapp *baseapp.BaseApp) {

// SetupTestingApp initializes the IBC-go testing application.
func SetupTestingApp() (ibctesting.TestingApp, map[string]json.RawMessage) {
encoding := app.MakeEncodingConfig(app.ModuleBasics)
db := db2.NewMemDB()

temp := tempDir()

encConfig := app.MakeEncodingConfig(app.ModuleBasics)
chainID := "nolus-testapp" + tmrand.NewRand().Str(6)
testApp := app.New(
log.NewNopLogger(),
db,
nil,
false,
true,
map[int64]bool{},
temp,
0,
encoding,
sims.EmptyAppOptions{},
fauxMerkleModeOpt,
baseapp.SetChainID("nolus"),
tempDir(),
simcli.FlagPeriodValue,
encConfig,
simtestutil.EmptyAppOptions{},
baseapp.SetChainID(chainID),
baseapp.SetMinGasPrices("0unls"),
)

ctx := testApp.BaseApp.NewContextLegacy(false, tmproto.Header{Height: 1, ChainID: "testchain-1", Time: time.Now().UTC()})

// Manually set validator signing info, otherwise we panic
vals, err := testApp.StakingKeeper.GetAllValidators(context.TODO())
if err != nil {
panic(err)
}
for _, val := range vals {
consAddr, _ := val.GetConsAddr()
signingInfo := slashingtypes.NewValidatorSigningInfo(
consAddr,
ctx.BlockHeight(),
0,
time.Unix(0, 0),
false,
0,
)
err := testApp.SlashingKeeper.SetValidatorSigningInfo(ctx, consAddr, signingInfo)
if err != nil {
panic(err)
}
}

// we need to set up a TestInitChainer where we can redefine MaxBlockGas in ConsensusParamsKeeper
testApp.SetInitChainer(testApp.TestInitChainer)
// and then we manually init baseapp and load states
if err := testApp.LoadLatestVersion(); err != nil {
tmos.Exit(err.Error())
}

// Initialize pinned codes in wasmvm as they are not persisted there
if err := testApp.WasmKeeper.InitializePinnedCodes(ctx); err != nil {
panic(err)
}

genesisState := app.NewDefaultGenesisState(encoding)

// genesisState := app.NewDefaultGenesisState(encoding)
genesisState := app.NewDefaultGenesisState(encConfig)

return testApp, genesisState
}
Expand Down
4 changes: 4 additions & 0 deletions x/contractmanager/keeper/grpc_query_failure.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import (
const FailuresQueryMaxLimit uint64 = query.DefaultLimit

func (k Keeper) Failures(c context.Context, req *types.QueryFailuresRequest) (*types.QueryFailuresResponse, error) {
if req == nil {
return nil, status.Error(codes.InvalidArgument, "invalid request")
}

failures, err := k.AddressFailures(c, &types.QueryFailuresByAddressRequest{Address: req.Address, Pagination: req.Pagination})
if err != nil {
return nil, err
Expand Down
10 changes: 5 additions & 5 deletions x/contractmanager/keeper/grpc_query_failure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,29 @@ func TestFailureQuerySingle(t *testing.T) {
for _, tc := range []struct {
desc string
request *types.QueryFailuresByAddressRequest
response *types.QueryAddressFailureResponse
response *types.QueryAddressFailuresResponse
err error
}{
{
desc: "First",
request: &types.QueryFailuresByAddressRequest{
Address: msgs[0][0].Address,
},
response: &types.QueryAddressFailureResponse{Failures: msgs[0], Pagination: &query.PageResponse{Total: 2}},
response: &types.QueryAddressFailuresResponse{Failures: msgs[0], Pagination: &query.PageResponse{Total: 2}},
},
{
desc: "Second",
request: &types.QueryFailuresByAddressRequest{
Address: msgs[1][0].Address,
},
response: &types.QueryAddressFailureResponse{Failures: msgs[1], Pagination: &query.PageResponse{Total: 2}},
response: &types.QueryAddressFailuresResponse{Failures: msgs[1], Pagination: &query.PageResponse{Total: 2}},
},
{
desc: "KeyIsAbsent",
request: &types.QueryFailuresByAddressRequest{
Address: "nolus17dtl0mjt3t77kpuhg2edqzjpszulwhgzcdvagh",
Address: "nolus1f6cu6ypvpyh0p8d7pqnps2pduj87hda5t9v4mqrc8ra67xp28uwq4f4ysz",
},
response: &types.QueryAddressFailureResponse{Failures: []types.Failure{}, Pagination: &query.PageResponse{Total: 0}},
response: &types.QueryAddressFailuresResponse{Failures: []types.Failure{}, Pagination: &query.PageResponse{Total: 0}},
},
{
desc: "InvalidAddress",
Expand Down
2 changes: 1 addition & 1 deletion x/contractmanager/migrations/v2/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (suite *V2ContractManagerMigrationTestSuite) TestFailuresUpgrade() {
)

addressOne := testutil.TestOwnerAddress
addressTwo := "nolus1fxudpred77a0grgh69u0j7y84yks5ev4n5050z45kecz792jnd6scqu98z"
addressTwo := "nolus17p9rzwnnfxcjp32un9ug7yhhzgtkhvl9jfksztgw5uh69wac2pgsmc5xhq"

// Write old state
store := ctx.KVStore(storeKey)
Expand Down
6 changes: 3 additions & 3 deletions x/feerefunder/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (k Keeper) DistributeAcknowledgementFee(ctx context.Context, receiver sdk.A
// try to return unused timeout fee
if err := k.distributeFee(c, sdk.MustAccAddressFromBech32(feeInfo.Payer), feeInfo.Fee.TimeoutFee); err != nil {
k.Logger(c).Error("error returning unused timeout fee", "receiver", feeInfo.Payer, "packet", packetID)
panic(errors.Wrapf(err, "error distributing unused timeout fee: receiver = %s, packetID=%v", feeInfo.Payer, packetID))
panic(errors.Wrapf(err, "error distributing unused timeout fee: receiver = %s, packetID=%v", receiver, packetID))
}

c.EventManager().EmitEvents(sdk.Events{
Expand Down Expand Up @@ -153,8 +153,8 @@ func (k Keeper) DistributeTimeoutFee(ctx context.Context, receiver sdk.AccAddres

// try to return unused ack fee
if err := k.distributeFee(c, sdk.MustAccAddressFromBech32(feeInfo.Payer), feeInfo.Fee.AckFee); err != nil {
k.Logger(c).Error("error returning unused ack fee", "receiver", feeInfo.Payer, "packet", packetID)
panic(errors.Wrapf(err, "error distributing unused ack fee: receiver = %s, packetID=%v", feeInfo.Payer, packetID))
k.Logger(c).Error("error returning unused ack fee", "receiver", receiver, "packet", packetID)
panic(errors.Wrapf(err, "error distributing unused ack fee: receiver = %s, packetID=%v", receiver, packetID))
}

c.EventManager().EmitEvents(sdk.Events{
Expand Down
Loading

0 comments on commit e17058b

Please sign in to comment.