Skip to content

Commit

Permalink
soroban-rpc: Add filtering support for diagnostic events (stellar#530)
Browse files Browse the repository at this point in the history
  • Loading branch information
2opremio authored Mar 24, 2023
1 parent eda3bcb commit a11c037
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 20 deletions.
30 changes: 13 additions & 17 deletions cmd/soroban-rpc/internal/methods/get_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ func (g *GetEventsRequest) Matches(event xdr.ContractEvent) bool {

const EventTypeSystem = "system"
const EventTypeContract = "contract"
const EventTypeDiagnostic = "diagnostic"

var eventTypeFromXDR = map[xdr.ContractEventType]string{
xdr.ContractEventTypeSystem: EventTypeSystem,
xdr.ContractEventTypeContract: EventTypeContract,
xdr.ContractEventTypeDiagnostic: EventTypeDiagnostic,
}

type EventFilter struct {
EventType string `json:"type,omitempty"`
Expand All @@ -88,10 +95,10 @@ type EventFilter struct {

func (e *EventFilter) Valid() error {
switch e.EventType {
case "", EventTypeSystem, EventTypeContract:
case "", EventTypeSystem, EventTypeContract, EventTypeDiagnostic:
// ok
default:
return errors.New("if set, type must be either 'system' or 'contract'")
return errors.New("if set, type must be either 'system', 'contract' or 'diagnostic'")
}
if len(e.ContractIDs) > 5 {
return errors.New("maximum 5 contract IDs per filter")
Expand All @@ -118,13 +125,7 @@ func (e *EventFilter) Matches(event xdr.ContractEvent) bool {
}

func (e *EventFilter) matchesEventType(event xdr.ContractEvent) bool {
if e.EventType == EventTypeContract && event.Type != xdr.ContractEventTypeContract {
return false
}
if e.EventType == EventTypeSystem && event.Type != xdr.ContractEventTypeSystem {
return false
}
return true
return e.EventType == "" || e.EventType == eventTypeFromXDR[event.Type]
}

func (e *EventFilter) matchesContractIDs(event xdr.ContractEvent) bool {
Expand Down Expand Up @@ -341,14 +342,9 @@ func eventInfoForEvent(event xdr.ContractEvent, cursor events.Cursor, ledgerClos
return EventInfo{}, errors.New("unknown event version")
}

var eventType string
switch event.Type {
case xdr.ContractEventTypeSystem:
eventType = EventTypeSystem
case xdr.ContractEventTypeContract:
eventType = EventTypeContract
default:
return EventInfo{}, errors.New("unknown event type")
eventType, ok := eventTypeFromXDR[event.Type]
if !ok {
return EventInfo{}, fmt.Errorf("unknown XDR ContractEventType type: %d", event.Type)
}

// base64-xdr encode the topic
Expand Down
27 changes: 24 additions & 3 deletions cmd/soroban-rpc/internal/methods/get_events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ func TestGetEventsRequestValid(t *testing.T) {
{EventType: "foo"},
},
Pagination: nil,
}).Valid(1000), "filter 1 invalid: if set, type must be either 'system' or 'contract'")
}).Valid(1000), "filter 1 invalid: if set, type must be either 'system', 'contract' or 'diagnostic'")

assert.EqualError(t, (&GetEventsRequest{
StartLedger: 1,
Expand Down Expand Up @@ -684,6 +684,13 @@ func TestGetEvents(t *testing.T) {
},
xdr.ScVal{Type: xdr.ScValTypeScvSymbol, Sym: &counter},
),
diagnosticEvent(
contractID,
xdr.ScVec{
xdr.ScVal{Type: xdr.ScValTypeScvSymbol, Sym: &counter},
},
xdr.ScVal{Type: xdr.ScValTypeScvSymbol, Sym: &counter},
),
}),
}
assert.NoError(t, store.IngestEvents(ledgerCloseMetaWithEvents(1, now.Unix(), txMeta...)))
Expand Down Expand Up @@ -978,7 +985,7 @@ func contractEvent(contractID xdr.Hash, topic []xdr.ScVal, body xdr.ScVal) xdr.C
Body: xdr.ContractEventBody{
V: 0,
V0: &xdr.ContractEventV0{
Topics: xdr.ScVec(topic),
Topics: topic,
Data: body,
},
},
Expand All @@ -992,7 +999,21 @@ func systemEvent(contractID xdr.Hash, topic []xdr.ScVal, body xdr.ScVal) xdr.Con
Body: xdr.ContractEventBody{
V: 0,
V0: &xdr.ContractEventV0{
Topics: xdr.ScVec(topic),
Topics: topic,
Data: body,
},
},
}
}

func diagnosticEvent(contractID xdr.Hash, topic []xdr.ScVal, body xdr.ScVal) xdr.ContractEvent {
return xdr.ContractEvent{
ContractId: &contractID,
Type: xdr.ContractEventTypeDiagnostic,
Body: xdr.ContractEventBody{
V: 0,
V0: &xdr.ContractEventV0{
Topics: topic,
Data: body,
},
},
Expand Down

0 comments on commit a11c037

Please sign in to comment.