accessors_metadata.go 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. }
  73. // ReadPreimage retrieves a single preimage of the provided hash.
  74. func ReadPreimage(db ethdb.KeyValueReader, hash common.Hash) []byte {
  75. data, _ := db.Get(preimageKey(hash))
  76. return data
  77. }
  78. // WritePreimages writes the provided set of preimages to the database.
  79. func WritePreimages(db ethdb.KeyValueWriter, preimages map[common.Hash][]byte) {
  80. for hash, preimage := range preimages {
  81. if err := db.Put(preimageKey(hash), preimage); err != nil {
  82. log.Crit("Failed to store trie preimage", "err", err)
  83. }
  84. }
  85. preimageCounter.Inc(int64(len(preimages)))
  86. preimageHitCounter.Inc(int64(len(preimages)))
  87. }