2020-08-19 18:46:20 +07:00
|
|
|
package transactions
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-08-29 22:28:09 +01:00
|
|
|
"time"
|
|
|
|
|
2020-08-19 18:46:20 +07:00
|
|
|
"github.com/holiman/uint256"
|
2022-12-15 12:25:42 +00:00
|
|
|
"github.com/ledgerwatch/log/v3"
|
|
|
|
|
2023-06-02 22:26:19 +02:00
|
|
|
"github.com/ledgerwatch/erigon-lib/chain"
|
|
|
|
libcommon "github.com/ledgerwatch/erigon-lib/common"
|
2021-07-29 18:53:13 +07:00
|
|
|
"github.com/ledgerwatch/erigon-lib/kv"
|
2023-01-13 18:12:18 +00:00
|
|
|
|
Fix trace error in Polygon | Pass Engin to the Base API (#6131)
So there is an issue with tracing certain blocks/transactions on
Polygon, for example:
```
> '{"method": "trace_transaction","params":["0xb198d93f640343a98f90d93aa2b74b4fc5c64f3a649f1608d2bfd1004f9dee0e"],"id":1,"jsonrpc":"2.0"}'
```
gives the error `first run for txIndex 1 error: insufficient funds for
gas * price + value: address 0x10AD27A96CDBffC90ab3b83bF695911426A69f5E
have 16927727762862809 want 17594166808296934`
The reason is that this transaction is from the author of the block,
which doesn't have enough ETH to pay for the gas fee + tx value if he's
not the block author receiving transactions fees.
The issue is that currently the APIs are using `ethash.NewFaker()`
Engine for running traces, etc. which doesn't know how to get the author
for a specific block (which is consensus dependant); as it was noting in
several TODO comments.
The fix is to pass the Engine to the BaseAPI, which can then be used to
create the right Block Context. I chose to split the current Engine
interface in 2, with Reader and Writer, so that the BaseAPI only
receives the Reader one, which might be safer (even though it's only
used for getting the block Author).
2022-12-04 06:17:39 +01:00
|
|
|
"github.com/ledgerwatch/erigon/consensus"
|
2021-05-21 01:25:53 +07:00
|
|
|
"github.com/ledgerwatch/erigon/core"
|
|
|
|
"github.com/ledgerwatch/erigon/core/state"
|
|
|
|
"github.com/ledgerwatch/erigon/core/types"
|
|
|
|
"github.com/ledgerwatch/erigon/core/vm"
|
2023-06-02 22:26:19 +02:00
|
|
|
"github.com/ledgerwatch/erigon/core/vm/evmtypes"
|
2021-05-21 01:25:53 +07:00
|
|
|
"github.com/ledgerwatch/erigon/rpc"
|
2022-10-25 09:58:25 +07:00
|
|
|
ethapi2 "github.com/ledgerwatch/erigon/turbo/adapter/ethapi"
|
2022-06-12 12:44:01 +01:00
|
|
|
"github.com/ledgerwatch/erigon/turbo/services"
|
2020-08-19 18:46:20 +07:00
|
|
|
)
|
|
|
|
|
2022-02-23 23:42:14 +00:00
|
|
|
func DoCall(
|
|
|
|
ctx context.Context,
|
Fix trace error in Polygon | Pass Engin to the Base API (#6131)
So there is an issue with tracing certain blocks/transactions on
Polygon, for example:
```
> '{"method": "trace_transaction","params":["0xb198d93f640343a98f90d93aa2b74b4fc5c64f3a649f1608d2bfd1004f9dee0e"],"id":1,"jsonrpc":"2.0"}'
```
gives the error `first run for txIndex 1 error: insufficient funds for
gas * price + value: address 0x10AD27A96CDBffC90ab3b83bF695911426A69f5E
have 16927727762862809 want 17594166808296934`
The reason is that this transaction is from the author of the block,
which doesn't have enough ETH to pay for the gas fee + tx value if he's
not the block author receiving transactions fees.
The issue is that currently the APIs are using `ethash.NewFaker()`
Engine for running traces, etc. which doesn't know how to get the author
for a specific block (which is consensus dependant); as it was noting in
several TODO comments.
The fix is to pass the Engine to the BaseAPI, which can then be used to
create the right Block Context. I chose to split the current Engine
interface in 2, with Reader and Writer, so that the BaseAPI only
receives the Reader one, which might be safer (even though it's only
used for getting the block Author).
2022-12-04 06:17:39 +01:00
|
|
|
engine consensus.EngineReader,
|
2022-10-25 09:58:25 +07:00
|
|
|
args ethapi2.CallArgs,
|
2022-11-28 02:03:03 +00:00
|
|
|
tx kv.Tx,
|
|
|
|
blockNrOrHash rpc.BlockNumberOrHash,
|
|
|
|
header *types.Header,
|
|
|
|
overrides *ethapi2.StateOverrides,
|
2022-02-23 23:42:14 +00:00
|
|
|
gasCap uint64,
|
2023-01-13 18:12:18 +00:00
|
|
|
chainConfig *chain.Config,
|
2022-09-05 21:31:00 +07:00
|
|
|
stateReader state.StateReader,
|
2022-11-28 02:03:03 +00:00
|
|
|
headerReader services.HeaderReader,
|
|
|
|
callTimeout time.Duration,
|
2022-02-23 23:42:14 +00:00
|
|
|
) (*core.ExecutionResult, error) {
|
2020-08-19 18:46:20 +07:00
|
|
|
// todo: Pending state is only known by the miner
|
|
|
|
/*
|
|
|
|
if blockNrOrHash.BlockNumber != nil && *blockNrOrHash.BlockNumber == rpc.PendingBlockNumber {
|
|
|
|
block, state, _ := b.eth.miner.Pending()
|
|
|
|
return state, block.Header(), nil
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2022-11-28 02:03:03 +00:00
|
|
|
state := state.New(stateReader)
|
2020-08-19 18:46:20 +07:00
|
|
|
|
|
|
|
// Override the fields of specified contracts before execution.
|
|
|
|
if overrides != nil {
|
2022-03-01 15:40:24 +00:00
|
|
|
if err := overrides.Override(state); err != nil {
|
|
|
|
return nil, err
|
2020-08-19 18:46:20 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup context so it may be cancelled the call has completed
|
|
|
|
// or, in case of unmetered gas, setup a context with a timeout.
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
if callTimeout > 0 {
|
|
|
|
ctx, cancel = context.WithTimeout(ctx, callTimeout)
|
|
|
|
} else {
|
|
|
|
ctx, cancel = context.WithCancel(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the context is cancelled when the call has completed
|
|
|
|
// this makes sure resources are cleaned up.
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// Get a new instance of the EVM.
|
2021-07-11 04:05:56 +00:00
|
|
|
var baseFee *uint256.Int
|
|
|
|
if header != nil && header.BaseFee != nil {
|
|
|
|
var overflow bool
|
|
|
|
baseFee, overflow = uint256.FromBig(header.BaseFee)
|
|
|
|
if overflow {
|
|
|
|
return nil, fmt.Errorf("header.BaseFee uint256 overflow")
|
|
|
|
}
|
|
|
|
}
|
2021-07-17 09:09:56 +07:00
|
|
|
msg, err := args.ToMessage(gasCap, baseFee)
|
2021-07-11 04:05:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-12-05 12:42:08 +07:00
|
|
|
blockCtx := NewEVMBlockContext(engine, header, blockNrOrHash.RequireCanonical, tx, headerReader)
|
|
|
|
txCtx := core.NewEVMTxContext(msg)
|
2020-08-19 18:46:20 +07:00
|
|
|
|
2021-07-11 04:05:56 +00:00
|
|
|
evm := vm.NewEVM(blockCtx, txCtx, state, chainConfig, vm.Config{NoBaseFee: true})
|
2020-08-19 18:46:20 +07:00
|
|
|
|
|
|
|
// Wait for the context to be done and cancel the evm. Even if the
|
|
|
|
// EVM has finished, cancelling may be done (repeatedly)
|
|
|
|
go func() {
|
|
|
|
<-ctx.Done()
|
|
|
|
evm.Cancel()
|
|
|
|
}()
|
|
|
|
|
2023-05-03 08:02:30 +06:00
|
|
|
gp := new(core.GasPool).AddGas(msg.Gas()).AddDataGas(msg.DataGas())
|
2021-02-12 16:47:32 +00:00
|
|
|
result, err := core.ApplyMessage(evm, msg, gp, true /* refunds */, false /* gasBailout */)
|
2020-08-19 18:46:20 +07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the timer caused an abort, return an appropriate error message
|
|
|
|
if evm.Cancelled() {
|
|
|
|
return nil, fmt.Errorf("execution aborted (timeout = %v)", callTimeout)
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2022-12-05 12:42:08 +07:00
|
|
|
func NewEVMBlockContext(engine consensus.EngineReader, header *types.Header, requireCanonical bool, tx kv.Tx, headerReader services.HeaderReader) evmtypes.BlockContext {
|
2023-06-02 22:26:19 +02:00
|
|
|
return core.NewEVMBlockContext(header, MakeHeaderGetter(requireCanonical, tx, headerReader), engine, nil /* author */)
|
2020-08-19 18:46:20 +07:00
|
|
|
}
|
|
|
|
|
2023-01-13 18:12:18 +00:00
|
|
|
func MakeHeaderGetter(requireCanonical bool, tx kv.Tx, headerReader services.HeaderReader) func(uint64) libcommon.Hash {
|
|
|
|
return func(n uint64) libcommon.Hash {
|
2022-06-12 12:44:01 +01:00
|
|
|
h, err := headerReader.HeaderByNumber(context.Background(), tx, n)
|
2020-08-19 18:46:20 +07:00
|
|
|
if err != nil {
|
2022-06-12 12:44:01 +01:00
|
|
|
log.Error("Can't get block hash by number", "number", n, "only-canonical", requireCanonical)
|
2023-01-13 18:12:18 +00:00
|
|
|
return libcommon.Hash{}
|
2020-08-19 18:46:20 +07:00
|
|
|
}
|
2023-01-25 16:29:41 +07:00
|
|
|
if h == nil {
|
|
|
|
log.Warn("[evm] header is nil", "blockNum", n)
|
|
|
|
return libcommon.Hash{}
|
|
|
|
}
|
2022-06-12 12:44:01 +01:00
|
|
|
return h.Hash()
|
2020-08-19 18:46:20 +07:00
|
|
|
}
|
|
|
|
}
|
2022-11-28 02:03:03 +00:00
|
|
|
|
|
|
|
type ReusableCaller struct {
|
|
|
|
evm *vm.EVM
|
|
|
|
intraBlockState *state.IntraBlockState
|
|
|
|
gasCap uint64
|
|
|
|
baseFee *uint256.Int
|
|
|
|
stateReader state.StateReader
|
|
|
|
callTimeout time.Duration
|
|
|
|
message *types.Message
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *ReusableCaller) DoCallWithNewGas(
|
|
|
|
ctx context.Context,
|
|
|
|
newGas uint64,
|
|
|
|
) (*core.ExecutionResult, error) {
|
|
|
|
var cancel context.CancelFunc
|
|
|
|
if r.callTimeout > 0 {
|
|
|
|
ctx, cancel = context.WithTimeout(ctx, r.callTimeout)
|
|
|
|
} else {
|
|
|
|
ctx, cancel = context.WithCancel(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the context is cancelled when the call has completed
|
|
|
|
// this makes sure resources are cleaned up.
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
r.message.ChangeGas(r.gasCap, newGas)
|
|
|
|
|
|
|
|
// reset the EVM so that we can continue to use it with the new context
|
|
|
|
txCtx := core.NewEVMTxContext(r.message)
|
2022-12-15 12:25:42 +00:00
|
|
|
r.intraBlockState = state.New(r.stateReader)
|
2022-11-28 02:03:03 +00:00
|
|
|
r.evm.Reset(txCtx, r.intraBlockState)
|
|
|
|
|
|
|
|
timedOut := false
|
|
|
|
go func() {
|
|
|
|
<-ctx.Done()
|
|
|
|
timedOut = true
|
|
|
|
}()
|
|
|
|
|
2023-05-03 08:02:30 +06:00
|
|
|
gp := new(core.GasPool).AddGas(r.message.Gas()).AddDataGas(r.message.DataGas())
|
2022-11-28 02:03:03 +00:00
|
|
|
|
|
|
|
result, err := core.ApplyMessage(r.evm, r.message, gp, true /* refunds */, false /* gasBailout */)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the timer caused an abort, return an appropriate error message
|
|
|
|
if timedOut {
|
|
|
|
return nil, fmt.Errorf("execution aborted (timeout = %v)", r.callTimeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewReusableCaller(
|
Fix trace error in Polygon | Pass Engin to the Base API (#6131)
So there is an issue with tracing certain blocks/transactions on
Polygon, for example:
```
> '{"method": "trace_transaction","params":["0xb198d93f640343a98f90d93aa2b74b4fc5c64f3a649f1608d2bfd1004f9dee0e"],"id":1,"jsonrpc":"2.0"}'
```
gives the error `first run for txIndex 1 error: insufficient funds for
gas * price + value: address 0x10AD27A96CDBffC90ab3b83bF695911426A69f5E
have 16927727762862809 want 17594166808296934`
The reason is that this transaction is from the author of the block,
which doesn't have enough ETH to pay for the gas fee + tx value if he's
not the block author receiving transactions fees.
The issue is that currently the APIs are using `ethash.NewFaker()`
Engine for running traces, etc. which doesn't know how to get the author
for a specific block (which is consensus dependant); as it was noting in
several TODO comments.
The fix is to pass the Engine to the BaseAPI, which can then be used to
create the right Block Context. I chose to split the current Engine
interface in 2, with Reader and Writer, so that the BaseAPI only
receives the Reader one, which might be safer (even though it's only
used for getting the block Author).
2022-12-04 06:17:39 +01:00
|
|
|
engine consensus.EngineReader,
|
2022-11-28 02:03:03 +00:00
|
|
|
stateReader state.StateReader,
|
|
|
|
overrides *ethapi2.StateOverrides,
|
|
|
|
header *types.Header,
|
|
|
|
initialArgs ethapi2.CallArgs,
|
|
|
|
gasCap uint64,
|
|
|
|
blockNrOrHash rpc.BlockNumberOrHash,
|
|
|
|
tx kv.Tx,
|
|
|
|
headerReader services.HeaderReader,
|
2023-01-13 18:12:18 +00:00
|
|
|
chainConfig *chain.Config,
|
2022-11-28 02:03:03 +00:00
|
|
|
callTimeout time.Duration,
|
|
|
|
) (*ReusableCaller, error) {
|
2022-12-15 12:25:42 +00:00
|
|
|
ibs := state.New(stateReader)
|
2022-11-28 02:03:03 +00:00
|
|
|
|
|
|
|
if overrides != nil {
|
2022-12-15 12:25:42 +00:00
|
|
|
if err := overrides.Override(ibs); err != nil {
|
2022-11-28 02:03:03 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var baseFee *uint256.Int
|
|
|
|
if header != nil && header.BaseFee != nil {
|
|
|
|
var overflow bool
|
|
|
|
baseFee, overflow = uint256.FromBig(header.BaseFee)
|
|
|
|
if overflow {
|
|
|
|
return nil, fmt.Errorf("header.BaseFee uint256 overflow")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
msg, err := initialArgs.ToMessage(gasCap, baseFee)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-12-05 12:42:08 +07:00
|
|
|
|
|
|
|
blockCtx := NewEVMBlockContext(engine, header, blockNrOrHash.RequireCanonical, tx, headerReader)
|
|
|
|
txCtx := core.NewEVMTxContext(msg)
|
2022-11-28 02:03:03 +00:00
|
|
|
|
2022-12-15 12:25:42 +00:00
|
|
|
evm := vm.NewEVM(blockCtx, txCtx, ibs, chainConfig, vm.Config{NoBaseFee: true})
|
2022-11-28 02:03:03 +00:00
|
|
|
|
|
|
|
return &ReusableCaller{
|
|
|
|
evm: evm,
|
2022-12-15 12:25:42 +00:00
|
|
|
intraBlockState: ibs,
|
2022-11-28 02:03:03 +00:00
|
|
|
baseFee: baseFee,
|
|
|
|
gasCap: gasCap,
|
|
|
|
callTimeout: callTimeout,
|
|
|
|
stateReader: stateReader,
|
|
|
|
message: &msg,
|
|
|
|
}, nil
|
|
|
|
}
|