github.com/core-coin/go-core/v2@v2.1.9/xcb/bloombits.go (about) 1 // Copyright 2017 by the Authors 2 // This file is part of the go-core library. 3 // 4 // The go-core library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-core library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-core library. If not, see <http://www.gnu.org/licenses/>. 16 17 package xcb 18 19 import ( 20 "context" 21 "time" 22 23 "github.com/core-coin/go-core/v2/xcbdb" 24 25 "github.com/core-coin/go-core/v2/common" 26 "github.com/core-coin/go-core/v2/common/bitutil" 27 "github.com/core-coin/go-core/v2/core" 28 "github.com/core-coin/go-core/v2/core/bloombits" 29 "github.com/core-coin/go-core/v2/core/rawdb" 30 "github.com/core-coin/go-core/v2/core/types" 31 ) 32 33 const ( 34 // bloomServiceThreads is the number of goroutines used globally by an Core 35 // instance to service bloombits lookups for all running filters. 36 bloomServiceThreads = 16 37 38 // bloomFilterThreads is the number of goroutines used locally per filter to 39 // multiplex requests onto the global servicing goroutines. 40 bloomFilterThreads = 3 41 42 // bloomRetrievalBatch is the maximum number of bloom bit retrievals to service 43 // in a single batch. 44 bloomRetrievalBatch = 16 45 46 // bloomRetrievalWait is the maximum time to wait for enough bloom bit requests 47 // to accumulate request an entire batch (avoiding hysteresis). 48 bloomRetrievalWait = time.Duration(0) 49 ) 50 51 // startBloomHandlers starts a batch of goroutines to accept bloom bit database 52 // retrievals from possibly a range of filters and serving the data to satisfy. 53 func (xcb *Core) startBloomHandlers(sectionSize uint64) { 54 for i := 0; i < bloomServiceThreads; i++ { 55 go func() { 56 for { 57 select { 58 case <-xcb.closeBloomHandler: 59 return 60 61 case request := <-xcb.bloomRequests: 62 task := <-request 63 task.Bitsets = make([][]byte, len(task.Sections)) 64 for i, section := range task.Sections { 65 head := rawdb.ReadCanonicalHash(xcb.chainDb, (section+1)*sectionSize-1) 66 if compVector, err := rawdb.ReadBloomBits(xcb.chainDb, task.Bit, section, head); err == nil { 67 if blob, err := bitutil.DecompressBytes(compVector, int(sectionSize/8)); err == nil { 68 task.Bitsets[i] = blob 69 } else { 70 task.Error = err 71 } 72 } else { 73 task.Error = err 74 } 75 } 76 request <- task 77 } 78 } 79 }() 80 } 81 } 82 83 const ( 84 // bloomThrottling is the time to wait between processing two consecutive index 85 // sections. It's useful during chain upgrades to prevent disk overload. 86 bloomThrottling = 100 * time.Millisecond 87 ) 88 89 // BloomIndexer implements a core.ChainIndexer, building up a rotated bloom bits index 90 // for the Core header bloom filters, permitting blazing fast filtering. 91 type BloomIndexer struct { 92 size uint64 // section size to generate bloombits for 93 db xcbdb.Database // database instance to write index data and metadata into 94 gen *bloombits.Generator // generator to rotate the bloom bits crating the bloom index 95 section uint64 // Section is the section number being processed currently 96 head common.Hash // Head is the hash of the last header processed 97 } 98 99 // NewBloomIndexer returns a chain indexer that generates bloom bits data for the 100 // canonical chain for fast logs filtering. 101 func NewBloomIndexer(db xcbdb.Database, size, confirms uint64) *core.ChainIndexer { 102 backend := &BloomIndexer{ 103 db: db, 104 size: size, 105 } 106 table := rawdb.NewTable(db, string(rawdb.BloomBitsIndexPrefix)) 107 108 return core.NewChainIndexer(db, table, backend, size, confirms, bloomThrottling, "bloombits") 109 } 110 111 // Reset implements core.ChainIndexerBackend, starting a new bloombits index 112 // section. 113 func (b *BloomIndexer) Reset(ctx context.Context, section uint64, lastSectionHead common.Hash) error { 114 gen, err := bloombits.NewGenerator(uint(b.size)) 115 b.gen, b.section, b.head = gen, section, common.Hash{} 116 return err 117 } 118 119 // Process implements core.ChainIndexerBackend, adding a new header's bloom into 120 // the index. 121 func (b *BloomIndexer) Process(ctx context.Context, header *types.Header) error { 122 b.gen.AddBloom(uint(header.Number.Uint64()-b.section*b.size), header.Bloom) 123 b.head = header.Hash() 124 return nil 125 } 126 127 // Commit implements core.ChainIndexerBackend, finalizing the bloom section and 128 // writing it out into the database. 129 func (b *BloomIndexer) Commit() error { 130 batch := b.db.NewBatch() 131 for i := 0; i < types.BloomBitLength; i++ { 132 bits, err := b.gen.Bitset(uint(i)) 133 if err != nil { 134 return err 135 } 136 rawdb.WriteBloomBits(batch, uint(i), b.section, b.head, bitutil.CompressBytes(bits)) 137 } 138 return batch.Write() 139 } 140 141 // Prune returns an empty error since we don't support pruning here. 142 func (b *BloomIndexer) Prune(threshold uint64) error { 143 return nil 144 }