gitlab.com/flarenetwork/coreth@v0.1.1/core/rawdb/accessors_metadata.go (about)

     1  // (c) 2019-2020, Ava Labs, Inc.
     2  //
     3  // This file is a derived work, based on the go-ethereum library whose original
     4  // notices appear below.
     5  //
     6  // It is distributed under a license compatible with the licensing terms of the
     7  // original code from which it is derived.
     8  //
     9  // Much love to the original authors for their work.
    10  // **********
    11  // Copyright 2018 The go-ethereum Authors
    12  // This file is part of the go-ethereum library.
    13  //
    14  // The go-ethereum library is free software: you can redistribute it and/or modify
    15  // it under the terms of the GNU Lesser General Public License as published by
    16  // the Free Software Foundation, either version 3 of the License, or
    17  // (at your option) any later version.
    18  //
    19  // The go-ethereum library is distributed in the hope that it will be useful,
    20  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    21  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    22  // GNU Lesser General Public License for more details.
    23  //
    24  // You should have received a copy of the GNU Lesser General Public License
    25  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    26  
    27  package rawdb
    28  
    29  import (
    30  	"encoding/json"
    31  
    32  	"github.com/ethereum/go-ethereum/common"
    33  	"github.com/ethereum/go-ethereum/ethdb"
    34  	"github.com/ethereum/go-ethereum/log"
    35  	"github.com/ethereum/go-ethereum/rlp"
    36  	"gitlab.com/flarenetwork/coreth/params"
    37  )
    38  
    39  // ReadDatabaseVersion retrieves the version number of the database.
    40  func ReadDatabaseVersion(db ethdb.KeyValueReader) *uint64 {
    41  	var version uint64
    42  
    43  	enc, _ := db.Get(databaseVersionKey)
    44  	if len(enc) == 0 {
    45  		return nil
    46  	}
    47  	if err := rlp.DecodeBytes(enc, &version); err != nil {
    48  		return nil
    49  	}
    50  
    51  	return &version
    52  }
    53  
    54  // WriteDatabaseVersion stores the version number of the database
    55  func WriteDatabaseVersion(db ethdb.KeyValueWriter, version uint64) {
    56  	enc, err := rlp.EncodeToBytes(version)
    57  	if err != nil {
    58  		log.Crit("Failed to encode database version", "err", err)
    59  	}
    60  	if err = db.Put(databaseVersionKey, enc); err != nil {
    61  		log.Crit("Failed to store the database version", "err", err)
    62  	}
    63  }
    64  
    65  // ReadChainConfig retrieves the consensus settings based on the given genesis hash.
    66  func ReadChainConfig(db ethdb.KeyValueReader, hash common.Hash) *params.ChainConfig {
    67  	data, _ := db.Get(configKey(hash))
    68  	if len(data) == 0 {
    69  		return nil
    70  	}
    71  	var config params.ChainConfig
    72  	if err := json.Unmarshal(data, &config); err != nil {
    73  		log.Error("Invalid chain config JSON", "hash", hash, "err", err)
    74  		return nil
    75  	}
    76  	return &config
    77  }
    78  
    79  // WriteChainConfig writes the chain config settings to the database.
    80  func WriteChainConfig(db ethdb.KeyValueWriter, hash common.Hash, cfg *params.ChainConfig) {
    81  	if cfg == nil {
    82  		return
    83  	}
    84  	data, err := json.Marshal(cfg)
    85  	if err != nil {
    86  		log.Crit("Failed to JSON encode chain config", "err", err)
    87  	}
    88  	if err := db.Put(configKey(hash), data); err != nil {
    89  		log.Crit("Failed to store chain config", "err", err)
    90  	}
    91  }