Skip to content

Commit

Permalink
Merge pull request #1846 from subspace/staking/fix_epoch_transition
Browse files Browse the repository at this point in the history
Domains: do epoch transition for the first operator registration and bump runtime version
  • Loading branch information
vedhavyas authored Aug 19, 2023
2 parents ed5d284 + 8c914ac commit 017e026
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 14 deletions.
25 changes: 23 additions & 2 deletions crates/pallet-domains/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -855,13 +855,22 @@ mod pallet {
) -> DispatchResult {
let owner = ensure_signed(origin)?;

let operator_id = do_register_operator::<T>(owner, domain_id, amount, config)
.map_err(Error::<T>::from)?;
let (operator_id, current_epoch_index) =
do_register_operator::<T>(owner, domain_id, amount, config)
.map_err(Error::<T>::from)?;

Self::deposit_event(Event::OperatorRegistered {
operator_id,
domain_id,
});

// if the domain's current epoch is 0,
// then do an epoch transition so that operator can start producing bundles
if current_epoch_index.is_zero() {
do_finalize_domain_current_epoch::<T>(domain_id, One::one())
.map_err(Error::<T>::from)?;
}

Ok(())
}

Expand Down Expand Up @@ -1067,6 +1076,18 @@ mod pallet {
let _ = LastEpochStakingDistribution::<T>::clear(u32::MAX, None);
Self::update_domain_tx_range();
}

// TODO: remove once the migration is done
fn on_runtime_upgrade() -> Weight {
for (domain_id, stake_summary) in DomainStakingSummary::<T>::iter() {
if stake_summary.current_epoch_index.is_zero() {
if let Err(err) = do_finalize_domain_current_epoch::<T>(domain_id, One::one()) {
log::error!(target: "runtime::domains", "Failed to do epoch transition for {domain_id:?}: {err:?}");
}
}
}
Weight::zero()
}
}

/// Constructs a `TransactionValidity` with pallet-executor specific defaults.
Expand Down
13 changes: 4 additions & 9 deletions crates/pallet-domains/src/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ pub(crate) fn do_register_operator<T: Config>(
domain_id: DomainId,
amount: BalanceOf<T>,
config: OperatorConfig<BalanceOf<T>>,
) -> Result<OperatorId, Error> {
) -> Result<(OperatorId, EpochIndex), Error> {
note_pending_staking_operation::<T>(domain_id)?;

DomainStakingSummary::<T>::try_mutate(domain_id, |maybe_domain_stake_summary| {
Expand Down Expand Up @@ -173,7 +173,7 @@ pub(crate) fn do_register_operator<T: Config>(
// update pending transfers
PendingDeposits::<T>::insert(operator_id, operator_owner, amount);

Ok(operator_id)
Ok((operator_id, domain_stake_summary.current_epoch_index))
})
}

Expand Down Expand Up @@ -703,15 +703,12 @@ pub(crate) mod tests {
next_domain_id: domain_id,
minimum_nominator_stake: 0,
nomination_tax: Default::default(),
current_total_stake: 0,
current_total_stake: operator_stake,
current_epoch_rewards: 0,
total_shares: 0,
total_shares: operator_stake,
status: OperatorStatus::Registered,
}
);
let pending_deposit =
PendingDeposits::<Test>::get(operator_id, operator_account).unwrap();
assert_eq!(pending_deposit, operator_stake);

let stake_summary = DomainStakingSummary::<Test>::get(domain_id).unwrap();
assert!(stake_summary.next_operators.contains(&operator_id));
Expand Down Expand Up @@ -762,8 +759,6 @@ pub(crate) mod tests {
)]),
);

let pending_deposit = PendingDeposits::<Test>::get(0, operator_account).unwrap();
assert_eq!(pending_deposit, operator_stake);
let pending_deposit = PendingDeposits::<Test>::get(0, nominator_account).unwrap();
assert_eq!(pending_deposit, nominator_stake);

Expand Down
4 changes: 2 additions & 2 deletions crates/pallet-domains/src/staking_epoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1065,8 +1065,8 @@ mod tests {
domain_stake_summary.current_total_stake,
total_updated_stake
);
// epoch should be 2 since we did 3 epoch transitions
assert_eq!(domain_stake_summary.current_epoch_index, 2);
// epoch should be 3 since we did 3 epoch transitions
assert_eq!(domain_stake_summary.current_epoch_index, 3);
});
}

Expand Down
2 changes: 1 addition & 1 deletion crates/subspace-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("subspace"),
impl_name: create_runtime_str!("subspace"),
authoring_version: 0,
spec_version: 1,
spec_version: 2,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 0,
Expand Down

0 comments on commit 017e026

Please sign in to comment.