referrerpolicy=no-referrer-when-downgrade

pallet_revive/vm/evm/instructions/
block_info.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18use super::Context;
19use crate::{
20	vm::{
21		evm::{U256Converter, BASE_FEE, DIFFICULTY},
22		Ext,
23	},
24	RuntimeCosts,
25};
26use revm::{
27	interpreter::gas as revm_gas,
28	primitives::{Address, U256},
29};
30use sp_core::H160;
31
32/// EIP-1344: ChainID opcode
33pub fn chainid<'ext, E: Ext>(context: Context<'_, 'ext, E>) {
34	gas_legacy!(context.interpreter, revm_gas::BASE);
35	push!(context.interpreter, U256::from(context.interpreter.extend.chain_id()));
36}
37
38/// Implements the COINBASE instruction.
39///
40/// Pushes the current block's beneficiary address onto the stack.
41pub fn coinbase<'ext, E: Ext>(context: Context<'_, 'ext, E>) {
42	gas!(context.interpreter, RuntimeCosts::BlockAuthor);
43	let coinbase: Address =
44		context.interpreter.extend.block_author().unwrap_or(H160::zero()).0.into();
45	push!(context.interpreter, coinbase.into_word().into());
46}
47
48/// Implements the TIMESTAMP instruction.
49///
50/// Pushes the current block's timestamp onto the stack.
51pub fn timestamp<'ext, E: Ext>(context: Context<'_, 'ext, E>) {
52	gas!(context.interpreter, RuntimeCosts::Now);
53	let timestamp = context.interpreter.extend.now();
54	push!(context.interpreter, timestamp.into_revm_u256());
55}
56
57/// Implements the NUMBER instruction.
58///
59/// Pushes the current block number onto the stack.
60pub fn block_number<'ext, E: Ext>(context: Context<'_, 'ext, E>) {
61	gas!(context.interpreter, RuntimeCosts::BlockNumber);
62	let block_number = context.interpreter.extend.block_number();
63	push!(context.interpreter, block_number.into_revm_u256());
64}
65
66/// Implements the DIFFICULTY/PREVRANDAO instruction.
67///
68/// Pushes the block difficulty (pre-merge) or prevrandao (post-merge) onto the stack.
69pub fn difficulty<'ext, E: Ext>(context: Context<'_, 'ext, E>) {
70	gas_legacy!(context.interpreter, revm_gas::BASE);
71	push!(context.interpreter, U256::from(DIFFICULTY));
72}
73
74/// Implements the GASLIMIT instruction.
75///
76/// Pushes the current block's gas limit onto the stack.
77pub fn gaslimit<'ext, E: Ext>(context: Context<'_, 'ext, E>) {
78	gas!(context.interpreter, RuntimeCosts::GasLimit);
79	let gas_limit = context.interpreter.extend.gas_limit();
80	push!(context.interpreter, U256::from(gas_limit));
81}
82
83/// EIP-3198: BASEFEE opcode
84pub fn basefee<'ext, E: Ext>(context: Context<'_, 'ext, E>) {
85	gas!(context.interpreter, RuntimeCosts::BaseFee);
86	push!(context.interpreter, BASE_FEE.into_revm_u256());
87}
88
89/// EIP-7516: BLOBBASEFEE opcode is not supported
90pub fn blob_basefee<'ext, E: Ext>(context: Context<'_, 'ext, E>) {
91	context.interpreter.halt(revm::interpreter::InstructionResult::NotActivated);
92}