2021-04-14 10:40:55 +00:00
|
|
|
|
# Nimbus
|
2023-01-31 12:38:08 +00:00
|
|
|
|
# Copyright (c) 2018-2023 Status Research & Development GmbH
|
2021-04-14 10:40:55 +00: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
|
|
|
|
|
## ===============================
|
|
|
|
|
##
|
|
|
|
|
|
|
|
|
|
import
|
2023-01-31 12:38:08 +00:00
|
|
|
|
eth/common,
|
2021-04-26 16:00:46 +00:00
|
|
|
|
../../computation,
|
2021-04-21 17:04:54 +00:00
|
|
|
|
../../stack,
|
2023-04-12 12:39:11 +00:00
|
|
|
|
../../async/operations,
|
2023-06-24 13:56:44 +00:00
|
|
|
|
../utils/utils_numeric,
|
2021-04-21 17:04:54 +00:00
|
|
|
|
../op_codes,
|
2023-01-31 12:38:08 +00:00
|
|
|
|
./oph_defs
|
2022-09-28 06:09:33 +00:00
|
|
|
|
|
2023-02-14 14:37:21 +00:00
|
|
|
|
{.push raises: [CatchableError].} # basically the annotation type of a `Vm2OpFn`
|
|
|
|
|
|
2022-09-28 06:09:33 +00:00
|
|
|
|
when not defined(evmc_enabled):
|
|
|
|
|
import ../../state
|
2021-04-14 10:40:55 +00:00
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
# Private, op handlers implementation
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
const
|
2021-04-20 12:07:01 +00:00
|
|
|
|
blockhashOp: Vm2OpFn = proc (k: var Vm2Ctx) =
|
2021-04-14 10:40:55 +00:00
|
|
|
|
## 0x40, Get the hash of one of the 256 most recent complete blocks.
|
2023-04-12 12:39:11 +00:00
|
|
|
|
let cpt = k.cpt
|
|
|
|
|
let (blockNumber) = cpt.stack.popInt(1)
|
|
|
|
|
cpt.asyncChainTo(ifNecessaryGetBlockHeaderByNumber(cpt.vmState, blockNumber)):
|
|
|
|
|
cpt.stack.push:
|
|
|
|
|
cpt.getBlockHash(blockNumber)
|
2021-04-14 10:40:55 +00:00
|
|
|
|
|
2021-04-20 12:07:01 +00:00
|
|
|
|
coinBaseOp: Vm2OpFn = proc (k: var Vm2Ctx) =
|
2021-04-14 10:40:55 +00:00
|
|
|
|
## 0x41, Get the block's beneficiary address.
|
|
|
|
|
k.cpt.stack.push:
|
|
|
|
|
k.cpt.getCoinbase
|
|
|
|
|
|
2021-04-20 12:07:01 +00:00
|
|
|
|
timestampOp: Vm2OpFn = proc (k: var Vm2Ctx) =
|
2021-04-14 10:40:55 +00:00
|
|
|
|
## 0x42, Get the block's timestamp.
|
|
|
|
|
k.cpt.stack.push:
|
|
|
|
|
k.cpt.getTimestamp
|
|
|
|
|
|
2021-04-20 12:07:01 +00:00
|
|
|
|
blocknumberOp: Vm2OpFn = proc (k: var Vm2Ctx) =
|
2021-04-14 10:40:55 +00:00
|
|
|
|
## 0x43, Get the block's number.
|
|
|
|
|
k.cpt.stack.push:
|
|
|
|
|
k.cpt.getBlockNumber
|
|
|
|
|
|
2021-04-20 12:07:01 +00:00
|
|
|
|
difficultyOp: Vm2OpFn = proc (k: var Vm2Ctx) =
|
2021-04-14 10:40:55 +00:00
|
|
|
|
## 0x44, Get the block's difficulty
|
|
|
|
|
k.cpt.stack.push:
|
|
|
|
|
k.cpt.getDifficulty
|
|
|
|
|
|
2021-04-20 12:07:01 +00:00
|
|
|
|
gasLimitOp: Vm2OpFn = proc (k: var Vm2Ctx) =
|
2021-04-14 10:40:55 +00:00
|
|
|
|
## 0x45, Get the block's gas limit
|
|
|
|
|
k.cpt.stack.push:
|
|
|
|
|
k.cpt.getGasLimit
|
|
|
|
|
|
2021-04-20 12:07:01 +00:00
|
|
|
|
chainIdOp: Vm2OpFn = proc (k: var Vm2Ctx) =
|
2021-04-14 10:40:55 +00:00
|
|
|
|
## 0x46, Get current chain’s EIP-155 unique identifier.
|
|
|
|
|
k.cpt.stack.push:
|
|
|
|
|
k.cpt.getChainId
|
|
|
|
|
|
2021-04-20 12:07:01 +00:00
|
|
|
|
selfBalanceOp: Vm2OpFn = proc (k: var Vm2Ctx) =
|
2021-04-14 10:40:55 +00:00
|
|
|
|
## 0x47, Get current contract's balance.
|
2023-04-12 12:39:11 +00:00
|
|
|
|
let cpt = k.cpt
|
|
|
|
|
cpt.asyncChainTo(ifNecessaryGetAccount(cpt.vmState, cpt.msg.contractAddress)):
|
|
|
|
|
cpt.stack.push:
|
|
|
|
|
cpt.getBalance(cpt.msg.contractAddress)
|
2021-04-14 10:40:55 +00:00
|
|
|
|
|
2021-06-27 13:19:22 +00:00
|
|
|
|
baseFeeOp: Vm2OpFn = proc (k: var Vm2Ctx) =
|
|
|
|
|
## 0x48, Get the block's base fee.
|
|
|
|
|
k.cpt.stack.push:
|
|
|
|
|
k.cpt.getBaseFee
|
|
|
|
|
|
2023-06-24 13:56:44 +00:00
|
|
|
|
blobHashOp: Vm2OpFn = proc (k: var Vm2Ctx) =
|
|
|
|
|
## 0x49, Get current transaction's EIP-4844 versioned hash.
|
|
|
|
|
let index = k.cpt.stack.popInt().safeInt
|
|
|
|
|
let len = k.cpt.getVersionedHashesLen
|
|
|
|
|
|
|
|
|
|
if index < len:
|
|
|
|
|
k.cpt.stack.push:
|
|
|
|
|
k.cpt.getVersionedHash(index)
|
|
|
|
|
else:
|
|
|
|
|
k.cpt.stack.push:
|
|
|
|
|
0
|
|
|
|
|
|
2023-10-01 07:24:15 +00:00
|
|
|
|
blobBaseFeeOp: Vm2OpFn = proc (k: var Vm2Ctx) =
|
|
|
|
|
## 0x4a, Get the block's base fee.
|
|
|
|
|
k.cpt.stack.push:
|
|
|
|
|
k.cpt.getBlobBaseFee
|
|
|
|
|
|
|
|
|
|
|
2021-04-14 10:40:55 +00:00
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
# Public, op exec table entries
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
const
|
|
|
|
|
vm2OpExecBlockData*: seq[Vm2OpExec] = @[
|
|
|
|
|
|
|
|
|
|
(opCode: Blockhash, ## 0x40, Hash of some most recent complete block
|
|
|
|
|
forks: Vm2OpAllForks,
|
2021-04-19 09:15:35 +00:00
|
|
|
|
name: "blockhash",
|
2021-04-14 10:40:55 +00:00
|
|
|
|
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,
|
2021-04-19 09:15:35 +00:00
|
|
|
|
name: "coinbase",
|
2021-04-14 10:40:55 +00:00
|
|
|
|
info: "Get the block's beneficiary address",
|
|
|
|
|
exec: (prep: vm2OpIgnore,
|
|
|
|
|
run: coinBaseOp,
|
|
|
|
|
post: vm2OpIgnore)),
|
|
|
|
|
|
|
|
|
|
(opCode: Timestamp, ## 0x42, Block timestamp.
|
|
|
|
|
forks: Vm2OpAllForks,
|
2021-04-19 09:15:35 +00:00
|
|
|
|
name: "timestamp",
|
2021-04-14 10:40:55 +00:00
|
|
|
|
info: "Get the block's timestamp",
|
|
|
|
|
exec: (prep: vm2OpIgnore,
|
|
|
|
|
run: timestampOp,
|
|
|
|
|
post: vm2OpIgnore)),
|
|
|
|
|
|
|
|
|
|
(opCode: Number, ## 0x43, Block number
|
|
|
|
|
forks: Vm2OpAllForks,
|
2021-04-19 09:15:35 +00:00
|
|
|
|
name: "blockNumber",
|
2021-04-14 10:40:55 +00:00
|
|
|
|
info: "Get the block's number",
|
|
|
|
|
exec: (prep: vm2OpIgnore,
|
|
|
|
|
run: blocknumberOp,
|
|
|
|
|
post: vm2OpIgnore)),
|
|
|
|
|
|
|
|
|
|
(opCode: Difficulty, ## 0x44, Block difficulty
|
2022-02-08 09:17:59 +00:00
|
|
|
|
forks: Vm2OpAllForks,
|
2021-04-19 09:15:35 +00:00
|
|
|
|
name: "difficulty",
|
2021-04-14 10:40:55 +00:00
|
|
|
|
info: "Get the block's difficulty",
|
|
|
|
|
exec: (prep: vm2OpIgnore,
|
|
|
|
|
run: difficultyOp,
|
|
|
|
|
post: vm2OpIgnore)),
|
|
|
|
|
|
|
|
|
|
(opCode: GasLimit, ## 0x45, Block gas limit
|
|
|
|
|
forks: Vm2OpAllForks,
|
2021-04-19 09:15:35 +00:00
|
|
|
|
name: "gasLimit",
|
2021-04-14 10:40:55 +00:00
|
|
|
|
info: "Get the block's gas limit",
|
|
|
|
|
exec: (prep: vm2OpIgnore,
|
|
|
|
|
run: gasLimitOp,
|
|
|
|
|
post: vm2OpIgnore)),
|
|
|
|
|
|
2021-05-15 08:31:58 +00:00
|
|
|
|
(opCode: ChainIdOp, ## 0x46, EIP-155 chain identifier
|
2021-04-19 09:15:35 +00:00
|
|
|
|
forks: Vm2OpIstanbulAndLater,
|
|
|
|
|
name: "chainId",
|
2021-04-14 10:40:55 +00:00
|
|
|
|
info: "Get current chain’s EIP-155 unique identifier",
|
|
|
|
|
exec: (prep: vm2OpIgnore,
|
|
|
|
|
run: chainIdOp,
|
|
|
|
|
post: vm2OpIgnore)),
|
|
|
|
|
|
|
|
|
|
(opCode: SelfBalance, ## 0x47, Contract balance.
|
2021-04-19 09:15:35 +00:00
|
|
|
|
forks: Vm2OpIstanbulAndLater,
|
|
|
|
|
name: "selfBalance",
|
2021-04-14 10:40:55 +00:00
|
|
|
|
info: "Get current contract's balance",
|
|
|
|
|
exec: (prep: vm2OpIgnore,
|
|
|
|
|
run: selfBalanceOp,
|
2021-06-27 13:19:22 +00:00
|
|
|
|
post: vm2OpIgnore)),
|
|
|
|
|
|
2022-02-01 08:55:51 +00:00
|
|
|
|
(opCode: BaseFee, ## 0x48, EIP-1559 Block base fee.
|
2021-06-27 13:19:22 +00:00
|
|
|
|
forks: Vm2OpLondonAndLater,
|
|
|
|
|
name: "baseFee",
|
|
|
|
|
info: "Get current block's EIP-1559 base fee",
|
|
|
|
|
exec: (prep: vm2OpIgnore,
|
|
|
|
|
run: baseFeeOp,
|
2023-06-24 13:56:44 +00:00
|
|
|
|
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,
|
2023-10-01 07:24:15 +00:00
|
|
|
|
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,
|
2021-04-14 10:40:55 +00:00
|
|
|
|
post: vm2OpIgnore))]
|
|
|
|
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
# End
|
|
|
|
|
# ------------------------------------------------------------------------------
|