Skip to content

Commit

Permalink
Add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaptic committed Jan 22, 2025
1 parent 5ba5b80 commit 4db05df
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 5 deletions.
2 changes: 1 addition & 1 deletion clients/stellarcore/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ func (c *Client) GetLedgerEntryRaw(ctx context.Context, ledgerSeq uint32, keys .

func (c *Client) GetLedgerEntries(ctx context.Context, ledgerSeq uint32, keys ...xdr.LedgerKey) (*proto.GetLedgerEntryResponse, error) {
var resp *proto.GetLedgerEntryResponse
return resp, c.makeLedgerKeyRequest(ctx, resp, "getledgerentry", ledgerSeq, keys...)
return resp, c.makeLedgerKeyRequest(ctx, &resp, "getledgerentry", ledgerSeq, keys...)
}

// SubmitTransaction calls the `tx` command on the connected stellar core with the provided envelope
Expand Down
62 changes: 61 additions & 1 deletion clients/stellarcore/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func TestManualClose_NotAvailable(t *testing.T) {
assert.EqualError(t, err, "exception in response: Set MANUAL_CLOSE=true")
}

func TestGetLedgerEntries(t *testing.T) {
func TestGetRawLedgerEntries(t *testing.T) {
hmock := httptest.NewClient()
c := &Client{HTTP: hmock, URL: "http://localhost:11626"}

Expand Down Expand Up @@ -139,6 +139,66 @@ func TestGetLedgerEntries(t *testing.T) {
require.Equal(t, "pretend this is another XDR lol", resp.Entries[1].Entry)
}

func TestGetLedgerEntries(t *testing.T) {
hmock := httptest.NewClient()
c := &Client{HTTP: hmock, URL: "http://localhost:11626"}

// build a fake response body
mockResp := proto.GetLedgerEntryResponse{
Ledger: 1215, // checkpoint align on expected request
Entries: []proto.LedgerEntryResponse{{
Entry: "pretend this is XDR lol",
State: "live",
Ttl: 1234,
}, {
Entry: "pretend this is another XDR lol",
State: "archived",
}},
}

var key xdr.LedgerKey
acc, err := xdr.AddressToAccountId(keypair.MustRandom().Address())
require.NoError(t, err)
key.SetAccount(acc)

// happy path - fetch an entry
ce := hmock.On("POST", "http://localhost:11626/getledgerentry")
hmock.RegisterResponder(
"POST",
"http://localhost:11626/getledgerentry",
func(r *http.Request) (*http.Response, error) {
// Ensure the request has the correct POST body
requestData, ierr := io.ReadAll(r.Body)
require.NoError(t, ierr)

keyB64, ierr := key.MarshalBinaryBase64()
require.NoError(t, ierr)
expected := fmt.Sprintf("key=%s&ledgerSeq=1234", url.QueryEscape(keyB64))
require.Equal(t, expected, string(requestData))

resp, ierr := httpmock.NewJsonResponse(http.StatusOK, &mockResp)
require.NoError(t, ierr)
ce.Return(httpmock.ResponderFromResponse(resp))
return resp, nil
})

resp, err := c.GetLedgerEntries(context.Background(), 1234, key)
require.NoError(t, err)
require.NotNil(t, resp)

require.EqualValues(t, 1215, resp.Ledger)
require.Len(t, resp.Entries, 2)
require.Equal(t, "pretend this is XDR lol", resp.Entries[0].Entry)
require.Equal(t, "pretend this is another XDR lol", resp.Entries[1].Entry)
require.EqualValues(t, 1234, resp.Entries[0].Ttl)
require.EqualValues(t, "live", resp.Entries[0].State)
require.EqualValues(t, "archived", resp.Entries[1].State)

key.Type = xdr.LedgerEntryTypeTtl
_, err = c.GetLedgerEntries(context.Background(), 1234, key)
require.Error(t, err)
}

func TestGenSorobanConfigUpgradeTxAndKey(t *testing.T) {
coreBinary := os.Getenv("STELLAR_CORE_BINARY_PATH")
if coreBinary == "" {
Expand Down
6 changes: 3 additions & 3 deletions protocols/stellarcore/getledgerentry_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ const (

// GetLedgerEntryResponse is the structure of Stellar Core's /getledgerentry
type GetLedgerEntryResponse struct {
Ledger uint32 `json:"ledger"`
Entries []RawLedgerEntriesResponse `json:"entries"`
Ledger uint32 `json:"ledger"`
Entries []LedgerEntryResponse `json:"entries"`
}

type RawLedgerEntriesResponse struct {
type LedgerEntryResponse struct {
Entry string `json:"e"` // base64-encoded xdr.LedgerEntry, or xdr.LedgerKey if state == new
State string `json:"state"` // one of the above states
Ttl uint32 `json:"ttl,omitempty"` // optionally, a Soroban entry's `liveUntilLedgerSeq`
Expand Down

0 comments on commit 4db05df

Please sign in to comment.