Skip to content

Added more ledger details output by getLatestLedger endpoint#544

Closed
cjonas9 wants to merge 5 commits intomainfrom
add-more-ledger-details-to-getlatestledger
Closed

Added more ledger details output by getLatestLedger endpoint#544
cjonas9 wants to merge 5 commits intomainfrom
add-more-ledger-details-to-getlatestledger

Conversation

@cjonas9
Copy link
Contributor

@cjonas9 cjonas9 commented Nov 24, 2025

MIGRATING THIS PR TO protocol 25 branch

What

See issue #79. Included more details to be output by the getLatestLedger endpoint such that it matches the info output by getLedgers. The following fields are output:

  • id: hash of the lastest ledger
  • protocolVersion: Stellar Core protocol version
  • sequence: sequence number of the latest ledger
  • closeTime: time the latest ledger closed
  • header: base64 encoded ledger header
  • headerJson: JSON representation of the latest ledger header (omitted if empty)
  • metadata: base64 encoded representation of latest ledger metadata
  • metadataJson: JSON representation of latest ledger metadata (omitted if empty)

Why

The getLatestLedger endpoint should return all information about the ledger, but it previously only output id, protocolVersion, and sequence.

Known limitations

N/A.

@cjonas9 cjonas9 marked this pull request as ready for review November 25, 2025 17:25
@cjonas9
Copy link
Contributor Author

cjonas9 commented Nov 25, 2025

for the reference/convenience of anyone interested:

curl -X POST http://localhost:8000 \
  -H "Content-Type: application/json" \
  -d '{
    "jsonrpc": "2.0",
    "id": 1,
    "method": "getLatestLedger"
  }'

@cjonas9 cjonas9 linked an issue Nov 25, 2025 that may be closed by this pull request
@@ -1,12 +1,24 @@
package protocol

import "encoding/json"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we moved the protocol package and the stellar-rpc client to the go sdk:

https://github.com/stellar/go-stellar-sdk/tree/master/protocols/rpc

https://github.com/stellar/go-stellar-sdk/tree/master/clients/rpcclient

I think we forgot to delete the code from the stellar-rpc repo. So, we will need to:

  1. delete the rpc client and protocols packages from the stellar-rpc repo
  2. update rpc codebase to use the client / protocols package from the go sdk
  3. create a PR in the go sdk repo to update the GetLatestLedgerResponse

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch, will raise an issue about this!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's already done on the protocol-25 branch (see #527). This could retarget that branch and be released as part of that, or we can try to merge the branches but that'd probably be more painful.

Comment on lines +16 to +23
// LedgerHeader of the latest ledger (base64-encoded XDR)
LedgerHeader string `json:"header"`
// JSON representation of the latest ledger header
LedgerHeaderJSON json.RawMessage `json:"headerJson,omitempty"`
// LedgerMetadata of the latest ledger (base64-encoded XDR)
LedgerMetadata string `json:"metadata"`
// JSON representation of the latest ledger metadata
LedgerMetadataJSON json.RawMessage `json:"metadataJson,omitempty"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend dropping JSON support and just having base64 fields, but still suffixing them appropriately in case we want to introduce json later.

Suggested change
// LedgerHeader of the latest ledger (base64-encoded XDR)
LedgerHeader string `json:"header"`
// JSON representation of the latest ledger header
LedgerHeaderJSON json.RawMessage `json:"headerJson,omitempty"`
// LedgerMetadata of the latest ledger (base64-encoded XDR)
LedgerMetadata string `json:"metadata"`
// JSON representation of the latest ledger metadata
LedgerMetadataJSON json.RawMessage `json:"metadataJson,omitempty"`
// LedgerHeader of the latest ledger (base64-encoded XDR)
LedgerHeader string `json:"headerXdr"`
// LedgerMetadata of the latest ledger (base64-encoded XDR)
LedgerMetadata string `json:"metadataXdr"`

LedgerCloseTime: latestLedger.LedgerCloseTime(),
LedgerHeader: base64.StdEncoding.EncodeToString(headerBytes),
}
if LedgerHeaderJSON, err := json.Marshal(header); err == nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah you can't just call Marshal on this because it's an XDR structure, you'll almost certainly error out every time; that's what xdr2json is for. I recommend not having JSON-formatted fields in this endpoint at all tbh, because we'd then need to add xdrFormat: "json" | "base64" to the request itself to switch between them like with the other endpoints.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oddly, this never error'd out on my testing of this (curling http://localhost:8000). still a great catch and indeed why xdr2json is there. becomes moot because I'm taking your advice to drop JSON support.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the conditional relies on err == nil 😉 you were hiding the error case


assert.Equal(t, expectedLatestLedgerProtocolVersion, latestLedgerResp.ProtocolVersion)
assert.Equal(t, expectedLatestLedgerSequence, latestLedgerResp.Sequence)
assert.Equal(t, int64(expectedLatestLedgerCloseTime), latestLedgerResp.LedgerCloseTime)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No tests for header/metadata equivalence?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in latest commit

@cjonas9 cjonas9 changed the base branch from main to protocol-25 November 26, 2025 22:45
@cjonas9 cjonas9 changed the base branch from protocol-25 to main November 26, 2025 23:06
@cjonas9 cjonas9 closed this Dec 3, 2025
@cjonas9 cjonas9 deleted the add-more-ledger-details-to-getlatestledger branch December 8, 2025 19:07
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 more ledger details to getLedger/getLatestLedger

3 participants