Merge pull request #1039 from nspcc-dev/neo3/rpc/getversion

rpc: update `getversion` RPC-call
This commit is contained in:
Roman Khimov 2020-06-10 11:08:45 +03:00 committed by GitHub
commit 4264a758ac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 14 deletions

View file

@ -345,17 +345,10 @@ func (s *Server) HandshakedPeersCount() int {
// getVersionMsg returns current version message. // getVersionMsg returns current version message.
func (s *Server) getVersionMsg() (*Message, error) { func (s *Server) getVersionMsg() (*Message, error) {
var port uint16 port, err := s.Port()
_, portStr, err := net.SplitHostPort(s.transport.Address())
if err != nil {
port = s.Port
} else {
p, err := strconv.ParseUint(portStr, 10, 16)
if err != nil { if err != nil {
return nil, err return nil, err
} }
port = uint16(p)
}
capabilities := []capability.Capability{ 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
}

View file

@ -541,10 +541,11 @@ var rpcClientTestCases = map[string][]rpcClientTestCase{
invoke: func(c *Client) (interface{}, error) { invoke: func(c *Client) (interface{}, error) {
return c.GetVersion() 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{} { result: func(c *Client) interface{} {
return &result.Version{ return &result.Version{
Port: uint16(20332), TCPPort: uint16(20332),
WSPort: uint16(20342),
Nonce: 2153672787, Nonce: 2153672787,
UserAgent: "/NEO-GO:0.73.1-pre-273-ge381358/", UserAgent: "/NEO-GO:0.73.1-pre-273-ge381358/",
} }

View file

@ -4,8 +4,9 @@ type (
// Version model used for reporting server version // Version model used for reporting server version
// info. // info.
Version struct { Version struct {
Port uint16 `json:"tcp_port"` TCPPort uint16 `json:"tcp_port"`
WSPort uint16 `json:"ws_port,omitempty"`
Nonce uint32 `json:"nonce"` Nonce uint32 `json:"nonce"`
UserAgent string `json:"useragent"` UserAgent string `json:"user_agent"`
} }
) )

View file

@ -442,8 +442,12 @@ func (s *Server) getBlockHash(reqParams request.Params) (interface{}, *response.
} }
func (s *Server) getVersion(_ request.Params) (interface{}, *response.Error) { 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{ return result.Version{
Port: s.coreServer.Port, TCPPort: port,
Nonce: s.coreServer.ID(), Nonce: s.coreServer.ID(),
UserAgent: s.coreServer.UserAgent, UserAgent: s.coreServer.UserAgent,
}, nil }, nil