From 3b5bb9fd43eaab2dc902c47b4900b8161b77330a Mon Sep 17 00:00:00 2001 From: David Hill Date: Wed, 19 Oct 2016 14:02:52 -0400 Subject: [PATCH 1/2] btcjson: Add preciousblock --- btcjson/chainsvrcmds.go | 14 ++++++++++++++ btcjson/chainsvrcmds_test.go | 13 +++++++++++++ rpcserver.go | 3 +++ 3 files changed, 30 insertions(+) diff --git a/btcjson/chainsvrcmds.go b/btcjson/chainsvrcmds.go index c16e97ab..352ea938 100644 --- a/btcjson/chainsvrcmds.go +++ b/btcjson/chainsvrcmds.go @@ -534,6 +534,19 @@ func NewPingCmd() *PingCmd { return &PingCmd{} } +// PreciousBlockCmd defines the preciousblock JSON-RPC command. +type PreciousBlockCmd struct { + BlockHash string +} + +// NewPreciousBlockCmd returns a new instance which can be used to issue a +// preciousblock JSON-RPC command. +func NewPreciousBlockCmd(blockHash string) *PreciousBlockCmd { + return &PreciousBlockCmd{ + BlockHash: blockHash, + } +} + // ReconsiderBlockCmd defines the reconsiderblock JSON-RPC command. type ReconsiderBlockCmd struct { BlockHash string @@ -743,6 +756,7 @@ func init() { MustRegisterCmd("help", (*HelpCmd)(nil), flags) MustRegisterCmd("invalidateblock", (*InvalidateBlockCmd)(nil), flags) MustRegisterCmd("ping", (*PingCmd)(nil), flags) + MustRegisterCmd("preciousblock", (*PreciousBlockCmd)(nil), flags) MustRegisterCmd("reconsiderblock", (*ReconsiderBlockCmd)(nil), flags) MustRegisterCmd("searchrawtransactions", (*SearchRawTransactionsCmd)(nil), flags) MustRegisterCmd("sendrawtransaction", (*SendRawTransactionCmd)(nil), flags) diff --git a/btcjson/chainsvrcmds_test.go b/btcjson/chainsvrcmds_test.go index a34d1047..fcfdc3b9 100644 --- a/btcjson/chainsvrcmds_test.go +++ b/btcjson/chainsvrcmds_test.go @@ -681,6 +681,19 @@ func TestChainSvrCmds(t *testing.T) { marshalled: `{"jsonrpc":"1.0","method":"ping","params":[],"id":1}`, unmarshalled: &btcjson.PingCmd{}, }, + { + name: "preciousblock", + newCmd: func() (interface{}, error) { + return btcjson.NewCmd("preciousblock", "0123") + }, + staticCmd: func() interface{} { + return btcjson.NewPreciousBlockCmd("0123") + }, + marshalled: `{"jsonrpc":"1.0","method":"preciousblock","params":["0123"],"id":1}`, + unmarshalled: &btcjson.PreciousBlockCmd{ + BlockHash: "0123", + }, + }, { name: "reconsiderblock", newCmd: func() (interface{}, error) { diff --git a/rpcserver.go b/rpcserver.go index 5ec19776..c1b20e50 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -230,6 +230,9 @@ var rpcUnimplemented = map[string]struct{}{ "getblockchaininfo": {}, "getchaintips": {}, "getnetworkinfo": {}, + "invalidateblock": {}, + "preciousblock": {}, + "reconsiderblock": {}, } // Commands that are available to a limited user From 07e1e308f1469b56ad3b8d7d3dc02466673e0deb Mon Sep 17 00:00:00 2001 From: David Hill Date: Wed, 19 Oct 2016 19:48:03 -0400 Subject: [PATCH 2/2] rpc: Add localaddr and relaytxes to getpeerinfo --- btcjson/chainsvrresults.go | 1 + peer/peer.go | 11 +++++++++++ rpcserver.go | 2 ++ rpcserverhelp.go | 1 + 4 files changed, 15 insertions(+) diff --git a/btcjson/chainsvrresults.go b/btcjson/chainsvrresults.go index b6e3b103..183cc017 100644 --- a/btcjson/chainsvrresults.go +++ b/btcjson/chainsvrresults.go @@ -164,6 +164,7 @@ type GetPeerInfoResult struct { Addr string `json:"addr"` AddrLocal string `json:"addrlocal,omitempty"` Services string `json:"services"` + RelayTxes bool `json:"relaytxes"` LastSend int64 `json:"lastsend"` LastRecv int64 `json:"lastrecv"` BytesSent uint64 `json:"bytessent"` diff --git a/peer/peer.go b/peer/peer.go index 50ff1020..fbb011cc 100644 --- a/peer/peer.go +++ b/peer/peer.go @@ -683,6 +683,17 @@ func (p *Peer) LastRecv() time.Time { return time.Unix(atomic.LoadInt64(&p.lastRecv), 0) } +// LocalAddr returns the local address of the connection. +// +// This function is safe fo concurrent access. +func (p *Peer) LocalAddr() net.Addr { + var localAddr net.Addr + if p.Connected() { + localAddr = p.conn.LocalAddr() + } + return localAddr +} + // BytesSent returns the total number of bytes sent by the peer. // // This function is safe for concurrent access. diff --git a/rpcserver.go b/rpcserver.go index c1b20e50..44ab3170 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -2203,7 +2203,9 @@ func handleGetPeerInfo(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) info := &btcjson.GetPeerInfoResult{ ID: statsSnap.ID, Addr: statsSnap.Addr, + AddrLocal: p.LocalAddr().String(), Services: fmt.Sprintf("%08d", uint64(statsSnap.Services)), + RelayTxes: !p.disableRelayTx, LastSend: statsSnap.LastSend.Unix(), LastRecv: statsSnap.LastRecv.Unix(), BytesSent: statsSnap.BytesSent, diff --git a/rpcserverhelp.go b/rpcserverhelp.go index ad430eb7..727bc0e5 100644 --- a/rpcserverhelp.go +++ b/rpcserverhelp.go @@ -376,6 +376,7 @@ var helpDescsEnUS = map[string]string{ "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", + "getpeerinforesult-relaytxes": "Peer has requested transactions be relayed to it", "getpeerinforesult-lastsend": "Time the last message was received in seconds since 1 Jan 1970 GMT", "getpeerinforesult-lastrecv": "Time the last message was sent in seconds since 1 Jan 1970 GMT", "getpeerinforesult-bytessent": "Total bytes sent",