nimbus-eth1/nimbus/rpc/params.nim

84 lines
2.5 KiB
Nim
Raw Normal View History

2024-04-16 12:52:07 +00:00
# Nimbus
# Copyright (c) 2024 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
# at your option.
# This file may not be copied, modified, or distributed except according to
# those terms.
{.push raises: [].}
import
chronicles,
eth/common/[addresses, headers],
web3/eth_api_types,
2024-04-16 12:52:07 +00:00
../transaction/call_common,
../evm/types,
../evm/evm_errors,
../constants
2024-04-16 12:52:07 +00:00
const
ZeroAddr = ZERO_ADDRESS
2024-04-16 12:52:07 +00:00
func sender*(args: TransactionArgs): Address =
args.source.get(ZeroAddr)
2024-04-16 12:52:07 +00:00
func destination*(args: TransactionArgs): Address =
args.to.get(ZeroAddr)
2024-04-16 12:52:07 +00:00
proc toCallParams*(vmState: BaseVMState, args: TransactionArgs,
globalGasCap: GasInt, baseFee: Opt[UInt256]): EvmResult[CallParams] =
2024-04-16 12:52:07 +00:00
# Reject invalid combinations of pre- and post-1559 fee styles
if args.gasPrice.isSome and
(args.maxFeePerGas.isSome or args.maxPriorityFeePerGas.isSome):
return err(evmErr(EvmInvalidParam))
2024-04-16 12:52:07 +00:00
# Set default gas & gas price if none were set
var gasLimit = globalGasCap
if gasLimit == 0:
gasLimit = high(uint64) div 2
2024-04-16 12:52:07 +00:00
if args.gas.isSome:
gasLimit = GasInt args.gas.get()
if globalGasCap != 0 and globalGasCap < gasLimit:
warn "Caller gas above allowance, capping",
requested = gasLimit,
cap = globalGasCap,
gasLimit = globalGasCap
var gasPrice = GasInt args.gasPrice.get(0.Quantity)
if baseFee.isSome:
# A basefee is provided, necessitating EIP-1559-type execution
let maxPriorityFee = GasInt args.maxPriorityFeePerGas.get(0.Quantity)
let maxFee = GasInt args.maxFeePerGas.get(0.Quantity)
# Backfill the legacy gasPrice for EVM execution, unless we're all zeroes
if maxPriorityFee > 0 or maxFee > 0:
let baseFee = baseFee.get().truncate(GasInt)
let priorityFee = min(maxPriorityFee, maxFee - baseFee)
gasPrice = priorityFee + baseFee
template versionedHashes(args: TransactionArgs): seq[VersionedHash] =
2024-04-16 12:52:07 +00:00
if args.blobVersionedHashes.isSome:
args.blobVersionedHashes.get
2024-04-16 12:52:07 +00:00
else:
@[]
ok(CallParams(
2024-04-16 12:52:07 +00:00
vmState: vmState,
sender: args.sender,
to: args.destination,
isCreate: args.to.isNone,
gasLimit: gasLimit,
gasPrice: gasPrice,
value: args.value.get(0.u256),
input: args.payload(),
accessList: args.accessList.get(@[]),
2024-04-16 12:52:07 +00:00
versionedHashes: args.versionedHashes,
))
2024-04-16 12:52:07 +00:00
{.pop.}