-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: [PRO-823] bind-nodes-executor-to-address #3987
Changes from 4 commits
cb7fdb8
56786f4
cafde7b
de1bae1
8b13e03
91f331c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -134,6 +134,11 @@ pub mod pallet { | |
#[pallet::storage] | ||
pub type RedemptionTTLSeconds<T: Config> = StorageValue<_, u64, ValueQuery>; | ||
|
||
/// Registered addresses for an executor. | ||
#[pallet::storage] | ||
pub type BoundExecutorAddress<T: Config> = | ||
StorageMap<_, Blake2_128Concat, AccountId<T>, EthereumAddress, OptionQuery>; | ||
|
||
/// List of restricted addresses | ||
#[pallet::storage] | ||
pub type RestrictedAddresses<T: Config> = | ||
|
@@ -151,7 +156,7 @@ pub mod pallet { | |
|
||
/// Map of bound addresses for accounts. | ||
#[pallet::storage] | ||
pub type BoundAddress<T: Config> = | ||
pub type BoundRedeemAddress<T: Config> = | ||
StorageMap<_, Blake2_128Concat, AccountId<T>, EthereumAddress>; | ||
|
||
/// The fee levied for every redemption request. Can be updated by Governance. | ||
|
@@ -233,6 +238,9 @@ pub mod pallet { | |
|
||
/// An account has been bound to an address. | ||
BoundRedeemAddress { account_id: AccountId<T>, address: EthereumAddress }, | ||
|
||
/// An account has been bound to an executor address. | ||
BoundExecutorAddress { account_id: AccountId<T>, address: EthereumAddress }, | ||
} | ||
|
||
#[pallet::error] | ||
|
@@ -288,6 +296,12 @@ pub mod pallet { | |
|
||
/// Stop Bidding is disabled due to Safe Mode. | ||
StopBiddingDisabled, | ||
|
||
/// Wrong executor address | ||
ExecutorBindingRestrictionViolated, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: please update the comment too - it's included in the type metadata and displayed to users, it should be useful. Something like |
||
|
||
/// The account is already bound to an executor address. | ||
ExecutorAddressAlreadyBound, | ||
} | ||
|
||
#[pallet::call] | ||
|
@@ -363,6 +377,13 @@ pub mod pallet { | |
) -> DispatchResultWithPostInfo { | ||
let account_id = ensure_signed(origin)?; | ||
|
||
if let Some(executor_addr) = BoundExecutorAddress::<T>::get(&account_id) { | ||
ensure!( | ||
executor_addr == executor.unwrap_or_default(), | ||
Error::<T>::ExecutorBindingRestrictionViolated | ||
); | ||
} | ||
|
||
ensure!(T::SafeMode::get().redeem_enabled, Error::<T>::RedeemDisabled); | ||
|
||
// Not allowed to redeem if we are an active bidder in the auction phase | ||
|
@@ -379,7 +400,7 @@ pub mod pallet { | |
let mut restricted_balances = RestrictedBalances::<T>::get(&account_id); | ||
let redemption_fee = RedemptionTax::<T>::get(); | ||
|
||
if let Some(bound_address) = BoundAddress::<T>::get(&account_id) { | ||
if let Some(bound_address) = BoundRedeemAddress::<T>::get(&account_id) { | ||
ensure!( | ||
bound_address == address || | ||
restricted_balances.keys().any(|res_address| res_address == &address), | ||
|
@@ -621,7 +642,7 @@ pub mod pallet { | |
/// | ||
/// - [BadOrigin](frame_support::error::BadOrigin) | ||
#[pallet::call_index(7)] | ||
#[pallet::weight(T::WeightInfo::update_restricted_addresses(addresses_to_add.len() as u32, addresses_to_remove.len() as u32))] | ||
#[pallet::weight(T::WeightInfo::update_restricted_addresses(addresses_to_add.len() as u32, addresses_to_remove.len() as u32, 10_u32))] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That seems to be a little odd, but I don't think that there is really a better way to do it. We can not access the real amount of RestrictedAddresses without decoding the storage, which would lead to an open door for DDOS attacks (I think). I think the trick is to benchmark it correctly and then pick a number that is higher than the maximum number of RestrictedAddresses for an account but not extraordinarily high to waste unnecessary transaction fees. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For now, I just picked 10 🤷♂️ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, sounds reasonable. |
||
pub fn update_restricted_addresses( | ||
origin: OriginFor<T>, | ||
addresses_to_add: Vec<EthereumAddress>, | ||
|
@@ -660,8 +681,11 @@ pub mod pallet { | |
address: EthereumAddress, | ||
) -> DispatchResultWithPostInfo { | ||
let account_id = ensure_signed(origin)?; | ||
ensure!(!BoundAddress::<T>::contains_key(&account_id), Error::<T>::AccountAlreadyBound); | ||
BoundAddress::<T>::insert(&account_id, address); | ||
ensure!( | ||
!BoundRedeemAddress::<T>::contains_key(&account_id), | ||
Error::<T>::AccountAlreadyBound | ||
); | ||
BoundRedeemAddress::<T>::insert(&account_id, address); | ||
Self::deposit_event(Event::BoundRedeemAddress { account_id, address }); | ||
Ok(().into()) | ||
} | ||
|
@@ -682,6 +706,35 @@ pub mod pallet { | |
Self::deposit_event(Event::<T>::RedemptionTaxAmountUpdated { amount }); | ||
Ok(()) | ||
} | ||
|
||
/// Binds executor address to an account. | ||
/// | ||
/// ## Events | ||
/// | ||
/// - [BoundExecutorAddress](Event::BoundExecutorAddress) | ||
/// | ||
/// ## Errors | ||
/// | ||
/// - [ExecutorAddressAlreadyBound](Error::ExecutorAddressAlreadyBound) | ||
/// - [BadOrigin](frame_support::error::BadOrigin) | ||
#[pallet::call_index(10)] | ||
#[pallet::weight(T::WeightInfo::bind_executor_address())] | ||
pub fn bind_executor_address( | ||
origin: OriginFor<T>, | ||
executor_address: EthereumAddress, | ||
) -> DispatchResultWithPostInfo { | ||
let account_id = ensure_signed(origin)?; | ||
ensure!( | ||
!BoundExecutorAddress::<T>::contains_key(&account_id), | ||
Error::<T>::ExecutorAddressAlreadyBound, | ||
); | ||
BoundExecutorAddress::<T>::insert(account_id.clone(), executor_address); | ||
Self::deposit_event(Event::BoundExecutorAddress { | ||
account_id, | ||
address: executor_address, | ||
}); | ||
Ok(().into()) | ||
} | ||
} | ||
|
||
#[pallet::genesis_config] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately this will require a storage migration (if it's not empty) 🙈
I don't mind leaving it out until we release - right now the storage is empty on perseverance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mhhh.... Well that's bad :D. But I think we will release from scratch anyway and if it's empty on perseverance I don't think it should be an issue or?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes that's what I mean - no need to migrate unless we use this in perseverance (and I don't think we will).