accessors_state.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. // WritePreimages writes the provided set of preimages to the database.
  28. func WritePreimages(db ethdb.KeyValueWriter, preimages map[common.Hash][]byte) {
  29. for hash, preimage := range preimages {
  30. if err := db.Put(preimageKey(hash), preimage); err != nil {
  31. log.Crit("Failed to store trie preimage", "err", err)
  32. }
  33. }
  34. preimageCounter.Inc(int64(len(preimages)))
  35. preimageHitCounter.Inc(int64(len(preimages)))
  36. }
  37. // ReadCode retrieves the contract code of the provided code hash.
  38. func ReadCode(db ethdb.KeyValueReader, hash common.Hash) []byte {
  39. // Try with the legacy code scheme first, if not then try with current
  40. // scheme. Since most of the code will be found with legacy scheme.
  41. //
  42. // todo(rjl493456442) change the order when we forcibly upgrade the code
  43. // scheme with snapshot.
  44. data, _ := db.Get(hash[:])
  45. if len(data) != 0 {
  46. return data
  47. }
  48. return ReadCodeWithPrefix(db, hash)
  49. }
  50. // ReadCodeWithPrefix retrieves the contract code of the provided code hash.
  51. // The main difference between this function and ReadCode is this function
  52. // will only check the existence with latest scheme(with prefix).
  53. func ReadCodeWithPrefix(db ethdb.KeyValueReader, hash common.Hash) []byte {
  54. data, _ := db.Get(codeKey(hash))
  55. return data
  56. }
  57. // WriteCode writes the provided contract code database.
  58. func WriteCode(db ethdb.KeyValueWriter, hash common.Hash, code []byte) {
  59. if err := db.Put(codeKey(hash), code); err != nil {
  60. log.Crit("Failed to store contract code", "err", err)
  61. }
  62. }
  63. // DeleteCode deletes the specified contract code from the database.
  64. func DeleteCode(db ethdb.KeyValueWriter, hash common.Hash) {
  65. if err := db.Delete(codeKey(hash)); err != nil {
  66. log.Crit("Failed to delete contract code", "err", err)
  67. }
  68. }
  69. // ReadTrieNode retrieves the trie node of the provided hash.
  70. func ReadTrieNode(db ethdb.KeyValueReader, hash common.Hash) []byte {
  71. data, _ := db.Get(hash.Bytes())
  72. return data
  73. }
  74. // WriteTrieNode writes the provided trie node database.
  75. func WriteTrieNode(db ethdb.KeyValueWriter, hash common.Hash, node []byte) {
  76. if err := db.Put(hash.Bytes(), node); err != nil {
  77. log.Crit("Failed to store trie node", "err", err)
  78. }
  79. }
  80. // DeleteTrieNode deletes the specified trie node from the database.
  81. func DeleteTrieNode(db ethdb.KeyValueWriter, hash common.Hash) {
  82. if err := db.Delete(hash.Bytes()); err != nil {
  83. log.Crit("Failed to delete trie node", "err", err)
  84. }
  85. }