github.com/DxChainNetwork/dxc@v0.8.1-0.20220824085222-1162e304b6e7/cmd/evm/internal/t8ntool/execution.go (about) 1 // Copyright 2020 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 t8ntool 18 19 import ( 20 "fmt" 21 "math/big" 22 "os" 23 24 "github.com/DxChainNetwork/dxc/common" 25 "github.com/DxChainNetwork/dxc/common/math" 26 "github.com/DxChainNetwork/dxc/consensus/misc" 27 "github.com/DxChainNetwork/dxc/core" 28 "github.com/DxChainNetwork/dxc/core/rawdb" 29 "github.com/DxChainNetwork/dxc/core/state" 30 "github.com/DxChainNetwork/dxc/core/types" 31 "github.com/DxChainNetwork/dxc/core/vm" 32 "github.com/DxChainNetwork/dxc/crypto" 33 "github.com/DxChainNetwork/dxc/ethdb" 34 "github.com/DxChainNetwork/dxc/log" 35 "github.com/DxChainNetwork/dxc/params" 36 "github.com/DxChainNetwork/dxc/rlp" 37 "github.com/DxChainNetwork/dxc/trie" 38 "golang.org/x/crypto/sha3" 39 ) 40 41 type Prestate struct { 42 Env stEnv `json:"env"` 43 Pre core.GenesisAlloc `json:"pre"` 44 } 45 46 // ExecutionResult contains the execution status after running a state test, any 47 // error that might have occurred and a dump of the final state if requested. 48 type ExecutionResult struct { 49 StateRoot common.Hash `json:"stateRoot"` 50 TxRoot common.Hash `json:"txRoot"` 51 ReceiptRoot common.Hash `json:"receiptRoot"` 52 LogsHash common.Hash `json:"logsHash"` 53 Bloom types.Bloom `json:"logsBloom" gencodec:"required"` 54 Receipts types.Receipts `json:"receipts"` 55 Rejected []*rejectedTx `json:"rejected,omitempty"` 56 } 57 58 type ommer struct { 59 Delta uint64 `json:"delta"` 60 Address common.Address `json:"address"` 61 } 62 63 //go:generate gencodec -type stEnv -field-override stEnvMarshaling -out gen_stenv.go 64 type stEnv struct { 65 Coinbase common.Address `json:"currentCoinbase" gencodec:"required"` 66 Difficulty *big.Int `json:"currentDifficulty" gencodec:"required"` 67 GasLimit uint64 `json:"currentGasLimit" gencodec:"required"` 68 Number uint64 `json:"currentNumber" gencodec:"required"` 69 Timestamp uint64 `json:"currentTimestamp" gencodec:"required"` 70 BlockHashes map[math.HexOrDecimal64]common.Hash `json:"blockHashes,omitempty"` 71 Ommers []ommer `json:"ommers,omitempty"` 72 BaseFee *big.Int `json:"currentBaseFee,omitempty"` 73 } 74 75 type stEnvMarshaling struct { 76 Coinbase common.UnprefixedAddress 77 Difficulty *math.HexOrDecimal256 78 GasLimit math.HexOrDecimal64 79 Number math.HexOrDecimal64 80 Timestamp math.HexOrDecimal64 81 BaseFee *math.HexOrDecimal256 82 } 83 84 type rejectedTx struct { 85 Index int `json:"index"` 86 Err string `json:"error"` 87 } 88 89 // Apply applies a set of transactions to a pre-state 90 func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig, 91 txs types.Transactions, miningReward int64, 92 getTracerFn func(txIndex int, txHash common.Hash) (tracer vm.Tracer, err error)) (*state.StateDB, *ExecutionResult, error) { 93 94 // Capture errors for BLOCKHASH operation, if we haven't been supplied the 95 // required blockhashes 96 var hashError error 97 getHash := func(num uint64) common.Hash { 98 if pre.Env.BlockHashes == nil { 99 hashError = fmt.Errorf("getHash(%d) invoked, no blockhashes provided", num) 100 return common.Hash{} 101 } 102 h, ok := pre.Env.BlockHashes[math.HexOrDecimal64(num)] 103 if !ok { 104 hashError = fmt.Errorf("getHash(%d) invoked, blockhash for that block not provided", num) 105 } 106 return h 107 } 108 var ( 109 statedb = MakePreState(rawdb.NewMemoryDatabase(), pre.Pre) 110 signer = types.MakeSigner(chainConfig, new(big.Int).SetUint64(pre.Env.Number)) 111 gaspool = new(core.GasPool) 112 blockHash = common.Hash{0x13, 0x37} 113 rejectedTxs []*rejectedTx 114 includedTxs types.Transactions 115 gasUsed = uint64(0) 116 receipts = make(types.Receipts, 0) 117 txIndex = 0 118 ) 119 gaspool.AddGas(pre.Env.GasLimit) 120 vmContext := vm.BlockContext{ 121 CanTransfer: core.CanTransfer, 122 Transfer: core.Transfer, 123 Coinbase: pre.Env.Coinbase, 124 BlockNumber: new(big.Int).SetUint64(pre.Env.Number), 125 Time: new(big.Int).SetUint64(pre.Env.Timestamp), 126 Difficulty: pre.Env.Difficulty, 127 GasLimit: pre.Env.GasLimit, 128 GetHash: getHash, 129 } 130 // If currentBaseFee is defined, add it to the vmContext. 131 if pre.Env.BaseFee != nil { 132 vmContext.BaseFee = new(big.Int).Set(pre.Env.BaseFee) 133 } 134 // If DAO is supported/enabled, we need to handle it here. In geth 'proper', it's 135 // done in StateProcessor.Process(block, ...), right before transactions are applied. 136 if chainConfig.DAOForkSupport && 137 chainConfig.DAOForkBlock != nil && 138 chainConfig.DAOForkBlock.Cmp(new(big.Int).SetUint64(pre.Env.Number)) == 0 { 139 misc.ApplyDAOHardFork(statedb) 140 } 141 142 for i, tx := range txs { 143 msg, err := tx.AsMessage(signer, pre.Env.BaseFee) 144 if err != nil { 145 log.Warn("rejected tx", "index", i, "hash", tx.Hash(), "error", err) 146 rejectedTxs = append(rejectedTxs, &rejectedTx{i, err.Error()}) 147 continue 148 } 149 tracer, err := getTracerFn(txIndex, tx.Hash()) 150 if err != nil { 151 return nil, nil, err 152 } 153 vmConfig.Tracer = tracer 154 vmConfig.Debug = (tracer != nil) 155 statedb.Prepare(tx.Hash(), txIndex) 156 txContext := core.NewEVMTxContext(msg) 157 snapshot := statedb.Snapshot() 158 evm := vm.NewEVM(vmContext, txContext, statedb, chainConfig, vmConfig) 159 160 // (ret []byte, usedGas uint64, failed bool, err error) 161 msgResult, err := core.ApplyMessage(evm, msg, gaspool) 162 if err != nil { 163 statedb.RevertToSnapshot(snapshot) 164 log.Info("rejected tx", "index", i, "hash", tx.Hash(), "from", msg.From(), "error", err) 165 rejectedTxs = append(rejectedTxs, &rejectedTx{i, err.Error()}) 166 continue 167 } 168 includedTxs = append(includedTxs, tx) 169 if hashError != nil { 170 return nil, nil, NewError(ErrorMissingBlockhash, hashError) 171 } 172 gasUsed += msgResult.UsedGas 173 174 // Receipt: 175 { 176 var root []byte 177 if chainConfig.IsByzantium(vmContext.BlockNumber) { 178 statedb.Finalise(true) 179 } else { 180 root = statedb.IntermediateRoot(chainConfig.IsEIP158(vmContext.BlockNumber)).Bytes() 181 } 182 183 // Create a new receipt for the transaction, storing the intermediate root and 184 // gas used by the tx. 185 receipt := &types.Receipt{Type: tx.Type(), PostState: root, CumulativeGasUsed: gasUsed} 186 if msgResult.Failed() { 187 receipt.Status = types.ReceiptStatusFailed 188 } else { 189 receipt.Status = types.ReceiptStatusSuccessful 190 } 191 receipt.TxHash = tx.Hash() 192 receipt.GasUsed = msgResult.UsedGas 193 194 // If the transaction created a contract, store the creation address in the receipt. 195 if msg.To() == nil { 196 receipt.ContractAddress = crypto.CreateAddress(evm.TxContext.Origin, tx.Nonce()) 197 } 198 199 // Set the receipt logs and create the bloom filter. 200 receipt.Logs = statedb.GetLogs(tx.Hash(), blockHash) 201 receipt.Bloom = types.CreateBloom(types.Receipts{receipt}) 202 // These three are non-consensus fields: 203 //receipt.BlockHash 204 //receipt.BlockNumber 205 receipt.TransactionIndex = uint(txIndex) 206 receipts = append(receipts, receipt) 207 } 208 209 txIndex++ 210 } 211 statedb.IntermediateRoot(chainConfig.IsEIP158(vmContext.BlockNumber)) 212 // Add mining reward? 213 if miningReward > 0 { 214 // Add mining reward. The mining reward may be `0`, which only makes a difference in the cases 215 // where 216 // - the coinbase suicided, or 217 // - there are only 'bad' transactions, which aren't executed. In those cases, 218 // the coinbase gets no txfee, so isn't created, and thus needs to be touched 219 var ( 220 blockReward = big.NewInt(miningReward) 221 minerReward = new(big.Int).Set(blockReward) 222 perOmmer = new(big.Int).Div(blockReward, big.NewInt(32)) 223 ) 224 for _, ommer := range pre.Env.Ommers { 225 // Add 1/32th for each ommer included 226 minerReward.Add(minerReward, perOmmer) 227 // Add (8-delta)/8 228 reward := big.NewInt(8) 229 reward.Sub(reward, big.NewInt(0).SetUint64(ommer.Delta)) 230 reward.Mul(reward, blockReward) 231 reward.Div(reward, big.NewInt(8)) 232 statedb.AddBalance(ommer.Address, reward) 233 } 234 statedb.AddBalance(pre.Env.Coinbase, minerReward) 235 } 236 // Commit block 237 root, err := statedb.Commit(chainConfig.IsEIP158(vmContext.BlockNumber)) 238 if err != nil { 239 fmt.Fprintf(os.Stderr, "Could not commit state: %v", err) 240 return nil, nil, NewError(ErrorEVM, fmt.Errorf("could not commit state: %v", err)) 241 } 242 execRs := &ExecutionResult{ 243 StateRoot: root, 244 TxRoot: types.DeriveSha(includedTxs, trie.NewStackTrie(nil)), 245 ReceiptRoot: types.DeriveSha(receipts, trie.NewStackTrie(nil)), 246 Bloom: types.CreateBloom(receipts), 247 LogsHash: rlpHash(statedb.Logs()), 248 Receipts: receipts, 249 Rejected: rejectedTxs, 250 } 251 return statedb, execRs, nil 252 } 253 254 func MakePreState(db ethdb.Database, accounts core.GenesisAlloc) *state.StateDB { 255 sdb := state.NewDatabase(db) 256 statedb, _ := state.New(common.Hash{}, sdb, nil) 257 for addr, a := range accounts { 258 statedb.SetCode(addr, a.Code) 259 statedb.SetNonce(addr, a.Nonce) 260 statedb.SetBalance(addr, a.Balance) 261 for k, v := range a.Storage { 262 statedb.SetState(addr, k, v) 263 } 264 } 265 // Commit and re-open to start with a clean state. 266 root, _ := statedb.Commit(false) 267 statedb, _ = state.New(root, sdb, nil) 268 return statedb 269 } 270 271 func rlpHash(x interface{}) (h common.Hash) { 272 hw := sha3.NewLegacyKeccak256() 273 rlp.Encode(hw, x) 274 hw.Sum(h[:0]) 275 return h 276 }