Skip to content

Commit

Permalink
Add Manual Close endpoint to Core's client (#3161)
Browse files Browse the repository at this point in the history
  • Loading branch information
2opremio authored Oct 26, 2020
1 parent 30679f3 commit 33adbc1
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
37 changes: 37 additions & 0 deletions clients/stellarcore/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,43 @@ func (c *Client) WaitForNetworkSync(ctx context.Context) error {
}
}

// ManualClose closes a ledger when Core is running in `MANUAL_CLOSE` mode
func (c *Client) ManualClose(ctx context.Context) (err error) {

q := url.Values{}

req, err := c.simpleGet(ctx, "manualclose", q)
if err != nil {
err = errors.Wrap(err, "failed to create request")
return
}

hresp, err := c.http().Do(req)
if err != nil {
err = errors.Wrap(err, "http request errored")
return
}
defer hresp.Body.Close()

if !(hresp.StatusCode >= 200 && hresp.StatusCode < 300) {
err = errors.New("http request failed with non-200 status code")
}

// verify there wasn't an exception
resp := struct {
Exception string `json:"exception"`
}{}
if decErr := json.NewDecoder(hresp.Body).Decode(&resp); decErr != nil {
return
}
if resp.Exception != "" {
err = fmt.Errorf("exception in response: %s", resp.Exception)
return
}

return
}

func (c *Client) http() HTTP {
if c.HTTP == nil {
return http.DefaultClient
Expand Down
28 changes: 28 additions & 0 deletions clients/stellarcore/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,31 @@ func TestSubmitTransaction(t *testing.T) {
assert.Equal(t, proto.TXStatusPending, resp.Status)
}
}

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

// happy path - new transaction
hmock.On("GET", "http://localhost:11626/manualclose").
ReturnString(http.StatusOK, "Manually triggered a ledger close with sequence number 7")

err := c.ManualClose(context.Background())

assert.NoError(t, err)
}

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

// happy path - new transaction
hmock.On("GET", "http://localhost:11626/manualclose").
ReturnString(http.StatusOK,
`{"exception": "Set MANUAL_CLOSE=true"}`,
)

err := c.ManualClose(context.Background())

assert.EqualError(t, err, "exception in response: Set MANUAL_CLOSE=true")
}

0 comments on commit 33adbc1

Please sign in to comment.