difftreelog
feat evm contract helpers pallet
in: master
5 files changed
pallets/evm-contract-helpers/Cargo.tomldiffbeforeafterboth--- /dev/null
+++ b/pallets/evm-contract-helpers/Cargo.toml
@@ -0,0 +1,36 @@
+[package]
+name = "pallet-evm-contract-helpers"
+version = "0.1.0"
+edition = "2018"
+
+[dependencies]
+frame-support = { default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.8' }
+frame-system = { default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.8' }
+sp-runtime = { default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.8' }
+sp-std = { default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.8' }
+sp-core = { default-features = false, version = '3.0.0', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.8' }
+evm-coder = { default-features = false, path = '../../crates/evm-coder' }
+pallet-evm-coder-substrate = { default-features = false, path = '../../pallets/evm-coder-substrate' }
+pallet-evm = { default-features = false, version = "5.0.0-dev", git = "https://github.com/uniquenetwork/frontier.git", branch = "injected-transactions-parachain" }
+up-sponsorship = { default-features = false, path = '../../primitives/sponsorship' }
+log = "0.4.14"
+
+[dependencies.codec]
+default-features = false
+features = ['derive']
+package = 'parity-scale-codec'
+version = '2.0.0'
+
+[features]
+default = ["std"]
+std = [
+ "frame-support/std",
+ "frame-system/std",
+ "sp-runtime/std",
+ "sp-std/std",
+ "sp-core/std",
+ "evm-coder/std",
+ "pallet-evm-coder-substrate/std",
+ "pallet-evm/std",
+ "up-sponsorship/std",
+]
pallets/evm-contract-helpers/src/eth.rsdiffbeforeafterboth--- /dev/null
+++ b/pallets/evm-contract-helpers/src/eth.rs
@@ -0,0 +1,150 @@
+use core::marker::PhantomData;
+use evm_coder::{abi::AbiWriter, execution::Result, solidity_interface, types::*};
+use pallet_evm_coder_substrate::SubstrateRecorder;
+use pallet_evm::{ExitReason, ExitRevert, OnCreate, OnMethodCall, PrecompileOutput};
+use sp_core::H160;
+use crate::{
+ AllowlistEnabled, Config, Owner, Pallet, SelfSponsoring, SponsorBasket, SponsoringRateLimit,
+};
+use frame_support::traits::Get;
+use up_sponsorship::SponsorshipHandler;
+use sp_std::vec::Vec;
+
+struct ContractHelpers<T: Config>(SubstrateRecorder<T>);
+
+#[solidity_interface(name = "ContractHelpers")]
+impl<T: Config> ContractHelpers<T> {
+ fn contract_owner(&self, contract: address) -> Result<address> {
+ self.0.consume_sload()?;
+ Ok(<Owner<T>>::get(contract))
+ }
+
+ fn sponsoring_enabled(&self, contract: address) -> Result<bool> {
+ self.0.consume_sload()?;
+ Ok(<SelfSponsoring<T>>::get(contract))
+ }
+
+ fn toggle_sponsoring(
+ &mut self,
+ caller: caller,
+ contract: address,
+ enabled: bool,
+ ) -> Result<void> {
+ self.0.consume_sload()?;
+ <Pallet<T>>::ensure_owner(contract, caller)?;
+ self.0.consume_sstore()?;
+ <Pallet<T>>::toggle_sponsoring(contract, enabled);
+ Ok(())
+ }
+
+ fn allowed(&self, contract: address, user: address) -> Result<bool> {
+ self.0.consume_sload()?;
+ Ok(<Pallet<T>>::allowed(contract, user))
+ }
+
+ fn allowlist_enabled(&self, contract: address) -> Result<bool> {
+ self.0.consume_sload()?;
+ Ok(<AllowlistEnabled<T>>::get(contract))
+ }
+
+ fn toggle_allowlist(
+ &mut self,
+ caller: caller,
+ contract: address,
+ enabled: bool,
+ ) -> Result<void> {
+ self.0.consume_sload()?;
+ <Pallet<T>>::ensure_owner(contract, caller)?;
+ self.0.consume_sstore()?;
+ <Pallet<T>>::toggle_allowlist(contract, enabled);
+ Ok(())
+ }
+
+ fn toggle_allowed(
+ &mut self,
+ caller: caller,
+ contract: address,
+ user: address,
+ allowed: bool,
+ ) -> Result<void> {
+ self.0.consume_sload()?;
+ <Pallet<T>>::ensure_owner(contract, caller)?;
+ self.0.consume_sstore()?;
+ <Pallet<T>>::toggle_allowed(contract, user, allowed);
+ Ok(())
+ }
+}
+
+pub struct HelpersOnMethodCall<T: Config>(PhantomData<*const T>);
+impl<T: Config> OnMethodCall<T> for HelpersOnMethodCall<T> {
+ fn is_reserved(contract: &sp_core::H160) -> bool {
+ contract == &T::ContractAddress::get()
+ }
+
+ fn is_used(contract: &sp_core::H160) -> bool {
+ contract == &T::ContractAddress::get()
+ }
+
+ fn call(
+ source: &sp_core::H160,
+ target: &sp_core::H160,
+ gas_left: u64,
+ input: &[u8],
+ value: sp_core::U256,
+ ) -> Option<PrecompileOutput> {
+ // TODO: Extract to another OnMethodCall handler
+ if !<Pallet<T>>::allowed(*target, *source) {
+ return Some(PrecompileOutput {
+ exit_status: ExitReason::Revert(ExitRevert::Reverted),
+ cost: 0,
+ output: {
+ let mut writer = AbiWriter::new_call(evm_coder::fn_selector!(Error(string)));
+ writer.string("Target contract is allowlisted");
+ writer.finish()
+ },
+ logs: sp_std::vec![],
+ });
+ }
+
+ if target != &T::ContractAddress::get() {
+ return None;
+ }
+
+ let mut helpers = ContractHelpers::<T>(SubstrateRecorder::<T>::new(*target, gas_left));
+ let result = pallet_evm_coder_substrate::call_internal(*source, &mut helpers, value, input);
+ helpers.0.evm_to_precompile_output(result)
+ }
+
+ fn get_code(contract: &sp_core::H160) -> Option<Vec<u8>> {
+ (contract == &T::ContractAddress::get())
+ .then(|| include_bytes!("./stubs/ContractHelpers.bin").to_vec())
+ }
+}
+
+pub struct HelpersOnCreate<T: Config>(PhantomData<*const T>);
+impl<T: Config> OnCreate<T> for HelpersOnCreate<T> {
+ fn on_create(owner: H160, contract: H160) {
+ <Owner<T>>::insert(contract, owner);
+ }
+}
+
+pub struct HelpersContractSponsoring<T: Config>(PhantomData<*const T>);
+impl<T: Config> SponsorshipHandler<H160, (H160, Vec<u8>)> for HelpersContractSponsoring<T> {
+ fn get_sponsor(who: &H160, call: &(H160, Vec<u8>)) -> Option<H160> {
+ if <SelfSponsoring<T>>::get(&call.0) {
+ let block_number = <frame_system::Pallet<T>>::block_number() as T::BlockNumber;
+ let limit = <SponsoringRateLimit<T>>::get(&call.0);
+ if let Some(last_tx_block) = <SponsorBasket<T>>::get(&call.0, who) {
+ <SponsorBasket<T>>::insert(&call.0, who, block_number);
+ let limit_time = last_tx_block + limit.into();
+ if block_number > limit_time {
+ return Some(call.0);
+ }
+ } else {
+ <SponsorBasket<T>>::insert(&call.0, who, block_number);
+ return Some(call.0);
+ }
+ }
+ None
+ }
+}
pallets/evm-contract-helpers/src/lib.rsdiffbeforeafterboth1#![cfg_attr(not(feature = "std"), no_std)]23pub use pallet::*;4pub use eth::*;5pub mod eth;67#[frame_support::pallet]8pub mod pallet {9 use evm_coder::execution::Result;10 use frame_support::pallet_prelude::*;11 use pallet_evm::RawEvent;12 use sp_core::H160;1314 #[pallet::config]15 pub trait Config: frame_system::Config + pallet_evm_coder_substrate::Config {16 type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;1718 type ContractAddress: Get<H160>;19 }2021 #[pallet::error]22 pub enum Error<T> {23 /// This method is only executable by owner24 NoPermission,25 }2627 #[pallet::pallet]28 #[pallet::generate_store(pub(super) trait Store)]29 pub struct Pallet<T>(_);3031 #[pallet::storage]32 pub(super) type Owner<T: Config> =33 StorageMap<Hasher = Twox128, Key = H160, Value = H160, QueryKind = ValueQuery>;3435 #[pallet::storage]36 pub(super) type SelfSponsoring<T: Config> =37 StorageMap<Hasher = Twox128, Key = H160, Value = bool, QueryKind = ValueQuery>;3839 #[pallet::storage]40 pub(super) type SponsoringRateLimit<T: Config> =41 StorageMap<Hasher = Twox128, Key = H160, Value = T::BlockNumber, QueryKind = ValueQuery>;4243 #[pallet::storage]44 pub(super) type SponsorBasket<T: Config> = StorageDoubleMap<45 Hasher1 = Twox128,46 Key1 = H160,47 Hasher2 = Twox128,48 Key2 = H160,49 Value = T::BlockNumber,50 QueryKind = OptionQuery,51 >;5253 #[pallet::storage]54 pub(super) type AllowlistEnabled<T: Config> =55 StorageMap<Hasher = Twox128, Key = H160, Value = bool, QueryKind = ValueQuery>;5657 #[pallet::storage]58 pub(super) type Allowlist<T: Config> = StorageDoubleMap<59 Hasher1 = Twox128,60 Key1 = H160,61 Hasher2 = Twox128,62 Key2 = H160,63 Value = bool,64 QueryKind = ValueQuery,65 >;6667 impl<T: Config> Pallet<T> {68 pub fn toggle_sponsoring(contract: H160, enabled: bool) {69 <SelfSponsoring<T>>::insert(contract, enabled);70 }7172 pub fn allowed(contract: H160, user: H160) -> bool {73 if !<AllowlistEnabled<T>>::get(contract) {74 return true;75 }76 <Allowlist<T>>::get(&contract, &user) || <Owner<T>>::get(&contract) == user77 }7879 pub fn toggle_allowlist(contract: H160, enabled: bool) {80 <AllowlistEnabled<T>>::insert(contract, enabled)81 }8283 pub fn toggle_allowed(contract: H160, user: H160, allowed: bool) {84 <Allowlist<T>>::insert(contract, user, allowed);85 }8687 pub fn ensure_owner(contract: H160, user: H160) -> Result<()> {88 ensure!(<Owner<T>>::get(&contract) == user, "no permission");89 Ok(())90 }91 }92}pallets/evm-contract-helpers/src/stubs/ContractHelpers.bindiffbeforeafterbothbinary blob — no preview
pallets/evm-contract-helpers/src/stubs/ContractHelpers.soldiffbeforeafterboth--- /dev/null
+++ b/pallets/evm-contract-helpers/src/stubs/ContractHelpers.sol
@@ -0,0 +1,40 @@
+contract ContractHelpers {
+ uint8 _dummmy = 0;
+ address _dummy_addr = 0x0000000000000000000000000000000000000000;
+ string stub_error = "this contract does not exists, contract helpers are implemented on substrate chain side";
+
+ function contractOwner(address contract_address) public view returns (address) {
+ require(false, stub_error);
+ contract_address;
+ return _dummy_addr;
+ }
+
+ function sponsoringEnabled(address contract_address) public view returns (bool) {
+ require(false, stub_error);
+ contract_address;
+ _dummmy;
+ return false;
+ }
+
+ function toggleSponsoring(address contract_address, bool enabled) public {
+ require(false, stub_error);
+ contract_address;
+ enabled;
+ _dummmy = 0;
+ }
+
+ function toggleAllowlist(address contract_address, bool enabled) public {
+ require(false, stub_error);
+ contract_address;
+ enabled;
+ _dummmy = 0;
+ }
+
+ function toggleAllowed(address contract_address, address user, bool allowed) public {
+ require(false, stub_error);
+ contract_address;
+ user;
+ allowed;
+ _dummmy = 0;
+ }
+}
\ No newline at end of file