Skip to content

Commit

Permalink
fix: Add Events to TxResponse (backport cosmos#10630) (cosmos#10643)
Browse files Browse the repository at this point in the history
* fix: Add Events to TxResponse (cosmos#10630)

(cherry picked from commit c4bedf8)

# Conflicts:
#	CHANGELOG.md
#	go.sum
#	types/abci.pb.go
#	types/result.go

* FOR REAL.

* Merging the merge

* This is not a commit

* You wouldn't get this from any other guy

* We'll figure it out on Monday

Co-authored-by: Aleksandr Bezobchuk <[email protected]>
Co-authored-by: Aleksandr Bezobchuk <[email protected]>
  • Loading branch information
3 people authored and JeancarloBarrios committed Sep 28, 2024
1 parent 5dff1d7 commit 03044f1
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 92 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ Ref: https://keepachangelog.com/en/1.0.0/
## [v0.44.4](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.44.4) - 2021-11-25

### Improvements

* (types) [\#10630](https://github.com/cosmos/cosmos-sdk/pull/10630) Add an `Events` field to the `TxResponse` type that captures _all_ events emitted by a transaction, unlike `Logs` which only contains events emitted during message execution.
* (x/upgrade) [\#10532](https://github.com/cosmos/cosmos-sdk/pull/10532) Add `keeper.DumpUpgradeInfoWithInfoToDisk` to include `Plan.Info` in the upgrade-info file.
* (store) [\#10544](https://github.com/cosmos/cosmos-sdk/pull/10544) Use the new IAVL iterator structure which significantly improves iterator performance.

Expand Down
7 changes: 4 additions & 3 deletions proto/cosmos/base/abci/v1beta1/abci.proto
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ message TxResponse {
string timestamp = 12;
// Events defines all the events emitted by processing a transaction. Note,
// these events include those emitted by processing all the messages and those
// emitted from the ante. Whereas Logs contains the events, with
// emitted from the ante handler. Whereas Logs contains the events, with
// additional metadata, emitted only by processing the messages.
repeated cometbft.abci.v1.Event events = 13
[(gogoproto.nullable) = false, (cosmos_proto.field_added_in) = "cosmos-sdk 0.45"];
//
// Since: cosmos-sdk 0.42.11, 0.44.5, 0.45
repeated tendermint.abci.Event events = 13 [(gogoproto.nullable) = false];
}

// ABCIMessageLog defines a structure containing an indexed tx ABCI message log.
Expand Down
132 changes: 65 additions & 67 deletions types/abci.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 59 additions & 10 deletions types/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package types
import (
"encoding/hex"
"encoding/json"
"math"
"strings"

cmtproto "github.com/cometbft/cometbft/api/cometbft/types/v1"
coretypes "github.com/cometbft/cometbft/rpc/core/types"
gogoprotoany "github.com/cosmos/gogoproto/types/any"
"github.com/gogo/protobuf/proto"
abci "github.com/tendermint/tendermint/abci/types"
ctypes "github.com/tendermint/tendermint/rpc/core/types"

"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
Expand Down Expand Up @@ -86,16 +87,64 @@ func NewResponseResultBlock(res *coretypes.ResultBlock, timestamp string) *cmtpr
return nil
}

blk, err := res.Block.ToProto()
if err != nil {
if !res.CheckTx.IsOK() {
return newTxResponseCheckTx(res)
}

return newTxResponseDeliverTx(res)
}

func newTxResponseCheckTx(res *ctypes.ResultBroadcastTxCommit) *TxResponse {
if res == nil {
return nil
}

var txHash string
if res.Hash != nil {
txHash = res.Hash.String()
}

parsedLogs, _ := ParseABCILogs(res.CheckTx.Log)

return &TxResponse{
Height: res.Height,
TxHash: txHash,
Codespace: res.CheckTx.Codespace,
Code: res.CheckTx.Code,
Data: strings.ToUpper(hex.EncodeToString(res.CheckTx.Data)),
RawLog: res.CheckTx.Log,
Logs: parsedLogs,
Info: res.CheckTx.Info,
GasWanted: res.CheckTx.GasWanted,
GasUsed: res.CheckTx.GasUsed,
Events: res.CheckTx.Events,
}
}

func newTxResponseDeliverTx(res *ctypes.ResultBroadcastTxCommit) *TxResponse {
if res == nil {
return nil
}

return &cmtproto.Block{
Header: blk.Header,
Data: blk.Data,
Evidence: blk.Evidence,
LastCommit: blk.LastCommit,
var txHash string
if res.Hash != nil {
txHash = res.Hash.String()
}

parsedLogs, _ := ParseABCILogs(res.DeliverTx.Log)

return &TxResponse{
Height: res.Height,
TxHash: txHash,
Codespace: res.DeliverTx.Codespace,
Code: res.DeliverTx.Code,
Data: strings.ToUpper(hex.EncodeToString(res.DeliverTx.Data)),
RawLog: res.DeliverTx.Log,
Logs: parsedLogs,
Info: res.DeliverTx.Info,
GasWanted: res.DeliverTx.GasWanted,
GasUsed: res.DeliverTx.GasUsed,
Events: res.DeliverTx.Events,
}
}

Expand Down
92 changes: 80 additions & 12 deletions types/result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ import (
"testing"
"time"

abci "github.com/cometbft/cometbft/api/cometbft/abci/v1"
cmtt "github.com/cometbft/cometbft/api/cometbft/types/v1"
coretypes "github.com/cometbft/cometbft/rpc/core/types"
cmt "github.com/cometbft/cometbft/types"
"github.com/golang/protobuf/proto"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/bytes"
ctypes "github.com/tendermint/tendermint/rpc/core/types"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
)

Expand Down Expand Up @@ -152,16 +155,81 @@ func (s *resultTestSuite) TestResponseResultBlock() {
timestamp := time.Now()
timestampStr := timestamp.UTC().Format(time.RFC3339)

// create a block
resultBlock := &coretypes.ResultBlock{Block: &cmt.Block{
Header: cmt.Header{
Height: 10,
Time: timestamp,
// test checkTx
checkTxResult := &ctypes.ResultBroadcastTxCommit{
Height: 10,
Hash: bytes.HexBytes([]byte("test")),
CheckTx: abci.ResponseCheckTx{
Code: 90,
Data: nil,
Log: `[]`,
Info: "info",
GasWanted: 99,
GasUsed: 100,
Codespace: "codespace",
Events: []abci.Event{
{
Type: "message",
Attributes: []abci.EventAttribute{
{
Key: []byte("action"),
Value: []byte("foo"),
Index: true,
},
},
},
},
},
Evidence: cmt.EvidenceData{
Evidence: make(cmt.EvidenceList, 0),
}
deliverTxResult := &ctypes.ResultBroadcastTxCommit{
Height: 10,
Hash: bytes.HexBytes([]byte("test")),
DeliverTx: abci.ResponseDeliverTx{
Code: 90,
Data: nil,
Log: `[]`,
Info: "info",
GasWanted: 99,
GasUsed: 100,
Codespace: "codespace",
Events: []abci.Event{
{
Type: "message",
Attributes: []abci.EventAttribute{
{
Key: []byte("action"),
Value: []byte("foo"),
Index: true,
},
},
},
},
},
}}
}
want := &sdk.TxResponse{
Height: 10,
TxHash: "74657374",
Codespace: "codespace",
Code: 90,
Data: "",
RawLog: `[]`,
Logs: logs,
Info: "info",
GasWanted: 99,
GasUsed: 100,
Events: []abci.Event{
{
Type: "message",
Attributes: []abci.EventAttribute{
{
Key: []byte("action"),
Value: []byte("foo"),
Index: true,
},
},
},
},
}

blk, err := resultBlock.Block.ToProto()
s.Require().NoError(err)
Expand Down

0 comments on commit 03044f1

Please sign in to comment.