From 0a00590ae21209f090636f6eacd7372d0cca62dc Mon Sep 17 00:00:00 2001 From: Aditya Vyas Date: Wed, 8 May 2024 11:50:02 -0400 Subject: [PATCH] Use transactionInfo struct instead of db.Transactions --- .../internal/methods/get_transactions.go | 51 ++++++++++++++++--- .../internal/methods/get_transactions_test.go | 10 ++-- .../internal/test/get_transactions_test.go | 12 ++--- 3 files changed, 54 insertions(+), 19 deletions(-) diff --git a/cmd/soroban-rpc/internal/methods/get_transactions.go b/cmd/soroban-rpc/internal/methods/get_transactions.go index 7f582699..0904e981 100644 --- a/cmd/soroban-rpc/internal/methods/get_transactions.go +++ b/cmd/soroban-rpc/internal/methods/get_transactions.go @@ -2,6 +2,7 @@ package methods import ( "context" + "encoding/base64" "fmt" "io" @@ -45,14 +46,37 @@ func (req GetTransactionsRequest) isValid(maxLimit uint, ledgerRange ledgerbucke return nil } +type transactionInfo struct { + // Successful indicates whether the transaction was successful or not + Successful bool `json:"status"` + // ApplicationOrder is the index of the transaction among all the transactions + // for that ledger. + ApplicationOrder int32 `json:"applicationOrder,omitempty"` + // FeeBump indicates whether the transaction is a feebump transaction + FeeBump bool `json:"feeBump,omitempty"` + // EnvelopeXdr is the TransactionEnvelope XDR value. + EnvelopeXdr string `json:"envelopeXdr,omitempty"` + // ResultXdr is the TransactionResult XDR value. + ResultXdr string `json:"resultXdr,omitempty"` + // ResultMetaXdr is the TransactionMeta XDR value. + ResultMetaXdr string `json:"resultMetaXdr,omitempty"` + // DiagnosticEventsXDR is present only if transaction was not successful. + // DiagnosticEventsXDR is a base64-encoded slice of xdr.DiagnosticEvent + DiagnosticEventsXDR []string `json:"diagnosticEventsXdr,omitempty"` + // Ledger is the sequence of the ledger which included the transaction. + Ledger uint32 `json:"ledger,omitempty"` + // LedgerCloseTime is the unix timestamp of when the transaction was included in the ledger. + LedgerCloseTime int64 `json:"createdAt,string,omitempty"` +} + // GetTransactionsResponse encapsulates the response structure for getTransactions queries. type GetTransactionsResponse struct { - Transactions []db.Transaction `json:"transactions"` - LatestLedger uint32 `json:"latestLedger"` - LatestLedgerCloseTime int64 `json:"latestLedgerCloseTimestamp"` - OldestLedger uint32 `json:"oldestLedger"` - OldestLedgerCloseTime int64 `json:"oldestLedgerCloseTimestamp"` - Cursor string `json:"cursor"` + Transactions []transactionInfo `json:"transactions"` + LatestLedger uint32 `json:"latestLedger"` + LatestLedgerCloseTime int64 `json:"latestLedgerCloseTimestamp"` + OldestLedger uint32 `json:"oldestLedger"` + OldestLedgerCloseTime int64 `json:"oldestLedgerCloseTimestamp"` + Cursor string `json:"cursor"` } type transactionsRPCHandler struct { @@ -100,7 +124,7 @@ func (h transactionsRPCHandler) getTransactionsByLedgerSequence(ctx context.Cont // Iterate through each ledger and its transactions until limit or end range is reached. // The latest ledger acts as the end ledger range for the request. - var txns []db.Transaction + var txns []transactionInfo var cursor *toid.ID LedgerLoop: for ledgerSeq := start.LedgerSequence; ledgerSeq <= int32(ledgerRange.LastLedger.Sequence); ledgerSeq++ { @@ -162,7 +186,18 @@ LedgerLoop: } } - txns = append(txns, tx) + txInfo := transactionInfo{ + Successful: tx.Successful, + ApplicationOrder: tx.ApplicationOrder, + FeeBump: tx.FeeBump, + ResultXdr: base64.StdEncoding.EncodeToString(tx.Result), + ResultMetaXdr: base64.StdEncoding.EncodeToString(tx.Meta), + EnvelopeXdr: base64.StdEncoding.EncodeToString(tx.Envelope), + DiagnosticEventsXDR: base64EncodeSlice(tx.Events), + Ledger: tx.Ledger.Sequence, + LedgerCloseTime: tx.Ledger.CloseTime, + } + txns = append(txns, txInfo) if len(txns) >= int(limit) { break LedgerLoop } diff --git a/cmd/soroban-rpc/internal/methods/get_transactions_test.go b/cmd/soroban-rpc/internal/methods/get_transactions_test.go index 186b7be9..9fb29764 100644 --- a/cmd/soroban-rpc/internal/methods/get_transactions_test.go +++ b/cmd/soroban-rpc/internal/methods/get_transactions_test.go @@ -139,8 +139,8 @@ func TestGetTransactions_CustomLimit(t *testing.T) { // assert transactions result assert.Equal(t, len(response.Transactions), 2) - assert.Equal(t, response.Transactions[0].Ledger.Sequence, uint32(1)) - assert.Equal(t, response.Transactions[1].Ledger.Sequence, uint32(1)) + assert.Equal(t, response.Transactions[0].Ledger, uint32(1)) + assert.Equal(t, response.Transactions[1].Ledger, uint32(1)) } func TestGetTransactions_CustomLimitAndCursor(t *testing.T) { @@ -183,9 +183,9 @@ func TestGetTransactions_CustomLimitAndCursor(t *testing.T) { // assert transactions result assert.Equal(t, len(response.Transactions), 3) - assert.Equal(t, response.Transactions[0].Ledger.Sequence, uint32(2)) - assert.Equal(t, response.Transactions[1].Ledger.Sequence, uint32(2)) - assert.Equal(t, response.Transactions[2].Ledger.Sequence, uint32(3)) + assert.Equal(t, response.Transactions[0].Ledger, uint32(2)) + assert.Equal(t, response.Transactions[1].Ledger, uint32(2)) + assert.Equal(t, response.Transactions[2].Ledger, uint32(3)) } func TestGetTransactions_InvalidStartLedger(t *testing.T) { diff --git a/cmd/soroban-rpc/internal/test/get_transactions_test.go b/cmd/soroban-rpc/internal/test/get_transactions_test.go index c73b4910..90ce2b8f 100644 --- a/cmd/soroban-rpc/internal/test/get_transactions_test.go +++ b/cmd/soroban-rpc/internal/test/get_transactions_test.go @@ -109,9 +109,9 @@ func TestGetTransactions(t *testing.T) { err := client.CallResult(context.Background(), "getTransactions", request, &result) assert.NoError(t, err) assert.Equal(t, len(result.Transactions), 3) - assert.Equal(t, result.Transactions[0].Ledger.Sequence, ledgers[0]) - assert.Equal(t, result.Transactions[1].Ledger.Sequence, ledgers[1]) - assert.Equal(t, result.Transactions[2].Ledger.Sequence, ledgers[2]) + assert.Equal(t, result.Transactions[0].Ledger, ledgers[0]) + assert.Equal(t, result.Transactions[1].Ledger, ledgers[1]) + assert.Equal(t, result.Transactions[2].Ledger, ledgers[2]) // Get transactions with limit request = methods.GetTransactionsRequest{ @@ -123,7 +123,7 @@ func TestGetTransactions(t *testing.T) { err = client.CallResult(context.Background(), "getTransactions", request, &result) assert.NoError(t, err) assert.Equal(t, len(result.Transactions), 1) - assert.Equal(t, result.Transactions[0].Ledger.Sequence, ledgers[0]) + assert.Equal(t, result.Transactions[0].Ledger, ledgers[0]) // Get transactions using previous result's cursor cursor := toid.Parse(Cursor) @@ -136,6 +136,6 @@ func TestGetTransactions(t *testing.T) { err = client.CallResult(context.Background(), "getTransactions", request, &result) assert.NoError(t, err) assert.Equal(t, len(result.Transactions), 2) - assert.Equal(t, result.Transactions[0].Ledger.Sequence, ledgers[1]) - assert.Equal(t, result.Transactions[1].Ledger.Sequence, ledgers[2]) + assert.Equal(t, result.Transactions[0].Ledger, ledgers[1]) + assert.Equal(t, result.Transactions[1].Ledger, ledgers[2]) }