Revert `SetSignerInfo` (#6894)

* Fix TestCLIMultisign tests

* Fix lint

Co-authored-by: SaReN <sahithnarahari@gmail.com>
This commit is contained in:
Amaury Martiny 2020-07-30 15:22:31 +02:00 committed by GitHub
parent fe6a341b4e
commit 8283165600
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 47 additions and 156 deletions

View File

@ -18,7 +18,6 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/rest" "github.com/cosmos/cosmos-sdk/types/rest"
txtypes "github.com/cosmos/cosmos-sdk/types/tx"
"github.com/cosmos/cosmos-sdk/types/tx/signing" "github.com/cosmos/cosmos-sdk/types/tx/signing"
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing" authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
) )
@ -309,41 +308,16 @@ func PrepareFactory(clientCtx client.Context, txf Factory) (Factory, error) {
return txf, nil return txf, nil
} }
// Helper function to retrieve sign bytes.
func getSignBytes(
signMode signing.SignMode, signerData authsigning.SignerData,
txBuilder client.TxBuilder, pubKey crypto.PubKey, txConfig client.TxConfig,
) ([]byte, error) {
// Set signer info, we only support single signature in this function.
err := txBuilder.SetSignerInfo(pubKey, &txtypes.ModeInfo{
Sum: &txtypes.ModeInfo_Single_{
Single: &txtypes.ModeInfo_Single{Mode: signMode},
},
})
if err != nil {
return nil, err
}
// generate the bytes to be signed
signBytes, err := txConfig.SignModeHandler().GetSignBytes(signMode, signerData, txBuilder.GetTx())
if err != nil {
return nil, err
}
return signBytes, nil
}
// SignWithPrivKey signs a given tx with the given private key, and returns the // SignWithPrivKey signs a given tx with the given private key, and returns the
// corresponding SignatureV2 if the signing is successful. // corresponding SignatureV2 if the signing is successful.
func SignWithPrivKey( func SignWithPrivKey(
signMode signing.SignMode, signerData authsigning.SignerData, signMode signing.SignMode, signerData authsigning.SignerData,
txBuilder client.TxBuilder, priv crypto.PrivKey, txConfig client.TxConfig, txBuilder client.TxBuilder, priv crypto.PrivKey, txConfig client.TxConfig,
) (signing.SignatureV2, error) { ) (signing.SignatureV2, error) {
var sigV2 signing.SignatureV2 var sigV2 signing.SignatureV2
// Generate the bytes to be signed // Generate the bytes to be signed.
signBytes, err := getSignBytes(signMode, signerData, txBuilder, priv.PubKey(), txConfig) signBytes, err := txConfig.SignModeHandler().GetSignBytes(signMode, signerData, txBuilder.GetTx())
if err != nil { if err != nil {
return sigV2, err return sigV2, err
} }
@ -394,8 +368,28 @@ func Sign(txf Factory, name string, txBuilder client.TxBuilder) error {
AccountSequence: txf.sequence, AccountSequence: txf.sequence,
} }
// Generate the bytes to be signed // For SIGN_MODE_DIRECT, calling SetSignatures calls SetSignerInfos on
signBytes, err := getSignBytes(signMode, signerData, txBuilder, pubKey, txf.txConfig) // TxBuilder under the hood, and SignerInfos is needed to generated the
// sign bytes. This is the reason for setting SetSignatures here, with a
// nil signature.
//
// Note: this line is not needed for SIGN_MODE_LEGACY_AMINO, but putting it
// also doesn't affect its generated sign bytes, so for code's simplicity
// sake, we put it here.
sigData := signing.SingleSignatureData{
SignMode: signMode,
Signature: nil,
}
sig := signing.SignatureV2{
PubKey: pubKey,
Data: &sigData,
}
if err := txBuilder.SetSignatures(sig); err != nil {
return err
}
// Generate the bytes to be signed.
signBytes, err := txf.txConfig.SignModeHandler().GetSignBytes(signMode, signerData, txBuilder.GetTx())
if err != nil { if err != nil {
return err return err
} }
@ -407,11 +401,11 @@ func Sign(txf Factory, name string, txBuilder client.TxBuilder) error {
} }
// Construct the SignatureV2 struct // Construct the SignatureV2 struct
sigData := signing.SingleSignatureData{ sigData = signing.SingleSignatureData{
SignMode: signMode, SignMode: signMode,
Signature: sigBytes, Signature: sigBytes,
} }
sig := signing.SignatureV2{ sig = signing.SignatureV2{
PubKey: pubKey, PubKey: pubKey,
Data: &sigData, Data: &sigData,
} }

View File

@ -1,10 +1,7 @@
package client package client
import ( import (
"github.com/tendermint/tendermint/crypto"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
txtypes "github.com/cosmos/cosmos-sdk/types/tx"
signingtypes "github.com/cosmos/cosmos-sdk/types/tx/signing" signingtypes "github.com/cosmos/cosmos-sdk/types/tx/signing"
"github.com/cosmos/cosmos-sdk/x/auth/signing" "github.com/cosmos/cosmos-sdk/x/auth/signing"
) )
@ -40,7 +37,6 @@ type (
GetTx() signing.SigFeeMemoTx GetTx() signing.SigFeeMemoTx
SetMsgs(msgs ...sdk.Msg) error SetMsgs(msgs ...sdk.Msg) error
SetSignerInfo(pubKey crypto.PubKey, modeInfo *txtypes.ModeInfo) error
SetSignatures(signatures ...signingtypes.SignatureV2) error SetSignatures(signatures ...signingtypes.SignatureV2) error
SetMemo(memo string) SetMemo(memo string)
SetFeeAmount(amount sdk.Coins) SetFeeAmount(amount sdk.Coins)

View File

@ -14,7 +14,6 @@ import (
"github.com/cosmos/cosmos-sdk/simapp" "github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/testutil/testdata" "github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
txtypes "github.com/cosmos/cosmos-sdk/types/tx"
"github.com/cosmos/cosmos-sdk/types/tx/signing" "github.com/cosmos/cosmos-sdk/types/tx/signing"
"github.com/cosmos/cosmos-sdk/x/auth/ante" "github.com/cosmos/cosmos-sdk/x/auth/ante"
xauthsigning "github.com/cosmos/cosmos-sdk/x/auth/signing" xauthsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
@ -87,21 +86,27 @@ func (suite *AnteTestSuite) CreateTestAccounts(numAccs int) []TestAccount {
// CreateTestTx is a helper function to create a tx given multiple inputs. // CreateTestTx is a helper function to create a tx given multiple inputs.
func (suite *AnteTestSuite) CreateTestTx(privs []crypto.PrivKey, accNums []uint64, accSeqs []uint64, chainID string) (xauthsigning.SigFeeMemoTx, error) { func (suite *AnteTestSuite) CreateTestTx(privs []crypto.PrivKey, accNums []uint64, accSeqs []uint64, chainID string) (xauthsigning.SigFeeMemoTx, error) {
// First round: we gather all the signer infos. // First round: we gather all the signer infos. We use the "set empty
// signature" hack to do that.
var sigsV2 []signing.SignatureV2
for _, priv := range privs { for _, priv := range privs {
err := suite.txBuilder.SetSignerInfo(priv.PubKey(), &txtypes.ModeInfo{ sigV2 := signing.SignatureV2{
Sum: &txtypes.ModeInfo_Single_{ PubKey: priv.PubKey(),
Single: &txtypes.ModeInfo_Single{ Data: &signing.SingleSignatureData{
Mode: suite.clientCtx.TxConfig.SignModeHandler().DefaultMode(), SignMode: suite.clientCtx.TxConfig.SignModeHandler().DefaultMode(),
Signature: nil,
}, },
}, }
})
sigsV2 = append(sigsV2, sigV2)
}
err := suite.txBuilder.SetSignatures(sigsV2...)
if err != nil { if err != nil {
return nil, err return nil, err
} }
}
// Second round: all signer infos are set, so each signer can sign. // Second round: all signer infos are set, so each signer can sign.
var sigsV2 []signing.SignatureV2 sigsV2 = []signing.SignatureV2{}
for i, priv := range privs { for i, priv := range privs {
signerData := xauthsigning.SignerData{ signerData := xauthsigning.SignerData{
ChainID: chainID, ChainID: chainID,
@ -115,7 +120,7 @@ func (suite *AnteTestSuite) CreateTestTx(privs []crypto.PrivKey, accNums []uint6
sigsV2 = append(sigsV2, sigV2) sigsV2 = append(sigsV2, sigV2)
} }
err := suite.txBuilder.SetSignatures(sigsV2...) err = suite.txBuilder.SetSignatures(sigsV2...)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -1,12 +1,9 @@
package tx package tx
import ( import (
"errors"
"fmt"
"testing" "testing"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/crypto"
"github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types" codectypes "github.com/cosmos/cosmos-sdk/codec/types"
@ -233,104 +230,3 @@ func TestBuilderValidateBasic(t *testing.T) {
err = txBuilder.ValidateBasic() err = txBuilder.ValidateBasic()
require.Error(t, err) require.Error(t, err)
} }
func TestBuilderSetSignerInfo(t *testing.T) {
// keys and addresses
_, pubKey1, addr1 := testdata.KeyTestPubAddr()
_, pubKey2, addr2 := testdata.KeyTestPubAddr()
// msg and signatures
msg1 := testdata.NewTestMsg(addr1)
msg2 := testdata.NewTestMsg(addr2)
// Variable data for each test
var (
pubKey crypto.PubKey
modeInfo txtypes.ModeInfo
txBuilder *builder
)
testCases := []struct {
desc string
malleate func()
expPass bool
expErr error
}{
{
"should fail if no msgs",
func() {
pubKey = pubKey1
},
false, sdkerrors.ErrInvalidPubKey,
},
{
"should fail if no public key",
func() {
pubKey = nil
txBuilder.SetMsgs(msg1)
},
false, sdkerrors.ErrInvalidPubKey,
},
{
"should fail if signer not in msgs",
func() {
_, pubKey, _ = testdata.KeyTestPubAddr()
txBuilder.SetMsgs(msg1, msg2)
},
false, sdkerrors.ErrInvalidPubKey,
},
{
"should pass with 2 signers",
func() {
// Run manually for signer 1.
txBuilder.SetMsgs(msg1, msg2)
modeInfo = txtypes.ModeInfo{Sum: &txtypes.ModeInfo_Single_{Single: &txtypes.ModeInfo_Single{Mode: signing.SignMode_SIGN_MODE_DIRECT}}}
txBuilder.SetSignerInfo(pubKey1, &modeInfo)
// Test case will handle signer 2.
pubKey = pubKey2
},
true, nil,
},
{
"should reset cached authInfoBz and pubKeys bytes after each call",
func() {
// Run manually for signer 1.
txBuilder.SetMsgs(msg1, msg2)
modeInfo = txtypes.ModeInfo{Sum: &txtypes.ModeInfo_Single_{Single: &txtypes.ModeInfo_Single{Mode: signing.SignMode_SIGN_MODE_DIRECT}}}
txBuilder.SetSignerInfo(pubKey1, &modeInfo)
// This populates the cached bytes.
_, _ = txBuilder.GetAuthInfoBytes(), txBuilder.GetPubKeys()
require.NotNil(t, txBuilder.authInfoBz)
require.NotNil(t, txBuilder.pubKeys)
// Run again, for signer 2. It should reset the cached bytes
// to nil.
txBuilder.SetSignerInfo(pubKey2, &modeInfo)
require.Nil(t, txBuilder.authInfoBz)
require.Nil(t, txBuilder.pubKeys)
// Run the test case, for fun.
pubKey = pubKey2
},
true, nil,
},
}
for _, tc := range testCases {
// Fresh txBuilder for each test case
txBuilder = newBuilder(std.DefaultPublicKeyCodec{})
tc.malleate()
fmt.Println(modeInfo)
err := txBuilder.SetSignerInfo(pubKey, &modeInfo)
if tc.expPass {
require.Nil(t, err, tc.desc)
} else {
require.Error(t, err, tc.desc)
require.True(t, errors.Is(tc.expErr, err), tc.desc)
}
}
}