andri lim b3a5c67532
Remove exceptions from EVM (#2314)
* Remove exception from evm memory

* Remove exception from gas meter

* Remove exception from stack

* Remove exception from precompiles

* Remove exception from gas_costs

* Remove exception from op handlers

* Remove exception from op dispatcher

* Remove exception from call_evm

* Remove exception from EVM

* Fix tools and tests

* Remove exception from EVMC

* fix evmc

* Fix evmc

* Remove remnants of async evm stuff

* Remove superflous error handling

* Proc to func

* Fix errors detected by CI

* Fix EVM op call stack usage

* REmove exception handling from getVmState

* Better error message instead of just doAssert

* Remove unused validation

* Remove superflous catchRaise

* Use results.expect instead of unsafeValue
2024-06-07 15:24:32 +07:00

189 lines
6.0 KiB
Nim
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Nimbus
# Copyright (c) 2018-2024 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or
# http://www.apache.org/licenses/LICENSE-2.0)
# * MIT license ([LICENSE-MIT](LICENSE-MIT) or
# http://opensource.org/licenses/MIT)
# at your option. This file may not be copied, modified, or distributed except
# according to those terms.
## EVM Opcode Handlers: Block Data
## ===============================
##
{.push raises: [].}
import
eth/common,
../../computation,
../../stack,
../../evm_errors,
../op_codes,
./oph_defs
when not defined(evmc_enabled):
import ../../state
# ------------------------------------------------------------------------------
# Private, op handlers implementation
# ------------------------------------------------------------------------------
const
blockhashOp: Vm2OpFn = proc (k: var Vm2Ctx): EvmResultVoid =
## 0x40, Get the hash of one of the 256 most recent complete blocks.
let
cpt = k.cpt
blockNumber = ? cpt.stack.popInt()
blockHash = cpt.getBlockHash(blockNumber)
cpt.stack.push blockHash
coinBaseOp: Vm2OpFn = proc (k: var Vm2Ctx): EvmResultVoid =
## 0x41, Get the block's beneficiary address.
k.cpt.stack.push k.cpt.getCoinbase
timestampOp: Vm2OpFn = proc (k: var Vm2Ctx): EvmResultVoid =
## 0x42, Get the block's timestamp.
k.cpt.stack.push k.cpt.getTimestamp
blocknumberOp: Vm2OpFn = proc (k: var Vm2Ctx): EvmResultVoid =
## 0x43, Get the block's number.
k.cpt.stack.push k.cpt.getBlockNumber
difficultyOp: Vm2OpFn = proc (k: var Vm2Ctx): EvmResultVoid =
## 0x44, Get the block's difficulty
k.cpt.stack.push k.cpt.getDifficulty
gasLimitOp: Vm2OpFn = proc (k: var Vm2Ctx): EvmResultVoid =
## 0x45, Get the block's gas limit
k.cpt.stack.push k.cpt.getGasLimit
chainIdOp: Vm2OpFn = proc (k: var Vm2Ctx): EvmResultVoid =
## 0x46, Get current chains EIP-155 unique identifier.
k.cpt.stack.push k.cpt.getChainId
selfBalanceOp: Vm2OpFn = proc (k: var Vm2Ctx): EvmResultVoid =
## 0x47, Get current contract's balance.
let cpt = k.cpt
cpt.stack.push cpt.getBalance(cpt.msg.contractAddress)
baseFeeOp: Vm2OpFn = proc (k: var Vm2Ctx): EvmResultVoid =
## 0x48, Get the block's base fee.
k.cpt.stack.push k.cpt.getBaseFee
blobHashOp: Vm2OpFn = proc (k: var Vm2Ctx): EvmResultVoid =
## 0x49, Get current transaction's EIP-4844 versioned hash.
let
index = ? k.cpt.stack.popSafeInt()
len = k.cpt.getVersionedHashesLen
if index < len:
k.cpt.stack.push k.cpt.getVersionedHash(index)
else:
k.cpt.stack.push 0
blobBaseFeeOp: Vm2OpFn = proc (k: var Vm2Ctx): EvmResultVoid =
## 0x4a, Get the block's base fee.
k.cpt.stack.push k.cpt.getBlobBaseFee
# ------------------------------------------------------------------------------
# Public, op exec table entries
# ------------------------------------------------------------------------------
const
vm2OpExecBlockData*: seq[Vm2OpExec] = @[
(opCode: Blockhash, ## 0x40, Hash of some most recent complete block
forks: Vm2OpAllForks,
name: "blockhash",
info: "Get the hash of one of the 256 most recent complete blocks",
exec: (prep: vm2OpIgnore,
run: blockhashOp,
post: vm2OpIgnore)),
(opCode: Coinbase, ## 0x41, Beneficiary address
forks: Vm2OpAllForks,
name: "coinbase",
info: "Get the block's beneficiary address",
exec: (prep: vm2OpIgnore,
run: coinBaseOp,
post: vm2OpIgnore)),
(opCode: Timestamp, ## 0x42, Block timestamp.
forks: Vm2OpAllForks,
name: "timestamp",
info: "Get the block's timestamp",
exec: (prep: vm2OpIgnore,
run: timestampOp,
post: vm2OpIgnore)),
(opCode: Number, ## 0x43, Block number
forks: Vm2OpAllForks,
name: "blockNumber",
info: "Get the block's number",
exec: (prep: vm2OpIgnore,
run: blocknumberOp,
post: vm2OpIgnore)),
(opCode: Difficulty, ## 0x44, Block difficulty
forks: Vm2OpAllForks,
name: "difficulty",
info: "Get the block's difficulty",
exec: (prep: vm2OpIgnore,
run: difficultyOp,
post: vm2OpIgnore)),
(opCode: GasLimit, ## 0x45, Block gas limit
forks: Vm2OpAllForks,
name: "gasLimit",
info: "Get the block's gas limit",
exec: (prep: vm2OpIgnore,
run: gasLimitOp,
post: vm2OpIgnore)),
(opCode: ChainIdOp, ## 0x46, EIP-155 chain identifier
forks: Vm2OpIstanbulAndLater,
name: "chainId",
info: "Get current chains EIP-155 unique identifier",
exec: (prep: vm2OpIgnore,
run: chainIdOp,
post: vm2OpIgnore)),
(opCode: SelfBalance, ## 0x47, Contract balance.
forks: Vm2OpIstanbulAndLater,
name: "selfBalance",
info: "Get current contract's balance",
exec: (prep: vm2OpIgnore,
run: selfBalanceOp,
post: vm2OpIgnore)),
(opCode: BaseFee, ## 0x48, EIP-1559 Block base fee.
forks: Vm2OpLondonAndLater,
name: "baseFee",
info: "Get current block's EIP-1559 base fee",
exec: (prep: vm2OpIgnore,
run: baseFeeOp,
post: vm2OpIgnore)),
(opCode: BlobHash, ## 0x49, EIP-4844 Transaction versioned hash
forks: Vm2OpCancunAndLater,
name: "blobHash",
info: "Get current transaction's EIP-4844 versioned hash",
exec: (prep: vm2OpIgnore,
run: blobHashOp,
post: vm2OpIgnore)),
(opCode: BlobBaseFee, ## 0x4a, EIP-7516 Returns the current data-blob base-fee
forks: Vm2OpCancunAndLater,
name: "blobBaseFee",
info: "Returns the current data-blob base-fee",
exec: (prep: vm2OpIgnore,
run: blobBaseFeeOp,
post: vm2OpIgnore))]
# ------------------------------------------------------------------------------
# End
# ------------------------------------------------------------------------------