From adf50156bf744c298ad9d8733dae05fe5b15daab Mon Sep 17 00:00:00 2001 From: Martin Boehm Date: Fri, 3 Mar 2023 00:12:50 +0100 Subject: [PATCH] Enable websocket getBlock only for extended index --- blockbook.go | 7 ++++--- common/internalstate.go | 2 ++ server/public.go | 4 ++-- server/public_test.go | 4 ++-- server/websocket.go | 20 ++++++++++++++------ static/test-websocket.html | 16 +++++++++++++--- 6 files changed, 37 insertions(+), 16 deletions(-) diff --git a/blockbook.go b/blockbook.go index e9442f3b..eae21db3 100644 --- a/blockbook.go +++ b/blockbook.go @@ -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) diff --git a/common/internalstate.go b/common/internalstate.go index f12f4729..434683be 100644 --- a/common/internalstate.go +++ b/common/internalstate.go @@ -87,6 +87,8 @@ type InternalState struct { HistoricalTokenFiatRatesTime time.Time `json:"historicalTokenFiatRatesTime"` CurrentTicker *CurrencyRatesTicker `json:"currentTicker"` + EnableSubNewTx bool `json:"-"` + BackendInfo BackendInfo `json:"-"` } diff --git a/server/public.go b/server/public.go index cd57179c..fb6c9118 100644 --- a/server/public.go +++ b/server/public.go @@ -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 } diff --git a/server/public_test.go b/server/public_test.go index f8e76d7b..65871377 100644 --- a/server/public_test.go +++ b/server/public_test.go @@ -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"}}}`, }, } diff --git a/server/websocket.go b/server/websocket.go index 1afac62e..4ff5fd53 100644 --- a/server/websocket.go +++ b/server/websocket.go @@ -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 } diff --git a/static/test-websocket.html b/static/test-websocket.html index ac8d5733..471b2acb 100644 --- a/static/test-websocket.html +++ b/static/test-websocket.html @@ -1,5 +1,6 @@ + @@ -7,7 +8,8 @@