Skip to content
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

feat: expose gas information #309

Merged
merged 1 commit into from
Aug 15, 2022
Merged
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
34 changes: 19 additions & 15 deletions types/simulation/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,39 +57,43 @@ type Operation func(r *rand.Rand, app *baseapp.BaseApp,

// OperationMsg - structure for operation output
type OperationMsg struct {
Route string `json:"route" yaml:"route"` // msg route (i.e module name)
Name string `json:"name" yaml:"name"` // operation name (msg Type or "no-operation")
Comment string `json:"comment" yaml:"comment"` // additional comment
OK bool `json:"ok" yaml:"ok"` // success
Msg json.RawMessage `json:"msg" yaml:"msg"` // JSON encoded msg
Route string `json:"route" yaml:"route"` // msg route (i.e module name)
Name string `json:"name" yaml:"name"` // operation name (msg Type or "no-operation")
Comment string `json:"comment" yaml:"comment"` // additional comment
OK bool `json:"ok" yaml:"ok"` // success
GasWanted uint64 `json:"gas_wanted" yaml:"gas_wanted"` // gas wanted value
GasUsed uint64 `json:"gas_used" yaml:"gas_used"` // gas used value
Msg json.RawMessage `json:"msg" yaml:"msg"` // JSON encoded msg
}

// NewOperationMsgBasic creates a new operation message from raw input.
func NewOperationMsgBasic(route, name, comment string, ok bool, msg []byte) OperationMsg {
func NewOperationMsgBasic(route, name, comment string, ok bool, gasWanted, gasUsed uint64, msg []byte) OperationMsg {
return OperationMsg{
Route: route,
Name: name,
Comment: comment,
OK: ok,
Msg: msg,
Route: route,
Name: name,
Comment: comment,
OK: ok,
GasWanted: gasWanted,
GasUsed: gasUsed,
Msg: msg,
}
}

// NewOperationMsg - create a new operation message from sdk.Msg
func NewOperationMsg(msg sdk.Msg, ok bool, comment string, cdc *codec.ProtoCodec) OperationMsg {
func NewOperationMsg(msg sdk.Msg, ok bool, comment string, gasWanted, gasUsed uint64, cdc *codec.ProtoCodec) OperationMsg {
if legacyMsg, okType := msg.(legacytx.LegacyMsg); okType {
return NewOperationMsgBasic(legacyMsg.Route(), legacyMsg.Type(), comment, ok, legacyMsg.GetSignBytes())
return NewOperationMsgBasic(legacyMsg.Route(), legacyMsg.Type(), comment, ok, gasWanted, gasUsed, legacyMsg.GetSignBytes())
}

bz := cdc.MustMarshalJSON(msg)

return NewOperationMsgBasic(sdk.MsgTypeURL(msg), sdk.MsgTypeURL(msg), comment, ok, bz)
return NewOperationMsgBasic(sdk.MsgTypeURL(msg), sdk.MsgTypeURL(msg), comment, ok, gasWanted, gasUsed, bz)

}

// NoOpMsg - create a no-operation message
func NoOpMsg(route, msgType, comment string) OperationMsg {
return NewOperationMsgBasic(route, msgType, comment, false, nil)
return NewOperationMsgBasic(route, msgType, comment, false, 0, 0, nil)
}

// log entry text for this operation msg
Expand Down
12 changes: 6 additions & 6 deletions x/authz/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,11 @@ func SimulateMsgGrant(ak authz.AccountKeeper, bk authz.BankKeeper, _ keeper.Keep
return simtypes.NoOpMsg(authz.ModuleName, TypeMsgGrant, "unable to generate mock tx"), nil, err
}

_, _, err = app.Deliver(txCfg.TxEncoder(), tx)
gasInfo, _, err := app.Deliver(txCfg.TxEncoder(), tx)
if err != nil {
return simtypes.NoOpMsg(authz.ModuleName, sdk.MsgTypeURL(msg), "unable to deliver tx"), nil, err
}
return simtypes.NewOperationMsg(msg, true, "", nil), nil, err
return simtypes.NewOperationMsg(msg, true, "", gasInfo.GasWanted, gasInfo.GasUsed, nil), nil, err
}
}

Expand Down Expand Up @@ -196,12 +196,12 @@ func SimulateMsgRevoke(ak authz.AccountKeeper, bk authz.BankKeeper, k keeper.Kee
return simtypes.NoOpMsg(authz.ModuleName, TypeMsgRevoke, err.Error()), nil, err
}

_, _, err = app.Deliver(txCfg.TxEncoder(), tx)
gasInfo, _, err := app.Deliver(txCfg.TxEncoder(), tx)
if err != nil {
return simtypes.NoOpMsg(authz.ModuleName, TypeMsgRevoke, "unable to deliver tx"), nil, err
}

return simtypes.NewOperationMsg(&msg, true, "", nil), nil, nil
return simtypes.NewOperationMsg(&msg, true, "", gasInfo.GasWanted, gasInfo.GasUsed, nil), nil, nil
}
}

Expand Down Expand Up @@ -286,7 +286,7 @@ func SimulateMsgExec(ak authz.AccountKeeper, bk authz.BankKeeper, k keeper.Keepe
return simtypes.NoOpMsg(authz.ModuleName, TypeMsgExec, err.Error()), nil, err
}

_, _, err = app.Deliver(txCfg.TxEncoder(), tx)
gasInfo, _, err := app.Deliver(txCfg.TxEncoder(), tx)
if err != nil {
return simtypes.NoOpMsg(authz.ModuleName, TypeMsgExec, err.Error()), nil, err
}
Expand All @@ -295,6 +295,6 @@ func SimulateMsgExec(ak authz.AccountKeeper, bk authz.BankKeeper, k keeper.Keepe
if err != nil {
return simtypes.NoOpMsg(authz.ModuleName, TypeMsgExec, "unmarshal error"), nil, err
}
return simtypes.NewOperationMsg(&msgExec, true, "success", nil), nil, nil
return simtypes.NewOperationMsg(&msgExec, true, "success", gasInfo.GasWanted, gasInfo.GasUsed, nil), nil, nil
}
}
22 changes: 11 additions & 11 deletions x/bank/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ func SimulateMsgSend(ak types.AccountKeeper, bk keeper.Keeper) simtypes.Operatio

msg := types.NewMsgSend(from.Address, to.Address, coins)

err := sendMsgSend(r, app, bk, ak, msg, ctx, chainID, []cryptotypes.PrivKey{from.PrivKey})
gasInfo, err := sendMsgSend(r, app, bk, ak, msg, ctx, chainID, []cryptotypes.PrivKey{from.PrivKey})
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "invalid transfers"), nil, err
}

return simtypes.NewOperationMsg(msg, true, "", nil), nil, nil
return simtypes.NewOperationMsg(msg, true, "", gasInfo.GasWanted, gasInfo.GasUsed, nil), nil, nil
}
}

Expand All @@ -91,20 +91,20 @@ func SimulateMsgSendToModuleAccount(ak types.AccountKeeper, bk keeper.Keeper, mo

msg := types.NewMsgSend(from.Address, to.Address, coins)

err := sendMsgSend(r, app, bk, ak, msg, ctx, chainID, []cryptotypes.PrivKey{from.PrivKey})
gasInfo, err := sendMsgSend(r, app, bk, ak, msg, ctx, chainID, []cryptotypes.PrivKey{from.PrivKey})
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "invalid transfers"), nil, err
}

return simtypes.NewOperationMsg(msg, true, "", nil), nil, nil
return simtypes.NewOperationMsg(msg, true, "", gasInfo.GasWanted, gasInfo.GasUsed, nil), nil, nil
}
}

// sendMsgSend sends a transaction with a MsgSend from a provided random account.
func sendMsgSend(
r *rand.Rand, app *baseapp.BaseApp, bk keeper.Keeper, ak types.AccountKeeper,
msg *types.MsgSend, ctx sdk.Context, chainID string, privkeys []cryptotypes.PrivKey,
) error {
) (sdk.GasInfo, error) {

var (
fees sdk.Coins
Expand All @@ -113,7 +113,7 @@ func sendMsgSend(

from, err := sdk.AccAddressFromBech32(msg.FromAddress)
if err != nil {
return err
return sdk.GasInfo{}, err
}

account := ak.GetAccount(ctx, from)
Expand All @@ -124,7 +124,7 @@ func sendMsgSend(
feeCoins := coins.FilterDenoms([]string{sdk.DefaultBondDenom})
fees, err = simtypes.RandomFees(r, ctx, feeCoins)
if err != nil {
return err
return sdk.GasInfo{}, err
}
}
txGen := simappparams.MakeTestEncodingConfig().TxConfig
Expand All @@ -139,15 +139,15 @@ func sendMsgSend(
privkeys...,
)
if err != nil {
return err
return sdk.GasInfo{}, err
}

_, _, err = app.Deliver(txGen.TxEncoder(), tx)
gasInfo, _, err := app.Deliver(txGen.TxEncoder(), tx)
if err != nil {
return err
return sdk.GasInfo{}, err
}

return nil
return gasInfo, nil
}

// randomSendFields returns the sender and recipient simulation accounts as well
Expand Down
4 changes: 2 additions & 2 deletions x/gov/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,12 @@ func SimulateMsgSubmitProposal(
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate mock tx"), nil, err
}

_, _, err = app.Deliver(txGen.TxEncoder(), tx)
gasInfo, _, err := app.Deliver(txGen.TxEncoder(), tx)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to deliver tx"), nil, err
}

opMsg := simtypes.NewOperationMsg(msg, true, "", nil)
opMsg := simtypes.NewOperationMsg(msg, true, "", gasInfo.GasWanted, gasInfo.GasUsed, nil)

// get the submitted proposal ID
proposalID, err := k.GetProposalID(ctx)
Expand Down
4 changes: 2 additions & 2 deletions x/simulation/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ func GenAndDeliverTx(txCtx OperationInput, fees sdk.Coins) (simtypes.OperationMs
return simtypes.NoOpMsg(txCtx.ModuleName, txCtx.MsgType, "unable to generate mock tx"), nil, err
}

_, _, err = txCtx.App.Deliver(txCtx.TxGen.TxEncoder(), tx)
gasInfo, _, err := txCtx.App.Deliver(txCtx.TxGen.TxEncoder(), tx)
if err != nil {
return simtypes.NoOpMsg(txCtx.ModuleName, txCtx.MsgType, "unable to deliver tx"), nil, err
}

return simtypes.NewOperationMsg(txCtx.Msg, true, "", txCtx.Cdc), nil, nil
return simtypes.NewOperationMsg(txCtx.Msg, true, "", gasInfo.GasWanted, gasInfo.GasUsed, txCtx.Cdc), nil, nil

}
12 changes: 6 additions & 6 deletions x/slashing/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func SimulateMsgUnjail(ak types.AccountKeeper, bk types.BankKeeper, k keeper.Kee
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to generate mock tx"), nil, err
}

_, res, err := app.Deliver(txGen.TxEncoder(), tx)
gasInfo, res, err := app.Deliver(txGen.TxEncoder(), tx)

// result should fail if:
// - validator cannot be unjailed due to tombstone
Expand All @@ -115,23 +115,23 @@ func SimulateMsgUnjail(ak types.AccountKeeper, bk types.BankKeeper, k keeper.Kee
validator.TokensFromShares(selfDel.GetShares()).TruncateInt().LT(validator.GetMinSelfDelegation()) {
if res != nil && err == nil {
if info.Tombstoned {
return simtypes.NewOperationMsg(msg, true, "", nil), nil, errors.New("validator should not have been unjailed if validator tombstoned")
return simtypes.NewOperationMsg(msg, true, "", gasInfo.GasWanted, gasInfo.GasUsed, nil), nil, errors.New("validator should not have been unjailed if validator tombstoned")
}
if ctx.BlockHeader().Time.Before(info.JailedUntil) {
return simtypes.NewOperationMsg(msg, true, "", nil), nil, errors.New("validator unjailed while validator still in jail period")
return simtypes.NewOperationMsg(msg, true, "", gasInfo.GasWanted, gasInfo.GasUsed, nil), nil, errors.New("validator unjailed while validator still in jail period")
}
if validator.TokensFromShares(selfDel.GetShares()).TruncateInt().LT(validator.GetMinSelfDelegation()) {
return simtypes.NewOperationMsg(msg, true, "", nil), nil, errors.New("validator unjailed even though self-delegation too low")
return simtypes.NewOperationMsg(msg, true, "", gasInfo.GasWanted, gasInfo.GasUsed, nil), nil, errors.New("validator unjailed even though self-delegation too low")
}
}
// msg failed as expected
return simtypes.NewOperationMsg(msg, false, "", nil), nil, nil
return simtypes.NewOperationMsg(msg, false, "", gasInfo.GasWanted, gasInfo.GasUsed, nil), nil, nil
}

if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "unable to deliver tx"), nil, errors.New(res.Log)
}

return simtypes.NewOperationMsg(msg, true, "", nil), nil, nil
return simtypes.NewOperationMsg(msg, true, "", gasInfo.GasWanted, gasInfo.GasUsed, nil), nil, nil
}
}