nimbus-eth1/nimbus/rpc/params.nim

90 lines
2.6 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,
../common/common,
../transaction/call_common,
../vm_types,
../beacon/web3_eth_conv,
../evm/evm_errors,
2024-04-16 12:52:07 +00:00
./rpc_types
export
web3_eth_conv,
rpc_types
const
ZeroAddr = w3Addr ZERO_ADDRESS
func sender*(args: TransactionArgs): EthAddress =
ethAddr args.source.get(ZeroAddr)
func destination*(args: TransactionArgs): EthAddress =
ethAddr args.to.get(ZeroAddr)
proc toCallParams*(vmState: BaseVMState, args: TransactionArgs,
globalGasCap: GasInt, baseFee: Opt[UInt256],
forkOverride = Opt.none(EVMFork)): 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 = GasInt(high(uint64) div 2)
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): VersionedHashes =
if args.blobVersionedHashes.isSome:
ethHashes args.blobVersionedHashes.get
else:
@[]
ok(CallParams(
2024-04-16 12:52:07 +00:00
vmState: vmState,
forkOverride: forkOverride,
sender: args.sender,
to: args.destination,
isCreate: args.to.isNone,
gasLimit: gasLimit,
gasPrice: gasPrice,
value: args.value.get(0.u256),
input: args.payload(),
accessList: ethAccessList args.accessList,
versionedHashes: args.versionedHashes,
))
2024-04-16 12:52:07 +00:00
{.pop.}