github.com/theQRL/go-zond@v0.1.1/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  	"time"
    22  
    23  	"github.com/theQRL/go-zond/common"
    24  	"github.com/theQRL/go-zond/zonddb"
    25  	"github.com/theQRL/go-zond/log"
    26  	"github.com/theQRL/go-zond/params"
    27  	"github.com/theQRL/go-zond/rlp"
    28  )
    29  
    30  // ReadDatabaseVersion retrieves the version number of the database.
    31  func ReadDatabaseVersion(db zonddb.KeyValueReader) *uint64 {
    32  	var version uint64
    33  
    34  	enc, _ := db.Get(databaseVersionKey)
    35  	if len(enc) == 0 {
    36  		return nil
    37  	}
    38  	if err := rlp.DecodeBytes(enc, &version); err != nil {
    39  		return nil
    40  	}
    41  
    42  	return &version
    43  }
    44  
    45  // WriteDatabaseVersion stores the version number of the database
    46  func WriteDatabaseVersion(db zonddb.KeyValueWriter, version uint64) {
    47  	enc, err := rlp.EncodeToBytes(version)
    48  	if err != nil {
    49  		log.Crit("Failed to encode database version", "err", err)
    50  	}
    51  	if err = db.Put(databaseVersionKey, enc); err != nil {
    52  		log.Crit("Failed to store the database version", "err", err)
    53  	}
    54  }
    55  
    56  // ReadChainConfig retrieves the consensus settings based on the given genesis hash.
    57  func ReadChainConfig(db zonddb.KeyValueReader, hash common.Hash) *params.ChainConfig {
    58  	data, _ := db.Get(configKey(hash))
    59  	if len(data) == 0 {
    60  		return nil
    61  	}
    62  	var config params.ChainConfig
    63  	if err := json.Unmarshal(data, &config); err != nil {
    64  		log.Error("Invalid chain config JSON", "hash", hash, "err", err)
    65  		return nil
    66  	}
    67  	return &config
    68  }
    69  
    70  // WriteChainConfig writes the chain config settings to the database.
    71  func WriteChainConfig(db zonddb.KeyValueWriter, hash common.Hash, cfg *params.ChainConfig) {
    72  	if cfg == nil {
    73  		return
    74  	}
    75  	data, err := json.Marshal(cfg)
    76  	if err != nil {
    77  		log.Crit("Failed to JSON encode chain config", "err", err)
    78  	}
    79  	if err := db.Put(configKey(hash), data); err != nil {
    80  		log.Crit("Failed to store chain config", "err", err)
    81  	}
    82  }
    83  
    84  // ReadGenesisStateSpec retrieves the genesis state specification based on the
    85  // given genesis (block-)hash.
    86  func ReadGenesisStateSpec(db zonddb.KeyValueReader, blockhash common.Hash) []byte {
    87  	data, _ := db.Get(genesisStateSpecKey(blockhash))
    88  	return data
    89  }
    90  
    91  // WriteGenesisStateSpec writes the genesis state specification into the disk.
    92  func WriteGenesisStateSpec(db zonddb.KeyValueWriter, blockhash common.Hash, data []byte) {
    93  	if err := db.Put(genesisStateSpecKey(blockhash), data); err != nil {
    94  		log.Crit("Failed to store genesis state", "err", err)
    95  	}
    96  }
    97  
    98  // crashList is a list of unclean-shutdown-markers, for rlp-encoding to the
    99  // database
   100  type crashList struct {
   101  	Discarded uint64   // how many ucs have we deleted
   102  	Recent    []uint64 // unix timestamps of 10 latest unclean shutdowns
   103  }
   104  
   105  const crashesToKeep = 10
   106  
   107  // PushUncleanShutdownMarker appends a new unclean shutdown marker and returns
   108  // the previous data
   109  // - a list of timestamps
   110  // - a count of how many old unclean-shutdowns have been discarded
   111  func PushUncleanShutdownMarker(db zonddb.KeyValueStore) ([]uint64, uint64, error) {
   112  	var uncleanShutdowns crashList
   113  	// Read old data
   114  	if data, err := db.Get(uncleanShutdownKey); err == nil {
   115  		if err := rlp.DecodeBytes(data, &uncleanShutdowns); err != nil {
   116  			return nil, 0, err
   117  		}
   118  	}
   119  	var discarded = uncleanShutdowns.Discarded
   120  	var previous = make([]uint64, len(uncleanShutdowns.Recent))
   121  	copy(previous, uncleanShutdowns.Recent)
   122  	// Add a new (but cap it)
   123  	uncleanShutdowns.Recent = append(uncleanShutdowns.Recent, uint64(time.Now().Unix()))
   124  	if count := len(uncleanShutdowns.Recent); count > crashesToKeep+1 {
   125  		numDel := count - (crashesToKeep + 1)
   126  		uncleanShutdowns.Recent = uncleanShutdowns.Recent[numDel:]
   127  		uncleanShutdowns.Discarded += uint64(numDel)
   128  	}
   129  	// And save it again
   130  	data, _ := rlp.EncodeToBytes(uncleanShutdowns)
   131  	if err := db.Put(uncleanShutdownKey, data); err != nil {
   132  		log.Warn("Failed to write unclean-shutdown marker", "err", err)
   133  		return nil, 0, err
   134  	}
   135  	return previous, discarded, nil
   136  }
   137  
   138  // PopUncleanShutdownMarker removes the last unclean shutdown marker
   139  func PopUncleanShutdownMarker(db zonddb.KeyValueStore) {
   140  	var uncleanShutdowns crashList
   141  	// Read old data
   142  	if data, err := db.Get(uncleanShutdownKey); err != nil {
   143  		log.Warn("Error reading unclean shutdown markers", "error", err)
   144  	} else if err := rlp.DecodeBytes(data, &uncleanShutdowns); err != nil {
   145  		log.Error("Error decoding unclean shutdown markers", "error", err) // Should mos def _not_ happen
   146  	}
   147  	if l := len(uncleanShutdowns.Recent); l > 0 {
   148  		uncleanShutdowns.Recent = uncleanShutdowns.Recent[:l-1]
   149  	}
   150  	data, _ := rlp.EncodeToBytes(uncleanShutdowns)
   151  	if err := db.Put(uncleanShutdownKey, data); err != nil {
   152  		log.Warn("Failed to clear unclean-shutdown marker", "err", err)
   153  	}
   154  }
   155  
   156  // UpdateUncleanShutdownMarker updates the last marker's timestamp to now.
   157  func UpdateUncleanShutdownMarker(db zonddb.KeyValueStore) {
   158  	var uncleanShutdowns crashList
   159  	// Read old data
   160  	if data, err := db.Get(uncleanShutdownKey); err != nil {
   161  		log.Warn("Error reading unclean shutdown markers", "error", err)
   162  	} else if err := rlp.DecodeBytes(data, &uncleanShutdowns); err != nil {
   163  		log.Warn("Error decoding unclean shutdown markers", "error", err)
   164  	}
   165  	// This shouldn't happen because we push a marker on Backend instantiation
   166  	count := len(uncleanShutdowns.Recent)
   167  	if count == 0 {
   168  		log.Warn("No unclean shutdown marker to update")
   169  		return
   170  	}
   171  	uncleanShutdowns.Recent[count-1] = uint64(time.Now().Unix())
   172  	data, _ := rlp.EncodeToBytes(uncleanShutdowns)
   173  	if err := db.Put(uncleanShutdownKey, data); err != nil {
   174  		log.Warn("Failed to write unclean-shutdown marker", "err", err)
   175  	}
   176  }
   177  
   178  // ReadTransitionStatus retrieves the eth2 transition status from the database
   179  func ReadTransitionStatus(db zonddb.KeyValueReader) []byte {
   180  	data, _ := db.Get(transitionStatusKey)
   181  	return data
   182  }
   183  
   184  // WriteTransitionStatus stores the eth2 transition status to the database
   185  func WriteTransitionStatus(db zonddb.KeyValueWriter, data []byte) {
   186  	if err := db.Put(transitionStatusKey, data); err != nil {
   187  		log.Crit("Failed to store the eth2 transition status", "err", err)
   188  	}
   189  }