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

Add Events to ABCIMessageLog #5032

Merged
merged 6 commits into from
Sep 12, 2019
Merged
Show file tree
Hide file tree
Changes from 5 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 4 additions & 10 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
}
Expand Down
22 changes: 20 additions & 2 deletions types/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"math"
"strings"

"github.com/cosmos/cosmos-sdk/codec"

ctypes "github.com/tendermint/tendermint/rpc/core/types"
)

Expand Down Expand Up @@ -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)
}
Expand All @@ -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
Expand Down
11 changes: 11 additions & 0 deletions types/result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package types
import (
"testing"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/stretchr/testify/require"
)

Expand All @@ -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())
}