package commands import ( "context" "errors" "fmt" "math/big" "time" "github.com/holiman/uint256" "github.com/ledgerwatch/turbo-geth/common" "github.com/ledgerwatch/turbo-geth/common/hexutil" "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/internal/ethapi" "github.com/ledgerwatch/turbo-geth/log" "github.com/ledgerwatch/turbo-geth/params" "github.com/ledgerwatch/turbo-geth/rpc" ) const callTimeout = 5 * time.Second func (api *APIImpl) Call(ctx context.Context, args ethapi.CallArgs, blockNrOrHash rpc.BlockNumberOrHash, overrides map[common.Address]ethapi.Account) (hexutil.Bytes, error) { result, err := api.doCall(ctx, args, blockNrOrHash, overrides) if err != nil { return nil, err } // If the result contains a revert reason, try to unpack and return it. if len(result.Revert()) > 0 { return nil, ethapi.NewRevertError(result) } return result.Return(), result.Err } func (api *APIImpl) doCall(ctx context.Context, args ethapi.CallArgs, blockNrOrHash rpc.BlockNumberOrHash, overrides map[common.Address]ethapi.Account) (*core.ExecutionResult, error) { // 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 } */ blockNumber, hash, err := GetBlockNumber(blockNrOrHash, api.dbReader) if err != nil { return nil, err } ds := state.NewPlainDBState(api.db, blockNumber) state := state.New(ds) if state == nil { return nil, fmt.Errorf("can't get the state for %d", blockNumber) } header := rawdb.ReadHeader(api.dbReader, hash, blockNumber) if header == nil { return nil, fmt.Errorf("block %d(%x) not found", blockNumber, hash) } // Override the fields of specified contracts before execution. 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(big.NewInt(0).SetUint64(cfg.gascap)) evmCtx := GetEvmContext(msg, header, blockNrOrHash.RequireCanonical, api.dbReader) evm := vm.NewEVM(evmCtx, state, params.MainnetChainConfig, vm.Config{}, nil) // 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()) result, err := core.ApplyMessage(evm, msg, gp) 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 } func GetEvmContext(msg core.Message, header *types.Header, requireCanonical bool, dbReader rawdb.DatabaseReader) vm.Context { return vm.Context{ CanTransfer: core.CanTransfer, Transfer: core.Transfer, GetHash: getHashGetter(requireCanonical, dbReader), Origin: msg.From(), Coinbase: header.Coinbase, BlockNumber: new(big.Int).Set(header.Number), Time: new(big.Int).SetUint64(header.Time), Difficulty: new(big.Int).Set(header.Difficulty), GasLimit: header.GasLimit, GasPrice: msg.GasPrice().ToBig(), } } func getHashGetter(requireCanonical bool, dbReader rawdb.DatabaseReader) func(uint64) common.Hash { return func(n uint64) common.Hash { hash, err := GetHashByNumber(n, requireCanonical, dbReader) if err != nil { log.Debug("can't get block hash by number", "number", n, "only-canonical", requireCanonical) } return hash } } // EstimateGas returns an estimate of the amount of gas needed to execute the // given transaction against the current pending block. func (api *APIImpl) EstimateGas(ctx context.Context, args ethapi.CallArgs) (hexutil.Uint64, error) { //fixme: blockNrOrHash := rpc.BlockNumberOrHashWithNumber(rpc.PendingBlockNumber) hash := rawdb.ReadHeadBlockHash(api.dbReader) return api.DoEstimateGas(ctx, args, rpc.BlockNumberOrHash{BlockHash: &hash}, big.NewInt(0).SetUint64(cfg.gascap)) } func (api *APIImpl) DoEstimateGas(ctx context.Context, args ethapi.CallArgs, blockNrOrHash rpc.BlockNumberOrHash, gasCap *big.Int) (hexutil.Uint64, error) { // Binary search the gas requirement, as it may be higher than the amount used var ( lo uint64 = params.TxGas - 1 hi uint64 cap uint64 ) // Use zero address if sender unspecified. if args.From == nil { args.From = new(common.Address) } blockNumber, hash, err := GetBlockNumber(blockNrOrHash, api.dbReader) if err != nil { return 0, err } // Determine the highest gas limit can be used during the estimation. if args.Gas != nil && uint64(*args.Gas) >= params.TxGas { hi = uint64(*args.Gas) } else { // Retrieve the block to act as the gas ceiling header := rawdb.ReadHeader(api.dbReader, hash, blockNumber) hi = header.GasLimit } // Recap the highest gas limit with account's available balance. if args.GasPrice != nil && args.GasPrice.ToInt().Uint64() != 0 { ds := state.NewPlainDBState(api.db, blockNumber) state := state.New(ds) if state == nil { return 0, fmt.Errorf("can't get the state for %d", blockNumber) } balance := state.GetBalance(*args.From) // from can't be nil available := balance.ToBig() if args.Value != nil { if args.Value.ToInt().Cmp(available) >= 0 { return 0, errors.New("insufficient funds for transfer") } available.Sub(available, args.Value.ToInt()) } allowance := new(big.Int).Div(available, args.GasPrice.ToInt()) if hi > allowance.Uint64() { transfer := args.Value if transfer == nil { transfer = new(hexutil.Big) } log.Warn("Gas estimation capped by limited funds", "original", hi, "balance", balance, "sent", transfer.ToInt(), "gasprice", args.GasPrice.ToInt(), "fundable", allowance) hi = allowance.Uint64() } } // Recap the highest gas allowance with specified gascap. if gasCap != nil && hi > gasCap.Uint64() { log.Warn("Caller gas above allowance, capping", "requested", hi, "cap", gasCap) hi = gasCap.Uint64() } cap = hi // Create a helper to check if a gas allowance results in an executable transaction executable := func(gas uint64) (bool, *core.ExecutionResult, error) { args.Gas = (*hexutil.Uint64)(&gas) result, err := api.doCall(ctx, args, blockNrOrHash, nil) if err != nil { if errors.Is(err, core.ErrIntrinsicGas) { // Special case, raise gas limit return true, nil, nil } // Bail out return true, nil, err } return result.Failed(), result, nil } // Execute the binary search and hone in on an executable gas limit for lo+1 < hi { mid := (hi + lo) / 2 failed, _, err := executable(mid) // If the error is not nil(consensus error), it means the provided message // call or transaction will never be accepted no matter how much gas it is // assigened. Return the error directly, don't struggle any more. if err != nil { return 0, err } if failed { lo = mid } else { hi = mid } } // Reject the transaction as invalid if it still fails at the highest allowance if hi == cap { failed, result, err := executable(hi) if err != nil { return 0, err } if failed { if result != nil && !errors.Is(result.Err, vm.ErrOutOfGas) { if len(result.Revert()) > 0 { return 0, ethapi.NewRevertError(result) } return 0, result.Err } // Otherwise, the specified gas cap is too low return 0, fmt.Errorf("gas required exceeds allowance (%d)", cap) } } return hexutil.Uint64(hi), nil }