From 0690766480ac46e9925fc064839a78a27ed7de09 Mon Sep 17 00:00:00 2001 From: pritsheth Date: Mon, 5 Feb 2024 14:33:08 -0800 Subject: [PATCH 1/2] Refactor GetDiagnosticEvents --- ingest/ledger_transaction.go | 39 +------------------------------- xdr/transaction_meta.go | 44 ++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 38 deletions(-) diff --git a/ingest/ledger_transaction.go b/ingest/ledger_transaction.go index 56f5af6070..77ca777206 100644 --- a/ingest/ledger_transaction.go +++ b/ingest/ledger_transaction.go @@ -1,8 +1,6 @@ package ingest import ( - "fmt" - "github.com/stellar/go/support/errors" "github.com/stellar/go/xdr" ) @@ -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() } diff --git a/xdr/transaction_meta.go b/xdr/transaction_meta.go index 3fee38ae93..327d19be72 100644 --- a/xdr/transaction_meta.go +++ b/xdr/transaction_meta.go @@ -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 { @@ -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 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([]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) + } +} From 794f7f2c68b564a20640bc27ba37795a752e728e Mon Sep 17 00:00:00 2001 From: pritsheth Date: Mon, 5 Feb 2024 15:26:29 -0800 Subject: [PATCH 2/2] Fix typo --- xdr/transaction_meta.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xdr/transaction_meta.go b/xdr/transaction_meta.go index 327d19be72..cd8443551a 100644 --- a/xdr/transaction_meta.go +++ b/xdr/transaction_meta.go @@ -34,7 +34,7 @@ func (t *TransactionMeta) GetDiagnosticEvents() ([]DiagnosticEvent, error) { 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 it's index in the tx) were available + // 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 }