Skip to content

Commit

Permalink
feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt committed Dec 2, 2024
1 parent b086719 commit d5302e2
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 33 deletions.
34 changes: 4 additions & 30 deletions server/v2/cometbft/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ package cometbft

import (
"context"
"encoding/json"
"errors"
"fmt"
"maps"
"math"
"slices"
"strings"
"time"

Expand Down Expand Up @@ -153,33 +150,10 @@ func intoABCIEvents(events []event.Event, indexSet map[string]struct{}, indexNon
abciEvents := make([]abci.Event, len(events))
for i, e := range events {
attrs := make([]event.Attribute, 0)

if e.Data != nil {
resp, err := e.Data()
if err != nil {
return nil, fmt.Errorf("failed to marshal event data: %w", err)
}

var attrMap map[string]json.RawMessage
if err := json.Unmarshal(resp, &attrMap); err != nil {
return nil, fmt.Errorf("failed to unmarshal event data: %w", err)
}

// sort the keys to ensure the order is always the same
keys := slices.Sorted(maps.Keys(attrMap))
for _, k := range keys {
v := attrMap[k]
attrs = append(attrs, event.Attribute{
Key: k,
Value: string(v),
})
}
} else {
var err error
attrs, err = e.Attributes()
if err != nil {
return nil, err
}
var err error
attrs, err = e.Attributes()
if err != nil {
return nil, err
}

abciEvents[i] = abci.Event{
Expand Down
57 changes: 54 additions & 3 deletions server/v2/stf/core_event_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"context"
"encoding/json"
"maps"
"slices"

"github.com/cosmos/gogoproto/jsonpb"
gogoproto "github.com/cosmos/gogoproto/proto"
Expand Down Expand Up @@ -37,8 +39,16 @@ type eventManager struct {
// Emit emits an typed event that is defined in the protobuf file.
// In the future these events will be added to consensus.
func (em *eventManager) Emit(tev transaction.Msg) error {
event := event.Event{
ev := event.Event{
Type: gogoproto.MessageName(tev),
Attributes: func() ([]event.Attribute, error) {
outerEvent, err := TypedEventToEvent(tev)
if err != nil {
return nil, err
}

return outerEvent.Attributes()
},
Data: func() (json.RawMessage, error) {
buf := new(bytes.Buffer)
jm := &jsonpb.Marshaler{OrigName: true, EmitDefaults: true, AnyResolver: nil}
Expand All @@ -50,12 +60,53 @@ func (em *eventManager) Emit(tev transaction.Msg) error {
},
}

em.executionContext.events = append(em.executionContext.events, event)
em.executionContext.events = append(em.executionContext.events, ev)
return nil
}

// EmitKV emits a key value pair event.
func (em *eventManager) EmitKV(eventType string, attrs ...event.Attribute) error {
em.executionContext.events = append(em.executionContext.events, event.NewEvent(eventType, attrs...))
ev := event.Event{
Type: eventType,
Attributes: func() ([]event.Attribute, error) {
return attrs, nil
},
Data: func() (json.RawMessage, error) {
return json.Marshal(attrs)
},
}

em.executionContext.events = append(em.executionContext.events, ev)
return nil
}

// TypedEventToEvent takes typed event and converts to Event object
func TypedEventToEvent(tev transaction.Msg) (event.Event, error) {
evtType := gogoproto.MessageName(tev)
buf := new(bytes.Buffer)
jm := &jsonpb.Marshaler{OrigName: true, EmitDefaults: true, AnyResolver: nil}
if err := jm.Marshal(buf, tev); err != nil {
return event.Event{}, err
}

var attrMap map[string]json.RawMessage
if err := json.Unmarshal(buf.Bytes(), &attrMap); err != nil {
return event.Event{}, err
}

// sort the keys to ensure the order is always the same
keys := slices.Sorted(maps.Keys(attrMap))
attrs := make([]event.Attribute, 0, len(attrMap))
for _, k := range keys {
v := attrMap[k]
attrs = append(attrs, event.Attribute{
Key: k,
Value: string(v),
})
}

return event.Event{
Type: evtType,
Attributes: func() ([]event.Attribute, error) { return attrs, nil },
}, nil
}

0 comments on commit d5302e2

Please sign in to comment.