github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/core/state/database.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // Copyright 2019 The go-aigar Authors 3 // This file is part of the go-aigar library. 4 // 5 // The go-aigar library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-aigar library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>. 17 18 package state 19 20 import ( 21 "fmt" 22 23 "github.com/AigarNetwork/aigar/common" 24 "github.com/AigarNetwork/aigar/ethdb" 25 "github.com/AigarNetwork/aigar/trie" 26 lru "github.com/hashicorp/golang-lru" 27 ) 28 29 const ( 30 // Number of codehash->size associations to keep. 31 codeSizeCacheSize = 100000 32 ) 33 34 // Database wraps access to tries and contract code. 35 type Database interface { 36 // OpenTrie opens the main account trie. 37 OpenTrie(root common.Hash) (Trie, error) 38 39 // OpenStorageTrie opens the storage trie of an account. 40 OpenStorageTrie(addrHash, root common.Hash) (Trie, error) 41 42 // CopyTrie returns an independent copy of the given trie. 43 CopyTrie(Trie) Trie 44 45 // ContractCode retrieves a particular contract's code. 46 ContractCode(addrHash, codeHash common.Hash) ([]byte, error) 47 48 // ContractCodeSize retrieves a particular contracts code's size. 49 ContractCodeSize(addrHash, codeHash common.Hash) (int, error) 50 51 // TrieDB retrieves the low level trie database used for data storage. 52 TrieDB() *trie.Database 53 } 54 55 // Trie is a Ethereum Merkle Patricia trie. 56 type Trie interface { 57 // GetKey returns the sha3 preimage of a hashed key that was previously used 58 // to store a value. 59 // 60 // TODO(fjl): remove this when SecureTrie is removed 61 GetKey([]byte) []byte 62 63 // TryGet returns the value for key stored in the trie. The value bytes must 64 // not be modified by the caller. If a node was not found in the database, a 65 // trie.MissingNodeError is returned. 66 TryGet(key []byte) ([]byte, error) 67 68 // TryUpdate associates key with value in the trie. If value has length zero, any 69 // existing value is deleted from the trie. The value bytes must not be modified 70 // by the caller while they are stored in the trie. If a node was not found in the 71 // database, a trie.MissingNodeError is returned. 72 TryUpdate(key, value []byte) error 73 74 // TryDelete removes any existing value for key from the trie. If a node was not 75 // found in the database, a trie.MissingNodeError is returned. 76 TryDelete(key []byte) error 77 78 // Hash returns the root hash of the trie. It does not write to the database and 79 // can be used even if the trie doesn't have one. 80 Hash() common.Hash 81 82 // Commit writes all nodes to the trie's memory database, tracking the internal 83 // and external (for account tries) references. 84 Commit(onleaf trie.LeafCallback) (common.Hash, error) 85 86 // NodeIterator returns an iterator that returns nodes of the trie. Iteration 87 // starts at the key after the given start key. 88 NodeIterator(startKey []byte) trie.NodeIterator 89 90 // Prove constructs a Merkle proof for key. The result contains all encoded nodes 91 // on the path to the value at key. The value itself is also included in the last 92 // node and can be retrieved by verifying the proof. 93 // 94 // If the trie does not contain a value for key, the returned proof contains all 95 // nodes of the longest existing prefix of the key (at least the root), ending 96 // with the node that proves the absence of the key. 97 Prove(key []byte, fromLevel uint, proofDb ethdb.KeyValueWriter) error 98 } 99 100 // NewDatabase creates a backing store for state. The returned database is safe for 101 // concurrent use, but does not retain any recent trie nodes in memory. To keep some 102 // historical state in memory, use the NewDatabaseWithCache constructor. 103 func NewDatabase(db ethdb.Database) Database { 104 return NewDatabaseWithCache(db, 0) 105 } 106 107 // NewDatabaseWithCache creates a backing store for state. The returned database 108 // is safe for concurrent use and retains a lot of collapsed RLP trie nodes in a 109 // large memory cache. 110 func NewDatabaseWithCache(db ethdb.Database, cache int) Database { 111 csc, _ := lru.New(codeSizeCacheSize) 112 return &cachingDB{ 113 db: trie.NewDatabaseWithCache(db, cache), 114 codeSizeCache: csc, 115 } 116 } 117 118 type cachingDB struct { 119 db *trie.Database 120 codeSizeCache *lru.Cache 121 } 122 123 // OpenTrie opens the main account trie at a specific root hash. 124 func (db *cachingDB) OpenTrie(root common.Hash) (Trie, error) { 125 return trie.NewSecure(root, db.db) 126 } 127 128 // OpenStorageTrie opens the storage trie of an account. 129 func (db *cachingDB) OpenStorageTrie(addrHash, root common.Hash) (Trie, error) { 130 return trie.NewSecure(root, db.db) 131 } 132 133 // CopyTrie returns an independent copy of the given trie. 134 func (db *cachingDB) CopyTrie(t Trie) Trie { 135 switch t := t.(type) { 136 case *trie.SecureTrie: 137 return t.Copy() 138 default: 139 panic(fmt.Errorf("unknown trie type %T", t)) 140 } 141 } 142 143 // ContractCode retrieves a particular contract's code. 144 func (db *cachingDB) ContractCode(addrHash, codeHash common.Hash) ([]byte, error) { 145 code, err := db.db.Node(codeHash) 146 if err == nil { 147 db.codeSizeCache.Add(codeHash, len(code)) 148 } 149 return code, err 150 } 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 161 // TrieDB retrieves any intermediate trie-node caching layer. 162 func (db *cachingDB) TrieDB() *trie.Database { 163 return db.db 164 }