Skip to content

Commit

Permalink
Better string building is lit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaptic committed Aug 15, 2024
1 parent 994e89e commit aaf30ad
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
23 changes: 16 additions & 7 deletions clients/stellarcore/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,18 +389,27 @@ func (c *Client) getResponse(req *http.Request) (*http.Response, error) {
return hresp, nil
}

// buildMultiKeyRequest is a workaround helper because, unfortunately,
// url.Values does not support multiple keys via Set(), so we have to build our
// URL parameters manually.
func buildMultiKeyRequest(keys ...xdr.LedgerKey) (string, error) {
// Unfortunately, url.Values does not support multiple keys via Set(), so we
// have to build our URL parameters manually.
q := ""
// The average ledger key length, according to a simple
//
// SELECT AVG(LENGTH(HEX(key))) / 2 FROM ledger_entries;
//
// is ~57.6. We can use this to preallocate a final string buffer for
// performance.
q := strings.Builder{}
q.Grow(50 * len(keys))

for _, key := range keys {
keyB64, err := key.MarshalBinaryBase64()
if err != nil {
return q, errors.Wrapf(err, "failed to encode LedgerKey")
return q.String(), errors.Wrapf(err, "failed to encode LedgerKey")
}
q += "key=" + url.QueryEscape(keyB64) + "&"
q.WriteString("key=" + url.QueryEscape(keyB64) + "&")
}

q, _ = strings.CutSuffix(q, "&")
return q, nil
s, _ := strings.CutSuffix(q.String(), "&") // trim trailing &
return s, nil
}
2 changes: 1 addition & 1 deletion protocols/stellarcore/getledgerentries_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ type GetLedgerEntriesResponse struct {

type LedgerEntryResponse struct {
Entry string `json:"e"` // base64-encoded xdr.LedgerEntry
State string `json:"state"` // one of: "live" | "new_entry_no_proof" | "new_entry_proof" | "archived_no_proof" | "archived_proof"
State string `json:"state"` // one of the above State constants
}

0 comments on commit aaf30ad

Please sign in to comment.