Skip to content

Commit

Permalink
Refactor to use new transaction db
Browse files Browse the repository at this point in the history
  • Loading branch information
aditya1702 committed May 3, 2024
1 parent 74e2941 commit ca6c4ca
Show file tree
Hide file tree
Showing 8 changed files with 251 additions and 409 deletions.
26 changes: 26 additions & 0 deletions cmd/soroban-rpc/internal/db/mock_ledger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package db

import (
"context"

"github.com/stellar/go/xdr"
)

type mockLedgerReader struct {
txn mockTransactionHandler
}

func NewMockLedgerReader(txn *mockTransactionHandler) *mockLedgerReader {
return &mockLedgerReader{
txn: *txn,
}
}

func (m *mockLedgerReader) GetLedger(ctx context.Context, sequence uint32) (xdr.LedgerCloseMeta, bool, error) {
lcm := m.txn.ledgerSeqToMeta[sequence]
return *lcm, true, nil
}

func (m *mockLedgerReader) StreamAllLedgers(ctx context.Context, f StreamLedgerFn) error {
return nil
}
20 changes: 12 additions & 8 deletions cmd/soroban-rpc/internal/db/mock_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,24 @@ import (
type mockTransactionHandler struct {
passphrase string

ledgerRange ledgerbucketwindow.LedgerRange
txs map[string]ingest.LedgerTransaction
ledgers map[string]*xdr.LedgerCloseMeta
ledgerRange ledgerbucketwindow.LedgerRange
txs map[string]ingest.LedgerTransaction
txHashToMeta map[string]*xdr.LedgerCloseMeta
ledgerSeqToMeta map[uint32]*xdr.LedgerCloseMeta
}

func NewMockTransactionStore(passphrase string) *mockTransactionHandler {
return &mockTransactionHandler{
passphrase: passphrase,
txs: make(map[string]ingest.LedgerTransaction),
ledgers: make(map[string]*xdr.LedgerCloseMeta),
passphrase: passphrase,
txs: make(map[string]ingest.LedgerTransaction),
txHashToMeta: make(map[string]*xdr.LedgerCloseMeta),
ledgerSeqToMeta: make(map[uint32]*xdr.LedgerCloseMeta),
}
}

func (txn *mockTransactionHandler) InsertTransactions(lcm xdr.LedgerCloseMeta) error {
txn.ledgerSeqToMeta[lcm.LedgerSequence()] = &lcm

reader, err := ingest.NewLedgerTransactionReaderFromLedgerCloseMeta(txn.passphrase, lcm)
if err != nil {
return err
Expand All @@ -44,7 +48,7 @@ func (txn *mockTransactionHandler) InsertTransactions(lcm xdr.LedgerCloseMeta) e

h := tx.Result.TransactionHash.HexString()
txn.txs[h] = tx
txn.ledgers[h] = &lcm
txn.txHashToMeta[h] = &lcm
}

if lcmSeq := lcm.LedgerSequence(); lcmSeq < txn.ledgerRange.FirstLedger.Sequence ||
Expand Down Expand Up @@ -72,7 +76,7 @@ func (txn *mockTransactionHandler) GetTransaction(ctx context.Context, hash xdr.
if tx, ok := txn.txs[hash.HexString()]; !ok {
return Transaction{}, txn.ledgerRange, ErrNoTransaction
} else {
itx, err := ParseTransaction(*txn.ledgers[hash.HexString()], tx)
itx, err := ParseTransaction(*txn.txHashToMeta[hash.HexString()], tx)
return itx, txn.ledgerRange, err
}
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/soroban-rpc/internal/jsonrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func NewJSONRPCHandler(cfg *config.Config, params HandlerParams) Handler {
},
{
methodName: "getTransactions",
underlyingHandler: methods.NewGetTransactionsHandler(params.Logger, params.LedgerReader, params.LedgerEntryReader, cfg.MaxTransactionsLimit, cfg.DefaultTransactionsLimit, cfg.NetworkPassphrase),
underlyingHandler: methods.NewGetTransactionsHandler(params.Logger, params.LedgerReader, params.TransactionReader, cfg.MaxTransactionsLimit, cfg.DefaultTransactionsLimit, cfg.NetworkPassphrase),
longName: "get_transactions",
queueLimit: cfg.RequestBacklogSendTransactionQueueLimit,
requestDurationLimit: cfg.MaxSendTransactionExecutionDuration,
Expand Down
63 changes: 19 additions & 44 deletions cmd/soroban-rpc/internal/methods/get_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,17 @@ type transactionInfo struct {

// GetTransactionsResponse encapsulates the response structure for getTransactions queries.
type GetTransactionsResponse struct {
Transactions []transactionInfo `json:"transactions"`
LatestLedger int64 `json:"latestLedger"`
LatestLedgerCloseTimestamp int64 `json:"latestLedgerCloseTimestamp"`
OldestLedger int64 `json:"oldestLedger"`
OldestLedgerCloseTimestamp 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 {
ledgerReader db.LedgerReader
ledgerEntryReader db.LedgerEntryReader
dbReader db.TransactionReader
maxLimit uint
defaultLimit uint
logger *log.Entry
Expand Down Expand Up @@ -101,20 +101,20 @@ func (h transactionsRPCHandler) getTransactionsByLedgerSequence(ctx context.Cont
}
}

// The latest ledger acts as the end ledger range for the request.
latestLedgerSequence, _, err := h.getLatestLedgerDetails(ctx)
ledgerRange, err := h.dbReader.GetLedgerRange(ctx)
if err != nil {
return GetTransactionsResponse{}, &jrpc2.Error{
Code: jrpc2.InvalidParams,
Message: err.Error(),
}
}

// Iterate through each ledger and its transactions until limit or end range is reached
// 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 []transactionInfo
var cursor *toid.ID
LedgerLoop:
for ledgerSeq := start.LedgerSequence; ledgerSeq <= int32(latestLedgerSequence); ledgerSeq++ {
for ledgerSeq := start.LedgerSequence; ledgerSeq <= int32(ledgerRange.LastLedger.Sequence); ledgerSeq++ {
// Get ledger close meta from db
ledger, found, err := h.ledgerReader.GetLedger(ctx, uint32(ledgerSeq))
if (err != nil) || (!found) {
Expand Down Expand Up @@ -190,21 +190,14 @@ LedgerLoop:
}
}

latestLedgerSequence, latestLedgerCloseTime, err := h.getLatestLedgerDetails(ctx)
if err != nil {
return GetTransactionsResponse{}, &jrpc2.Error{
Code: jrpc2.InvalidParams,
Message: err.Error(),
}
}

return GetTransactionsResponse{
Transactions: txns,
LatestLedger: latestLedgerSequence,
LatestLedgerCloseTimestamp: latestLedgerCloseTime,
Cursor: cursor.String(),
Transactions: txns,
LatestLedger: ledgerRange.LastLedger.Sequence,
LatestLedgerCloseTime: ledgerRange.LastLedger.CloseTime,
OldestLedger: ledgerRange.FirstLedger.Sequence,
OldestLedgerCloseTime: ledgerRange.FirstLedger.CloseTime,
Cursor: cursor.String(),
}, nil

}

// getTransactionDetails fetches XDR for the following transaction details - result, meta and envelope.
Expand All @@ -225,28 +218,10 @@ func (h transactionsRPCHandler) getTransactionDetails(tx ingest.LedgerTransactio
return txResult, txMeta, txEnvelope, nil
}

// getLatestLedgerDetails fetches the latest ledger sequence and close time.
func (h transactionsRPCHandler) getLatestLedgerDetails(ctx context.Context) (sequence int64, ledgerCloseTime int64, err error) {
latestSequence, err := h.ledgerEntryReader.GetLatestLedgerSequence(ctx)
if err != nil {
return 0, 0, err
}

latestLedger, found, err := h.ledgerReader.GetLedger(ctx, latestSequence)
if (err != nil) || (!found) {
if err == nil {
err = errors.Errorf("ledger close meta not found: %d", latestSequence)
}
return 0, 0, err
}

return int64(latestSequence), latestLedger.LedgerCloseTime(), nil
}

func NewGetTransactionsHandler(logger *log.Entry, ledgerReader db.LedgerReader, ledgerEntryReader db.LedgerEntryReader, maxLimit, defaultLimit uint, networkPassphrase string) jrpc2.Handler {
func NewGetTransactionsHandler(logger *log.Entry, ledgerReader db.LedgerReader, dbReader db.TransactionReader, maxLimit, defaultLimit uint, networkPassphrase string) jrpc2.Handler {
transactionsHandler := transactionsRPCHandler{
ledgerReader: ledgerReader,
ledgerEntryReader: ledgerEntryReader,
dbReader: dbReader,
maxLimit: maxLimit,
defaultLimit: defaultLimit,
logger: logger,
Expand Down
Loading

0 comments on commit ca6c4ca

Please sign in to comment.