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

difftreelog

path: Add sponsorship for CrossAccountId.

Trubnikov Sergey2022-08-03parent: #6fbb721.patch.diff
in: master

2 files changed

modifiedpallets/evm-contract-helpers/src/eth.rsdiffbeforeafterboth
51 Ok(<Owner<T>>::get(contract_address))51 Ok(<Owner<T>>::get(contract_address))
52 }52 }
53
54 fn set_sponsor(
55 &mut self,
56 caller: caller,
57 contract_address: address,
58 sponsor: address,
59 ) -> Result<void> {
60 Pallet::<T>::set_sponsor(
61 &T::CrossAccountId::from_eth(caller),
62 contract_address,
63 &T::CrossAccountId::from_eth(sponsor),
64 )
65 .map_err(dispatch_to_evm::<T>)?;
66 Ok(())
67 }
68
69 fn confirm_sponsorship(&mut self, caller: caller, contract_address: address) -> Result<void> {
70 Pallet::<T>::confirm_sponsorship(&T::CrossAccountId::from_eth(caller), contract_address)
71 .map_err(dispatch_to_evm::<T>)?;
72 Ok(())
73 }
74
75 fn get_sponsor(&self, contract_address: address) -> Result<address> {
76 let sponsor =
77 Pallet::<T>::get_sponsor(contract_address).ok_or("Contract has no sponsor")?;
78 Ok(*sponsor.as_eth())
79 }
5380
54 fn sponsoring_enabled(&self, contract_address: address) -> Result<bool> {81 fn sponsoring_enabled(&self, contract_address: address) -> Result<bool> {
55 Ok(<Pallet<T>>::sponsoring_mode(contract_address) != SponsoringModeT::Disabled)82 Ok(<Pallet<T>>::sponsoring_mode(contract_address) != SponsoringModeT::Disabled)
190 return None;217 return None;
191 }218 }
219
220 let sponsor = match <Pallet<T>>::get_sponsor(*contract) {
221 Some(sponsor) => sponsor,
222 None => return None,
223 };
192224
193 if mode == SponsoringModeT::Allowlisted && !<Pallet<T>>::allowed(*contract, *who.as_eth()) {225 if mode == SponsoringModeT::Allowlisted && !<Pallet<T>>::allowed(*contract, *who.as_eth()) {
194 return None;226 return None;
206238
207 <SponsorBasket<T>>::insert(contract, who.as_eth(), block_number);239 <SponsorBasket<T>>::insert(contract, who.as_eth(), block_number);
208240
209 let sponsor = T::CrossAccountId::from_eth(*contract);
210 Some(sponsor)241 Some(sponsor)
211 }242 }
212}243}
modifiedpallets/evm-contract-helpers/src/lib.rsdiffbeforeafterboth
15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
1616
17#![cfg_attr(not(feature = "std"), no_std)]17#![cfg_attr(not(feature = "std"), no_std)]
18#![feature(is_some_with)]
1918
20use codec::{Decode, Encode, MaxEncodedLen};19use codec::{Decode, Encode, MaxEncodedLen};
21pub use pallet::*;20pub use pallet::*;
27pub mod pallet {26pub mod pallet {
28 pub use super::*;27 pub use super::*;
29 use frame_support::pallet_prelude::*;28 use frame_support::pallet_prelude::*;
29 use pallet_evm_coder_substrate::DispatchResult;
30 use sp_core::H160;30 use sp_core::H160;
31 use pallet_evm::account::CrossAccountId;31 use pallet_evm::account::CrossAccountId;
32 use frame_system::pallet_prelude::BlockNumberFor;32 use frame_system::pallet_prelude::BlockNumberFor;
33 use up_data_structs::SponsorshipState;
3334
34 #[pallet::config]35 #[pallet::config]
35 pub trait Config:36 pub trait Config:
44 /// This method is only executable by owner.45 /// This method is only executable by owner.
45 NoPermission,46 NoPermission,
4647
47 /// Contract has no owner.48 /// No pending sponsor for contract.
48 NoContractOwner,49 NoPendingSponsor,
49 }50 }
5051
51 const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);52 const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
68 pub(super) type SelfSponsoring<T: Config> =69 pub(super) type SelfSponsoring<T: Config> =
69 StorageMap<Hasher = Twox128, Key = H160, Value = bool, QueryKind = ValueQuery>;70 StorageMap<Hasher = Twox128, Key = H160, Value = bool, QueryKind = ValueQuery>;
71
72 #[pallet::storage]
73 pub(super) type Sponsoring<T: Config> = StorageMap<
74 Hasher = Twox128,
75 Key = H160,
76 Value = SponsorshipState<T::CrossAccountId>,
77 QueryKind = ValueQuery,
78 >;
7079
71 /// Store for sponsoring mode.80 /// Store for sponsoring mode.
72 /// 81 ///
73 /// ### Usage82 /// ### Usage
74 /// Prefer to delete collection from storage if mode chaged to [`Disabled`](SponsoringModeT::Disabled).83 /// Prefer to delete collection from storage if mode chaged to [`Disabled`](SponsoringModeT::Disabled).
75 /// 84 ///
76 /// * **Key** - contract address.85 /// * **Key** - contract address.
77 /// * **Value** - [`sponsoring mode`](SponsoringModeT).86 /// * **Value** - [`sponsoring mode`](SponsoringModeT).
78 #[pallet::storage]87 #[pallet::storage]
79 pub(super) type SponsoringMode<T: Config> =88 pub(super) type SponsoringMode<T: Config> =
80 StorageMap<Hasher = Twox128, Key = H160, Value = SponsoringModeT, QueryKind = OptionQuery>;89 StorageMap<Hasher = Twox128, Key = H160, Value = SponsoringModeT, QueryKind = OptionQuery>;
8190
82 /// Storage for sponsoring rate limit in blocks.91 /// Storage for sponsoring rate limit in blocks.
83 /// 92 ///
84 /// * **Key** - contract address.93 /// * **Key** - contract address.
85 /// * **Value** - amount of sponsored blocks.94 /// * **Value** - amount of sponsored blocks.
86 #[pallet::storage]95 #[pallet::storage]
103 >;112 >;
104113
105 /// Storege for contracts with [`Allowlisted`](SponsoringModeT::Allowlisted) sponsoring mode.114 /// Storege for contracts with [`Allowlisted`](SponsoringModeT::Allowlisted) sponsoring mode.
106 /// 115 ///
107 /// ### Usage116 /// ### Usage
108 /// Prefer to delete collection from storage if mode chaged to non `Allowlisted`, than set **Value** to **false**.117 /// Prefer to delete collection from storage if mode chaged to non `Allowlisted`, than set **Value** to **false**.
109 /// 118 ///
110 /// * **Key** - contract address.119 /// * **Key** - contract address.
111 /// * **Value** - is contract in [`Allowlisted`](SponsoringModeT::Allowlisted) mode.120 /// * **Value** - is contract in [`Allowlisted`](SponsoringModeT::Allowlisted) mode.
112 #[pallet::storage]121 #[pallet::storage]
113 pub(super) type AllowlistEnabled<T: Config> =122 pub(super) type AllowlistEnabled<T: Config> =
114 StorageMap<Hasher = Twox128, Key = H160, Value = bool, QueryKind = ValueQuery>;123 StorageMap<Hasher = Twox128, Key = H160, Value = bool, QueryKind = ValueQuery>;
115124
125 /// Storage for users that allowed for sponsorship.
126 ///
127 /// ### Usage
128 /// Prefer to delete record from storage if user no more allowed for sponsorship.
129 ///
130 /// * **Key1** - contract address.
131 /// * **Key2** - user that allowed for sponsorship.
132 /// * **Value** - allowance for sponsorship.
116 #[pallet::storage]133 #[pallet::storage]
117 pub(super) type Allowlist<T: Config> = StorageDoubleMap<134 pub(super) type Allowlist<T: Config> = StorageDoubleMap<
118 Hasher1 = Twox128,135 Hasher1 = Twox128,
135 }151 }
136152
137 impl<T: Config> Pallet<T> {153 impl<T: Config> Pallet<T> {
154 pub fn set_sponsor(
155 sender: &T::CrossAccountId,
156 contract: H160,
157 sponsor: &T::CrossAccountId,
158 ) -> DispatchResult {
159 Pallet::<T>::ensure_owner(contract, *sender.as_eth())?;
160 Sponsoring::<T>::insert(
161 contract,
162 SponsorshipState::<T::CrossAccountId>::Unconfirmed(sponsor.clone()),
163 );
164 Ok(())
165 }
166
167 pub fn confirm_sponsorship(sender: &T::CrossAccountId, contract: H160) -> DispatchResult {
168 match Sponsoring::<T>::get(contract) {
169 SponsorshipState::Unconfirmed(sponsor) => {
170 ensure!(sponsor == *sender, Error::<T>::NoPermission);
171 Ok(())
172 }
173 SponsorshipState::Disabled | SponsorshipState::Confirmed(_) => {
174 Err(Error::<T>::NoPendingSponsor.into())
175 }
176 }
177 }
178
179 pub fn get_sponsor(contract: H160) -> Option<T::CrossAccountId> {
180 match Sponsoring::<T>::get(contract) {
181 SponsorshipState::Disabled | SponsorshipState::Unconfirmed(_) => None,
182 SponsorshipState::Confirmed(sponsor) => Some(sponsor),
183 }
184 }
185
138 pub fn sponsoring_mode(contract: H160) -> SponsoringModeT {186 pub fn sponsoring_mode(contract: H160) -> SponsoringModeT {
139 <SponsoringMode<T>>::get(contract)187 <SponsoringMode<T>>::get(contract)