Skip to content

Commit

Permalink
refactor(abci): add msg index to event (cosmos#15845)
Browse files Browse the repository at this point in the history
Co-authored-by: Facundo Medica <14063057+facundomedica@users.noreply.github.com>
Co-authored-by: Facundo Medica <facundomedica@gmail.com>
  • Loading branch information
3 people authored and czarcas7ic committed Feb 13, 2024
1 parent 1e7ee79 commit 4a0fb85
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 16 deletions.
2 changes: 1 addition & 1 deletion baseapp/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ func TestABCI_DeliverTx(t *testing.T) {
events := res.GetEvents()
require.Len(t, events, 3, "should contain ante handler, message type and counter events respectively")
require.Equal(t, sdk.MarkEventsToIndex(counterEvent("ante_handler", counter).ToABCIEvents(), map[string]struct{}{})[0], events[0], "ante handler event")
require.Equal(t, sdk.MarkEventsToIndex(counterEvent(sdk.EventTypeMessage, counter).ToABCIEvents(), map[string]struct{}{})[0], events[2], "msg handler update counter event")
require.Equal(t, sdk.MarkEventsToIndex(counterEvent(sdk.EventTypeMessage, counter).ToABCIEvents(), map[string]struct{}{})[0].Attributes[0], events[2].Attributes[0], "msg handler update counter event")
}

suite.baseApp.EndBlock(abci.RequestEndBlock{})
Expand Down
11 changes: 7 additions & 4 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"sort"
"strconv"
"strings"

dbm "github.com/cometbft/cometbft-db"
Expand Down Expand Up @@ -781,7 +782,6 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, re
// Handler does not exist for a given message route. Otherwise, a reference to a
// Result is returned. The caller must not commit state if an error is returned.
func (app *BaseApp) runMsgs(ctx sdk.Context, msgs []sdk.Msg, mode runTxMode) (*sdk.Result, error) {
msgLogs := make(sdk.ABCIMessageLogs, 0, len(msgs))
events := sdk.EmptyEvents()
var msgResponses []*codectypes.Any

Expand All @@ -805,10 +805,15 @@ func (app *BaseApp) runMsgs(ctx sdk.Context, msgs []sdk.Msg, mode runTxMode) (*s
// create message events
msgEvents := createEvents(msgResult.GetEvents(), msg)

// append message events, data and logs
// append message events and data
//
// Note: Each message result's data must be length-prefixed in order to
// separate each result.
for j, event := range msgEvents {
// append message index to all events
msgEvents[j] = event.AppendAttributes(sdk.NewAttribute("msg_index", strconv.Itoa(i)))
}

events = events.AppendEvents(msgEvents)

// Each individual sdk.Result that went through the MsgServiceRouter
Expand All @@ -824,7 +829,6 @@ func (app *BaseApp) runMsgs(ctx sdk.Context, msgs []sdk.Msg, mode runTxMode) (*s
msgResponses = append(msgResponses, msgResponse)
}

msgLogs = append(msgLogs, sdk.NewABCIMessageLog(uint32(i), msgResult.Log, msgEvents))
}

data, err := makeABCIData(msgResponses)
Expand All @@ -834,7 +838,6 @@ func (app *BaseApp) runMsgs(ctx sdk.Context, msgs []sdk.Msg, mode runTxMode) (*s

return &sdk.Result{
Data: data,
Log: strings.TrimSpace(msgLogs.String()),
Events: events.ToABCIEvents(),
MsgResponses: msgResponses,
}, nil
Expand Down
1 change: 1 addition & 0 deletions docs/docs/core/08-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ An Event contains:

* A `type` to categorize the Event at a high-level; for example, the Cosmos SDK uses the `"message"` type to filter Events by `Msg`s.
* A list of `attributes` are key-value pairs that give more information about the categorized Event. For example, for the `"message"` type, we can filter Events by key-value pairs using `message.action={some_action}`, `message.module={some_module}` or `message.sender={some_sender}`.
* A `msg_index` to identify which messages relate to the same transaction

:::tip
To parse the attribute values as strings, make sure to add `'` (single quotes) around each attribute value.
Expand Down
21 changes: 17 additions & 4 deletions tests/e2e/auth/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
"strings"
"testing"

"cosmossdk.io/depinject"
"cosmossdk.io/math"
abci "github.com/cometbft/cometbft/abci/types"
tmcli "github.com/cometbft/cometbft/libs/cli"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"

"cosmossdk.io/depinject"
"cosmossdk.io/math"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/hd"
Expand Down Expand Up @@ -545,7 +545,9 @@ func (s *E2ETestSuite) TestCLIQueryTxCmdByHash() {
var result sdk.TxResponse
s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &result))
s.Require().NotNil(result.Height)
s.Require().Contains(result.RawLog, tc.rawLogContains)
if ok := s.deepContains(result.Events, tc.rawLogContains); !ok {
s.Require().Fail("raw log does not contain the expected value, expected value: %s", tc.rawLogContains)
}
}
})
}
Expand Down Expand Up @@ -2062,3 +2064,14 @@ func (s *E2ETestSuite) getBalances(clientCtx client.Context, addr sdk.AccAddress
startTokens := balRes.Balances.AmountOf(denom)
return startTokens
}

func (s *E2ETestSuite) deepContains(events []abci.Event, value string) bool {
for _, e := range events {
for _, attr := range e.Attributes {
if strings.Contains(attr.Value, value) {
return true
}
}
}
return false
}
2 changes: 1 addition & 1 deletion tests/e2e/staking/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func (s *E2ETestSuite) TestNewCreateValidatorCmd() {
s.Require().Equal(tc.expectedCode, txResp.Code, out.String())

var hadEvent bool
events := txResp.Logs[0].GetEvents()
events := txResp.Events
for i := 0; i < len(events); i++ {
if events[i].GetType() == "create_validator" {
attributes := events[i].GetAttributes()
Expand Down
13 changes: 7 additions & 6 deletions tests/e2e/tx/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ func (s E2ETestSuite) TestGetTxEvents_GRPC() {
Page: 2,
Limit: 2,
},
false, "", 1,
false, "", 2,
},
{
"with multi events",
Expand All @@ -361,12 +361,13 @@ func (s E2ETestSuite) TestGetTxEvents_GRPC() {
s.Require().NoError(err)
s.Require().GreaterOrEqual(len(grpcRes.Txs), 1)
s.Require().Equal("foobar", grpcRes.Txs[0].Body.Memo)
s.Require().Equal(len(grpcRes.Txs), tc.expLen)
s.Require().Equal(tc.expLen, len(grpcRes.Txs))

// Make sure fields are populated.
// ref: https://github.com/cosmos/cosmos-sdk/issues/8680
// ref: https://github.com/cosmos/cosmos-sdk/issues/8681
s.Require().NotEmpty(grpcRes.TxResponses[0].Timestamp)
s.Require().NotEmpty(grpcRes.TxResponses[0].RawLog)
s.Require().Empty(grpcRes.TxResponses[0].RawLog) // logs are empty if the transactions are successful
}
})
}
Expand Down Expand Up @@ -395,9 +396,9 @@ func (s E2ETestSuite) TestGetTxEvents_GRPCGateway() {
},
{
"with pagination",
fmt.Sprintf("%s/cosmos/tx/v1beta1/txs?events=%s&page=%d&limit=%d", val.APIAddress, bankMsgSendEventAction, 2, 2),
fmt.Sprintf("%s/cosmos/tx/v1beta1/txs?events=%s&page=%d&limit=%d", val.APIAddress, bankMsgSendEventAction, 1, 2),
false,
"", 1,
"", 2,
},
{
"valid request: order by asc",
Expand Down Expand Up @@ -517,7 +518,7 @@ func (s E2ETestSuite) TestGetTx_GRPCGateway() {
// ref: https://github.com/cosmos/cosmos-sdk/issues/8680
// ref: https://github.com/cosmos/cosmos-sdk/issues/8681
s.Require().NotEmpty(result.TxResponse.Timestamp)
s.Require().NotEmpty(result.TxResponse.RawLog)
s.Require().Empty(result.TxResponse.RawLog) // logs are empty on successful transactions
}
})
}
Expand Down

0 comments on commit 4a0fb85

Please sign in to comment.