diff --git a/node/src/chain_spec.rs b/node/src/chain_spec.rs index 3272392..4d0a755 100644 --- a/node/src/chain_spec.rs +++ b/node/src/chain_spec.rs @@ -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; ( diff --git a/pallets/network/src/delegate_staking.rs b/pallets/network/src/delegate_staking.rs index 6f120ff..8005c2d 100644 --- a/pallets/network/src/delegate_staking.rs +++ b/pallets/network/src/delegate_staking.rs @@ -178,22 +178,11 @@ impl Pallet { ) -> DispatchResult { let account_id: T::AccountId = ensure_signed(origin)?; - let block: u64 = Self::get_current_block_as_u64(); - ensure!( - block - LastDelegateStakeTransfer::::get(account_id.clone()) > DelegateStakeTransferPeriod::::get(), - Error::::DelegateStakeTransferPeriodExceeded - ); - - LastDelegateStakeTransfer::::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::::NotEnoughStakeToWithdraw ); - let from_account_delegate_stake_shares: u128 = AccountSubnetDelegateStakeShares::::get(&account_id.clone(), from_subnet_id); // --- Ensure that the account has enough delegate_stake to withdraw. @@ -201,7 +190,17 @@ impl Pallet { from_account_delegate_stake_shares >= delegate_stake_shares_to_be_switched, Error::::NotEnoughStakeToWithdraw ); - + + let block: u64 = Self::get_current_block_as_u64(); + + // --- Logic + ensure!( + block - LastDelegateStakeTransfer::::get(account_id.clone()) > DelegateStakeTransferPeriod::::get(), + Error::::DelegateStakeTransferPeriodExceeded + ); + + LastDelegateStakeTransfer::::insert(account_id.clone(), block); + let total_from_subnet_delegated_stake_shares = TotalSubnetDelegateStakeShares::::get(from_subnet_id); let total_from_subnet_delegated_stake_balance = TotalSubnetDelegateStakeBalance::::get(from_subnet_id); diff --git a/pallets/network/src/info.rs b/pallets/network/src/info.rs index 9967d00..3bb2b28 100644 --- a/pallets/network/src/info.rs +++ b/pallets/network/src/info.rs @@ -25,24 +25,6 @@ impl Pallet { Self::get_classified_subnet_nodes(subnet_id, &SubnetNodeClass::Included, epoch) } - // pub fn get_subnet_nodes_submittable( - // subnet_id: u32, - // ) -> Vec> { - // if !SubnetsData::::contains_key(subnet_id) { - // return Vec::new(); - // } - - // // let node_sets: BTreeMap = SubnetNodesClasses::::get(subnet_id, SubnetNodeClass::Submittable); - - // let subnet_nodes: Vec = SubnetNodesClasses::::get(subnet_id, SubnetNodeClass::Submittable).iter() - // .map(|x| { - // *x.0 - // } ) - // .collect(); - - // subnet_nodes - // } - pub fn get_subnet_nodes_submittable( subnet_id: u32, ) -> Vec> { @@ -128,6 +110,7 @@ impl Pallet { } } + // TODO: Make this only return true is Submittable subnet node pub fn is_subnet_node_by_peer_id(subnet_id: u32, peer_id: Vec) -> bool { match SubnetNodeAccount::::try_get(subnet_id, PeerId(peer_id)) { Ok(account_id) => true, diff --git a/pallets/network/src/lib.rs b/pallets/network/src/lib.rs index d4e4ec0..090d702 100644 --- a/pallets/network/src/lib.rs +++ b/pallets/network/src/lib.rs @@ -310,7 +310,7 @@ pub mod pallet { InvalidSubnetId, DelegateStakeTransferPeriodExceeded, - + MustUnstakeToRegister, // Admin /// Consensus block epoch_length invalid, must reach minimum InvalidEpochLengthsInterval, @@ -739,7 +739,6 @@ pub mod pallet { pub block: u64, pub epoch: u32, pub data: Vec, - // pub attests: Attests, } #[derive(Default, Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, scale_info::TypeInfo)] @@ -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 { @@ -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::::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, @@ -1889,7 +1888,6 @@ pub mod pallet { ) } - /// Update a subnet peer #[pallet::call_index(6)] #[pallet::weight({0})] pub fn deactivate_subnet_node( @@ -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::::get(account_id.clone(), subnet_id) == 0, - Error::::InvalidSubnetRegistrationCooldown + Error::::MustUnstakeToRegister ); // ==================== @@ -2718,6 +2716,11 @@ pub mod pallet { Err(()) => return Err(Error::::SubnetNotExist.into()), }; + // ensure!( + // SubnetNodesData::::contains_key(subnet_id, account_id.clone()), + // Error::::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 diff --git a/pallets/network/src/rewards.rs b/pallets/network/src/rewards.rs index d6d4eb0..c643713 100644 --- a/pallets/network/src/rewards.rs +++ b/pallets/network/src/rewards.rs @@ -135,7 +135,7 @@ impl Pallet { // 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 diff --git a/pallets/network/src/tests.rs b/pallets/network/src/tests.rs index 229843f..1334627 100644 --- a/pallets/network/src/tests.rs +++ b/pallets/network/src/tests.rs @@ -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 = "petals-team/StableBeluga2".into(); + + let registration_blocks = MinSubnetRegistrationBlocks::::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::::get(subnet_path.clone()).unwrap(); + let subnet = SubnetsData::::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 = "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::::get(subnet_path.clone()).unwrap(); + let total_subnet_nodes = TotalSubnetNodes::::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(|| { diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index e5fa44f..c2a3afb 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -471,7 +471,7 @@ impl pallet_collective::Config 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 @@ -572,7 +572,6 @@ impl pallet_admin::Config for Runtime { type RuntimeEvent = RuntimeEvent; type CollectiveOrigin = pallet_collective::EnsureProportionAtLeast; type NetworkAdminInterface = Network; - // type SubnetDemocracyAdminInterface = SubnetDemocracy; } impl pallet_atomic_swap::Config for Runtime {