Remove TxBuilder and usages (#6941)

This commit is contained in:
SaReN
2020-08-05 18:28:33 +00:00
committed by GitHub
parent 95cf3c95a6
commit 4835cdc178
5 changed files with 0 additions and 623 deletions
-65
View File
@@ -1,65 +0,0 @@
package client
import (
"log"
"net/http"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/cosmos/cosmos-sdk/x/auth/types"
)
// WriteGenerateStdTxResponse writes response for the generate only mode.
func WriteGenerateStdTxResponse(w http.ResponseWriter, clientCtx client.Context, br rest.BaseReq, msgs []sdk.Msg) {
gasAdj, ok := rest.ParseFloat64OrReturnBadRequest(w, br.GasAdjustment, flags.DefaultGasAdjustment)
if !ok {
return
}
gasSetting, err := flags.ParseGasSetting(br.Gas)
if rest.CheckBadRequestError(w, err) {
return
}
txBldr := types.NewTxBuilder(
GetTxEncoder(clientCtx.Codec), br.AccountNumber, br.Sequence, gasSetting.Gas, gasAdj,
br.Simulate, br.ChainID, br.Memo, br.Fees, br.GasPrices,
)
if br.Simulate || gasSetting.Simulate {
if gasAdj < 0 {
rest.WriteErrorResponse(w, http.StatusBadRequest, errors.ErrorInvalidGasAdjustment.Error())
return
}
txBldr, err = EnrichWithGas(txBldr, clientCtx, msgs)
if rest.CheckInternalServerError(w, err) {
return
}
if br.Simulate {
rest.WriteSimulationResponse(w, clientCtx.Codec, txBldr.Gas())
return
}
}
stdMsg, err := txBldr.BuildSignMsg(msgs)
if rest.CheckBadRequestError(w, err) {
return
}
// NOTE: amino is used intentionally here, don't migrate it
output, err := clientCtx.Codec.MarshalJSON(types.NewStdTx(stdMsg.Msgs, stdMsg.Fee, nil, stdMsg.Memo))
if rest.CheckInternalServerError(w, err) {
return
}
w.Header().Set("Content-Type", "application/json")
if _, err := w.Write(output); err != nil {
log.Printf("could not write response: %v", err)
}
}
-49
View File
@@ -37,40 +37,6 @@ func (gr GasEstimateResponse) String() string {
return fmt.Sprintf("gas estimate: %d", gr.GasEstimate)
}
// EnrichWithGas calculates the gas estimate that would be consumed by the
// transaction and set the transaction's respective value accordingly.
func EnrichWithGas(txBldr authtypes.TxBuilder, clientCtx client.Context, msgs []sdk.Msg) (authtypes.TxBuilder, error) {
_, adjusted, err := simulateMsgs(txBldr, clientCtx, msgs)
if err != nil {
return txBldr, err
}
return txBldr.WithGas(adjusted), nil
}
// CalculateGas simulates the execution of a transaction and returns
// the simulation response obtained by the query and the adjusted gas amount.
func CalculateGas(
queryFunc func(string, []byte) ([]byte, int64, error), cdc *codec.Codec,
txBytes []byte, adjustment float64,
) (sdk.SimulationResponse, uint64, error) {
// run a simulation (via /app/simulate query) to
// estimate gas and update TxBuilder accordingly
rawRes, _, err := queryFunc("/app/simulate", txBytes)
if err != nil {
return sdk.SimulationResponse{}, 0, err
}
simRes, err := ParseQueryResponse(rawRes)
if err != nil {
return sdk.SimulationResponse{}, 0, err
}
adjusted := adjustGasEstimate(simRes.GasUsed, adjustment)
return simRes, adjusted, nil
}
// PrintUnsignedStdTx builds an unsigned StdTx and prints it to os.Stdout.
func PrintUnsignedStdTx(txBldr tx.Factory, clientCtx client.Context, msgs []sdk.Msg) error {
err := tx.GenerateTx(clientCtx, txBldr, msgs...)
@@ -196,21 +162,6 @@ func GetTxEncoder(cdc *codec.Codec) (encoder sdk.TxEncoder) {
return encoder
}
// simulateMsgs simulates the transaction and returns the simulation response and
// the adjusted gas value.
func simulateMsgs(txBldr authtypes.TxBuilder, clientCtx client.Context, msgs []sdk.Msg) (sdk.SimulationResponse, uint64, error) {
txBytes, err := txBldr.BuildTxForSim(msgs)
if err != nil {
return sdk.SimulationResponse{}, 0, err
}
return CalculateGas(clientCtx.QueryWithData, clientCtx.Codec, txBytes, txBldr.GasAdjustment())
}
func adjustGasEstimate(estimate uint64, adjustment float64) uint64 {
return uint64(adjustment * float64(estimate))
}
func ParseQueryResponse(bz []byte) (sdk.SimulationResponse, error) {
var simRes sdk.SimulationResponse
if err := jsonpb.Unmarshal(strings.NewReader(string(bz)), &simRes); err != nil {
-53
View File
@@ -1,7 +1,6 @@
package client_test
import (
"errors"
"fmt"
"strings"
"testing"
@@ -45,58 +44,6 @@ func TestParseQueryResponse(t *testing.T) {
require.Error(t, err)
}
func TestCalculateGas(t *testing.T) {
cdc := makeCodec()
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, _ := codec.ProtoMarshalJSON(simRes)
return bz, 0, nil
}
}
type args struct {
queryFuncGasUsed uint64
queryFuncWantErr bool
adjustment float64
}
tests := []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 _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
queryFunc := makeQueryFunc(tt.args.queryFuncGasUsed, tt.args.queryFuncWantErr)
simRes, gotAdjusted, err := authclient.CalculateGas(queryFunc, cdc, []byte(""), tt.args.adjustment)
if tt.expPass {
require.NoError(t, err)
require.Equal(t, simRes.GasInfo.GasUsed, tt.wantEstimate)
require.Equal(t, gotAdjusted, tt.wantAdjusted)
require.NotNil(t, simRes.Result)
} else {
require.Error(t, err)
require.Nil(t, simRes.Result)
}
})
}
}
// TODO: remove this and authclient.GetTxEncoder after the proto tx migration is complete
func TestDefaultTxEncoder(t *testing.T) {
cdc := makeCodec()