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