From 4a0fb851cb7358a10761442b424bd16ae9541ab9 Mon Sep 17 00:00:00 2001 From: Marko Date: Mon, 24 Apr 2023 11:07:14 +0200 Subject: [PATCH 1/9] refactor(abci): add msg index to event (#15845) Co-authored-by: Facundo Medica <14063057+facundomedica@users.noreply.github.com> Co-authored-by: Facundo Medica --- baseapp/abci_test.go | 2 +- baseapp/baseapp.go | 11 +++++++---- docs/docs/core/08-events.md | 1 + tests/e2e/auth/suite.go | 21 +++++++++++++++++---- tests/e2e/staking/suite.go | 2 +- tests/e2e/tx/service_test.go | 13 +++++++------ 6 files changed, 34 insertions(+), 16 deletions(-) diff --git a/baseapp/abci_test.go b/baseapp/abci_test.go index c3d606db018a..691b6c94b8cf 100644 --- a/baseapp/abci_test.go +++ b/baseapp/abci_test.go @@ -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{}) diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index 9b4faa5376ed..704732714746 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "sort" + "strconv" "strings" dbm "github.com/cometbft/cometbft-db" @@ -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 @@ -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 @@ -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) @@ -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 diff --git a/docs/docs/core/08-events.md b/docs/docs/core/08-events.md index e8b1d5c33384..be19d9e3e907 100644 --- a/docs/docs/core/08-events.md +++ b/docs/docs/core/08-events.md @@ -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. diff --git a/tests/e2e/auth/suite.go b/tests/e2e/auth/suite.go index e103f46052ae..489e7d6ac9bb 100644 --- a/tests/e2e/auth/suite.go +++ b/tests/e2e/auth/suite.go @@ -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" @@ -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) + } } }) } @@ -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 +} diff --git a/tests/e2e/staking/suite.go b/tests/e2e/staking/suite.go index 1b60e726b04f..a04a2debcb6b 100644 --- a/tests/e2e/staking/suite.go +++ b/tests/e2e/staking/suite.go @@ -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() diff --git a/tests/e2e/tx/service_test.go b/tests/e2e/tx/service_test.go index f174344aabe5..dd370a09be2e 100644 --- a/tests/e2e/tx/service_test.go +++ b/tests/e2e/tx/service_test.go @@ -340,7 +340,7 @@ func (s E2ETestSuite) TestGetTxEvents_GRPC() { Page: 2, Limit: 2, }, - false, "", 1, + false, "", 2, }, { "with multi events", @@ -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 } }) } @@ -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", @@ -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 } }) } From 7179370c07c1a2aa9b1ed34b886a0d5e817c7f66 Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Tue, 13 Feb 2024 16:57:32 -0700 Subject: [PATCH 2/9] fix e2e tests --- tests/e2e/group/tx.go | 3 +-- tests/e2e/tx/service_test.go | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/e2e/group/tx.go b/tests/e2e/group/tx.go index bd54fc9b2baf..6a90879ef2b6 100644 --- a/tests/e2e/group/tx.go +++ b/tests/e2e/group/tx.go @@ -2489,14 +2489,13 @@ func (s *E2ETestSuite) getGroupIDFromTxResponse(txResp sdk.TxResponse) string { s.Require().Greater(len(txResp.Logs), 0) s.Require().NotNil(txResp.Logs[0].Events) events := txResp.Logs[0].Events - createProposalEvent, _ := sdk.TypedEventToEvent(&group.EventCreateGroup{}) + createProposalEvent, _ := sdk.TypedEventToEvent(&group.EventSubmitProposal{}) for _, e := range events { if e.Type == createProposalEvent.Type { return strings.ReplaceAll(e.Attributes[0].Value, "\"", "") } } - return "" } diff --git a/tests/e2e/tx/service_test.go b/tests/e2e/tx/service_test.go index dd370a09be2e..a89ac9b7daab 100644 --- a/tests/e2e/tx/service_test.go +++ b/tests/e2e/tx/service_test.go @@ -337,7 +337,7 @@ func (s E2ETestSuite) TestGetTxEvents_GRPC() { "with pagination", &tx.GetTxsEventRequest{ Events: []string{bankMsgSendEventAction}, - Page: 2, + Page: 1, Limit: 2, }, false, "", 2, From 00be831663f1c02382b7b2b23ae6f7b3279c93e6 Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Tue, 13 Feb 2024 19:06:27 -0700 Subject: [PATCH 3/9] another e2e fix --- tests/e2e/group/tx.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/e2e/group/tx.go b/tests/e2e/group/tx.go index 6a90879ef2b6..9d9c0122f344 100644 --- a/tests/e2e/group/tx.go +++ b/tests/e2e/group/tx.go @@ -2486,9 +2486,9 @@ func (s *E2ETestSuite) TestExecProposalsWhenMemberLeavesOrIsUpdated() { } func (s *E2ETestSuite) getGroupIDFromTxResponse(txResp sdk.TxResponse) string { - s.Require().Greater(len(txResp.Logs), 0) - s.Require().NotNil(txResp.Logs[0].Events) - events := txResp.Logs[0].Events + s.Require().Greater(len(txResp.Events), 0) + s.Require().NotNil(txResp.Events[0]) + events := txResp.Events createProposalEvent, _ := sdk.TypedEventToEvent(&group.EventSubmitProposal{}) for _, e := range events { From 48efcb188693db3b55e43331874da95f434293ae Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Tue, 13 Feb 2024 20:55:03 -0700 Subject: [PATCH 4/9] e2e --- tests/e2e/group/tx.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/e2e/group/tx.go b/tests/e2e/group/tx.go index 9d9c0122f344..e57ba6472353 100644 --- a/tests/e2e/group/tx.go +++ b/tests/e2e/group/tx.go @@ -1970,9 +1970,9 @@ func (s *E2ETestSuite) TestTxWithdrawProposal() { } func (s *E2ETestSuite) getProposalIDFromTxResponse(txResp sdk.TxResponse) string { - s.Require().Greater(len(txResp.Logs), 0) - s.Require().NotNil(txResp.Logs[0].Events) - events := txResp.Logs[0].Events + s.Require().Greater(len(txResp.Events), 0) + s.Require().NotNil(txResp.Events[0]) + events := txResp.Events createProposalEvent, _ := sdk.TypedEventToEvent(&group.EventSubmitProposal{}) for _, e := range events { From 015514fcf84aee4b17a67d5ed6f502056cc78e0d Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Tue, 13 Feb 2024 21:10:11 -0700 Subject: [PATCH 5/9] reset groupID --- tests/e2e/group/tx.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/e2e/group/tx.go b/tests/e2e/group/tx.go index e57ba6472353..596f546973f4 100644 --- a/tests/e2e/group/tx.go +++ b/tests/e2e/group/tx.go @@ -2486,9 +2486,9 @@ func (s *E2ETestSuite) TestExecProposalsWhenMemberLeavesOrIsUpdated() { } func (s *E2ETestSuite) getGroupIDFromTxResponse(txResp sdk.TxResponse) string { - s.Require().Greater(len(txResp.Events), 0) - s.Require().NotNil(txResp.Events[0]) - events := txResp.Events + s.Require().Greater(len(txResp.Logs), 0) + s.Require().NotNil(txResp.Logs[0].Events) + events := txResp.Logs[0].Events createProposalEvent, _ := sdk.TypedEventToEvent(&group.EventSubmitProposal{}) for _, e := range events { From 3c6a449cb1919db384184edd767836ff0c04c1bb Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Tue, 13 Feb 2024 21:43:39 -0700 Subject: [PATCH 6/9] e2e --- tests/e2e/group/tx.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/e2e/group/tx.go b/tests/e2e/group/tx.go index 596f546973f4..1807a5fdb025 100644 --- a/tests/e2e/group/tx.go +++ b/tests/e2e/group/tx.go @@ -2486,10 +2486,10 @@ func (s *E2ETestSuite) TestExecProposalsWhenMemberLeavesOrIsUpdated() { } func (s *E2ETestSuite) getGroupIDFromTxResponse(txResp sdk.TxResponse) string { - s.Require().Greater(len(txResp.Logs), 0) - s.Require().NotNil(txResp.Logs[0].Events) - events := txResp.Logs[0].Events - createProposalEvent, _ := sdk.TypedEventToEvent(&group.EventSubmitProposal{}) + s.Require().Greater(len(txResp.Events), 0) + s.Require().NotNil(txResp.Events[0]) + events := txResp.Events + createProposalEvent, _ := sdk.TypedEventToEvent(&group.EventCreateGroup{}) for _, e := range events { if e.Type == createProposalEvent.Type { From 704e56f72ee84baa4e5289c8761e666723343571 Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Tue, 13 Feb 2024 22:02:01 -0700 Subject: [PATCH 7/9] switch check to events --- tests/e2e/group/tx.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/e2e/group/tx.go b/tests/e2e/group/tx.go index 1807a5fdb025..d60a09463182 100644 --- a/tests/e2e/group/tx.go +++ b/tests/e2e/group/tx.go @@ -2479,7 +2479,7 @@ func (s *E2ETestSuite) TestExecProposalsWhenMemberLeavesOrIsUpdated() { s.Require().NoError(err) if tc.expectLogErr { - s.Require().Contains(execResp.RawLog, tc.errMsg) + s.Require().Contains(execResp.Events, tc.errMsg) } }) } From 2eca4ba91edc3dab2e048da70d88c120d52a88ec Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Tue, 13 Feb 2024 22:30:42 -0700 Subject: [PATCH 8/9] e2e --- tests/e2e/group/tx.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/tests/e2e/group/tx.go b/tests/e2e/group/tx.go index d60a09463182..88306c82a0a0 100644 --- a/tests/e2e/group/tx.go +++ b/tests/e2e/group/tx.go @@ -2479,7 +2479,19 @@ func (s *E2ETestSuite) TestExecProposalsWhenMemberLeavesOrIsUpdated() { s.Require().NoError(err) if tc.expectLogErr { - s.Require().Contains(execResp.Events, tc.errMsg) + found := false + for _, event := range execResp.Events { + for _, attr := range event.Attributes { + if attr.Value == tc.errMsg { + found = true + break + } + } + if found { + break + } + } + s.Require().True(found, "Expected to find '%s' in execResp.Events", tc.errMsg) } }) } From b031e44be45a17f43a033b7801f00a7944a1811f Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Tue, 13 Feb 2024 23:00:25 -0700 Subject: [PATCH 9/9] e2e --- tests/e2e/group/tx.go | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/tests/e2e/group/tx.go b/tests/e2e/group/tx.go index 88306c82a0a0..9c660ccbf465 100644 --- a/tests/e2e/group/tx.go +++ b/tests/e2e/group/tx.go @@ -2479,19 +2479,7 @@ func (s *E2ETestSuite) TestExecProposalsWhenMemberLeavesOrIsUpdated() { s.Require().NoError(err) if tc.expectLogErr { - found := false - for _, event := range execResp.Events { - for _, attr := range event.Attributes { - if attr.Value == tc.errMsg { - found = true - break - } - } - if found { - break - } - } - s.Require().True(found, "Expected to find '%s' in execResp.Events", tc.errMsg) + s.Require().True(strings.Contains(execResp.Events[len(execResp.Events)-1].String(), tc.errMsg)) } }) }