-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prefer sending tx_bytes to Simulate gRPC endpoint #8926
Merged
Merged
Changes from 24 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
7149841
First run
amaury1093 0a6b14a
Remove dead code
amaury1093 981f68d
Make test pass
amaury1093 2858900
Proto gen
amaury1093 a716ec6
Fix lint
amaury1093 8ee3a37
Merge branch 'master' into am/8425-fix-simulation
amaury1093 4165d78
Add changelog
amaury1093 76d16d4
Fix tests
amaury1093 806c3b5
Fix test
amaury1093 67a4d77
Merge branch 'master' of ssh://github.com/cosmos/cosmos-sdk into am/8…
amaury1093 7a9cd64
Update x/auth/tx/service.go
amaury1093 ca088c1
Remove protoTxProvider
amaury1093 5151a92
Add grpc-gateway test
amaury1093 693ea28
Add comment
amaury1093 573e5bd
move to api breaking
amaury1093 b8f7ccb
lesser diff
amaury1093 d58dd5f
Merge branch 'master' into am/8425-fix-simulation
amaury1093 781d389
Merge branch 'master' into am/8425-fix-simulation
amaury1093 aca386f
Merge branch 'master' into am/8425-fix-simulation
8e6c7aa
Merge master
amaury1093 a38d91c
Merge branch 'am/8425-fix-simulation' of ssh://github.com/cosmos/cosm…
amaury1093 c557f76
remove conflict
amaury1093 6412b30
Merge branch 'master' into am/8425-fix-simulation
519f643
Merge branch 'master' into am/8425-fix-simulation
mergify[bot] 40cebc3
Merge branch 'master' into am/8425-fix-simulation
amaury1093 6d9e49d
Merge branch 'master' into am/8425-fix-simulation
amaury1093 1dae35e
empty commit to rerun CI
amaury1093 5c2e4a4
Merge branch 'master' into am/8425-fix-simulation
amaury1093 375b439
empty commit to rerun CI
amaury1093 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,13 @@ package tx | |
|
||
import ( | ||
"bufio" | ||
"context" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"os" | ||
|
||
gogogrpc "github.com/gogo/protobuf/grpc" | ||
"github.com/spf13/pflag" | ||
|
||
"github.com/cosmos/cosmos-sdk/client" | ||
|
@@ -20,7 +22,6 @@ import ( | |
"github.com/cosmos/cosmos-sdk/types/tx" | ||
"github.com/cosmos/cosmos-sdk/types/tx/signing" | ||
authsigning "github.com/cosmos/cosmos-sdk/x/auth/signing" | ||
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx" | ||
) | ||
|
||
// GenerateOrBroadcastTxCLI will either generate and print and unsigned transaction | ||
|
@@ -50,7 +51,7 @@ func GenerateTx(clientCtx client.Context, txf Factory, msgs ...sdk.Msg) error { | |
return errors.New("cannot estimate gas in offline mode") | ||
} | ||
|
||
_, adjusted, err := CalculateGas(clientCtx.QueryWithData, txf, msgs...) | ||
_, adjusted, err := CalculateGas(clientCtx, txf, msgs...) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -76,13 +77,13 @@ func GenerateTx(clientCtx client.Context, txf Factory, msgs ...sdk.Msg) error { | |
// given set of messages. It will also simulate gas requirements if necessary. | ||
// It will return an error upon failure. | ||
func BroadcastTx(clientCtx client.Context, txf Factory, msgs ...sdk.Msg) error { | ||
txf, err := PrepareFactory(clientCtx, txf) | ||
txf, err := prepareFactory(clientCtx, txf) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if txf.SimulateAndExecute() || clientCtx.Simulate { | ||
_, adjusted, err := CalculateGas(clientCtx.QueryWithData, txf, msgs...) | ||
_, adjusted, err := CalculateGas(clientCtx, txf, msgs...) | ||
if err != nil { | ||
return err | ||
} | ||
|
@@ -142,8 +143,9 @@ func BroadcastTx(clientCtx client.Context, txf Factory, msgs ...sdk.Msg) error { | |
// BaseReq. Upon any error, the error will be written to the http.ResponseWriter. | ||
// Note that this function returns the legacy StdTx Amino JSON format for compatibility | ||
// with legacy clients. | ||
// Deprecated: We are removing Amino soon. | ||
func WriteGeneratedTxResponse( | ||
ctx client.Context, w http.ResponseWriter, br rest.BaseReq, msgs ...sdk.Msg, | ||
clientCtx client.Context, w http.ResponseWriter, br rest.BaseReq, msgs ...sdk.Msg, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. usually we don't need to repeat a typename in the variable name. |
||
) { | ||
gasAdj, ok := rest.ParseFloat64OrReturnBadRequest(w, br.GasAdjustment, flags.DefaultGasAdjustment) | ||
if !ok { | ||
|
@@ -163,7 +165,7 @@ func WriteGeneratedTxResponse( | |
WithMemo(br.Memo). | ||
WithChainID(br.ChainID). | ||
WithSimulateAndExecute(br.Simulate). | ||
WithTxConfig(ctx.TxConfig). | ||
WithTxConfig(clientCtx.TxConfig). | ||
WithTimeoutHeight(br.TimeoutHeight) | ||
|
||
if br.Simulate || gasSetting.Simulate { | ||
|
@@ -172,15 +174,15 @@ func WriteGeneratedTxResponse( | |
return | ||
} | ||
|
||
_, adjusted, err := CalculateGas(ctx.QueryWithData, txf, msgs...) | ||
_, adjusted, err := CalculateGas(clientCtx, txf, msgs...) | ||
if rest.CheckInternalServerError(w, err) { | ||
return | ||
} | ||
|
||
txf = txf.WithGas(adjusted) | ||
|
||
if br.Simulate { | ||
rest.WriteSimulationResponse(w, ctx.LegacyAmino, txf.Gas()) | ||
rest.WriteSimulationResponse(w, clientCtx.LegacyAmino, txf.Gas()) | ||
return | ||
} | ||
} | ||
|
@@ -190,12 +192,12 @@ func WriteGeneratedTxResponse( | |
return | ||
} | ||
|
||
stdTx, err := ConvertTxToStdTx(ctx.LegacyAmino, tx.GetTx()) | ||
stdTx, err := ConvertTxToStdTx(clientCtx.LegacyAmino, tx.GetTx()) | ||
if rest.CheckInternalServerError(w, err) { | ||
return | ||
} | ||
|
||
output, err := ctx.LegacyAmino.MarshalJSON(stdTx) | ||
output, err := clientCtx.LegacyAmino.MarshalJSON(stdTx) | ||
if rest.CheckInternalServerError(w, err) { | ||
return | ||
} | ||
|
@@ -268,46 +270,35 @@ func BuildSimTx(txf Factory, msgs ...sdk.Msg) ([]byte, error) { | |
return nil, err | ||
} | ||
|
||
protoProvider, ok := txb.(authtx.ProtoTxProvider) | ||
if !ok { | ||
return nil, fmt.Errorf("cannot simulate amino tx") | ||
} | ||
simReq := tx.SimulateRequest{Tx: protoProvider.GetProtoTx()} | ||
|
||
return simReq.Marshal() | ||
return txf.txConfig.TxEncoder()(txb.GetTx()) | ||
} | ||
|
||
// 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), txf Factory, msgs ...sdk.Msg, | ||
) (tx.SimulateResponse, uint64, error) { | ||
clientCtx gogogrpc.ClientConn, txf Factory, msgs ...sdk.Msg, | ||
) (*tx.SimulateResponse, uint64, error) { | ||
txBytes, err := BuildSimTx(txf, msgs...) | ||
if err != nil { | ||
return tx.SimulateResponse{}, 0, err | ||
return nil, 0, err | ||
} | ||
|
||
// TODO This should use the generated tx service Client. | ||
// https://github.com/cosmos/cosmos-sdk/issues/7726 | ||
bz, _, err := queryFunc("/cosmos.tx.v1beta1.Service/Simulate", txBytes) | ||
txSvcClient := tx.NewServiceClient(clientCtx) | ||
simRes, err := txSvcClient.Simulate(context.Background(), &tx.SimulateRequest{ | ||
TxBytes: txBytes, | ||
}) | ||
if err != nil { | ||
return tx.SimulateResponse{}, 0, err | ||
} | ||
|
||
var simRes tx.SimulateResponse | ||
|
||
if err := simRes.Unmarshal(bz); err != nil { | ||
return tx.SimulateResponse{}, 0, err | ||
return nil, 0, err | ||
} | ||
|
||
return simRes, uint64(txf.GasAdjustment() * float64(simRes.GasInfo.GasUsed)), nil | ||
} | ||
|
||
// PrepareFactory ensures the account defined by ctx.GetFromAddress() exists and | ||
// prepareFactory ensures the account defined by ctx.GetFromAddress() exists and | ||
// if the account number and/or the account sequence number are zero (not set), | ||
// they will be queried for and set on the provided Factory. A new Factory with | ||
// the updated fields will be returned. | ||
func PrepareFactory(clientCtx client.Context, txf Factory) (Factory, error) { | ||
func prepareFactory(clientCtx client.Context, txf Factory) (Factory, error) { | ||
from := clientCtx.GetFromAddress() | ||
|
||
if err := txf.accountRetriever.EnsureExists(clientCtx, from); err != nil { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typealias is not needed.