database.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Copyright 2017 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 state
  17. import (
  18. "errors"
  19. "fmt"
  20. "github.com/VictoriaMetrics/fastcache"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/core/rawdb"
  23. "github.com/ethereum/go-ethereum/ethdb"
  24. "github.com/ethereum/go-ethereum/trie"
  25. lru "github.com/hashicorp/golang-lru"
  26. )
  27. const (
  28. // Number of codehash->size associations to keep.
  29. codeSizeCacheSize = 100000
  30. // Cache size granted for caching clean code.
  31. codeCacheSize = 64 * 1024 * 1024
  32. )
  33. // Database wraps access to tries and contract code.
  34. type Database interface {
  35. // OpenTrie opens the main account trie.
  36. OpenTrie(root common.Hash) (Trie, error)
  37. // OpenStorageTrie opens the storage trie of an account.
  38. OpenStorageTrie(addrHash, root common.Hash) (Trie, error)
  39. // CopyTrie returns an independent copy of the given trie.
  40. CopyTrie(Trie) Trie
  41. // ContractCode retrieves a particular contract's code.
  42. ContractCode(addrHash, codeHash common.Hash) ([]byte, error)
  43. // ContractCodeSize retrieves a particular contracts code's size.
  44. ContractCodeSize(addrHash, codeHash common.Hash) (int, error)
  45. // TrieDB retrieves the low level trie database used for data storage.
  46. TrieDB() *trie.Database
  47. }
  48. // Trie is a Ethereum Merkle Patricia trie.
  49. type Trie interface {
  50. // GetKey returns the sha3 preimage of a hashed key that was previously used
  51. // to store a value.
  52. //
  53. // TODO(fjl): remove this when SecureTrie is removed
  54. GetKey([]byte) []byte
  55. // TryGet returns the value for key stored in the trie. The value bytes must
  56. // not be modified by the caller. If a node was not found in the database, a
  57. // trie.MissingNodeError is returned.
  58. TryGet(key []byte) ([]byte, error)
  59. // TryUpdate associates key with value in the trie. If value has length zero, any
  60. // existing value is deleted from the trie. The value bytes must not be modified
  61. // by the caller while they are stored in the trie. If a node was not found in the
  62. // database, a trie.MissingNodeError is returned.
  63. TryUpdate(key, value []byte) error
  64. // TryDelete removes any existing value for key from the trie. If a node was not
  65. // found in the database, a trie.MissingNodeError is returned.
  66. TryDelete(key []byte) error
  67. // Hash returns the root hash of the trie. It does not write to the database and
  68. // can be used even if the trie doesn't have one.
  69. Hash() common.Hash
  70. // Commit writes all nodes to the trie's memory database, tracking the internal
  71. // and external (for account tries) references.
  72. Commit(onleaf trie.LeafCallback) (common.Hash, error)
  73. // NodeIterator returns an iterator that returns nodes of the trie. Iteration
  74. // starts at the key after the given start key.
  75. NodeIterator(startKey []byte) trie.NodeIterator
  76. // Prove constructs a Merkle proof for key. The result contains all encoded nodes
  77. // on the path to the value at key. The value itself is also included in the last
  78. // node and can be retrieved by verifying the proof.
  79. //
  80. // If the trie does not contain a value for key, the returned proof contains all
  81. // nodes of the longest existing prefix of the key (at least the root), ending
  82. // with the node that proves the absence of the key.
  83. Prove(key []byte, fromLevel uint, proofDb ethdb.KeyValueWriter) error
  84. }
  85. // NewDatabase creates a backing store for state. The returned database is safe for
  86. // concurrent use, but does not retain any recent trie nodes in memory. To keep some
  87. // historical state in memory, use the NewDatabaseWithConfig constructor.
  88. func NewDatabase(db ethdb.Database) Database {
  89. return NewDatabaseWithConfig(db, nil)
  90. }
  91. // NewDatabaseWithConfig creates a backing store for state. The returned database
  92. // is safe for concurrent use and retains a lot of collapsed RLP trie nodes in a
  93. // large memory cache.
  94. func NewDatabaseWithConfig(db ethdb.Database, config *trie.Config) Database {
  95. csc, _ := lru.New(codeSizeCacheSize)
  96. return &cachingDB{
  97. db: trie.NewDatabaseWithConfig(db, config),
  98. codeSizeCache: csc,
  99. codeCache: fastcache.New(codeCacheSize),
  100. }
  101. }
  102. type cachingDB struct {
  103. db *trie.Database
  104. codeSizeCache *lru.Cache
  105. codeCache *fastcache.Cache
  106. }
  107. // OpenTrie opens the main account trie at a specific root hash.
  108. func (db *cachingDB) OpenTrie(root common.Hash) (Trie, error) {
  109. return trie.NewSecure(root, db.db)
  110. }
  111. // OpenStorageTrie opens the storage trie of an account.
  112. func (db *cachingDB) OpenStorageTrie(addrHash, root common.Hash) (Trie, error) {
  113. return trie.NewSecure(root, db.db)
  114. }
  115. // CopyTrie returns an independent copy of the given trie.
  116. func (db *cachingDB) CopyTrie(t Trie) Trie {
  117. switch t := t.(type) {
  118. case *trie.SecureTrie:
  119. return t.Copy()
  120. default:
  121. panic(fmt.Errorf("unknown trie type %T", t))
  122. }
  123. }
  124. // ContractCode retrieves a particular contract's code.
  125. func (db *cachingDB) ContractCode(addrHash, codeHash common.Hash) ([]byte, error) {
  126. if code := db.codeCache.Get(nil, codeHash.Bytes()); len(code) > 0 {
  127. return code, nil
  128. }
  129. code := rawdb.ReadCode(db.db.DiskDB(), codeHash)
  130. if len(code) > 0 {
  131. db.codeCache.Set(codeHash.Bytes(), code)
  132. db.codeSizeCache.Add(codeHash, len(code))
  133. return code, nil
  134. }
  135. return nil, errors.New("not found")
  136. }
  137. // ContractCodeWithPrefix retrieves a particular contract's code. If the
  138. // code can't be found in the cache, then check the existence with **new**
  139. // db scheme.
  140. func (db *cachingDB) ContractCodeWithPrefix(addrHash, codeHash common.Hash) ([]byte, error) {
  141. if code := db.codeCache.Get(nil, codeHash.Bytes()); len(code) > 0 {
  142. return code, nil
  143. }
  144. code := rawdb.ReadCodeWithPrefix(db.db.DiskDB(), codeHash)
  145. if len(code) > 0 {
  146. db.codeCache.Set(codeHash.Bytes(), code)
  147. db.codeSizeCache.Add(codeHash, len(code))
  148. return code, nil
  149. }
  150. return nil, errors.New("not found")
  151. }
  152. // ContractCodeSize retrieves a particular contracts code's size.
  153. func (db *cachingDB) ContractCodeSize(addrHash, codeHash common.Hash) (int, error) {
  154. if cached, ok := db.codeSizeCache.Get(codeHash); ok {
  155. return cached.(int), nil
  156. }
  157. code, err := db.ContractCode(addrHash, codeHash)
  158. return len(code), err
  159. }
  160. // TrieDB retrieves any intermediate trie-node caching layer.
  161. func (db *cachingDB) TrieDB() *trie.Database {
  162. return db.db
  163. }