From dc14eb58b6c5c18402a7610880b7101597790d31 Mon Sep 17 00:00:00 2001 From: Martin Boehm Date: Wed, 7 Feb 2018 21:42:56 +0100 Subject: [PATCH] Implement Bitcore socket.io method getInfo --- server/socketio.go | 33 ++++++++++++++++++++++++++++++++- server/static/test.html | 23 ++++++++++++++++++++++- 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/server/socketio.go b/server/socketio.go index 0fb650c8..d6b2b55b 100644 --- a/server/socketio.go +++ b/server/socketio.go @@ -132,6 +132,9 @@ var onMessageHandlers = map[string]func(*SocketIoServer, json.RawMessage) (inter } return }, + "\"getInfo\"": func(s *SocketIoServer, params json.RawMessage) (rv interface{}, err error) { + return s.getInfo() + }, } func (s *SocketIoServer) onMessage(c *gosocketio.Channel, req map[string]json.RawMessage) interface{} { @@ -256,7 +259,7 @@ type resultGetBlockHeader struct { func (s *SocketIoServer) getBlockHeader(height uint32, hash string) (res resultGetBlockHeader, err error) { if hash == "" { - // trezor is only interested in hash + // trezor is interested only in hash if height == 0 { height, hash, err = s.db.GetBestBlock() if err != nil { @@ -314,6 +317,34 @@ func (s *SocketIoServer) estimateSmartFee(blocks int, conservative bool) (res re return } +type resultGetInfo struct { + Result struct { + Version int `json:"version"` + ProtocolVersion int `json:"protocolVersion"` + Blocks int `json:"blocks"` + TimeOffset int `json:"timeOffset"` + Connections int `json:"connections"` + Proxy string `json:"proxy"` + Difficulty float64 `json:"difficulty"` + Testnet bool `json:"testnet"` + RelayFee float64 `json:"relayFee"` + Errors string `json:"errors"` + Network string `json:"network"` + Subversion string `json:"subversion"` + LocalServices string `json:"localServices"` + } `json:"result"` +} + +func (s *SocketIoServer) getInfo() (res resultGetInfo, err error) { + // trezor is interested only in best block height + height, _, err := s.db.GetBestBlock() + if err != nil { + return + } + res.Result.Blocks = int(height) + return +} + func (s *SocketIoServer) onSubscribe(c *gosocketio.Channel, req map[string]json.RawMessage) interface{} { glog.Info(c.Id(), " onSubscribe ", req) return nil diff --git a/server/static/test.html b/server/static/test.html index 4c522374..b65f0602 100644 --- a/server/static/test.html +++ b/server/static/test.html @@ -81,6 +81,21 @@ const params = [blocks, conservative]; return socket.send({ method, params }, f); } + + function getInfo() { + lookupSyncStatus(function (result) { + console.log('getInfo sent successfully'); + console.log(result); + document.getElementById('getInfoResult').innerText = JSON.stringify(result).replace(/,/g, ", "); + }); + } + + function lookupSyncStatus(f) { + const method = 'getInfo'; + const params = []; + return socket.send({ method, params }, f); + } + @@ -146,7 +161,13 @@
- +
+
+ +
+
+
+