Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Lockwarr committed Nov 11, 2024
1 parent 92c1605 commit af63941
Show file tree
Hide file tree
Showing 191 changed files with 39,511 additions and 357 deletions.
79 changes: 41 additions & 38 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import (

"github.com/spf13/cast"

capabilitykeeper "github.com/cosmos/ibc-go/modules/capability/keeper"
ibckeeper "github.com/cosmos/ibc-go/v8/modules/core/keeper"
ibctestingtypes "github.com/cosmos/ibc-go/v8/testing/types"

autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
reflectionv1 "cosmossdk.io/api/cosmos/reflection/v1"
"cosmossdk.io/client/v2/autocli"
Expand Down Expand Up @@ -487,41 +491,40 @@ func (app *App) RegisterNodeService(clientCtx client.Context, cfg config.Config)
nodeservice.RegisterNodeService(clientCtx, app.GRPCQueryRouter(), cfg)
}

// TODO
// // GetTxConfig implements the TestingApp interface.
// func (app *App) GetTxConfig() client.TxConfig {
// return app.encodingConfig.TxConfig
// }

// // GetIBCKeeper implements the TestingApp interface.
// func (app *App) GetIBCKeeper() *ibckeeper.Keeper {
// return app.IBCKeeper
// }

// // GetScopedIBCKeeper implements the TestingApp interface.
// func (app *App) GetScopedIBCKeeper() capabilitykeeper.ScopedKeeper {
// return app.ScopedIBCKeeper
// }

// // GetStakingKeeper implements the TestingApp interface.
// func (app *App) GetStakingKeeper() ibctestingtypes.StakingKeeper {
// return app.StakingKeeper
// }

// // InitChainer application update at chain initialization.
// // ONLY FOR TESTING PURPOSES.
// func (app *App) TestInitChainer(ctx sdk.Context, req *abci.RequestInitChain) (*abci.ResponseInitChain, error) {
// var genesisState GenesisState
// if err := tmjson.Unmarshal(req.AppStateBytes, &genesisState); err != nil {
// panic(err)
// }

// // manually set consensus params here, cause there is no way to set it using ibctesting stuff for now
// // TODO: app.ConsensusParamsKeeper.Set(ctx, sims.DefaultConsensusParams)

// err := app.UpgradeKeeper.SetModuleVersionMap(ctx, app.mm.GetVersionMap())
// if err != nil {
// return nil, fmt.Errorf("failed to set module version map: %w", err)
// }
// return app.mm.InitGenesis(ctx, app.appCodec, genesisState)
// }
// GetTxConfig implements the TestingApp interface.
func (app *App) GetTxConfig() client.TxConfig {
return app.encodingConfig.TxConfig
}

// GetIBCKeeper implements the TestingApp interface.
func (app *App) GetIBCKeeper() *ibckeeper.Keeper {
return app.IBCKeeper
}

// GetScopedIBCKeeper implements the TestingApp interface.
func (app *App) GetScopedIBCKeeper() capabilitykeeper.ScopedKeeper {
return app.ScopedIBCKeeper
}

// GetStakingKeeper implements the TestingApp interface.
func (app *App) GetStakingKeeper() ibctestingtypes.StakingKeeper {
return app.StakingKeeper
}

// InitChainer application update at chain initialization.
// ONLY FOR TESTING PURPOSES.
func (app *App) TestInitChainer(ctx sdk.Context, req *abci.RequestInitChain) (*abci.ResponseInitChain, error) {
var genesisState GenesisState
if err := tmjson.Unmarshal(req.AppStateBytes, &genesisState); err != nil {
panic(err)
}

// manually set consensus params here, cause there is no way to set it using ibctesting stuff for now
// TODO: app.ConsensusParamsKeeper.Set(ctx, sims.DefaultConsensusParams)

err := app.UpgradeKeeper.SetModuleVersionMap(ctx, app.mm.GetVersionMap())
if err != nil {
return nil, fmt.Errorf("failed to set module version map: %w", err)
}
return app.mm.InitGenesis(ctx, app.appCodec, genesisState)
}
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@ require (
cosmossdk.io/x/feegrant v0.1.1
cosmossdk.io/x/tx v0.13.4
cosmossdk.io/x/upgrade v0.1.4
github.com/cosmos/admin-module/v2 v2.0.0-20240430142959-8b3328d1b1a2
github.com/cosmos/cosmos-db v1.0.2
github.com/cosmos/cosmos-proto v1.0.0-beta.5
github.com/cosmos/ibc-go/modules/capability v1.0.1
github.com/cosmos/ics23/go v0.10.0
github.com/gogo/protobuf v1.3.3
github.com/golang/mock v1.6.0
github.com/hashicorp/go-metrics v0.5.3
github.com/pkg/errors v0.9.1
google.golang.org/genproto/googleapis/api v0.0.0-20240610135401-a8a62080eff3
google.golang.org/protobuf v1.34.2
gotest.tools/v3 v3.5.1
Expand Down Expand Up @@ -81,7 +85,6 @@ require (
github.com/cosmos/go-bip39 v1.0.0 // indirect
github.com/cosmos/gogogateway v1.2.0 // indirect
github.com/cosmos/iavl v1.2.0 // indirect
github.com/cosmos/ics23/go v0.10.0 // indirect
github.com/cosmos/ledger-cosmos-go v0.13.3 // indirect
github.com/creachadair/atomicfile v0.3.3 // indirect
github.com/creachadair/tomledit v0.0.26 // indirect
Expand All @@ -107,7 +110,6 @@ require (
github.com/go-logr/stdr v1.2.2 // indirect
github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 // indirect
github.com/gogo/googleapis v1.4.1 // indirect
github.com/gogo/protobuf v1.3.3 // indirect
github.com/golang/glog v1.2.1 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect
Expand Down Expand Up @@ -165,7 +167,6 @@ require (
github.com/opencontainers/runc v1.1.12 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/petermattis/goid v0.0.0-20231207134359-e60b3f734c67 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/prometheus/client_golang v1.20.1 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
Expand Down
57 changes: 57 additions & 0 deletions testutil/common/nullify/nullify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Package nullify provides methods to init nil values structs for test assertion.
package nullify

import (
"reflect"
"unsafe"

sdk "github.com/cosmos/cosmos-sdk/types"
)

var (
coinType = reflect.TypeOf(sdk.Coin{})
coinsType = reflect.TypeOf(sdk.Coins{})
)

// Fill analyze all struct fields and slices with
// reflection and initialize the nil and empty slices,
// structs, and pointers.
func Fill(x interface{}) interface{} {
v := reflect.Indirect(reflect.ValueOf(x))
switch v.Kind() {
case reflect.Slice:
for i := 0; i < v.Len(); i++ {
obj := v.Index(i)
objPt := reflect.NewAt(obj.Type(), unsafe.Pointer(obj.UnsafeAddr())).Interface()
objPt = Fill(objPt)
obj.Set(reflect.ValueOf(objPt))
}
case reflect.Struct:
for i := 0; i < v.NumField(); i++ {
f := reflect.Indirect(v.Field(i))
if !f.CanSet() {
continue
}
switch f.Kind() {
case reflect.Slice:
f.Set(reflect.MakeSlice(f.Type(), 0, 0))
case reflect.Struct:
switch f.Type() {
case coinType:
coin := reflect.New(coinType).Interface()
s := reflect.ValueOf(coin).Elem()
f.Set(s)
case coinsType:
coins := reflect.New(coinsType).Interface()
s := reflect.ValueOf(coins).Elem()
f.Set(s)
default:
objPt := reflect.NewAt(f.Type(), unsafe.Pointer(f.UnsafeAddr())).Interface()
s := Fill(objPt)
f.Set(reflect.ValueOf(s))
}
}
}
}
return reflect.Indirect(v).Interface()
}
13 changes: 13 additions & 0 deletions testutil/common/sample/sample.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package sample

import (
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// AccAddress returns a sample account address
func AccAddress() string {
pk := ed25519.GenPrivKey().PubKey()
addr := pk.Address()
return sdk.AccAddress(addr).String()
}
52 changes: 52 additions & 0 deletions testutil/contractmanager/keeper/contractmanager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package keeper

import (
"testing"

"cosmossdk.io/log"
metrics2 "cosmossdk.io/store/metrics"
db2 "github.com/cosmos/cosmos-db"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"

"cosmossdk.io/store"
storetypes "cosmossdk.io/store/types"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/stretchr/testify/require"

"github.com/Nolus-Protocol/nolus-core/x/contractmanager/keeper"
"github.com/Nolus-Protocol/nolus-core/x/contractmanager/types"
)

func ContractManagerKeeper(t testing.TB, wasmKeeper types.WasmKeeper) (*keeper.Keeper, sdk.Context) {
storeKey := storetypes.NewKVStoreKey(types.StoreKey)
memStoreKey := storetypes.NewMemoryStoreKey(types.MemStoreKey)

db := db2.NewMemDB()
stateStore := store.NewCommitMultiStore(db, log.NewNopLogger(), metrics2.NewNoOpMetrics())
stateStore.MountStoreWithDB(storeKey, storetypes.StoreTypeIAVL, db)
stateStore.MountStoreWithDB(memStoreKey, storetypes.StoreTypeMemory, nil)
require.NoError(t, stateStore.LoadLatestVersion())

registry := codectypes.NewInterfaceRegistry()
cdc := codec.NewProtoCodec(registry)

k := keeper.NewKeeper(
cdc,
storeKey,
memStoreKey,
wasmKeeper,
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)

ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger())

// Initialize params
err := k.SetParams(ctx, types.DefaultParams())
require.NoError(t, err)

return k, ctx
}
96 changes: 96 additions & 0 deletions testutil/contractmanager/network/network.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package network

import (
"fmt"
"testing"
"time"

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

pruningtypes "cosmossdk.io/store/pruning/types"
tmrand "github.com/cometbft/cometbft/libs/rand"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
"github.com/cosmos/cosmos-sdk/testutil/network"
"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"
)

type (
Network = network.Network
Config = network.Config
)

// New creates instance with fully configured cosmos network.
// Accepts optional config, that will be used in place of the DefaultConfig() if provided.
func New(t *testing.T, configs ...network.Config) *network.Network {
if len(configs) > 1 {
panic("at most one config should be provided")
}
var cfg network.Config
if len(configs) == 0 {
cfg = DefaultConfig()
} else {
cfg = configs[0]
}
net, err := network.New(t, t.TempDir(), cfg)
require.NoError(t, err)
t.Cleanup(net.Cleanup)
return net
}

// 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()
chainID := "chain-" + tmrand.NewRand().Str(6)
return network.Config{
Codec: encoding.Marshaler,
TxConfig: encoding.TxConfig,
LegacyAmino: encoding.Amino,
InterfaceRegistry: encoding.InterfaceRegistry,
AccountRetriever: authtypes.AccountRetriever{},
AppConstructor: func(val network.ValidatorI) servertypes.Application {
return app.New(
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),
)
},
GenesisState: app.ModuleBasics.DefaultGenesis(encoding.Marshaler),
TimeoutCommit: 2 * time.Second,
ChainID: chainID,
// Some changes are introduced to make the tests run as if neutron 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),
PruningStrategy: pruningtypes.PruningOptionNothing,
CleanupDir: true,
SigningAlgo: string(hd.Secp256k1Type),
KeyringOptions: []keyring.Option{},
}
}
Loading

0 comments on commit af63941

Please sign in to comment.