Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/auth/ante: Migrate tests to use the new client.TxConfig #6661

Merged
merged 61 commits into from
Jul 20, 2020
Merged
Show file tree
Hide file tree
Changes from 53 commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
038fcb0
WIP: using encoding config
amaury1093 Jul 8, 2020
ae855fc
Merge branch 'master' into am-6213-xauthante
amaury1093 Jul 9, 2020
3b05f06
Merge branch 'master' into am-6213-xauthante
amaury1093 Jul 9, 2020
5c7da7b
Make it compile, test fails
amaury1093 Jul 9, 2020
aa485fa
test should be okay
amaury1093 Jul 9, 2020
ba338a4
Make tests pass
amaury1093 Jul 9, 2020
30c4b6b
Add comments
amaury1093 Jul 9, 2020
c78c31d
Convert more tests
amaury1093 Jul 10, 2020
9c99fbe
Merge branch 'master' into am-6213-xauthante
amaury1093 Jul 10, 2020
f208c45
Make TestAnteHandlerSigErrors work
amaury1093 Jul 10, 2020
edc3be6
Make first 2 tests pass
amaury1093 Jul 10, 2020
f6d1265
TestAnteHandlerAccountNumbers
amaury1093 Jul 10, 2020
1f79c24
Merge branch 'master' into am-6213-xauthante
amaury1093 Jul 10, 2020
5dec3f8
Use table tests
amaury1093 Jul 10, 2020
7516b46
Remove print
amaury1093 Jul 10, 2020
0c53051
Merge branch 'master' into am-6213-xauthante
amaury1093 Jul 10, 2020
0e7e1e3
Use test table
amaury1093 Jul 13, 2020
0f8bbff
Merge branch 'master' into am-6213-xauthante
amaury1093 Jul 13, 2020
d6ebbfa
TestAnteHandlerSigErrors
amaury1093 Jul 13, 2020
8151c4b
TestAnteHandlerAccountNumbers
amaury1093 Jul 13, 2020
6ced722
TestAnteHandlerAccountNumbers
amaury1093 Jul 13, 2020
d3c942a
Refactor TestAccount
amaury1093 Jul 13, 2020
a68a7c7
Refactor getSignBytes
amaury1093 Jul 13, 2020
39e382e
TestAnteHandlerAccountNumbersAtBlockHeightZero
amaury1093 Jul 13, 2020
03fc35d
TestAnteHandlerSequences
amaury1093 Jul 13, 2020
af9e5a3
TestAnteHandlerFees
amaury1093 Jul 13, 2020
0196749
TestAnteHandlerMultiSigner
amaury1093 Jul 13, 2020
5a27cc2
TestAnteHandlerBadSignBytes
amaury1093 Jul 13, 2020
5ff8cdf
TestAnteHandlerSetPubKey
amaury1093 Jul 13, 2020
327a4f7
TestAnteHandlerSigLimitExceeded
amaury1093 Jul 13, 2020
94b6297
TestCustomSignatureVerificationGasConsumer
amaury1093 Jul 13, 2020
ca42457
TestAnteHandlerReCheck
amaury1093 Jul 13, 2020
b00c3a1
Make all tests pass
amaury1093 Jul 13, 2020
7df4794
Merge branch 'master' into am-6213-xauthante
amaury1093 Jul 13, 2020
9dd9846
Refactor a little bit more
amaury1093 Jul 14, 2020
a47d1f3
Fee test
amaury1093 Jul 14, 2020
349742e
SetupTest
amaury1093 Jul 14, 2020
1fd5029
All tests pass
amaury1093 Jul 14, 2020
b958b00
Refactor to RunTestCase
amaury1093 Jul 14, 2020
880c803
Merge branch 'master' into am-6213-xauthante
amaury1093 Jul 14, 2020
39669ae
Merge branch 'master' into am-6213-xauthante
amaury1093 Jul 15, 2020
75f47ed
Don't use StdFee
amaury1093 Jul 15, 2020
4d08eca
Merge branch 'master' into am-6213-xauthante
amaury1093 Jul 15, 2020
f4f79dd
Revert some little stuff
amaury1093 Jul 15, 2020
2f2f333
Finish up last couple of test cases
amaury1093 Jul 15, 2020
d4b4665
Merge branch 'master' into am-6213-xauthante
alexanderbez Jul 15, 2020
1b85d33
Merge branch 'master' into am-6213-xauthante
amaury1093 Jul 16, 2020
ca228a3
Merge branch 'master' into am-6213-xauthante
amaury1093 Jul 16, 2020
8349420
Less verbose
amaury1093 Jul 16, 2020
db52a3e
s/TxGenerator/TxConfig
amaury1093 Jul 16, 2020
7839b6c
Add comments
amaury1093 Jul 16, 2020
02cd879
Indent
amaury1093 Jul 16, 2020
1bea3ff
Move KeyTestPubAddr to testdata
amaury1093 Jul 16, 2020
887cab8
Move testdata to /testutil
amaury1093 Jul 16, 2020
9094a39
Merge branch 'master' into am-6213-xauthante
amaury1093 Jul 17, 2020
b7ffcba
Merge branch 'master' into am-6213-xauthante
amaury1093 Jul 17, 2020
a8c574e
Revert to use signature: nil step in signing
amaury1093 Jul 17, 2020
091ea35
Add comments
amaury1093 Jul 17, 2020
fffb443
Merge branch 'master' into am-6213-xauthante
amaury1093 Jul 17, 2020
d90bf35
Merge branch 'master' into am-6213-xauthante
amaury1093 Jul 20, 2020
87e7bca
Merge branch 'master' into am-6213-xauthante
amaury1093 Jul 20, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions client/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type Context struct {
GenerateOnly bool
Offline bool
SkipConfirm bool
TxGenerator TxGenerator
TxConfig TxConfig
AccountRetriever AccountRetriever
NodeURI string
Verifier tmlite.Verifier
Expand Down Expand Up @@ -319,9 +319,9 @@ func (ctx Context) WithSkipConfirmation(skip bool) Context {
return ctx
}

// WithTxGenerator returns the context with an updated TxGenerator
func (ctx Context) WithTxGenerator(generator TxGenerator) Context {
ctx.TxGenerator = generator
// WithTxConfig returns the context with an updated TxConfig
func (ctx Context) WithTxConfig(generator TxConfig) Context {
ctx.TxConfig = generator
return ctx
}

Expand Down
63 changes: 31 additions & 32 deletions client/testutil/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/cosmos/cosmos-sdk/codec/testdata"
"github.com/cosmos/cosmos-sdk/crypto/types/multisig"
"github.com/cosmos/cosmos-sdk/x/auth/signing"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/stretchr/testify/suite"
"github.com/tendermint/tendermint/crypto"

Expand All @@ -17,27 +16,27 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

// TxGeneratorTestSuite provides a test suite that can be used to test that a TxGenerator implementation is correct
//nolint:golint // type name will be used as tx.TxGeneratorTestSuite by other packages, and that stutters; consider calling this GeneratorTestSuite
type TxGeneratorTestSuite struct {
// TxConfigTestSuite provides a test suite that can be used to test that a TxConfig implementation is correct
//nolint:golint // type name will be used as tx.TxConfigTestSuite by other packages, and that stutters; consider calling this GeneratorTestSuite
type TxConfigTestSuite struct {
suite.Suite
TxGenerator client.TxGenerator
TxConfig client.TxConfig
}

// NewTxGeneratorTestSuite returns a new TxGeneratorTestSuite with the provided TxGenerator implementation
func NewTxGeneratorTestSuite(txGenerator client.TxGenerator) *TxGeneratorTestSuite {
return &TxGeneratorTestSuite{TxGenerator: txGenerator}
// NewTxConfigTestSuite returns a new TxConfigTestSuite with the provided TxConfig implementation
func NewTxConfigTestSuite(txConfig client.TxConfig) *TxConfigTestSuite {
return &TxConfigTestSuite{TxConfig: txConfig}
}

func (s *TxGeneratorTestSuite) TestTxBuilderGetTx() {
txBuilder := s.TxGenerator.NewTxBuilder()
func (s *TxConfigTestSuite) TestTxBuilderGetTx() {
txBuilder := s.TxConfig.NewTxBuilder()
tx := txBuilder.GetTx()
s.Require().NotNil(tx)
s.Require().Equal(len(tx.GetMsgs()), 0)
}

func (s *TxGeneratorTestSuite) TestTxBuilderSetFeeAmount() {
txBuilder := s.TxGenerator.NewTxBuilder()
func (s *TxConfigTestSuite) TestTxBuilderSetFeeAmount() {
txBuilder := s.TxConfig.NewTxBuilder()
feeAmount := sdk.Coins{
sdk.NewInt64Coin("atom", 20000000),
}
Expand All @@ -46,30 +45,30 @@ func (s *TxGeneratorTestSuite) TestTxBuilderSetFeeAmount() {
s.Require().Equal(feeAmount, feeTx.GetFee())
}

func (s *TxGeneratorTestSuite) TestTxBuilderSetGasLimit() {
func (s *TxConfigTestSuite) TestTxBuilderSetGasLimit() {
const newGas uint64 = 300000
txBuilder := s.TxGenerator.NewTxBuilder()
txBuilder := s.TxConfig.NewTxBuilder()
txBuilder.SetGasLimit(newGas)
feeTx := txBuilder.GetTx()
s.Require().Equal(newGas, feeTx.GetGas())
}

func (s *TxGeneratorTestSuite) TestTxBuilderSetMemo() {
func (s *TxConfigTestSuite) TestTxBuilderSetMemo() {
const newMemo string = "newfoomemo"
txBuilder := s.TxGenerator.NewTxBuilder()
txBuilder := s.TxConfig.NewTxBuilder()
txBuilder.SetMemo(newMemo)
txWithMemo := txBuilder.GetTx()
s.Require().Equal(txWithMemo.GetMemo(), newMemo)
}

func (s *TxGeneratorTestSuite) TestTxBuilderSetMsgs() {
_, _, addr1 := authtypes.KeyTestPubAddr()
_, _, addr2 := authtypes.KeyTestPubAddr()
func (s *TxConfigTestSuite) TestTxBuilderSetMsgs() {
_, _, addr1 := testdata.KeyTestPubAddr()
_, _, addr2 := testdata.KeyTestPubAddr()
msg1 := testdata.NewTestMsg(addr1)
msg2 := testdata.NewTestMsg(addr2)
msgs := []sdk.Msg{msg1, msg2}

txBuilder := s.TxGenerator.NewTxBuilder()
txBuilder := s.TxConfig.NewTxBuilder()

err := txBuilder.SetMsgs(msgs...)
s.Require().NoError(err)
Expand All @@ -80,12 +79,12 @@ func (s *TxGeneratorTestSuite) TestTxBuilderSetMsgs() {
s.Require().Error(tx.ValidateBasic()) // should fail because of no signatures
}

func (s *TxGeneratorTestSuite) TestTxBuilderSetSignatures() {
privKey, pubkey, addr := authtypes.KeyTestPubAddr()
privKey2, pubkey2, _ := authtypes.KeyTestPubAddr()
func (s *TxConfigTestSuite) TestTxBuilderSetSignatures() {
privKey, pubkey, addr := testdata.KeyTestPubAddr()
privKey2, pubkey2, _ := testdata.KeyTestPubAddr()
multisigPk := multisig.NewPubKeyMultisigThreshold(2, []crypto.PubKey{pubkey, pubkey2})

txBuilder := s.TxGenerator.NewTxBuilder()
txBuilder := s.TxConfig.NewTxBuilder()

// set test msg
msg := testdata.NewTestMsg(addr)
Expand All @@ -97,7 +96,7 @@ func (s *TxGeneratorTestSuite) TestTxBuilderSetSignatures() {
// check that validation fails
s.Require().Error(txBuilder.GetTx().ValidateBasic())

signModeHandler := s.TxGenerator.SignModeHandler()
signModeHandler := s.TxConfig.SignModeHandler()
s.Require().Contains(signModeHandler.Modes(), signModeHandler.DefaultMode())

// set SignatureV2 without actual signature bytes
Expand Down Expand Up @@ -227,8 +226,8 @@ func sigDataEquals(data1, data2 signingtypes.SignatureData) bool {
}
}

func (s *TxGeneratorTestSuite) TestTxEncodeDecode() {
_, pubkey, addr := authtypes.KeyTestPubAddr()
func (s *TxConfigTestSuite) TestTxEncodeDecode() {
_, pubkey, addr := testdata.KeyTestPubAddr()
feeAmount := sdk.Coins{sdk.NewInt64Coin("atom", 150)}
gasLimit := uint64(50000)
memo := "foomemo"
Expand All @@ -242,7 +241,7 @@ func (s *TxGeneratorTestSuite) TestTxEncodeDecode() {
},
}

txBuilder := s.TxGenerator.NewTxBuilder()
txBuilder := s.TxConfig.NewTxBuilder()
txBuilder.SetFeeAmount(feeAmount)
txBuilder.SetGasLimit(gasLimit)
txBuilder.SetMemo(memo)
Expand All @@ -253,12 +252,12 @@ func (s *TxGeneratorTestSuite) TestTxEncodeDecode() {
tx := txBuilder.GetTx()

s.T().Log("encode transaction")
txBytes, err := s.TxGenerator.TxEncoder()(tx)
txBytes, err := s.TxConfig.TxEncoder()(tx)
s.Require().NoError(err)
s.Require().NotNil(txBytes)

s.T().Log("decode transaction")
tx2, err := s.TxGenerator.TxDecoder()(txBytes)
tx2, err := s.TxConfig.TxDecoder()(txBytes)
s.Require().NoError(err)
tx3, ok := tx2.(signing.SigFeeMemoTx)
s.Require().True(ok)
Expand All @@ -270,12 +269,12 @@ func (s *TxGeneratorTestSuite) TestTxEncodeDecode() {
s.Require().Equal([]crypto.PubKey{pubkey}, tx3.GetPubKeys())

s.T().Log("JSON encode transaction")
jsonTxBytes, err := s.TxGenerator.TxJSONEncoder()(tx)
jsonTxBytes, err := s.TxConfig.TxJSONEncoder()(tx)
s.Require().NoError(err)
s.Require().NotNil(jsonTxBytes)

s.T().Log("JSON decode transaction")
tx2, err = s.TxGenerator.TxJSONDecoder()(jsonTxBytes)
tx2, err = s.TxConfig.TxJSONDecoder()(jsonTxBytes)
s.Require().NoError(err)
tx3, ok = tx2.(signing.SigFeeMemoTx)
s.Require().True(ok)
Expand Down
10 changes: 5 additions & 5 deletions client/tx/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
// signing an application-specific transaction.
type Factory struct {
keybase keyring.Keyring
txGenerator client.TxGenerator
txConfig client.TxConfig
accountRetriever client.AccountRetriever
accountNumber uint64
sequence uint64
Expand Down Expand Up @@ -56,7 +56,7 @@ func NewFactoryCLI(clientCtx client.Context, flagSet *pflag.FlagSet) Factory {
gasSetting, _ := flags.ParseGasSetting(gasStr)

f := Factory{
txGenerator: clientCtx.TxGenerator,
txConfig: clientCtx.TxConfig,
accountRetriever: clientCtx.AccountRetriever,
keybase: clientCtx.Keyring,
chainID: clientCtx.ChainID,
Expand Down Expand Up @@ -134,9 +134,9 @@ func (f Factory) AccountRetriever() client.AccountRetriever { return f.accountRe
// using the gas from the simulation results
func (f Factory) SimulateAndExecute() bool { return f.simulateAndExecute }

// WithTxGenerator returns a copy of the Factory with an updated TxGenerator.
func (f Factory) WithTxGenerator(g client.TxGenerator) Factory {
f.txGenerator = g
// WithTxConfig returns a copy of the Factory with an updated TxConfig.
func (f Factory) WithTxConfig(g client.TxConfig) Factory {
f.txConfig = g
return f
}

Expand Down
96 changes: 67 additions & 29 deletions client/tx/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/gogo/protobuf/jsonpb"
"github.com/spf13/pflag"
"github.com/tendermint/tendermint/crypto"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
Expand All @@ -33,7 +34,7 @@ func GenerateOrBroadcastTxCLI(clientCtx client.Context, flagSet *pflag.FlagSet,
//
// TODO: Remove in favor of GenerateOrBroadcastTxCLI
func GenerateOrBroadcastTx(clientCtx client.Context, msgs ...sdk.Msg) error {
txf := NewFactoryFromDeprecated(clientCtx.Input).WithTxGenerator(clientCtx.TxGenerator).WithAccountRetriever(clientCtx.AccountRetriever)
txf := NewFactoryFromDeprecated(clientCtx.Input).WithTxConfig(clientCtx.TxConfig).WithAccountRetriever(clientCtx.AccountRetriever)
return GenerateOrBroadcastTxWithFactory(clientCtx, txf, msgs...)
}

Expand Down Expand Up @@ -124,7 +125,7 @@ func BroadcastTx(clientCtx client.Context, txf Factory, msgs ...sdk.Msg) error {
return err
}

txBytes, err := clientCtx.TxGenerator.TxEncoder()(tx.GetTx())
txBytes, err := clientCtx.TxConfig.TxEncoder()(tx.GetTx())
if err != nil {
return err
}
Expand Down Expand Up @@ -162,7 +163,7 @@ func WriteGeneratedTxResponse(
WithMemo(br.Memo).
WithChainID(br.ChainID).
WithSimulateAndExecute(br.Simulate).
WithTxGenerator(ctx.TxGenerator)
WithTxConfig(ctx.TxConfig)

if br.Simulate || gasSetting.Simulate {
if gasAdj < 0 {
Expand Down Expand Up @@ -225,7 +226,7 @@ func BuildUnsignedTx(txf Factory, msgs ...sdk.Msg) (client.TxBuilder, error) {
}
}

tx := txf.txGenerator.NewTxBuilder()
tx := txf.txConfig.NewTxBuilder()

if err := tx.SetMsgs(msgs...); err != nil {
return nil, err
Expand Down Expand Up @@ -254,7 +255,7 @@ func BuildSimTx(txf Factory, msgs ...sdk.Msg) ([]byte, error) {
return nil, err
}

return txf.txGenerator.TxEncoder()(tx.GetTx())
return txf.txConfig.TxEncoder()(tx.GetTx())
}

// CalculateGas simulates the execution of a transaction and returns the
Expand Down Expand Up @@ -310,6 +311,46 @@ func PrepareFactory(clientCtx client.Context, txf Factory) (Factory, error) {
return txf, nil
}

amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
// SignWithPrivKey signs a given tx with the given private key, and returns the
// corresponding SignatureV2 if the signing is successful.
func SignWithPrivKey(
signMode signing.SignMode,
signerData authsigning.SignerData,
txBuilder client.TxBuilder,
priv crypto.PrivKey,
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
txConfig client.TxConfig,
) (signing.SignatureV2, error) {
var sigV2 signing.SignatureV2

// Generate the bytes to be signed
signBytes, err := txConfig.SignModeHandler().GetSignBytes(
signMode,
signerData,
txBuilder.GetTx(),
)
if err != nil {
return sigV2, err
}

// Sign those bytes
signature, err := priv.Sign(signBytes)
if err != nil {
return sigV2, err
}

// Construct the SignatureV2 struct
sigData := signing.SingleSignatureData{
SignMode: signMode,
Signature: signature,
}
sigV2 = signing.SignatureV2{
PubKey: priv.PubKey(),
Data: &sigData,
}

return sigV2, nil
}

// Sign signs a given tx with the provided name and passphrase. If the Factory's
// Keybase is not set, a new one will be created based on the client's backend.
// The bytes signed over are canconical. The resulting signature will be set on
Expand All @@ -318,15 +359,15 @@ func PrepareFactory(clientCtx client.Context, txf Factory) (Factory, error) {
//
// Note, It is assumed the Factory has the necessary fields set that are required
// by the CanonicalSignBytes call.
func Sign(txf Factory, name string, tx client.TxBuilder) error {
func Sign(txf Factory, name string, txBuilder client.TxBuilder) error {
if txf.keybase == nil {
return errors.New("keybase must be set prior to signing a transaction")
}

signMode := txf.signMode
if signMode == signing.SignMode_SIGN_MODE_UNSPECIFIED {
// use the SignModeHandler's default mode if unspecified
signMode = txf.txGenerator.SignModeHandler().DefaultMode()
signMode = txf.txConfig.SignModeHandler().DefaultMode()
}

key, err := txf.keybase.Key(name)
Expand All @@ -335,43 +376,40 @@ func Sign(txf Factory, name string, tx client.TxBuilder) error {
}

pubKey := key.GetPubKey()
sigData := &signing.SingleSignatureData{
SignMode: signMode,
Signature: nil,
}
sig := signing.SignatureV2{
PubKey: pubKey,
Data: sigData,
}
err = tx.SetSignatures(sig)
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}

signBytes, err := txf.txGenerator.SignModeHandler().GetSignBytes(
// Generate the bytes to be signed
signerData := authsigning.SignerData{
ChainID: txf.chainID,
AccountNumber: txf.accountNumber,
AccountSequence: txf.sequence,
}
signBytes, err := txf.txConfig.SignModeHandler().GetSignBytes(
signMode,
authsigning.SignerData{
ChainID: txf.chainID,
AccountNumber: txf.accountNumber,
AccountSequence: txf.sequence,
}, tx.GetTx(),
signerData,
txBuilder.GetTx(),
)
if err != nil {
return err
}

// Sign those bytes
sigBytes, _, err := txf.keybase.Sign(name, signBytes)
if err != nil {
return err
}

sigData.Signature = sigBytes
sig = signing.SignatureV2{
// Construct the SignatureV2 struct
sigData := signing.SingleSignatureData{
SignMode: signMode,
Signature: sigBytes,
}
sig := signing.SignatureV2{
PubKey: pubKey,
Data: sigData,
Data: &sigData,
}

return tx.SetSignatures(sig)
// And here the tx is populated with the signature
return txBuilder.SetSignatures(sig)
}

// GasEstimateResponse defines a response definition for tx gas estimation.
Expand Down
Loading