2021-04-14 11:40:55 +01:00
|
|
|
|
# Nimbus
|
2024-02-20 14:16:12 +07:00
|
|
|
|
# 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
|
|
|
|
|
## ===============================
|
|
|
|
|
##
|
|
|
|
|
|
2024-05-22 21:01:19 +00:00
|
|
|
|
{.push raises: [].}
|
|
|
|
|
|
2021-04-14 11:40:55 +01:00
|
|
|
|
import
|
2023-01-31 13:38:08 +01:00
|
|
|
|
eth/common,
|
2021-04-26 17:00:46 +01:00
|
|
|
|
../../computation,
|
2021-04-21 18:04:54 +01:00
|
|
|
|
../../stack,
|
2024-06-07 15:24:32 +07:00
|
|
|
|
../../evm_errors,
|
2024-07-18 18:59:53 +07:00
|
|
|
|
../utils/utils_numeric,
|
2021-04-21 18:04:54 +01:00
|
|
|
|
../op_codes,
|
2023-01-31 13:38:08 +01:00
|
|
|
|
./oph_defs
|
2022-09-28 13:09:33 +07:00
|
|
|
|
|
|
|
|
|
when not defined(evmc_enabled):
|
|
|
|
|
import ../../state
|
2021-04-14 11:40:55 +01:00
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
# Private, op handlers implementation
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
2024-07-19 08:44:01 +07:00
|
|
|
|
proc blockhashOp(cpt: VmCpt): EvmResultVoid =
|
2024-06-17 18:13:38 +02:00
|
|
|
|
## 0x40, Get the hash of one of the 256 most recent complete blocks.
|
2024-07-18 18:59:53 +07:00
|
|
|
|
template block256(top, number, conv) =
|
|
|
|
|
if number > high(BlockNumber).u256:
|
|
|
|
|
top = zero(UInt256)
|
|
|
|
|
else:
|
2024-07-19 08:44:01 +07:00
|
|
|
|
conv(cpt.getBlockHash(number.truncate(BlockNumber)), top)
|
2024-06-17 18:13:38 +02:00
|
|
|
|
|
2024-07-19 08:44:01 +07:00
|
|
|
|
cpt.stack.unaryWithTop(block256)
|
2024-06-17 18:13:38 +02:00
|
|
|
|
|
2024-07-19 08:44:01 +07:00
|
|
|
|
proc coinBaseOp(cpt: VmCpt): EvmResultVoid =
|
2024-06-17 18:13:38 +02:00
|
|
|
|
## 0x41, Get the block's beneficiary address.
|
2024-07-19 08:44:01 +07:00
|
|
|
|
cpt.stack.push cpt.getCoinbase
|
2024-06-17 18:13:38 +02:00
|
|
|
|
|
2024-07-19 08:44:01 +07:00
|
|
|
|
proc timestampOp(cpt: VmCpt): EvmResultVoid =
|
2024-06-17 18:13:38 +02:00
|
|
|
|
## 0x42, Get the block's timestamp.
|
2024-07-19 08:44:01 +07:00
|
|
|
|
cpt.stack.push cpt.getTimestamp
|
2024-06-17 18:13:38 +02:00
|
|
|
|
|
2024-07-19 08:44:01 +07:00
|
|
|
|
proc blocknumberOp(cpt: VmCpt): EvmResultVoid =
|
2024-06-17 18:13:38 +02:00
|
|
|
|
## 0x43, Get the block's number.
|
2024-07-19 08:44:01 +07:00
|
|
|
|
cpt.stack.push cpt.getBlockNumber
|
2024-06-17 18:13:38 +02:00
|
|
|
|
|
2024-07-19 08:44:01 +07:00
|
|
|
|
proc difficultyOp(cpt: VmCpt): EvmResultVoid =
|
2024-06-17 18:13:38 +02:00
|
|
|
|
## 0x44, Get the block's difficulty
|
2024-07-19 08:44:01 +07:00
|
|
|
|
cpt.stack.push cpt.getDifficulty
|
2024-06-17 18:13:38 +02:00
|
|
|
|
|
2024-07-19 08:44:01 +07:00
|
|
|
|
proc gasLimitOp(cpt: VmCpt): EvmResultVoid =
|
2024-06-17 18:13:38 +02:00
|
|
|
|
## 0x45, Get the block's gas limit
|
2024-07-19 08:44:01 +07:00
|
|
|
|
cpt.stack.push cpt.getGasLimit
|
2024-06-17 18:13:38 +02:00
|
|
|
|
|
2024-07-19 08:44:01 +07:00
|
|
|
|
proc chainIdOp(cpt: VmCpt): EvmResultVoid =
|
2024-06-17 18:13:38 +02:00
|
|
|
|
## 0x46, Get current chain’s EIP-155 unique identifier.
|
2024-07-19 08:44:01 +07:00
|
|
|
|
cpt.stack.push cpt.getChainId
|
2024-06-17 18:13:38 +02:00
|
|
|
|
|
2024-07-19 08:44:01 +07:00
|
|
|
|
proc selfBalanceOp(cpt: VmCpt): EvmResultVoid =
|
2024-06-17 18:13:38 +02:00
|
|
|
|
## 0x47, Get current contract's balance.
|
|
|
|
|
cpt.stack.push cpt.getBalance(cpt.msg.contractAddress)
|
|
|
|
|
|
2024-07-19 08:44:01 +07:00
|
|
|
|
proc baseFeeOp(cpt: VmCpt): EvmResultVoid =
|
2024-06-17 18:13:38 +02:00
|
|
|
|
## 0x48, Get the block's base fee.
|
2024-07-19 08:44:01 +07:00
|
|
|
|
cpt.stack.push cpt.getBaseFee
|
2024-06-17 18:13:38 +02:00
|
|
|
|
|
2024-07-19 08:44:01 +07:00
|
|
|
|
proc blobHashOp(cpt: VmCpt): EvmResultVoid =
|
2024-06-17 18:13:38 +02:00
|
|
|
|
## 0x49, Get current transaction's EIP-4844 versioned hash.
|
2024-07-18 18:59:53 +07:00
|
|
|
|
template blob256(top, number, conv) =
|
|
|
|
|
let
|
|
|
|
|
index = number.safeInt
|
2024-07-19 08:44:01 +07:00
|
|
|
|
len = cpt.getVersionedHashesLen
|
2024-07-18 18:59:53 +07:00
|
|
|
|
|
|
|
|
|
if index < len:
|
2024-07-19 08:44:01 +07:00
|
|
|
|
conv(cpt.getVersionedHash(index), top)
|
2024-07-18 18:59:53 +07:00
|
|
|
|
else:
|
|
|
|
|
top = zero(UInt256)
|
|
|
|
|
|
2024-07-19 08:44:01 +07:00
|
|
|
|
cpt.stack.unaryWithTop(blob256)
|
2024-06-17 18:13:38 +02:00
|
|
|
|
|
2024-07-19 08:44:01 +07:00
|
|
|
|
proc blobBaseFeeOp(cpt: VmCpt): EvmResultVoid =
|
2024-06-17 18:13:38 +02:00
|
|
|
|
## 0x4a, Get the block's base fee.
|
2024-07-19 08:44:01 +07:00
|
|
|
|
cpt.stack.push cpt.getBlobBaseFee
|
2023-10-01 14:24:15 +07:00
|
|
|
|
|
|
|
|
|
|
2021-04-14 11:40:55 +01:00
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
# Public, op exec table entries
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
const
|
2024-06-15 23:18:53 +07:00
|
|
|
|
VmOpExecBlockData*: seq[VmOpExec] = @[
|
2021-04-14 11:40:55 +01:00
|
|
|
|
|
|
|
|
|
(opCode: Blockhash, ## 0x40, Hash of some most recent complete block
|
2024-06-15 23:18:53 +07:00
|
|
|
|
forks: VmOpAllForks,
|
2021-04-19 10:15:35 +01:00
|
|
|
|
name: "blockhash",
|
2021-04-14 11:40:55 +01:00
|
|
|
|
info: "Get the hash of one of the 256 most recent complete blocks",
|
2024-06-24 12:58:15 +07:00
|
|
|
|
exec: blockhashOp),
|
|
|
|
|
|
2021-04-14 11:40:55 +01:00
|
|
|
|
|
|
|
|
|
(opCode: Coinbase, ## 0x41, Beneficiary address
|
2024-06-15 23:18:53 +07:00
|
|
|
|
forks: VmOpAllForks,
|
2021-04-19 10:15:35 +01:00
|
|
|
|
name: "coinbase",
|
2021-04-14 11:40:55 +01:00
|
|
|
|
info: "Get the block's beneficiary address",
|
2024-06-24 12:58:15 +07:00
|
|
|
|
exec: coinBaseOp),
|
|
|
|
|
|
2021-04-14 11:40:55 +01:00
|
|
|
|
|
|
|
|
|
(opCode: Timestamp, ## 0x42, Block timestamp.
|
2024-06-15 23:18:53 +07:00
|
|
|
|
forks: VmOpAllForks,
|
2021-04-19 10:15:35 +01:00
|
|
|
|
name: "timestamp",
|
2021-04-14 11:40:55 +01:00
|
|
|
|
info: "Get the block's timestamp",
|
2024-06-24 12:58:15 +07:00
|
|
|
|
exec: timestampOp),
|
|
|
|
|
|
2021-04-14 11:40:55 +01:00
|
|
|
|
|
|
|
|
|
(opCode: Number, ## 0x43, Block number
|
2024-06-15 23:18:53 +07:00
|
|
|
|
forks: VmOpAllForks,
|
2021-04-19 10:15:35 +01:00
|
|
|
|
name: "blockNumber",
|
2021-04-14 11:40:55 +01:00
|
|
|
|
info: "Get the block's number",
|
2024-06-24 12:58:15 +07:00
|
|
|
|
exec: blocknumberOp),
|
|
|
|
|
|
2021-04-14 11:40:55 +01:00
|
|
|
|
|
|
|
|
|
(opCode: Difficulty, ## 0x44, Block difficulty
|
2024-06-15 23:18:53 +07:00
|
|
|
|
forks: VmOpAllForks,
|
2021-04-19 10:15:35 +01:00
|
|
|
|
name: "difficulty",
|
2021-04-14 11:40:55 +01:00
|
|
|
|
info: "Get the block's difficulty",
|
2024-06-24 12:58:15 +07:00
|
|
|
|
exec: difficultyOp),
|
|
|
|
|
|
2021-04-14 11:40:55 +01:00
|
|
|
|
|
|
|
|
|
(opCode: GasLimit, ## 0x45, Block gas limit
|
2024-06-15 23:18:53 +07:00
|
|
|
|
forks: VmOpAllForks,
|
2021-04-19 10:15:35 +01:00
|
|
|
|
name: "gasLimit",
|
2021-04-14 11:40:55 +01:00
|
|
|
|
info: "Get the block's gas limit",
|
2024-06-24 12:58:15 +07:00
|
|
|
|
exec: gasLimitOp),
|
|
|
|
|
|
2021-04-14 11:40:55 +01:00
|
|
|
|
|
2021-05-15 15:31:58 +07:00
|
|
|
|
(opCode: ChainIdOp, ## 0x46, EIP-155 chain identifier
|
2024-06-15 23:18:53 +07:00
|
|
|
|
forks: VmOpIstanbulAndLater,
|
2021-04-19 10:15:35 +01:00
|
|
|
|
name: "chainId",
|
2021-04-14 11:40:55 +01:00
|
|
|
|
info: "Get current chain’s EIP-155 unique identifier",
|
2024-06-24 12:58:15 +07:00
|
|
|
|
exec: chainIdOp),
|
|
|
|
|
|
2021-04-14 11:40:55 +01:00
|
|
|
|
|
|
|
|
|
(opCode: SelfBalance, ## 0x47, Contract balance.
|
2024-06-15 23:18:53 +07:00
|
|
|
|
forks: VmOpIstanbulAndLater,
|
2021-04-19 10:15:35 +01:00
|
|
|
|
name: "selfBalance",
|
2021-04-14 11:40:55 +01:00
|
|
|
|
info: "Get current contract's balance",
|
2024-06-24 12:58:15 +07:00
|
|
|
|
exec: selfBalanceOp),
|
|
|
|
|
|
2021-06-27 20:19:22 +07:00
|
|
|
|
|
2022-02-01 15:55:51 +07:00
|
|
|
|
(opCode: BaseFee, ## 0x48, EIP-1559 Block base fee.
|
2024-06-15 23:18:53 +07:00
|
|
|
|
forks: VmOpLondonAndLater,
|
2021-06-27 20:19:22 +07:00
|
|
|
|
name: "baseFee",
|
|
|
|
|
info: "Get current block's EIP-1559 base fee",
|
2024-06-24 12:58:15 +07:00
|
|
|
|
exec: baseFeeOp),
|
|
|
|
|
|
2023-06-24 20:56:44 +07:00
|
|
|
|
|
|
|
|
|
(opCode: BlobHash, ## 0x49, EIP-4844 Transaction versioned hash
|
2024-06-15 23:18:53 +07:00
|
|
|
|
forks: VmOpCancunAndLater,
|
2023-06-24 20:56:44 +07:00
|
|
|
|
name: "blobHash",
|
|
|
|
|
info: "Get current transaction's EIP-4844 versioned hash",
|
2024-06-24 12:58:15 +07:00
|
|
|
|
exec: blobHashOp),
|
|
|
|
|
|
2023-10-01 14:24:15 +07:00
|
|
|
|
|
|
|
|
|
(opCode: BlobBaseFee, ## 0x4a, EIP-7516 Returns the current data-blob base-fee
|
2024-06-15 23:18:53 +07:00
|
|
|
|
forks: VmOpCancunAndLater,
|
2023-10-01 14:24:15 +07:00
|
|
|
|
name: "blobBaseFee",
|
|
|
|
|
info: "Returns the current data-blob base-fee",
|
2024-06-24 12:58:15 +07:00
|
|
|
|
exec: blobBaseFeeOp)]
|
2021-04-14 11:40:55 +01:00
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
# End
|
|
|
|
|
# ------------------------------------------------------------------------------
|