Enable websocket getBlock only for extended index

This commit is contained in:
Martin Boehm 2023-03-03 00:12:50 +01:00
parent 7ae521e738
commit adf50156bf
6 changed files with 37 additions and 16 deletions

View File

@ -181,7 +181,7 @@ func mainWithExitCode() int {
}
defer index.Close()
internalState, err = newInternalState(coin, coinShortcut, coinLabel, index)
internalState, err = newInternalState(coin, coinShortcut, coinLabel, index, *enableSubNewTx)
if err != nil {
glog.Error("internalState: ", err)
return exitCodeFatal
@ -406,7 +406,7 @@ func startInternalServer() (*server.InternalServer, error) {
func startPublicServer() (*server.PublicServer, error) {
// start public server in limited functionality, extend it after sync is finished by calling ConnectFullPublicInterface
publicServer, err := server.NewPublicServer(*publicBinding, *certFiles, index, chain, mempool, txCache, *explorerURL, metrics, internalState, *debugMode, *enableSubNewTx)
publicServer, err := server.NewPublicServer(*publicBinding, *certFiles, index, chain, mempool, txCache, *explorerURL, metrics, internalState, *debugMode)
if err != nil {
return nil, err
}
@ -479,7 +479,7 @@ func blockbookAppInfoMetric(db *db.RocksDB, chain bchain.BlockChain, txCache *db
return nil
}
func newInternalState(coin, coinShortcut, coinLabel string, d *db.RocksDB) (*common.InternalState, error) {
func newInternalState(coin, coinShortcut, coinLabel string, d *db.RocksDB, enableSubNewTx bool) (*common.InternalState, error) {
is, err := d.LoadInternalState(coin)
if err != nil {
return nil, err
@ -489,6 +489,7 @@ func newInternalState(coin, coinShortcut, coinLabel string, d *db.RocksDB) (*com
coinLabel = coin
}
is.CoinLabel = coinLabel
is.EnableSubNewTx = enableSubNewTx
name, err := os.Hostname()
if err != nil {
glog.Error("get hostname ", err)

View File

@ -87,6 +87,8 @@ type InternalState struct {
HistoricalTokenFiatRatesTime time.Time `json:"historicalTokenFiatRatesTime"`
CurrentTicker *CurrencyRatesTicker `json:"currentTicker"`
EnableSubNewTx bool `json:"-"`
BackendInfo BackendInfo `json:"-"`
}

View File

@ -63,7 +63,7 @@ type PublicServer struct {
// NewPublicServer creates new public server http interface to blockbook and returns its handle
// only basic functionality is mapped, to map all functions, call
func NewPublicServer(binding string, certFiles string, db *db.RocksDB, chain bchain.BlockChain, mempool bchain.Mempool, txCache *db.TxCache, explorerURL string, metrics *common.Metrics, is *common.InternalState, debugMode bool, enableSubNewTx bool) (*PublicServer, error) {
func NewPublicServer(binding string, certFiles string, db *db.RocksDB, chain bchain.BlockChain, mempool bchain.Mempool, txCache *db.TxCache, explorerURL string, metrics *common.Metrics, is *common.InternalState, debugMode bool) (*PublicServer, error) {
api, err := api.NewWorker(db, chain, mempool, txCache, metrics, is)
if err != nil {
@ -75,7 +75,7 @@ func NewPublicServer(binding string, certFiles string, db *db.RocksDB, chain bch
return nil, err
}
websocket, err := NewWebsocketServer(db, chain, mempool, txCache, metrics, is, enableSubNewTx)
websocket, err := NewWebsocketServer(db, chain, mempool, txCache, metrics, is)
if err != nil {
return nil, err
}

View File

@ -123,7 +123,7 @@ func setupPublicHTTPServer(parser bchain.BlockChainParser, chain bchain.BlockCha
}
// s.Run is never called, binding can be to any port
s, err := NewPublicServer("localhost:12345", "", d, chain, mempool, txCache, "", metrics, is, false, false)
s, err := NewPublicServer("localhost:12345", "", d, chain, mempool, txCache, "", metrics, is, false)
if err != nil {
t.Fatal(err)
}
@ -1443,7 +1443,7 @@ func websocketTestsBitcoinType(t *testing.T, ts *httptest.Server) {
"id": "00000000eb0443fd7dc4a1ed5c686a8e995057805f9a161d9a5a77a95e72b7b6",
},
},
want: `{"id":"40","data":{"page":1,"totalPages":1,"itemsOnPage":100000,"hash":"00000000eb0443fd7dc4a1ed5c686a8e995057805f9a161d9a5a77a95e72b7b6","previousBlockHash":"0000000076fbbed90fd75b0e18856aa35baa984e9c9d444cf746ad85e94e2997","height":225494,"confirmations":1,"size":2345678,"time":1521595678,"version":0,"merkleRoot":"","nonce":"","bits":"","difficulty":"","txCount":4,"txs":[{"txid":"7c3be24063f268aaa1ed81b64776798f56088757641a34fb156c4f51ed2e9d25","vin":[{"n":0,"addresses":["mv9uLThosiEnGRbVPS7Vhyw6VssbVRsiAw"],"isAddress":true,"value":"1234567890123"},{"n":1,"addresses":["mtGXQvBowMkBpnhLckhxhbwYK44Gs9eEtz"],"isAddress":true,"value":"12345"}],"vout":[{"value":"317283951061","n":0,"spent":true,"addresses":["mzB8cYrfRwFRFAGTDzV8LkUQy5BQicxGhX"],"isAddress":true},{"value":"917283951061","n":1,"addresses":["mtR97eM2HPWVM6c8FGLGcukgaHHQv7THoL"],"isAddress":true},{"value":"0","n":2,"addresses":["OP_RETURN 2020f1686f6a20"],"isAddress":false}],"blockHash":"00000000eb0443fd7dc4a1ed5c686a8e995057805f9a161d9a5a77a95e72b7b6","blockHeight":225494,"confirmations":1,"blockTime":1521595678,"value":"1234567902122","valueIn":"1234567902468","fees":"346"},{"txid":"3d90d15ed026dc45e19ffb52875ed18fa9e8012ad123d7f7212176e2b0ebdb71","vin":[{"n":0,"addresses":["mzB8cYrfRwFRFAGTDzV8LkUQy5BQicxGhX"],"isAddress":true,"value":"317283951061"},{"n":1,"addresses":["2MzmAKayJmja784jyHvRUW1bXPget1csRRG"],"isAddress":true,"value":"1"}],"vout":[{"value":"118641975500","n":0,"addresses":["2N6utyMZfPNUb1Bk8oz7p2JqJrXkq83gegu"],"isAddress":true},{"value":"198641975500","n":1,"addresses":["mmJx9Y8ayz9h14yd9fgCW1bUKoEpkBAquP"],"isAddress":true}],"blockHash":"00000000eb0443fd7dc4a1ed5c686a8e995057805f9a161d9a5a77a95e72b7b6","blockHeight":225494,"confirmations":1,"blockTime":1521595678,"value":"317283951000","valueIn":"317283951062","fees":"62"},{"txid":"05e2e48aeabdd9b75def7b48d756ba304713c2aba7b522bf9dbc893fc4231b07","vin":[{"n":0,"addresses":["2NEVv9LJmAnY99W1pFoc5UJjVdypBqdnvu1"],"isAddress":true,"value":"9876"}],"vout":[{"value":"9000","n":0,"addresses":["2NEVv9LJmAnY99W1pFoc5UJjVdypBqdnvu1"],"isAddress":true}],"blockHash":"00000000eb0443fd7dc4a1ed5c686a8e995057805f9a161d9a5a77a95e72b7b6","blockHeight":225494,"confirmations":1,"blockTime":1521595678,"value":"9000","valueIn":"9876","fees":"876"},{"txid":"fdd824a780cbb718eeb766eb05d83fdefc793a27082cd5e67f856d69798cf7db","vin":[{"n":0,"isAddress":false,"value":"0"}],"vout":[{"value":"1360030331","n":0,"addresses":["mzVznVsCHkVHX9UN8WPFASWUUHtxnNn4Jj"],"isAddress":true},{"value":"0","n":1,"addresses":[],"isAddress":false}],"blockHash":"00000000eb0443fd7dc4a1ed5c686a8e995057805f9a161d9a5a77a95e72b7b6","blockHeight":225494,"confirmations":1,"blockTime":1521595678,"value":"1360030331","valueIn":"0","fees":"0"}]}}`,
want: `{"id":"40","data":{"error":{"message":"Not supported"}}}`,
},
}

View File

@ -86,7 +86,7 @@ type WebsocketServer struct {
}
// NewWebsocketServer creates new websocket interface to blockbook and returns its handle
func NewWebsocketServer(db *db.RocksDB, chain bchain.BlockChain, mempool bchain.Mempool, txCache *db.TxCache, metrics *common.Metrics, is *common.InternalState, enableSubNewTx bool) (*WebsocketServer, error) {
func NewWebsocketServer(db *db.RocksDB, chain bchain.BlockChain, mempool bchain.Mempool, txCache *db.TxCache, metrics *common.Metrics, is *common.InternalState) (*WebsocketServer, error) {
api, err := api.NewWorker(db, chain, mempool, txCache, metrics, is)
if err != nil {
return nil, err
@ -111,7 +111,7 @@ func NewWebsocketServer(db *db.RocksDB, chain bchain.BlockChain, mempool bchain.
api: api,
block0hash: b0,
newBlockSubscriptions: make(map[*websocketChannel]string),
newTransactionEnabled: enableSubNewTx,
newTransactionEnabled: is.EnableSubNewTx,
newTransactionSubscriptions: make(map[*websocketChannel]string),
addressSubscriptions: make(map[string]map[*websocketChannel]string),
fiatRatesSubscriptions: make(map[string]map[*websocketChannel]string),
@ -292,12 +292,20 @@ var requestHandlers = map[string]func(*WebsocketServer, *websocketChannel, *webs
return
},
"getBlock": func(s *WebsocketServer, c *websocketChannel, req *websocketReq) (rv interface{}, err error) {
if !s.is.ExtendedIndex {
return nil, errors.New("Not supported")
}
r := struct {
Id string `json:"id"`
Id string `json:"id"`
PageSize int `json:"pageSize"`
Page int `json:"page"`
}{}
err = json.Unmarshal(req.Params, &r)
if r.PageSize == 0 {
r.PageSize = 1000000
}
if err == nil {
rv, err = s.getBlock(r.Id)
rv, err = s.getBlock(r.Id, r.Page, r.PageSize)
}
return
},
@ -626,8 +634,8 @@ func (s *WebsocketServer) getBlockHash(height int) (interface{}, error) {
}, nil
}
func (s *WebsocketServer) getBlock(id string) (interface{}, error) {
block, err := s.api.GetBlock(id, 0, 100000)
func (s *WebsocketServer) getBlock(id string, page, pageSize int) (interface{}, error) {
block, err := s.api.GetBlock(id, page, pageSize)
if err != nil {
return nil, err
}

View File

@ -1,5 +1,6 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
@ -7,7 +8,8 @@
<style>
.row {
margin-top: 1%;
}
}
::placeholder {
color: #ccc !important;
}
@ -99,7 +101,7 @@
}
function paramAsArray(name) {
const p = document.getElementById(name).value;
if(p) {
if (p) {
return p.split(",").map(s => s.trim());
}
}
@ -136,9 +138,13 @@
function getBlock() {
const method = "getBlock";
const id = document.getElementById("getBlockId").value;
const page = parseInt(document.getElementById("getBlockPage").value);
const pageSize = parseInt(document.getElementById("getBlockPageSize").value);
const params = {
id,
};
if (page) params.page = page;
if (pageSize) params.pageSize = pageSize;
send(method, params, function (result) {
document.getElementById("getBlockResult").innerText = JSON.stringify(result).replace(/,/g, ", ");
});
@ -472,7 +478,11 @@
<input class="btn btn-secondary" type="button" value="getBlock" onclick="getBlock()">
</div>
<div class="col-8">
<input type="text" class="form-control" placeholder="height/hash" id="getBlockId" value="">
<div class="row" style="margin: 0;">
<input type="text" class="form-control" placeholder="height/hash" style="width: 68%; margin-right: 5px;" id="getBlockId" value="">
<input type="text" class="form-control" placeholder="page" style="width: 15%; margin-right: 5px;" id="getBlockPage" value="">
<input type="text" class="form-control" placeholder="page size" style="width: 15%;" id="getBlockPageSize" value="">
</div>
</div>
<div class="col">
</div>