Skip to content

Commit

Permalink
Merge pull request stellar#5198 from stellar/update-getDiagnosticEvents
Browse files Browse the repository at this point in the history
Refactor GetDiagnosticEvents so that it can be used directly from TransactionMeta
  • Loading branch information
psheth9 authored Feb 6, 2024
2 parents 4a57a2a + 794f7f2 commit d16cd8a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 38 deletions.
39 changes: 1 addition & 38 deletions ingest/ledger_transaction.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package ingest

import (
"fmt"

"github.com/stellar/go/support/errors"
"github.com/stellar/go/xdr"
)
Expand Down Expand Up @@ -155,40 +153,5 @@ func operationChanges(ops []xdr.OperationMeta, index uint32) []Change {

// GetDiagnosticEvents returns all contract events emitted by a given operation.
func (t *LedgerTransaction) GetDiagnosticEvents() ([]xdr.DiagnosticEvent, error) {
switch t.UnsafeMeta.V {
case 1:
return nil, nil
case 2:
return nil, nil
case 3:
var diagnosticEvents []xdr.DiagnosticEvent
var contractEvents []xdr.ContractEvent
if sorobanMeta := t.UnsafeMeta.MustV3().SorobanMeta; sorobanMeta != nil {
diagnosticEvents = sorobanMeta.DiagnosticEvents
if len(diagnosticEvents) > 0 {
// all contract events and diag events for a single operation(by it's index in the tx) were available
// in tx meta's DiagnosticEvents, no need to look anywhere else for events
return diagnosticEvents, nil
}

contractEvents = sorobanMeta.Events
if len(contractEvents) == 0 {
// no events were present in this tx meta
return nil, nil
}
}

// tx meta only provided contract events, no diagnostic events, we convert the contract
// event to a diagnostic event, to fit the response interface.
convertedDiagnosticEvents := make([]xdr.DiagnosticEvent, len(contractEvents))
for i, event := range contractEvents {
convertedDiagnosticEvents[i] = xdr.DiagnosticEvent{
InSuccessfulContractCall: true,
Event: event,
}
}
return convertedDiagnosticEvents, nil
default:
return nil, fmt.Errorf("unsupported TransactionMeta version: %v", t.UnsafeMeta.V)
}
return t.UnsafeMeta.GetDiagnosticEvents()
}
44 changes: 44 additions & 0 deletions xdr/transaction_meta.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package xdr

import (
"fmt"
)

// Operations is a helper on TransactionMeta that returns operations
// meta from `TransactionMeta.Operations` or `TransactionMeta.V1.Operations`.
func (transactionMeta *TransactionMeta) OperationsMeta() []OperationMeta {
Expand All @@ -16,3 +20,43 @@ func (transactionMeta *TransactionMeta) OperationsMeta() []OperationMeta {
panic("Unsupported TransactionMeta version")
}
}

// GetDiagnosticEvents returns all contract events emitted by a given operation.
func (t *TransactionMeta) GetDiagnosticEvents() ([]DiagnosticEvent, error) {
switch t.V {
case 1:
return nil, nil
case 2:
return nil, nil
case 3:
var diagnosticEvents []DiagnosticEvent
var contractEvents []ContractEvent
if sorobanMeta := t.MustV3().SorobanMeta; sorobanMeta != nil {
diagnosticEvents = sorobanMeta.DiagnosticEvents
if len(diagnosticEvents) > 0 {
// all contract events and diag events for a single operation(by its index in the tx) were available
// in tx meta's DiagnosticEvents, no need to look anywhere else for events
return diagnosticEvents, nil
}

contractEvents = sorobanMeta.Events
if len(contractEvents) == 0 {
// no events were present in this tx meta
return nil, nil
}
}

// tx meta only provided contract events, no diagnostic events, we convert the contract
// event to a diagnostic event, to fit the response interface.
convertedDiagnosticEvents := make([]DiagnosticEvent, len(contractEvents))
for i, event := range contractEvents {
convertedDiagnosticEvents[i] = DiagnosticEvent{
InSuccessfulContractCall: true,
Event: event,
}
}
return convertedDiagnosticEvents, nil
default:
return nil, fmt.Errorf("unsupported TransactionMeta version: %v", t.V)
}
}

0 comments on commit d16cd8a

Please sign in to comment.