diff --git a/docs/rpc.md b/docs/rpc.md index ba407b74f..573d2a53a 100644 --- a/docs/rpc.md +++ b/docs/rpc.md @@ -242,7 +242,7 @@ burned). #### `getpeerheights` call -This method returns the block height and user agent of all connected peers. +This method returns the block height and user agent of all connected peers. The block height field may be stale depending on the PingInterval node config and the time since the last ping. Ping behavior may differ between node implementations. #### Historic calls diff --git a/pkg/neorpc/result/peer_heights.go b/pkg/neorpc/result/peer_heights.go index 718bcd20a..e0bdf1f96 100644 --- a/pkg/neorpc/result/peer_heights.go +++ b/pkg/neorpc/result/peer_heights.go @@ -1,12 +1,15 @@ package result type ( + // GetPeerHeights is the payload for outputting peer heights in `getpeerheights` RPC call. GetPeerHeights struct { - PeerHeights PeerHeights `json:"peers"` + PeerHeights PeerHeights `json:"connected"` } + // PeerHeights represents a slice of PeerHeight. PeerHeights []PeerHeight + // PeerHeight gives the user agent and last known block height of a connected peer. PeerHeight struct { Address string `json:"address"` UserAgent string `json:"useragent"` @@ -21,6 +24,7 @@ func NewGetPeerHeights() GetPeerHeights { } } +// AddPeers adds a set of peers to the peer heights slice. func (g *GetPeerHeights) AddPeers(ps []struct { Address string UserAgent string diff --git a/pkg/rpcclient/rpc.go b/pkg/rpcclient/rpc.go index 466d21d1d..ce2cd78f6 100644 --- a/pkg/rpcclient/rpc.go +++ b/pkg/rpcclient/rpc.go @@ -397,6 +397,16 @@ func (c *Client) GetPeers() (*result.GetPeers, error) { return resp, nil } +// GetPeerHeights returns the last known block height and user agent of all connected peers. +func (c *Client) GetPeerHeights() (*result.GetPeerHeights, error) { + var resp = &result.GetPeerHeights{} + + if err := c.performRequest("getpeerheights", nil, resp); err != nil { + return resp, err + } + return resp, nil +} + // GetRawMemPool returns a list of unconfirmed transactions in the memory. func (c *Client) GetRawMemPool() ([]util.Uint256, error) { var resp = new([]util.Uint256) diff --git a/pkg/rpcclient/rpc_test.go b/pkg/rpcclient/rpc_test.go index 03525ca5e..bdf5f4270 100644 --- a/pkg/rpcclient/rpc_test.go +++ b/pkg/rpcclient/rpc_test.go @@ -594,6 +594,37 @@ var rpcClientTestCases = map[string][]rpcClientTestCase{ }, }, }, + "getpeerheights": { + { + name: "positive", + invoke: func(c *Client) (any, error) { + return c.GetPeerHeights() + }, + serverResponse: `{"jsonrpc":"2.0","id":1,"result":{"connected":[{"useragent":"/NEO-GO:0.106.1/","height":1337}]}}`, + result: func(c *Client) any { + return &result.GetPeerHeights{ + PeerHeights: []result.PeerHeight{ + { + UserAgent: "/NEO-GO:0.106.1/", + Height: 1337, + }, + }, + } + }, + }, + { + name: "empty", + invoke: func(c *Client) (any, error) { + return c.GetPeerHeights() + }, + serverResponse: `{"jsonrpc":"2.0","id":1,"result":{"connected":[]}}`, + result: func(c *Client) any { + return &result.GetPeerHeights{ + PeerHeights: []result.PeerHeight{}, + } + }, + }, + }, "getrawmempool": { { name: "positive", @@ -1768,6 +1799,12 @@ var rpcClientErrorCases = map[string][]rpcClientErrorCase{ return c.GetPeers() }, }, + { + name: "getpeerheights_unmarshalling_error", + invoke: func(c *Client) (any, error) { + return c.GetPeerHeights() + }, + }, { name: "getrawmempool_unmarshalling_error", invoke: func(c *Client) (any, error) {