Skip to content

Commit

Permalink
Fix lint issues part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
psheth9 committed Jun 20, 2024
1 parent a3f8987 commit 1a03730
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 29 deletions.
2 changes: 1 addition & 1 deletion cmd/soroban-rpc/internal/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func MustNew(cfg *config.Config, logger *supportlog.Entry) *Daemon {
logger.WithError(err).Error("could not run ingestion. Retrying")
}

// Take the larger of (event retention, tx retention) and then the smaller
// Take the largest of (event retention, tx retention) and then the smallest
// of (tx retention, default event retention) if event retention wasn't
// specified, for some reason...?
maxRetentionWindow := ordered.Max(cfg.EventLedgerRetentionWindow, cfg.TransactionLedgerRetentionWindow)
Expand Down
33 changes: 18 additions & 15 deletions cmd/soroban-rpc/internal/db/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package db
import (
"context"
"fmt"
"io"
"time"

sq "github.com/Masterminds/squirrel"
"github.com/prometheus/client_golang/prometheus"
"github.com/stellar/go/ingest"
Expand All @@ -11,8 +14,6 @@ import (
"github.com/stellar/go/support/log"
"github.com/stellar/go/xdr"
"github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/events"
"io"
"time"
)

const eventTableName = "events"
Expand All @@ -25,7 +26,7 @@ type EventWriter interface {
// EventReader has all the public methods to fetch events from DB
type EventReader interface {
GetEvents(ctx context.Context, cursorRange events.CursorRange, contractIds []string, f ScanFunction) error

Check failure on line 28 in cmd/soroban-rpc/internal/db/event.go

View workflow job for this annotation

GitHub Actions / golangci-lint

var-naming: interface method parameter contractIds should be contractIDs (revive)
//GetLedgerRange(ctx context.Context) error
// GetLedgerRange(ctx context.Context) error
}

type eventHandler struct {
Expand All @@ -44,17 +45,17 @@ func (eventHandler *eventHandler) InsertEvents(lcm xdr.LedgerCloseMeta) error {
txCount := lcm.CountTransactions()

if eventHandler.stmtCache == nil {
return errors.New("EventWriter incorrectly initialized without stmtCache")
return fmt.Errorf("EventWriter incorrectly initialized without stmtCache")
} else if txCount == 0 {
return nil
}

var txReader *ingest.LedgerTransactionReader
txReader, err := ingest.NewLedgerTransactionReaderFromLedgerCloseMeta(eventHandler.passphrase, lcm)
if err != nil {
return errors.Wrapf(err,
"failed to open transaction reader for ledger %d",
lcm.LedgerSequence())
return fmt.Errorf(
"failed to open transaction reader for ledger %d: %w ",
lcm.LedgerSequence(), err)
}
defer func() {
closeErr := txReader.Close()
Expand Down Expand Up @@ -130,7 +131,6 @@ func (eventHandler *eventHandler) trimEvents(latestLedgerSeq uint32, retentionWi
// If f returns false, the scan terminates early (f will not be applied on
// remaining events in the range).
func (eventHandler *eventHandler) GetEvents(ctx context.Context, cursorRange events.CursorRange, contractIds []string, f ScanFunction) error {

start := time.Now()

var rows []struct {
Expand All @@ -141,8 +141,8 @@ func (eventHandler *eventHandler) GetEvents(ctx context.Context, cursorRange eve

rowQ := sq.
Select("e.id", "e.application_order", "lcm.meta").
From(fmt.Sprintf("%s e", eventTableName)).
Join(fmt.Sprintf("%s lcm ON (e.ledger_sequence = lcm.sequence)", ledgerCloseMetaTableName)).
From(eventTableName + " e").
Join(ledgerCloseMetaTableName + "lcm ON (e.ledger_sequence = lcm.sequence)").
Where(sq.GtOrEq{"e.id": cursorRange.Start.String()}).
Where(sq.Lt{"e.id": cursorRange.End.String()}).
OrderBy("e.id ASC")
Expand All @@ -152,18 +152,21 @@ func (eventHandler *eventHandler) GetEvents(ctx context.Context, cursorRange eve
}

if err := eventHandler.db.Select(ctx, &rows, rowQ); err != nil {
return errors.Wrapf(err, "db read failed for start ledger cursor= %v contractIds= %v", cursorRange.Start.String(), contractIds)
return fmt.Errorf("db read failed for start ledger cursor= %v contractIds= %v: %w", cursorRange.Start.String(), contractIds, err)
} else if len(rows) < 1 {
return errors.New("No LCM found with requested event filters")

Check failure on line 157 in cmd/soroban-rpc/internal/db/event.go

View workflow job for this annotation

GitHub Actions / golangci-lint

use of `errors.New` forbidden because "Do not use stellar/go/support/errors, use the standard 'errors' package and fmt.Errorf()." (forbidigo)
}

for _, row := range rows {
eventCursorId, txIndex, lcm := row.EventCursorId, row.TxIndex, row.Lcm

Check failure on line 161 in cmd/soroban-rpc/internal/db/event.go

View workflow job for this annotation

GitHub Actions / golangci-lint

var-naming: var eventCursorId should be eventCursorID (revive)
reader, err := ingest.NewLedgerTransactionReaderFromLedgerCloseMeta(eventHandler.passphrase, lcm)
reader.Seek(txIndex - 1)
if err != nil {
return fmt.Errorf("failed to index to tx %d in ledger %d: %w", txIndex, lcm.LedgerSequence(), err)
}

err = reader.Seek(txIndex - 1)
if err != nil {
return errors.Wrapf(err, "failed to index to tx %d in ledger %d", txIndex, lcm.LedgerSequence())
return fmt.Errorf("failed to index to tx %d in ledger %d: %w", txIndex, lcm.LedgerSequence(), err)
}

ledgerCloseTime := lcm.LedgerCloseTime()
Expand All @@ -172,12 +175,12 @@ func (eventHandler *eventHandler) GetEvents(ctx context.Context, cursorRange eve
diagEvents, diagErr := ledgerTx.GetDiagnosticEvents()

if diagErr != nil {
return errors.Wrapf(err, "db read failed for Event Id %s", eventCursorId)
return fmt.Errorf("db read failed for Event Id %s: %w", eventCursorId, err)
}

// Find events based on filter passed in function f
for eventIndex, event := range diagEvents {
cur := events.Cursor{lcm.LedgerSequence(), uint32(txIndex), 0, uint32(eventIndex)}
cur := events.Cursor{Ledger: lcm.LedgerSequence(), Tx: uint32(txIndex), Event: uint32(eventIndex)}
if f != nil && !f(event, cur, ledgerCloseTime, &transactionHash) {
return nil
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/soroban-rpc/internal/db/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package db

import (
"context"
"github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/events"
"io"

"github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/events"

"github.com/prometheus/client_golang/prometheus"

"github.com/stellar/go/ingest"
Expand Down Expand Up @@ -60,7 +61,7 @@ func (eventHandler *mockEventHandler) GetEvents(ctx context.Context, cursorRange
}

for _, event := range diagEvents {
if !f(event, events.Cursor{0, 0, 0, 0}, 0, &ledgerTx.Result.TransactionHash) {
if !f(event, events.Cursor{}, 0, &ledgerTx.Result.TransactionHash) {
return nil
}
}
Expand Down Expand Up @@ -192,8 +193,7 @@ func (m *mockLedgerReader) StreamAllLedgers(ctx context.Context, f StreamLedgerF
return nil
}

type mockEventReader struct {
}
type mockEventReader struct{}

func NewMockEventReader() {
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/soroban-rpc/internal/db/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package db
import (
"context"
"encoding/hex"
"github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/events"
"math/rand"
"testing"

"github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/events"

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -29,7 +30,6 @@ func TestTransactionNotFound(t *testing.T) {
}

func txMetaWithEvents(acctSeq uint32, successful bool) xdr.LedgerCloseMeta {

meta := txMeta(acctSeq, successful)

contractIDBytes, _ := hex.DecodeString("df06d62447fd25da07c0135eed7557e5a5497ee7d15b7fe345bd47e191d8f577")
Expand Down Expand Up @@ -102,6 +102,7 @@ func TestTransactionFound(t *testing.T) {
cursorRange := events.CursorRange{Start: start, End: end}

err = eventReader.GetEvents(ctx, cursorRange, nil, nil)
require.NoError(t, err)

// check all 200 cases
for _, lcm := range lcms {
Expand Down
14 changes: 7 additions & 7 deletions cmd/soroban-rpc/internal/methods/get_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"context"
"encoding/json"
"fmt"
"strings"
"time"

"github.com/stellar/go/support/log"
"github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/db"
"github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/ledgerbucketwindow"
"strings"
"time"

"github.com/creachadair/jrpc2"

Expand Down Expand Up @@ -107,7 +108,7 @@ func (g *GetEventsRequest) Valid(maxLimit uint) error {
}
for i, filter := range g.Filters {
if err := filter.Valid(); err != nil {
return errors.Wrapf(err, "filter %d invalid", i+1)
return fmt.Errorf("filter %d invalid: %w", i+1)

Check failure on line 111 in cmd/soroban-rpc/internal/methods/get_events.go

View workflow job for this annotation

GitHub Actions / Unit tests (ubuntu-20.04)

fmt.Errorf format %w reads arg #2, but call has 1 arg

Check failure on line 111 in cmd/soroban-rpc/internal/methods/get_events.go

View workflow job for this annotation

GitHub Actions / Unit tests (ubuntu-22.04)

fmt.Errorf format %w reads arg #2, but call has 1 arg
}
}

Expand Down Expand Up @@ -162,7 +163,7 @@ func (e *EventFilter) Valid() error {
}
for i, topic := range e.Topics {
if err := topic.Valid(); err != nil {
return errors.Wrapf(err, "topic %d invalid", i+1)
return fmt.Errorf("topic %d invalid: %w", i+1, err)
}
}
return nil
Expand Down Expand Up @@ -220,7 +221,7 @@ func (t *TopicFilter) Valid() error {
}
for i, segment := range *t {
if err := segment.Valid(); err != nil {
return errors.Wrapf(err, "segment %d invalid", i+1)
return fmt.Errorf("segment %d invalid: %w", i+1, err)
}
}
return nil
Expand Down Expand Up @@ -372,7 +373,6 @@ func (h eventsRPCHandler) getEvents(ctx context.Context, request GetEventsReques
}

err := h.dbReader.GetEvents(ctx, cursorRange, contractIds, f)

if err != nil {
return GetEventsResponse{}, &jrpc2.Error{
Code: jrpc2.InvalidRequest,
Expand All @@ -393,7 +393,7 @@ func (h eventsRPCHandler) getEvents(ctx context.Context, request GetEventsReques
}
results = append(results, info)
}
//TODO (prit): Refactor latest ledger code !!
// TODO (prit): Refactor latest ledger code !!
return GetEventsResponse{
LatestLedger: 0,
Events: results,
Expand Down

0 comments on commit 1a03730

Please sign in to comment.