accessors_state.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 2020 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. "github.com/ethereum/go-ethereum/common"
  19. "github.com/ethereum/go-ethereum/ethdb"
  20. "github.com/ethereum/go-ethereum/log"
  21. )
  22. // ReadPreimage retrieves a single preimage of the provided hash.
  23. func ReadPreimage(db ethdb.KeyValueReader, hash common.Hash) []byte {
  24. data, _ := db.Get(preimageKey(hash))
  25. return data
  26. }
  27. // ReadCode retrieves the contract code of the provided code hash.
  28. func ReadCode(db ethdb.KeyValueReader, hash common.Hash) []byte {
  29. // Try with the prefixed code scheme first, if not then try with legacy
  30. // scheme.
  31. data := ReadCodeWithPrefix(db, hash)
  32. if len(data) != 0 {
  33. return data
  34. }
  35. data, _ = db.Get(hash.Bytes())
  36. return data
  37. }
  38. // ReadCodeWithPrefix retrieves the contract code of the provided code hash.
  39. // The main difference between this function and ReadCode is this function
  40. // will only check the existence with latest scheme(with prefix).
  41. func ReadCodeWithPrefix(db ethdb.KeyValueReader, hash common.Hash) []byte {
  42. data, _ := db.Get(codeKey(hash))
  43. return data
  44. }
  45. // ReadTrieNode retrieves the trie node of the provided hash.
  46. func ReadTrieNode(db ethdb.KeyValueReader, hash common.Hash) []byte {
  47. data, _ := db.Get(hash.Bytes())
  48. return data
  49. }
  50. // HasCode checks if the contract code corresponding to the
  51. // provided code hash is present in the db.
  52. func HasCode(db ethdb.KeyValueReader, hash common.Hash) bool {
  53. // Try with the prefixed code scheme first, if not then try with legacy
  54. // scheme.
  55. if ok := HasCodeWithPrefix(db, hash); ok {
  56. return true
  57. }
  58. ok, _ := db.Has(hash.Bytes())
  59. return ok
  60. }
  61. // HasCodeWithPrefix checks if the contract code corresponding to the
  62. // provided code hash is present in the db. This function will only check
  63. // presence using the prefix-scheme.
  64. func HasCodeWithPrefix(db ethdb.KeyValueReader, hash common.Hash) bool {
  65. ok, _ := db.Has(codeKey(hash))
  66. return ok
  67. }
  68. // HasTrieNode checks if the trie node with the provided hash is present in db.
  69. func HasTrieNode(db ethdb.KeyValueReader, hash common.Hash) bool {
  70. ok, _ := db.Has(hash.Bytes())
  71. return ok
  72. }
  73. // WritePreimages writes the provided set of preimages to the database.
  74. func WritePreimages(db ethdb.KeyValueWriter, preimages map[common.Hash][]byte) {
  75. for hash, preimage := range preimages {
  76. if err := db.Put(preimageKey(hash), preimage); err != nil {
  77. log.Crit("Failed to store trie preimage", "err", err)
  78. }
  79. }
  80. preimageCounter.Inc(int64(len(preimages)))
  81. preimageHitCounter.Inc(int64(len(preimages)))
  82. }
  83. // WriteCode writes the provided contract code database.
  84. func WriteCode(db ethdb.KeyValueWriter, hash common.Hash, code []byte) {
  85. if err := db.Put(codeKey(hash), code); err != nil {
  86. log.Crit("Failed to store contract code", "err", err)
  87. }
  88. }
  89. // WriteTrieNode writes the provided trie node database.
  90. func WriteTrieNode(db ethdb.KeyValueWriter, hash common.Hash, node []byte) {
  91. if err := db.Put(hash.Bytes(), node); err != nil {
  92. log.Crit("Failed to store trie node", "err", err)
  93. }
  94. }
  95. // DeleteCode deletes the specified contract code from the database.
  96. func DeleteCode(db ethdb.KeyValueWriter, hash common.Hash) {
  97. if err := db.Delete(codeKey(hash)); err != nil {
  98. log.Crit("Failed to delete contract code", "err", err)
  99. }
  100. }
  101. // DeleteTrieNode deletes the specified trie node from the database.
  102. func DeleteTrieNode(db ethdb.KeyValueWriter, hash common.Hash) {
  103. if err := db.Delete(hash.Bytes()); err != nil {
  104. log.Crit("Failed to delete trie node", "err", err)
  105. }
  106. }