Skip to content

Commit

Permalink
chore: add genesis tests (#286)
Browse files Browse the repository at this point in the history
add airdrop and oracle genesis tests
  • Loading branch information
zarazan authored Oct 5, 2023
1 parent ecee0c2 commit ef2d082
Show file tree
Hide file tree
Showing 8 changed files with 410 additions and 148 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ require (
google.golang.org/genproto/googleapis/api v0.0.0-20230711160842-782d3b101e98
google.golang.org/grpc v1.58.2
gopkg.in/yaml.v3 v3.0.1
gotest.tools/v3 v3.5.1
mvdan.cc/gofumpt v0.5.0
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2253,9 +2253,9 @@ gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
gotest.tools/v3 v3.5.0 h1:Ljk6PdHdOhAb5aDMWXjDLMMhph+BpztA4v1QdqEW2eY=
gotest.tools/v3 v3.5.1 h1:EENdUnS3pdur5nybKYIh2Vfgc8IUNBjxDPSjtiJcOzU=
gotest.tools/v3 v3.5.1/go.mod h1:isy3WKz7GK6uNw/sbHzfKBLvlvXwUyV06n6brMxxopU=
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
109 changes: 109 additions & 0 deletions tests/integration/test_helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package integration

import (
"fmt"
"testing"

tmrand "github.com/cometbft/cometbft/libs/rand"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
"github.com/cosmos/cosmos-sdk/x/staking"
stakingtestutil "github.com/cosmos/cosmos-sdk/x/staking/testutil"
"github.com/stretchr/testify/require"

ojoapp "github.com/ojo-network/ojo/app"
appparams "github.com/ojo-network/ojo/app/params"
airdropkeeper "github.com/ojo-network/ojo/x/airdrop/keeper"
airdroptypes "github.com/ojo-network/ojo/x/airdrop/types"
oraclekeeper "github.com/ojo-network/ojo/x/oracle/keeper"
oracletypes "github.com/ojo-network/ojo/x/oracle/types"
)

const (
bondDenom = appparams.BondDenom
initialPower = int64(1000)
isCheckTx = false
)

var validatorPowers = []int64{599, 398, 2}

type TestValidatorKey struct {
PubKey cryptotypes.PubKey
ValAddress sdk.ValAddress
AccAddress sdk.AccAddress
Power int64
}

func CreateTestValidatorKeys(numValidators int) []TestValidatorKey {
var validatorKeys []TestValidatorKey

for i := 0; i < numValidators; i++ {
pubKey := secp256k1.GenPrivKey().PubKey()
valInfo := TestValidatorKey{
PubKey: pubKey,
ValAddress: sdk.ValAddress(pubKey.Address()),
AccAddress: sdk.AccAddress(pubKey.Address()),
Power: validatorPowers[i],
}
validatorKeys = append(validatorKeys, valInfo)
}

return validatorKeys
}

func SetupAppWithContext(
t *testing.T,
) (
*ojoapp.App,
sdk.Context,
[]TestValidatorKey,
) {
config := sdk.GetConfig()
config.SetBech32PrefixForAccount(appparams.AccountAddressPrefix, appparams.AccountPubKeyPrefix)
config.SetBech32PrefixForValidator(appparams.ValidatorAddressPrefix, appparams.ValidatorPubKeyPrefix)
config.SetBech32PrefixForConsensusNode(appparams.ConsNodeAddressPrefix, appparams.ConsNodePubKeyPrefix)

app := ojoapp.Setup(t)
ctx := app.BaseApp.NewContext(isCheckTx, tmproto.Header{
ChainID: fmt.Sprintf("test-chain-%s", tmrand.Str(4)),
Height: 9,
})

queryHelper := baseapp.NewQueryServerTestHelper(ctx, app.InterfaceRegistry())
oracletypes.RegisterQueryServer(queryHelper, oraclekeeper.NewQuerier(app.OracleKeeper))
airdroptypes.RegisterQueryServer(queryHelper, airdropkeeper.NewQuerier(app.AirdropKeeper))

sh := stakingtestutil.NewHelper(t, ctx, app.StakingKeeper)
sh.Denom = bondDenom

// amt := sdk.TokensFromConsensusPower(100, sdk.DefaultPowerReduction)

// make validators 60%, 29%, 1% of total power

initTokens := sdk.TokensFromConsensusPower(initialPower, sdk.DefaultPowerReduction)
initCoins := sdk.NewCoins(sdk.NewCoin(appparams.BondDenom, initTokens))

validatorKeys := CreateTestValidatorKeys(3)

// mint and send coins to validators
for _, val := range validatorKeys {
require.NoError(t, app.BankKeeper.MintCoins(ctx, minttypes.ModuleName, initCoins))
require.NoError(t, app.BankKeeper.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, val.AccAddress, initCoins))
sh.CreateValidatorWithValPower(val.ValAddress, val.PubKey, val.Power, true)
// sh.CreateValidator(val.ValAddress, val.PubKey, amt, true)
}

// mint and send coins to oracle module to fill up reward pool
require.NoError(t, app.BankKeeper.MintCoins(ctx, minttypes.ModuleName, initCoins))
require.NoError(t,
app.BankKeeper.SendCoinsFromModuleToModule(ctx, minttypes.ModuleName, oracletypes.ModuleName, initCoins),
)

staking.EndBlocker(ctx, app.StakingKeeper)

return app, ctx, validatorKeys
}
80 changes: 6 additions & 74 deletions x/airdrop/abci_test.go
Original file line number Diff line number Diff line change
@@ -1,105 +1,37 @@
package airdrop_test

import (
"fmt"
"testing"

"github.com/cometbft/cometbft/crypto/secp256k1"
tmrand "github.com/cometbft/cometbft/libs/rand"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
"github.com/cosmos/cosmos-sdk/x/staking"
stakingtestutil "github.com/cosmos/cosmos-sdk/x/staking/testutil"
"github.com/stretchr/testify/suite"

ojoapp "github.com/ojo-network/ojo/app"
appparams "github.com/ojo-network/ojo/app/params"
"github.com/ojo-network/ojo/tests/integration"
"github.com/ojo-network/ojo/x/airdrop"
"github.com/ojo-network/ojo/x/airdrop/types"
)

const (
displayDenom string = appparams.DisplayDenom
bondDenom string = appparams.BondDenom
)
const bondDenom = appparams.BondDenom

type IntegrationTestSuite struct {
suite.Suite

ctx sdk.Context
app *ojoapp.App
ctx sdk.Context
app *ojoapp.App
keys []integration.TestValidatorKey
}

const (
initialPower = int64(1000)
)

func TestIntegrationTestSuite(t *testing.T) {
suite.Run(t, new(IntegrationTestSuite))
}

func (s *IntegrationTestSuite) SetupTest() {
require := s.Require()
isCheckTx := false
app := ojoapp.Setup(s.T())
ctx := app.BaseApp.NewContext(isCheckTx, tmproto.Header{
ChainID: fmt.Sprintf("test-chain-%s", tmrand.Str(4)),
})

airdrop.InitGenesis(ctx, app.AirdropKeeper, *types.DefaultGenesisState())

setupVals := app.StakingKeeper.GetBondedValidatorsByPower(ctx)
s.Require().Len(setupVals, 1)
s.Require().Equal(int64(1), setupVals[0].GetConsensusPower(app.StakingKeeper.PowerReduction(ctx)))

sh := stakingtestutil.NewHelper(s.T(), ctx, app.StakingKeeper)
sh.Denom = bondDenom

// mint and send coins to validators
require.NoError(app.BankKeeper.MintCoins(ctx, minttypes.ModuleName, initCoins.MulInt(sdk.NewIntFromUint64(3))))
require.NoError(app.BankKeeper.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, addr1, initCoins))
require.NoError(app.BankKeeper.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, addr2, initCoins))
require.NoError(app.BankKeeper.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, addr3, initCoins))

// mint and send coins to oracle module to fill up reward pool
require.NoError(app.BankKeeper.MintCoins(ctx, minttypes.ModuleName, initCoins))
require.NoError(app.BankKeeper.SendCoinsFromModuleToModule(ctx, minttypes.ModuleName, types.ModuleName, initCoins))

sh.CreateValidatorWithValPower(valAddr1, valPubKey1, 599, true)
sh.CreateValidatorWithValPower(valAddr2, valPubKey2, 398, true)
sh.CreateValidatorWithValPower(valAddr3, valPubKey3, 2, true)

staking.EndBlocker(ctx, app.StakingKeeper)

s.app = app
s.ctx = ctx
s.app, s.ctx, s.keys = integration.SetupAppWithContext(s.T())
}

// Test addresses
var (
valPubKeys = simtestutil.CreateTestPubKeys(3)

valPubKey1 = valPubKeys[0]
pubKey1 = secp256k1.GenPrivKey().PubKey()
addr1 = sdk.AccAddress(pubKey1.Address())
valAddr1 = sdk.ValAddress(pubKey1.Address())

valPubKey2 = valPubKeys[1]
pubKey2 = secp256k1.GenPrivKey().PubKey()
addr2 = sdk.AccAddress(pubKey2.Address())
valAddr2 = sdk.ValAddress(pubKey2.Address())

valPubKey3 = valPubKeys[2]
pubKey3 = secp256k1.GenPrivKey().PubKey()
addr3 = sdk.AccAddress(pubKey3.Address())
valAddr3 = sdk.ValAddress(pubKey3.Address())

initTokens = sdk.TokensFromConsensusPower(initialPower, sdk.DefaultPowerReduction)
initCoins = sdk.NewCoins(sdk.NewCoin(bondDenom, initTokens))
)

func (s *IntegrationTestSuite) TestEndBlockerAccountCreation() {
app, ctx := s.app, s.ctx

Expand Down
47 changes: 47 additions & 0 deletions x/airdrop/genesis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package airdrop_test

import (
"gotest.tools/v3/assert"

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

"github.com/ojo-network/ojo/x/airdrop"
"github.com/ojo-network/ojo/x/airdrop/types"
)

func (s *IntegrationTestSuite) TestGenesis_InitGenesis() {
keeper, ctx := s.app.AirdropKeeper, s.ctx

genesisState := types.GenesisState{
Params: types.DefaultParams(),
AirdropAccounts: []*types.AirdropAccount{},
}

s.Assertions.NotPanics(func() { airdrop.InitGenesis(ctx, keeper, genesisState) })
}

func (s *IntegrationTestSuite) TestGenesis_ExportGenesis() {
keeper, ctx := s.app.AirdropKeeper, s.ctx

params := types.DefaultParams()

airdropAccounts := []*types.AirdropAccount{
{
VestingEndTime: 100,
OriginAddress: "ojo1ner6kc63xl903wrv2n8p9mtun79gegjld93lx0",
OriginAmount: sdk.NewInt(100).Uint64(),
},
}

genesisState := types.GenesisState{
Params: params,
AirdropAccounts: airdropAccounts,
}

airdrop.InitGenesis(ctx, keeper, genesisState)

result := airdrop.ExportGenesis(ctx, keeper)

assert.DeepEqual(s.T(), params, result.Params)
assert.DeepEqual(s.T(), airdropAccounts, result.AirdropAccounts)
}
76 changes: 7 additions & 69 deletions x/airdrop/keeper/keeper_suite_test.go
Original file line number Diff line number Diff line change
@@ -1,99 +1,37 @@
package keeper_test

import (
"fmt"
"testing"

"github.com/cometbft/cometbft/crypto/secp256k1"
tmrand "github.com/cometbft/cometbft/libs/rand"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/cosmos/cosmos-sdk/baseapp"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
"github.com/cosmos/cosmos-sdk/x/staking"
stakingtestutil "github.com/cosmos/cosmos-sdk/x/staking/testutil"
"github.com/stretchr/testify/suite"

ojoapp "github.com/ojo-network/ojo/app"
appparams "github.com/ojo-network/ojo/app/params"
"github.com/ojo-network/ojo/tests/integration"
"github.com/ojo-network/ojo/x/airdrop/keeper"
"github.com/ojo-network/ojo/x/airdrop/types"
)

const (
displayDenom string = appparams.DisplayDenom
bondDenom string = appparams.BondDenom

initialPower = int64(10000000000)
)

// Test addresses
var (
valPubKeys = simtestutil.CreateTestPubKeys(2)

valPubKey = valPubKeys[0]
pubKey = secp256k1.GenPrivKey().PubKey()
addr = sdk.AccAddress(pubKey.Address())
valAddr = sdk.ValAddress(pubKey.Address())

valPubKey2 = valPubKeys[1]
pubKey2 = secp256k1.GenPrivKey().PubKey()
addr2 = sdk.AccAddress(pubKey2.Address())
valAddr2 = sdk.ValAddress(pubKey2.Address())

initTokens = sdk.TokensFromConsensusPower(initialPower, sdk.DefaultPowerReduction)
initCoins = sdk.NewCoins(sdk.NewCoin(appparams.BondDenom, initTokens))
)

type IntegrationTestSuite struct {
suite.Suite

ctx sdk.Context
app *ojoapp.App
queryClient types.QueryClient
msgServer types.MsgServer
ctx sdk.Context
app *ojoapp.App
keys []integration.TestValidatorKey
msgServer types.MsgServer
}

func TestIntegrationTestSuite(t *testing.T) {
suite.Run(t, new(IntegrationTestSuite))
}

func (s *IntegrationTestSuite) SetupSuite() {
require := s.Require()
isCheckTx := false

config := sdk.GetConfig()
config.SetBech32PrefixForAccount(appparams.AccountAddressPrefix, appparams.AccountPubKeyPrefix)
config.SetBech32PrefixForValidator(appparams.ValidatorAddressPrefix, appparams.ValidatorPubKeyPrefix)
config.SetBech32PrefixForConsensusNode(appparams.ConsNodeAddressPrefix, appparams.ConsNodePubKeyPrefix)

app := ojoapp.Setup(s.T())
ctx := app.BaseApp.NewContext(isCheckTx, tmproto.Header{
ChainID: fmt.Sprintf("test-chain-%s", tmrand.Str(4)),
Height: 9,
})

queryHelper := baseapp.NewQueryServerTestHelper(ctx, app.InterfaceRegistry())
types.RegisterQueryServer(queryHelper, keeper.NewQuerier(app.AirdropKeeper))

sh := stakingtestutil.NewHelper(s.T(), ctx, app.StakingKeeper)
sh.Denom = bondDenom
amt := sdk.TokensFromConsensusPower(100, sdk.DefaultPowerReduction)

// mint and send coins to validators
require.NoError(app.BankKeeper.MintCoins(ctx, minttypes.ModuleName, initCoins))
require.NoError(app.BankKeeper.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, addr, initCoins))
require.NoError(app.BankKeeper.MintCoins(ctx, minttypes.ModuleName, initCoins))
require.NoError(app.BankKeeper.SendCoinsFromModuleToAccount(ctx, minttypes.ModuleName, addr2, initCoins))

sh.CreateValidator(valAddr, valPubKey, amt, true)
sh.CreateValidator(valAddr2, valPubKey2, amt, true)

staking.EndBlocker(ctx, app.StakingKeeper)

s.app = app
s.ctx = ctx
s.queryClient = types.NewQueryClient(queryHelper)
s.msgServer = keeper.NewMsgServerImpl(app.AirdropKeeper)
s.app, s.ctx, s.keys = integration.SetupAppWithContext(s.T())
s.msgServer = keeper.NewMsgServerImpl(s.app.AirdropKeeper)
}
Loading

0 comments on commit ef2d082

Please sign in to comment.