github.com/ccm-chain/ccmchain@v1.0.0/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/ccm-chain/ccmchain/common"
    25  	"github.com/ccm-chain/ccmchain/common/prque"
    26  	"github.com/ccm-chain/ccmchain/database"
    27  	"github.com/ccm-chain/ccmchain/log"
    28  	"github.com/ccm-chain/ccmchain/rlp"
    29  	"golang.org/x/crypto/sha3"
    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 database.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() > database.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
    88  func iterateTransactions(db database.Database, from uint64, to uint64, reverse bool) (chan *blockTxHashes, chan struct{}) {
    89  	// One thread sequentially reads data from db
    90  	type numberRlp struct {
    91  		number uint64
    92  		rlp    rlp.RawValue
    93  	}
    94  	if to == from {
    95  		return nil, nil
    96  	}
    97  	threads := to - from
    98  	if cpus := runtime.NumCPU(); threads > uint64(cpus) {
    99  		threads = uint64(cpus)
   100  	}
   101  	var (
   102  		rlpCh    = make(chan *numberRlp, threads*2)     // we send raw rlp over this channel
   103  		hashesCh = make(chan *blockTxHashes, threads*2) // send hashes over hashesCh
   104  		abortCh  = make(chan struct{})
   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 <-abortCh:
   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  
   138  		var hasher = sha3.NewLegacyKeccak256()
   139  		for data := range rlpCh {
   140  			it, err := rlp.NewListIterator(data.rlp)
   141  			if err != nil {
   142  				log.Warn("tx iteration error", "error", err)
   143  				return
   144  			}
   145  			it.Next()
   146  			txs := it.Value()
   147  			txIt, err := rlp.NewListIterator(txs)
   148  			if err != nil {
   149  				log.Warn("tx iteration error", "error", err)
   150  				return
   151  			}
   152  			var hashes []common.Hash
   153  			for txIt.Next() {
   154  				if err := txIt.Err(); err != nil {
   155  					log.Warn("tx iteration error", "error", err)
   156  					return
   157  				}
   158  				var txHash common.Hash
   159  				hasher.Reset()
   160  				hasher.Write(txIt.Value())
   161  				hasher.Sum(txHash[:0])
   162  				hashes = append(hashes, txHash)
   163  			}
   164  			result := &blockTxHashes{
   165  				hashes: hashes,
   166  				number: data.number,
   167  			}
   168  			// Feed the block to the aggregator, or abort on interrupt
   169  			select {
   170  			case hashesCh <- result:
   171  			case <-abortCh:
   172  				return
   173  			}
   174  		}
   175  	}
   176  	go lookup() // start the sequential db accessor
   177  	for i := 0; i < int(threads); i++ {
   178  		go process()
   179  	}
   180  	return hashesCh, abortCh
   181  }
   182  
   183  // IndexTransactions creates txlookup indices of the specified block range.
   184  //
   185  // This function iterates canonical chain in reverse order, it has one main advantage:
   186  // We can write tx index tail flag periodically even without the whole indexing
   187  // procedure is finished. So that we can resume indexing procedure next time quickly.
   188  func IndexTransactions(db database.Database, from uint64, to uint64) {
   189  	// short circuit for invalid range
   190  	if from >= to {
   191  		return
   192  	}
   193  	var (
   194  		hashesCh, abortCh = iterateTransactions(db, from, to, true)
   195  		batch             = db.NewBatch()
   196  		start             = time.Now()
   197  		logged            = start.Add(-7 * time.Second)
   198  		//  Since we iterate in reverse, we expect the first number to come
   199  		// in to be [to-1]. Therefore, setting lastNum to means that the
   200  		// prqueue gap-evaluation will work correctly
   201  		lastNum = to
   202  		queue   = prque.New(nil)
   203  		// for stats reporting
   204  		blocks, txs = 0, 0
   205  	)
   206  	defer close(abortCh)
   207  
   208  	for chanDelivery := range hashesCh {
   209  		// Push the delivery into the queue and process contiguous ranges.
   210  		// Since we iterate in reverse, so lower numbers have lower prio, and
   211  		// we can use the number directly as prio marker
   212  		queue.Push(chanDelivery, int64(chanDelivery.number))
   213  		for !queue.Empty() {
   214  			// If the next available item is gapped, return
   215  			if _, priority := queue.Peek(); priority != int64(lastNum-1) {
   216  				break
   217  			}
   218  			// Next block available, pop it off and index it
   219  			delivery := queue.PopItem().(*blockTxHashes)
   220  			lastNum = delivery.number
   221  			WriteTxLookupEntriesByHash(batch, delivery.number, delivery.hashes)
   222  			blocks++
   223  			txs += len(delivery.hashes)
   224  			// If enough data was accumulated in memory or we're at the last block, dump to disk
   225  			if batch.ValueSize() > database.IdealBatchSize {
   226  				// Also write the tail there
   227  				WriteTxIndexTail(batch, lastNum)
   228  				if err := batch.Write(); err != nil {
   229  					log.Crit("Failed writing batch to db", "error", err)
   230  					return
   231  				}
   232  				batch.Reset()
   233  			}
   234  			// If we've spent too much time already, notify the user of what we're doing
   235  			if time.Since(logged) > 8*time.Second {
   236  				log.Info("Indexing transactions", "blocks", blocks, "txs", txs, "tail", lastNum, "total", to-from, "elapsed", common.PrettyDuration(time.Since(start)))
   237  				logged = time.Now()
   238  			}
   239  		}
   240  	}
   241  	if lastNum < to {
   242  		WriteTxIndexTail(batch, lastNum)
   243  		// No need to write the batch if we never entered the loop above...
   244  		if err := batch.Write(); err != nil {
   245  			log.Crit("Failed writing batch to db", "error", err)
   246  			return
   247  		}
   248  	}
   249  	log.Info("Indexed transactions", "blocks", blocks, "txs", txs, "tail", lastNum, "elapsed", common.PrettyDuration(time.Since(start)))
   250  }
   251  
   252  // UnindexTransactions removes txlookup indices of the specified block range.
   253  func UnindexTransactions(db database.Database, from uint64, to uint64) {
   254  	// short circuit for invalid range
   255  	if from >= to {
   256  		return
   257  	}
   258  	// Write flag first and then unindex the transaction indices. Some indices
   259  	// will be left in the database if crash happens but it's fine.
   260  	WriteTxIndexTail(db, to)
   261  	// If only one block is unindexed, do it directly
   262  	//if from+1 == to {
   263  	//	data := ReadCanonicalBodyRLP(db, uint64(from))
   264  	//	DeleteTxLookupEntries(db, ReadBlock(db, ReadCanonicalHash(db, from), from))
   265  	//	log.Info("Unindexed transactions", "blocks", 1, "tail", to)
   266  	//	return
   267  	//}
   268  	// TODO @holiman, add this back (if we want it)
   269  	var (
   270  		hashesCh, abortCh = iterateTransactions(db, from, to, false)
   271  		batch             = db.NewBatch()
   272  		start             = time.Now()
   273  		logged            = start.Add(-7 * time.Second)
   274  	)
   275  	defer close(abortCh)
   276  	// Otherwise spin up the concurrent iterator and unindexer
   277  	blocks, txs := 0, 0
   278  	for delivery := range hashesCh {
   279  		DeleteTxLookupEntriesByHash(batch, delivery.hashes)
   280  		txs += len(delivery.hashes)
   281  		blocks++
   282  
   283  		// If enough data was accumulated in memory or we're at the last block, dump to disk
   284  		// A batch counts the size of deletion as '1', so we need to flush more
   285  		// often than that.
   286  		if blocks%1000 == 0 {
   287  			if err := batch.Write(); err != nil {
   288  				log.Crit("Failed writing batch to db", "error", err)
   289  				return
   290  			}
   291  			batch.Reset()
   292  		}
   293  		// If we've spent too much time already, notify the user of what we're doing
   294  		if time.Since(logged) > 8*time.Second {
   295  			log.Info("Unindexing transactions", "blocks", blocks, "txs", txs, "total", to-from, "elapsed", common.PrettyDuration(time.Since(start)))
   296  			logged = time.Now()
   297  		}
   298  	}
   299  	if err := batch.Write(); err != nil {
   300  		log.Crit("Failed writing batch to db", "error", err)
   301  		return
   302  	}
   303  	log.Info("Unindexed transactions", "blocks", blocks, "txs", txs, "tail", to, "elapsed", common.PrettyDuration(time.Since(start)))
   304  }