Skip to content

Commit

Permalink
rpc: add get health
Browse files Browse the repository at this point in the history
  • Loading branch information
yihau committed Nov 14, 2023
1 parent 558c81b commit 22328cd
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
12 changes: 12 additions & 0 deletions rpc/get_health.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package rpc

import (
"context"
)

type GetHealthResponse JsonRpcResponse[string]

// GetHealthResponse returns the current health of the node. A healthy node is one that is within HEALTH_CHECK_SLOT_DISTANCE slots of the latest cluster confirmed slot.
func (c *RpcClient) GetHealth(ctx context.Context) (JsonRpcResponse[string], error) {
return call[JsonRpcResponse[string]](c, ctx, "getHealth")
}
77 changes: 77 additions & 0 deletions rpc/get_health_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package rpc

import (
"context"
"testing"

"github.com/blocto/solana-go-sdk/internal/client_test"
)

func TestGetHealth(t *testing.T) {
client_test.TestAll(
t,
[]client_test.Param{
{
RequestBody: `{"jsonrpc":"2.0", "id":1, "method":"getHealth"}`,
ResponseBody: `{"jsonrpc":"2.0","result":"ok","id":1}`,
F: func(url string) (any, error) {
c := NewRpcClient(url)
return c.GetHealth(
context.TODO(),
)
},
ExpectedValue: JsonRpcResponse[string]{
JsonRpc: "2.0",
Id: 1,
Error: nil,
Result: "ok",
},
ExpectedError: nil,
},
{
RequestBody: `{"jsonrpc":"2.0", "id":1, "method":"getHealth"}`,
ResponseBody: `{"jsonrpc":"2.0","error":{"code":-32005,"message":"Node is behind by 42 slots","data":{"numSlotsBehind":42}},"id":1}`,
F: func(url string) (any, error) {
c := NewRpcClient(url)
return c.GetHealth(
context.TODO(),
)
},
ExpectedValue: JsonRpcResponse[string]{
JsonRpc: "2.0",
Id: 1,
Error: &JsonRpcError{
Code: -32005,
Message: `Node is behind by 42 slots`,
Data: map[string]any{
"numSlotsBehind": float64(42),
},
},
Result: "",
},
ExpectedError: nil,
},
{
RequestBody: `{"jsonrpc":"2.0", "id":1, "method":"getHealth"}`,
ResponseBody: `{"jsonrpc":"2.0","error":{"code":-32005,"message":"Node is unhealthy","data":{}},"id":1}`,
F: func(url string) (any, error) {
c := NewRpcClient(url)
return c.GetHealth(
context.TODO(),
)
},
ExpectedValue: JsonRpcResponse[string]{
JsonRpc: "2.0",
Id: 1,
Error: &JsonRpcError{
Code: -32005,
Message: `Node is unhealthy`,
Data: map[string]any{},
},
Result: "",
},
ExpectedError: nil,
},
},
)
}

0 comments on commit 22328cd

Please sign in to comment.