github.com/ethereum/go-ethereum@v1.14.4-0.20240516095835-473ee8fc07a3/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, 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 } 244 balanceCheck.Add(balanceCheck, st.msg.Value) 245 246 if st.evm.ChainConfig().IsCancun(st.evm.Context.BlockNumber, st.evm.Context.Time) { 247 if blobGas := st.blobGasUsed(); blobGas > 0 { 248 // Check that the user has enough funds to cover blobGasUsed * tx.BlobGasFeeCap 249 blobBalanceCheck := new(big.Int).SetUint64(blobGas) 250 blobBalanceCheck.Mul(blobBalanceCheck, st.msg.BlobGasFeeCap) 251 balanceCheck.Add(balanceCheck, blobBalanceCheck) 252 // Pay for blobGasUsed * actual blob fee 253 blobFee := new(big.Int).SetUint64(blobGas) 254 blobFee.Mul(blobFee, st.evm.Context.BlobBaseFee) 255 mgval.Add(mgval, blobFee) 256 } 257 } 258 balanceCheckU256, overflow := uint256.FromBig(balanceCheck) 259 if overflow { 260 return fmt.Errorf("%w: address %v required balance exceeds 256 bits", ErrInsufficientFunds, st.msg.From.Hex()) 261 } 262 if have, want := st.state.GetBalance(st.msg.From), balanceCheckU256; have.Cmp(want) < 0 { 263 return fmt.Errorf("%w: address %v have %v want %v", ErrInsufficientFunds, st.msg.From.Hex(), have, want) 264 } 265 if err := st.gp.SubGas(st.msg.GasLimit); err != nil { 266 return err 267 } 268 269 if st.evm.Config.Tracer != nil && st.evm.Config.Tracer.OnGasChange != nil { 270 st.evm.Config.Tracer.OnGasChange(0, st.msg.GasLimit, tracing.GasChangeTxInitialBalance) 271 } 272 st.gasRemaining = st.msg.GasLimit 273 274 st.initialGas = st.msg.GasLimit 275 mgvalU256, _ := uint256.FromBig(mgval) 276 st.state.SubBalance(st.msg.From, mgvalU256, tracing.BalanceDecreaseGasBuy) 277 return nil 278 } 279 280 func (st *StateTransition) preCheck() error { 281 // Only check transactions that are not fake 282 msg := st.msg 283 if !msg.SkipAccountChecks { 284 // Make sure this transaction's nonce is correct. 285 stNonce := st.state.GetNonce(msg.From) 286 if msgNonce := msg.Nonce; stNonce < msgNonce { 287 return fmt.Errorf("%w: address %v, tx: %d state: %d", ErrNonceTooHigh, 288 msg.From.Hex(), msgNonce, stNonce) 289 } else if stNonce > msgNonce { 290 return fmt.Errorf("%w: address %v, tx: %d state: %d", ErrNonceTooLow, 291 msg.From.Hex(), msgNonce, stNonce) 292 } else if stNonce+1 < stNonce { 293 return fmt.Errorf("%w: address %v, nonce: %d", ErrNonceMax, 294 msg.From.Hex(), stNonce) 295 } 296 // Make sure the sender is an EOA 297 codeHash := st.state.GetCodeHash(msg.From) 298 if codeHash != (common.Hash{}) && codeHash != types.EmptyCodeHash { 299 return fmt.Errorf("%w: address %v, codehash: %s", ErrSenderNoEOA, 300 msg.From.Hex(), codeHash) 301 } 302 } 303 // Make sure that transaction gasFeeCap is greater than the baseFee (post london) 304 if st.evm.ChainConfig().IsLondon(st.evm.Context.BlockNumber) { 305 // Skip the checks if gas fields are zero and baseFee was explicitly disabled (eth_call) 306 skipCheck := st.evm.Config.NoBaseFee && msg.GasFeeCap.BitLen() == 0 && msg.GasTipCap.BitLen() == 0 307 if !skipCheck { 308 if l := msg.GasFeeCap.BitLen(); l > 256 { 309 return fmt.Errorf("%w: address %v, maxFeePerGas bit length: %d", ErrFeeCapVeryHigh, 310 msg.From.Hex(), l) 311 } 312 if l := msg.GasTipCap.BitLen(); l > 256 { 313 return fmt.Errorf("%w: address %v, maxPriorityFeePerGas bit length: %d", ErrTipVeryHigh, 314 msg.From.Hex(), l) 315 } 316 if msg.GasFeeCap.Cmp(msg.GasTipCap) < 0 { 317 return fmt.Errorf("%w: address %v, maxPriorityFeePerGas: %s, maxFeePerGas: %s", ErrTipAboveFeeCap, 318 msg.From.Hex(), msg.GasTipCap, msg.GasFeeCap) 319 } 320 // This will panic if baseFee is nil, but basefee presence is verified 321 // as part of header validation. 322 if msg.GasFeeCap.Cmp(st.evm.Context.BaseFee) < 0 { 323 return fmt.Errorf("%w: address %v, maxFeePerGas: %s, baseFee: %s", ErrFeeCapTooLow, 324 msg.From.Hex(), msg.GasFeeCap, st.evm.Context.BaseFee) 325 } 326 } 327 } 328 // Check the blob version validity 329 if msg.BlobHashes != nil { 330 // The to field of a blob tx type is mandatory, and a `BlobTx` transaction internally 331 // has it as a non-nillable value, so any msg derived from blob transaction has it non-nil. 332 // However, messages created through RPC (eth_call) don't have this restriction. 333 if msg.To == nil { 334 return ErrBlobTxCreate 335 } 336 if len(msg.BlobHashes) == 0 { 337 return ErrMissingBlobHashes 338 } 339 for i, hash := range msg.BlobHashes { 340 if !kzg4844.IsValidVersionedHash(hash[:]) { 341 return fmt.Errorf("blob %d has invalid hash version", i) 342 } 343 } 344 } 345 // Check that the user is paying at least the current blob fee 346 if st.evm.ChainConfig().IsCancun(st.evm.Context.BlockNumber, st.evm.Context.Time) { 347 if st.blobGasUsed() > 0 { 348 // Skip the checks if gas fields are zero and blobBaseFee was explicitly disabled (eth_call) 349 skipCheck := st.evm.Config.NoBaseFee && msg.BlobGasFeeCap.BitLen() == 0 350 if !skipCheck { 351 // This will panic if blobBaseFee is nil, but blobBaseFee presence 352 // is verified as part of header validation. 353 if msg.BlobGasFeeCap.Cmp(st.evm.Context.BlobBaseFee) < 0 { 354 return fmt.Errorf("%w: address %v blobGasFeeCap: %v, blobBaseFee: %v", ErrBlobFeeCapTooLow, 355 msg.From.Hex(), msg.BlobGasFeeCap, st.evm.Context.BlobBaseFee) 356 } 357 } 358 } 359 } 360 return st.buyGas() 361 } 362 363 // TransitionDb will transition the state by applying the current message and 364 // returning the evm execution result with following fields. 365 // 366 // - used gas: total gas used (including gas being refunded) 367 // - returndata: the returned data from evm 368 // - concrete execution error: various EVM errors which abort the execution, e.g. 369 // ErrOutOfGas, ErrExecutionReverted 370 // 371 // However if any consensus issue encountered, return the error directly with 372 // nil evm execution result. 373 func (st *StateTransition) TransitionDb() (*ExecutionResult, error) { 374 // First check this message satisfies all consensus rules before 375 // applying the message. The rules include these clauses 376 // 377 // 1. the nonce of the message caller is correct 378 // 2. caller has enough balance to cover transaction fee(gaslimit * gasprice) 379 // 3. the amount of gas required is available in the block 380 // 4. the purchased gas is enough to cover intrinsic usage 381 // 5. there is no overflow when calculating intrinsic gas 382 // 6. caller has enough balance to cover asset transfer for **topmost** call 383 384 // Check clauses 1-3, buy gas if everything is correct 385 if err := st.preCheck(); err != nil { 386 return nil, err 387 } 388 389 var ( 390 msg = st.msg 391 sender = vm.AccountRef(msg.From) 392 rules = st.evm.ChainConfig().Rules(st.evm.Context.BlockNumber, st.evm.Context.Random != nil, st.evm.Context.Time) 393 contractCreation = msg.To == nil 394 ) 395 396 // Check clauses 4-5, subtract intrinsic gas if everything is correct 397 gas, err := IntrinsicGas(msg.Data, msg.AccessList, contractCreation, rules.IsHomestead, rules.IsIstanbul, rules.IsShanghai) 398 if err != nil { 399 return nil, err 400 } 401 if st.gasRemaining < gas { 402 return nil, fmt.Errorf("%w: have %d, want %d", ErrIntrinsicGas, st.gasRemaining, gas) 403 } 404 if t := st.evm.Config.Tracer; t != nil && t.OnGasChange != nil { 405 t.OnGasChange(st.gasRemaining, st.gasRemaining-gas, tracing.GasChangeTxIntrinsicGas) 406 } 407 st.gasRemaining -= gas 408 409 if rules.IsEIP4762 { 410 st.evm.AccessEvents.AddTxOrigin(msg.From) 411 412 if targetAddr := msg.To; targetAddr != nil { 413 st.evm.AccessEvents.AddTxDestination(*targetAddr, msg.Value.Sign() != 0) 414 } 415 } 416 417 // Check clause 6 418 value, overflow := uint256.FromBig(msg.Value) 419 if overflow { 420 return nil, fmt.Errorf("%w: address %v", ErrInsufficientFundsForTransfer, msg.From.Hex()) 421 } 422 if !value.IsZero() && !st.evm.Context.CanTransfer(st.state, msg.From, value) { 423 return nil, fmt.Errorf("%w: address %v", ErrInsufficientFundsForTransfer, msg.From.Hex()) 424 } 425 426 // Check whether the init code size has been exceeded. 427 if rules.IsShanghai && contractCreation && len(msg.Data) > params.MaxInitCodeSize { 428 return nil, fmt.Errorf("%w: code size %v limit %v", ErrMaxInitCodeSizeExceeded, len(msg.Data), params.MaxInitCodeSize) 429 } 430 431 // Execute the preparatory steps for state transition which includes: 432 // - prepare accessList(post-berlin) 433 // - reset transient storage(eip 1153) 434 st.state.Prepare(rules, msg.From, st.evm.Context.Coinbase, msg.To, vm.ActivePrecompiles(rules), msg.AccessList) 435 436 var ( 437 ret []byte 438 vmerr error // vm errors do not effect consensus and are therefore not assigned to err 439 ) 440 if contractCreation { 441 ret, _, st.gasRemaining, vmerr = st.evm.Create(sender, msg.Data, st.gasRemaining, value) 442 } else { 443 // Increment the nonce for the next transaction 444 st.state.SetNonce(msg.From, st.state.GetNonce(sender.Address())+1) 445 ret, st.gasRemaining, vmerr = st.evm.Call(sender, st.to(), msg.Data, st.gasRemaining, value) 446 } 447 448 var gasRefund uint64 449 if !rules.IsLondon { 450 // Before EIP-3529: refunds were capped to gasUsed / 2 451 gasRefund = st.refundGas(params.RefundQuotient) 452 } else { 453 // After EIP-3529: refunds are capped to gasUsed / 5 454 gasRefund = st.refundGas(params.RefundQuotientEIP3529) 455 } 456 effectiveTip := msg.GasPrice 457 if rules.IsLondon { 458 effectiveTip = cmath.BigMin(msg.GasTipCap, new(big.Int).Sub(msg.GasFeeCap, st.evm.Context.BaseFee)) 459 } 460 effectiveTipU256, _ := uint256.FromBig(effectiveTip) 461 462 if st.evm.Config.NoBaseFee && msg.GasFeeCap.Sign() == 0 && msg.GasTipCap.Sign() == 0 { 463 // Skip fee payment when NoBaseFee is set and the fee fields 464 // are 0. This avoids a negative effectiveTip being applied to 465 // the coinbase when simulating calls. 466 } else { 467 fee := new(uint256.Int).SetUint64(st.gasUsed()) 468 fee.Mul(fee, effectiveTipU256) 469 st.state.AddBalance(st.evm.Context.Coinbase, fee, tracing.BalanceIncreaseRewardTransactionFee) 470 471 // add the coinbase to the witness iff the fee is greater than 0 472 if rules.IsEIP4762 && fee.Sign() != 0 { 473 st.evm.AccessEvents.BalanceGas(st.evm.Context.Coinbase, true) 474 } 475 } 476 477 return &ExecutionResult{ 478 UsedGas: st.gasUsed(), 479 RefundedGas: gasRefund, 480 Err: vmerr, 481 ReturnData: ret, 482 }, nil 483 } 484 485 func (st *StateTransition) refundGas(refundQuotient uint64) uint64 { 486 // Apply refund counter, capped to a refund quotient 487 refund := st.gasUsed() / refundQuotient 488 if refund > st.state.GetRefund() { 489 refund = st.state.GetRefund() 490 } 491 492 if st.evm.Config.Tracer != nil && st.evm.Config.Tracer.OnGasChange != nil && refund > 0 { 493 st.evm.Config.Tracer.OnGasChange(st.gasRemaining, st.gasRemaining+refund, tracing.GasChangeTxRefunds) 494 } 495 496 st.gasRemaining += refund 497 498 // Return ETH for remaining gas, exchanged at the original rate. 499 remaining := uint256.NewInt(st.gasRemaining) 500 remaining.Mul(remaining, uint256.MustFromBig(st.msg.GasPrice)) 501 st.state.AddBalance(st.msg.From, remaining, tracing.BalanceIncreaseGasReturn) 502 503 if st.evm.Config.Tracer != nil && st.evm.Config.Tracer.OnGasChange != nil && st.gasRemaining > 0 { 504 st.evm.Config.Tracer.OnGasChange(st.gasRemaining, 0, tracing.GasChangeTxLeftOverReturned) 505 } 506 507 // Also return remaining gas to the block gas counter so it is 508 // available for the next transaction. 509 st.gp.AddGas(st.gasRemaining) 510 511 return refund 512 } 513 514 // gasUsed returns the amount of gas used up by the state transition. 515 func (st *StateTransition) gasUsed() uint64 { 516 return st.initialGas - st.gasRemaining 517 } 518 519 // blobGasUsed returns the amount of blob gas used by the message. 520 func (st *StateTransition) blobGasUsed() uint64 { 521 return uint64(len(st.msg.BlobHashes) * params.BlobTxBlobGasPerBlob) 522 }