accessors_metadata.go 3.0 KB

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