Skip to content

Commit

Permalink
Fix linter - 1
Browse files Browse the repository at this point in the history
  • Loading branch information
aditya1702 committed Dec 13, 2024
1 parent e4ca0fd commit 1507d34
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 30 deletions.
51 changes: 22 additions & 29 deletions cmd/stellar-rpc/internal/db/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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).
Expand Down
2 changes: 1 addition & 1 deletion cmd/stellar-rpc/internal/methods/get_transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down

0 comments on commit 1507d34

Please sign in to comment.