diff --git a/events/event.go b/events/event.go index 2a3fb32..25d32a9 100644 --- a/events/event.go +++ b/events/event.go @@ -2,6 +2,7 @@ package events import ( abci "github.com/tendermint/tendermint/abci/types" + "github.com/tendermint/tendermint/libs/json" ) // Event stores type, module, action and attributes list of sdk @@ -18,6 +19,44 @@ type ABCIEventWithHeight struct { abci.Event } +// Marshal extends the Marshal function of abci.Event to ABCIEventWithHeight +func (e *ABCIEventWithHeight) Marshal() (dAtA []byte, err error) { + bz, err := e.Event.Marshal() + if err != nil { + return nil, err + } + + data := struct { + Height int64 + Event []byte + }{ + Height: e.Height, + Event: bz, + } + return json.Marshal(data) +} + +// Unmarshal extends the Unmarshal function of abci.Event to ABCIEventWithHeight +func (e *ABCIEventWithHeight) Unmarshal(dAta []byte) error { + data := struct { + Height int64 + Event []byte + }{} + + if err := json.Unmarshal(dAta, &data); err != nil { + return err + } + + var event abci.Event + if err := event.Unmarshal(data.Event); err != nil { + return err + } + + e.Height = data.Height + e.Event = event + return nil +} + // Map transforms the ABCIEventWithHeight into an Event // Deprecated func Map(event ABCIEventWithHeight) Event { diff --git a/events/event_test.go b/events/event_test.go new file mode 100644 index 0000000..efb0f41 --- /dev/null +++ b/events/event_test.go @@ -0,0 +1,28 @@ +package events + +import ( + "testing" + + "github.com/stretchr/testify/assert" + abci "github.com/tendermint/tendermint/abci/types" +) + +func TestEventMarshalling(t *testing.T) { + actualEvent := ABCIEventWithHeight{ + Height: 120, + Event: abci.Event{ + Type: "eventType", + Attributes: []abci.EventAttribute{ + {Key: []byte("key1"), Value: []byte("value1")}, + }, + }, + } + + bz, err := actualEvent.Marshal() + assert.NoError(t, err) + + var unmarshalledEvent ABCIEventWithHeight + + assert.NoError(t, unmarshalledEvent.Unmarshal(bz)) + assert.Equal(t, actualEvent, unmarshalledEvent) +}