github.com/tacshi/go-ethereum@v0.0.0-20230616113857-84a434e20921/core/rawdb/chain_freezer.go (about)

     1  // Copyright 2022 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  	"fmt"
    21  	"sync"
    22  	"sync/atomic"
    23  	"time"
    24  
    25  	"github.com/tacshi/go-ethereum/common"
    26  	"github.com/tacshi/go-ethereum/ethdb"
    27  	"github.com/tacshi/go-ethereum/log"
    28  	"github.com/tacshi/go-ethereum/params"
    29  )
    30  
    31  const (
    32  	// freezerRecheckInterval is the frequency to check the key-value database for
    33  	// chain progression that might permit new blocks to be frozen into immutable
    34  	// storage.
    35  	freezerRecheckInterval = time.Minute
    36  
    37  	// freezerBatchLimit is the maximum number of blocks to freeze in one batch
    38  	// before doing an fsync and deleting it from the key-value store.
    39  	freezerBatchLimit = 30000
    40  )
    41  
    42  // chainFreezer is a wrapper of freezer with additional chain freezing feature.
    43  // The background thread will keep moving ancient chain segments from key-value
    44  // database to flat files for saving space on live database.
    45  type chainFreezer struct {
    46  	// WARNING: The `threshold` field is accessed atomically. On 32 bit platforms, only
    47  	// 64-bit aligned fields can be atomic. The struct is guaranteed to be so aligned,
    48  	// so take advantage of that (https://golang.org/pkg/sync/atomic/#pkg-note-BUG).
    49  	threshold uint64 // Number of recent blocks not to freeze (params.FullImmutabilityThreshold apart from tests)
    50  
    51  	*Freezer
    52  	quit    chan struct{}
    53  	wg      sync.WaitGroup
    54  	trigger chan chan struct{} // Manual blocking freeze trigger, test determinism
    55  }
    56  
    57  // newChainFreezer initializes the freezer for ancient chain data.
    58  func newChainFreezer(datadir string, namespace string, readonly bool) (*chainFreezer, error) {
    59  	freezer, err := NewChainFreezer(datadir, namespace, readonly)
    60  	if err != nil {
    61  		return nil, err
    62  	}
    63  	return &chainFreezer{
    64  		Freezer:   freezer,
    65  		threshold: params.FullImmutabilityThreshold,
    66  		quit:      make(chan struct{}),
    67  		trigger:   make(chan chan struct{}),
    68  	}, nil
    69  }
    70  
    71  // Close closes the chain freezer instance and terminates the background thread.
    72  func (f *chainFreezer) Close() error {
    73  	select {
    74  	case <-f.quit:
    75  	default:
    76  		close(f.quit)
    77  	}
    78  	f.wg.Wait()
    79  	return f.Freezer.Close()
    80  }
    81  
    82  // freeze is a background thread that periodically checks the blockchain for any
    83  // import progress and moves ancient data from the fast database into the freezer.
    84  //
    85  // This functionality is deliberately broken off from block importing to avoid
    86  // incurring additional data shuffling delays on block propagation.
    87  func (f *chainFreezer) freeze(db ethdb.KeyValueStore) {
    88  	var (
    89  		backoff   bool
    90  		triggered chan struct{} // Used in tests
    91  		nfdb      = &nofreezedb{KeyValueStore: db}
    92  	)
    93  	timer := time.NewTimer(freezerRecheckInterval)
    94  	defer timer.Stop()
    95  
    96  	for {
    97  		select {
    98  		case <-f.quit:
    99  			log.Info("Freezer shutting down")
   100  			return
   101  		default:
   102  		}
   103  		if backoff {
   104  			// If we were doing a manual trigger, notify it
   105  			if triggered != nil {
   106  				triggered <- struct{}{}
   107  				triggered = nil
   108  			}
   109  			select {
   110  			case <-timer.C:
   111  				backoff = false
   112  				timer.Reset(freezerRecheckInterval)
   113  			case triggered = <-f.trigger:
   114  				backoff = false
   115  			case <-f.quit:
   116  				return
   117  			}
   118  		}
   119  		// Retrieve the freezing threshold.
   120  		hash := ReadHeadBlockHash(nfdb)
   121  		if hash == (common.Hash{}) {
   122  			log.Debug("Current full block hash unavailable") // new chain, empty database
   123  			backoff = true
   124  			continue
   125  		}
   126  		number := ReadHeaderNumber(nfdb, hash)
   127  		threshold := atomic.LoadUint64(&f.threshold)
   128  		frozen := atomic.LoadUint64(&f.frozen)
   129  		switch {
   130  		case number == nil:
   131  			log.Error("Current full block number unavailable", "hash", hash)
   132  			backoff = true
   133  			continue
   134  
   135  		case *number < threshold:
   136  			log.Debug("Current full block not old enough", "number", *number, "hash", hash, "delay", threshold)
   137  			backoff = true
   138  			continue
   139  
   140  		case *number-threshold <= frozen:
   141  			log.Debug("Ancient blocks frozen already", "number", *number, "hash", hash, "frozen", frozen)
   142  			backoff = true
   143  			continue
   144  		}
   145  		head := ReadHeader(nfdb, hash, *number)
   146  		if head == nil {
   147  			log.Error("Current full block unavailable", "number", *number, "hash", hash)
   148  			backoff = true
   149  			continue
   150  		}
   151  
   152  		// Seems we have data ready to be frozen, process in usable batches
   153  		var (
   154  			start    = time.Now()
   155  			first, _ = f.Ancients()
   156  			limit    = *number - threshold
   157  		)
   158  		if limit-first > freezerBatchLimit {
   159  			limit = first + freezerBatchLimit
   160  		}
   161  		ancients, err := f.freezeRange(nfdb, first, limit)
   162  		if err != nil {
   163  			log.Error("Error in block freeze operation", "err", err)
   164  			backoff = true
   165  			continue
   166  		}
   167  
   168  		// Batch of blocks have been frozen, flush them before wiping from leveldb
   169  		if err := f.Sync(); err != nil {
   170  			log.Crit("Failed to flush frozen tables", "err", err)
   171  		}
   172  
   173  		// Wipe out all data from the active database
   174  		batch := db.NewBatch()
   175  		for i := 0; i < len(ancients); i++ {
   176  			// Always keep the genesis block in active database
   177  			if first+uint64(i) != 0 {
   178  				DeleteBlockWithoutNumber(batch, ancients[i], first+uint64(i))
   179  				DeleteCanonicalHash(batch, first+uint64(i))
   180  			}
   181  		}
   182  		if err := batch.Write(); err != nil {
   183  			log.Crit("Failed to delete frozen canonical blocks", "err", err)
   184  		}
   185  		batch.Reset()
   186  
   187  		// Wipe out side chains also and track dangling side chains
   188  		var dangling []common.Hash
   189  		frozen = atomic.LoadUint64(&f.frozen) // Needs reload after during freezeRange
   190  		for number := first; number < frozen; number++ {
   191  			// Always keep the genesis block in active database
   192  			if number != 0 {
   193  				dangling = ReadAllHashes(db, number)
   194  				for _, hash := range dangling {
   195  					log.Trace("Deleting side chain", "number", number, "hash", hash)
   196  					DeleteBlock(batch, hash, number)
   197  				}
   198  			}
   199  		}
   200  		if err := batch.Write(); err != nil {
   201  			log.Crit("Failed to delete frozen side blocks", "err", err)
   202  		}
   203  		batch.Reset()
   204  
   205  		// Step into the future and delete and dangling side chains
   206  		if frozen > 0 {
   207  			tip := frozen
   208  			for len(dangling) > 0 {
   209  				drop := make(map[common.Hash]struct{})
   210  				for _, hash := range dangling {
   211  					log.Debug("Dangling parent from Freezer", "number", tip-1, "hash", hash)
   212  					drop[hash] = struct{}{}
   213  				}
   214  				children := ReadAllHashes(db, tip)
   215  				for i := 0; i < len(children); i++ {
   216  					// Dig up the child and ensure it's dangling
   217  					child := ReadHeader(nfdb, children[i], tip)
   218  					if child == nil {
   219  						log.Error("Missing dangling header", "number", tip, "hash", children[i])
   220  						continue
   221  					}
   222  					if _, ok := drop[child.ParentHash]; !ok {
   223  						children = append(children[:i], children[i+1:]...)
   224  						i--
   225  						continue
   226  					}
   227  					// Delete all block data associated with the child
   228  					log.Debug("Deleting dangling block", "number", tip, "hash", children[i], "parent", child.ParentHash)
   229  					DeleteBlock(batch, children[i], tip)
   230  				}
   231  				dangling = children
   232  				tip++
   233  			}
   234  			if err := batch.Write(); err != nil {
   235  				log.Crit("Failed to delete dangling side blocks", "err", err)
   236  			}
   237  		}
   238  
   239  		// Log something friendly for the user
   240  		context := []interface{}{
   241  			"blocks", frozen - first, "elapsed", common.PrettyDuration(time.Since(start)), "number", frozen - 1,
   242  		}
   243  		if n := len(ancients); n > 0 {
   244  			context = append(context, []interface{}{"hash", ancients[n-1]}...)
   245  		}
   246  		log.Debug("Deep froze chain segment", context...)
   247  
   248  		// Avoid database thrashing with tiny writes
   249  		if frozen-first < freezerBatchLimit {
   250  			backoff = true
   251  		}
   252  	}
   253  }
   254  
   255  func (f *chainFreezer) freezeRange(nfdb *nofreezedb, number, limit uint64) (hashes []common.Hash, err error) {
   256  	hashes = make([]common.Hash, 0, limit-number)
   257  
   258  	_, err = f.ModifyAncients(func(op ethdb.AncientWriteOp) error {
   259  		for ; number <= limit; number++ {
   260  			// Retrieve all the components of the canonical block.
   261  			hash := ReadCanonicalHash(nfdb, number)
   262  			if hash == (common.Hash{}) {
   263  				return fmt.Errorf("canonical hash missing, can't freeze block %d", number)
   264  			}
   265  			header := ReadHeaderRLP(nfdb, hash, number)
   266  			if len(header) == 0 {
   267  				return fmt.Errorf("block header missing, can't freeze block %d", number)
   268  			}
   269  			body := ReadBodyRLP(nfdb, hash, number)
   270  			if len(body) == 0 {
   271  				return fmt.Errorf("block body missing, can't freeze block %d", number)
   272  			}
   273  			receipts := ReadReceiptsRLP(nfdb, hash, number)
   274  			if len(receipts) == 0 {
   275  				return fmt.Errorf("block receipts missing, can't freeze block %d", number)
   276  			}
   277  			td := ReadTdRLP(nfdb, hash, number)
   278  			if len(td) == 0 {
   279  				return fmt.Errorf("total difficulty missing, can't freeze block %d", number)
   280  			}
   281  
   282  			// Write to the batch.
   283  			if err := op.AppendRaw(ChainFreezerHashTable, number, hash[:]); err != nil {
   284  				return fmt.Errorf("can't write hash to Freezer: %v", err)
   285  			}
   286  			if err := op.AppendRaw(ChainFreezerHeaderTable, number, header); err != nil {
   287  				return fmt.Errorf("can't write header to Freezer: %v", err)
   288  			}
   289  			if err := op.AppendRaw(ChainFreezerBodiesTable, number, body); err != nil {
   290  				return fmt.Errorf("can't write body to Freezer: %v", err)
   291  			}
   292  			if err := op.AppendRaw(ChainFreezerReceiptTable, number, receipts); err != nil {
   293  				return fmt.Errorf("can't write receipts to Freezer: %v", err)
   294  			}
   295  			if err := op.AppendRaw(ChainFreezerDifficultyTable, number, td); err != nil {
   296  				return fmt.Errorf("can't write td to Freezer: %v", err)
   297  			}
   298  
   299  			hashes = append(hashes, hash)
   300  		}
   301  		return nil
   302  	})
   303  
   304  	return hashes, err
   305  }