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

[Bugfix] emit missing wasm tax events #566

Merged
merged 2 commits into from
Sep 27, 2021
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
2 changes: 1 addition & 1 deletion client/docs/statik/statik.go

Large diffs are not rendered by default.

19 changes: 15 additions & 4 deletions client/docs/swagger-ui/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1098,10 +1098,10 @@ paths:
deprecated: true
tags:
- Transactions
summary: Encode a transaction to the Amino wire format
summary: Encode a legacy transaction to the Proto wire format
description: >-
Encode a transaction (signed or not) from JSON to base64-encoded Amino
serialized bytes
Encode a legacy transaction (signed or not) from JSON to base64-encoded
Proto serialized bytes
consumes:
- application/json
produces:
Expand Down Expand Up @@ -1161,6 +1161,17 @@ paths:
sequence:
type: string
example: '0'
sequences:
required: false
type: array
items:
type: string
example: '1'
fee_granter:
type: string
description: bech32 encoded address
example: terra1wg2mlrxdmnnkkykgqg4znky86nyrtc45q336yv
required: false
responses:
'200':
description: The tx was successfully decoded and re-encoded
Expand All @@ -1169,7 +1180,7 @@ paths:
properties:
tx:
type: string
example: The base64-encoded Amino-serialized bytes for the tx
example: The base64-encoded Proto-serialized bytes for the tx
'400':
description: The tx was malformed
'500':
Expand Down
15 changes: 12 additions & 3 deletions client/docs/swagger_legacy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,8 @@ paths:
deprecated: true
tags:
- Transactions
summary: Encode a transaction to the Amino wire format
description: Encode a transaction (signed or not) from JSON to base64-encoded Amino serialized bytes
summary: Encode a legacy transaction to the Proto wire format
description: Encode a legacy transaction (signed or not) from JSON to base64-encoded Proto serialized bytes
consumes:
- application/json
produces:
Expand All @@ -356,6 +356,15 @@ paths:
properties:
tx:
$ref: "#/definitions/StdTx"
sequences:
required: false
type: array
items:
type: string
example: "1"
fee_granter:
required: false
$ref: "#/definitions/Address"
responses:
200:
description: The tx was successfully decoded and re-encoded
Expand All @@ -364,7 +373,7 @@ paths:
properties:
tx:
type: string
example: The base64-encoded Amino-serialized bytes for the tx
example: The base64-encoded Proto-serialized bytes for the tx
400:
description: The tx was malformed
500:
Expand Down
2 changes: 1 addition & 1 deletion custom/auth/client/rest/broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func BroadcastTxRequest(clientCtx client.Context) http.HandlerFunc {
return
}

// compute signature bytes
// compute tx bytes
txBytes, err := clientCtx.TxConfig.TxEncoder()(txBuilder.GetTx())
if rest.CheckInternalServerError(w, err) {
return
Expand Down
112 changes: 112 additions & 0 deletions custom/auth/client/rest/encode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package rest

import (
"errors"
"io/ioutil"
"net/http"

"github.com/cosmos/cosmos-sdk/client"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/cosmos/cosmos-sdk/x/auth/legacy/legacytx"
)

// EncodeReq defines a tx encode request.
type EncodeReq struct {
Tx legacytx.StdTx `json:"tx" yaml:"tx"`
Sequences []uint64 `json:"sequences" yaml:"sequences"`
FeeGranter string `json:"fee_granter" yaml:"fee_granter"`
}

// EncodeResq defines a tx encode response.
type EncodeResq struct {
Tx []byte `json:"tx" yaml:"tx"`
}

var _ codectypes.UnpackInterfacesMessage = EncodeReq{}

// UnpackInterfaces implements the UnpackInterfacesMessage interface.
func (m EncodeReq) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
return m.Tx.UnpackInterfaces(unpacker)
}

// EncodeTxRequest implements a tx encode handler that is responsible
// for encoding a legacy tx into new format tx bytes.
func EncodeTxRequest(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req EncodeReq

body, err := ioutil.ReadAll(r.Body)
if rest.CheckBadRequestError(w, err) {
return
}

// NOTE: amino is used intentionally here, don't migrate it!
err = clientCtx.LegacyAmino.UnmarshalJSON(body, &req)
if err != nil {
if rest.CheckBadRequestError(w, err) {
return
}
}

txBuilder := clientCtx.TxConfig.NewTxBuilder()
txBuilder.SetFeeAmount(req.Tx.GetFee())
txBuilder.SetGasLimit(req.Tx.GetGas())
txBuilder.SetMemo(req.Tx.GetMemo())
if err := txBuilder.SetMsgs(req.Tx.GetMsgs()...); rest.CheckBadRequestError(w, err) {
return
}

txBuilder.SetTimeoutHeight(req.Tx.GetTimeoutHeight())
if req.FeeGranter != "" {
addr, err := sdk.AccAddressFromBech32(req.FeeGranter)
if rest.CheckBadRequestError(w, err) {
return
}

txBuilder.SetFeeGranter(addr)
}

signatures, err := req.Tx.GetSignaturesV2()
if rest.CheckBadRequestError(w, err) {
return
}

// if sequence is not given, try fetch from the chain
if len(req.Sequences) == 0 {
for _, sig := range signatures {
_, seq, err := clientCtx.AccountRetriever.GetAccountNumberSequence(clientCtx, sdk.AccAddress(sig.PubKey.Address().Bytes()))
if rest.CheckBadRequestError(w, err) {
return
}
req.Sequences = append(req.Sequences, seq)
}
}

// check the sequence nubmer is equal with the signature nubmer
if len(signatures) != len(req.Sequences) {
rest.CheckBadRequestError(w, errors.New("Must provide each signers's sequence number"))
return
}

// fill sequence number to new signature
for i, seq := range req.Sequences {
signatures[i].Sequence = seq
}

if err := txBuilder.SetSignatures(signatures...); rest.CheckBadRequestError(w, err) {
return
}

// compute tx bytes
txBytes, err := clientCtx.TxConfig.TxEncoder()(txBuilder.GetTx())
if rest.CheckInternalServerError(w, err) {
return
}

rest.PostProcessResponseBare(w, clientCtx, EncodeResq{
Tx: txBytes,
})
}
}
1 change: 1 addition & 0 deletions custom/auth/client/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ import (
func RegisterTxRoutes(clientCtx client.Context, rtr *mux.Router) {
r := clientrest.WithHTTPDeprecationHeaders(rtr)
r.HandleFunc("/txs/estimate_fee", EstimateTxFeeRequestHandlerFn(clientCtx)).Methods("POST")
r.HandleFunc("/txs/encode", EncodeTxRequest(clientCtx)).Methods("POST")
r.HandleFunc("/txs", BroadcastTxRequest(clientCtx)).Methods("POST")
}
5 changes: 4 additions & 1 deletion x/wasm/keeper/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,13 @@ func (k Keeper) dispatchMessage(ctx sdk.Context, contractAddr sdk.AccAddress, ms
// Charge tax on result msg
taxes := ante.FilterMsgAndComputeTax(ctx, k.treasuryKeeper, sdkMsg)
if !taxes.IsZero() {
eventManager := sdk.NewEventManager()
contractAcc := k.accountKeeper.GetAccount(ctx, contractAddr)
if err := cosmosante.DeductFees(k.bankKeeper, ctx, contractAcc, taxes); err != nil {
if err := cosmosante.DeductFees(k.bankKeeper, ctx.WithEventManager(eventManager), contractAcc, taxes); err != nil {
return nil, nil, err
}

events = eventManager.Events()
}

res, err := k.handleSdkMessage(ctx, contractAddr, sdkMsg)
Expand Down