From db8fa6f8508e8a5cec7553bec4b32df1f6273bd1 Mon Sep 17 00:00:00 2001 From: David Hill Date: Sun, 1 Mar 2015 16:31:03 -0500 Subject: [PATCH] Add id and timeoffset to getpeerinfo. --- btcjson/cmdhelp.go | 6 ++++-- btcjson/jsonresults.go | 4 +++- btcjson/v2/btcjson/chainsvrresults.go | 4 +++- peer.go | 12 ++++++++++++ rpcserverhelp.go | 2 ++ server.go | 2 ++ 6 files changed, 26 insertions(+), 4 deletions(-) diff --git a/btcjson/cmdhelp.go b/btcjson/cmdhelp.go index 5b4a2643..a1d973e2 100644 --- a/btcjson/cmdhelp.go +++ b/btcjson/cmdhelp.go @@ -329,14 +329,16 @@ Returns a list of JSON objects containing information about each connected network node. The objects have the following format: [ { + "id":n, # Unique node ID. "addr":"host:port" # IP and port of the peer as a string. "addrlocal":"ip:port" # Local address as a string. "services":"00000001", # Services bitmask as a string. "lastsend":t, # Time in seconds since epoch since last send. "lastrecv":t, # Time in seconds since epoch since last received message. "bytessent":n, # Total number of bytes sent. - "bytesrecv":n, # Total number of bytes received - "conntime":t, # Connection time in seconds since epoc. + "bytesrecv":n, # Total number of bytes received. + "conntime":t, # Connection time in seconds since epoch. + "timeoffset":n, # Peer time offset. "pingtime":n, # Ping time "pingwait":n, # Ping wait. "version":n, # The numeric peer version. diff --git a/btcjson/jsonresults.go b/btcjson/jsonresults.go index 54a4a636..21de6c6e 100644 --- a/btcjson/jsonresults.go +++ b/btcjson/jsonresults.go @@ -137,6 +137,7 @@ type GetNetworkInfoResult struct { // GetPeerInfoResult models the data returned from the getpeerinfo command. type GetPeerInfoResult struct { + ID string `json:"id"` Addr string `json:"addr"` AddrLocal string `json:"addrlocal,omitempty"` Services string `json:"services"` @@ -144,9 +145,10 @@ type GetPeerInfoResult struct { LastRecv int64 `json:"lastrecv"` BytesSent uint64 `json:"bytessent"` BytesRecv uint64 `json:"bytesrecv"` + ConnTime int64 `json:"conntime"` + TimeOffset int64 `json:"timeoffset"` PingTime float64 `json:"pingtime"` PingWait float64 `json:"pingwait,omitempty"` - ConnTime int64 `json:"conntime"` Version uint32 `json:"version"` SubVer string `json:"subver"` Inbound bool `json:"inbound"` diff --git a/btcjson/v2/btcjson/chainsvrresults.go b/btcjson/v2/btcjson/chainsvrresults.go index 60859f60..2f025a84 100644 --- a/btcjson/v2/btcjson/chainsvrresults.go +++ b/btcjson/v2/btcjson/chainsvrresults.go @@ -136,6 +136,7 @@ type GetNetworkInfoResult struct { // GetPeerInfoResult models the data returned from the getpeerinfo command. type GetPeerInfoResult struct { + ID int32 `json:"id"` Addr string `json:"addr"` AddrLocal string `json:"addrlocal,omitempty"` Services string `json:"services"` @@ -143,9 +144,10 @@ type GetPeerInfoResult struct { LastRecv int64 `json:"lastrecv"` BytesSent uint64 `json:"bytessent"` BytesRecv uint64 `json:"bytesrecv"` + ConnTime int64 `json:"conntime"` + TimeOffset int64 `json:"timeoffset"` PingTime float64 `json:"pingtime"` PingWait float64 `json:"pingwait,omitempty"` - ConnTime int64 `json:"conntime"` Version uint32 `json:"version"` SubVer string `json:"subver"` Inbound bool `json:"inbound"` diff --git a/peer.go b/peer.go index 69ff0644..e9582c49 100644 --- a/peer.go +++ b/peer.go @@ -56,6 +56,10 @@ const ( ) var ( + // nodeCount is the total number of peer connections made since startup + // and is used to assign an id to a peer. + nodeCount int32 + // userAgentName is the user agent name and is used to help identify // ourselves to other bitcoin peers. userAgentName = "btcd" @@ -150,6 +154,7 @@ type peer struct { conn net.Conn addr string na *wire.NetAddress + id int32 inbound bool persistent bool knownAddresses map[string]struct{} @@ -179,6 +184,7 @@ type peer struct { versionKnown bool protocolVersion uint32 services wire.ServiceFlag + timeOffset int64 timeConnected time.Time lastSend time.Time lastRecv time.Time @@ -402,6 +408,12 @@ func (p *peer) handleVersionMsg(msg *wire.MsgVersion) { // Set the remote peer's user agent. p.userAgent = msg.UserAgent + // Set the peer's time offset. + p.timeOffset = msg.Timestamp.Unix() - time.Now().Unix() + + // Set the peer's ID. + p.id = atomic.AddInt32(&nodeCount, 1) + p.StatsMtx.Unlock() // Choose whether or not to relay transactions before a filter command diff --git a/rpcserverhelp.go b/rpcserverhelp.go index 1e5694b7..78950e07 100644 --- a/rpcserverhelp.go +++ b/rpcserverhelp.go @@ -307,6 +307,7 @@ var helpDescsEnUS = map[string]string{ "getnettotalsresult-timemillis": "Number of milliseconds since 1 Jan 1970 GMT", // GetPeerInfoResult help. + "getpeerinforesult-id": "A unique node ID", "getpeerinforesult-addr": "The ip address and port of the peer", "getpeerinforesult-addrlocal": "Local address", "getpeerinforesult-services": "Services bitmask which represents the services supported by the peer", @@ -315,6 +316,7 @@ var helpDescsEnUS = map[string]string{ "getpeerinforesult-bytessent": "Total bytes sent", "getpeerinforesult-bytesrecv": "Total bytes received", "getpeerinforesult-conntime": "Time the connection was made in seconds since 1 Jan 1970 GMT", + "getpeerinforesult-timeoffset": "The time offset of the peer", "getpeerinforesult-pingtime": "Number of microseconds the last ping took", "getpeerinforesult-pingwait": "Number of microseconds a queued ping has been waiting for a response", "getpeerinforesult-version": "The protocol version of the peer", diff --git a/server.go b/server.go index 383288d3..c3e1e76f 100644 --- a/server.go +++ b/server.go @@ -402,6 +402,7 @@ func (s *server) handleQuery(querymsg interface{}, state *peerState) { // version. p.StatsMtx.Lock() info := &btcjson.GetPeerInfoResult{ + ID: p.id, Addr: p.addr, Services: fmt.Sprintf("%08d", p.services), LastSend: p.lastSend.Unix(), @@ -409,6 +410,7 @@ func (s *server) handleQuery(querymsg interface{}, state *peerState) { BytesSent: p.bytesSent, BytesRecv: p.bytesReceived, ConnTime: p.timeConnected.Unix(), + TimeOffset: p.timeOffset, Version: p.protocolVersion, SubVer: p.userAgent, Inbound: p.inbound,