Skip to content

Commit

Permalink
Go mod changes
Browse files Browse the repository at this point in the history
  • Loading branch information
aditya1702 committed Apr 16, 2024
1 parent 35da2a5 commit 35ed84d
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 79 deletions.
58 changes: 36 additions & 22 deletions cmd/soroban-rpc/internal/methods/get_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package methods

import (
"context"
"fmt"

"github.com/creachadair/jrpc2"
"github.com/creachadair/jrpc2/handler"
Expand All @@ -24,7 +25,7 @@ type GetTransactionsRequest struct {
Pagination *TransactionsPaginationOptions `json:"pagination,omitempty"`
}

func (req GetTransactionsRequest) isValid() error {
func (req *GetTransactionsRequest) isValid(maxLimit uint) error {
// Validate the start and end ledger sequence
if req.StartLedger < 0 {
return errors.New("start ledger cannot be negative")
Expand All @@ -33,6 +34,16 @@ func (req GetTransactionsRequest) isValid() error {
return errors.New("end ledger cannot be less than start ledger")
}

// Validate pagination
if req.Pagination != nil && req.Pagination.Cursor != nil {
if req.StartLedger != 0 {
return errors.New("startLedger and cursor cannot both be set")
}
}
if req.Pagination != nil && req.Pagination.Limit > maxLimit {
return fmt.Errorf("limit must not exceed %d", maxLimit)
}

return nil
}

Expand Down Expand Up @@ -62,8 +73,8 @@ type transactionsRPCHandler struct {
networkPassphrase string
}

func (h transactionsRPCHandler) getTransactionsByLedgerSequence(ctx context.Context, request GetTransactionsRequest) (GetTransactionsResponse, error) {
err := request.isValid()
func (h *transactionsRPCHandler) getTransactionsByLedgerSequence(ctx context.Context, request GetTransactionsRequest) (GetTransactionsResponse, error) {
err := request.isValid(h.maxLimit)
if err != nil {
return GetTransactionsResponse{}, &jrpc2.Error{
Code: jrpc2.InvalidParams,
Expand Down Expand Up @@ -168,46 +179,49 @@ LedgerLoop:
}
}

tx, err := h.ledgerEntryReader.NewTx(ctx)
latestLedgerSequence, latestLedgerCloseTime, err := h.getLatestLedgerDetails(ctx)
if err != nil {
return GetTransactionsResponse{}, &jrpc2.Error{
Code: jrpc2.InvalidParams,
Message: err.Error(),
}
}

return GetTransactionsResponse{
Transactions: txns,
LatestLedger: latestLedgerSequence,
LatestLedgerCloseTimestamp: latestLedgerCloseTime,
Pagination: &TransactionsPaginationOptions{
Cursor: &cursor,
Limit: limit,
},
}, nil

}

func (h *transactionsRPCHandler) getLatestLedgerDetails(ctx context.Context) (sequence int64, ledgerCloseTime int64, err error) {
tx, err := h.ledgerEntryReader.NewTx(ctx)
if err != nil {
return 0, 0, err
}
defer func() {
_ = tx.Done()
}()

latestSequence, err := tx.GetLatestLedgerSequence()
if err != nil {
return GetTransactionsResponse{}, &jrpc2.Error{
Code: jrpc2.InvalidParams,
Message: err.Error(),
}
return 0, 0, err
}

latestLedger, found, err := h.ledgerReader.GetLedger(ctx, latestSequence)
if (err != nil) || (!found) {
if err == nil {
err = errors.New("ledger close meta not found")
}
return GetTransactionsResponse{}, &jrpc2.Error{
Code: jrpc2.InvalidParams,
Message: err.Error(),
}
return 0, 0, err
}

return GetTransactionsResponse{
Transactions: txns,
LatestLedger: int64(latestLedger.LedgerSequence()),
LatestLedgerCloseTimestamp: int64(latestLedger.LedgerHeaderHistoryEntry().Header.ScpValue.CloseTime),
Pagination: &TransactionsPaginationOptions{
Cursor: &cursor,
Limit: limit,
},
}, nil

return int64(latestLedger.LedgerSequence()), int64(latestLedger.LedgerHeaderHistoryEntry().Header.ScpValue.CloseTime), nil
}

func NewGetTransactionsHandler(logger *log.Entry, ledgerReader db.LedgerReader, ledgerEntryReader db.LedgerEntryReader, maxLimit, defaultLimit uint, networkPassphrase string) jrpc2.Handler {
Expand Down
41 changes: 24 additions & 17 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ toolchain go1.22.1
require (
github.com/Masterminds/squirrel v1.5.4
github.com/cenkalti/backoff/v4 v4.2.1
github.com/creachadair/jrpc2 v1.2.0
github.com/creachadair/jrpc2 v1.1.2
github.com/go-chi/chi v4.1.2+incompatible
github.com/go-git/go-git/v5 v5.9.0
github.com/mattn/go-sqlite3 v1.14.17
Expand All @@ -18,26 +18,28 @@ require (
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5
github.com/stellar/go v0.0.0-20240207003209-73de95c8eb55
github.com/stellar/go v0.0.0-20240416162339-73233da7c30e
github.com/stretchr/testify v1.8.4
golang.org/x/mod v0.13.0
)

require (
cloud.google.com/go v0.111.0 // indirect
cloud.google.com/go v0.112.0 // indirect
cloud.google.com/go/compute v1.23.3 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/iam v1.1.5 // indirect
cloud.google.com/go/storage v1.30.1 // indirect
cloud.google.com/go/storage v1.37.0 // indirect
dario.cat/mergo v1.0.0 // indirect
github.com/cloudflare/circl v1.3.5 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/djherbis/fscache v0.10.1 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.3.0 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/s2a-go v0.1.7 // indirect
github.com/google/uuid v1.4.0 // indirect
github.com/google/uuid v1.5.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
github.com/hashicorp/golang-lru v1.0.2 // indirect
Expand All @@ -50,18 +52,23 @@ require (
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/otel v1.19.0 // indirect
go.opentelemetry.io/otel/metric v1.19.0 // indirect
go.opentelemetry.io/otel/trace v1.19.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.46.1 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.1 // indirect
go.opentelemetry.io/otel v1.21.0 // indirect
go.opentelemetry.io/otel/metric v1.21.0 // indirect
go.opentelemetry.io/otel/trace v1.21.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/oauth2 v0.13.0 // indirect
golang.org/x/oauth2 v0.16.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.14.0 // indirect
google.golang.org/api v0.149.0 // indirect
google.golang.org/api v0.157.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto v0.0.0-20231212172506-995d672761c0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20231211222908-989df2bf70f3 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240102182953-50ed04b92917 // indirect
google.golang.org/genproto v0.0.0-20240116215550-a9fa1716bcac // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240122161410-6c6643bf1457 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240116215550-a9fa1716bcac // indirect
google.golang.org/grpc v1.60.1 // indirect
gopkg.in/djherbis/atime.v1 v1.0.0 // indirect
gopkg.in/djherbis/stream.v1 v1.3.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
)

Expand All @@ -74,7 +81,7 @@ require (
github.com/aws/aws-sdk-go v1.45.27 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/creachadair/mds v0.13.4 // indirect
github.com/creachadair/mds v0.3.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emirpasic/gods v1.18.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
Expand Down Expand Up @@ -106,9 +113,9 @@ require (
github.com/stellar/go-xdr v0.0.0-20231122183749-b53fb00bcac2 // indirect
github.com/stretchr/objx v0.5.1 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
golang.org/x/crypto v0.16.0 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
Expand Down
Loading

0 comments on commit 35ed84d

Please sign in to comment.