github.com/theQRL/go-zond@v0.1.1/zond/state_accessor.go (about) 1 // Copyright 2021 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 zond 18 19 import ( 20 "context" 21 "errors" 22 "fmt" 23 "time" 24 25 "github.com/theQRL/go-zond/common" 26 "github.com/theQRL/go-zond/core" 27 "github.com/theQRL/go-zond/core/rawdb" 28 "github.com/theQRL/go-zond/core/state" 29 "github.com/theQRL/go-zond/core/types" 30 "github.com/theQRL/go-zond/core/vm" 31 "github.com/theQRL/go-zond/log" 32 "github.com/theQRL/go-zond/trie" 33 "github.com/theQRL/go-zond/zond/tracers" 34 ) 35 36 // noopReleaser is returned in case there is no operation expected 37 // for releasing state. 38 var noopReleaser = tracers.StateReleaseFunc(func() {}) 39 40 func (eth *Ethereum) hashState(ctx context.Context, block *types.Block, reexec uint64, base *state.StateDB, readOnly bool, preferDisk bool) (statedb *state.StateDB, release tracers.StateReleaseFunc, err error) { 41 var ( 42 current *types.Block 43 database state.Database 44 triedb *trie.Database 45 report = true 46 origin = block.NumberU64() 47 ) 48 // The state is only for reading purposes, check the state presence in 49 // live database. 50 if readOnly { 51 // The state is available in live database, create a reference 52 // on top to prevent garbage collection and return a release 53 // function to deref it. 54 if statedb, err = eth.blockchain.StateAt(block.Root()); err == nil { 55 eth.blockchain.TrieDB().Reference(block.Root(), common.Hash{}) 56 return statedb, func() { 57 eth.blockchain.TrieDB().Dereference(block.Root()) 58 }, nil 59 } 60 } 61 // The state is both for reading and writing, or it's unavailable in disk, 62 // try to construct/recover the state over an ephemeral trie.Database for 63 // isolating the live one. 64 if base != nil { 65 if preferDisk { 66 // Create an ephemeral trie.Database for isolating the live one. Otherwise 67 // the internal junks created by tracing will be persisted into the disk. 68 // TODO(rjl493456442), clean cache is disabled to prevent memory leak, 69 // please re-enable it for better performance. 70 database = state.NewDatabaseWithConfig(eth.chainDb, trie.HashDefaults) 71 if statedb, err = state.New(block.Root(), database, nil); err == nil { 72 log.Info("Found disk backend for state trie", "root", block.Root(), "number", block.Number()) 73 return statedb, noopReleaser, nil 74 } 75 } 76 // The optional base statedb is given, mark the start point as parent block 77 statedb, database, triedb, report = base, base.Database(), base.Database().TrieDB(), false 78 current = eth.blockchain.GetBlock(block.ParentHash(), block.NumberU64()-1) 79 } else { 80 // Otherwise, try to reexec blocks until we find a state or reach our limit 81 current = block 82 83 // Create an ephemeral trie.Database for isolating the live one. Otherwise 84 // the internal junks created by tracing will be persisted into the disk. 85 // TODO(rjl493456442), clean cache is disabled to prevent memory leak, 86 // please re-enable it for better performance. 87 triedb = trie.NewDatabase(eth.chainDb, trie.HashDefaults) 88 database = state.NewDatabaseWithNodeDB(eth.chainDb, triedb) 89 90 // If we didn't check the live database, do check state over ephemeral database, 91 // otherwise we would rewind past a persisted block (specific corner case is 92 // chain tracing from the genesis). 93 if !readOnly { 94 statedb, err = state.New(current.Root(), database, nil) 95 if err == nil { 96 return statedb, noopReleaser, nil 97 } 98 } 99 // Database does not have the state for the given block, try to regenerate 100 for i := uint64(0); i < reexec; i++ { 101 if err := ctx.Err(); err != nil { 102 return nil, nil, err 103 } 104 if current.NumberU64() == 0 { 105 return nil, nil, errors.New("genesis state is missing") 106 } 107 parent := eth.blockchain.GetBlock(current.ParentHash(), current.NumberU64()-1) 108 if parent == nil { 109 return nil, nil, fmt.Errorf("missing block %v %d", current.ParentHash(), current.NumberU64()-1) 110 } 111 current = parent 112 113 statedb, err = state.New(current.Root(), database, nil) 114 if err == nil { 115 break 116 } 117 } 118 if err != nil { 119 switch err.(type) { 120 case *trie.MissingNodeError: 121 return nil, nil, fmt.Errorf("required historical state unavailable (reexec=%d)", reexec) 122 default: 123 return nil, nil, err 124 } 125 } 126 } 127 // State is available at historical point, re-execute the blocks on top for 128 // the desired state. 129 var ( 130 start = time.Now() 131 logged time.Time 132 parent common.Hash 133 ) 134 for current.NumberU64() < origin { 135 if err := ctx.Err(); err != nil { 136 return nil, nil, err 137 } 138 // Print progress logs if long enough time elapsed 139 if time.Since(logged) > 8*time.Second && report { 140 log.Info("Regenerating historical state", "block", current.NumberU64()+1, "target", origin, "remaining", origin-current.NumberU64()-1, "elapsed", time.Since(start)) 141 logged = time.Now() 142 } 143 // Retrieve the next block to regenerate and process it 144 next := current.NumberU64() + 1 145 if current = eth.blockchain.GetBlockByNumber(next); current == nil { 146 return nil, nil, fmt.Errorf("block #%d not found", next) 147 } 148 _, _, _, err := eth.blockchain.Processor().Process(current, statedb, vm.Config{}) 149 if err != nil { 150 return nil, nil, fmt.Errorf("processing block %d failed: %v", current.NumberU64(), err) 151 } 152 // Finalize the state so any modifications are written to the trie 153 root, err := statedb.Commit(current.NumberU64(), eth.blockchain.Config().IsEIP158(current.Number())) 154 if err != nil { 155 return nil, nil, fmt.Errorf("stateAtBlock commit failed, number %d root %v: %w", 156 current.NumberU64(), current.Root().Hex(), err) 157 } 158 statedb, err = state.New(root, database, nil) 159 if err != nil { 160 return nil, nil, fmt.Errorf("state reset after block %d failed: %v", current.NumberU64(), err) 161 } 162 // Hold the state reference and also drop the parent state 163 // to prevent accumulating too many nodes in memory. 164 triedb.Reference(root, common.Hash{}) 165 if parent != (common.Hash{}) { 166 triedb.Dereference(parent) 167 } 168 parent = root 169 } 170 if report { 171 _, nodes, imgs := triedb.Size() // all memory is contained within the nodes return in hashdb 172 log.Info("Historical state regenerated", "block", current.NumberU64(), "elapsed", time.Since(start), "nodes", nodes, "preimages", imgs) 173 } 174 return statedb, func() { triedb.Dereference(block.Root()) }, nil 175 } 176 177 func (eth *Ethereum) pathState(block *types.Block) (*state.StateDB, func(), error) { 178 // Check if the requested state is available in the live chain. 179 statedb, err := eth.blockchain.StateAt(block.Root()) 180 if err == nil { 181 return statedb, noopReleaser, nil 182 } 183 // TODO historic state is not supported in path-based scheme. 184 // Fully archive node in pbss will be implemented by relying 185 // on state history, but needs more work on top. 186 return nil, nil, errors.New("historical state not available in path scheme yet") 187 } 188 189 // stateAtBlock retrieves the state database associated with a certain block. 190 // If no state is locally available for the given block, a number of blocks 191 // are attempted to be reexecuted to generate the desired state. The optional 192 // base layer statedb can be provided which is regarded as the statedb of the 193 // parent block. 194 // 195 // An additional release function will be returned if the requested state is 196 // available. Release is expected to be invoked when the returned state is no 197 // longer needed. Its purpose is to prevent resource leaking. Though it can be 198 // noop in some cases. 199 // 200 // Parameters: 201 // - block: The block for which we want the state(state = block.Root) 202 // - reexec: The maximum number of blocks to reprocess trying to obtain the desired state 203 // - base: If the caller is tracing multiple blocks, the caller can provide the parent 204 // state continuously from the callsite. 205 // - readOnly: If true, then the live 'blockchain' state database is used. No mutation should 206 // be made from caller, e.g. perform Commit or other 'save-to-disk' changes. 207 // Otherwise, the trash generated by caller may be persisted permanently. 208 // - preferDisk: This arg can be used by the caller to signal that even though the 'base' is 209 // provided, it would be preferable to start from a fresh state, if we have it 210 // on disk. 211 func (eth *Ethereum) stateAtBlock(ctx context.Context, block *types.Block, reexec uint64, base *state.StateDB, readOnly bool, preferDisk bool) (statedb *state.StateDB, release tracers.StateReleaseFunc, err error) { 212 if eth.blockchain.TrieDB().Scheme() == rawdb.HashScheme { 213 return eth.hashState(ctx, block, reexec, base, readOnly, preferDisk) 214 } 215 return eth.pathState(block) 216 } 217 218 // stateAtTransaction returns the execution environment of a certain transaction. 219 func (eth *Ethereum) stateAtTransaction(ctx context.Context, block *types.Block, txIndex int, reexec uint64) (*core.Message, vm.BlockContext, *state.StateDB, tracers.StateReleaseFunc, error) { 220 // Short circuit if it's genesis block. 221 if block.NumberU64() == 0 { 222 return nil, vm.BlockContext{}, nil, nil, errors.New("no transaction in genesis") 223 } 224 // Create the parent state database 225 parent := eth.blockchain.GetBlock(block.ParentHash(), block.NumberU64()-1) 226 if parent == nil { 227 return nil, vm.BlockContext{}, nil, nil, fmt.Errorf("parent %#x not found", block.ParentHash()) 228 } 229 // Lookup the statedb of parent block from the live database, 230 // otherwise regenerate it on the flight. 231 statedb, release, err := eth.stateAtBlock(ctx, parent, reexec, nil, true, false) 232 if err != nil { 233 return nil, vm.BlockContext{}, nil, nil, err 234 } 235 if txIndex == 0 && len(block.Transactions()) == 0 { 236 return nil, vm.BlockContext{}, statedb, release, nil 237 } 238 // Recompute transactions up to the target index. 239 signer := types.MakeSigner(eth.blockchain.Config(), block.Number(), block.Time()) 240 for idx, tx := range block.Transactions() { 241 // Assemble the transaction call message and return if the requested offset 242 msg, _ := core.TransactionToMessage(tx, signer, block.BaseFee()) 243 txContext := core.NewEVMTxContext(msg) 244 context := core.NewEVMBlockContext(block.Header(), eth.blockchain, nil) 245 if idx == txIndex { 246 return msg, context, statedb, release, nil 247 } 248 // Not yet the searched for transaction, execute on top of the current state 249 vmenv := vm.NewEVM(context, txContext, statedb, eth.blockchain.Config(), vm.Config{}) 250 statedb.SetTxContext(tx.Hash(), idx) 251 if _, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(tx.Gas())); err != nil { 252 return nil, vm.BlockContext{}, nil, nil, fmt.Errorf("transaction %#x failed: %v", tx.Hash(), err) 253 } 254 // Ensure any modifications are committed to the state 255 // Only delete empty objects if EIP158/161 (a.k.a Spurious Dragon) is in effect 256 statedb.Finalise(vmenv.ChainConfig().IsEIP158(block.Number())) 257 } 258 return nil, vm.BlockContext{}, nil, nil, fmt.Errorf("transaction index %d out of range for block %#x", txIndex, block.Hash()) 259 }