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