Add id and timeoffset to getpeerinfo.

This commit is contained in:
David Hill 2015-03-01 16:31:03 -05:00
parent 62432a6f90
commit db8fa6f850
6 changed files with 26 additions and 4 deletions

View file

@ -329,14 +329,16 @@ Returns a list of JSON objects containing information about each connected
network node. The objects have the following format: 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. "addr":"host:port" # IP and port of the peer as a string.
"addrlocal":"ip:port" # Local address as a string. "addrlocal":"ip:port" # Local address as a string.
"services":"00000001", # Services bitmask as a string. "services":"00000001", # Services bitmask as a string.
"lastsend":t, # Time in seconds since epoch since last send. "lastsend":t, # Time in seconds since epoch since last send.
"lastrecv":t, # Time in seconds since epoch since last received message. "lastrecv":t, # Time in seconds since epoch since last received message.
"bytessent":n, # Total number of bytes sent. "bytessent":n, # Total number of bytes sent.
"bytesrecv":n, # Total number of bytes received "bytesrecv":n, # Total number of bytes received.
"conntime":t, # Connection time in seconds since epoc. "conntime":t, # Connection time in seconds since epoch.
"timeoffset":n, # Peer time offset.
"pingtime":n, # Ping time "pingtime":n, # Ping time
"pingwait":n, # Ping wait. "pingwait":n, # Ping wait.
"version":n, # The numeric peer version. "version":n, # The numeric peer version.

View file

@ -137,6 +137,7 @@ type GetNetworkInfoResult struct {
// GetPeerInfoResult models the data returned from the getpeerinfo command. // GetPeerInfoResult models the data returned from the getpeerinfo command.
type GetPeerInfoResult struct { type GetPeerInfoResult struct {
ID string `json:"id"`
Addr string `json:"addr"` Addr string `json:"addr"`
AddrLocal string `json:"addrlocal,omitempty"` AddrLocal string `json:"addrlocal,omitempty"`
Services string `json:"services"` Services string `json:"services"`
@ -144,9 +145,10 @@ type GetPeerInfoResult struct {
LastRecv int64 `json:"lastrecv"` LastRecv int64 `json:"lastrecv"`
BytesSent uint64 `json:"bytessent"` BytesSent uint64 `json:"bytessent"`
BytesRecv uint64 `json:"bytesrecv"` BytesRecv uint64 `json:"bytesrecv"`
ConnTime int64 `json:"conntime"`
TimeOffset int64 `json:"timeoffset"`
PingTime float64 `json:"pingtime"` PingTime float64 `json:"pingtime"`
PingWait float64 `json:"pingwait,omitempty"` PingWait float64 `json:"pingwait,omitempty"`
ConnTime int64 `json:"conntime"`
Version uint32 `json:"version"` Version uint32 `json:"version"`
SubVer string `json:"subver"` SubVer string `json:"subver"`
Inbound bool `json:"inbound"` Inbound bool `json:"inbound"`

View file

@ -136,6 +136,7 @@ type GetNetworkInfoResult struct {
// GetPeerInfoResult models the data returned from the getpeerinfo command. // GetPeerInfoResult models the data returned from the getpeerinfo command.
type GetPeerInfoResult struct { type GetPeerInfoResult struct {
ID int32 `json:"id"`
Addr string `json:"addr"` Addr string `json:"addr"`
AddrLocal string `json:"addrlocal,omitempty"` AddrLocal string `json:"addrlocal,omitempty"`
Services string `json:"services"` Services string `json:"services"`
@ -143,9 +144,10 @@ type GetPeerInfoResult struct {
LastRecv int64 `json:"lastrecv"` LastRecv int64 `json:"lastrecv"`
BytesSent uint64 `json:"bytessent"` BytesSent uint64 `json:"bytessent"`
BytesRecv uint64 `json:"bytesrecv"` BytesRecv uint64 `json:"bytesrecv"`
ConnTime int64 `json:"conntime"`
TimeOffset int64 `json:"timeoffset"`
PingTime float64 `json:"pingtime"` PingTime float64 `json:"pingtime"`
PingWait float64 `json:"pingwait,omitempty"` PingWait float64 `json:"pingwait,omitempty"`
ConnTime int64 `json:"conntime"`
Version uint32 `json:"version"` Version uint32 `json:"version"`
SubVer string `json:"subver"` SubVer string `json:"subver"`
Inbound bool `json:"inbound"` Inbound bool `json:"inbound"`

12
peer.go
View file

@ -56,6 +56,10 @@ const (
) )
var ( 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 // userAgentName is the user agent name and is used to help identify
// ourselves to other bitcoin peers. // ourselves to other bitcoin peers.
userAgentName = "btcd" userAgentName = "btcd"
@ -150,6 +154,7 @@ type peer struct {
conn net.Conn conn net.Conn
addr string addr string
na *wire.NetAddress na *wire.NetAddress
id int32
inbound bool inbound bool
persistent bool persistent bool
knownAddresses map[string]struct{} knownAddresses map[string]struct{}
@ -179,6 +184,7 @@ type peer struct {
versionKnown bool versionKnown bool
protocolVersion uint32 protocolVersion uint32
services wire.ServiceFlag services wire.ServiceFlag
timeOffset int64
timeConnected time.Time timeConnected time.Time
lastSend time.Time lastSend time.Time
lastRecv time.Time lastRecv time.Time
@ -402,6 +408,12 @@ func (p *peer) handleVersionMsg(msg *wire.MsgVersion) {
// Set the remote peer's user agent. // Set the remote peer's user agent.
p.userAgent = msg.UserAgent 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() p.StatsMtx.Unlock()
// Choose whether or not to relay transactions before a filter command // Choose whether or not to relay transactions before a filter command

View file

@ -307,6 +307,7 @@ var helpDescsEnUS = map[string]string{
"getnettotalsresult-timemillis": "Number of milliseconds since 1 Jan 1970 GMT", "getnettotalsresult-timemillis": "Number of milliseconds since 1 Jan 1970 GMT",
// GetPeerInfoResult help. // GetPeerInfoResult help.
"getpeerinforesult-id": "A unique node ID",
"getpeerinforesult-addr": "The ip address and port of the peer", "getpeerinforesult-addr": "The ip address and port of the peer",
"getpeerinforesult-addrlocal": "Local address", "getpeerinforesult-addrlocal": "Local address",
"getpeerinforesult-services": "Services bitmask which represents the services supported by the peer", "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-bytessent": "Total bytes sent",
"getpeerinforesult-bytesrecv": "Total bytes received", "getpeerinforesult-bytesrecv": "Total bytes received",
"getpeerinforesult-conntime": "Time the connection was made in seconds since 1 Jan 1970 GMT", "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-pingtime": "Number of microseconds the last ping took",
"getpeerinforesult-pingwait": "Number of microseconds a queued ping has been waiting for a response", "getpeerinforesult-pingwait": "Number of microseconds a queued ping has been waiting for a response",
"getpeerinforesult-version": "The protocol version of the peer", "getpeerinforesult-version": "The protocol version of the peer",

View file

@ -402,6 +402,7 @@ func (s *server) handleQuery(querymsg interface{}, state *peerState) {
// version. // version.
p.StatsMtx.Lock() p.StatsMtx.Lock()
info := &btcjson.GetPeerInfoResult{ info := &btcjson.GetPeerInfoResult{
ID: p.id,
Addr: p.addr, Addr: p.addr,
Services: fmt.Sprintf("%08d", p.services), Services: fmt.Sprintf("%08d", p.services),
LastSend: p.lastSend.Unix(), LastSend: p.lastSend.Unix(),
@ -409,6 +410,7 @@ func (s *server) handleQuery(querymsg interface{}, state *peerState) {
BytesSent: p.bytesSent, BytesSent: p.bytesSent,
BytesRecv: p.bytesReceived, BytesRecv: p.bytesReceived,
ConnTime: p.timeConnected.Unix(), ConnTime: p.timeConnected.Unix(),
TimeOffset: p.timeOffset,
Version: p.protocolVersion, Version: p.protocolVersion,
SubVer: p.userAgent, SubVer: p.userAgent,
Inbound: p.inbound, Inbound: p.inbound,