github.com/aidoskuneen/adk-node@v0.0.0-20220315131952-2e32567cb7f4/core/rawdb/chain_iterator.go (about)

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