This commit is contained in:
Aleksandr Bezobchuk 2020-03-24 19:07:27 -04:00
parent 33c090ee17
commit 70d0241373
No known key found for this signature in database
GPG Key ID: 7DAC30FBD99879B0
2 changed files with 107 additions and 1 deletions

View File

@ -191,7 +191,7 @@ func BuildUnsignedTx(txf Factory, msgs ...sdk.Msg) (ClientTx, error) {
tx.SetFee(auth.NewStdFee(txf.gas, fees))
tx.SetMsgs(msgs...)
tx.SetMemo(txf.memo)
tx.SetSignatures(nil)
tx.SetSignatures()
return tx, nil
}

106
client/tx/tx_test.go Normal file
View File

@ -0,0 +1,106 @@
package tx_test
import (
"encoding/json"
"errors"
"testing"
"github.com/stretchr/testify/require"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/codec/std"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank"
)
func TestCalculateGas(t *testing.T) {
makeQueryFunc := func(gasUsed uint64, wantErr bool) func(string, []byte) ([]byte, int64, error) {
return func(string, []byte) ([]byte, int64, error) {
if wantErr {
return nil, 0, errors.New("query failed")
}
simRes := sdk.SimulationResponse{
GasInfo: sdk.GasInfo{GasUsed: gasUsed, GasWanted: gasUsed},
Result: &sdk.Result{Data: []byte("tx data"), Log: "log"},
}
bz, err := json.Marshal(simRes)
if err != nil {
return nil, 0, err
}
return bz, 0, nil
}
}
type args struct {
queryFuncGasUsed uint64
queryFuncWantErr bool
adjustment float64
}
testCases := []struct {
name string
args args
wantEstimate uint64
wantAdjusted uint64
expPass bool
}{
{"error", args{0, true, 1.2}, 0, 0, false},
{"adjusted gas", args{10, false, 1.2}, 10, 12, true},
}
for _, tc := range testCases {
stc := tc
t.Run(stc.name, func(t *testing.T) {
queryFunc := makeQueryFunc(stc.args.queryFuncGasUsed, stc.args.queryFuncWantErr)
simRes, gotAdjusted, err := tx.CalculateGas(queryFunc, []byte(""), stc.args.adjustment)
if stc.expPass {
require.NoError(t, err)
require.Equal(t, simRes.GasInfo.GasUsed, stc.wantEstimate)
require.Equal(t, gotAdjusted, stc.wantAdjusted)
require.NotNil(t, simRes.Result)
} else {
require.Error(t, err)
require.Nil(t, simRes.Result)
}
})
}
}
func TestBuildSimTx(t *testing.T) {
txf := tx.Factory{}.
WithTxGenerator(std.TxGenerator{}).
WithAccountNumber(50).
WithSequence(23).
WithFees("50stake").
WithMemo("memo").
WithChainID("test-chain")
msg := bank.NewMsgSend(sdk.AccAddress("from"), sdk.AccAddress("to"), nil)
bz, err := tx.BuildSimTx(txf, msg)
require.NoError(t, err)
require.NotNil(t, bz)
tx := &std.Transaction{}
require.NoError(t, tx.Unmarshal(bz))
require.Equal(t, []sdk.Signature{sdk.Signature(auth.StdSignature{})}, tx.GetSignatures())
}
func TestBuildUnsignedTx(t *testing.T) {
txf := tx.Factory{}.
WithTxGenerator(std.TxGenerator{}).
WithAccountNumber(50).
WithSequence(23).
WithFees("50stake").
WithMemo("memo").
WithChainID("test-chain")
msg := bank.NewMsgSend(sdk.AccAddress("from"), sdk.AccAddress("to"), nil)
tx, err := tx.BuildUnsignedTx(txf, msg)
require.NoError(t, err)
require.NotNil(t, tx)
require.Equal(t, []sdk.Signature{}, tx.GetSignatures())
}