do.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package ethapi
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "github.com/ethereum/go-ethereum/common"
  7. "github.com/ethereum/go-ethereum/common/gopool"
  8. "github.com/ethereum/go-ethereum/common/hexutil"
  9. "github.com/ethereum/go-ethereum/core"
  10. "github.com/ethereum/go-ethereum/core/state"
  11. "github.com/ethereum/go-ethereum/core/types"
  12. "github.com/ethereum/go-ethereum/core/vm"
  13. "github.com/ethereum/go-ethereum/log"
  14. "github.com/ethereum/go-ethereum/params"
  15. "github.com/ethereum/go-ethereum/rpc"
  16. "math/big"
  17. "time"
  18. )
  19. func DoCall(ctx context.Context, b Backend, args CallArgs, blockNrOrHash rpc.BlockNumberOrHash, overrides *StateOverride, vmCfg vm.Config, timeout time.Duration, globalGasCap uint64) (*core.ExecutionResult, error) {
  20. defer func(start time.Time) { log.Debug("Executing EVM call finished", "runtime", time.Since(start)) }(time.Now())
  21. state, header, err := b.StateAndHeaderByNumberOrHash(ctx, blockNrOrHash)
  22. if state == nil || err != nil {
  23. return nil, err
  24. }
  25. if err := overrides.Apply(state); err != nil {
  26. return nil, err
  27. }
  28. // Setup context so it may be cancelled the call has completed
  29. // or, in case of unmetered gas, setup a context with a timeout.
  30. var cancel context.CancelFunc
  31. if timeout > 0 {
  32. ctx, cancel = context.WithTimeout(ctx, timeout)
  33. } else {
  34. ctx, cancel = context.WithCancel(ctx)
  35. }
  36. // Make sure the context is cancelled when the call has completed
  37. // this makes sure resources are cleaned up.
  38. defer cancel()
  39. return doCall(ctx, b, args, state, header, timeout, new(core.GasPool).AddGas(globalGasCap), nil)
  40. }
  41. func doCall(ctx context.Context, b Backend, args CallArgs, state *state.StateDB, header *types.Header, timeout time.Duration, gp *core.GasPool, blockContext *vm.BlockContext) (*core.ExecutionResult, error) {
  42. // Get a new instance of the EVM.
  43. msg := args.ToMessage(gp.Gas())
  44. evm, vmError, err := b.GetEVM(ctx, msg, state, header, nil, blockContext)
  45. if err != nil {
  46. return nil, err
  47. }
  48. // Wait for the context to be done and cancel the evm. Even if the
  49. // EVM has finished, cancelling may be done (repeatedly)
  50. gopool.Submit(func() {
  51. <-ctx.Done()
  52. evm.Cancel()
  53. })
  54. // Execute the message.
  55. //gp := new(core.GasPool).AddGas(math.MaxUint64)
  56. result, err := core.ApplyMessage(evm, msg, gp)
  57. if err := vmError(); err != nil {
  58. return nil, err
  59. }
  60. // If the timer caused an abort, return an appropriate error message
  61. if evm.Cancelled() {
  62. return nil, fmt.Errorf("execution aborted (timeout = %v)", timeout)
  63. }
  64. if err != nil {
  65. return result, fmt.Errorf("err: %w (supplied gas %d)", err, msg.Gas())
  66. }
  67. return result, nil
  68. }
  69. func DoEstimateGas(ctx context.Context, b Backend, args CallArgs, blockNrOrHash rpc.BlockNumberOrHash, gasCap uint64) (hexutil.Uint64, error) {
  70. // Binary search the gas requirement, as it may be higher than the amount used
  71. var (
  72. lo uint64 = params.TxGas - 1
  73. hi uint64
  74. cap uint64
  75. )
  76. // Use zero address if sender unspecified.
  77. if args.From == nil {
  78. args.From = new(common.Address)
  79. }
  80. // Determine the highest gas limit can be used during the estimation.
  81. if args.Gas != nil && uint64(*args.Gas) >= params.TxGas {
  82. hi = uint64(*args.Gas)
  83. } else {
  84. // Retrieve the block to act as the gas ceiling
  85. block, err := b.BlockByNumberOrHash(ctx, blockNrOrHash)
  86. if err != nil {
  87. return 0, err
  88. }
  89. if block == nil {
  90. return 0, errors.New("block not found")
  91. }
  92. hi = block.GasLimit()
  93. }
  94. // Recap the highest gas limit with account's available balance.
  95. if args.GasPrice != nil && args.GasPrice.ToInt().BitLen() != 0 {
  96. state, _, err := b.StateAndHeaderByNumberOrHash(ctx, blockNrOrHash)
  97. if err != nil {
  98. return 0, err
  99. }
  100. balance := state.GetBalance(*args.From) // from can't be nil
  101. available := new(big.Int).Set(balance)
  102. msgStr := balance
  103. log.Info("gas", "balnce", msgStr)
  104. if args.Value != nil {
  105. if args.Value.ToInt().Cmp(available) >= 0 {
  106. return 0, errors.New("insufficient funds for transfer")
  107. }
  108. available.Sub(available, args.Value.ToInt())
  109. }
  110. allowance := new(big.Int).Div(available, args.GasPrice.ToInt())
  111. // If the allowance is larger than maximum uint64, skip checking
  112. if allowance.IsUint64() && hi > allowance.Uint64() {
  113. transfer := args.Value
  114. if transfer == nil {
  115. transfer = new(hexutil.Big)
  116. }
  117. log.Warn("Gas estimation capped by limited funds", "original", hi, "balance", balance,
  118. "sent", transfer.ToInt(), "gasprice", args.GasPrice.ToInt(), "fundable", allowance)
  119. hi = allowance.Uint64()
  120. }
  121. }
  122. // Recap the highest gas allowance with specified gascap.
  123. if gasCap != 0 && hi > gasCap {
  124. log.Debug("Caller gas above allowance, capping", "requested", hi, "cap", gasCap)
  125. hi = gasCap
  126. }
  127. cap = hi
  128. // Create a helper to check if a gas allowance results in an executable transaction
  129. executable := func(gas uint64) (bool, *core.ExecutionResult, error) {
  130. args.Gas = (*hexutil.Uint64)(&gas)
  131. result, err := DoCall(ctx, b, args, blockNrOrHash, nil, vm.Config{}, 0, gasCap)
  132. if err != nil {
  133. if errors.Is(err, core.ErrIntrinsicGas) {
  134. return true, nil, nil // Special case, raise gas limit
  135. }
  136. return true, nil, err // Bail out
  137. }
  138. return result.Failed(), result, nil
  139. }
  140. // Execute the binary search and hone in on an executable gas limit
  141. for lo+1 < hi {
  142. mid := (hi + lo) / 2
  143. failed, _, err := executable(mid)
  144. // If the error is not nil(consensus error), it means the provided message
  145. // call or transaction will never be accepted no matter how much gas it is
  146. // assigned. Return the error directly, don't struggle any more.
  147. if err != nil {
  148. return 0, err
  149. }
  150. if failed {
  151. lo = mid
  152. } else {
  153. hi = mid
  154. }
  155. }
  156. // Reject the transaction as invalid if it still fails at the highest allowance
  157. if hi == cap {
  158. failed, result, err := executable(hi)
  159. if err != nil {
  160. return 0, err
  161. }
  162. if failed {
  163. if result != nil && result.Err != vm.ErrOutOfGas {
  164. if len(result.Revert()) > 0 {
  165. return 0, newRevertError(result)
  166. }
  167. return 0, result.Err
  168. }
  169. // Otherwise, the specified gas cap is too low
  170. return 0, fmt.Errorf("gas required exceeds allowance (%d)", cap)
  171. }
  172. }
  173. return hexutil.Uint64(hi), nil
  174. }