github.com/phillinzzz/newBsc@v1.1.6/core/rawdb/chain_iterator.go (about) 1 // Copyright 2019 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum 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-ethereum 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-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package rawdb 18 19 import ( 20 "runtime" 21 "sync/atomic" 22 "time" 23 24 "github.com/phillinzzz/newBsc/common" 25 "github.com/phillinzzz/newBsc/common/gopool" 26 "github.com/phillinzzz/newBsc/common/prque" 27 "github.com/phillinzzz/newBsc/core/types" 28 "github.com/phillinzzz/newBsc/ethdb" 29 "github.com/phillinzzz/newBsc/log" 30 "github.com/phillinzzz/newBsc/rlp" 31 ) 32 33 // InitDatabaseFromFreezer reinitializes an empty database from a previous batch 34 // of frozen ancient blocks. The method iterates over all the frozen blocks and 35 // injects into the database the block hash->number mappings. 36 func InitDatabaseFromFreezer(db ethdb.Database) { 37 // If we can't access the freezer or it's empty, abort 38 frozen, err := db.Ancients() 39 if err != nil || frozen == 0 { 40 return 41 } 42 var ( 43 batch = db.NewBatch() 44 start = time.Now() 45 logged = start.Add(-7 * time.Second) // Unindex during import is fast, don't double log 46 hash common.Hash 47 ) 48 for i := uint64(0); i < frozen; i++ { 49 // Since the freezer has all data in sequential order on a file, 50 // it would be 'neat' to read more data in one go, and let the 51 // freezerdb return N items (e.g up to 1000 items per go) 52 // That would require an API change in Ancients though 53 if h, err := db.Ancient(freezerHashTable, i); err != nil { 54 log.Crit("Failed to init database from freezer", "err", err) 55 } else { 56 hash = common.BytesToHash(h) 57 } 58 WriteHeaderNumber(batch, hash, i) 59 // If enough data was accumulated in memory or we're at the last block, dump to disk 60 if batch.ValueSize() > ethdb.IdealBatchSize { 61 if err := batch.Write(); err != nil { 62 log.Crit("Failed to write data to db", "err", err) 63 } 64 batch.Reset() 65 } 66 // If we've spent too much time already, notify the user of what we're doing 67 if time.Since(logged) > 8*time.Second { 68 log.Info("Initializing database from freezer", "total", frozen, "number", i, "hash", hash, "elapsed", common.PrettyDuration(time.Since(start))) 69 logged = time.Now() 70 } 71 } 72 if err := batch.Write(); err != nil { 73 log.Crit("Failed to write data to db", "err", err) 74 } 75 batch.Reset() 76 77 WriteHeadHeaderHash(db, hash) 78 WriteHeadFastBlockHash(db, hash) 79 log.Info("Initialized database from freezer", "blocks", frozen, "elapsed", common.PrettyDuration(time.Since(start))) 80 } 81 82 type blockTxHashes struct { 83 number uint64 84 hashes []common.Hash 85 } 86 87 // iterateTransactions iterates over all transactions in the (canon) block 88 // number(s) given, and yields the hashes on a channel. If there is a signal 89 // received from interrupt channel, the iteration will be aborted and result 90 // channel will be closed. 91 func iterateTransactions(db ethdb.Database, from uint64, to uint64, reverse bool, interrupt chan struct{}) chan *blockTxHashes { 92 // One thread sequentially reads data from db 93 type numberRlp struct { 94 number uint64 95 rlp rlp.RawValue 96 } 97 if to == from { 98 return nil 99 } 100 threads := to - from 101 if cpus := runtime.NumCPU(); threads > uint64(cpus) { 102 threads = uint64(cpus) 103 } 104 var ( 105 rlpCh = make(chan *numberRlp, threads*2) // we send raw rlp over this channel 106 hashesCh = make(chan *blockTxHashes, threads*2) // send hashes over hashesCh 107 ) 108 // lookup runs in one instance 109 lookup := func() { 110 n, end := from, to 111 if reverse { 112 n, end = to-1, from-1 113 } 114 defer close(rlpCh) 115 for n != end { 116 data := ReadCanonicalBodyRLP(db, n) 117 // Feed the block to the aggregator, or abort on interrupt 118 select { 119 case rlpCh <- &numberRlp{n, data}: 120 case <-interrupt: 121 return 122 } 123 if reverse { 124 n-- 125 } else { 126 n++ 127 } 128 } 129 } 130 // process runs in parallel 131 nThreadsAlive := int32(threads) 132 process := func() { 133 defer func() { 134 // Last processor closes the result channel 135 if atomic.AddInt32(&nThreadsAlive, -1) == 0 { 136 close(hashesCh) 137 } 138 }() 139 for data := range rlpCh { 140 var body types.Body 141 if err := rlp.DecodeBytes(data.rlp, &body); err != nil { 142 log.Warn("Failed to decode block body", "block", data.number, "error", err) 143 return 144 } 145 var hashes []common.Hash 146 for _, tx := range body.Transactions { 147 hashes = append(hashes, tx.Hash()) 148 } 149 result := &blockTxHashes{ 150 hashes: hashes, 151 number: data.number, 152 } 153 // Feed the block to the aggregator, or abort on interrupt 154 select { 155 case hashesCh <- result: 156 case <-interrupt: 157 return 158 } 159 } 160 } 161 go lookup() // start the sequential db accessor 162 for i := 0; i < int(threads); i++ { 163 gopool.Submit(func() { 164 process() 165 }) 166 } 167 return hashesCh 168 } 169 170 // indexTransactions creates txlookup indices of the specified block range. 171 // 172 // This function iterates canonical chain in reverse order, it has one main advantage: 173 // We can write tx index tail flag periodically even without the whole indexing 174 // procedure is finished. So that we can resume indexing procedure next time quickly. 175 // 176 // There is a passed channel, the whole procedure will be interrupted if any 177 // signal received. 178 func indexTransactions(db ethdb.Database, from uint64, to uint64, interrupt chan struct{}, hook func(uint64) bool) { 179 // short circuit for invalid range 180 if from >= to { 181 return 182 } 183 var ( 184 hashesCh = iterateTransactions(db, from, to, true, interrupt) 185 batch = db.NewBatch() 186 start = time.Now() 187 logged = start.Add(-7 * time.Second) 188 // Since we iterate in reverse, we expect the first number to come 189 // in to be [to-1]. Therefore, setting lastNum to means that the 190 // prqueue gap-evaluation will work correctly 191 lastNum = to 192 queue = prque.New(nil) 193 // for stats reporting 194 blocks, txs = 0, 0 195 ) 196 for chanDelivery := range hashesCh { 197 // Push the delivery into the queue and process contiguous ranges. 198 // Since we iterate in reverse, so lower numbers have lower prio, and 199 // we can use the number directly as prio marker 200 queue.Push(chanDelivery, int64(chanDelivery.number)) 201 for !queue.Empty() { 202 // If the next available item is gapped, return 203 if _, priority := queue.Peek(); priority != int64(lastNum-1) { 204 break 205 } 206 // For testing 207 if hook != nil && !hook(lastNum-1) { 208 break 209 } 210 // Next block available, pop it off and index it 211 delivery := queue.PopItem().(*blockTxHashes) 212 lastNum = delivery.number 213 WriteTxLookupEntries(batch, delivery.number, delivery.hashes) 214 blocks++ 215 txs += len(delivery.hashes) 216 // If enough data was accumulated in memory or we're at the last block, dump to disk 217 if batch.ValueSize() > ethdb.IdealBatchSize { 218 WriteTxIndexTail(batch, lastNum) // Also write the tail here 219 if err := batch.Write(); err != nil { 220 log.Crit("Failed writing batch to db", "error", err) 221 return 222 } 223 batch.Reset() 224 } 225 // If we've spent too much time already, notify the user of what we're doing 226 if time.Since(logged) > 8*time.Second { 227 log.Info("Indexing transactions", "blocks", blocks, "txs", txs, "tail", lastNum, "total", to-from, "elapsed", common.PrettyDuration(time.Since(start))) 228 logged = time.Now() 229 } 230 } 231 } 232 // Flush the new indexing tail and the last committed data. It can also happen 233 // that the last batch is empty because nothing to index, but the tail has to 234 // be flushed anyway. 235 WriteTxIndexTail(batch, lastNum) 236 if err := batch.Write(); err != nil { 237 log.Crit("Failed writing batch to db", "error", err) 238 return 239 } 240 select { 241 case <-interrupt: 242 log.Debug("Transaction indexing interrupted", "blocks", blocks, "txs", txs, "tail", lastNum, "elapsed", common.PrettyDuration(time.Since(start))) 243 default: 244 log.Info("Indexed transactions", "blocks", blocks, "txs", txs, "tail", lastNum, "elapsed", common.PrettyDuration(time.Since(start))) 245 } 246 } 247 248 // IndexTransactions creates txlookup indices of the specified block range. 249 // 250 // This function iterates canonical chain in reverse order, it has one main advantage: 251 // We can write tx index tail flag periodically even without the whole indexing 252 // procedure is finished. So that we can resume indexing procedure next time quickly. 253 // 254 // There is a passed channel, the whole procedure will be interrupted if any 255 // signal received. 256 func IndexTransactions(db ethdb.Database, from uint64, to uint64, interrupt chan struct{}) { 257 indexTransactions(db, from, to, interrupt, nil) 258 } 259 260 // indexTransactionsForTesting is the internal debug version with an additional hook. 261 func indexTransactionsForTesting(db ethdb.Database, from uint64, to uint64, interrupt chan struct{}, hook func(uint64) bool) { 262 indexTransactions(db, from, to, interrupt, hook) 263 } 264 265 // unindexTransactions removes txlookup indices of the specified block range. 266 // 267 // There is a passed channel, the whole procedure will be interrupted if any 268 // signal received. 269 func unindexTransactions(db ethdb.Database, from uint64, to uint64, interrupt chan struct{}, hook func(uint64) bool) { 270 // short circuit for invalid range 271 if from >= to { 272 return 273 } 274 var ( 275 hashesCh = iterateTransactions(db, from, to, false, interrupt) 276 batch = db.NewBatch() 277 start = time.Now() 278 logged = start.Add(-7 * time.Second) 279 // we expect the first number to come in to be [from]. Therefore, setting 280 // nextNum to from means that the prqueue gap-evaluation will work correctly 281 nextNum = from 282 queue = prque.New(nil) 283 // for stats reporting 284 blocks, txs = 0, 0 285 ) 286 // Otherwise spin up the concurrent iterator and unindexer 287 for delivery := range hashesCh { 288 // Push the delivery into the queue and process contiguous ranges. 289 queue.Push(delivery, -int64(delivery.number)) 290 for !queue.Empty() { 291 // If the next available item is gapped, return 292 if _, priority := queue.Peek(); -priority != int64(nextNum) { 293 break 294 } 295 // For testing 296 if hook != nil && !hook(nextNum) { 297 break 298 } 299 delivery := queue.PopItem().(*blockTxHashes) 300 nextNum = delivery.number + 1 301 DeleteTxLookupEntries(batch, delivery.hashes) 302 txs += len(delivery.hashes) 303 blocks++ 304 305 // If enough data was accumulated in memory or we're at the last block, dump to disk 306 // A batch counts the size of deletion as '1', so we need to flush more 307 // often than that. 308 if blocks%1000 == 0 { 309 WriteTxIndexTail(batch, nextNum) 310 if err := batch.Write(); err != nil { 311 log.Crit("Failed writing batch to db", "error", err) 312 return 313 } 314 batch.Reset() 315 } 316 // If we've spent too much time already, notify the user of what we're doing 317 if time.Since(logged) > 8*time.Second { 318 log.Info("Unindexing transactions", "blocks", blocks, "txs", txs, "total", to-from, "elapsed", common.PrettyDuration(time.Since(start))) 319 logged = time.Now() 320 } 321 } 322 } 323 // Flush the new indexing tail and the last committed data. It can also happen 324 // that the last batch is empty because nothing to unindex, but the tail has to 325 // be flushed anyway. 326 WriteTxIndexTail(batch, nextNum) 327 if err := batch.Write(); err != nil { 328 log.Crit("Failed writing batch to db", "error", err) 329 return 330 } 331 select { 332 case <-interrupt: 333 log.Debug("Transaction unindexing interrupted", "blocks", blocks, "txs", txs, "tail", to, "elapsed", common.PrettyDuration(time.Since(start))) 334 default: 335 log.Info("Unindexed transactions", "blocks", blocks, "txs", txs, "tail", to, "elapsed", common.PrettyDuration(time.Since(start))) 336 } 337 } 338 339 // UnindexTransactions removes txlookup indices of the specified block range. 340 // 341 // There is a passed channel, the whole procedure will be interrupted if any 342 // signal received. 343 func UnindexTransactions(db ethdb.Database, from uint64, to uint64, interrupt chan struct{}) { 344 unindexTransactions(db, from, to, interrupt, nil) 345 } 346 347 // unindexTransactionsForTesting is the internal debug version with an additional hook. 348 func unindexTransactionsForTesting(db ethdb.Database, from uint64, to uint64, interrupt chan struct{}, hook func(uint64) bool) { 349 unindexTransactions(db, from, to, interrupt, hook) 350 }