|
|
@ -674,7 +674,7 @@ type CallArgs struct { |
|
|
|
Data *hexutil.Bytes `json:"data"` |
|
|
|
Data *hexutil.Bytes `json:"data"` |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func DoCall(ctx context.Context, b Backend, args CallArgs, blockNr rpc.BlockNumber, vmCfg vm.Config, timeout time.Duration) ([]byte, uint64, bool, error) { |
|
|
|
func DoCall(ctx context.Context, b Backend, args CallArgs, blockNr rpc.BlockNumber, vmCfg vm.Config, timeout time.Duration, globalGasCap *big.Int) ([]byte, uint64, bool, error) { |
|
|
|
defer func(start time.Time) { log.Debug("Executing EVM call finished", "runtime", time.Since(start)) }(time.Now()) |
|
|
|
defer func(start time.Time) { log.Debug("Executing EVM call finished", "runtime", time.Since(start)) }(time.Now()) |
|
|
|
|
|
|
|
|
|
|
|
state, header, err := b.StateAndHeaderByNumber(ctx, blockNr) |
|
|
|
state, header, err := b.StateAndHeaderByNumber(ctx, blockNr) |
|
|
@ -697,6 +697,10 @@ func DoCall(ctx context.Context, b Backend, args CallArgs, blockNr rpc.BlockNumb |
|
|
|
if args.Gas != nil { |
|
|
|
if args.Gas != nil { |
|
|
|
gas = uint64(*args.Gas) |
|
|
|
gas = uint64(*args.Gas) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if globalGasCap != nil && globalGasCap.Uint64() < gas { |
|
|
|
|
|
|
|
log.Warn("Caller gas above allowance, capping", "requested", gas, "cap", globalGasCap) |
|
|
|
|
|
|
|
gas = globalGasCap.Uint64() |
|
|
|
|
|
|
|
} |
|
|
|
gasPrice := new(big.Int).SetUint64(defaultGasPrice) |
|
|
|
gasPrice := new(big.Int).SetUint64(defaultGasPrice) |
|
|
|
if args.GasPrice != nil { |
|
|
|
if args.GasPrice != nil { |
|
|
|
gasPrice = args.GasPrice.ToInt() |
|
|
|
gasPrice = args.GasPrice.ToInt() |
|
|
@ -752,11 +756,11 @@ func DoCall(ctx context.Context, b Backend, args CallArgs, blockNr rpc.BlockNumb |
|
|
|
// Call executes the given transaction on the state for the given block number.
|
|
|
|
// Call executes the given transaction on the state for the given block number.
|
|
|
|
// It doesn't make and changes in the state/blockchain and is useful to execute and retrieve values.
|
|
|
|
// It doesn't make and changes in the state/blockchain and is useful to execute and retrieve values.
|
|
|
|
func (s *PublicBlockChainAPI) Call(ctx context.Context, args CallArgs, blockNr rpc.BlockNumber) (hexutil.Bytes, error) { |
|
|
|
func (s *PublicBlockChainAPI) Call(ctx context.Context, args CallArgs, blockNr rpc.BlockNumber) (hexutil.Bytes, error) { |
|
|
|
result, _, _, err := DoCall(ctx, s.b, args, blockNr, vm.Config{}, 5*time.Second) |
|
|
|
result, _, _, err := DoCall(ctx, s.b, args, blockNr, vm.Config{}, 5*time.Second, s.b.RPCGasCap()) |
|
|
|
return (hexutil.Bytes)(result), err |
|
|
|
return (hexutil.Bytes)(result), err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func DoEstimateGas(ctx context.Context, b Backend, args CallArgs, blockNr rpc.BlockNumber) (hexutil.Uint64, error) { |
|
|
|
func DoEstimateGas(ctx context.Context, b Backend, args CallArgs, blockNr rpc.BlockNumber, gasCap *big.Int) (hexutil.Uint64, error) { |
|
|
|
// Binary search the gas requirement, as it may be higher than the amount used
|
|
|
|
// Binary search the gas requirement, as it may be higher than the amount used
|
|
|
|
var ( |
|
|
|
var ( |
|
|
|
lo uint64 = params.TxGas - 1 |
|
|
|
lo uint64 = params.TxGas - 1 |
|
|
@ -773,13 +777,17 @@ func DoEstimateGas(ctx context.Context, b Backend, args CallArgs, blockNr rpc.Bl |
|
|
|
} |
|
|
|
} |
|
|
|
hi = block.GasLimit() |
|
|
|
hi = block.GasLimit() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if gasCap != nil && hi > gasCap.Uint64() { |
|
|
|
|
|
|
|
log.Warn("Caller gas above allowance, capping", "requested", hi, "cap", gasCap) |
|
|
|
|
|
|
|
hi = gasCap.Uint64() |
|
|
|
|
|
|
|
} |
|
|
|
cap = hi |
|
|
|
cap = hi |
|
|
|
|
|
|
|
|
|
|
|
// Create a helper to check if a gas allowance results in an executable transaction
|
|
|
|
// Create a helper to check if a gas allowance results in an executable transaction
|
|
|
|
executable := func(gas uint64) bool { |
|
|
|
executable := func(gas uint64) bool { |
|
|
|
args.Gas = (*hexutil.Uint64)(&gas) |
|
|
|
args.Gas = (*hexutil.Uint64)(&gas) |
|
|
|
|
|
|
|
|
|
|
|
_, _, failed, err := DoCall(ctx, b, args, rpc.PendingBlockNumber, vm.Config{}, 0) |
|
|
|
_, _, failed, err := DoCall(ctx, b, args, rpc.PendingBlockNumber, vm.Config{}, 0, gasCap) |
|
|
|
if err != nil || failed { |
|
|
|
if err != nil || failed { |
|
|
|
return false |
|
|
|
return false |
|
|
|
} |
|
|
|
} |
|
|
@ -797,7 +805,7 @@ func DoEstimateGas(ctx context.Context, b Backend, args CallArgs, blockNr rpc.Bl |
|
|
|
// Reject the transaction as invalid if it still fails at the highest allowance
|
|
|
|
// Reject the transaction as invalid if it still fails at the highest allowance
|
|
|
|
if hi == cap { |
|
|
|
if hi == cap { |
|
|
|
if !executable(hi) { |
|
|
|
if !executable(hi) { |
|
|
|
return 0, fmt.Errorf("gas required exceeds allowance or always failing transaction") |
|
|
|
return 0, fmt.Errorf("gas required exceeds allowance (%d) or always failing transaction", cap) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
return hexutil.Uint64(hi), nil |
|
|
|
return hexutil.Uint64(hi), nil |
|
|
@ -806,7 +814,7 @@ func DoEstimateGas(ctx context.Context, b Backend, args CallArgs, blockNr rpc.Bl |
|
|
|
// EstimateGas returns an estimate of the amount of gas needed to execute the
|
|
|
|
// EstimateGas returns an estimate of the amount of gas needed to execute the
|
|
|
|
// given transaction against the current pending block.
|
|
|
|
// given transaction against the current pending block.
|
|
|
|
func (s *PublicBlockChainAPI) EstimateGas(ctx context.Context, args CallArgs) (hexutil.Uint64, error) { |
|
|
|
func (s *PublicBlockChainAPI) EstimateGas(ctx context.Context, args CallArgs) (hexutil.Uint64, error) { |
|
|
|
return DoEstimateGas(ctx, s.b, args, rpc.PendingBlockNumber) |
|
|
|
return DoEstimateGas(ctx, s.b, args, rpc.PendingBlockNumber, s.b.RPCGasCap()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// ExecutionResult groups all structured logs emitted by the EVM
|
|
|
|
// ExecutionResult groups all structured logs emitted by the EVM
|
|
|
|