github.com/Steality/go-ethereum@v1.9.7/core/rawdb/accessors_metadata.go (about)

     1  // Copyright 2018 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  	"encoding/json"
    21  
    22  	"github.com/ethereum/go-ethereum/common"
    23  	"github.com/ethereum/go-ethereum/ethdb"
    24  	"github.com/ethereum/go-ethereum/log"
    25  	"github.com/ethereum/go-ethereum/params"
    26  	"github.com/ethereum/go-ethereum/rlp"
    27  )
    28  
    29  // ReadDatabaseVersion retrieves the version number of the database.
    30  func ReadDatabaseVersion(db ethdb.KeyValueReader) *uint64 {
    31  	var version uint64
    32  
    33  	enc, _ := db.Get(databaseVerisionKey)
    34  	if len(enc) == 0 {
    35  		return nil
    36  	}
    37  	if err := rlp.DecodeBytes(enc, &version); err != nil {
    38  		return nil
    39  	}
    40  
    41  	return &version
    42  }
    43  
    44  // WriteDatabaseVersion stores the version number of the database
    45  func WriteDatabaseVersion(db ethdb.KeyValueWriter, version uint64) {
    46  	enc, err := rlp.EncodeToBytes(version)
    47  	if err != nil {
    48  		log.Crit("Failed to encode database version", "err", err)
    49  	}
    50  	if err = db.Put(databaseVerisionKey, enc); err != nil {
    51  		log.Crit("Failed to store the database version", "err", err)
    52  	}
    53  }
    54  
    55  // ReadChainConfig retrieves the consensus settings based on the given genesis hash.
    56  func ReadChainConfig(db ethdb.KeyValueReader, hash common.Hash) *params.ChainConfig {
    57  	data, _ := db.Get(configKey(hash))
    58  	if len(data) == 0 {
    59  		return nil
    60  	}
    61  	var config params.ChainConfig
    62  	if err := json.Unmarshal(data, &config); err != nil {
    63  		log.Error("Invalid chain config JSON", "hash", hash, "err", err)
    64  		return nil
    65  	}
    66  	return &config
    67  }
    68  
    69  // WriteChainConfig writes the chain config settings to the database.
    70  func WriteChainConfig(db ethdb.KeyValueWriter, hash common.Hash, cfg *params.ChainConfig) {
    71  	if cfg == nil {
    72  		return
    73  	}
    74  	data, err := json.Marshal(cfg)
    75  	if err != nil {
    76  		log.Crit("Failed to JSON encode chain config", "err", err)
    77  	}
    78  	if err := db.Put(configKey(hash), data); err != nil {
    79  		log.Crit("Failed to store chain config", "err", err)
    80  	}
    81  }
    82  
    83  // ReadPreimage retrieves a single preimage of the provided hash.
    84  func ReadPreimage(db ethdb.KeyValueReader, hash common.Hash) []byte {
    85  	data, _ := db.Get(preimageKey(hash))
    86  	return data
    87  }
    88  
    89  // WritePreimages writes the provided set of preimages to the database.
    90  func WritePreimages(db ethdb.KeyValueWriter, preimages map[common.Hash][]byte) {
    91  	for hash, preimage := range preimages {
    92  		if err := db.Put(preimageKey(hash), preimage); err != nil {
    93  			log.Crit("Failed to store trie preimage", "err", err)
    94  		}
    95  	}
    96  	preimageCounter.Inc(int64(len(preimages)))
    97  	preimageHitCounter.Inc(int64(len(preimages)))
    98  }