github.com/core-coin/go-core/v2@v2.1.9/core/rawdb/accessors_metadata.go (about) 1 // Copyright 2018 by the Authors 2 // This file is part of the go-core library. 3 // 4 // The go-core 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-core 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-core library. If not, see <http://www.gnu.org/licenses/>. 16 17 package rawdb 18 19 import ( 20 "encoding/json" 21 22 "github.com/core-coin/go-core/v2/xcbdb" 23 24 "github.com/core-coin/go-core/v2/common" 25 "github.com/core-coin/go-core/v2/log" 26 "github.com/core-coin/go-core/v2/params" 27 "github.com/core-coin/go-core/v2/rlp" 28 ) 29 30 // ReadDatabaseVersion retrieves the version number of the database. 31 func ReadDatabaseVersion(db xcbdb.KeyValueReader) *uint64 { 32 var version uint64 33 34 enc, _ := db.Get(databaseVerisionKey) 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 xcbdb.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(databaseVerisionKey, 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 xcbdb.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 xcbdb.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 }