accessors_metadata.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. package rawdb
  17. import (
  18. "encoding/json"
  19. "github.com/ethereum/go-ethereum/common"
  20. "github.com/ethereum/go-ethereum/ethdb"
  21. "github.com/ethereum/go-ethereum/log"
  22. "github.com/ethereum/go-ethereum/params"
  23. "github.com/ethereum/go-ethereum/rlp"
  24. )
  25. // ReadDatabaseVersion retrieves the version number of the database.
  26. func ReadDatabaseVersion(db ethdb.KeyValueReader) *uint64 {
  27. var version uint64
  28. enc, _ := db.Get(databaseVerisionKey)
  29. if len(enc) == 0 {
  30. return nil
  31. }
  32. if err := rlp.DecodeBytes(enc, &version); err != nil {
  33. return nil
  34. }
  35. return &version
  36. }
  37. // WriteDatabaseVersion stores the version number of the database
  38. func WriteDatabaseVersion(db ethdb.KeyValueWriter, version uint64) {
  39. enc, err := rlp.EncodeToBytes(version)
  40. if err != nil {
  41. log.Crit("Failed to encode database version", "err", err)
  42. }
  43. if err = db.Put(databaseVerisionKey, enc); err != nil {
  44. log.Crit("Failed to store the database version", "err", err)
  45. }
  46. }
  47. // ReadChainConfig retrieves the consensus settings based on the given genesis hash.
  48. func ReadChainConfig(db ethdb.KeyValueReader, hash common.Hash) *params.ChainConfig {
  49. data, _ := db.Get(configKey(hash))
  50. if len(data) == 0 {
  51. return nil
  52. }
  53. var config params.ChainConfig
  54. if err := json.Unmarshal(data, &config); err != nil {
  55. log.Error("Invalid chain config JSON", "hash", hash, "err", err)
  56. return nil
  57. }
  58. return &config
  59. }
  60. // WriteChainConfig writes the chain config settings to the database.
  61. func WriteChainConfig(db ethdb.KeyValueWriter, 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. }