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