Skip to content

Commit

Permalink
[backport v6.2.x] imp(core): allow huckleberry events with a prefix (#…
Browse files Browse the repository at this point in the history
…5541) (#7260)

* imp(core): allow huckleberry events with a prefix (#5541)

* feat: initial impl

* imp: moved event helpers to types and added tests

* imp(testing): added mock events to mock module

* test: added msg_server tests for application events

* imp: converted suffix to prefix

* docs: updated inline comments

* imp: review item

* test: review items

* imp: review items

* chore: rm markdown link checker

---------

Co-authored-by: srdtrk <59252793+srdtrk@users.noreply.github.com>
  • Loading branch information
damiannolan and srdtrk authored Sep 10, 2024
1 parent a89bb2f commit 639f146
Show file tree
Hide file tree
Showing 9 changed files with 260 additions and 53 deletions.
10 changes: 0 additions & 10 deletions .github/workflows/link-check-config.json

This file was deleted.

10 changes: 0 additions & 10 deletions .github/workflows/link-check.yml

This file was deleted.

106 changes: 106 additions & 0 deletions modules/core/keeper/events_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package keeper_test

import (
"testing"

"github.com/stretchr/testify/require"

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

"github.com/cosmos/ibc-go/v6/modules/core/keeper"
"github.com/cosmos/ibc-go/v6/modules/core/types"
)

func TestConvertToErrorEvents(t *testing.T) {
var (
events sdk.Events
expEvents sdk.Events
)

tc := []struct {
name string
malleate func()
}{
{
"success: nil events",
func() {
events = nil
expEvents = nil
},
},
{
"success: empty events",
func() {
events = sdk.Events{}
expEvents = sdk.Events{}
},
},
{
"success: event with no attributes",
func() {
events = sdk.Events{
sdk.NewEvent("testevent"),
}
expEvents = sdk.Events{
sdk.NewEvent(types.ErrorAttributeKeyPrefix + "testevent"),
}
},
},
{
"success: event with attributes",
func() {
events = sdk.Events{
sdk.NewEvent("testevent",
sdk.NewAttribute("key1", "value1"),
sdk.NewAttribute("key2", "value2"),
),
}
expEvents = sdk.Events{
sdk.NewEvent(types.ErrorAttributeKeyPrefix+"testevent",
sdk.NewAttribute(types.ErrorAttributeKeyPrefix+"key1", "value1"),
sdk.NewAttribute(types.ErrorAttributeKeyPrefix+"key2", "value2"),
),
}
},
},
{
"success: multiple events with attributes",
func() {
events = sdk.Events{
sdk.NewEvent("testevent1",
sdk.NewAttribute("key1", "value1"),
sdk.NewAttribute("key2", "value2"),
),
sdk.NewEvent("testevent2",
sdk.NewAttribute("key3", "value3"),
sdk.NewAttribute("key4", "value4"),
),
}
expEvents = sdk.Events{
sdk.NewEvent(types.ErrorAttributeKeyPrefix+"testevent1",
sdk.NewAttribute(types.ErrorAttributeKeyPrefix+"key1", "value1"),
sdk.NewAttribute(types.ErrorAttributeKeyPrefix+"key2", "value2"),
),
sdk.NewEvent(types.ErrorAttributeKeyPrefix+"testevent2",
sdk.NewAttribute(types.ErrorAttributeKeyPrefix+"key3", "value3"),
sdk.NewAttribute(types.ErrorAttributeKeyPrefix+"key4", "value4"),
),
}
},
},
}

for _, tc := range tc {
t.Run(tc.name, func(t *testing.T) {
// initial events and expected events are reset so that the test fails if
// the malleate function does not set them
events = nil
expEvents = sdk.Events{}

tc.malleate()

newEvents := keeper.ConvertToErrorEvents(events)
require.Equal(t, expEvents, newEvents)
})
}
}
9 changes: 9 additions & 0 deletions modules/core/keeper/export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package keeper

import sdk "github.com/cosmos/cosmos-sdk/types"

// ConvertToErrorEvents is a wrapper around convertToErrorEvents
// to allow the function to be directly called in tests.
func ConvertToErrorEvents(events sdk.Events) sdk.Events {
return convertToErrorEvents(events)
}
25 changes: 25 additions & 0 deletions modules/core/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,9 @@ func (k Keeper) RecvPacket(goCtx context.Context, msg *channeltypes.MsgRecvPacke
if ack == nil || ack.Success() {
// write application state changes for asynchronous and successful acknowledgements
writeFn()
} else {
// Modify events in cached context to reflect unsuccessful acknowledgement
ctx.EventManager().EmitEvents(convertToErrorEvents(cacheCtx.EventManager().Events()))
}

// Set packet acknowledgement only if the acknowledgement is not nil.
Expand Down Expand Up @@ -632,3 +635,25 @@ func (k Keeper) Acknowledgement(goCtx context.Context, msg *channeltypes.MsgAckn

return &channeltypes.MsgAcknowledgementResponse{Result: channeltypes.SUCCESS}, nil
}

// convertToErrorEvents converts all events to error events by appending the
// error attribute prefix to each event's attribute key.
func convertToErrorEvents(events sdk.Events) sdk.Events {
if events == nil {
return nil
}

newEvents := make(sdk.Events, len(events))
for i, event := range events {
newAttributes := make([]sdk.Attribute, len(event.Attributes))
for j, attribute := range event.Attributes {
newAttributes[j] = sdk.NewAttribute(coretypes.ErrorAttributeKeyPrefix+string(attribute.Key), string(attribute.Value))
}

// no need to append the error attribute prefix to the event type because
// the event type is not associated to a value that can be misinterpreted
newEvents[i] = sdk.NewEvent(coretypes.ErrorAttributeKeyPrefix+event.Type, newAttributes...)
}

return newEvents
}
Loading

0 comments on commit 639f146

Please sign in to comment.