Skip to content
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

Merged
merged 6 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/lib/src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ impl QueryApi {

Ok(self
.state_chain_client
.storage_map_entry::<pallet_cf_funding::BoundAddress<state_chain_runtime::Runtime>>(
.storage_map_entry::<pallet_cf_funding::BoundRedeemAddress<state_chain_runtime::Runtime>>(
block_hash,
&account_id,
)
Expand Down
4 changes: 2 additions & 2 deletions state-chain/pallets/cf-funding/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ benchmarks! {
let caller: T::AccountId = whitelisted_caller();
}:_(RawOrigin::Signed(caller.clone()), Default::default())
verify {
assert!(BoundAddress::<T>::contains_key(&caller));
assert!(BoundRedeemAddress::<T>::contains_key(&caller));
}

update_restricted_addresses {
Expand All @@ -208,7 +208,7 @@ benchmarks! {
let caller: T::AccountId = whitelisted_caller();
}:_(RawOrigin::Signed(caller.clone()), Default::default())
verify {
assert!(ExecutorAddressBinding::<T>::contains_key(&caller));
assert!(BoundExecutorAddress::<T>::contains_key(&caller));
}

impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Test,);
Expand Down
23 changes: 13 additions & 10 deletions state-chain/pallets/cf-funding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ pub mod pallet {

/// Registered addresses for an executor.
#[pallet::storage]
pub type ExecutorAddressBinding<T: Config> =
pub type BoundExecutorAddress<T: Config> =
StorageMap<_, Blake2_128Concat, AccountId<T>, EthereumAddress, OptionQuery>;

/// List of restricted addresses
Expand All @@ -156,7 +156,7 @@ pub mod pallet {

/// Map of bound addresses for accounts.
#[pallet::storage]
pub type BoundAddress<T: Config> =
pub type BoundRedeemAddress<T: Config> =
Copy link
Collaborator

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.

Copy link
Contributor Author

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?

Copy link
Collaborator

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).

StorageMap<_, Blake2_128Concat, AccountId<T>, EthereumAddress>;

/// The fee levied for every redemption request. Can be updated by Governance.
Expand Down Expand Up @@ -298,7 +298,7 @@ pub mod pallet {
StopBiddingDisabled,

/// Wrong executor address
InvalidExecutorAddress,
ExecutorBindingRestrictionViolated,
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 executor for this account is bound to another address.


/// The account is already bound to an executor address.
ExecutorAddressAlreadyBound,
Expand Down Expand Up @@ -377,10 +377,10 @@ pub mod pallet {
) -> DispatchResultWithPostInfo {
let account_id = ensure_signed(origin)?;

if let Some(executor_addr) = ExecutorAddressBinding::<T>::get(&account_id) {
if let Some(executor_addr) = BoundExecutorAddress::<T>::get(&account_id) {
ensure!(
executor_addr == executor.unwrap_or_default(),
Error::<T>::InvalidExecutorAddress
Error::<T>::ExecutorBindingRestrictionViolated
);
}

Expand All @@ -400,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),
Expand Down Expand Up @@ -681,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())
}
Expand Down Expand Up @@ -722,10 +725,10 @@ pub mod pallet {
) -> DispatchResultWithPostInfo {
let account_id = ensure_signed(origin)?;
ensure!(
!ExecutorAddressBinding::<T>::contains_key(&account_id),
!BoundExecutorAddress::<T>::contains_key(&account_id),
Error::<T>::ExecutorAddressAlreadyBound,
);
ExecutorAddressBinding::<T>::insert(account_id.clone(), executor_address);
BoundExecutorAddress::<T>::insert(account_id.clone(), executor_address);
Self::deposit_event(Event::BoundExecutorAddress {
account_id,
address: executor_address,
Expand Down
18 changes: 9 additions & 9 deletions state-chain/pallets/cf-funding/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
mock::*, pallet, ActiveBidder, Error, EthereumAddress, ExecutorAddressBinding,
mock::*, pallet, ActiveBidder, BoundExecutorAddress, Error, EthereumAddress,
PendingRedemptions, RedemptionAmount, RedemptionTax, RestrictedAddresses, RestrictedBalances,
};
use cf_primitives::FlipBalance;
Expand All @@ -10,7 +10,7 @@ use cf_traits::{
};
use sp_core::H160;

use crate::BoundAddress;
use crate::BoundRedeemAddress;
use frame_support::{assert_noop, assert_ok};
use pallet_cf_flip::Bonder;
use sp_runtime::{traits::BadOrigin, DispatchError};
Expand Down Expand Up @@ -862,7 +862,7 @@ fn can_only_redeem_funds_to_bound_address() {
const UNRESTRICTED_ADDRESS: EthereumAddress = H160([0x03; 20]);
const AMOUNT: u128 = 100;
RestrictedAddresses::<Test>::insert(RESTRICTED_ADDRESS_1, ());
BoundAddress::<Test>::insert(ALICE, BOUND_ADDRESS);
BoundRedeemAddress::<Test>::insert(ALICE, BOUND_ADDRESS);
assert_ok!(Funding::funded(
RuntimeOrigin::root(),
ALICE,
Expand Down Expand Up @@ -903,7 +903,7 @@ fn redeem_funds_until_restricted_balance_is_zero_and_then_redeem_to_redeem_addre
const UNRESTRICTED_ADDRESS: EthereumAddress = H160([0x03; 20]);
const AMOUNT: u128 = 100;
RestrictedAddresses::<Test>::insert(RESTRICTED_ADDRESS, ());
BoundAddress::<Test>::insert(ALICE, REDEEM_ADDRESS);
BoundRedeemAddress::<Test>::insert(ALICE, REDEEM_ADDRESS);
assert_ok!(Funding::funded(
RuntimeOrigin::root(),
ALICE,
Expand Down Expand Up @@ -1277,8 +1277,8 @@ fn can_bind_redeem_address() {
address,
}) if address == REDEEM_ADDRESS,
);
assert!(BoundAddress::<Test>::contains_key(ALICE));
assert_eq!(BoundAddress::<Test>::get(ALICE).unwrap(), REDEEM_ADDRESS);
assert!(BoundRedeemAddress::<Test>::contains_key(ALICE));
assert_eq!(BoundRedeemAddress::<Test>::get(ALICE).unwrap(), REDEEM_ADDRESS);
});
}

Expand Down Expand Up @@ -1480,8 +1480,8 @@ fn bind_executor_address() {
address,
}) if address == EXECUTOR_ADDRESS,
);
assert!(ExecutorAddressBinding::<Test>::contains_key(ALICE));
assert_eq!(ExecutorAddressBinding::<Test>::get(ALICE).unwrap(), EXECUTOR_ADDRESS);
assert!(BoundExecutorAddress::<Test>::contains_key(ALICE));
assert_eq!(BoundExecutorAddress::<Test>::get(ALICE).unwrap(), EXECUTOR_ADDRESS);
assert_noop!(
Funding::bind_executor_address(RuntimeOrigin::signed(ALICE), EXECUTOR_ADDRESS),
Error::<Test>::ExecutorAddressAlreadyBound
Expand All @@ -1502,7 +1502,7 @@ fn detect_wrong_executor_address() {
ETH_DUMMY_ADDR,
Some(WRONG_EXECUTOR_ADDRESS)
),
Error::<Test>::InvalidExecutorAddress
Error::<Test>::ExecutorBindingRestrictionViolated
);
});
}
Loading