Skip to content

Add support for delegatable precompile in transactions #628

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions Cargo.lock

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

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,9 @@ members = [
"template/runtime",
]
resolver = "2"

[patch.crates-io]
evm = { git = "https://github.com/rust-blockchain/evm", branch = "sp-transact-delegatecall" }
evm-core = { git = "https://github.com/rust-blockchain/evm", branch = "sp-transact-delegatecall" }
evm-runtime = { git = "https://github.com/rust-blockchain/evm", branch = "sp-transact-delegatecall" }
evm-gasometer = { git = "https://github.com/rust-blockchain/evm", branch = "sp-transact-delegatecall" }
5 changes: 2 additions & 3 deletions frame/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub use evm::{
use fp_evm::GenesisAccount;
pub use fp_evm::{
Account, CallInfo, CreateInfo, ExecutionInfo, FeeCalculator, LinearCostPrecompile, Log,
Precompile, PrecompileFailure, PrecompileOutput, PrecompileResult, PrecompileSet, Vicinity,
Precompile, PrecompileFailure, PrecompileOutput, PrecompileResult, DelegatablePrecompileSet, Vicinity,
};

pub use self::{pallet::*, runner::Runner};
Expand Down Expand Up @@ -124,8 +124,7 @@ pub mod pallet {
/// The overarching event type.
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
/// Precompiles associated with this EVM engine.
type PrecompilesType: PrecompileSet;
type PrecompilesValue: Get<Self::PrecompilesType>;
type Precompiles: DelegatablePrecompileSet + Default;
/// Chain ID of EVM.
type ChainId: Get<u64>;
/// The block gas limit. Can be a simple constant, or an adjustment algorithm in another pallet.
Expand Down
18 changes: 11 additions & 7 deletions frame/evm/src/runner/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use evm::{
executor::stack::{Accessed, StackExecutor, StackState as StackStateT, StackSubstateMetadata},
ExitError, ExitReason, Transfer,
};
use fp_evm::{CallInfo, CreateInfo, ExecutionInfo, Log, Vicinity};
use fp_evm::{CallInfo, CreateInfo, ExecutionInfo, Log, Vicinity, DelegatablePrecompileSet};
use frame_support::{
ensure,
traits::{Currency, ExistenceRequirement, Get},
Expand All @@ -51,7 +51,7 @@ impl<T: Config> Runner<T> {
max_priority_fee_per_gas: Option<U256>,
nonce: Option<U256>,
config: &'config evm::Config,
precompiles: &'precompiles T::PrecompilesType,
precompiles: &'precompiles T::Precompiles,
f: F,
) -> Result<ExecutionInfo<R>, Error<T>>
where
Expand All @@ -60,7 +60,7 @@ impl<T: Config> Runner<T> {
'config,
'precompiles,
SubstrateStackState<'_, 'config, T>,
T::PrecompilesType,
T::Precompiles,
>,
) -> (ExitReason, R),
{
Expand Down Expand Up @@ -218,7 +218,7 @@ impl<T: Config> RunnerT<T> for Runner<T> {
access_list: Vec<(H160, Vec<H256>)>,
config: &evm::Config,
) -> Result<CallInfo, Self::Error> {
let precompiles = T::PrecompilesValue::get();
let precompiles = T::Precompiles::default();
Self::execute(
source,
value,
Expand All @@ -228,7 +228,11 @@ impl<T: Config> RunnerT<T> for Runner<T> {
nonce,
config,
&precompiles,
|executor| executor.transact_call(source, target, value, input, gas_limit, access_list),
|executor| if precompiles.is_delegatable_precompile(target) {
executor.transact_delegatecall(source, target, value, input, gas_limit, access_list)
} else {
executor.transact_call(source, target, value, input, gas_limit, access_list)
},
)
}

Expand All @@ -243,7 +247,7 @@ impl<T: Config> RunnerT<T> for Runner<T> {
access_list: Vec<(H160, Vec<H256>)>,
config: &evm::Config,
) -> Result<CreateInfo, Self::Error> {
let precompiles = T::PrecompilesValue::get();
let precompiles = T::Precompiles::default();
Self::execute(
source,
value,
Expand Down Expand Up @@ -274,7 +278,7 @@ impl<T: Config> RunnerT<T> for Runner<T> {
access_list: Vec<(H160, Vec<H256>)>,
config: &evm::Config,
) -> Result<CreateInfo, Self::Error> {
let precompiles = T::PrecompilesValue::get();
let precompiles = T::Precompiles::default();
let code_hash = H256::from_slice(Keccak256::digest(&init).as_slice());
Self::execute(
source,
Expand Down
2 changes: 2 additions & 0 deletions primitives/evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ targets = ["x86_64-unknown-linux-gnu"]
evm = { version = "0.35.0", default-features = false, features = ["with-codec"] }
serde = { version = "1.0.101", features = ["derive"], optional = true }

primitive-types = { version = "0.11.1", default-features = false, features = ["rlp", "byteorder"] }
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false }
sp-core = { version = "6.0.0", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
sp-std = { version = "4.0.0", git = "https://github.com/paritytech/substrate", branch = "master", default-features = false }
Expand All @@ -27,6 +28,7 @@ std = [
"evm/with-serde",
"serde",

"primitive-types/std",
"codec/std",
"sp-core/std",
"sp-std/std",
Expand Down
2 changes: 1 addition & 1 deletion primitives/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub use evm::backend::{Basic as Account, Log};

pub use self::precompile::{
Context, ExitError, ExitRevert, ExitSucceed, LinearCostPrecompile, Precompile,
PrecompileFailure, PrecompileOutput, PrecompileResult, PrecompileSet,
PrecompileFailure, PrecompileOutput, PrecompileResult, PrecompileSet, DelegatablePrecompileSet,
};

#[derive(Clone, Eq, PartialEq, Encode, Decode, Default)]
Expand Down
5 changes: 5 additions & 0 deletions primitives/evm/src/precompile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub use evm::{
Context, ExitError, ExitRevert, ExitSucceed,
};
use sp_std::vec::Vec;
use primitive_types::H160;

pub type PrecompileResult = Result<PrecompileOutput, PrecompileFailure>;

Expand All @@ -36,6 +37,10 @@ pub trait Precompile {
) -> PrecompileResult;
}

pub trait DelegatablePrecompileSet: PrecompileSet {
fn is_delegatable_precompile(&self, address: H160) -> bool;
}

pub trait LinearCostPrecompile {
const BASE: u64;
const WORD: u64;
Expand Down
3 changes: 1 addition & 2 deletions template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,8 +315,7 @@ impl pallet_evm::Config for Runtime {
type Currency = Balances;
type Event = Event;
type Runner = pallet_evm::runner::stack::Runner<Self>;
type PrecompilesType = FrontierPrecompiles<Self>;
type PrecompilesValue = PrecompilesValue;
type Precompiles = FrontierPrecompiles<Self>;
type ChainId = ChainId;
type BlockGasLimit = BlockGasLimit;
type OnChargeTransaction = ();
Expand Down
13 changes: 12 additions & 1 deletion template/runtime/src/precompiles.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use pallet_evm::{Context, Precompile, PrecompileResult, PrecompileSet};
use pallet_evm::{Context, Precompile, PrecompileResult, PrecompileSet, DelegatablePrecompileSet};
use sp_core::H160;
use sp_std::marker::PhantomData;

use pallet_evm_precompile_modexp::Modexp;
use pallet_evm_precompile_sha3fips::Sha3FIPS256;
use pallet_evm_precompile_simple::{ECRecover, ECRecoverPublicKey, Identity, Ripemd160, Sha256};

#[derive(Default)]
pub struct FrontierPrecompiles<R>(PhantomData<R>);

impl<R> FrontierPrecompiles<R>
Expand All @@ -15,13 +16,15 @@ where
pub fn new() -> Self {
Self(Default::default())
}

pub fn used_addresses() -> sp_std::vec::Vec<H160> {
sp_std::vec![1, 2, 3, 4, 5, 1024, 1025]
.into_iter()
.map(|x| hash(x))
.collect()
}
}

impl<R> PrecompileSet for FrontierPrecompiles<R>
where
R: pallet_evm::Config,
Expand Down Expand Up @@ -57,6 +60,14 @@ where
}
}

impl<R> DelegatablePrecompileSet for FrontierPrecompiles<R>
where
R: pallet_evm::Config,
{
fn is_delegatable_precompile(&self, address: H160) -> bool {
false
}

fn hash(a: u64) -> H160 {
H160::from_low_u64_be(a)
}