Skip to content

Commit

Permalink
rpc: Store and serve the event transaction ID
Browse files Browse the repository at this point in the history
  • Loading branch information
2opremio committed Jan 30, 2024
1 parent b6671e2 commit bb4d1d3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
10 changes: 8 additions & 2 deletions cmd/soroban-rpc/internal/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type event struct {
txIndex uint32
opIndex uint32
eventIndex uint32
txHash *xdr.Hash // intentionally stored as a pointer to save memory (amortized as soon as there are two events in a transaction)
}

func (e event) cursor(ledgerSeq uint32) Cursor {
Expand Down Expand Up @@ -98,13 +99,15 @@ type Range struct {
ClampEnd bool
}

type ScanFunction func(xdr.DiagnosticEvent, Cursor, int64, *xdr.Hash) bool

// Scan applies f on all the events occurring in the given range.
// The events are processed in sorted ascending Cursor order.
// If f returns false, the scan terminates early (f will not be applied on
// remaining events in the range). Note that a read lock is held for the
// entire duration of the Scan function so f should be written in a way
// to minimize latency.
func (m *MemoryStore) Scan(eventRange Range, f func(xdr.DiagnosticEvent, Cursor, int64) bool) (uint32, error) {
func (m *MemoryStore) Scan(eventRange Range, f ScanFunction) (uint32, error) {
startTime := time.Now()
m.lock.RLock()
defer m.lock.RUnlock()
Expand All @@ -129,7 +132,7 @@ func (m *MemoryStore) Scan(eventRange Range, f func(xdr.DiagnosticEvent, Cursor,
if eventRange.End.Cmp(cur) <= 0 {
return lastLedgerInWindow, nil
}
if !f(event.contents, cur, timestamp) {
if !f(event.contents, cur, timestamp, event.txHash) {
return lastLedgerInWindow, nil
}
}
Expand Down Expand Up @@ -236,10 +239,12 @@ func readEvents(networkPassphrase string, ledgerCloseMeta xdr.LedgerCloseMeta) (
if !tx.Result.Successful() {
continue
}

txEvents, err := tx.GetDiagnosticEvents()
if err != nil {
return nil, err
}
txHash := tx.Result.TransactionHash
for index, e := range txEvents {
events = append(events, event{
contents: e,
Expand All @@ -250,6 +255,7 @@ func readEvents(networkPassphrase string, ledgerCloseMeta xdr.LedgerCloseMeta) (
// can only contain a single Host Function Invocation.
opIndex: 0,
eventIndex: uint32(index),
txHash: &txHash,
})
}
}
Expand Down
12 changes: 8 additions & 4 deletions cmd/soroban-rpc/internal/methods/get_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ type EventInfo struct {
Topic []string `json:"topic"`
Value string `json:"value"`
InSuccessfulContractCall bool `json:"inSuccessfulContractCall"`
TransactionHash string `json:"txHash"`
}

type GetEventsRequest struct {
Expand Down Expand Up @@ -299,7 +300,7 @@ type GetEventsResponse struct {
}

type eventScanner interface {
Scan(eventRange events.Range, f func(xdr.DiagnosticEvent, events.Cursor, int64) bool) (uint32, error)
Scan(eventRange events.Range, f events.ScanFunction) (uint32, error)
}

type eventsRPCHandler struct {
Expand Down Expand Up @@ -334,6 +335,7 @@ func (h eventsRPCHandler) getEvents(request GetEventsRequest) (GetEventsResponse
cursor events.Cursor
ledgerCloseTimestamp int64
event xdr.DiagnosticEvent
txHash *xdr.Hash
}
var found []entry
latestLedger, err := h.scanner.Scan(
Expand All @@ -343,9 +345,9 @@ func (h eventsRPCHandler) getEvents(request GetEventsRequest) (GetEventsResponse
End: events.MaxCursor,
ClampEnd: true,
},
func(event xdr.DiagnosticEvent, cursor events.Cursor, ledgerCloseTimestamp int64) bool {
func(event xdr.DiagnosticEvent, cursor events.Cursor, ledgerCloseTimestamp int64, txHash *xdr.Hash) bool {
if request.Matches(event) {
found = append(found, entry{cursor, ledgerCloseTimestamp, event})
found = append(found, entry{cursor, ledgerCloseTimestamp, event, txHash})
}
return uint(len(found)) < limit
},
Expand All @@ -363,6 +365,7 @@ func (h eventsRPCHandler) getEvents(request GetEventsRequest) (GetEventsResponse
entry.event,
entry.cursor,
time.Unix(entry.ledgerCloseTimestamp, 0).UTC().Format(time.RFC3339),
entry.txHash.HexString(),
)
if err != nil {
return GetEventsResponse{}, errors.Wrap(err, "could not parse event")
Expand All @@ -375,7 +378,7 @@ func (h eventsRPCHandler) getEvents(request GetEventsRequest) (GetEventsResponse
}, nil
}

func eventInfoForEvent(event xdr.DiagnosticEvent, cursor events.Cursor, ledgerClosedAt string) (EventInfo, error) {
func eventInfoForEvent(event xdr.DiagnosticEvent, cursor events.Cursor, ledgerClosedAt string, txHash string) (EventInfo, error) {
v0, ok := event.Event.Body.GetV0()
if !ok {
return EventInfo{}, errors.New("unknown event version")
Expand Down Expand Up @@ -411,6 +414,7 @@ func eventInfoForEvent(event xdr.DiagnosticEvent, cursor events.Cursor, ledgerCl
Topic: topic,
Value: data,
InSuccessfulContractCall: event.InSuccessfulContractCall,
TransactionHash: txHash,
}
if event.Event.ContractId != nil {
info.ContractID = strkey.MustEncode(strkey.VersionByteContract, (*event.Event.ContractId)[:])
Expand Down

0 comments on commit bb4d1d3

Please sign in to comment.