github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/core/rawdb/accessors_metadata.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // Copyright 2019 The go-aigar Authors 3 // This file is part of the go-aigar library. 4 // 5 // The go-aigar library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-aigar library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>. 17 18 package rawdb 19 20 import ( 21 "encoding/json" 22 23 "github.com/AigarNetwork/aigar/common" 24 "github.com/AigarNetwork/aigar/ethdb" 25 "github.com/AigarNetwork/aigar/log" 26 "github.com/AigarNetwork/aigar/params" 27 "github.com/AigarNetwork/aigar/rlp" 28 ) 29 30 // ReadDatabaseVersion retrieves the version number of the database. 31 func ReadDatabaseVersion(db ethdb.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 ethdb.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 ethdb.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 ethdb.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 // ReadPreimage retrieves a single preimage of the provided hash. 85 func ReadPreimage(db ethdb.KeyValueReader, hash common.Hash) []byte { 86 data, _ := db.Get(preimageKey(hash)) 87 return data 88 } 89 90 // WritePreimages writes the provided set of preimages to the database. 91 func WritePreimages(db ethdb.KeyValueWriter, preimages map[common.Hash][]byte) { 92 for hash, preimage := range preimages { 93 if err := db.Put(preimageKey(hash), preimage); err != nil { 94 log.Crit("Failed to store trie preimage", "err", err) 95 } 96 } 97 preimageCounter.Inc(int64(len(preimages))) 98 preimageHitCounter.Inc(int64(len(preimages))) 99 }