github.com/ethereum/go-ethereum@v1.14.3/core/state_transition.go (about) 1 // Copyright 2014 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 core 18 19 import ( 20 "fmt" 21 "math" 22 "math/big" 23 24 "github.com/ethereum/go-ethereum/common" 25 cmath "github.com/ethereum/go-ethereum/common/math" 26 "github.com/ethereum/go-ethereum/core/tracing" 27 "github.com/ethereum/go-ethereum/core/types" 28 "github.com/ethereum/go-ethereum/core/vm" 29 "github.com/ethereum/go-ethereum/crypto/kzg4844" 30 "github.com/ethereum/go-ethereum/params" 31 "github.com/holiman/uint256" 32 ) 33 34 // ExecutionResult includes all output after executing given evm 35 // message no matter the execution itself is successful or not. 36 type ExecutionResult struct { 37 UsedGas uint64 // Total used gas, not including the refunded gas 38 RefundedGas uint64 // Total gas refunded after execution 39 Err error // Any error encountered during the execution(listed in core/vm/errors.go) 40 ReturnData []byte // Returned data from evm(function result or data supplied with revert opcode) 41 } 42 43 // Unwrap returns the internal evm error which allows us for further 44 // analysis outside. 45 func (result *ExecutionResult) Unwrap() error { 46 return result.Err 47 } 48 49 // Failed returns the indicator whether the execution is successful or not 50 func (result *ExecutionResult) Failed() bool { return result.Err != nil } 51 52 // Return is a helper function to help caller distinguish between revert reason 53 // and function return. Return returns the data after execution if no error occurs. 54 func (result *ExecutionResult) Return() []byte { 55 if result.Err != nil { 56 return nil 57 } 58 return common.CopyBytes(result.ReturnData) 59 } 60 61 // Revert returns the concrete revert reason if the execution is aborted by `REVERT` 62 // opcode. Note the reason can be nil if no data supplied with revert opcode. 63 func (result *ExecutionResult) Revert() []byte { 64 if result.Err != vm.ErrExecutionReverted { 65 return nil 66 } 67 return common.CopyBytes(result.ReturnData) 68 } 69 70 // IntrinsicGas computes the 'intrinsic gas' for a message with the given data. 71 func IntrinsicGas(data []byte, accessList types.AccessList, isContractCreation bool, isHomestead, isEIP2028, isEIP3860 bool) (uint64, error) { 72 // Set the starting gas for the raw transaction 73 var gas uint64 74 if isContractCreation && isHomestead { 75 gas = params.TxGasContractCreation 76 } else { 77 gas = params.TxGas 78 } 79 dataLen := uint64(len(data)) 80 // Bump the required gas by the amount of transactional data 81 if dataLen > 0 { 82 // Zero and non-zero bytes are priced differently 83 var nz uint64 84 for _, byt := range data { 85 if byt != 0 { 86 nz++ 87 } 88 } 89 // Make sure we don't exceed uint64 for all data combinations 90 nonZeroGas := params.TxDataNonZeroGasFrontier 91 if isEIP2028 { 92 nonZeroGas = params.TxDataNonZeroGasEIP2028 93 } 94 if (math.MaxUint64-gas)/nonZeroGas < nz { 95 return 0, ErrGasUintOverflow 96 } 97 gas += nz * nonZeroGas 98 99 z := dataLen - nz 100 if (math.MaxUint64-gas)/params.TxDataZeroGas < z { 101 return 0, ErrGasUintOverflow 102 } 103 gas += z * params.TxDataZeroGas 104 105 if isContractCreation && isEIP3860 { 106 lenWords := toWordSize(dataLen) 107 if (math.MaxUint64-gas)/params.InitCodeWordGas < lenWords { 108 return 0, ErrGasUintOverflow 109 } 110 gas += lenWords * params.InitCodeWordGas 111 } 112 } 113 if accessList != nil { 114 gas += uint64(len(accessList)) * params.TxAccessListAddressGas 115 gas += uint64(accessList.StorageKeys()) * params.TxAccessListStorageKeyGas 116 } 117 return gas, nil 118 } 119 120 // toWordSize returns the ceiled word size required for init code payment calculation. 121 func toWordSize(size uint64) uint64 { 122 if size > math.MaxUint64-31 { 123 return math.MaxUint64/32 + 1 124 } 125 126 return (size + 31) / 32 127 } 128 129 // A Message contains the data derived from a single transaction that is relevant to state 130 // processing. 131 type Message struct { 132 To *common.Address 133 From common.Address 134 Nonce uint64 135 Value *big.Int 136 GasLimit uint64 137 GasPrice *big.Int 138 GasFeeCap *big.Int 139 GasTipCap *big.Int 140 Data []byte 141 AccessList types.AccessList 142 BlobGasFeeCap *big.Int 143 BlobHashes []common.Hash 144 145 // When SkipAccountChecks is true, the message nonce is not checked against the 146 // account nonce in state. It also disables checking that the sender is an EOA. 147 // This field will be set to true for operations like RPC eth_call. 148 SkipAccountChecks bool 149 } 150 151 // TransactionToMessage converts a transaction into a Message. 152 func TransactionToMessage(tx *types.Transaction, s types.Signer, baseFee *big.Int) (*Message, error) { 153 msg := &Message{ 154 Nonce: tx.Nonce(), 155 GasLimit: tx.Gas(), 156 GasPrice: new(big.Int).Set(tx.GasPrice()), 157 GasFeeCap: new(big.Int).Set(tx.GasFeeCap()), 158 GasTipCap: new(big.Int).Set(tx.GasTipCap()), 159 To: tx.To(), 160 Value: tx.Value(), 161 Data: tx.Data(), 162 AccessList: tx.AccessList(), 163 SkipAccountChecks: false, 164 BlobHashes: tx.BlobHashes(), 165 BlobGasFeeCap: tx.BlobGasFeeCap(), 166 } 167 // If baseFee provided, set gasPrice to effectiveGasPrice. 168 if baseFee != nil { 169 msg.GasPrice = cmath.BigMin(msg.GasPrice.Add(msg.GasTipCap, baseFee), msg.GasFeeCap) 170 } 171 var err error 172 msg.From, err = types.Sender(s, tx) 173 return msg, err 174 } 175 176 // ApplyMessage computes the new state by applying the given message 177 // against the old state within the environment. 178 // 179 // ApplyMessage returns the bytes returned by any EVM execution (if it took place), 180 // the gas used (which includes gas refunds) and an error if it failed. An error always 181 // indicates a core error meaning that the message would always fail for that particular 182 // state and would never be accepted within a block. 183 func ApplyMessage(evm *vm.EVM, msg *Message, gp *GasPool) (*ExecutionResult, error) { 184 return NewStateTransition(evm, msg, gp).TransitionDb() 185 } 186 187 // StateTransition represents a state transition. 188 // 189 // == The State Transitioning Model 190 // 191 // A state transition is a change made when a transaction is applied to the current world 192 // state. The state transitioning model does all the necessary work to work out a valid new 193 // state root. 194 // 195 // 1. Nonce handling 196 // 2. Pre pay gas 197 // 3. Create a new state object if the recipient is nil 198 // 4. Value transfer 199 // 200 // == If contract creation == 201 // 202 // 4a. Attempt to run transaction data 203 // 4b. If valid, use result as code for the new state object 204 // 205 // == end == 206 // 207 // 5. Run Script section 208 // 6. Derive new state root 209 type StateTransition struct { 210 gp *GasPool 211 msg *Message 212 gasRemaining uint64 213 initialGas uint64 214 state vm.StateDB 215 evm *vm.EVM 216 } 217 218 // NewStateTransition initialises and returns a new state transition object. 219 func NewStateTransition(evm *vm.EVM, msg *Message, gp *GasPool) *StateTransition { 220 return &StateTransition{ 221 gp: gp, 222 evm: evm, 223 msg: msg, 224 state: evm.StateDB, 225 } 226 } 227 228 // to returns the recipient of the message. 229 func (st *StateTransition) to() common.Address { 230 if st.msg == nil || st.msg.To == nil /* contract creation */ { 231 return common.Address{} 232 } 233 return *st.msg.To 234 } 235 236 func (st *StateTransition) buyGas() error { 237 mgval := new(big.Int).SetUint64(st.msg.GasLimit) 238 mgval.Mul(mgval, st.msg.GasPrice) 239 balanceCheck := new(big.Int).Set(mgval) 240 if st.msg.GasFeeCap != nil { 241 balanceCheck.SetUint64(st.msg.GasLimit) 242 balanceCheck = balanceCheck.Mul(balanceCheck, st.msg.GasFeeCap) 243 balanceCheck.Add(balanceCheck, st.msg.Value) 244 } 245 if st.evm.ChainConfig().IsCancun(st.evm.Context.BlockNumber, st.evm.Context.Time) { 246 if blobGas := st.blobGasUsed(); blobGas > 0 { 247 // Check that the user has enough funds to cover blobGasUsed * tx.BlobGasFeeCap 248 blobBalanceCheck := new(big.Int).SetUint64(blobGas) 249 blobBalanceCheck.Mul(blobBalanceCheck, st.msg.BlobGasFeeCap) 250 balanceCheck.Add(balanceCheck, blobBalanceCheck) 251 // Pay for blobGasUsed * actual blob fee 252 blobFee := new(big.Int).SetUint64(blobGas) 253 blobFee.Mul(blobFee, st.evm.Context.BlobBaseFee) 254 mgval.Add(mgval, blobFee) 255 } 256 } 257 balanceCheckU256, overflow := uint256.FromBig(balanceCheck) 258 if overflow { 259 return fmt.Errorf("%w: address %v required balance exceeds 256 bits", ErrInsufficientFunds, st.msg.From.Hex()) 260 } 261 if have, want := st.state.GetBalance(st.msg.From), balanceCheckU256; have.Cmp(want) < 0 { 262 return fmt.Errorf("%w: address %v have %v want %v", ErrInsufficientFunds, st.msg.From.Hex(), have, want) 263 } 264 if err := st.gp.SubGas(st.msg.GasLimit); err != nil { 265 return err 266 } 267 268 if st.evm.Config.Tracer != nil && st.evm.Config.Tracer.OnGasChange != nil { 269 st.evm.Config.Tracer.OnGasChange(0, st.msg.GasLimit, tracing.GasChangeTxInitialBalance) 270 } 271 st.gasRemaining = st.msg.GasLimit 272 273 st.initialGas = st.msg.GasLimit 274 mgvalU256, _ := uint256.FromBig(mgval) 275 st.state.SubBalance(st.msg.From, mgvalU256, tracing.BalanceDecreaseGasBuy) 276 return nil 277 } 278 279 func (st *StateTransition) preCheck() error { 280 // Only check transactions that are not fake 281 msg := st.msg 282 if !msg.SkipAccountChecks { 283 // Make sure this transaction's nonce is correct. 284 stNonce := st.state.GetNonce(msg.From) 285 if msgNonce := msg.Nonce; stNonce < msgNonce { 286 return fmt.Errorf("%w: address %v, tx: %d state: %d", ErrNonceTooHigh, 287 msg.From.Hex(), msgNonce, stNonce) 288 } else if stNonce > msgNonce { 289 return fmt.Errorf("%w: address %v, tx: %d state: %d", ErrNonceTooLow, 290 msg.From.Hex(), msgNonce, stNonce) 291 } else if stNonce+1 < stNonce { 292 return fmt.Errorf("%w: address %v, nonce: %d", ErrNonceMax, 293 msg.From.Hex(), stNonce) 294 } 295 // Make sure the sender is an EOA 296 codeHash := st.state.GetCodeHash(msg.From) 297 if codeHash != (common.Hash{}) && codeHash != types.EmptyCodeHash { 298 return fmt.Errorf("%w: address %v, codehash: %s", ErrSenderNoEOA, 299 msg.From.Hex(), codeHash) 300 } 301 } 302 // Make sure that transaction gasFeeCap is greater than the baseFee (post london) 303 if st.evm.ChainConfig().IsLondon(st.evm.Context.BlockNumber) { 304 // Skip the checks if gas fields are zero and baseFee was explicitly disabled (eth_call) 305 skipCheck := st.evm.Config.NoBaseFee && msg.GasFeeCap.BitLen() == 0 && msg.GasTipCap.BitLen() == 0 306 if !skipCheck { 307 if l := msg.GasFeeCap.BitLen(); l > 256 { 308 return fmt.Errorf("%w: address %v, maxFeePerGas bit length: %d", ErrFeeCapVeryHigh, 309 msg.From.Hex(), l) 310 } 311 if l := msg.GasTipCap.BitLen(); l > 256 { 312 return fmt.Errorf("%w: address %v, maxPriorityFeePerGas bit length: %d", ErrTipVeryHigh, 313 msg.From.Hex(), l) 314 } 315 if msg.GasFeeCap.Cmp(msg.GasTipCap) < 0 { 316 return fmt.Errorf("%w: address %v, maxPriorityFeePerGas: %s, maxFeePerGas: %s", ErrTipAboveFeeCap, 317 msg.From.Hex(), msg.GasTipCap, msg.GasFeeCap) 318 } 319 // This will panic if baseFee is nil, but basefee presence is verified 320 // as part of header validation. 321 if msg.GasFeeCap.Cmp(st.evm.Context.BaseFee) < 0 { 322 return fmt.Errorf("%w: address %v, maxFeePerGas: %s, baseFee: %s", ErrFeeCapTooLow, 323 msg.From.Hex(), msg.GasFeeCap, st.evm.Context.BaseFee) 324 } 325 } 326 } 327 // Check the blob version validity 328 if msg.BlobHashes != nil { 329 // The to field of a blob tx type is mandatory, and a `BlobTx` transaction internally 330 // has it as a non-nillable value, so any msg derived from blob transaction has it non-nil. 331 // However, messages created through RPC (eth_call) don't have this restriction. 332 if msg.To == nil { 333 return ErrBlobTxCreate 334 } 335 if len(msg.BlobHashes) == 0 { 336 return ErrMissingBlobHashes 337 } 338 for i, hash := range msg.BlobHashes { 339 if !kzg4844.IsValidVersionedHash(hash[:]) { 340 return fmt.Errorf("blob %d has invalid hash version", i) 341 } 342 } 343 } 344 // Check that the user is paying at least the current blob fee 345 if st.evm.ChainConfig().IsCancun(st.evm.Context.BlockNumber, st.evm.Context.Time) { 346 if st.blobGasUsed() > 0 { 347 // Skip the checks if gas fields are zero and blobBaseFee was explicitly disabled (eth_call) 348 skipCheck := st.evm.Config.NoBaseFee && msg.BlobGasFeeCap.BitLen() == 0 349 if !skipCheck { 350 // This will panic if blobBaseFee is nil, but blobBaseFee presence 351 // is verified as part of header validation. 352 if msg.BlobGasFeeCap.Cmp(st.evm.Context.BlobBaseFee) < 0 { 353 return fmt.Errorf("%w: address %v blobGasFeeCap: %v, blobBaseFee: %v", ErrBlobFeeCapTooLow, 354 msg.From.Hex(), msg.BlobGasFeeCap, st.evm.Context.BlobBaseFee) 355 } 356 } 357 } 358 } 359 return st.buyGas() 360 } 361 362 // TransitionDb will transition the state by applying the current message and 363 // returning the evm execution result with following fields. 364 // 365 // - used gas: total gas used (including gas being refunded) 366 // - returndata: the returned data from evm 367 // - concrete execution error: various EVM errors which abort the execution, e.g. 368 // ErrOutOfGas, ErrExecutionReverted 369 // 370 // However if any consensus issue encountered, return the error directly with 371 // nil evm execution result. 372 func (st *StateTransition) TransitionDb() (*ExecutionResult, error) { 373 // First check this message satisfies all consensus rules before 374 // applying the message. The rules include these clauses 375 // 376 // 1. the nonce of the message caller is correct 377 // 2. caller has enough balance to cover transaction fee(gaslimit * gasprice) 378 // 3. the amount of gas required is available in the block 379 // 4. the purchased gas is enough to cover intrinsic usage 380 // 5. there is no overflow when calculating intrinsic gas 381 // 6. caller has enough balance to cover asset transfer for **topmost** call 382 383 // Check clauses 1-3, buy gas if everything is correct 384 if err := st.preCheck(); err != nil { 385 return nil, err 386 } 387 388 var ( 389 msg = st.msg 390 sender = vm.AccountRef(msg.From) 391 rules = st.evm.ChainConfig().Rules(st.evm.Context.BlockNumber, st.evm.Context.Random != nil, st.evm.Context.Time) 392 contractCreation = msg.To == nil 393 ) 394 395 // Check clauses 4-5, subtract intrinsic gas if everything is correct 396 gas, err := IntrinsicGas(msg.Data, msg.AccessList, contractCreation, rules.IsHomestead, rules.IsIstanbul, rules.IsShanghai) 397 if err != nil { 398 return nil, err 399 } 400 if st.gasRemaining < gas { 401 return nil, fmt.Errorf("%w: have %d, want %d", ErrIntrinsicGas, st.gasRemaining, gas) 402 } 403 if t := st.evm.Config.Tracer; t != nil && t.OnGasChange != nil { 404 t.OnGasChange(st.gasRemaining, st.gasRemaining-gas, tracing.GasChangeTxIntrinsicGas) 405 } 406 st.gasRemaining -= gas 407 408 // Check clause 6 409 value, overflow := uint256.FromBig(msg.Value) 410 if overflow { 411 return nil, fmt.Errorf("%w: address %v", ErrInsufficientFundsForTransfer, msg.From.Hex()) 412 } 413 if !value.IsZero() && !st.evm.Context.CanTransfer(st.state, msg.From, value) { 414 return nil, fmt.Errorf("%w: address %v", ErrInsufficientFundsForTransfer, msg.From.Hex()) 415 } 416 417 // Check whether the init code size has been exceeded. 418 if rules.IsShanghai && contractCreation && len(msg.Data) > params.MaxInitCodeSize { 419 return nil, fmt.Errorf("%w: code size %v limit %v", ErrMaxInitCodeSizeExceeded, len(msg.Data), params.MaxInitCodeSize) 420 } 421 422 // Execute the preparatory steps for state transition which includes: 423 // - prepare accessList(post-berlin) 424 // - reset transient storage(eip 1153) 425 st.state.Prepare(rules, msg.From, st.evm.Context.Coinbase, msg.To, vm.ActivePrecompiles(rules), msg.AccessList) 426 427 var ( 428 ret []byte 429 vmerr error // vm errors do not effect consensus and are therefore not assigned to err 430 ) 431 if contractCreation { 432 ret, _, st.gasRemaining, vmerr = st.evm.Create(sender, msg.Data, st.gasRemaining, value) 433 } else { 434 // Increment the nonce for the next transaction 435 st.state.SetNonce(msg.From, st.state.GetNonce(sender.Address())+1) 436 ret, st.gasRemaining, vmerr = st.evm.Call(sender, st.to(), msg.Data, st.gasRemaining, value) 437 } 438 439 var gasRefund uint64 440 if !rules.IsLondon { 441 // Before EIP-3529: refunds were capped to gasUsed / 2 442 gasRefund = st.refundGas(params.RefundQuotient) 443 } else { 444 // After EIP-3529: refunds are capped to gasUsed / 5 445 gasRefund = st.refundGas(params.RefundQuotientEIP3529) 446 } 447 effectiveTip := msg.GasPrice 448 if rules.IsLondon { 449 effectiveTip = cmath.BigMin(msg.GasTipCap, new(big.Int).Sub(msg.GasFeeCap, st.evm.Context.BaseFee)) 450 } 451 effectiveTipU256, _ := uint256.FromBig(effectiveTip) 452 453 if st.evm.Config.NoBaseFee && msg.GasFeeCap.Sign() == 0 && msg.GasTipCap.Sign() == 0 { 454 // Skip fee payment when NoBaseFee is set and the fee fields 455 // are 0. This avoids a negative effectiveTip being applied to 456 // the coinbase when simulating calls. 457 } else { 458 fee := new(uint256.Int).SetUint64(st.gasUsed()) 459 fee.Mul(fee, effectiveTipU256) 460 st.state.AddBalance(st.evm.Context.Coinbase, fee, tracing.BalanceIncreaseRewardTransactionFee) 461 } 462 463 return &ExecutionResult{ 464 UsedGas: st.gasUsed(), 465 RefundedGas: gasRefund, 466 Err: vmerr, 467 ReturnData: ret, 468 }, nil 469 } 470 471 func (st *StateTransition) refundGas(refundQuotient uint64) uint64 { 472 // Apply refund counter, capped to a refund quotient 473 refund := st.gasUsed() / refundQuotient 474 if refund > st.state.GetRefund() { 475 refund = st.state.GetRefund() 476 } 477 478 if st.evm.Config.Tracer != nil && st.evm.Config.Tracer.OnGasChange != nil && refund > 0 { 479 st.evm.Config.Tracer.OnGasChange(st.gasRemaining, st.gasRemaining+refund, tracing.GasChangeTxRefunds) 480 } 481 482 st.gasRemaining += refund 483 484 // Return ETH for remaining gas, exchanged at the original rate. 485 remaining := uint256.NewInt(st.gasRemaining) 486 remaining.Mul(remaining, uint256.MustFromBig(st.msg.GasPrice)) 487 st.state.AddBalance(st.msg.From, remaining, tracing.BalanceIncreaseGasReturn) 488 489 if st.evm.Config.Tracer != nil && st.evm.Config.Tracer.OnGasChange != nil && st.gasRemaining > 0 { 490 st.evm.Config.Tracer.OnGasChange(st.gasRemaining, 0, tracing.GasChangeTxLeftOverReturned) 491 } 492 493 // Also return remaining gas to the block gas counter so it is 494 // available for the next transaction. 495 st.gp.AddGas(st.gasRemaining) 496 497 return refund 498 } 499 500 // gasUsed returns the amount of gas used up by the state transition. 501 func (st *StateTransition) gasUsed() uint64 { 502 return st.initialGas - st.gasRemaining 503 } 504 505 // blobGasUsed returns the amount of blob gas used by the message. 506 func (st *StateTransition) blobGasUsed() uint64 { 507 return uint64(len(st.msg.BlobHashes) * params.BlobTxBlobGasPerBlob) 508 }