EstimateGas returns an estimate of the amount of gas needed to execute the given transaction against the current pending block.
(ctx context.Context, args CallArgs)
| 784 | // EstimateGas returns an estimate of the amount of gas needed to execute the |
| 785 | // given transaction against the current pending block. |
| 786 | func (s *PublicBlockChainAPI) EstimateGas(ctx context.Context, args CallArgs) (hexutil.Uint64, error) { |
| 787 | // Binary search the gas requirement, as it may be higher than the amount used |
| 788 | var ( |
| 789 | lo uint64 = configs.TxGas - 1 |
| 790 | hi uint64 |
| 791 | cap uint64 |
| 792 | ) |
| 793 | if uint64(args.Gas) >= configs.TxGas { |
| 794 | hi = uint64(args.Gas) |
| 795 | } else { |
| 796 | // Retrieve the current pending block to act as the gas ceiling |
| 797 | block, err := s.b.BlockByNumber(ctx, rpc.PendingBlockNumber) |
| 798 | if err != nil { |
| 799 | return 0, err |
| 800 | } |
| 801 | hi = block.GasLimit() |
| 802 | } |
| 803 | cap = hi |
| 804 | |
| 805 | // Create a helper to check if a gas allowance results in an executable transaction |
| 806 | executable := func(gas uint64) bool { |
| 807 | args.Gas = hexutil.Uint64(gas) |
| 808 | |
| 809 | _, _, failed, err := s.doCall(ctx, args, rpc.PendingBlockNumber, vm.Config{}, 0) |
| 810 | if err != nil || failed { |
| 811 | return false |
| 812 | } |
| 813 | return true |
| 814 | } |
| 815 | // Execute the binary search and hone in on an executable gas limit |
| 816 | for lo+1 < hi { |
| 817 | mid := (hi + lo) / 2 |
| 818 | if !executable(mid) { |
| 819 | lo = mid |
| 820 | } else { |
| 821 | hi = mid |
| 822 | } |
| 823 | } |
| 824 | // Reject the transaction as invalid if it still fails at the highest allowance |
| 825 | if hi == cap { |
| 826 | if !executable(hi) { |
| 827 | return 0, fmt.Errorf("gas required exceeds allowance or always failing transaction") |
| 828 | } |
| 829 | } |
| 830 | return hexutil.Uint64(hi), nil |
| 831 | } |
| 832 | |
| 833 | // ExecutionResult groups all structured logs emitted by the EVM |
| 834 | // while replaying a transaction in debug mode as well as transaction |
nothing calls this directly
no test coverage detected