180 lines
5.1 KiB
Nim
Raw Normal View History

2021-04-14 11:40:55 +01:00
# Nimbus
# Copyright (c) 2018-2024 Status Research & Development GmbH
2021-04-14 11:40:55 +01:00
# 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: [].}
2021-04-14 11:40:55 +01:00
import
eth/common,
../../computation,
../../stack,
../../evm_errors,
../utils/utils_numeric,
../op_codes,
./oph_defs
when not defined(evmc_enabled):
import ../../state
2021-04-14 11:40:55 +01:00
# ------------------------------------------------------------------------------
# Private, op handlers implementation
# ------------------------------------------------------------------------------
proc blockhashOp(cpt: VmCpt): EvmResultVoid =
## 0x40, Get the hash of one of the 256 most recent complete blocks.
template block256(top, number, conv) =
if number > high(BlockNumber).u256:
top = zero(UInt256)
else:
conv(cpt.getBlockHash(number.truncate(BlockNumber)), top)
cpt.stack.unaryWithTop(block256)
proc coinBaseOp(cpt: VmCpt): EvmResultVoid =
## 0x41, Get the block's beneficiary address.
cpt.stack.push cpt.getCoinbase
proc timestampOp(cpt: VmCpt): EvmResultVoid =
## 0x42, Get the block's timestamp.
cpt.stack.push cpt.getTimestamp
proc blocknumberOp(cpt: VmCpt): EvmResultVoid =
## 0x43, Get the block's number.
cpt.stack.push cpt.getBlockNumber
proc difficultyOp(cpt: VmCpt): EvmResultVoid =
## 0x44, Get the block's difficulty
cpt.stack.push cpt.getDifficulty
proc gasLimitOp(cpt: VmCpt): EvmResultVoid =
## 0x45, Get the block's gas limit
cpt.stack.push cpt.getGasLimit
proc chainIdOp(cpt: VmCpt): EvmResultVoid =
## 0x46, Get current chains EIP-155 unique identifier.
cpt.stack.push cpt.getChainId
proc selfBalanceOp(cpt: VmCpt): EvmResultVoid =
## 0x47, Get current contract's balance.
cpt.stack.push cpt.getBalance(cpt.msg.contractAddress)
proc baseFeeOp(cpt: VmCpt): EvmResultVoid =
## 0x48, Get the block's base fee.
cpt.stack.push cpt.getBaseFee
proc blobHashOp(cpt: VmCpt): EvmResultVoid =
## 0x49, Get current transaction's EIP-4844 versioned hash.
template blob256(top, number, conv) =
let
index = number.safeInt
len = cpt.getVersionedHashesLen
if index < len:
conv(cpt.getVersionedHash(index), top)
else:
top = zero(UInt256)
cpt.stack.unaryWithTop(blob256)
proc blobBaseFeeOp(cpt: VmCpt): EvmResultVoid =
## 0x4a, Get the block's base fee.
cpt.stack.push cpt.getBlobBaseFee
2021-04-14 11:40:55 +01:00
# ------------------------------------------------------------------------------
# Public, op exec table entries
# ------------------------------------------------------------------------------
const
VmOpExecBlockData*: seq[VmOpExec] = @[
2021-04-14 11:40:55 +01:00
(opCode: Blockhash, ## 0x40, Hash of some most recent complete block
forks: VmOpAllForks,
name: "blockhash",
2021-04-14 11:40:55 +01:00
info: "Get the hash of one of the 256 most recent complete blocks",
exec: blockhashOp),
2021-04-14 11:40:55 +01:00
(opCode: Coinbase, ## 0x41, Beneficiary address
forks: VmOpAllForks,
name: "coinbase",
2021-04-14 11:40:55 +01:00
info: "Get the block's beneficiary address",
exec: coinBaseOp),
2021-04-14 11:40:55 +01:00
(opCode: Timestamp, ## 0x42, Block timestamp.
forks: VmOpAllForks,
name: "timestamp",
2021-04-14 11:40:55 +01:00
info: "Get the block's timestamp",
exec: timestampOp),
2021-04-14 11:40:55 +01:00
(opCode: Number, ## 0x43, Block number
forks: VmOpAllForks,
name: "blockNumber",
2021-04-14 11:40:55 +01:00
info: "Get the block's number",
exec: blocknumberOp),
2021-04-14 11:40:55 +01:00
(opCode: Difficulty, ## 0x44, Block difficulty
forks: VmOpAllForks,
name: "difficulty",
2021-04-14 11:40:55 +01:00
info: "Get the block's difficulty",
exec: difficultyOp),
2021-04-14 11:40:55 +01:00
(opCode: GasLimit, ## 0x45, Block gas limit
forks: VmOpAllForks,
name: "gasLimit",
2021-04-14 11:40:55 +01:00
info: "Get the block's gas limit",
exec: gasLimitOp),
2021-04-14 11:40:55 +01:00
(opCode: ChainIdOp, ## 0x46, EIP-155 chain identifier
forks: VmOpIstanbulAndLater,
name: "chainId",
2021-04-14 11:40:55 +01:00
info: "Get current chains EIP-155 unique identifier",
exec: chainIdOp),
2021-04-14 11:40:55 +01:00
(opCode: SelfBalance, ## 0x47, Contract balance.
forks: VmOpIstanbulAndLater,
name: "selfBalance",
2021-04-14 11:40:55 +01:00
info: "Get current contract's balance",
exec: selfBalanceOp),
(opCode: BaseFee, ## 0x48, EIP-1559 Block base fee.
forks: VmOpLondonAndLater,
name: "baseFee",
info: "Get current block's EIP-1559 base fee",
exec: baseFeeOp),
(opCode: BlobHash, ## 0x49, EIP-4844 Transaction versioned hash
forks: VmOpCancunAndLater,
name: "blobHash",
info: "Get current transaction's EIP-4844 versioned hash",
exec: blobHashOp),
(opCode: BlobBaseFee, ## 0x4a, EIP-7516 Returns the current data-blob base-fee
forks: VmOpCancunAndLater,
name: "blobBaseFee",
info: "Returns the current data-blob base-fee",
exec: blobBaseFeeOp)]
2021-04-14 11:40:55 +01:00
# ------------------------------------------------------------------------------
# End
# ------------------------------------------------------------------------------