accessors_metadata.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. "time"
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/ethdb"
  22. "github.com/ethereum/go-ethereum/log"
  23. "github.com/ethereum/go-ethereum/params"
  24. "github.com/ethereum/go-ethereum/rlp"
  25. )
  26. // ReadDatabaseVersion retrieves the version number of the database.
  27. func ReadDatabaseVersion(db ethdb.KeyValueReader) *uint64 {
  28. var version uint64
  29. enc, _ := db.Get(databaseVersionKey)
  30. if len(enc) == 0 {
  31. return nil
  32. }
  33. if err := rlp.DecodeBytes(enc, &version); err != nil {
  34. return nil
  35. }
  36. return &version
  37. }
  38. // WriteDatabaseVersion stores the version number of the database
  39. func WriteDatabaseVersion(db ethdb.KeyValueWriter, version uint64) {
  40. enc, err := rlp.EncodeToBytes(version)
  41. if err != nil {
  42. log.Crit("Failed to encode database version", "err", err)
  43. }
  44. if err = db.Put(databaseVersionKey, enc); err != nil {
  45. log.Crit("Failed to store the database version", "err", err)
  46. }
  47. }
  48. // ReadChainConfig retrieves the consensus settings based on the given genesis hash.
  49. func ReadChainConfig(db ethdb.KeyValueReader, hash common.Hash) *params.ChainConfig {
  50. data, _ := db.Get(configKey(hash))
  51. if len(data) == 0 {
  52. return nil
  53. }
  54. var config params.ChainConfig
  55. if err := json.Unmarshal(data, &config); err != nil {
  56. log.Error("Invalid chain config JSON", "hash", hash, "err", err)
  57. return nil
  58. }
  59. return &config
  60. }
  61. // WriteChainConfig writes the chain config settings to the database.
  62. func WriteChainConfig(db ethdb.KeyValueWriter, hash common.Hash, cfg *params.ChainConfig) {
  63. if cfg == nil {
  64. return
  65. }
  66. data, err := json.Marshal(cfg)
  67. if err != nil {
  68. log.Crit("Failed to JSON encode chain config", "err", err)
  69. }
  70. if err := db.Put(configKey(hash), data); err != nil {
  71. log.Crit("Failed to store chain config", "err", err)
  72. }
  73. }
  74. // crashList is a list of unclean-shutdown-markers, for rlp-encoding to the
  75. // database
  76. type crashList struct {
  77. Discarded uint64 // how many ucs have we deleted
  78. Recent []uint64 // unix timestamps of 10 latest unclean shutdowns
  79. }
  80. const crashesToKeep = 10
  81. // PushUncleanShutdownMarker appends a new unclean shutdown marker and returns
  82. // the previous data
  83. // - a list of timestamps
  84. // - a count of how many old unclean-shutdowns have been discarded
  85. func PushUncleanShutdownMarker(db ethdb.KeyValueStore) ([]uint64, uint64, error) {
  86. var uncleanShutdowns crashList
  87. // Read old data
  88. if data, err := db.Get(uncleanShutdownKey); err != nil {
  89. log.Warn("Error reading unclean shutdown markers", "error", err)
  90. } else if err := rlp.DecodeBytes(data, &uncleanShutdowns); err != nil {
  91. return nil, 0, err
  92. }
  93. var discarded = uncleanShutdowns.Discarded
  94. var previous = make([]uint64, len(uncleanShutdowns.Recent))
  95. copy(previous, uncleanShutdowns.Recent)
  96. // Add a new (but cap it)
  97. uncleanShutdowns.Recent = append(uncleanShutdowns.Recent, uint64(time.Now().Unix()))
  98. if count := len(uncleanShutdowns.Recent); count > crashesToKeep+1 {
  99. numDel := count - (crashesToKeep + 1)
  100. uncleanShutdowns.Recent = uncleanShutdowns.Recent[numDel:]
  101. uncleanShutdowns.Discarded += uint64(numDel)
  102. }
  103. // And save it again
  104. data, _ := rlp.EncodeToBytes(uncleanShutdowns)
  105. if err := db.Put(uncleanShutdownKey, data); err != nil {
  106. log.Warn("Failed to write unclean-shutdown marker", "err", err)
  107. return nil, 0, err
  108. }
  109. return previous, discarded, nil
  110. }
  111. // PopUncleanShutdownMarker removes the last unclean shutdown marker
  112. func PopUncleanShutdownMarker(db ethdb.KeyValueStore) {
  113. var uncleanShutdowns crashList
  114. // Read old data
  115. if data, err := db.Get(uncleanShutdownKey); err != nil {
  116. log.Warn("Error reading unclean shutdown markers", "error", err)
  117. } else if err := rlp.DecodeBytes(data, &uncleanShutdowns); err != nil {
  118. log.Error("Error decoding unclean shutdown markers", "error", err) // Should mos def _not_ happen
  119. }
  120. if l := len(uncleanShutdowns.Recent); l > 0 {
  121. uncleanShutdowns.Recent = uncleanShutdowns.Recent[:l-1]
  122. }
  123. data, _ := rlp.EncodeToBytes(uncleanShutdowns)
  124. if err := db.Put(uncleanShutdownKey, data); err != nil {
  125. log.Warn("Failed to clear unclean-shutdown marker", "err", err)
  126. }
  127. }