Skip to content
This repository has been archived by the owner on Dec 21, 2022. It is now read-only.

add estimate gas process to get tx's real gas wanted #165

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
49 changes: 39 additions & 10 deletions client/context/broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package context
import (
"fmt"
"strings"
"encoding/json"

"github.com/tendermint/tendermint/crypto/tmhash"
"github.com/tendermint/tendermint/mempool"
"github.com/tendermint/tendermint/rpc/core"

"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -16,16 +18,16 @@ import (
// based on the context parameters. The result of the broadcast is parsed into
// an intermediate structure which is logged if the context has a logger
// defined.
func (ctx CLIContext) BroadcastTx(txBytes []byte) (res sdk.TxResponse, err error) {
func (ctx CLIContext) BroadcastTx(txBytes []byte, estGasUse uint64) (res sdk.TxResponse, err error) {
switch ctx.BroadcastMode {
case flags.BroadcastSync:
res, err = ctx.BroadcastTxSync(txBytes)
res, err = ctx.BroadcastTxSync(txBytes, estGasUse)

case flags.BroadcastAsync:
res, err = ctx.BroadcastTxAsync(txBytes)
res, err = ctx.BroadcastTxAsync(txBytes, estGasUse)

case flags.BroadcastBlock:
res, err = ctx.BroadcastTxCommit(txBytes)
res, err = ctx.BroadcastTxCommit(txBytes, estGasUse)

default:
return sdk.TxResponse{}, fmt.Errorf("unsupported return type %s; supported types: sync, async, block", ctx.BroadcastMode)
Expand Down Expand Up @@ -81,13 +83,22 @@ func CheckTendermintError(err error, txBytes []byte) *sdk.TxResponse {
// NOTE: This should ideally not be used as the request may timeout but the tx
// may still be included in a block. Use BroadcastTxAsync or BroadcastTxSync
// instead.
func (ctx CLIContext) BroadcastTxCommit(txBytes []byte) (sdk.TxResponse, error) {
func (ctx CLIContext) BroadcastTxCommit(txBytes []byte, estGasUse uint64) (sdk.TxResponse, error) {
node, err := ctx.GetNode()
if err != nil {
return sdk.TxResponse{}, err
}

res, err := node.BroadcastTxCommit(txBytes)
wMsg := core.WrapEthMsg {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
WrapEthMsg not declared by package core (typecheck)

RawTxData: txBytes,
EstimatedGasUse: estGasUse,
}
wTxBytes, err := json.Marshal(wMsg)
if err != nil {
return sdk.TxResponse{}, err
}

res, err := node.BroadcastTxCommit(wTxBytes)
if err != nil {
if errRes := CheckTendermintError(err, txBytes); errRes != nil {
return *errRes, nil
Expand All @@ -109,13 +120,22 @@ func (ctx CLIContext) BroadcastTxCommit(txBytes []byte) (sdk.TxResponse, error)

// BroadcastTxSync broadcasts transaction bytes to a Tendermint node
// synchronously (i.e. returns after CheckTx execution).
func (ctx CLIContext) BroadcastTxSync(txBytes []byte) (sdk.TxResponse, error) {
func (ctx CLIContext) BroadcastTxSync(txBytes []byte, estGasUse uint64) (sdk.TxResponse, error) {
node, err := ctx.GetNode()
if err != nil {
return sdk.TxResponse{}, err
}

res, err := node.BroadcastTxSync(txBytes)
wMsg := core.WrapEthMsg {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
WrapEthMsg not declared by package core (typecheck)

RawTxData: txBytes,
EstimatedGasUse: estGasUse,
}
wTxBytes, err := json.Marshal(wMsg)
if err != nil {
return sdk.TxResponse{}, err
}

res, err := node.BroadcastTxSync(wTxBytes)
if errRes := CheckTendermintError(err, txBytes); errRes != nil {
return *errRes, nil
}
Expand All @@ -125,13 +145,22 @@ func (ctx CLIContext) BroadcastTxSync(txBytes []byte) (sdk.TxResponse, error) {

// BroadcastTxAsync broadcasts transaction bytes to a Tendermint node
// asynchronously (i.e. returns immediately).
func (ctx CLIContext) BroadcastTxAsync(txBytes []byte) (sdk.TxResponse, error) {
func (ctx CLIContext) BroadcastTxAsync(txBytes []byte, estGasUse uint64) (sdk.TxResponse, error) {
node, err := ctx.GetNode()
if err != nil {
return sdk.TxResponse{}, err
}

res, err := node.BroadcastTxAsync(txBytes)
wMsg := core.WrapEthMsg {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
WrapEthMsg not declared by package core (typecheck)

RawTxData: txBytes,
EstimatedGasUse: estGasUse,
}
wTxBytes, err := json.Marshal(wMsg)
if err != nil {
return sdk.TxResponse{}, err
}

res, err := node.BroadcastTxAsync(wTxBytes)
if errRes := CheckTendermintError(err, txBytes); errRes != nil {
return *errRes, nil
}
Expand Down
2 changes: 1 addition & 1 deletion client/context/broadcast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestBroadcastError(t *testing.T) {
for _, mode := range modes {
for err, code := range errors {
ctx := CreateContextWithErrorAndMode(err, mode)
resp, returnedErr := ctx.BroadcastTx(txBytes)
resp, returnedErr := ctx.BroadcastTx(txBytes, 1000)
require.NoError(t, returnedErr)
require.Equal(t, code, resp.Code)
require.Equal(t, txHash, resp.TxHash)
Expand Down
2 changes: 1 addition & 1 deletion x/auth/client/cli/broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ $ <appcli> tx broadcast ./mytxn.json
return
}

res, err := cliCtx.BroadcastTx(txBytes)
res, err := cliCtx.BroadcastTx(txBytes, stdTx.GetGas())
cliCtx.PrintOutput(res)

return err
Expand Down
2 changes: 1 addition & 1 deletion x/auth/client/rest/broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func BroadcastTxRequest(cliCtx context.CLIContext) http.HandlerFunc {

cliCtx = cliCtx.WithBroadcastMode(req.Mode)

res, err := cliCtx.BroadcastTx(txBytes)
res, err := cliCtx.BroadcastTx(txBytes, req.Tx.GetGas())
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
Expand Down
2 changes: 1 addition & 1 deletion x/auth/client/utils/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func CompleteAndBroadcastTxCLI(txBldr authtypes.TxBuilder, cliCtx context.CLICon
}

// broadcast to a Tendermint node
res, err := cliCtx.BroadcastTx(txBytes)
res, err := cliCtx.BroadcastTx(txBytes, txBldr.Gas())
if err != nil {
return err
}
Expand Down