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