git.delta.rocks / unique-network / refs/commits / a5d769284714

difftreelog

feat(contract-helpers) generous sponsoring mode

Yaroslav Bolyukin2022-01-19parent: #fcf0631.patch.diff
in: master

7 files changed

modifiedcrates/evm-coder/src/abi.rsdiffbeforeafterboth
95 string::from_utf8(self.bytes()?).map_err(|_| Error::Error(ExitError::InvalidRange))95 string::from_utf8(self.bytes()?).map_err(|_| Error::Error(ExitError::InvalidRange))
96 }96 }
97
98 pub fn uint8(&mut self) -> Result<u8> {
99 Ok(self.read_padleft::<1>()?[0])
100 }
97101
98 pub fn uint32(&mut self) -> Result<u32> {102 pub fn uint32(&mut self) -> Result<u32> {
99 Ok(u32::from_be_bytes(self.read_padleft()?))103 Ok(u32::from_be_bytes(self.read_padleft()?))
243 };247 };
244}248}
245249
250impl_abi_readable!(u8, uint8);
246impl_abi_readable!(u32, uint32);251impl_abi_readable!(u32, uint32);
247impl_abi_readable!(u64, uint64);252impl_abi_readable!(u64, uint64);
248impl_abi_readable!(u128, uint128);253impl_abi_readable!(u128, uint128);
deletedpallets/evm-contract-helpers/exp.rsdiffbeforeafterboth

no changes

modifiedpallets/evm-contract-helpers/src/eth.rsdiffbeforeafterboth
4use pallet_evm::{ExitRevert, OnCreate, OnMethodCall, PrecompileResult, PrecompileFailure};4use pallet_evm::{ExitRevert, OnCreate, OnMethodCall, PrecompileResult, PrecompileFailure};
5use sp_core::H160;5use sp_core::H160;
6use crate::{6use crate::{
7 AllowlistEnabled, Config, Owner, Pallet, SelfSponsoring, SponsorBasket, SponsoringRateLimit,7 AllowlistEnabled, Config, Owner, Pallet, SelfSponsoring, SponsoringMode, SponsorBasket,
8 SponsoringRateLimit, SponsoringModeT,
8};9};
9use frame_support::traits::Get;10use frame_support::traits::Get;
10use up_sponsorship::SponsorshipHandler;11use up_sponsorship::SponsorshipHandler;
31 Ok(<SelfSponsoring<T>>::get(contract_address))32 Ok(<SelfSponsoring<T>>::get(contract_address))
32 }33 }
3334
35 /// Deprecated
34 fn toggle_sponsoring(36 fn toggle_sponsoring(
35 &mut self,37 &mut self,
36 caller: caller,38 caller: caller,
42 Ok(())44 Ok(())
43 }45 }
46
47 fn set_sponsoring_mode(
48 &mut self,
49 caller: caller,
50 contract_address: address,
51 mode: uint8,
52 ) -> Result<void> {
53 <Pallet<T>>::ensure_owner(contract_address, caller)?;
54 let mode = SponsoringModeT::from_eth(mode).ok_or("unknown mode")?;
55 <Pallet<T>>::set_sponsoring_mode(contract_address, mode);
56 Ok(())
57 }
58
59 fn sponsoring_mode(&self, contract_address: address) -> Result<uint8> {
60 Ok(<Pallet<T>>::sponsoring_mode(contract_address).to_eth())
61 }
4462
45 fn set_sponsoring_rate_limit(63 fn set_sponsoring_rate_limit(
46 &mut self,64 &mut self,
147pub struct HelpersContractSponsoring<T: Config>(PhantomData<*const T>);165pub struct HelpersContractSponsoring<T: Config>(PhantomData<*const T>);
148impl<T: Config> SponsorshipHandler<H160, (H160, Vec<u8>)> for HelpersContractSponsoring<T> {166impl<T: Config> SponsorshipHandler<H160, (H160, Vec<u8>)> for HelpersContractSponsoring<T> {
149 fn get_sponsor(who: &H160, call: &(H160, Vec<u8>)) -> Option<H160> {167 fn get_sponsor(who: &H160, call: &(H160, Vec<u8>)) -> Option<H160> {
150 if !<SelfSponsoring<T>>::get(&call.0) {168 let mode = <Pallet<T>>::sponsoring_mode(call.0);
151 return None;169 if mode == SponsoringModeT::Disabled {
152 }170 return None;
171 }
153 if !<Pallet<T>>::allowed(call.0, *who) {172 if mode == SponsoringModeT::Allowlisted && !<Pallet<T>>::allowed(call.0, *who) {
154 return None;173 return None;
155 }174 }
156 let block_number = <frame_system::Pallet<T>>::block_number() as T::BlockNumber;175 let block_number = <frame_system::Pallet<T>>::block_number() as T::BlockNumber;
modifiedpallets/evm-contract-helpers/src/lib.rsdiffbeforeafterboth
1#![cfg_attr(not(feature = "std"), no_std)]1#![cfg_attr(not(feature = "std"), no_std)]
22
3use codec::{Decode, Encode};
3pub use pallet::*;4pub use pallet::*;
4pub use eth::*;5pub use eth::*;
6use scale_info::TypeInfo;
5pub mod eth;7pub mod eth;
68
7#[frame_support::pallet]9#[frame_support::pallet]
8pub mod pallet {10pub mod pallet {
11 pub use super::*;
9 use evm_coder::execution::Result;12 use evm_coder::execution::Result;
10 use frame_support::pallet_prelude::*;13 use frame_support::pallet_prelude::*;
11 use sp_core::H160;14 use sp_core::H160;
31 StorageMap<Hasher = Twox128, Key = H160, Value = H160, QueryKind = ValueQuery>;34 StorageMap<Hasher = Twox128, Key = H160, Value = H160, QueryKind = ValueQuery>;
3235
33 #[pallet::storage]36 #[pallet::storage]
37 #[deprecated]
34 pub(super) type SelfSponsoring<T: Config> =38 pub(super) type SelfSponsoring<T: Config> =
35 StorageMap<Hasher = Twox128, Key = H160, Value = bool, QueryKind = ValueQuery>;39 StorageMap<Hasher = Twox128, Key = H160, Value = bool, QueryKind = ValueQuery>;
40
41 #[pallet::storage]
42 pub(super) type SponsoringMode<T: Config> =
43 StorageMap<Hasher = Twox128, Key = H160, Value = SponsoringModeT, QueryKind = OptionQuery>;
3644
37 #[pallet::storage]45 #[pallet::storage]
38 pub(super) type SponsoringRateLimit<T: Config> = StorageMap<46 pub(super) type SponsoringRateLimit<T: Config> = StorageMap<
68 >;76 >;
6977
70 impl<T: Config> Pallet<T> {78 impl<T: Config> Pallet<T> {
79 pub fn sponsoring_mode(contract: H160) -> SponsoringModeT {
80 <SponsoringMode<T>>::get(contract)
81 .or_else(|| {
82 <SelfSponsoring<T>>::get(contract).then(|| SponsoringModeT::Allowlisted)
83 })
84 .unwrap_or_default()
85 }
86 pub fn set_sponsoring_mode(contract: H160, mode: SponsoringModeT) {
87 if mode == SponsoringModeT::Disabled {
88 <SponsoringMode<T>>::remove(contract);
89 } else {
90 <SponsoringMode<T>>::insert(contract, mode);
91 }
92 <SelfSponsoring<T>>::remove(contract)
93 }
94
71 pub fn toggle_sponsoring(contract: H160, enabled: bool) {95 pub fn toggle_sponsoring(contract: H160, enabled: bool) {
72 <SelfSponsoring<T>>::insert(contract, enabled);96 Self::set_sponsoring_mode(
97 contract,
98 if enabled {
99 SponsoringModeT::Allowlisted
100 } else {
101 SponsoringModeT::Disabled
102 },
103 )
73 }104 }
74105
95 }126 }
96}127}
128
129#[derive(Encode, Decode, PartialEq, TypeInfo)]
130pub enum SponsoringModeT {
131 Disabled,
132 Allowlisted,
133 Generous,
134}
135
136impl SponsoringModeT {
137 fn from_eth(v: u8) -> Option<Self> {
138 Some(match v {
139 0 => Self::Disabled,
140 1 => Self::Allowlisted,
141 2 => Self::Generous,
142 _ => return None,
143 })
144 }
145 fn to_eth(self) -> u8 {
146 match self {
147 SponsoringModeT::Disabled => 0,
148 SponsoringModeT::Allowlisted => 1,
149 SponsoringModeT::Generous => 2,
150 }
151 }
152}
153
154impl Default for SponsoringModeT {
155 fn default() -> Self {
156 Self::Disabled
157 }
158}
97159
modifiedpallets/evm-contract-helpers/src/stubs/ContractHelpers.rawdiffbeforeafterboth

binary blob — no preview

modifiedpallets/evm-contract-helpers/src/stubs/ContractHelpers.soldiffbeforeafterboth
21 }21 }
22}22}
2323
24// Selector: 31acb1fe24// Selector: 7b4866f9
25contract ContractHelpers is Dummy, ERC165 {25contract ContractHelpers is Dummy, ERC165 {
26 // Selector: contractOwner(address) 5152b14c26 // Selector: contractOwner(address) 5152b14c
27 function contractOwner(address contractAddress)27 function contractOwner(address contractAddress)
47 return false;47 return false;
48 }48 }
4949
50 // Deprecated
51 //
50 // Selector: toggleSponsoring(address,bool) fcac6d8652 // Selector: toggleSponsoring(address,bool) fcac6d86
51 function toggleSponsoring(address contractAddress, bool enabled) public {53 function toggleSponsoring(address contractAddress, bool enabled) public {
52 require(false, stub_error);54 require(false, stub_error);
55 dummy = 0;57 dummy = 0;
56 }58 }
59
60 // Selector: setSponsoringMode(address,uint8) fde8a560
61 function setSponsoringMode(address contractAddress, uint8 mode) public {
62 require(false, stub_error);
63 contractAddress;
64 mode;
65 dummy = 0;
66 }
67
68 // Selector: sponsoringMode(address) b70c7267
69 function sponsoringMode(address contractAddress)
70 public
71 view
72 returns (uint8)
73 {
74 require(false, stub_error);
75 contractAddress;
76 dummy;
77 return 0;
78 }
5779
58 // Selector: setSponsoringRateLimit(address,uint32) 77b6c90880 // Selector: setSponsoringRateLimit(address,uint32) 77b6c908
59 function setSponsoringRateLimit(address contractAddress, uint32 rateLimit)81 function setSponsoringRateLimit(address contractAddress, uint32 rateLimit)
modifiedtests/src/eth/api/ContractHelpers.soldiffbeforeafterboth
12 function supportsInterface(bytes4 interfaceID) external view returns (bool);12 function supportsInterface(bytes4 interfaceID) external view returns (bool);
13}13}
1414
15// Selector: 31acb1fe15// Selector: 7b4866f9
16interface ContractHelpers is Dummy, ERC165 {16interface ContractHelpers is Dummy, ERC165 {
17 // Selector: contractOwner(address) 5152b14c17 // Selector: contractOwner(address) 5152b14c
18 function contractOwner(address contractAddress)18 function contractOwner(address contractAddress)
26 view26 view
27 returns (bool);27 returns (bool);
2828
29 // Deprecated
30 //
29 // Selector: toggleSponsoring(address,bool) fcac6d8631 // Selector: toggleSponsoring(address,bool) fcac6d86
30 function toggleSponsoring(address contractAddress, bool enabled) external;32 function toggleSponsoring(address contractAddress, bool enabled) external;
33
34 // Selector: setSponsoringMode(address,uint8) fde8a560
35 function setSponsoringMode(address contractAddress, uint8 mode) external;
36
37 // Selector: sponsoringMode(address) b70c7267
38 function sponsoringMode(address contractAddress)
39 external
40 view
41 returns (uint8);
3142
32 // Selector: setSponsoringRateLimit(address,uint32) 77b6c90843 // Selector: setSponsoringRateLimit(address,uint32) 77b6c908
33 function setSponsoringRateLimit(address contractAddress, uint32 rateLimit)44 function setSponsoringRateLimit(address contractAddress, uint32 rateLimit)