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