Skip to content

Commit

Permalink
feat: add marshalling functionality to ABCIEventWithHeight (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
cgorenflo authored Jul 4, 2023
1 parent 2c843b8 commit 3cf9108
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
39 changes: 39 additions & 0 deletions events/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
28 changes: 28 additions & 0 deletions events/event_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 3cf9108

Please sign in to comment.