Skip to content

Commit

Permalink
rpc: unify all ledger sequence types to json-stringified uint32
Browse files Browse the repository at this point in the history
  • Loading branch information
2opremio committed Nov 23, 2023
1 parent 7f8ad60 commit 86ecac1
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 25 deletions.
2 changes: 1 addition & 1 deletion cmd/soroban-rpc/internal/methods/get_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand Down
8 changes: 1 addition & 7 deletions cmd/soroban-rpc/internal/methods/get_events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion cmd/soroban-rpc/internal/methods/get_latest_ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions cmd/soroban-rpc/internal/methods/get_ledger_entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand All @@ -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
Expand Down Expand Up @@ -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),

Check failure on line 142 in cmd/soroban-rpc/internal/methods/get_ledger_entries.go

View workflow job for this annotation

GitHub Actions / golangci

unnecessary conversion (unconvert)
}
return response, nil
})
Expand Down
12 changes: 6 additions & 6 deletions cmd/soroban-rpc/internal/methods/get_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`

Expand All @@ -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"`
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions cmd/soroban-rpc/internal/methods/send_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions cmd/soroban-rpc/internal/methods/simulate_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -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,
}
})
Expand Down

0 comments on commit 86ecac1

Please sign in to comment.