Skip to content

Commit

Permalink
Use transactionInfo struct instead of db.Transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
aditya1702 committed May 8, 2024
1 parent 31c80f7 commit 0a00590
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 19 deletions.
51 changes: 43 additions & 8 deletions cmd/soroban-rpc/internal/methods/get_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package methods

import (
"context"
"encoding/base64"
"fmt"
"io"

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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++ {
Expand Down Expand Up @@ -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
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/soroban-rpc/internal/methods/get_transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
12 changes: 6 additions & 6 deletions cmd/soroban-rpc/internal/test/get_transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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)
Expand All @@ -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])
}

0 comments on commit 0a00590

Please sign in to comment.