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,
|
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-06-17 18:13:38 +02:00
|
|
|
|
proc blockhashOp (k: var VmCtx): EvmResultVoid =
|
|
|
|
|
## 0x40, Get the hash of one of the 256 most recent complete blocks.
|
|
|
|
|
let
|
|
|
|
|
cpt = k.cpt
|
|
|
|
|
blockNumber = ? cpt.stack.popInt()
|
|
|
|
|
|
2024-06-19 08:58:08 +07:00
|
|
|
|
if blockNumber > high(BlockNumber).u256:
|
|
|
|
|
cpt.stack.push Hash256()
|
|
|
|
|
else:
|
|
|
|
|
cpt.stack.push cpt.getBlockHash(blockNumber.truncate(BlockNumber))
|
2024-06-17 18:13:38 +02:00
|
|
|
|
|
|
|
|
|
proc coinBaseOp (k: var VmCtx): EvmResultVoid =
|
|
|
|
|
## 0x41, Get the block's beneficiary address.
|
|
|
|
|
k.cpt.stack.push k.cpt.getCoinbase
|
|
|
|
|
|
|
|
|
|
proc timestampOp (k: var VmCtx): EvmResultVoid =
|
|
|
|
|
## 0x42, Get the block's timestamp.
|
|
|
|
|
k.cpt.stack.push k.cpt.getTimestamp
|
|
|
|
|
|
|
|
|
|
proc blocknumberOp (k: var VmCtx): EvmResultVoid =
|
|
|
|
|
## 0x43, Get the block's number.
|
|
|
|
|
k.cpt.stack.push k.cpt.getBlockNumber
|
|
|
|
|
|
|
|
|
|
proc difficultyOp (k: var VmCtx): EvmResultVoid =
|
|
|
|
|
## 0x44, Get the block's difficulty
|
|
|
|
|
k.cpt.stack.push k.cpt.getDifficulty
|
|
|
|
|
|
|
|
|
|
proc gasLimitOp (k: var VmCtx): EvmResultVoid =
|
|
|
|
|
## 0x45, Get the block's gas limit
|
|
|
|
|
k.cpt.stack.push k.cpt.getGasLimit
|
|
|
|
|
|
|
|
|
|
proc chainIdOp (k: var VmCtx): EvmResultVoid =
|
|
|
|
|
## 0x46, Get current chain’s EIP-155 unique identifier.
|
|
|
|
|
k.cpt.stack.push k.cpt.getChainId
|
|
|
|
|
|
|
|
|
|
proc selfBalanceOp (k: var VmCtx): EvmResultVoid =
|
|
|
|
|
## 0x47, Get current contract's balance.
|
|
|
|
|
let cpt = k.cpt
|
|
|
|
|
cpt.stack.push cpt.getBalance(cpt.msg.contractAddress)
|
|
|
|
|
|
|
|
|
|
proc baseFeeOp (k: var VmCtx): EvmResultVoid =
|
|
|
|
|
## 0x48, Get the block's base fee.
|
|
|
|
|
k.cpt.stack.push k.cpt.getBaseFee
|
|
|
|
|
|
|
|
|
|
proc blobHashOp (k: var VmCtx): 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
|
|
|
|
|
|
|
|
|
|
proc blobBaseFeeOp (k: var VmCtx): EvmResultVoid =
|
|
|
|
|
## 0x4a, Get the block's base fee.
|
|
|
|
|
k.cpt.stack.push k.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
|
|
|
|
|
# ------------------------------------------------------------------------------
|