From 3418e4f7f43fe727ff05e69c1e1e7867f2672c5c Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Wed, 10 Jun 2020 10:01:21 +0300 Subject: [PATCH] rpc: update `getversion` RPC-call closes #1035 --- pkg/network/server.go | 27 ++++++++++++++++++--------- pkg/rpc/client/rpc_test.go | 5 +++-- pkg/rpc/response/result/version.go | 5 +++-- pkg/rpc/server/server.go | 6 +++++- 4 files changed, 29 insertions(+), 14 deletions(-) diff --git a/pkg/network/server.go b/pkg/network/server.go index 512473b47..cb95b5600 100644 --- a/pkg/network/server.go +++ b/pkg/network/server.go @@ -345,16 +345,9 @@ func (s *Server) HandshakedPeersCount() int { // getVersionMsg returns current version message. func (s *Server) getVersionMsg() (*Message, error) { - var port uint16 - _, portStr, err := net.SplitHostPort(s.transport.Address()) + port, err := s.Port() if err != nil { - port = s.Port - } else { - p, err := strconv.ParseUint(portStr, 10, 16) - if err != nil { - return nil, err - } - port = uint16(p) + return nil, err } capabilities := []capability.Capability{ @@ -942,3 +935,19 @@ func (s *Server) broadcastTxLoop() { } } } + +// Port returns actual server port. It may differs from that of server.Config. +func (s *Server) Port() (uint16, error) { + var port uint16 + _, portStr, err := net.SplitHostPort(s.transport.Address()) + if err != nil { + port = s.ServerConfig.Port + } else { + p, err := strconv.ParseUint(portStr, 10, 16) + if err != nil { + return 0, err + } + port = uint16(p) + } + return port, nil +} diff --git a/pkg/rpc/client/rpc_test.go b/pkg/rpc/client/rpc_test.go index 68fe40364..c3c955d12 100644 --- a/pkg/rpc/client/rpc_test.go +++ b/pkg/rpc/client/rpc_test.go @@ -541,10 +541,11 @@ var rpcClientTestCases = map[string][]rpcClientTestCase{ invoke: func(c *Client) (interface{}, error) { return c.GetVersion() }, - serverResponse: `{"id":1,"jsonrpc":"2.0","result":{"tcp_port":20332,"nonce":2153672787,"useragent":"/NEO-GO:0.73.1-pre-273-ge381358/"}}`, + serverResponse: `{"id":1,"jsonrpc":"2.0","result":{"tcp_port":20332,"ws_port":20342,"nonce":2153672787,"user_agent":"/NEO-GO:0.73.1-pre-273-ge381358/"}}`, result: func(c *Client) interface{} { return &result.Version{ - Port: uint16(20332), + TCPPort: uint16(20332), + WSPort: uint16(20342), Nonce: 2153672787, UserAgent: "/NEO-GO:0.73.1-pre-273-ge381358/", } diff --git a/pkg/rpc/response/result/version.go b/pkg/rpc/response/result/version.go index 5c80fed5e..482d216ce 100644 --- a/pkg/rpc/response/result/version.go +++ b/pkg/rpc/response/result/version.go @@ -4,8 +4,9 @@ type ( // Version model used for reporting server version // info. Version struct { - Port uint16 `json:"tcp_port"` + TCPPort uint16 `json:"tcp_port"` + WSPort uint16 `json:"ws_port,omitempty"` Nonce uint32 `json:"nonce"` - UserAgent string `json:"useragent"` + UserAgent string `json:"user_agent"` } ) diff --git a/pkg/rpc/server/server.go b/pkg/rpc/server/server.go index 8006b7293..c42ddf6ed 100644 --- a/pkg/rpc/server/server.go +++ b/pkg/rpc/server/server.go @@ -442,8 +442,12 @@ func (s *Server) getBlockHash(reqParams request.Params) (interface{}, *response. } func (s *Server) getVersion(_ request.Params) (interface{}, *response.Error) { + port, err := s.coreServer.Port() + if err != nil { + return nil, response.NewInternalServerError("Cannot fetch tcp port", err) + } return result.Version{ - Port: s.coreServer.Port, + TCPPort: port, Nonce: s.coreServer.ID(), UserAgent: s.coreServer.UserAgent, }, nil