Skip to content

Removal of registration class logic #9

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

Merged
merged 2 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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 node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ fn testnet_genesis(
},
"network": {
"subnetPath": subnet_path,
"memoryMb": 10000,
"memoryMb": 2000,
"subnetNodes": endowed_accounts.iter().cloned().map(|k| {
peer_index += 1;
(
Expand Down
23 changes: 11 additions & 12 deletions pallets/network/src/delegate_staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,30 +178,29 @@ impl<T: Config> Pallet<T> {
) -> DispatchResult {
let account_id: T::AccountId = ensure_signed(origin)?;

let block: u64 = Self::get_current_block_as_u64();
ensure!(
block - LastDelegateStakeTransfer::<T>::get(account_id.clone()) > DelegateStakeTransferPeriod::<T>::get(),
Error::<T>::DelegateStakeTransferPeriodExceeded
);

LastDelegateStakeTransfer::<T>::insert(account_id.clone(), block);

// --- Remove

// --- Ensure that the delegate_stake amount to be removed is above zero.
ensure!(
delegate_stake_shares_to_be_switched > 0,
Error::<T>::NotEnoughStakeToWithdraw
);

let from_account_delegate_stake_shares: u128 = AccountSubnetDelegateStakeShares::<T>::get(&account_id.clone(), from_subnet_id);

// --- Ensure that the account has enough delegate_stake to withdraw.
ensure!(
from_account_delegate_stake_shares >= delegate_stake_shares_to_be_switched,
Error::<T>::NotEnoughStakeToWithdraw
);


let block: u64 = Self::get_current_block_as_u64();

// --- Logic
ensure!(
block - LastDelegateStakeTransfer::<T>::get(account_id.clone()) > DelegateStakeTransferPeriod::<T>::get(),
Error::<T>::DelegateStakeTransferPeriodExceeded
);

LastDelegateStakeTransfer::<T>::insert(account_id.clone(), block);

let total_from_subnet_delegated_stake_shares = TotalSubnetDelegateStakeShares::<T>::get(from_subnet_id);
let total_from_subnet_delegated_stake_balance = TotalSubnetDelegateStakeBalance::<T>::get(from_subnet_id);

Expand Down
19 changes: 1 addition & 18 deletions pallets/network/src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,6 @@ impl<T: Config> Pallet<T> {
Self::get_classified_subnet_nodes(subnet_id, &SubnetNodeClass::Included, epoch)
}

// pub fn get_subnet_nodes_submittable(
// subnet_id: u32,
// ) -> Vec<SubnetNode<T::AccountId>> {
// if !SubnetsData::<T>::contains_key(subnet_id) {
// return Vec::new();
// }

// // let node_sets: BTreeMap<T::AccountId, u64> = SubnetNodesClasses::<T>::get(subnet_id, SubnetNodeClass::Submittable);

// let subnet_nodes: Vec<T::AccountId> = SubnetNodesClasses::<T>::get(subnet_id, SubnetNodeClass::Submittable).iter()
// .map(|x| {
// *x.0
// } )
// .collect();

// subnet_nodes
// }

pub fn get_subnet_nodes_submittable(
subnet_id: u32,
) -> Vec<SubnetNode<T::AccountId>> {
Expand Down Expand Up @@ -128,6 +110,7 @@ impl<T: Config> Pallet<T> {
}
}

// TODO: Make this only return true is Submittable subnet node
pub fn is_subnet_node_by_peer_id(subnet_id: u32, peer_id: Vec<u8>) -> bool {
match SubnetNodeAccount::<T>::try_get(subnet_id, PeerId(peer_id)) {
Ok(account_id) => true,
Expand Down
21 changes: 12 additions & 9 deletions pallets/network/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ pub mod pallet {
InvalidSubnetId,

DelegateStakeTransferPeriodExceeded,

MustUnstakeToRegister,
// Admin
/// Consensus block epoch_length invalid, must reach minimum
InvalidEpochLengthsInterval,
Expand Down Expand Up @@ -739,7 +739,6 @@ pub mod pallet {
pub block: u64,
pub epoch: u32,
pub data: Vec<AccountantDataNodeParams>,
// pub attests: Attests<AccountId>,
}

#[derive(Default, Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, scale_info::TypeInfo)]
Expand Down Expand Up @@ -1000,8 +999,11 @@ pub mod pallet {
// 9 days at 6s blocks
// 129_600

// Testnet 1 day
14400
// Testnet 1 hour
// 600

// Local 24 blocks
50
}
#[pallet::type_value]
pub fn DefaultMaxSubnetRegistrationBlocks() -> u64 {
Expand Down Expand Up @@ -1786,9 +1788,6 @@ pub mod pallet {
// 3. Subnet has less than required minimum delegate stake balance
// ----

let min_required_subnet_consensus_submit_epochs = MinRequiredSubnetConsensusSubmitEpochs::<T>::get();
let block: u64 = Self::get_current_block_as_u64();

// --- Ensure the subnet has passed it's required period to begin consensus submissions
ensure!(
subnet.activated != 0,
Expand Down Expand Up @@ -1889,7 +1888,6 @@ pub mod pallet {
)
}

/// Update a subnet peer
#[pallet::call_index(6)]
#[pallet::weight({0})]
pub fn deactivate_subnet_node(
Expand Down Expand Up @@ -2632,7 +2630,7 @@ pub mod pallet {
// If a subnet node deregisters, then they must fully unstake its stake balance to register again using that same balance
ensure!(
AccountSubnetStake::<T>::get(account_id.clone(), subnet_id) == 0,
Error::<T>::InvalidSubnetRegistrationCooldown
Error::<T>::MustUnstakeToRegister
);

// ====================
Expand Down Expand Up @@ -2718,6 +2716,11 @@ pub mod pallet {
Err(()) => return Err(Error::<T>::SubnetNotExist.into()),
};

// ensure!(
// SubnetNodesData::<T>::contains_key(subnet_id, account_id.clone()),
// Error::<T>::SubnetNotExist
// );

// if subnet.activated == 0 {
// // --- Subnet nodes can only activate if within registration period or if it's activated
// // --- Ensure the subnet outside of the enactment period or still registering
Expand Down
2 changes: 1 addition & 1 deletion pallets/network/src/rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ impl<T: Config> Pallet<T> {
// Always continue if any of these are true
// Note: Only ``included`` or above nodes can get emissions
if subnet_node.classification.class == SubnetNodeClass::Registered {
if subnet_node.classification.start_epoch.saturating_add(subnet_node_registration_epochs) > epoch as u64 {
if epoch as u64 > subnet_node.classification.start_epoch.saturating_add(subnet_node_registration_epochs) {
Self::perform_remove_subnet_node(block, subnet_id, account_id);
}
continue
Expand Down
89 changes: 89 additions & 0 deletions pallets/network/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1809,6 +1809,95 @@ fn test_register_subnet_node_subnet_registering_or_activated_error() {
})
}

#[test]
fn test_register_subnet_node_then_activate() {
new_test_ext().execute_with(|| {

let deposit_amount: u128 = 10000000000000000000000;
let amount: u128 = 1000000000000000000000;

let cost = Network::get_subnet_initialization_cost(0);
let _ = Balances::deposit_creating(&account(0), cost+deposit_amount);

let subnet_path: Vec<u8> = "petals-team/StableBeluga2".into();

let registration_blocks = MinSubnetRegistrationBlocks::<Test>::get();

let add_subnet_data = RegistrationSubnetData {
path: subnet_path.clone().into(),
memory_mb: DEFAULT_MEM_MB,
registration_blocks: registration_blocks,
};

// --- Register subnet for activation
assert_ok!(
Network::register_subnet(
RuntimeOrigin::signed(account(0)),
add_subnet_data,
)
);

let subnet_id = SubnetPaths::<Test>::get(subnet_path.clone()).unwrap();
let subnet = SubnetsData::<Test>::get(subnet_id).unwrap();

assert_ok!(
Network::register_subnet_node(
RuntimeOrigin::signed(account(0)),
subnet_id,
peer(0),
amount,
None,
None,
None,
),
);

assert_ok!(
Network::activate_subnet_node(
RuntimeOrigin::signed(account(0)),
subnet_id,
),
);
})
}

#[test]
fn test_activate_subnet_then_register_subnet_node_then_activate() {
new_test_ext().execute_with(|| {
let subnet_path: Vec<u8> = "petals-team/StableBeluga2".into();

let deposit_amount: u128 = 10000000000000000000000;
let amount: u128 = 1000000000000000000000;

build_activated_subnet(subnet_path.clone(), 0, 0, deposit_amount, amount);

let subnet_id = SubnetPaths::<Test>::get(subnet_path.clone()).unwrap();
let total_subnet_nodes = TotalSubnetNodes::<Test>::get(subnet_id);
let n_account = total_subnet_nodes + 1;

let _ = Balances::deposit_creating(&account(n_account), deposit_amount);

assert_ok!(
Network::register_subnet_node(
RuntimeOrigin::signed(account(n_account)),
subnet_id,
peer(n_account),
amount,
None,
None,
None,
),
);

assert_ok!(
Network::activate_subnet_node(
RuntimeOrigin::signed(account(n_account)),
subnet_id,
),
);
})
}

#[test]
fn test_activate_subnet_node_subnet_registering_or_activated_error() {
new_test_ext().execute_with(|| {
Expand Down
3 changes: 1 addition & 2 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ impl pallet_collective::Config<CouncilCollective> for Runtime {

parameter_types! {
pub const InitialTxRateLimit: u64 = 0;
pub const EpochLength: u64 = 690; // Testnet 690 blocks per erpoch / 69 mins per epoch
pub const EpochLength: u64 = 10; // Testnet 690 blocks per erpoch / 69 mins per epoch, Local 10
pub const NetworkPalletId: PalletId = PalletId(*b"/network");
pub const SubnetInitializationCost: u128 = 100_000_000_000_000_000_000;
pub const MinProposalStake: u128 = 1_000_000_000_000_000_000; // 1 * 1e18
Expand Down Expand Up @@ -572,7 +572,6 @@ impl pallet_admin::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type CollectiveOrigin = pallet_collective::EnsureProportionAtLeast<AccountId, CouncilCollective, 2, 3>;
type NetworkAdminInterface = Network;
// type SubnetDemocracyAdminInterface = SubnetDemocracy;
}

impl pallet_atomic_swap::Config for Runtime {
Expand Down
Loading