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