From a1cbf1043506ce068049cde77deebd35d2d0219f Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Wed, 11 Sep 2019 10:11:45 -0400 Subject: [PATCH 1/5] Add events to ABCIMessageLog --- baseapp/baseapp.go | 9 +++++---- types/result.go | 7 +++++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index 6b91e3d3fe76..d53b5640032e 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -664,7 +664,7 @@ 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} + idxLog := sdk.ABCIMessageLog{MsgIndex: uint16(i), Log: msgResult.Log, Events: events} // stop execution and return on first failed message if !msgResult.IsOK() { @@ -680,14 +680,15 @@ func (app *BaseApp) runMsgs(ctx sdk.Context, msgs []sdk.Msg, mode runTxMode) (re idxLogs = append(idxLogs, idxLog) } - logJSON := codec.Cdc.MustMarshalJSON(idxLogs) result = sdk.Result{ Code: code, Codespace: codespace, Data: data, - Log: strings.TrimSpace(string(logJSON)), + Log: strings.TrimSpace(string(codec.Cdc.MustMarshalJSON(idxLogs))), GasUsed: ctx.GasMeter().GasConsumed(), - Events: events, + + // DEPRECATED: Remove in next major release. + Events: events, } return result diff --git a/types/result.go b/types/result.go index 58ea83321ce4..a6df029b44ec 100644 --- a/types/result.go +++ b/types/result.go @@ -34,6 +34,9 @@ type Result struct { // Events contains a slice of Event objects that were emitted during some // execution. + // + // DEPRECATED: Remove in the next next major release in favor of using the + // ABCIMessageLog.Events field. Events Events } @@ -50,6 +53,10 @@ 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 Events `json:"events"` } // String implements the fmt.Stringer interface for the ABCIMessageLogs type. From 69890e47d50fb4d98ed5ff52c2845ee1b82f2888 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Wed, 11 Sep 2019 10:15:17 -0400 Subject: [PATCH 2/5] Add changelog entry --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a07ffd1cb698..502915ba4a2d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -88,6 +88,9 @@ block time is >= the `halt-time`. 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 From 19aa1236a14f857686b65b44e36b7d56ff0f0b46 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Wed, 11 Sep 2019 15:28:30 -0400 Subject: [PATCH 3/5] Add events to ABCIMessageLogs --- baseapp/baseapp.go | 13 ++++--------- types/result.go | 23 +++++++++++++++++------ 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index d53b5640032e..1f94d7718e29 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 + idxLogs := make(sdk.ABCIMessageLogs, 0, len(msgs)) var ( data []byte @@ -664,27 +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, Events: events} - // stop execution and return on first failed message if !msgResult.IsOK() { - idxLog.Success = false - idxLogs = append(idxLogs, idxLog) + idxLogs = append(idxLogs, sdk.NewABCIMessageLog(uint16(i), false, msgResult.Log, events)) code = msgResult.Code codespace = msgResult.Codespace break } - idxLog.Success = true - idxLogs = append(idxLogs, idxLog) + idxLogs = append(idxLogs, sdk.NewABCIMessageLog(uint16(i), true, msgResult.Log, events)) } result = sdk.Result{ Code: code, Codespace: codespace, Data: data, - Log: strings.TrimSpace(string(codec.Cdc.MustMarshalJSON(idxLogs))), + Log: strings.TrimSpace(idxLogs.String()), GasUsed: ctx.GasMeter().GasConsumed(), // DEPRECATED: Remove in next major release. diff --git a/types/result.go b/types/result.go index a6df029b44ec..41a31b84ba3c 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" ) @@ -34,9 +36,6 @@ type Result struct { // Events contains a slice of Event objects that were emitted during some // execution. - // - // DEPRECATED: Remove in the next next major release in favor of using the - // ABCIMessageLog.Events field. Events Events } @@ -56,13 +55,22 @@ type ABCIMessageLog struct { // Events contains a slice of Event objects that were emitted during some // execution. - Events Events `json:"events"` + 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) } @@ -83,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 From 4c9f36d70f28f10759ef9282ce89164ac0826b1d Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Wed, 11 Sep 2019 15:32:10 -0400 Subject: [PATCH 4/5] Cleanup --- baseapp/baseapp.go | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index 1f94d7718e29..f284cc903031 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -629,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.ABCIMessageLogs, 0, len(msgs)) + msgLogs := make(sdk.ABCIMessageLogs, 0, len(msgs)) var ( data []byte @@ -665,25 +665,23 @@ func (app *BaseApp) runMsgs(ctx sdk.Context, msgs []sdk.Msg, mode runTxMode) (re // stop execution and return on first failed message if !msgResult.IsOK() { - idxLogs = append(idxLogs, sdk.NewABCIMessageLog(uint16(i), false, msgResult.Log, events)) + msgLogs = append(msgLogs, sdk.NewABCIMessageLog(uint16(i), false, msgResult.Log, events)) code = msgResult.Code codespace = msgResult.Codespace break } - idxLogs = append(idxLogs, sdk.NewABCIMessageLog(uint16(i), true, msgResult.Log, events)) + msgLogs = append(msgLogs, sdk.NewABCIMessageLog(uint16(i), true, msgResult.Log, events)) } result = sdk.Result{ Code: code, Codespace: codespace, Data: data, - Log: strings.TrimSpace(idxLogs.String()), + Log: strings.TrimSpace(msgLogs.String()), GasUsed: ctx.GasMeter().GasConsumed(), - - // DEPRECATED: Remove in next major release. - Events: events, + Events: events, } return result From c1ce5776334eafbf68daa1ee42023dd4c2a3bc17 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Wed, 11 Sep 2019 15:33:58 -0400 Subject: [PATCH 5/5] Add TestABCIMessageLog --- types/result_test.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/types/result_test.go b/types/result_test.go index 6a020d47455b..c4fedc2992f3 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()) +}