121 lines
3.8 KiB
Go
121 lines
3.8 KiB
Go
package keeper_test
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"testing"
|
|
|
|
"github.com/cosmos/cosmos-sdk/baseapp"
|
|
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
|
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
|
|
"github.com/cosmos/cosmos-sdk/testutil"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
|
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
|
"github.com/cosmos/cosmos-sdk/x/auth/keeper"
|
|
"github.com/cosmos/cosmos-sdk/x/auth/types"
|
|
"github.com/stretchr/testify/suite"
|
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
|
"pgregory.net/rapid"
|
|
)
|
|
|
|
type DeterministicTestSuite struct {
|
|
suite.Suite
|
|
|
|
ctx sdk.Context
|
|
queryClient types.QueryClient
|
|
accountKeeper keeper.AccountKeeper
|
|
encCfg moduletestutil.TestEncodingConfig
|
|
}
|
|
|
|
func TestDeterministicTestSuite(t *testing.T) {
|
|
suite.Run(t, new(DeterministicTestSuite))
|
|
}
|
|
|
|
func (suite *DeterministicTestSuite) SetupTest() {
|
|
suite.encCfg = moduletestutil.MakeTestEncodingConfig(auth.AppModuleBasic{})
|
|
|
|
key := sdk.NewKVStoreKey(types.StoreKey)
|
|
testCtx := testutil.DefaultContextWithDB(suite.T(), key, sdk.NewTransientStoreKey("transient_test"))
|
|
suite.ctx = testCtx.Ctx.WithBlockHeader(tmproto.Header{})
|
|
|
|
maccPerms := map[string][]string{
|
|
"fee_collector": nil,
|
|
"mint": {"minter"},
|
|
"bonded_tokens_pool": {"burner", "staking"},
|
|
"not_bonded_tokens_pool": {"burner", "staking"},
|
|
multiPerm: {"burner", "minter", "staking"},
|
|
randomPerm: {"random"},
|
|
}
|
|
|
|
suite.accountKeeper = keeper.NewAccountKeeper(
|
|
suite.encCfg.Codec,
|
|
key,
|
|
types.ProtoBaseAccount,
|
|
maccPerms,
|
|
"cosmos",
|
|
types.NewModuleAddress("gov").String(),
|
|
)
|
|
|
|
queryHelper := baseapp.NewQueryServerTestHelper(suite.ctx, suite.encCfg.InterfaceRegistry)
|
|
types.RegisterQueryServer(queryHelper, suite.accountKeeper)
|
|
suite.queryClient = types.NewQueryClient(queryHelper)
|
|
}
|
|
|
|
func (suite *DeterministicTestSuite) runAccountIterations(addr sdk.AccAddress, prevRes types.AccountI) {
|
|
for i := 0; i < 1000; i++ {
|
|
acc, err := suite.queryClient.Account(suite.ctx, &types.QueryAccountRequest{Address: addr.String()})
|
|
suite.Require().NoError(err)
|
|
suite.Require().NotNil(acc)
|
|
|
|
var account types.AccountI
|
|
err = suite.encCfg.InterfaceRegistry.UnpackAny(acc.Account, &account)
|
|
suite.Require().NoError(err)
|
|
suite.Require().Equal(account.GetAddress(), addr)
|
|
|
|
if prevRes != nil {
|
|
any, err := codectypes.NewAnyWithValue(prevRes)
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().Equal(acc.Account, any)
|
|
suite.Require().Equal(account, prevRes)
|
|
}
|
|
|
|
prevRes = account
|
|
}
|
|
}
|
|
|
|
func (suite *DeterministicTestSuite) TestGRPCQueryAccount() {
|
|
rapid.Check(suite.T(), func(t *rapid.T) {
|
|
pub := pubkeyGenerator(t).Draw(t, "pubkey")
|
|
addr := sdk.AccAddress(pub.Address())
|
|
accNum := rapid.Uint64().Draw(t, "account-number")
|
|
seq := rapid.Uint64().Draw(t, "sequence")
|
|
|
|
acc1 := types.NewBaseAccount(addr, &pub, accNum, seq)
|
|
suite.accountKeeper.SetAccount(suite.ctx, acc1)
|
|
|
|
suite.runAccountIterations(addr, acc1)
|
|
})
|
|
|
|
// Regression test
|
|
addr1 := sdk.MustAccAddressFromBech32("cosmos139f7kncmglres2nf3h4hc4tade85ekfr8sulz5")
|
|
pub, err := hex.DecodeString("02950e1cdfcb133d6024109fd489f734eeb4502418e538c28481f22bce276f248c")
|
|
suite.Require().NoError(err)
|
|
|
|
accNum := uint64(10087)
|
|
seq := uint64(0)
|
|
|
|
acc1 := types.NewBaseAccount(addr1, &secp256k1.PubKey{Key: pub}, accNum, seq)
|
|
|
|
suite.accountKeeper.SetAccount(suite.ctx, acc1)
|
|
suite.runAccountIterations(addr1, acc1)
|
|
}
|
|
|
|
// pubkeyGenerator creates and returns a random pubkey generator using rapid.
|
|
func pubkeyGenerator(t *rapid.T) *rapid.Generator[secp256k1.PubKey] {
|
|
return rapid.Custom(func(t *rapid.T) secp256k1.PubKey {
|
|
pkBz := rapid.SliceOfN(rapid.Byte(), 33, 33).Draw(t, "hex")
|
|
return secp256k1.PubKey{Key: pkBz}
|
|
})
|
|
}
|