diff --git a/clients/stellarcore/client.go b/clients/stellarcore/client.go index e8b817e6ff..ca3d098521 100644 --- a/clients/stellarcore/client.go +++ b/clients/stellarcore/client.go @@ -6,7 +6,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "net/http" "net/url" "path" @@ -22,7 +21,7 @@ import ( // Client represents a client that is capable of communicating with a // stellar-core server using HTTP type Client struct { - // HTTP is the client to use when communicating with stellar-core. If nil, + // HTTP is the client to use when communicating with stellar-core. If nil, // http.DefaultClient will be used. HTTP HTTP @@ -37,7 +36,7 @@ type Client struct { // in case an error was encountered during either the draining or closing of the // stream, that error would be returned. func drainReponse(hresp *http.Response, close bool, err *error) (outerror error) { - _, err2 := io.Copy(ioutil.Discard, hresp.Body) + _, err2 := io.Copy(io.Discard, hresp.Body) if err2 != nil { if err != nil && *err == nil { *err = errors.Wrap(err2, "unable to read excess data from response") @@ -322,7 +321,7 @@ func (c *Client) rawPost( // makeLedgerKeyRequest is a generic method to perform a request in the form // `key=...&key=...&ledgerSeq=...` which is useful because three Stellar Core -// endpoints all use this request format. Be sure to pass `target by reference. +// endpoints all use this request format. Be sure to pass `target` by reference. func (c *Client) makeLedgerKeyRequest( ctx context.Context, target interface{}, @@ -374,10 +373,15 @@ func (c *Client) getResponse(req *http.Request) (*http.Response, error) { func buildMultiKeyRequest(keys ...xdr.LedgerKey) (string, error) { // The average ledger key length, according to a simple // - // SELECT AVG(LENGTH(HEX(key))) / 2 FROM ledger_entries; + // SELECT AVG(LENGTH(HEX(key))) / 2 FROM ledger_entries; // // on a pubnet RPC instance is ~57.6. We can use this to preallocate a // string buffer for performance. + // + // We know that these endpoints will almost exclusively be used for + // ContractData and the like, so we could optimize the buffer further for + // that, but that data is harder to query since it'd involve parsing the XDR + // from the DB to check the key type. q := strings.Builder{} q.Grow(50 * len(keys)) diff --git a/clients/stellarcore/client_test.go b/clients/stellarcore/client_test.go index c646dd6e76..aa068610b1 100644 --- a/clients/stellarcore/client_test.go +++ b/clients/stellarcore/client_test.go @@ -83,11 +83,24 @@ func TestGetLedgerEntries(t *testing.T) { hmock := httptest.NewClient() c := &Client{HTTP: hmock, URL: "http://localhost:11626"} + // build a fake response body + mockResp := proto.GetLedgerEntriesResponse{ + Ledger: 1215, // checkpoint align on expected request + Entries: []proto.LedgerEntryResponse{ + { + Entry: "pretend this is XDR lol", + State: proto.DeadState, + }, + { + Entry: "pretend this is another XDR lol", + State: proto.ArchivedStateNoProof, + }, + }, + } + // happy path - fetch an entry hmock.On("POST", "http://localhost:11626/getledgerentry"). - ReturnString(http.StatusOK, - `{"ledger": 1234, "entries": [ {"e": "pretend it's xdr lol", "state": "dead" }]}`, - ) + ReturnJSON(http.StatusOK, &mockResp) var key xdr.LedgerKey acc, err := xdr.AddressToAccountId(keypair.MustRandom().Address()) @@ -98,7 +111,8 @@ func TestGetLedgerEntries(t *testing.T) { require.NoError(t, err) require.NotNil(t, resp) - require.EqualValues(t, 1234, resp.Ledger) - require.Len(t, resp.Entries, 1) + require.EqualValues(t, 1215, resp.Ledger) + require.Len(t, resp.Entries, 2) require.Equal(t, resp.Entries[0].State, proto.DeadState) + require.Equal(t, resp.Entries[1].State, proto.ArchivedStateNoProof) }