Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new precompile PrecompileRegistry #2138

Merged
merged 8 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ members = [
"precompiles/crowdloan-rewards",
"precompiles/pallet-democracy",
"precompiles/parachain-staking",
"precompiles/precompile-registry",
"precompiles/preimage",
"precompiles/proxy",
"precompiles/randomness",
Expand Down Expand Up @@ -81,6 +82,7 @@ pallet-evm-precompile-preimage = { path = "precompiles/preimage", default-featur
pallet-evm-precompile-proxy = { path = "precompiles/proxy", default-features = false }
pallet-evm-precompile-randomness = { path = "precompiles/randomness", default-features = false }
pallet-evm-precompile-referenda = { path = "precompiles/referenda", default-features = false }
pallet-evm-precompile-registry = { path = "precompiles/precompile-registry", default-features = false }
pallet-evm-precompile-relay-encoder = { path = "precompiles/relay-encoder", default-features = false }
pallet-evm-precompile-xcm-transactor = { path = "precompiles/xcm-transactor", default-features = false }
pallet-evm-precompile-xcm-utils = { path = "precompiles/xcm-utils", default-features = false }
Expand Down
53 changes: 53 additions & 0 deletions precompiles/precompile-registry/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
[package]
name = "pallet-evm-precompile-registry"
authors = { workspace = true }
description = "Registry of active precompiles"
edition = "2021"
version = "0.1.0"

[dependencies]
log = { workspace = true }

# Moonbeam
precompile-utils = { workspace = true }

# Substrate
frame-support = { workspace = true }
frame-system = { workspace = true }
parity-scale-codec = { workspace = true }
sp-core = { workspace = true }
sp-io = { workspace = true }
sp-std = { workspace = true }

# Frontier
fp-evm = { workspace = true }
pallet-evm = { workspace = true, features = [ "forbid-evm-reentrancy" ] }

[dev-dependencies]
derive_more = { workspace = true }
hex-literal = { workspace = true }
serde = { workspace = true }

# Moonbeam
precompile-utils = { workspace = true, features = [ "std", "testing" ] }

# Substrate
pallet-balances = { workspace = true, features = [ "std" ] }
pallet-scheduler = { workspace = true }
pallet-timestamp = { workspace = true }
scale-info = { workspace = true, features = [ "derive" ] }
sp-runtime = { workspace = true }

[features]
default = [ "std" ]
std = [
"fp-evm/std",
"frame-support/std",
"frame-system/std",
"pallet-evm/std",
"parity-scale-codec/std",
"precompile-utils/std",
"sp-core/std",
"sp-io/std",
"sp-std/std",
]
32 changes: 32 additions & 0 deletions precompiles/precompile-registry/PrecompileRegistry.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SPDX-License-Identifier: GPL-3.0-only
pragma solidity >=0.8.3;

/// @author The Moonbeam Team
/// @title Precompile Registry
/// @dev Interface to the set of available precompiles.
interface PrecompileRegistry {
/// @dev Query if the given address is a precompile. Note that deactivated precompiles
/// are still considered precompiles and will return `true`.
/// @param a: Address to query
/// @return output Is this address a precompile?
/// @custom:selector 446b450e
function isPrecompile(address a) external returns (bool);

/// @dev Query if the given address is an active precompile. Will return false if the
/// address is not a precompile or if this precompile is deactivated.
/// @param a: Address to query
/// @return output Is this address an active precompile?
/// @custom:selector 6f5e23cf
function isActivePrecompile(address a) external returns (bool);

/// @dev Update the account code of a precompile address.
/// As precompiles are implemented inside the Runtime, they don't have a bytecode, and
/// their account code is empty by default. However in Solidity calling a function of a
/// contract often automatically adds a check that the contract bytecode is non-empty.
/// For that reason a dummy code (0x60006000fd) can be inserted at the precompile address
/// to pass that check. This function allows any user to insert that code to precompile address
/// if they need it.
/// @param a: Address of the precompile.
/// @custom:selector 48ceb1b4
function updateAccountCode(address a) external;
}
79 changes: 79 additions & 0 deletions precompiles/precompile-registry/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright 2019-2022 PureStake Inc.
// This file is part of Moonbeam.

// Moonbeam is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Moonbeam is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Moonbeam. If not, see <http://www.gnu.org/licenses/>.

#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;

use core::marker::PhantomData;
use fp_evm::PrecompileSet;
use precompile_utils::{precompile_set::IsActivePrecompile, prelude::*};
use sp_core::Get;

const DUMMY_CODE: [u8; 5] = [0x60, 0x00, 0x60, 0x00, 0xfd];

pub struct PrecompileRegistry<Runtime>(PhantomData<Runtime>);

#[precompile_utils::precompile]
impl<Runtime> PrecompileRegistry<Runtime>
where
Runtime: pallet_evm::Config,
Runtime::PrecompilesType: IsActivePrecompile,
{
#[precompile::public("isPrecompile(address)")]
#[precompile::view]
fn is_precompile(handle: &mut impl PrecompileHandle, address: Address) -> EvmResult<bool> {
// We consider the precompile set is optimized to do at most one storage read.
handle.record_cost(RuntimeHelper::<Runtime>::db_read_gas_cost())?;

let active = <Runtime::PrecompilesValue>::get().is_precompile(address.0);
Ok(active)
}

#[precompile::public("isActivePrecompile(address)")]
#[precompile::view]
fn is_active_precompile(
handle: &mut impl PrecompileHandle,
address: Address,
) -> EvmResult<bool> {
// We consider the precompile set is optimized to do at most one storage read.
handle.record_cost(RuntimeHelper::<Runtime>::db_read_gas_cost())?;

let active = <Runtime::PrecompilesValue>::get().is_active_precompile(address.0);
Ok(active)
}

#[precompile::public("updateAccountCode(address)")]
fn update_account_code(handle: &mut impl PrecompileHandle, address: Address) -> EvmResult<()> {
// We consider the precompile set is optimized to do at most one storage read.
handle.record_cost(RuntimeHelper::<Runtime>::db_read_gas_cost())?;

// We will write into the account code.
handle.record_cost(RuntimeHelper::<Runtime>::db_write_gas_cost())?;

// Prevent touching addresses that are not precompiles.
if !<Runtime::PrecompilesValue>::get().is_precompile(address.0) {
return Err(revert("provided address is not a precompile"));
}

pallet_evm::Pallet::<Runtime>::create_account(address.0, DUMMY_CODE.to_vec());

Ok(())
}
}