difftreelog
Merge remote-tracking branch 'origin/develop' into feature/CORE-164
in: master
5 files changed
Makefilediffbeforeafterboth--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,10 @@
-.PHONY: _eth_codegen
-_eth_codegen:
+.PHONY: _help
+_help:
+ @echo "regenerate_solidity - generate stubs/interfaces for contracts defined in native (via evm-coder)"
+ @echo "evm_stubs - recompile contract stubs"
+ @echo "bench - run frame-benchmarking"
+ @echo " bench-evm-migration"
+ @echo " bench-nft"
.PHONY: regenerate_solidity
regenerate_solidity:
pallets/evm-contract-helpers/src/eth.rsdiffbeforeafterboth1use core::marker::PhantomData;2use evm_coder::{abi::AbiWriter, execution::Result, generate_stubgen, solidity_interface, types::*};3use pallet_evm_coder_substrate::SubstrateRecorder;4use pallet_evm::{ExitReason, ExitRevert, OnCreate, OnMethodCall, PrecompileOutput};5use sp_core::H160;6use crate::{7 AllowlistEnabled, Config, Owner, Pallet, SelfSponsoring, SponsorBasket, SponsoringRateLimit,8};9use frame_support::traits::Get;10use up_sponsorship::SponsorshipHandler;11use sp_std::vec::Vec;1213struct ContractHelpers<T: Config>(SubstrateRecorder<T>);1415#[solidity_interface(name = "ContractHelpers")]16impl<T: Config> ContractHelpers<T> {17 fn contract_owner(&self, contract_address: address) -> Result<address> {18 self.0.consume_sload()?;19 Ok(<Owner<T>>::get(contract_address))20 }2122 fn sponsoring_enabled(&self, contract_address: address) -> Result<bool> {23 self.0.consume_sload()?;24 Ok(<SelfSponsoring<T>>::get(contract_address))25 }2627 fn toggle_sponsoring(28 &mut self,29 caller: caller,30 contract_address: address,31 enabled: bool,32 ) -> Result<void> {33 self.0.consume_sload()?;34 <Pallet<T>>::ensure_owner(contract_address, caller)?;35 self.0.consume_sstore()?;36 <Pallet<T>>::toggle_sponsoring(contract_address, enabled);37 Ok(())38 }3940 fn set_sponsoring_rate_limit(41 &mut self,42 caller: caller,43 contract_address: address,44 rate_limit: uint32,45 ) -> Result<void> {46 self.0.consume_sload()?;47 <Pallet<T>>::ensure_owner(contract_address, caller)?;48 self.0.consume_sstore()?;49 <Pallet<T>>::set_sponsoring_rate_limit(contract_address, rate_limit.into());50 Ok(())51 }5253 fn allowed(&self, contract_address: address, user: address) -> Result<bool> {54 self.0.consume_sload()?;55 Ok(<Pallet<T>>::allowed(contract_address, user, true))56 }5758 fn allowlist_enabled(&self, contract_address: address) -> Result<bool> {59 self.0.consume_sload()?;60 Ok(<AllowlistEnabled<T>>::get(contract_address))61 }6263 fn toggle_allowlist(64 &mut self,65 caller: caller,66 contract_address: address,67 enabled: bool,68 ) -> Result<void> {69 self.0.consume_sload()?;70 <Pallet<T>>::ensure_owner(contract_address, caller)?;71 self.0.consume_sstore()?;72 <Pallet<T>>::toggle_allowlist(contract_address, enabled);73 Ok(())74 }7576 fn toggle_allowed(77 &mut self,78 caller: caller,79 contract_address: address,80 user: address,81 allowed: bool,82 ) -> Result<void> {83 self.0.consume_sload()?;84 <Pallet<T>>::ensure_owner(contract_address, caller)?;85 self.0.consume_sstore()?;86 <Pallet<T>>::toggle_allowed(contract_address, user, allowed);87 Ok(())88 }89}9091pub struct HelpersOnMethodCall<T: Config>(PhantomData<*const T>);92impl<T: Config> OnMethodCall<T> for HelpersOnMethodCall<T> {93 fn is_reserved(contract: &sp_core::H160) -> bool {94 contract == &T::ContractAddress::get()95 }9697 fn is_used(contract: &sp_core::H160) -> bool {98 contract == &T::ContractAddress::get()99 }100101 fn call(102 source: &sp_core::H160,103 target: &sp_core::H160,104 gas_left: u64,105 input: &[u8],106 value: sp_core::U256,107 ) -> Option<PrecompileOutput> {108 // TODO: Extract to another OnMethodCall handler109 if !<Pallet<T>>::allowed(*target, *source, true) {110 return Some(PrecompileOutput {111 exit_status: ExitReason::Revert(ExitRevert::Reverted),112 cost: 0,113 output: {114 let mut writer = AbiWriter::new_call(evm_coder::fn_selector!(Error(string)));115 writer.string("Target contract is allowlisted");116 writer.finish()117 },118 logs: sp_std::vec![],119 });120 }121122 if target != &T::ContractAddress::get() {123 return None;124 }125126 let mut helpers = ContractHelpers::<T>(SubstrateRecorder::<T>::new(*target, gas_left));127 let result = pallet_evm_coder_substrate::call_internal(*source, &mut helpers, value, input);128 helpers.0.evm_to_precompile_output(result)129 }130131 fn get_code(contract: &sp_core::H160) -> Option<Vec<u8>> {132 (contract == &T::ContractAddress::get())133 .then(|| include_bytes!("./stubs/ContractHelpers.raw").to_vec())134 }135}136137pub struct HelpersOnCreate<T: Config>(PhantomData<*const T>);138impl<T: Config> OnCreate<T> for HelpersOnCreate<T> {139 fn on_create(owner: H160, contract: H160) {140 <Owner<T>>::insert(contract, owner);141 }142}143144pub struct HelpersContractSponsoring<T: Config>(PhantomData<*const T>);145impl<T: Config> SponsorshipHandler<H160, (H160, Vec<u8>)> for HelpersContractSponsoring<T> {146 fn get_sponsor(who: &H160, call: &(H160, Vec<u8>)) -> Option<H160> {147 if <SelfSponsoring<T>>::get(&call.0) && <Pallet<T>>::allowed(call.0, *who, false) {148 let block_number = <frame_system::Pallet<T>>::block_number() as T::BlockNumber;149 if let Some(last_tx_block) = <SponsorBasket<T>>::get(&call.0, who) {150 let rate_limit = <SponsoringRateLimit<T>>::get(&call.0);151 let limit_time = last_tx_block + rate_limit;152153 if block_number > limit_time {154 <SponsorBasket<T>>::insert(&call.0, who, block_number);155 return Some(call.0);156 }157 } else {158 <SponsorBasket<T>>::insert(&call.0, who, block_number);159 return Some(call.0);160 }161 }162 None163 }164}165166generate_stubgen!(contract_helpers_impl, ContractHelpersCall, true);167generate_stubgen!(contract_helpers_iface, ContractHelpersCall, false);1use core::marker::PhantomData;2use evm_coder::{abi::AbiWriter, execution::Result, generate_stubgen, solidity_interface, types::*};3use pallet_evm_coder_substrate::SubstrateRecorder;4use pallet_evm::{ExitReason, ExitRevert, OnCreate, OnMethodCall, PrecompileOutput};5use sp_core::H160;6use crate::{7 AllowlistEnabled, Config, Owner, Pallet, SelfSponsoring, SponsorBasket, SponsoringRateLimit,8};9use frame_support::traits::Get;10use up_sponsorship::SponsorshipHandler;11use sp_std::{convert::TryInto, vec::Vec};1213struct ContractHelpers<T: Config>(SubstrateRecorder<T>);1415#[solidity_interface(name = "ContractHelpers")]16impl<T: Config> ContractHelpers<T> {17 fn contract_owner(&self, contract_address: address) -> Result<address> {18 self.0.consume_sload()?;19 Ok(<Owner<T>>::get(contract_address))20 }2122 fn sponsoring_enabled(&self, contract_address: address) -> Result<bool> {23 self.0.consume_sload()?;24 Ok(<SelfSponsoring<T>>::get(contract_address))25 }2627 fn toggle_sponsoring(28 &mut self,29 caller: caller,30 contract_address: address,31 enabled: bool,32 ) -> Result<void> {33 self.0.consume_sload()?;34 <Pallet<T>>::ensure_owner(contract_address, caller)?;35 self.0.consume_sstore()?;36 <Pallet<T>>::toggle_sponsoring(contract_address, enabled);37 Ok(())38 }3940 fn set_sponsoring_rate_limit(41 &mut self,42 caller: caller,43 contract_address: address,44 rate_limit: uint32,45 ) -> Result<void> {46 self.0.consume_sload()?;47 <Pallet<T>>::ensure_owner(contract_address, caller)?;48 self.0.consume_sstore()?;49 <Pallet<T>>::set_sponsoring_rate_limit(contract_address, rate_limit.into());50 Ok(())51 }5253 fn get_sponsoring_rate_limit(&self, contract_address: address) -> Result<uint32> {54 self.0.consume_sload()?;55 Ok(<SponsoringRateLimit<T>>::get(contract_address)56 .try_into()57 .map_err(|_| "rate limit > u32::MAX")?)58 }5960 fn allowed(&self, contract_address: address, user: address) -> Result<bool> {61 self.0.consume_sload()?;62 Ok(<Pallet<T>>::allowed(contract_address, user, true))63 }6465 fn allowlist_enabled(&self, contract_address: address) -> Result<bool> {66 self.0.consume_sload()?;67 Ok(<AllowlistEnabled<T>>::get(contract_address))68 }6970 fn toggle_allowlist(71 &mut self,72 caller: caller,73 contract_address: address,74 enabled: bool,75 ) -> Result<void> {76 self.0.consume_sload()?;77 <Pallet<T>>::ensure_owner(contract_address, caller)?;78 self.0.consume_sstore()?;79 <Pallet<T>>::toggle_allowlist(contract_address, enabled);80 Ok(())81 }8283 fn toggle_allowed(84 &mut self,85 caller: caller,86 contract_address: address,87 user: address,88 allowed: bool,89 ) -> Result<void> {90 self.0.consume_sload()?;91 <Pallet<T>>::ensure_owner(contract_address, caller)?;92 self.0.consume_sstore()?;93 <Pallet<T>>::toggle_allowed(contract_address, user, allowed);94 Ok(())95 }96}9798pub struct HelpersOnMethodCall<T: Config>(PhantomData<*const T>);99impl<T: Config> OnMethodCall<T> for HelpersOnMethodCall<T> {100 fn is_reserved(contract: &sp_core::H160) -> bool {101 contract == &T::ContractAddress::get()102 }103104 fn is_used(contract: &sp_core::H160) -> bool {105 contract == &T::ContractAddress::get()106 }107108 fn call(109 source: &sp_core::H160,110 target: &sp_core::H160,111 gas_left: u64,112 input: &[u8],113 value: sp_core::U256,114 ) -> Option<PrecompileOutput> {115 // TODO: Extract to another OnMethodCall handler116 if !<Pallet<T>>::allowed(*target, *source, true) {117 return Some(PrecompileOutput {118 exit_status: ExitReason::Revert(ExitRevert::Reverted),119 cost: 0,120 output: {121 let mut writer = AbiWriter::new_call(evm_coder::fn_selector!(Error(string)));122 writer.string("Target contract is allowlisted");123 writer.finish()124 },125 logs: sp_std::vec![],126 });127 }128129 if target != &T::ContractAddress::get() {130 return None;131 }132133 let mut helpers = ContractHelpers::<T>(SubstrateRecorder::<T>::new(*target, gas_left));134 let result = pallet_evm_coder_substrate::call_internal(*source, &mut helpers, value, input);135 helpers.0.evm_to_precompile_output(result)136 }137138 fn get_code(contract: &sp_core::H160) -> Option<Vec<u8>> {139 (contract == &T::ContractAddress::get())140 .then(|| include_bytes!("./stubs/ContractHelpers.raw").to_vec())141 }142}143144pub struct HelpersOnCreate<T: Config>(PhantomData<*const T>);145impl<T: Config> OnCreate<T> for HelpersOnCreate<T> {146 fn on_create(owner: H160, contract: H160) {147 <Owner<T>>::insert(contract, owner);148 }149}150151pub struct HelpersContractSponsoring<T: Config>(PhantomData<*const T>);152impl<T: Config> SponsorshipHandler<H160, (H160, Vec<u8>)> for HelpersContractSponsoring<T> {153 fn get_sponsor(who: &H160, call: &(H160, Vec<u8>)) -> Option<H160> {154 if <SelfSponsoring<T>>::get(&call.0) && <Pallet<T>>::allowed(call.0, *who, false) {155 let block_number = <frame_system::Pallet<T>>::block_number() as T::BlockNumber;156 if let Some(last_tx_block) = <SponsorBasket<T>>::get(&call.0, who) {157 let rate_limit = <SponsoringRateLimit<T>>::get(&call.0);158 let limit_time = last_tx_block + rate_limit;159160 if block_number > limit_time {161 <SponsorBasket<T>>::insert(&call.0, who, block_number);162 return Some(call.0);163 }164 } else {165 <SponsorBasket<T>>::insert(&call.0, who, block_number);166 return Some(call.0);167 }168 }169 None170 }171}172173generate_stubgen!(contract_helpers_impl, ContractHelpersCall, true);174generate_stubgen!(contract_helpers_iface, ContractHelpersCall, false);pallets/evm-contract-helpers/src/stubs/ContractHelpers.rawdiffbeforeafterbothbinary blob — no preview
pallets/evm-contract-helpers/src/stubs/ContractHelpers.soldiffbeforeafterboth--- a/pallets/evm-contract-helpers/src/stubs/ContractHelpers.sol
+++ b/pallets/evm-contract-helpers/src/stubs/ContractHelpers.sol
@@ -52,6 +52,18 @@
dummy = 0;
}
+ // Selector: getSponsoringRateLimit(address) 610cfabd
+ function getSponsoringRateLimit(address contractAddress)
+ public
+ view
+ returns (uint32)
+ {
+ require(false, stub_error);
+ contractAddress;
+ dummy;
+ return 0;
+ }
+
// Selector: allowed(address,address) 5c658165
function allowed(address contractAddress, address user)
public
tests/src/eth/api/ContractHelpers.soldiffbeforeafterboth--- a/tests/src/eth/api/ContractHelpers.sol
+++ b/tests/src/eth/api/ContractHelpers.sol
@@ -28,6 +28,12 @@
function setSponsoringRateLimit(address contractAddress, uint32 rateLimit)
external;
+ // Selector: getSponsoringRateLimit(address) 610cfabd
+ function getSponsoringRateLimit(address contractAddress)
+ external
+ view
+ returns (uint32);
+
// Selector: allowed(address,address) 5c658165
function allowed(address contractAddress, address user)
external