-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add precompile support for Substrate dispatchables (#246)
- Loading branch information
Showing
14 changed files
with
139 additions
and
22 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
[package] | ||
name = "pallet-evm-precompile-dispatch" | ||
version = "2.0.0" | ||
authors = ["Parity Technologies <admin@parity.io>"] | ||
edition = "2018" | ||
license = "Apache-2.0" | ||
homepage = "https://substrate.dev" | ||
repository = "https://github.com/paritytech/substrate/" | ||
description = "DISPATCH precompiles for EVM pallet." | ||
|
||
[dependencies] | ||
sp-core = { version = "2.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "frontier" } | ||
sp-io = { version = "2.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "frontier" } | ||
frame-support = { version = "2.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "frontier" } | ||
pallet-evm = { version = "2.0.0", default-features = false, path = "../.." } | ||
fp-evm = { version = "0.8.0", default-features = false, path = "../../../../primitives/evm" } | ||
evm = { version = "0.20.0", default-features = false, features = ["with-codec"] } | ||
codec = { package = "parity-scale-codec", version = "1.3.5", default-features = false } | ||
|
||
[features] | ||
default = ["std"] | ||
std = [ | ||
"sp-core/std", | ||
"sp-io/std", | ||
"frame-support/std", | ||
"pallet-evm/std", | ||
"fp-evm/std", | ||
"evm/std", | ||
"codec/std", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// This file is part of Frontier. | ||
// | ||
// Copyright (c) 2020 Parity Technologies (UK) Ltd. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#![cfg_attr(not(feature = "std"), no_std)] | ||
|
||
extern crate alloc; | ||
|
||
use alloc::vec::Vec; | ||
use core::marker::PhantomData; | ||
use fp_evm::Precompile; | ||
use evm::{ExitSucceed, ExitError, Context}; | ||
use frame_support::{dispatch::{Dispatchable, GetDispatchInfo, PostDispatchInfo}, weights::{Pays, DispatchClass}}; | ||
use pallet_evm::AddressMapping; | ||
use codec::Decode; | ||
|
||
pub struct Dispatch<T: pallet_evm::Config> { | ||
_marker: PhantomData<T>, | ||
} | ||
|
||
impl<T> Precompile for Dispatch<T> where | ||
T: pallet_evm::Config, | ||
T::Call: Dispatchable<PostInfo=PostDispatchInfo> + GetDispatchInfo + Decode, | ||
<T::Call as Dispatchable>::Origin: From<Option<T::AccountId>>, | ||
{ | ||
fn execute( | ||
input: &[u8], | ||
target_gas: Option<usize>, | ||
context: &Context, | ||
) -> core::result::Result<(ExitSucceed, Vec<u8>, usize), ExitError> { | ||
let call = T::Call::decode(&mut &input[..]).map_err(|_| ExitError::Other("decode failed".into()))?; | ||
let info = call.get_dispatch_info(); | ||
|
||
let valid_call = info.pays_fee == Pays::Yes && info.class == DispatchClass::Normal; | ||
if !valid_call { | ||
return Err(ExitError::Other("invalid call".into())) | ||
} | ||
|
||
if let Some(gas) = target_gas { | ||
let valid_weight = info.weight <= gas as u64; | ||
if !valid_weight { | ||
return Err(ExitError::OutOfGas) | ||
} | ||
} | ||
|
||
let origin = T::AddressMapping::into_account_id(context.caller); | ||
|
||
match call.dispatch(Some(origin).into()) { | ||
Ok(post_info) => { | ||
let cost = post_info.actual_weight.unwrap_or(info.weight) as usize; | ||
Ok((ExitSucceed::Stopped, Default::default(), cost)) | ||
}, | ||
Err(_) => Err(ExitError::Other("dispatch execution failed".into())), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters