2020-08-19 11:46:20 +00:00
|
|
|
package transactions
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2020-08-29 21:28:09 +00:00
|
|
|
"math/big"
|
|
|
|
"time"
|
|
|
|
|
2020-08-19 11:46:20 +00:00
|
|
|
"github.com/holiman/uint256"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/common"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/core"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/core/rawdb"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/core/state"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/core/types"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/core/vm"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/ethdb"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/internal/ethapi"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/log"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/params"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/rpc"
|
|
|
|
"github.com/ledgerwatch/turbo-geth/turbo/rpchelper"
|
|
|
|
)
|
|
|
|
|
2020-09-08 19:40:01 +00:00
|
|
|
const callTimeout = 5 * time.Minute
|
2020-08-19 11:46:20 +00:00
|
|
|
|
2021-03-30 09:53:54 +00:00
|
|
|
func DoCall(ctx context.Context, args ethapi.CallArgs, tx ethdb.Tx, blockNrOrHash rpc.BlockNumberOrHash, overrides *map[common.Address]ethapi.Account, GasCap uint64, chainConfig *params.ChainConfig, pending *rpchelper.Pending) (*core.ExecutionResult, error) {
|
2020-08-19 11:46:20 +00: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
|
|
|
|
}
|
|
|
|
*/
|
2021-03-30 07:09:00 +00:00
|
|
|
blockNumber, hash, err := rpchelper.GetBlockNumber(blockNrOrHash, tx, pending)
|
2020-08-19 11:46:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-04 08:01:06 +00:00
|
|
|
var stateReader state.StateReader
|
|
|
|
if num, ok := blockNrOrHash.Number(); ok && num == rpc.LatestBlockNumber {
|
2021-04-05 13:04:58 +00:00
|
|
|
stateReader = state.NewPlainStateReader(tx)
|
2020-10-04 08:01:06 +00:00
|
|
|
} else {
|
2021-04-26 05:37:48 +00:00
|
|
|
stateReader = state.NewPlainKvState(tx, blockNumber)
|
2020-08-19 11:46:20 +00:00
|
|
|
}
|
2020-10-04 08:01:06 +00:00
|
|
|
state := state.New(stateReader)
|
2020-08-19 11:46:20 +00:00
|
|
|
|
2021-03-30 09:53:54 +00:00
|
|
|
header := rawdb.ReadHeader(ethdb.NewRoTxDb(tx), hash, blockNumber)
|
2020-08-19 11:46:20 +00:00
|
|
|
if header == nil {
|
|
|
|
return nil, fmt.Errorf("block %d(%x) not found", blockNumber, hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Override the fields of specified contracts before execution.
|
|
|
|
if overrides != nil {
|
|
|
|
for addr, account := range *overrides {
|
|
|
|
// Override account nonce.
|
|
|
|
if account.Nonce != nil {
|
|
|
|
state.SetNonce(addr, uint64(*account.Nonce))
|
|
|
|
}
|
|
|
|
// Override account(contract) code.
|
|
|
|
if account.Code != nil {
|
|
|
|
state.SetCode(addr, *account.Code)
|
|
|
|
}
|
|
|
|
// Override account balance.
|
|
|
|
if account.Balance != nil {
|
|
|
|
balance, _ := uint256.FromBig((*big.Int)(*account.Balance))
|
|
|
|
state.SetBalance(addr, balance)
|
|
|
|
}
|
|
|
|
if account.State != nil && account.StateDiff != nil {
|
|
|
|
return nil, fmt.Errorf("account %s has both 'state' and 'stateDiff'", addr.Hex())
|
|
|
|
}
|
|
|
|
// Replace entire state if caller requires.
|
|
|
|
if account.State != nil {
|
|
|
|
state.SetStorage(addr, *account.State)
|
|
|
|
}
|
|
|
|
// Apply state diff into specified accounts.
|
|
|
|
if account.StateDiff != nil {
|
|
|
|
for key, value := range *account.StateDiff {
|
|
|
|
key := key
|
|
|
|
state.SetState(addr, &key, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
msg := args.ToMessage(GasCap)
|
|
|
|
|
2021-03-14 18:52:15 +00:00
|
|
|
blockCtx, txCtx := GetEvmContext(msg, header, blockNrOrHash.RequireCanonical, tx)
|
2020-08-19 11:46:20 +00:00
|
|
|
|
2021-03-14 18:52:15 +00:00
|
|
|
evm := vm.NewEVM(blockCtx, txCtx, state, chainConfig, vm.Config{})
|
2020-08-19 11:46:20 +00: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()
|
|
|
|
}()
|
|
|
|
|
|
|
|
gp := new(core.GasPool).AddGas(msg.Gas())
|
2021-02-12 16:47:32 +00:00
|
|
|
result, err := core.ApplyMessage(evm, msg, gp, true /* refunds */, false /* gasBailout */)
|
2020-08-19 11:46:20 +00: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
|
|
|
|
}
|
|
|
|
|
2021-03-30 09:53:54 +00:00
|
|
|
func GetEvmContext(msg core.Message, header *types.Header, requireCanonical bool, tx ethdb.Tx) (vm.BlockContext, vm.TxContext) {
|
2021-03-14 18:52:15 +00:00
|
|
|
return vm.BlockContext{
|
|
|
|
CanTransfer: core.CanTransfer,
|
|
|
|
Transfer: core.Transfer,
|
2021-03-30 09:53:54 +00:00
|
|
|
GetHash: getHashGetter(requireCanonical, tx),
|
2021-03-14 18:52:15 +00:00
|
|
|
Coinbase: header.Coinbase,
|
2021-04-22 17:11:37 +00:00
|
|
|
BlockNumber: header.Number.Uint64(),
|
|
|
|
Time: header.Time,
|
2021-03-14 18:52:15 +00:00
|
|
|
Difficulty: new(big.Int).Set(header.Difficulty),
|
|
|
|
GasLimit: header.GasLimit,
|
|
|
|
},
|
|
|
|
vm.TxContext{
|
|
|
|
Origin: msg.From(),
|
|
|
|
GasPrice: msg.GasPrice().ToBig(),
|
|
|
|
}
|
2020-08-19 11:46:20 +00:00
|
|
|
}
|
|
|
|
|
2021-03-30 09:53:54 +00:00
|
|
|
func getHashGetter(requireCanonical bool, tx ethdb.Tx) func(uint64) common.Hash {
|
2020-08-19 11:46:20 +00:00
|
|
|
return func(n uint64) common.Hash {
|
2021-04-15 05:23:10 +00:00
|
|
|
hash, err := rawdb.ReadCanonicalHash(tx, n)
|
2020-08-19 11:46:20 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Debug("can't get block hash by number", "number", n, "only-canonical", requireCanonical)
|
|
|
|
}
|
|
|
|
return hash
|
|
|
|
}
|
|
|
|
}
|