Skip to content

Commit

Permalink
Use reader.Seek()
Browse files Browse the repository at this point in the history
  • Loading branch information
aditya1702 committed Apr 16, 2024
1 parent f509457 commit f813d01
Showing 1 changed file with 22 additions and 26 deletions.
48 changes: 22 additions & 26 deletions cmd/soroban-rpc/internal/methods/get_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ package methods

import (
"context"
"encoding/hex"

"github.com/creachadair/jrpc2"
"github.com/creachadair/jrpc2/handler"
"github.com/stellar/go/network"
"github.com/stellar/go/ingest"
"github.com/stellar/go/support/errors"
"github.com/stellar/go/support/log"
"github.com/stellar/go/xdr"

"github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/db"
"github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/transactions"
Expand Down Expand Up @@ -113,56 +111,54 @@ LedgerLoop:
}
}

// Build transaction envelopes in the ledger
byHash := map[xdr.Hash]xdr.TransactionEnvelope{}
for _, tx := range ledger.TransactionEnvelopes() {
hash, err := network.HashTransactionInEnvelope(tx, h.networkPassphrase)
if err != nil {
return GetTransactionsResponse{}, &jrpc2.Error{
Code: jrpc2.InvalidParams,
Message: err.Error(),
}
// Initialise tx reader and move it to start idx.
reader, err := ingest.NewLedgerTransactionReaderFromLedgerCloseMeta(h.networkPassphrase, ledger)
if err != nil {
return GetTransactionsResponse{}, &jrpc2.Error{
Code: jrpc2.InvalidParams,
Message: err.Error(),
}
}
err = reader.Seek(int(start.TxIdx))
if err != nil {
return GetTransactionsResponse{}, &jrpc2.Error{
Code: jrpc2.InvalidParams,
Message: err.Error(),
}
byHash[hash] = tx
}

// Decode transaction info from ledger meta
txCount := ledger.CountTransactions()
for i := start.TxIdx; i < uint32(txCount); i++ {
cursor = transactions.NewCursor(ledger.LedgerSequence(), i, 0)

hash := ledger.TransactionHash(int(i))
envelope, ok := byHash[hash]
if !ok {
hexHash := hex.EncodeToString(hash[:])
err = errors.Errorf("unknown tx hash in LedgerCloseMeta: %v", hexHash)
tx, err := reader.Read()
if err != nil {
return GetTransactionsResponse{}, &jrpc2.Error{
Code: jrpc2.InvalidParams,
Message: err.Error(),
}
}
txResult := ledger.TransactionResultPair(int(i))
unsafeMeta := ledger.TxApplyProcessing(int(i))

txInfo := TransactionInfo{
FeeBump: envelope.IsFeeBump(),
ApplicationOrder: int32(i + 1),
Successful: txResult.Result.Successful(),
FeeBump: tx.Envelope.IsFeeBump(),
ApplicationOrder: int32(tx.Index),
Successful: tx.Result.Result.Successful(),
LedgerSequence: uint(ledger.LedgerSequence()),
}
if txInfo.Result, err = txResult.Result.MarshalBinary(); err != nil {
if txInfo.Result, err = tx.Result.Result.MarshalBinary(); err != nil {
return GetTransactionsResponse{}, &jrpc2.Error{
Code: jrpc2.InvalidParams,
Message: err.Error(),
}
}
if txInfo.Meta, err = unsafeMeta.MarshalBinary(); err != nil {
if txInfo.Meta, err = tx.UnsafeMeta.MarshalBinary(); err != nil {
return GetTransactionsResponse{}, &jrpc2.Error{
Code: jrpc2.InvalidParams,
Message: err.Error(),
}
}
if txInfo.Envelope, err = envelope.MarshalBinary(); err != nil {
if txInfo.Envelope, err = tx.Envelope.MarshalBinary(); err != nil {
return GetTransactionsResponse{}, &jrpc2.Error{
Code: jrpc2.InvalidParams,
Message: err.Error(),
Expand Down

0 comments on commit f813d01

Please sign in to comment.