Add getInfo websocket method

ethereum
Martin Boehm 2018-12-16 00:26:41 +01:00
parent a04b2b67b5
commit f332c0aa9e
3 changed files with 66 additions and 41 deletions

View File

@ -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()

View File

@ -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) {

View File

@ -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);
}
</script>
</head>
@ -311,15 +306,14 @@
<div class="row">
<div class="col" id="estimateFeeResult">
</div>
</div>
</div>-->
<div class="row">
<div class="col">
<input class="btn btn-secondary" type="button" value="getInfo" onclick="getInfo()">
</div>
<div class="col-10" id="getInfoResult">
</div>
</div>
-->
</div>
<div class="row">
<div class="col">
<input class="btn btn-secondary" type="button" value="sendTransaction" onclick="sendTransaction()">