-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add unit test with mocks to x/genutil (#13007)
## Description Closes: [#12759](#12759) --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
- Loading branch information
1 parent
ef4ad67
commit 07c549b
Showing
3 changed files
with
524 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,337 @@ | ||
package genutil_test | ||
|
||
import ( | ||
"cosmossdk.io/math" | ||
"encoding/json" | ||
"fmt" | ||
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" | ||
"github.com/cosmos/cosmos-sdk/testutil" | ||
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" | ||
|
||
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" | ||
"github.com/cosmos/cosmos-sdk/x/genutil" | ||
genutiltestutil "github.com/cosmos/cosmos-sdk/x/genutil/testutil" | ||
"github.com/cosmos/cosmos-sdk/x/genutil/types" | ||
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" | ||
"github.com/golang/mock/gomock" | ||
"github.com/stretchr/testify/suite" | ||
abci "github.com/tendermint/tendermint/abci/types" | ||
"math/rand" | ||
"testing" | ||
"time" | ||
) | ||
|
||
var ( | ||
priv1 = secp256k1.GenPrivKey() | ||
priv2 = secp256k1.GenPrivKey() | ||
pk1 = priv1.PubKey() | ||
pk2 = priv2.PubKey() | ||
addr1 = sdk.AccAddress(pk1.Address()) | ||
addr2 = sdk.AccAddress(pk2.Address()) | ||
desc = stakingtypes.NewDescription("testname", "", "", "", "") | ||
comm = stakingtypes.CommissionRates{} | ||
) | ||
|
||
// GenTxTestSuite is a test suite to be used with gentx tests. | ||
type GenTxTestSuite struct { | ||
suite.Suite | ||
|
||
ctx sdk.Context | ||
|
||
stakingKeeper *genutiltestutil.MockStakingKeeper | ||
encodingConfig moduletestutil.TestEncodingConfig | ||
msg1, msg2 *stakingtypes.MsgCreateValidator | ||
} | ||
|
||
func (suite *GenTxTestSuite) SetupTest() { | ||
suite.encodingConfig = moduletestutil.MakeTestEncodingConfig(genutil.AppModuleBasic{}) | ||
key := sdk.NewKVStoreKey("a_Store_Key") | ||
tkey := sdk.NewTransientStoreKey("a_transient_store") | ||
suite.ctx = testutil.DefaultContext(key, tkey) | ||
|
||
ctrl := gomock.NewController(suite.T()) | ||
suite.stakingKeeper = genutiltestutil.NewMockStakingKeeper(ctrl) | ||
|
||
stakingtypes.RegisterInterfaces(suite.encodingConfig.InterfaceRegistry) | ||
banktypes.RegisterInterfaces(suite.encodingConfig.InterfaceRegistry) | ||
|
||
var err error | ||
amount := sdk.NewInt64Coin(sdk.DefaultBondDenom, 50) | ||
one := math.OneInt() | ||
suite.msg1, err = stakingtypes.NewMsgCreateValidator( | ||
sdk.ValAddress(pk1.Address()), pk1, amount, desc, comm, one) | ||
suite.NoError(err) | ||
suite.msg2, err = stakingtypes.NewMsgCreateValidator( | ||
sdk.ValAddress(pk2.Address()), pk1, amount, desc, comm, one) | ||
suite.NoError(err) | ||
} | ||
|
||
func (suite *GenTxTestSuite) setAccountBalance(balances []banktypes.Balance) json.RawMessage { | ||
bankGenesisState := banktypes.GenesisState{ | ||
Params: banktypes.Params{DefaultSendEnabled: true}, | ||
Balances: []banktypes.Balance{ | ||
{ | ||
Address: "cosmos1fl48vsnmsdzcv85q5d2q4z5ajdha8yu34mf0eh", | ||
Coins: sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 1000000)}, | ||
}, | ||
{ | ||
Address: "cosmos1jv65s3grqf6v6jl3dp4t6c9t9rk99cd88lyufl", | ||
Coins: sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 2059726)}, | ||
}, | ||
{ | ||
Address: "cosmos1k5lndq46x9xpejdxq52q3ql3ycrphg4qxlfqn7", | ||
Coins: sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 100000000000000)}, | ||
}, | ||
}, | ||
Supply: sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 0)}, | ||
} | ||
for _, balance := range balances { | ||
bankGenesisState.Balances = append(bankGenesisState.Balances, balance) | ||
|
||
} | ||
for _, balance := range bankGenesisState.Balances { | ||
bankGenesisState.Supply.Add(balance.Coins...) | ||
} | ||
bankGenesis, err := suite.encodingConfig.Amino.MarshalJSON(bankGenesisState) // TODO switch this to use Marshaler | ||
suite.Require().NoError(err) | ||
|
||
return bankGenesis | ||
} | ||
|
||
func (suite *GenTxTestSuite) TestSetGenTxsInAppGenesisState() { | ||
var ( | ||
txBuilder = suite.encodingConfig.TxConfig.NewTxBuilder() | ||
genTxs []sdk.Tx | ||
) | ||
|
||
testCases := []struct { | ||
msg string | ||
malleate func() | ||
expPass bool | ||
}{ | ||
{ | ||
"one genesis transaction", | ||
func() { | ||
err := txBuilder.SetMsgs(suite.msg1) | ||
suite.Require().NoError(err) | ||
tx := txBuilder.GetTx() | ||
genTxs = []sdk.Tx{tx} | ||
}, | ||
true, | ||
}, | ||
{ | ||
"two genesis transactions", | ||
func() { | ||
err := txBuilder.SetMsgs(suite.msg1, suite.msg2) | ||
suite.Require().NoError(err) | ||
tx := txBuilder.GetTx() | ||
genTxs = []sdk.Tx{tx} | ||
}, | ||
true, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() { | ||
suite.SetupTest() | ||
cdc := suite.encodingConfig.Codec | ||
txJSONEncoder := suite.encodingConfig.TxConfig.TxJSONEncoder() | ||
|
||
tc.malleate() | ||
appGenesisState, err := genutil.SetGenTxsInAppGenesisState(cdc, txJSONEncoder, make(map[string]json.RawMessage), genTxs) | ||
|
||
if tc.expPass { | ||
suite.Require().NoError(err) | ||
suite.Require().NotNil(appGenesisState[types.ModuleName]) | ||
|
||
var genesisState types.GenesisState | ||
err := cdc.UnmarshalJSON(appGenesisState[types.ModuleName], &genesisState) | ||
suite.Require().NoError(err) | ||
suite.Require().NotNil(genesisState.GenTxs) | ||
} else { | ||
suite.Require().Error(err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func (suite *GenTxTestSuite) TestValidateAccountInGenesis() { | ||
var ( | ||
appGenesisState = make(map[string]json.RawMessage) | ||
coins sdk.Coins | ||
) | ||
|
||
testCases := []struct { | ||
msg string | ||
malleate func() | ||
expPass bool | ||
}{ | ||
{ | ||
"no accounts", | ||
func() { | ||
coins = sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 0)} | ||
}, | ||
false, | ||
}, | ||
{ | ||
"account without balance in the genesis state", | ||
func() { | ||
coins = sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 0)} | ||
balances := banktypes.Balance{ | ||
Address: addr2.String(), | ||
Coins: sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 50)}, | ||
} | ||
appGenesisState[banktypes.ModuleName] = suite.setAccountBalance([]banktypes.Balance{balances}) | ||
}, | ||
false, | ||
}, | ||
{ | ||
"account without enough funds of default bond denom", | ||
func() { | ||
coins = sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 50)} | ||
balances := banktypes.Balance{ | ||
Address: addr1.String(), | ||
Coins: sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 25)}, | ||
} | ||
appGenesisState[banktypes.ModuleName] = suite.setAccountBalance([]banktypes.Balance{balances}) | ||
}, | ||
false, | ||
}, | ||
{ | ||
"account with enough funds of default bond denom", | ||
func() { | ||
coins = sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 10)} | ||
balances := banktypes.Balance{ | ||
Address: addr1.String(), | ||
Coins: sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 25)}, | ||
} | ||
appGenesisState[banktypes.ModuleName] = suite.setAccountBalance([]banktypes.Balance{balances}) | ||
}, | ||
true, | ||
}, | ||
} | ||
for _, tc := range testCases { | ||
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() { | ||
suite.SetupTest() | ||
cdc := suite.encodingConfig.Codec | ||
|
||
stakingGenesis, err := cdc.MarshalJSON(&stakingtypes.GenesisState{Params: stakingtypes.DefaultParams()}) // TODO switch this to use Marshaler | ||
suite.Require().NoError(err) | ||
appGenesisState[stakingtypes.ModuleName] = stakingGenesis | ||
|
||
tc.malleate() | ||
err = genutil.ValidateAccountInGenesis( | ||
appGenesisState, banktypes.GenesisBalancesIterator{}, | ||
addr1, coins, cdc, | ||
) | ||
|
||
if tc.expPass { | ||
suite.Require().NoError(err) | ||
} else { | ||
suite.Require().Error(err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func (suite *GenTxTestSuite) TestDeliverGenTxs() { | ||
var ( | ||
genTxs []json.RawMessage | ||
txBuilder = suite.encodingConfig.TxConfig.NewTxBuilder() | ||
) | ||
|
||
testCases := []struct { | ||
msg string | ||
malleate func() | ||
deliverTxFn func(abci.RequestDeliverTx) abci.ResponseDeliverTx | ||
expPass bool | ||
}{ | ||
{ | ||
"no signature supplied", | ||
func() { | ||
err := txBuilder.SetMsgs(suite.msg1) | ||
suite.Require().NoError(err) | ||
|
||
genTxs = make([]json.RawMessage, 1) | ||
tx, err := suite.encodingConfig.TxConfig.TxJSONEncoder()(txBuilder.GetTx()) | ||
suite.Require().NoError(err) | ||
genTxs[0] = tx | ||
}, | ||
func(_ abci.RequestDeliverTx) abci.ResponseDeliverTx { | ||
|
||
return abci.ResponseDeliverTx{ | ||
Code: sdkerrors.ErrNoSignatures.ABCICode(), | ||
GasWanted: int64(10000000), | ||
GasUsed: int64(41913), | ||
Log: "no signatures supplied", | ||
} | ||
}, | ||
false, | ||
}, | ||
{ | ||
"success", | ||
func() { | ||
r := rand.New(rand.NewSource(time.Now().UnixNano())) | ||
msg := banktypes.NewMsgSend(addr1, addr2, sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 1)}) | ||
tx, err := simtestutil.GenSignedMockTx( | ||
r, | ||
suite.encodingConfig.TxConfig, | ||
[]sdk.Msg{msg}, | ||
sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 10)}, | ||
simtestutil.DefaultGenTxGas, | ||
suite.ctx.ChainID(), | ||
[]uint64{7}, | ||
[]uint64{0}, | ||
priv1, | ||
) | ||
suite.Require().NoError(err) | ||
|
||
genTxs = make([]json.RawMessage, 1) | ||
genTx, err := suite.encodingConfig.TxConfig.TxJSONEncoder()(tx) | ||
suite.Require().NoError(err) | ||
genTxs[0] = genTx | ||
}, | ||
func(tx abci.RequestDeliverTx) abci.ResponseDeliverTx { | ||
return abci.ResponseDeliverTx{ | ||
Code: sdkerrors.ErrUnauthorized.ABCICode(), | ||
GasWanted: int64(10000000), | ||
GasUsed: int64(41353), | ||
Log: "signature verification failed; please verify account number (4) and chain-id (): unauthorized", | ||
Codespace: "sdk", | ||
} | ||
}, | ||
true, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() { | ||
suite.SetupTest() | ||
|
||
tc.malleate() | ||
|
||
if tc.expPass { | ||
suite.Require().NotPanics(func() { | ||
genutil.DeliverGenTxs( | ||
suite.ctx, genTxs, suite.stakingKeeper, tc.deliverTxFn, | ||
suite.encodingConfig.TxConfig, | ||
) | ||
}) | ||
} else { | ||
_, err := genutil.DeliverGenTxs( | ||
suite.ctx, genTxs, suite.stakingKeeper, tc.deliverTxFn, | ||
suite.encodingConfig.TxConfig, | ||
) | ||
|
||
suite.Require().Error(err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestGenTxTestSuite(t *testing.T) { | ||
suite.Run(t, new(GenTxTestSuite)) | ||
} |
Oops, something went wrong.