github.com/cosmos/cosmos-sdk@v0.50.10/testutil/testdata/tx.go (about) 1 package testdata 2 3 import ( 4 "testing" 5 6 "gotest.tools/v3/assert" 7 "pgregory.net/rapid" 8 9 "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" 10 "github.com/cosmos/cosmos-sdk/crypto/keys/secp256r1" 11 cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" 12 sdk "github.com/cosmos/cosmos-sdk/types" 13 sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" 14 "github.com/cosmos/cosmos-sdk/types/query" 15 ) 16 17 // AddressGenerator creates and returns a random address generator using rapid. 18 func AddressGenerator(t *rapid.T) *rapid.Generator[sdk.AccAddress] { 19 return rapid.Custom(func(t *rapid.T) sdk.AccAddress { 20 pkBz := rapid.SliceOfN(rapid.Byte(), 20, 20).Draw(t, "hex") 21 return sdk.AccAddress(pkBz) 22 }) 23 } 24 25 // PaginationGenerator creates and returns a pagination PageRequest generator 26 // using rapid. 27 func PaginationGenerator(t *rapid.T, maxLimit uint64) *rapid.Generator[*query.PageRequest] { 28 return rapid.Custom(func(t *rapid.T) *query.PageRequest { 29 return &query.PageRequest{ 30 Offset: rapid.Uint64Range(0, maxLimit).Draw(t, "offset"), 31 Limit: rapid.Uint64Range(0, maxLimit).Draw(t, "limit"), 32 CountTotal: rapid.Bool().Draw(t, "count-total"), 33 Reverse: rapid.Bool().Draw(t, "reverse"), 34 } 35 }) 36 } 37 38 // KeyTestPubAddr generates a new secp256k1 keypair. 39 func KeyTestPubAddr() (cryptotypes.PrivKey, cryptotypes.PubKey, sdk.AccAddress) { 40 key := secp256k1.GenPrivKey() 41 pub := key.PubKey() 42 addr := sdk.AccAddress(pub.Address()) 43 return key, pub, addr 44 } 45 46 // KeyTestPubAddr generates a new secp256r1 keypair. 47 func KeyTestPubAddrSecp256R1(t *testing.T) (cryptotypes.PrivKey, cryptotypes.PubKey, sdk.AccAddress) { 48 key, err := secp256r1.GenPrivKey() 49 assert.NilError(t, err) 50 pub := key.PubKey() 51 addr := sdk.AccAddress(pub.Address()) 52 return key, pub, addr 53 } 54 55 // NewTestFeeAmount is a test fee amount. 56 func NewTestFeeAmount() sdk.Coins { 57 return sdk.NewCoins(sdk.NewInt64Coin("atom", 150)) 58 } 59 60 // NewTestGasLimit is a test fee gas limit. 61 func NewTestGasLimit() uint64 { 62 return 200000 63 } 64 65 // NewTestMsg creates a message for testing with the given signers. 66 func NewTestMsg(addrs ...sdk.AccAddress) *TestMsg { 67 var accAddresses []string 68 69 for _, addr := range addrs { 70 accAddresses = append(accAddresses, addr.String()) 71 } 72 73 return &TestMsg{ 74 Signers: accAddresses, 75 } 76 } 77 78 var _ sdk.Msg = (*TestMsg)(nil) 79 80 func (msg *TestMsg) GetSigners() []sdk.AccAddress { 81 signers := make([]sdk.AccAddress, 0, len(msg.Signers)) 82 for _, addr := range msg.Signers { 83 a, _ := sdk.AccAddressFromBech32(addr) 84 signers = append(signers, a) 85 } 86 return signers 87 } 88 89 func (msg *TestMsg) ValidateBasic() error { 90 for _, addr := range msg.Signers { 91 if _, err := sdk.AccAddressFromBech32(addr); err != nil { 92 return sdkerrors.ErrInvalidAddress.Wrapf("invalid signer address: %s", err) 93 } 94 } 95 return nil 96 } 97 98 var _ sdk.Msg = &MsgCreateDog{} 99 100 func (msg *MsgCreateDog) GetSigners() []sdk.AccAddress { return []sdk.AccAddress{} } 101 func (msg *MsgCreateDog) ValidateBasic() error { return nil }