github.com/theQRL/go-zond@v0.1.1/les/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 les 18 19 import ( 20 "context" 21 "errors" 22 "fmt" 23 24 "github.com/theQRL/go-zond/core" 25 "github.com/theQRL/go-zond/core/state" 26 "github.com/theQRL/go-zond/core/types" 27 "github.com/theQRL/go-zond/core/vm" 28 "github.com/theQRL/go-zond/zond/tracers" 29 "github.com/theQRL/go-zond/light" 30 ) 31 32 // noopReleaser is returned in case there is no operation expected 33 // for releasing state. 34 var noopReleaser = tracers.StateReleaseFunc(func() {}) 35 36 // stateAtBlock retrieves the state database associated with a certain block. 37 func (leth *LightEthereum) stateAtBlock(ctx context.Context, block *types.Block, reexec uint64) (*state.StateDB, tracers.StateReleaseFunc, error) { 38 return light.NewState(ctx, block.Header(), leth.odr), noopReleaser, nil 39 } 40 41 // stateAtTransaction returns the execution environment of a certain transaction. 42 func (leth *LightEthereum) stateAtTransaction(ctx context.Context, block *types.Block, txIndex int, reexec uint64) (*core.Message, vm.BlockContext, *state.StateDB, tracers.StateReleaseFunc, error) { 43 // Short circuit if it's genesis block. 44 if block.NumberU64() == 0 { 45 return nil, vm.BlockContext{}, nil, nil, errors.New("no transaction in genesis") 46 } 47 // Create the parent state database 48 parent, err := leth.blockchain.GetBlock(ctx, block.ParentHash(), block.NumberU64()-1) 49 if err != nil { 50 return nil, vm.BlockContext{}, nil, nil, err 51 } 52 statedb, release, err := leth.stateAtBlock(ctx, parent, reexec) 53 if err != nil { 54 return nil, vm.BlockContext{}, nil, nil, err 55 } 56 if txIndex == 0 && len(block.Transactions()) == 0 { 57 return nil, vm.BlockContext{}, statedb, release, nil 58 } 59 // Recompute transactions up to the target index. 60 signer := types.MakeSigner(leth.blockchain.Config(), block.Number(), block.Time()) 61 for idx, tx := range block.Transactions() { 62 // Assemble the transaction call message and return if the requested offset 63 msg, _ := core.TransactionToMessage(tx, signer, block.BaseFee()) 64 txContext := core.NewEVMTxContext(msg) 65 context := core.NewEVMBlockContext(block.Header(), leth.blockchain, nil) 66 statedb.SetTxContext(tx.Hash(), idx) 67 if idx == txIndex { 68 return msg, context, statedb, release, nil 69 } 70 // Not yet the searched for transaction, execute on top of the current state 71 vmenv := vm.NewEVM(context, txContext, statedb, leth.blockchain.Config(), vm.Config{}) 72 if _, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(tx.Gas())); err != nil { 73 return nil, vm.BlockContext{}, nil, nil, fmt.Errorf("transaction %#x failed: %v", tx.Hash(), err) 74 } 75 // Ensure any modifications are committed to the state 76 // Only delete empty objects if EIP158/161 (a.k.a Spurious Dragon) is in effect 77 statedb.Finalise(vmenv.ChainConfig().IsEIP158(block.Number())) 78 } 79 return nil, vm.BlockContext{}, nil, nil, fmt.Errorf("transaction index %d out of range for block %#x", txIndex, block.Hash()) 80 }