authclient: update txBuilder to return sdk.SimulationResponse

This commit is contained in:
Federico Kunze 2020-03-19 22:40:07 -03:00
parent eeea5ebf89
commit 3d2746434f
No known key found for this signature in database
GPG Key ID: 655F93A970080A30
2 changed files with 47 additions and 34 deletions

View File

@ -129,26 +129,26 @@ func EnrichWithGas(txBldr authtypes.TxBuilder, cliCtx context.CLIContext, msgs [
} }
// CalculateGas simulates the execution of a transaction and returns // CalculateGas simulates the execution of a transaction and returns
// both the estimate obtained by the query and the adjusted amount. // the simulation response obtained by the query and the adjusted amount.
func CalculateGas( func CalculateGas(
queryFunc func(string, []byte) ([]byte, int64, error), cdc *codec.Codec, queryFunc func(string, []byte) ([]byte, int64, error), cdc *codec.Codec,
txBytes []byte, adjustment float64, txBytes []byte, adjustment float64,
) (estimate, adjusted uint64, err error) { ) (sdk.SimulationResponse, uint64, error) {
// run a simulation (via /app/simulate query) to // run a simulation (via /app/simulate query) to
// estimate gas and update TxBuilder accordingly // estimate gas and update TxBuilder accordingly
rawRes, _, err := queryFunc("/app/simulate", txBytes) rawRes, _, err := queryFunc("/app/simulate", txBytes)
if err != nil { if err != nil {
return estimate, adjusted, err return sdk.SimulationResponse{}, 0, err
} }
estimate, err = parseQueryResponse(cdc, rawRes) simRes, err := parseQueryResponse(cdc, rawRes)
if err != nil { if err != nil {
return return sdk.SimulationResponse{}, 0, err
} }
adjusted = adjustGasEstimate(estimate, adjustment) adjusted := adjustGasEstimate(simRes.GasUsed, adjustment)
return estimate, adjusted, nil return simRes, adjusted, nil
} }
// PrintUnsignedStdTx builds an unsigned StdTx and prints it to os.Stdout. // PrintUnsignedStdTx builds an unsigned StdTx and prints it to os.Stdout.
@ -272,28 +272,27 @@ func GetTxEncoder(cdc *codec.Codec) (encoder sdk.TxEncoder) {
} }
// nolint // nolint
// SimulateMsgs simulates the transaction and returns the gas estimate and the adjusted value. // SimulateMsgs simulates the transaction and returns the simulation response and the adjusted value.
func simulateMsgs(txBldr authtypes.TxBuilder, cliCtx context.CLIContext, msgs []sdk.Msg) (estimated, adjusted uint64, err error) { func simulateMsgs(txBldr authtypes.TxBuilder, cliCtx context.CLIContext, msgs []sdk.Msg) (sdk.SimulationResponse, uint64, error) {
txBytes, err := txBldr.BuildTxForSim(msgs) txBytes, err := txBldr.BuildTxForSim(msgs)
if err != nil { if err != nil {
return return sdk.SimulationResponse{}, 0, err
} }
estimated, adjusted, err = CalculateGas(cliCtx.QueryWithData, cliCtx.Codec, txBytes, txBldr.GasAdjustment()) return CalculateGas(cliCtx.QueryWithData, cliCtx.Codec, txBytes, txBldr.GasAdjustment())
return
} }
func adjustGasEstimate(estimate uint64, adjustment float64) uint64 { func adjustGasEstimate(estimate uint64, adjustment float64) uint64 {
return uint64(adjustment * float64(estimate)) return uint64(adjustment * float64(estimate))
} }
func parseQueryResponse(cdc *codec.Codec, rawRes []byte) (uint64, error) { func parseQueryResponse(cdc *codec.Codec, rawRes []byte) (sdk.SimulationResponse, error) {
var gasUsed uint64 var simRes sdk.SimulationResponse
if err := cdc.UnmarshalBinaryBare(rawRes, &gasUsed); err != nil { if err := cdc.UnmarshalBinaryBare(rawRes, &simRes); err != nil {
return 0, err return sdk.SimulationResponse{}, err
} }
return gasUsed, nil return simRes, nil
} }
// PrepareTxBuilder populates a TxBuilder in preparation for the build of a Tx. // PrepareTxBuilder populates a TxBuilder in preparation for the build of a Tx.

View File

@ -7,8 +7,8 @@ import (
"os" "os"
"testing" "testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/crypto/ed25519" "github.com/tendermint/tendermint/crypto/ed25519"
"github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/codec"
@ -23,13 +23,18 @@ var (
func TestParseQueryResponse(t *testing.T) { func TestParseQueryResponse(t *testing.T) {
cdc := makeCodec() cdc := makeCodec()
sdkResBytes := cdc.MustMarshalBinaryBare(uint64(10)) simRes := sdk.SimulationResponse{
gas, err := parseQueryResponse(cdc, sdkResBytes) GasInfo: sdk.GasInfo{GasUsed: 10, GasWanted: 20},
assert.Equal(t, gas, uint64(10)) Result: nil,
assert.Nil(t, err) }
gas, err = parseQueryResponse(cdc, []byte("fuzzy"))
assert.Equal(t, gas, uint64(0)) bz := cdc.MustMarshalBinaryBare(simRes)
assert.Error(t, err) res, err := parseQueryResponse(cdc, bz)
require.NoError(t, err)
require.Equal(t, 10, int(res.GasInfo.GasUsed))
res, err = parseQueryResponse(cdc, []byte("fuzzy"))
require.Error(t, err)
} }
func TestCalculateGas(t *testing.T) { func TestCalculateGas(t *testing.T) {
@ -37,9 +42,14 @@ func TestCalculateGas(t *testing.T) {
makeQueryFunc := func(gasUsed uint64, wantErr bool) func(string, []byte) ([]byte, int64, error) { makeQueryFunc := func(gasUsed uint64, wantErr bool) func(string, []byte) ([]byte, int64, error) {
return func(string, []byte) ([]byte, int64, error) { return func(string, []byte) ([]byte, int64, error) {
if wantErr { if wantErr {
return nil, 0, errors.New("") return nil, 0, errors.New("query failed")
} }
return cdc.MustMarshalBinaryBare(gasUsed), 0, nil simRes := sdk.SimulationResponse{
GasInfo: sdk.GasInfo{GasUsed: gasUsed, GasWanted: gasUsed},
Result: nil,
}
return cdc.MustMarshalBinaryBare(simRes), 0, nil
} }
} }
@ -54,20 +64,24 @@ func TestCalculateGas(t *testing.T) {
args args args args
wantEstimate uint64 wantEstimate uint64
wantAdjusted uint64 wantAdjusted uint64
wantErr bool expPass bool
}{ }{
{"error", args{0, true, 1.2}, 0, 0, true}, {"error", args{0, true, 1.2}, 0, 0, false},
{"adjusted gas", args{10, false, 1.2}, 10, 12, false}, {"adjusted gas", args{10, false, 1.2}, 10, 12, true},
} }
for _, tt := range tests { for _, tt := range tests {
tt := tt tt := tt
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
queryFunc := makeQueryFunc(tt.args.queryFuncGasUsed, tt.args.queryFuncWantErr) queryFunc := makeQueryFunc(tt.args.queryFuncGasUsed, tt.args.queryFuncWantErr)
gotEstimate, gotAdjusted, err := CalculateGas(queryFunc, cdc, []byte(""), tt.args.adjustment) simRes, gotAdjusted, err := CalculateGas(queryFunc, cdc, []byte(""), tt.args.adjustment)
assert.Equal(t, err != nil, tt.wantErr) if tt.expPass {
assert.Equal(t, gotEstimate, tt.wantEstimate) require.NoError(t, err)
assert.Equal(t, gotAdjusted, tt.wantAdjusted) } else {
require.Error(t, err)
require.Equal(t, simRes.GasInfo.GasUsed, tt.wantEstimate)
require.Equal(t, gotAdjusted, tt.wantAdjusted)
}
}) })
} }
} }