From 1507d349dd9533d2b00bb77a10fb7a0e1df976bd Mon Sep 17 00:00:00 2001 From: Aditya Vyas Date: Fri, 13 Dec 2024 23:05:07 +0530 Subject: [PATCH] Fix linter - 1 --- cmd/stellar-rpc/internal/db/ledger.go | 51 ++++++++----------- .../internal/methods/get_transactions_test.go | 2 +- 2 files changed, 23 insertions(+), 30 deletions(-) diff --git a/cmd/stellar-rpc/internal/db/ledger.go b/cmd/stellar-rpc/internal/db/ledger.go index b9b157d8..7dd03611 100644 --- a/cmd/stellar-rpc/internal/db/ledger.go +++ b/cmd/stellar-rpc/internal/db/ledger.go @@ -78,22 +78,9 @@ func (l ledgerReaderTx) BatchGetLedgers(ctx context.Context, sequence uint32, return results, nil } -// GetLedger fetches a single ledger from the db. +// GetLedger fetches a single ledger from the db using a transaction. func (l ledgerReaderTx) GetLedger(ctx context.Context, sequence uint32) (xdr.LedgerCloseMeta, bool, error) { - sql := sq.Select("meta").From(ledgerCloseMetaTableName).Where(sq.Eq{"sequence": sequence}) - var results []xdr.LedgerCloseMeta - if err := l.tx.Select(ctx, &results, sql); err != nil { - return xdr.LedgerCloseMeta{}, false, err - } - switch len(results) { - case 0: - return xdr.LedgerCloseMeta{}, false, nil - case 1: - return results[0], true, nil - default: - return xdr.LedgerCloseMeta{}, false, fmt.Errorf("multiple lcm entries (%d) for sequence %d in table %q", - len(results), sequence, ledgerCloseMetaTableName) - } + return getLedgerFromDB(ctx, l.tx, sequence) } func (l ledgerReaderTx) Done() error { @@ -170,20 +157,7 @@ func (r ledgerReader) StreamLedgerRange( // GetLedger fetches a single ledger from the db. func (r ledgerReader) GetLedger(ctx context.Context, sequence uint32) (xdr.LedgerCloseMeta, bool, error) { - sql := sq.Select("meta").From(ledgerCloseMetaTableName).Where(sq.Eq{"sequence": sequence}) - var results []xdr.LedgerCloseMeta - if err := r.db.Select(ctx, &results, sql); err != nil { - return xdr.LedgerCloseMeta{}, false, err - } - switch len(results) { - case 0: - return xdr.LedgerCloseMeta{}, false, nil - case 1: - return results[0], true, nil - default: - return xdr.LedgerCloseMeta{}, false, fmt.Errorf("multiple lcm entries (%d) for sequence %d in table %q", - len(results), sequence, ledgerCloseMetaTableName) - } + return getLedgerFromDB(ctx, r.db, sequence) } // GetLedgerRange pulls the min/max ledger sequence numbers from the meta table. @@ -279,6 +253,25 @@ func (l ledgerWriter) trimLedgers(latestLedgerSeq uint32, retentionWindow uint32 return err } +// getLedgerFromDB is a helper function that encapsulates the common logic +// for fetching a single ledger from the database +func getLedgerFromDB(ctx context.Context, db readDB, sequence uint32) (xdr.LedgerCloseMeta, bool, error) { + sql := sq.Select("meta").From(ledgerCloseMetaTableName).Where(sq.Eq{"sequence": sequence}) + var results []xdr.LedgerCloseMeta + if err := db.Select(ctx, &results, sql); err != nil { + return xdr.LedgerCloseMeta{}, false, err + } + switch len(results) { + case 0: + return xdr.LedgerCloseMeta{}, false, nil + case 1: + return results[0], true, nil + default: + return xdr.LedgerCloseMeta{}, false, fmt.Errorf("multiple lcm entries (%d) for sequence %d in table %q", + len(results), sequence, ledgerCloseMetaTableName) + } +} + // InsertLedger inserts a ledger in the db. func (l ledgerWriter) InsertLedger(ledger xdr.LedgerCloseMeta) error { _, err := sq.StatementBuilder.RunWith(l.stmtCache). diff --git a/cmd/stellar-rpc/internal/methods/get_transactions_test.go b/cmd/stellar-rpc/internal/methods/get_transactions_test.go index a240168e..636bb7a8 100644 --- a/cmd/stellar-rpc/internal/methods/get_transactions_test.go +++ b/cmd/stellar-rpc/internal/methods/get_transactions_test.go @@ -7,10 +7,10 @@ import ( "testing" "github.com/creachadair/jrpc2" - "github.com/stellar/go/support/log" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + "github.com/stellar/go/support/log" "github.com/stellar/go/toid" "github.com/stellar/go/xdr"