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