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