From 86ecac13eb22184e9b420c30fbdaf6a0f01cd557 Mon Sep 17 00:00:00 2001 From: Alfonso Acosta Date: Thu, 23 Nov 2023 16:31:36 +0100 Subject: [PATCH] rpc: unify all ledger sequence types to json-stringified uint32 --- cmd/soroban-rpc/internal/methods/get_events.go | 2 +- cmd/soroban-rpc/internal/methods/get_events_test.go | 8 +------- .../internal/methods/get_latest_ledger.go | 2 +- .../internal/methods/get_ledger_entries.go | 8 ++++---- cmd/soroban-rpc/internal/methods/get_transaction.go | 12 ++++++------ cmd/soroban-rpc/internal/methods/send_transaction.go | 6 +++--- .../internal/methods/simulate_transaction.go | 6 +++--- 7 files changed, 19 insertions(+), 25 deletions(-) diff --git a/cmd/soroban-rpc/internal/methods/get_events.go b/cmd/soroban-rpc/internal/methods/get_events.go index b16da281b..20a95433d 100644 --- a/cmd/soroban-rpc/internal/methods/get_events.go +++ b/cmd/soroban-rpc/internal/methods/get_events.go @@ -79,7 +79,7 @@ type EventInfo struct { } type GetEventsRequest struct { - StartLedger int32 `json:"startLedger,string,omitempty"` + StartLedger uint32 `json:"startLedger,string,omitempty"` Filters []EventFilter `json:"filters"` Pagination *PaginationOptions `json:"pagination,omitempty"` } diff --git a/cmd/soroban-rpc/internal/methods/get_events_test.go b/cmd/soroban-rpc/internal/methods/get_events_test.go index dcaa5b306..4d15e2c0a 100644 --- a/cmd/soroban-rpc/internal/methods/get_events_test.go +++ b/cmd/soroban-rpc/internal/methods/get_events_test.go @@ -404,7 +404,7 @@ func TestGetEventsRequestValid(t *testing.T) { []byte("{ \"filters\": [], \"pagination\": { \"cursor\": \"0000000021474840576-0000000000\"} }"), &request, )) - assert.Equal(t, int32(0), request.StartLedger) + assert.Equal(t, uint32(0), request.StartLedger) assert.NoError(t, request.Valid(1000)) assert.EqualError(t, (&GetEventsRequest{ @@ -431,12 +431,6 @@ func TestGetEventsRequestValid(t *testing.T) { Pagination: nil, }).Valid(1000), "startLedger must be positive") - assert.EqualError(t, (&GetEventsRequest{ - StartLedger: -100, - Filters: []EventFilter{}, - Pagination: nil, - }).Valid(1000), "startLedger must be positive") - assert.EqualError(t, (&GetEventsRequest{ StartLedger: 1, Filters: []EventFilter{ diff --git a/cmd/soroban-rpc/internal/methods/get_latest_ledger.go b/cmd/soroban-rpc/internal/methods/get_latest_ledger.go index 96d0d11ed..2a6ae6dab 100644 --- a/cmd/soroban-rpc/internal/methods/get_latest_ledger.go +++ b/cmd/soroban-rpc/internal/methods/get_latest_ledger.go @@ -15,7 +15,7 @@ type GetLatestLedgerResponse struct { // Stellar Core protocol version associated with the ledger. ProtocolVersion uint32 `json:"protocolVersion,string"` // Sequence number of the latest ledger. - Sequence uint32 `json:"sequence"` + Sequence uint32 `json:"sequence,string"` } // NewGetLatestLedgerHandler returns a JSON RPC handler to retrieve the latest ledger entry from Stellar core. diff --git a/cmd/soroban-rpc/internal/methods/get_ledger_entries.go b/cmd/soroban-rpc/internal/methods/get_ledger_entries.go index 0b444ce1c..f099995d6 100644 --- a/cmd/soroban-rpc/internal/methods/get_ledger_entries.go +++ b/cmd/soroban-rpc/internal/methods/get_ledger_entries.go @@ -25,7 +25,7 @@ type LedgerEntryResult struct { // Ledger entry data encoded in base 64. XDR string `json:"xdr"` // Last modified ledger for this entry. - LastModifiedLedger int64 `json:"lastModifiedLedgerSeq,string"` + LastModifiedLedger uint32 `json:"lastModifiedLedgerSeq,string"` // The ledger sequence until the entry is live, available for entries that have associated ttl ledger entries. LiveUntilLedgerSeq *uint32 `json:"liveUntilLedgerSeq,string,omitempty"` } @@ -34,7 +34,7 @@ type GetLedgerEntriesResponse struct { // All found ledger entries. Entries []LedgerEntryResult `json:"entries"` // Sequence number of the latest ledger at time of request. - LatestLedger int64 `json:"latestLedger,string"` + LatestLedger uint32 `json:"latestLedger,string"` } const getLedgerEntriesMaxKeys = 200 @@ -132,14 +132,14 @@ func NewGetLedgerEntriesHandler(logger *log.Entry, ledgerEntryReader db.LedgerEn ledgerEntryResults = append(ledgerEntryResults, LedgerEntryResult{ Key: keyXDR, XDR: entryXDR, - LastModifiedLedger: int64(ledgerKeyAndEntry.Entry.LastModifiedLedgerSeq), + LastModifiedLedger: uint32(ledgerKeyAndEntry.Entry.LastModifiedLedgerSeq), LiveUntilLedgerSeq: ledgerKeyAndEntry.LiveUntilLedgerSeq, }) } response := GetLedgerEntriesResponse{ Entries: ledgerEntryResults, - LatestLedger: int64(latestLedger), + LatestLedger: uint32(latestLedger), } return response, nil }) diff --git a/cmd/soroban-rpc/internal/methods/get_transaction.go b/cmd/soroban-rpc/internal/methods/get_transaction.go index 257d435f3..bf418ded1 100644 --- a/cmd/soroban-rpc/internal/methods/get_transaction.go +++ b/cmd/soroban-rpc/internal/methods/get_transaction.go @@ -30,11 +30,11 @@ type GetTransactionResponse struct { // Status is one of: TransactionSuccess, TransactionNotFound, or TransactionFailed. Status string `json:"status"` // LatestLedger is the latest ledger stored in Soroban-RPC. - LatestLedger int64 `json:"latestLedger,string"` + LatestLedger uint32 `json:"latestLedger,string"` // LatestLedgerCloseTime is the unix timestamp of when the latest ledger was closed. LatestLedgerCloseTime int64 `json:"latestLedgerCloseTime,string"` // LatestLedger is the oldest ledger stored in Soroban-RPC. - OldestLedger int64 `json:"oldestLedger,string"` + OldestLedger uint32 `json:"oldestLedger,string"` // LatestLedgerCloseTime is the unix timestamp of when the oldest ledger was closed. OldestLedgerCloseTime int64 `json:"oldestLedgerCloseTime,string"` @@ -53,7 +53,7 @@ type GetTransactionResponse struct { ResultMetaXdr string `json:"resultMetaXdr,omitempty"` // Ledger is the sequence of the ledger which included the transaction. - Ledger int64 `json:"ledger,string,omitempty"` + Ledger uint32 `json:"ledger,string,omitempty"` // LedgerCloseTime is the unix timestamp of when the transaction was included in the ledger. LedgerCloseTime int64 `json:"createdAt,string,omitempty"` } @@ -86,9 +86,9 @@ func GetTransaction(getter transactionGetter, request GetTransactionRequest) (Ge tx, found, storeRange := getter.GetTransaction(txHash) response := GetTransactionResponse{ - LatestLedger: int64(storeRange.LastLedger.Sequence), + LatestLedger: storeRange.LastLedger.Sequence, LatestLedgerCloseTime: storeRange.LastLedger.CloseTime, - OldestLedger: int64(storeRange.FirstLedger.Sequence), + OldestLedger: storeRange.FirstLedger.Sequence, OldestLedgerCloseTime: storeRange.FirstLedger.CloseTime, } if !found { @@ -98,7 +98,7 @@ func GetTransaction(getter transactionGetter, request GetTransactionRequest) (Ge response.ApplicationOrder = tx.ApplicationOrder response.FeeBump = tx.FeeBump - response.Ledger = int64(tx.Ledger.Sequence) + response.Ledger = tx.Ledger.Sequence response.LedgerCloseTime = tx.Ledger.CloseTime response.ResultXdr = base64.StdEncoding.EncodeToString(tx.Result) diff --git a/cmd/soroban-rpc/internal/methods/send_transaction.go b/cmd/soroban-rpc/internal/methods/send_transaction.go index 3651d7e68..7ae87d581 100644 --- a/cmd/soroban-rpc/internal/methods/send_transaction.go +++ b/cmd/soroban-rpc/internal/methods/send_transaction.go @@ -31,7 +31,7 @@ type SendTransactionResponse struct { Hash string `json:"hash"` // LatestLedger is the latest ledger known to Soroban-RPC at the time it handled // the transaction submission request. - LatestLedger int64 `json:"latestLedger,string"` + LatestLedger uint32 `json:"latestLedger,string"` // LatestLedgerCloseTime is the unix timestamp of the close time of the latest ledger known to // Soroban-RPC at the time it handled the transaction submission request. LatestLedgerCloseTime int64 `json:"latestLedgerCloseTime,string"` @@ -106,14 +106,14 @@ func NewSendTransactionHandler(daemon interfaces.Daemon, logger *log.Entry, stor ErrorResultXDR: resp.Error, Status: resp.Status, Hash: txHash, - LatestLedger: int64(ledgerInfo.Sequence), + LatestLedger: ledgerInfo.Sequence, LatestLedgerCloseTime: ledgerInfo.CloseTime, }, nil case proto.TXStatusPending, proto.TXStatusDuplicate, proto.TXStatusTryAgainLater: return SendTransactionResponse{ Status: resp.Status, Hash: txHash, - LatestLedger: int64(ledgerInfo.Sequence), + LatestLedger: ledgerInfo.Sequence, LatestLedgerCloseTime: ledgerInfo.CloseTime, }, nil default: diff --git a/cmd/soroban-rpc/internal/methods/simulate_transaction.go b/cmd/soroban-rpc/internal/methods/simulate_transaction.go index cd6a8bae1..504b6d5c3 100644 --- a/cmd/soroban-rpc/internal/methods/simulate_transaction.go +++ b/cmd/soroban-rpc/internal/methods/simulate_transaction.go @@ -43,7 +43,7 @@ type SimulateTransactionResponse struct { Results []SimulateHostFunctionResult `json:"results,omitempty"` // an array of the individual host function call results Cost SimulateTransactionCost `json:"cost,omitempty"` // the effective cpu and memory cost of the invoked transaction execution. RestorePreamble *RestorePreamble `json:"restorePreamble,omitempty"` // If present, it indicates that a prior RestoreFootprint is required - LatestLedger int64 `json:"latestLedger,string"` + LatestLedger uint32 `json:"latestLedger,string"` } type PreflightGetter interface { @@ -125,7 +125,7 @@ func NewSimulateTransactionHandler(logger *log.Entry, ledgerEntryReader db.Ledge if err != nil { return SimulateTransactionResponse{ Error: err.Error(), - LatestLedger: int64(latestLedger), + LatestLedger: latestLedger, } } @@ -154,7 +154,7 @@ func NewSimulateTransactionHandler(logger *log.Entry, ledgerEntryReader db.Ledge CPUInstructions: result.CPUInstructions, MemoryBytes: result.MemoryBytes, }, - LatestLedger: int64(latestLedger), + LatestLedger: latestLedger, RestorePreamble: restorePreamble, } })