From 9eb5375fda401da9bf4f951322711de45b08ff44 Mon Sep 17 00:00:00 2001 From: Alexander Bezobchuk Date: Thu, 12 Sep 2019 11:05:27 -0400 Subject: [PATCH] Merge PR #5032: Add Events to ABCIMessageLog --- CHANGELOG.md | 3 +++ baseapp/baseapp.go | 14 ++++---------- types/result.go | 22 ++++++++++++++++++++-- types/result_test.go | 11 +++++++++++ 4 files changed, 38 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d37557e44..ea39bc6d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -93,6 +93,9 @@ and tx hash will be returned for specific Tendermint errors: caching through `CommitKVStoreCacheManager`. Any application wishing to utilize an inter-block cache must set it in their app via a `BaseApp` option. The `BaseApp` docs have been drastically improved to detail this new feature and how state transitions occur. +* [\#4990](https://github.com/cosmos/cosmos-sdk/issues/4990) Add `Events` to the `ABCIMessageLog` to +provide context and grouping of events based on the messages they correspond to. The `Events` field +in `TxResponse` is deprecated and will be removed in the next major release. ### Bug Fixes diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index 6b91e3d3f..f284cc903 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -16,7 +16,6 @@ import ( "github.com/tendermint/tendermint/libs/log" dbm "github.com/tendermint/tm-db" - "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/store" storetypes "github.com/cosmos/cosmos-sdk/store/types" sdk "github.com/cosmos/cosmos-sdk/types" @@ -630,7 +629,7 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte, tx sdk.Tx) (result sdk // runMsgs iterates through all the messages and executes them. // nolint: gocyclo func (app *BaseApp) runMsgs(ctx sdk.Context, msgs []sdk.Msg, mode runTxMode) (result sdk.Result) { - idxLogs := make([]sdk.ABCIMessageLog, 0, len(msgs)) // a list of JSON-encoded logs with msg index + msgLogs := make(sdk.ABCIMessageLogs, 0, len(msgs)) var ( data []byte @@ -664,28 +663,23 @@ func (app *BaseApp) runMsgs(ctx sdk.Context, msgs []sdk.Msg, mode runTxMode) (re events = events.AppendEvent(sdk.NewEvent(sdk.EventTypeMessage, sdk.NewAttribute(sdk.AttributeKeyAction, msg.Type()))) events = events.AppendEvents(msgResult.Events) - idxLog := sdk.ABCIMessageLog{MsgIndex: uint16(i), Log: msgResult.Log} - // stop execution and return on first failed message if !msgResult.IsOK() { - idxLog.Success = false - idxLogs = append(idxLogs, idxLog) + msgLogs = append(msgLogs, sdk.NewABCIMessageLog(uint16(i), false, msgResult.Log, events)) code = msgResult.Code codespace = msgResult.Codespace break } - idxLog.Success = true - idxLogs = append(idxLogs, idxLog) + msgLogs = append(msgLogs, sdk.NewABCIMessageLog(uint16(i), true, msgResult.Log, events)) } - logJSON := codec.Cdc.MustMarshalJSON(idxLogs) result = sdk.Result{ Code: code, Codespace: codespace, Data: data, - Log: strings.TrimSpace(string(logJSON)), + Log: strings.TrimSpace(msgLogs.String()), GasUsed: ctx.GasMeter().GasConsumed(), Events: events, } diff --git a/types/result.go b/types/result.go index 58ea83321..41a31b84b 100644 --- a/types/result.go +++ b/types/result.go @@ -7,6 +7,8 @@ import ( "math" "strings" + "github.com/cosmos/cosmos-sdk/codec" + ctypes "github.com/tendermint/tendermint/rpc/core/types" ) @@ -50,12 +52,25 @@ type ABCIMessageLog struct { MsgIndex uint16 `json:"msg_index"` Success bool `json:"success"` Log string `json:"log"` + + // Events contains a slice of Event objects that were emitted during some + // execution. + Events StringEvents `json:"events"` +} + +func NewABCIMessageLog(i uint16, success bool, log string, events Events) ABCIMessageLog { + return ABCIMessageLog{ + MsgIndex: i, + Success: success, + Log: log, + Events: StringifyEvents(events.ToABCIEvents()), + } } // String implements the fmt.Stringer interface for the ABCIMessageLogs type. func (logs ABCIMessageLogs) String() (str string) { if logs != nil { - raw, err := json.Marshal(logs) + raw, err := codec.Cdc.MarshalJSON(logs) if err == nil { str = string(raw) } @@ -76,10 +91,13 @@ type TxResponse struct { Info string `json:"info,omitempty"` GasWanted int64 `json:"gas_wanted,omitempty"` GasUsed int64 `json:"gas_used,omitempty"` - Events StringEvents `json:"events,omitempty"` Codespace string `json:"codespace,omitempty"` Tx Tx `json:"tx,omitempty"` Timestamp string `json:"timestamp,omitempty"` + + // DEPRECATED: Remove in the next next major release in favor of using the + // ABCIMessageLog.Events field. + Events StringEvents `json:"events,omitempty"` } // NewResponseResultTx returns a TxResponse given a ResultTx from tendermint diff --git a/types/result_test.go b/types/result_test.go index 6a020d474..c4fedc299 100644 --- a/types/result_test.go +++ b/types/result_test.go @@ -3,6 +3,7 @@ package types import ( "testing" + "github.com/cosmos/cosmos-sdk/codec" "github.com/stretchr/testify/require" ) @@ -27,3 +28,13 @@ func TestParseABCILog(t *testing.T) { require.Equal(t, res[0].MsgIndex, uint16(1)) require.True(t, res[0].Success) } + +func TestABCIMessageLog(t *testing.T) { + events := Events{NewEvent("transfer", NewAttribute("sender", "foo"))} + msgLog := NewABCIMessageLog(0, true, "", events) + + msgLogs := ABCIMessageLogs{msgLog} + bz, err := codec.Cdc.MarshalJSON(msgLogs) + require.NoError(t, err) + require.Equal(t, string(bz), msgLogs.String()) +}