github.com/ethereum/go-ethereum@v1.16.1/core/rawdb/ancient_scheme.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  	"path/filepath"
    21  
    22  	"github.com/ethereum/go-ethereum/ethdb"
    23  )
    24  
    25  // The list of table names of chain freezer.
    26  const (
    27  	// ChainFreezerHeaderTable indicates the name of the freezer header table.
    28  	ChainFreezerHeaderTable = "headers"
    29  
    30  	// ChainFreezerHashTable indicates the name of the freezer canonical hash table.
    31  	ChainFreezerHashTable = "hashes"
    32  
    33  	// ChainFreezerBodiesTable indicates the name of the freezer block body table.
    34  	ChainFreezerBodiesTable = "bodies"
    35  
    36  	// ChainFreezerReceiptTable indicates the name of the freezer receipts table.
    37  	ChainFreezerReceiptTable = "receipts"
    38  )
    39  
    40  // chainFreezerTableConfigs configures the settings for tables in the chain freezer.
    41  // Compression is disabled for hashes as they don't compress well. Additionally,
    42  // tail truncation is disabled for the header and hash tables, as these are intended
    43  // to be retained long-term.
    44  var chainFreezerTableConfigs = map[string]freezerTableConfig{
    45  	ChainFreezerHeaderTable:  {noSnappy: false, prunable: false},
    46  	ChainFreezerHashTable:    {noSnappy: true, prunable: false},
    47  	ChainFreezerBodiesTable:  {noSnappy: false, prunable: true},
    48  	ChainFreezerReceiptTable: {noSnappy: false, prunable: true},
    49  }
    50  
    51  // freezerTableConfig contains the settings for a freezer table.
    52  type freezerTableConfig struct {
    53  	noSnappy bool // disables item compression
    54  	prunable bool // true for tables that can be pruned by TruncateTail
    55  }
    56  
    57  const (
    58  	// stateHistoryTableSize defines the maximum size of freezer data files.
    59  	stateHistoryTableSize = 2 * 1000 * 1000 * 1000
    60  
    61  	// stateHistoryAccountIndex indicates the name of the freezer state history table.
    62  	stateHistoryMeta         = "history.meta"
    63  	stateHistoryAccountIndex = "account.index"
    64  	stateHistoryStorageIndex = "storage.index"
    65  	stateHistoryAccountData  = "account.data"
    66  	stateHistoryStorageData  = "storage.data"
    67  )
    68  
    69  // stateFreezerTableConfigs configures the settings for tables in the state freezer.
    70  var stateFreezerTableConfigs = map[string]freezerTableConfig{
    71  	stateHistoryMeta:         {noSnappy: true, prunable: true},
    72  	stateHistoryAccountIndex: {noSnappy: false, prunable: true},
    73  	stateHistoryStorageIndex: {noSnappy: false, prunable: true},
    74  	stateHistoryAccountData:  {noSnappy: false, prunable: true},
    75  	stateHistoryStorageData:  {noSnappy: false, prunable: true},
    76  }
    77  
    78  // The list of identifiers of ancient stores.
    79  var (
    80  	ChainFreezerName       = "chain"        // the folder name of chain segment ancient store.
    81  	MerkleStateFreezerName = "state"        // the folder name of state history ancient store.
    82  	VerkleStateFreezerName = "state_verkle" // the folder name of state history ancient store.
    83  )
    84  
    85  // freezers the collections of all builtin freezers.
    86  var freezers = []string{ChainFreezerName, MerkleStateFreezerName, VerkleStateFreezerName}
    87  
    88  // NewStateFreezer initializes the ancient store for state history.
    89  //
    90  //   - if the empty directory is given, initializes the pure in-memory
    91  //     state freezer (e.g. dev mode).
    92  //   - if non-empty directory is given, initializes the regular file-based
    93  //     state freezer.
    94  func NewStateFreezer(ancientDir string, verkle bool, readOnly bool) (ethdb.ResettableAncientStore, error) {
    95  	if ancientDir == "" {
    96  		return NewMemoryFreezer(readOnly, stateFreezerTableConfigs), nil
    97  	}
    98  	var name string
    99  	if verkle {
   100  		name = filepath.Join(ancientDir, VerkleStateFreezerName)
   101  	} else {
   102  		name = filepath.Join(ancientDir, MerkleStateFreezerName)
   103  	}
   104  	return newResettableFreezer(name, "eth/db/state", readOnly, stateHistoryTableSize, stateFreezerTableConfigs)
   105  }