rpc: encode port numbers as proper numbers in getpeers

The way C# node does it.
This commit is contained in:
Roman Khimov 2021-02-15 22:19:28 +03:00
parent 850d29060a
commit 1ef91fa409
3 changed files with 10 additions and 8 deletions

View file

@ -517,25 +517,25 @@ var rpcClientTestCases = map[string][]rpcClientTestCase{
invoke: func(c *Client) (interface{}, error) { invoke: func(c *Client) (interface{}, error) {
return c.GetPeers() return c.GetPeers()
}, },
serverResponse: `{"id":1,"jsonrpc":"2.0","result":{"unconnected":[{"address":"172.200.0.1","port":"20333"}],"connected":[{"address":"127.0.0.1","port":"20335"}],"bad":[{"address":"172.200.0.254","port":"20332"}]}}`, serverResponse: `{"id":1,"jsonrpc":"2.0","result":{"unconnected":[{"address":"172.200.0.1","port":20333}],"connected":[{"address":"127.0.0.1","port":20335}],"bad":[{"address":"172.200.0.254","port":20332}]}}`,
result: func(c *Client) interface{} { result: func(c *Client) interface{} {
return &result.GetPeers{ return &result.GetPeers{
Unconnected: result.Peers{ Unconnected: result.Peers{
{ {
Address: "172.200.0.1", Address: "172.200.0.1",
Port: "20333", Port: 20333,
}, },
}, },
Connected: result.Peers{ Connected: result.Peers{
{ {
Address: "127.0.0.1", Address: "127.0.0.1",
Port: "20335", Port: 20335,
}, },
}, },
Bad: result.Peers{ Bad: result.Peers{
{ {
Address: "172.200.0.254", Address: "172.200.0.254",
Port: "20332", Port: 20332,
}, },
}, },
} }

View file

@ -1,6 +1,7 @@
package result package result
import ( import (
"strconv"
"strings" "strings"
) )
@ -18,7 +19,7 @@ type (
// Peer represents the peer. // Peer represents the peer.
Peer struct { Peer struct {
Address string `json:"address"` Address string `json:"address"`
Port string `json:"port"` Port uint16 `json:"port"`
} }
) )
@ -50,9 +51,10 @@ func (g *GetPeers) AddBad(addrs []string) {
func (p *Peers) addPeers(addrs []string) { func (p *Peers) addPeers(addrs []string) {
for i := range addrs { for i := range addrs {
addressParts := strings.Split(addrs[i], ":") addressParts := strings.Split(addrs[i], ":")
port, _ := strconv.Atoi(addressParts[1]) // We know it's a good port number.
peer := Peer{ peer := Peer{
Address: addressParts[0], Address: addressParts[0],
Port: addressParts[1], Port: uint16(port),
} }
*p = append(*p, peer) *p = append(*p, peer)

View file

@ -20,7 +20,7 @@ func TestGetPeers(t *testing.T) {
require.Equal(t, 1, len(gp.Connected)) require.Equal(t, 1, len(gp.Connected))
require.Equal(t, 1, len(gp.Bad)) require.Equal(t, 1, len(gp.Bad))
require.Equal(t, "192.168.0.1", gp.Connected[0].Address) require.Equal(t, "192.168.0.1", gp.Connected[0].Address)
require.Equal(t, "10333", gp.Connected[0].Port) require.Equal(t, uint16(10333), gp.Connected[0].Port)
require.Equal(t, "127.0.0.1", gp.Bad[0].Address) require.Equal(t, "127.0.0.1", gp.Bad[0].Address)
require.Equal(t, "20333", gp.Bad[0].Port) require.Equal(t, uint16(20333), gp.Bad[0].Port)
} }