Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getLedgers implementation #303

Merged
merged 40 commits into from
Nov 13, 2024
Merged

Conversation

aditya1702
Copy link
Contributor

@aditya1702 aditya1702 commented Sep 26, 2024

What

Closes #111

Adding new endpoint - getLedgers. It has the following request structure:

type GetLedgersRequest struct {
	StartLedger uint32                   `json:"startLedger"`
	Pagination  *LedgerPaginationOptions `json:"pagination,omitempty"`
	Format      string                   `json:"xdrFormat,omitempty"`
}

and the response structure:

// LedgerInfo represents a single ledger in the response.
type LedgerInfo struct {
	Hash            string `json:"hash"`
	Sequence        uint32 `json:"sequence"`
	LedgerCloseTime int64  `json:"ledgerCloseTime,string"`

	LedgerHeader     string          `json:"headerXdr"`
	LedgerHeaderJSON json.RawMessage `json:"headerJson,omitempty"`

	LedgerMetadata     string          `json:"metadataXdr"`
	LedgerMetadataJSON json.RawMessage `json:"metadataJSON,omitempty"`
}

// GetLedgersResponse encapsulates the response structure for getLedgers queries.
type GetLedgersResponse struct {
	Ledgers               []LedgerInfo `json:"ledgers"`
	LatestLedger          uint32       `json:"latestLedger"`
	LatestLedgerCloseTime int64        `json:"latestLedgerCloseTime"`
	OldestLedger          uint32       `json:"oldestLedger"`
	OldestLedgerCloseTime int64        `json:"oldestLedgerCloseTime"`
	Cursor                string       `json:"cursor"`
}

Pagination functionality is similar to getTransactions and getEvents.

Why

This endpoint allows users to access ledger-level info more easily and enables common use-cases: examining ledger sequence numbers, time of close/finality, fees, or getting a snapshot of the ledger at that point in time. For more info refer to the discussion on the original issue ticket.

Benchmarking Details

  • BatchGetLedgers: Fetches ledgers in batches from the DB. Note that I have used a batch size of 200 for benchmarking as that is the max ledgers request limit we have set for getLedgers endpoint.
goos: darwin
goarch: arm64
pkg: github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/db
BenchmarkBatchGetLedgers
BenchmarkBatchGetLedgers-12    	    3655	    337446 ns/op
  • GetLedgers: The RPC endpoint to get ledgers.
goos: darwin
goarch: arm64
pkg: github.com/stellar/soroban-rpc/cmd/soroban-rpc/internal/methods
BenchmarkGetLedgers
BenchmarkGetLedgers-12    	    1225	    973161 ns/op

@Shaptic
Copy link
Contributor

Shaptic commented Oct 7, 2024

Based on this thread, should we rename LedgerCloseMeta -> LedgerMetadata?

@Shaptic
Copy link
Contributor

Shaptic commented Oct 7, 2024

Should we cherry-pick this onto the protocol22 branch? I don't think it makes sense to target main given how close that is to merging upstream.

@aditya1702 aditya1702 requested a review from Shaptic November 1, 2024 18:06
@2opremio
Copy link
Contributor

2opremio commented Nov 4, 2024

@aditya1702 The PR looks really good! We should resolve the problem of the response ledger range potentially being inconsistent with the ledgers in the response.

…dgers

# Conflicts:
#	cmd/soroban-rpc/internal/config/main.go
#	cmd/soroban-rpc/internal/jsonrpc.go
@aditya1702 aditya1702 merged commit 4f10b54 into stellar:main Nov 13, 2024
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

add getLedgers endpoint
4 participants