diff --git a/cmd/soroban-rpc/internal/db/event.go b/cmd/soroban-rpc/internal/db/event.go index 7dea9987..fb6cc78f 100644 --- a/cmd/soroban-rpc/internal/db/event.go +++ b/cmd/soroban-rpc/internal/db/event.go @@ -5,6 +5,7 @@ import ( "github.com/prometheus/client_golang/prometheus" "github.com/stellar/go/ingest" "github.com/stellar/go/support/db" + "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/events" @@ -29,12 +30,21 @@ type eventHandler struct { ingestMetric, countMetric prometheus.Observer } -func (eventHandler *eventHandler) InsertEvents(lcm xdr.LedgerCloseMeta) (err error) { +func (eventHandler *eventHandler) InsertEvents(lcm xdr.LedgerCloseMeta) error { + txCount := lcm.CountTransactions() + + if eventHandler.stmtCache == nil { + return errors.New("EventWriter incorrectly initialized without stmtCache") + } else if txCount == 0 { + return nil + } var txReader *ingest.LedgerTransactionReader - txReader, err = ingest.NewLedgerTransactionReaderFromLedgerCloseMeta(eventHandler.passphrase, lcm) + txReader, err := ingest.NewLedgerTransactionReaderFromLedgerCloseMeta(eventHandler.passphrase, lcm) if err != nil { - return + return errors.Wrapf(err, + "failed to open transaction reader for ledger %d", + lcm.LedgerSequence()) } defer func() { closeErr := txReader.Close() @@ -51,7 +61,7 @@ func (eventHandler *eventHandler) InsertEvents(lcm xdr.LedgerCloseMeta) (err err break } if err != nil { - return + return err } if !tx.Result.Successful() { diff --git a/cmd/soroban-rpc/internal/db/event_test.go b/cmd/soroban-rpc/internal/db/event_test.go index 6cbfcca5..ebe024b5 100644 --- a/cmd/soroban-rpc/internal/db/event_test.go +++ b/cmd/soroban-rpc/internal/db/event_test.go @@ -173,8 +173,8 @@ func TestInsertEvents(t *testing.T) { EventType int `db:"event_type"` } - query1 := sq.Select("*").From(fmt.Sprintf("%s", eventTableName)) - err = db.Select(ctx, &rows, query1) + query := sq.Select("*").From(fmt.Sprintf("%s", eventTableName)) + err = db.Select(ctx, &rows, query) assert.NoError(t, err) assert.Equal(t, 10, len(rows)) } diff --git a/cmd/soroban-rpc/internal/ingest/mock_db_test.go b/cmd/soroban-rpc/internal/ingest/mock_db_test.go index 6e57658d..320ec80a 100644 --- a/cmd/soroban-rpc/internal/ingest/mock_db_test.go +++ b/cmd/soroban-rpc/internal/ingest/mock_db_test.go @@ -36,6 +36,11 @@ type MockTx struct { mock.Mock } +func (m MockTx) EventWriter() db.EventWriter { + args := m.Called() + return args.Get(0).(db.EventWriter) +} + func (m MockTx) LedgerEntryWriter() db.LedgerEntryWriter { args := m.Called() return args.Get(0).(db.LedgerEntryWriter)