diff --git a/server/socketio.go b/server/socketio.go index 92e8c25d..837c6c54 100644 --- a/server/socketio.go +++ b/server/socketio.go @@ -583,10 +583,7 @@ type resultGetInfo struct { } func (s *SocketIoServer) getInfo() (res resultGetInfo, err error) { - height, _, err := s.db.GetBestBlock() - if err != nil { - return - } + _, height, _ := s.is.GetSyncState() res.Result.Blocks = int(height) res.Result.Testnet = s.chain.IsTestnet() res.Result.Network = s.chain.GetNetworkName() diff --git a/server/websocket.go b/server/websocket.go index 560d816f..8226343e 100644 --- a/server/websocket.go +++ b/server/websocket.go @@ -60,6 +60,7 @@ type WebsocketServer struct { metrics *common.Metrics is *common.InternalState api *api.Worker + block0hash string newBlockSubscriptions map[*websocketChannel]string newBlockSubscriptionsLock sync.Mutex addressSubscriptions map[string]map[*websocketChannel]string @@ -72,6 +73,10 @@ func NewWebsocketServer(db *db.RocksDB, chain bchain.BlockChain, txCache *db.TxC if err != nil { return nil, err } + b0, err := db.GetBlockHash(0) + if err != nil { + return nil, err + } s := &WebsocketServer{ upgrader: &websocket.Upgrader{ ReadBufferSize: 1024 * 32, @@ -85,6 +90,7 @@ func NewWebsocketServer(db *db.RocksDB, chain bchain.BlockChain, txCache *db.TxC metrics: metrics, is: is, api: api, + block0hash: b0, newBlockSubscriptions: make(map[*websocketChannel]string), addressSubscriptions: make(map[string]map[*websocketChannel]string), } @@ -216,6 +222,9 @@ var requestHandlers = map[string]func(*WebsocketServer, *websocketChannel, *webs } return }, + "getInfo": func(s *WebsocketServer, c *websocketChannel, req *websocketReq) (rv interface{}, err error) { + return s.getInfo() + }, "sendTransaction": func(s *WebsocketServer, c *websocketChannel, req *websocketReq) (rv interface{}, err error) { r := struct { Hex string `json:"hex"` @@ -303,27 +312,52 @@ func unmarshalGetAccountInfoRequest(params []byte) (*accountInfoReq, error) { } func (s *WebsocketServer) getAccountInfo(req *accountInfoReq) (res *api.Address, err error) { - if s.chainParser.GetChainType() == bchain.ChainEthereumType { - var opt api.GetAddressOption - switch req.Details { - case "balance": - opt = api.Balance - case "txids": - opt = api.TxidHistory - case "txs": - opt = api.TxHistory - default: - opt = api.Basic - } - - return s.api.GetAddress(req.Descriptor, req.Page, req.PageSize, opt, &api.AddressFilter{ - FromHeight: uint32(req.FromHeight), - ToHeight: uint32(req.ToHeight), - Contract: req.ContractFilter, - Vout: api.AddressFilterVoutOff, - }) + var opt api.GetAddressOption + switch req.Details { + case "balance": + opt = api.Balance + case "txids": + opt = api.TxidHistory + case "txs": + opt = api.TxHistory + default: + opt = api.Basic } - return nil, errors.New("Not implemented") + + return s.api.GetAddress(req.Descriptor, req.Page, req.PageSize, opt, &api.AddressFilter{ + FromHeight: uint32(req.FromHeight), + ToHeight: uint32(req.ToHeight), + Contract: req.ContractFilter, + Vout: api.AddressFilterVoutOff, + }) +} + +func (s *WebsocketServer) getInfo() (interface{}, error) { + vi := common.GetVersionInfo() + height, hash, err := s.db.GetBestBlock() + if err != nil { + return nil, err + } + type info struct { + Name string `json:"name"` + Shortcut string `json:"shortcut"` + Decimals int `json:"decimals"` + Version string `json:"version"` + BestHeight int `json:"bestheight"` + BestHash string `json:"besthash"` + Block0Hash string `json:"block0hash"` + Testnet bool `json:"testnet"` + } + return &info{ + Name: s.is.Coin, + Shortcut: s.is.CoinShortcut, + Decimals: s.chainParser.AmountDecimals(), + BestHeight: int(height), + BestHash: hash, + Version: vi.Version, + Block0Hash: s.block0hash, + Testnet: s.chain.IsTestnet(), + }, nil } func (s *WebsocketServer) sendTransaction(tx string) (res resultSendTransaction, err error) { diff --git a/static/test-websocket.html b/static/test-websocket.html index df41f084..606a5c13 100644 --- a/static/test-websocket.html +++ b/static/test-websocket.html @@ -121,6 +121,15 @@ }); } + function getInfo() { + const method = 'getInfo'; + const params = { + }; + send(method, params, function (result) { + document.getElementById('getInfoResult').innerText = JSON.stringify(result).replace(/,/g, ", "); + }); + } + function sendTransaction() { var hex = document.getElementById('sendTransactionHex').value.trim(); const method = 'sendTransaction'; @@ -224,20 +233,6 @@ 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); - } - @@ -311,15 +306,14 @@
-
+ -->
-
- --> +