Skip to content

Commit

Permalink
Merge branch 'main' of github.com:subspace/subspace into fix-er-gap
Browse files Browse the repository at this point in the history
  • Loading branch information
NingLin-P committed Jul 22, 2024
2 parents 2a6e900 + 111f91c commit 4d43993
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 140 deletions.
8 changes: 4 additions & 4 deletions crates/pallet-domains/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ mod benchmarks {

/// Benchmark prune bad ER and slash the submitter based on the number of submitter
#[benchmark]
fn handle_bad_receipt(n: Linear<1, MAX_BUNLDE_PER_BLOCK>) {
fn handle_bad_receipt(n: Linear<1, MAX_BUNDLE_PER_BLOCK>) {
let minimum_nominator_stake = T::MinNominatorStake::get();
let domain_id = register_domain::<T>();
let mut operator_ids = Vec::new();
Expand Down Expand Up @@ -247,8 +247,8 @@ mod benchmarks {
/// in this block
#[benchmark]
fn confirm_domain_block(
n: Linear<1, MAX_BUNLDE_PER_BLOCK>,
s: Linear<0, MAX_BUNLDE_PER_BLOCK>,
n: Linear<1, MAX_BUNDLE_PER_BLOCK>,
s: Linear<0, MAX_BUNDLE_PER_BLOCK>,
) {
let minimum_nominator_stake = T::MinNominatorStake::get();
let operator_rewards =
Expand Down Expand Up @@ -315,7 +315,7 @@ mod benchmarks {
/// Benchmark `operator_take_reward_tax_and_stake` based on the number of operator who has reward
/// in the current epoch
#[benchmark]
fn operator_reward_tax_and_restake(n: Linear<1, MAX_BUNLDE_PER_BLOCK>) {
fn operator_reward_tax_and_restake(n: Linear<1, MAX_BUNDLE_PER_BLOCK>) {
let minimum_nominator_stake = T::MinNominatorStake::get();
let operator_rewards =
T::Currency::minimum_balance().saturating_mul(BalanceOf::<T>::from(1000u32));
Expand Down
62 changes: 31 additions & 31 deletions crates/pallet-domains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
/// and based on the consensus chain slot probability and domain bundle slot probability, usually
/// the value is 6 on average, smaller/bigger value with less probability, we hypocritically use
/// 100 as the maximum number of bundle per block for benchmarking.
const MAX_BUNLDE_PER_BLOCK: u32 = 100;
const MAX_BUNDLE_PER_BLOCK: u32 = 100;

pub(crate) type StateRootOf<T> = <<T as frame_system::Config>::Hashing as Hash>::Output;

Expand Down Expand Up @@ -211,7 +211,7 @@ mod pallet {
use crate::{
BalanceOf, BlockSlot, BlockTreeNodeFor, DomainBlockNumberFor, ElectionVerificationParams,
ExecutionReceiptOf, FraudProofFor, HoldIdentifier, NominatorId, OpaqueBundleOf,
ReceiptHashFor, SingletonReceiptOf, StateRootOf, MAX_BUNLDE_PER_BLOCK, STORAGE_VERSION,
ReceiptHashFor, SingletonReceiptOf, StateRootOf, MAX_BUNDLE_PER_BLOCK, STORAGE_VERSION,
};
#[cfg(not(feature = "std"))]
use alloc::string::String;
Expand Down Expand Up @@ -927,9 +927,20 @@ mod pallet {
operator_id: OperatorId,
domain_id: DomainId,
},
NominatedStakedUnlocked {
operator_id: OperatorId,
nominator_id: NominatorId<T>,
unlocked_amount: BalanceOf<T>,
},
StorageFeeUnlocked {
operator_id: OperatorId,
nominator_id: NominatorId<T>,
storage_fee: BalanceOf<T>,
},
OperatorNominated {
operator_id: OperatorId,
nominator_id: NominatorId<T>,
amount: BalanceOf<T>,
},
DomainInstantiated {
domain_id: DomainId,
Expand All @@ -941,17 +952,13 @@ mod pallet {
OperatorDeregistered {
operator_id: OperatorId,
},
OperatorUnlocked {
operator_id: OperatorId,
},
WithdrewStake {
NominatorUnlocked {
operator_id: OperatorId,
nominator_id: NominatorId<T>,
},
FundsUnlocked {
WithdrewStake {
operator_id: OperatorId,
nominator_id: NominatorId<T>,
amount: BalanceOf<T>,
},
PreferredOperator {
operator_id: OperatorId,
Expand Down Expand Up @@ -1209,7 +1216,7 @@ mod pallet {
#[pallet::call_index(15)]
#[pallet::weight((
T::WeightInfo::submit_fraud_proof().saturating_add(
T::WeightInfo::handle_bad_receipt(MAX_BUNLDE_PER_BLOCK)
T::WeightInfo::handle_bad_receipt(MAX_BUNDLE_PER_BLOCK)
),
DispatchClass::Operational
))]
Expand Down Expand Up @@ -1254,7 +1261,7 @@ mod pallet {
.ok_or::<Error<T>>(FraudProofError::BadReceiptNotFound.into())?;

actual_weight = actual_weight.saturating_add(T::WeightInfo::handle_bad_receipt(
(block_tree_node.operator_ids.len() as u32).min(MAX_BUNLDE_PER_BLOCK),
(block_tree_node.operator_ids.len() as u32).min(MAX_BUNDLE_PER_BLOCK),
));

do_mark_operators_as_slashed::<T>(
Expand Down Expand Up @@ -1374,11 +1381,6 @@ mod pallet {
do_nominate_operator::<T>(operator_id, nominator_id.clone(), amount)
.map_err(Error::<T>::from)?;

Self::deposit_event(Event::OperatorNominated {
operator_id,
nominator_id,
});

Ok(())
}

Expand Down Expand Up @@ -1447,13 +1449,8 @@ mod pallet {
#[pallet::weight(T::WeightInfo::unlock_funds())]
pub fn unlock_funds(origin: OriginFor<T>, operator_id: OperatorId) -> DispatchResult {
let nominator_id = ensure_signed(origin)?;
let unlocked_funds = do_unlock_funds::<T>(operator_id, nominator_id.clone())
do_unlock_funds::<T>(operator_id, nominator_id.clone())
.map_err(crate::pallet::Error::<T>::from)?;
Self::deposit_event(Event::FundsUnlocked {
operator_id,
nominator_id,
amount: unlocked_funds,
});
Ok(())
}

Expand All @@ -1464,10 +1461,13 @@ mod pallet {
pub fn unlock_nominator(origin: OriginFor<T>, operator_id: OperatorId) -> DispatchResult {
let nominator = ensure_signed(origin)?;

do_unlock_nominator::<T>(operator_id, nominator)
do_unlock_nominator::<T>(operator_id, nominator.clone())
.map_err(crate::pallet::Error::<T>::from)?;

Self::deposit_event(Event::OperatorUnlocked { operator_id });
Self::deposit_event(Event::NominatorUnlocked {
operator_id,
nominator_id: nominator,
});

Ok(())
}
Expand Down Expand Up @@ -1621,7 +1621,7 @@ mod pallet {
.ok_or::<Error<T>>(FraudProofError::BadReceiptNotFound.into())?;

actual_weight = actual_weight.saturating_add(T::WeightInfo::handle_bad_receipt(
(block_tree_node.operator_ids.len() as u32).min(MAX_BUNLDE_PER_BLOCK),
(block_tree_node.operator_ids.len() as u32).min(MAX_BUNDLE_PER_BLOCK),
));

do_mark_operators_as_slashed::<T>(
Expand Down Expand Up @@ -2731,11 +2731,11 @@ impl<T: Config> Pallet<T> {
// NOTE: within `submit_bundle`, only one of (or none) `handle_bad_receipt` and
// `confirm_domain_block` can happen, thus we use the `max` of them

// We use `MAX_BUNLDE_PER_BLOCK` number to assume the number of slashed operators.
// We use `MAX_BUNDLE_PER_BLOCK` number to assume the number of slashed operators.
// We do not expect so many operators to be slashed but nontheless, if it did happen
// we will limit the weight to 100 operators.
T::WeightInfo::handle_bad_receipt(MAX_BUNLDE_PER_BLOCK).max(
T::WeightInfo::confirm_domain_block(MAX_BUNLDE_PER_BLOCK, MAX_BUNLDE_PER_BLOCK),
T::WeightInfo::handle_bad_receipt(MAX_BUNDLE_PER_BLOCK).max(
T::WeightInfo::confirm_domain_block(MAX_BUNDLE_PER_BLOCK, MAX_BUNDLE_PER_BLOCK),
),
)
.saturating_add(Self::max_staking_epoch_transition())
Expand All @@ -2745,22 +2745,22 @@ impl<T: Config> Pallet<T> {
pub fn max_submit_receipt_weight() -> Weight {
T::WeightInfo::submit_bundle()
.saturating_add(
// We use `MAX_BUNLDE_PER_BLOCK` number to assume the number of slashed operators.
// We use `MAX_BUNDLE_PER_BLOCK` number to assume the number of slashed operators.
// We do not expect so many operators to be slashed but nontheless, if it did happen
// we will limit the weight to 100 operators.
T::WeightInfo::handle_bad_receipt(MAX_BUNLDE_PER_BLOCK),
T::WeightInfo::handle_bad_receipt(MAX_BUNDLE_PER_BLOCK),
)
.saturating_add(T::WeightInfo::slash_operator(MAX_NOMINATORS_TO_SLASH))
}

pub fn max_staking_epoch_transition() -> Weight {
T::WeightInfo::operator_reward_tax_and_restake(MAX_BUNLDE_PER_BLOCK).saturating_add(
T::WeightInfo::operator_reward_tax_and_restake(MAX_BUNDLE_PER_BLOCK).saturating_add(
T::WeightInfo::finalize_domain_epoch_staking(T::MaxPendingStakingOperation::get()),
)
}

pub fn max_prune_domain_execution_receipt() -> Weight {
T::WeightInfo::handle_bad_receipt(MAX_BUNLDE_PER_BLOCK)
T::WeightInfo::handle_bad_receipt(MAX_BUNDLE_PER_BLOCK)
.saturating_add(T::DbWeight::get().reads_writes(3, 1))
}

Expand Down
38 changes: 34 additions & 4 deletions crates/pallet-domains/src/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,11 @@ pub(crate) fn do_nominate_operator<T: Config>(
.map_err(Error::BundleStorageFund)?;

hold_deposit::<T>(&nominator_id, operator_id, new_deposit.staking)?;
Pallet::<T>::deposit_event(Event::OperatorNominated {
operator_id,
nominator_id: nominator_id.clone(),
amount: new_deposit.staking,
});

// increment total deposit for operator pool within this epoch
operator.deposits_in_epoch = operator
Expand Down Expand Up @@ -910,7 +915,7 @@ pub(crate) fn do_withdraw_stake<T: Config>(
pub(crate) fn do_unlock_funds<T: Config>(
operator_id: OperatorId,
nominator_id: NominatorId<T>,
) -> Result<BalanceOf<T>, Error> {
) -> Result<(), Error> {
let operator = Operators::<T>::get(operator_id).ok_or(Error::UnknownOperator)?;
ensure!(
*operator.status::<T>(operator_id) == OperatorStatus::Registered,
Expand Down Expand Up @@ -966,6 +971,12 @@ pub(crate) fn do_unlock_funds<T: Config>(
)
.map_err(|_| Error::RemoveLock)?;

Pallet::<T>::deposit_event(Event::NominatedStakedUnlocked {
operator_id,
nominator_id: nominator_id.clone(),
unlocked_amount: amount_to_unlock,
});

// Release storage fund
let storage_fund_hold_id = T::HoldIdentifier::storage_fund_withdrawal(operator_id);
T::Currency::release(
Expand All @@ -976,6 +987,12 @@ pub(crate) fn do_unlock_funds<T: Config>(
)
.map_err(|_| Error::RemoveLock)?;

Pallet::<T>::deposit_event(Event::StorageFeeUnlocked {
operator_id,
nominator_id: nominator_id.clone(),
storage_fee: storage_fee_refund,
});

// if there are no withdrawals, then delete the storage as well
if withdrawal.withdrawals.is_empty() && withdrawal.withdrawal_in_shares.is_none() {
*maybe_withdrawal = None;
Expand All @@ -990,7 +1007,7 @@ pub(crate) fn do_unlock_funds<T: Config>(
});
}

Ok(amount_to_unlock)
Ok(())
})
}

Expand Down Expand Up @@ -1101,6 +1118,12 @@ pub(crate) fn do_unlock_nominator<T: Config>(
)
.map_err(|_| Error::RemoveLock)?;

Pallet::<T>::deposit_event(Event::NominatedStakedUnlocked {
operator_id,
nominator_id: nominator_id.clone(),
unlocked_amount: total_amount_to_unlock,
});

total_stake = total_stake.saturating_sub(nominator_staked_amount);
total_shares = total_shares.saturating_sub(nominator_shares);

Expand All @@ -1120,8 +1143,15 @@ pub(crate) fn do_unlock_nominator<T: Config>(
.map_err(Error::BundleStorageFund)?;

// Release all storage fee that of the nominator.
T::Currency::release_all(&storage_fund_hold_id, &nominator_id, Precision::Exact)
.map_err(|_| Error::RemoveLock)?;
let storage_fee_refund =
T::Currency::release_all(&storage_fund_hold_id, &nominator_id, Precision::Exact)
.map_err(|_| Error::RemoveLock)?;

Pallet::<T>::deposit_event(Event::StorageFeeUnlocked {
operator_id,
nominator_id: nominator_id.clone(),
storage_fee: storage_fee_refund,
});

// reduce total storage fee deposit with nominator total fee deposit
total_storage_fee_deposit =
Expand Down
5 changes: 4 additions & 1 deletion crates/sc-consensus-subspace/src/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,9 @@ where
);

let best_number = self.client.info().best_number;
// Reject block below archiving point, but only if we received it from the network
if *block.header.number() + self.chain_constants.confirmation_depth_k().into() < best_number
&& matches!(block.origin, BlockOrigin::NetworkBroadcast)
{
debug!(
header = ?block.header,
Expand Down Expand Up @@ -579,7 +581,8 @@ where
} = checked_header;

let slot = pre_digest.slot();
// Estimate what the "current" slot is according to sync target since we don't have other way to know it
// Estimate what the "current" slot is according to sync target since we don't have other
// way to know it
let diff_in_blocks = self
.sync_target_block_number
.load(Ordering::Relaxed)
Expand Down
Loading

0 comments on commit 4d43993

Please sign in to comment.