From b9318a0837ddf852e6d65f5a3bb1fe2dd145ba05 Mon Sep 17 00:00:00 2001 From: Yahor Tsaryk Date: Thu, 21 Dec 2023 18:56:01 +0100 Subject: [PATCH] chore: PR #211 is backported --- node/service/chain-specs/example.json | 2 + pallets/ddc-clusters/src/testing_utils.rs | 4 +- pallets/ddc-clusters/src/tests.rs | 110 +++++++++++++++++++++- pallets/ddc-nodes/src/benchmarking.rs | 4 +- pallets/ddc-nodes/src/lib.rs | 1 + pallets/ddc-nodes/src/node.rs | 2 + pallets/ddc-nodes/src/storage_node.rs | 15 ++- pallets/ddc-nodes/src/testing_utils.rs | 8 +- pallets/ddc-nodes/src/tests.rs | 102 +++++++++++++++++++- pallets/ddc-staking/src/benchmarking.rs | 4 +- pallets/ddc-staking/src/testing_utils.rs | 8 +- primitives/src/lib.rs | 2 + 12 files changed, 245 insertions(+), 17 deletions(-) diff --git a/node/service/chain-specs/example.json b/node/service/chain-specs/example.json index 961ae7795..5ad984508 100644 --- a/node/service/chain-specs/example.json +++ b/node/service/chain-specs/example.json @@ -187,6 +187,8 @@ 46, 49 ], + "domain": [], + "ssl": false, "http_port": 8080, "grpc_port": 8081, "p2p_port": 8082, diff --git a/pallets/ddc-clusters/src/testing_utils.rs b/pallets/ddc-clusters/src/testing_utils.rs index 999a066ba..5acb0dc42 100644 --- a/pallets/ddc-clusters/src/testing_utils.rs +++ b/pallets/ddc-clusters/src/testing_utils.rs @@ -55,7 +55,9 @@ where let cluster_params = ClusterParams { node_provider_auth_contract: Some(user.clone()) }; let storage_node_params = StorageNodeParams { mode: StorageNodeMode::Storage, - host: vec![1u8, 255], + host: vec![1u8; 255], + domain: vec![2u8; 255], + ssl: true, http_port: 35000u16, grpc_port: 25000u16, p2p_port: 15000u16, diff --git a/pallets/ddc-clusters/src/tests.rs b/pallets/ddc-clusters/src/tests.rs index 948b28d1c..1288e5ec0 100644 --- a/pallets/ddc-clusters/src/tests.rs +++ b/pallets/ddc-clusters/src/tests.rs @@ -57,6 +57,51 @@ fn create_cluster_works() { cluster_gov_params.clone() )); + let created_cluster = DdcClusters::clusters(cluster_id).unwrap(); + assert_eq!(created_cluster.cluster_id, cluster_id); + assert_eq!(created_cluster.manager_id, cluster_manager_id); + assert_eq!(created_cluster.reserve_id, cluster_reserve_id); + assert_eq!(created_cluster.props.node_provider_auth_contract, Some(auth_contract.clone())); + + let created_cluster_gov_params = DdcClusters::clusters_gov_params(cluster_id).unwrap(); + assert_eq!(created_cluster_gov_params.treasury_share, cluster_gov_params.treasury_share); + assert_eq!( + created_cluster_gov_params.validators_share, + cluster_gov_params.validators_share + ); + assert_eq!( + created_cluster_gov_params.cluster_reserve_share, + cluster_gov_params.cluster_reserve_share + ); + assert_eq!( + created_cluster_gov_params.storage_bond_size, + cluster_gov_params.storage_bond_size + ); + assert_eq!( + created_cluster_gov_params.storage_chill_delay, + cluster_gov_params.storage_chill_delay + ); + assert_eq!( + created_cluster_gov_params.storage_unbonding_delay, + cluster_gov_params.storage_unbonding_delay + ); + assert_eq!( + created_cluster_gov_params.unit_per_mb_stored, + cluster_gov_params.unit_per_mb_stored + ); + assert_eq!( + created_cluster_gov_params.unit_per_mb_streamed, + cluster_gov_params.unit_per_mb_streamed + ); + assert_eq!( + created_cluster_gov_params.unit_per_put_request, + cluster_gov_params.unit_per_put_request + ); + assert_eq!( + created_cluster_gov_params.unit_per_get_request, + cluster_gov_params.unit_per_get_request + ); + // Creating cluster with same id should fail assert_noop!( DdcClusters::create_cluster( @@ -143,7 +188,9 @@ fn add_and_delete_node_works() { let storage_node_params = StorageNodeParams { mode: StorageNodeMode::Storage, - host: vec![1u8, 255], + host: vec![1u8; 255], + domain: vec![2u8; 255], + ssl: true, http_port: 35000u16, grpc_port: 25000u16, p2p_port: 15000u16, @@ -351,9 +398,12 @@ fn set_cluster_params_works() { assert_ok!(DdcClusters::set_cluster_params( RuntimeOrigin::signed(cluster_manager_id), cluster_id, - ClusterParams { node_provider_auth_contract: Some(auth_contract_2) }, + ClusterParams { node_provider_auth_contract: Some(auth_contract_2.clone()) }, )); + let updated_cluster = DdcClusters::clusters(cluster_id).unwrap(); + assert_eq!(updated_cluster.props.node_provider_auth_contract, Some(auth_contract_2)); + // Checking that event was emitted assert_eq!(System::events().len(), 2); System::assert_last_event(Event::ClusterParamsSet { cluster_id }.into()) @@ -406,17 +456,69 @@ fn set_cluster_gov_params_works() { DdcClusters::set_cluster_gov_params( RuntimeOrigin::signed(cluster_manager_id), cluster_id, - cluster_gov_params.clone() + cluster_gov_params ), BadOrigin ); + let updated_gov_params = ClusterGovParams { + treasury_share: Perbill::from_float(0.06), + validators_share: Perbill::from_float(0.02), + cluster_reserve_share: Perbill::from_float(0.03), + storage_bond_size: 1000, + storage_chill_delay: 500, + storage_unbonding_delay: 500, + unit_per_mb_stored: 100, + unit_per_mb_streamed: 100, + unit_per_put_request: 100, + unit_per_get_request: 100, + }; + assert_ok!(DdcClusters::set_cluster_gov_params( RuntimeOrigin::root(), cluster_id, - cluster_gov_params + updated_gov_params.clone() )); + let updated_cluster_gov_params = DdcClusters::clusters_gov_params(cluster_id).unwrap(); + assert_eq!(updated_cluster_gov_params.treasury_share, updated_gov_params.treasury_share); + assert_eq!( + updated_cluster_gov_params.validators_share, + updated_gov_params.validators_share + ); + assert_eq!( + updated_cluster_gov_params.cluster_reserve_share, + updated_gov_params.cluster_reserve_share + ); + assert_eq!( + updated_cluster_gov_params.storage_bond_size, + updated_gov_params.storage_bond_size + ); + assert_eq!( + updated_cluster_gov_params.storage_chill_delay, + updated_gov_params.storage_chill_delay + ); + assert_eq!( + updated_cluster_gov_params.storage_unbonding_delay, + updated_gov_params.storage_unbonding_delay + ); + assert_eq!( + updated_cluster_gov_params.unit_per_mb_stored, + updated_gov_params.unit_per_mb_stored + ); + assert_eq!( + updated_cluster_gov_params.unit_per_mb_streamed, + updated_gov_params.unit_per_mb_streamed + ); + assert_eq!( + updated_cluster_gov_params.unit_per_put_request, + updated_gov_params.unit_per_put_request + ); + assert_eq!( + updated_cluster_gov_params.unit_per_get_request, + updated_gov_params.unit_per_get_request + ); + // Checking that event was emitted assert_eq!(System::events().len(), 2); System::assert_last_event(Event::ClusterGovParamsSet { cluster_id }.into()) diff --git a/pallets/ddc-nodes/src/benchmarking.rs b/pallets/ddc-nodes/src/benchmarking.rs index 451ef0c2a..b13012d4c 100644 --- a/pallets/ddc-nodes/src/benchmarking.rs +++ b/pallets/ddc-nodes/src/benchmarking.rs @@ -46,7 +46,9 @@ benchmarks! { StorageNodePubKey::new([0; 32])).unwrap().props, StorageNodeProps { mode: StorageNodeMode::Storage, - host: vec![2u8, 255].try_into().unwrap(), + host: vec![3u8; 255].try_into().unwrap(), + domain: vec![4u8; 255].try_into().unwrap(), + ssl: true, http_port: 45000u16, grpc_port: 55000u16, p2p_port: 65000u16, diff --git a/pallets/ddc-nodes/src/lib.rs b/pallets/ddc-nodes/src/lib.rs index acf6b430b..e849d158b 100644 --- a/pallets/ddc-nodes/src/lib.rs +++ b/pallets/ddc-nodes/src/lib.rs @@ -77,6 +77,7 @@ pub mod pallet { OnlyNodeProvider, NodeIsAssignedToCluster, HostLenExceedsLimit, + DomainLenExceedsLimit, NodeHasDanglingStake, } diff --git a/pallets/ddc-nodes/src/node.rs b/pallets/ddc-nodes/src/node.rs index 2688400f4..e277431d8 100644 --- a/pallets/ddc-nodes/src/node.rs +++ b/pallets/ddc-nodes/src/node.rs @@ -91,12 +91,14 @@ impl NodeTrait for Node { #[derive(Debug, PartialEq)] pub enum NodeError { StorageHostLenExceedsLimit, + StorageDomainLenExceedsLimit, } impl From for Error { fn from(error: NodeError) -> Self { match error { NodeError::StorageHostLenExceedsLimit => Error::::HostLenExceedsLimit, + NodeError::StorageDomainLenExceedsLimit => Error::::DomainLenExceedsLimit, } } } diff --git a/pallets/ddc-nodes/src/storage_node.rs b/pallets/ddc-nodes/src/storage_node.rs index 3021409aa..c2034d74c 100644 --- a/pallets/ddc-nodes/src/storage_node.rs +++ b/pallets/ddc-nodes/src/storage_node.rs @@ -10,8 +10,8 @@ use serde::{Deserialize, Serialize}; use sp_runtime::RuntimeDebug; parameter_types! { - pub MaxStorageNodeParamsLen: u16 = 2048; pub MaxHostLen: u8 = 255; + pub MaxDomainLen: u8 = 255; } #[cfg_attr(feature = "std", derive(Serialize, Deserialize))] @@ -28,6 +28,8 @@ pub struct StorageNode { #[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo, PartialEq)] pub struct StorageNodeProps { pub host: BoundedVec, + pub domain: BoundedVec, + pub ssl: bool, pub http_port: u16, pub grpc_port: u16, pub p2p_port: u16, @@ -52,6 +54,11 @@ impl StorageNode { Ok(vec) => vec, Err(_) => return Err(NodeError::StorageHostLenExceedsLimit), }, + domain: match node_params.domain.try_into() { + Ok(vec) => vec, + Err(_) => return Err(NodeError::StorageDomainLenExceedsLimit), + }, + ssl: node_params.ssl, http_port: node_params.http_port, grpc_port: node_params.grpc_port, p2p_port: node_params.p2p_port, @@ -81,10 +88,16 @@ impl NodeTrait for StorageNode { fn set_params(&mut self, node_params: NodeParams) -> Result<(), NodeError> { match node_params { NodeParams::StorageParams(storage_params) => { + self.props.mode = storage_params.mode; self.props.host = match storage_params.host.try_into() { Ok(vec) => vec, Err(_) => return Err(NodeError::StorageHostLenExceedsLimit), }; + self.props.domain = match storage_params.domain.try_into() { + Ok(vec) => vec, + Err(_) => return Err(NodeError::StorageDomainLenExceedsLimit), + }; + self.props.ssl = storage_params.ssl; self.props.http_port = storage_params.http_port; self.props.grpc_port = storage_params.grpc_port; self.props.p2p_port = storage_params.p2p_port; diff --git a/pallets/ddc-nodes/src/testing_utils.rs b/pallets/ddc-nodes/src/testing_utils.rs index dd408c320..8ebe3f7bc 100644 --- a/pallets/ddc-nodes/src/testing_utils.rs +++ b/pallets/ddc-nodes/src/testing_utils.rs @@ -16,7 +16,9 @@ pub fn create_user_and_config( let node = NodePubKey::StoragePubKey(StorageNodePubKey::new([0; 32])); let storage_node_params = NodeParams::StorageParams(StorageNodeParams { mode: StorageNodeMode::Storage, - host: vec![1u8, 255], + host: vec![1u8; 255], + domain: vec![2u8; 255], + ssl: false, http_port: 35000u16, grpc_port: 25000u16, p2p_port: 15000u16, @@ -24,7 +26,9 @@ pub fn create_user_and_config( let new_storage_node_params = NodeParams::StorageParams(StorageNodeParams { mode: StorageNodeMode::Storage, - host: vec![2u8, 255], + host: vec![3u8; 255], + domain: vec![4u8; 255], + ssl: true, http_port: 45000u16, grpc_port: 55000u16, p2p_port: 65000u16, diff --git a/pallets/ddc-nodes/src/tests.rs b/pallets/ddc-nodes/src/tests.rs index aadc801c0..1f1657ad9 100644 --- a/pallets/ddc-nodes/src/tests.rs +++ b/pallets/ddc-nodes/src/tests.rs @@ -4,6 +4,7 @@ use super::{mock::*, *}; use ddc_primitives::{NodePubKey, StorageNodeMode, StorageNodeParams}; use frame_support::{assert_noop, assert_ok}; use sp_runtime::AccountId32; +use storage_node::{MaxDomainLen, MaxHostLen}; #[test] fn create_storage_node_works() { @@ -13,7 +14,9 @@ fn create_storage_node_works() { let node_pub_key = AccountId32::from(bytes); let storage_node_params = StorageNodeParams { mode: StorageNodeMode::Storage, - host: vec![1u8, 255], + host: vec![1u8; 255], + domain: vec![2u8; 255], + ssl: true, http_port: 35000u16, grpc_port: 25000u16, p2p_port: 15000u16, @@ -27,6 +30,8 @@ fn create_storage_node_works() { NodeParams::StorageParams(StorageNodeParams { mode: StorageNodeMode::Storage, host: vec![1u8; 256], + domain: vec![2u8; 255], + ssl: true, http_port: 35000u16, grpc_port: 25000u16, p2p_port: 15000u16, @@ -35,6 +40,24 @@ fn create_storage_node_works() { Error::::HostLenExceedsLimit ); + // Host length exceeds limit + assert_noop!( + DdcNodes::create_node( + RuntimeOrigin::signed(1), + NodePubKey::StoragePubKey(node_pub_key.clone()), + NodeParams::StorageParams(StorageNodeParams { + mode: StorageNodeMode::Storage, + host: vec![1u8; 255], + domain: vec![2u8; 256], + ssl: true, + http_port: 35000u16, + grpc_port: 25000u16, + p2p_port: 15000u16, + }) + ), + Error::::DomainLenExceedsLimit + ); + // Node created assert_ok!(DdcNodes::create_node( RuntimeOrigin::signed(1), @@ -42,6 +65,23 @@ fn create_storage_node_works() { NodeParams::StorageParams(storage_node_params.clone()) )); + let created_storage_node = DdcNodes::storage_nodes(&node_pub_key).unwrap(); + let expected_host: BoundedVec = + storage_node_params.clone().host.try_into().unwrap(); + let expected_domain: BoundedVec = + storage_node_params.clone().domain.try_into().unwrap(); + + assert_eq!(created_storage_node.pub_key, node_pub_key); + assert_eq!(created_storage_node.provider_id, 1); + assert_eq!(created_storage_node.cluster_id, None); + assert_eq!(created_storage_node.props.host, expected_host); + assert_eq!(created_storage_node.props.domain, expected_domain); + assert_eq!(created_storage_node.props.ssl, storage_node_params.ssl); + assert_eq!(created_storage_node.props.http_port, storage_node_params.http_port); + assert_eq!(created_storage_node.props.grpc_port, storage_node_params.grpc_port); + assert_eq!(created_storage_node.props.p2p_port, storage_node_params.p2p_port); + assert_eq!(created_storage_node.props.mode, storage_node_params.mode); + // Check storage assert!(StorageNodes::::contains_key(node_pub_key.clone())); assert!(DdcNodes::exists(&NodePubKey::StoragePubKey(node_pub_key.clone()))); @@ -79,7 +119,9 @@ fn create_storage_node_with_node_creator() { let node_pub_key = AccountId32::from(bytes); let storage_node_params = StorageNodeParams { mode: StorageNodeMode::Storage, - host: vec![1u8, 255], + host: vec![1u8; 255], + domain: vec![2u8; 255], + ssl: true, http_port: 35000u16, grpc_port: 25000u16, p2p_port: 15000u16, @@ -113,7 +155,9 @@ fn set_storage_node_params_works() { let node_pub_key = AccountId32::from(bytes); let storage_node_params = StorageNodeParams { mode: StorageNodeMode::Storage, - host: vec![1u8, 255], + host: vec![1u8; 255], + domain: vec![2u8; 255], + ssl: true, http_port: 35000u16, grpc_port: 25000u16, p2p_port: 15000u16, @@ -136,13 +180,39 @@ fn set_storage_node_params_works() { NodeParams::StorageParams(storage_node_params.clone()) )); + let updated_params = StorageNodeParams { + mode: StorageNodeMode::Full, + host: vec![3u8; 255], + domain: vec![4u8; 255], + ssl: false, + http_port: 35000u16, + grpc_port: 25000u16, + p2p_port: 15000u16, + }; + // Set node params assert_ok!(DdcNodes::set_node_params( RuntimeOrigin::signed(1), NodePubKey::StoragePubKey(node_pub_key.clone()), - NodeParams::StorageParams(storage_node_params.clone()) + NodeParams::StorageParams(updated_params.clone()) )); + let updated_storage_node = DdcNodes::storage_nodes(&node_pub_key).unwrap(); + let expected_host: BoundedVec = updated_params.host.try_into().unwrap(); + let expected_domain: BoundedVec = + updated_params.domain.try_into().unwrap(); + + assert_eq!(updated_storage_node.pub_key, node_pub_key); + assert_eq!(updated_storage_node.provider_id, 1); + assert_eq!(updated_storage_node.cluster_id, None); + assert_eq!(updated_storage_node.props.host, expected_host); + assert_eq!(updated_storage_node.props.domain, expected_domain); + assert_eq!(updated_storage_node.props.ssl, updated_params.ssl); + assert_eq!(updated_storage_node.props.http_port, updated_params.http_port); + assert_eq!(updated_storage_node.props.grpc_port, updated_params.grpc_port); + assert_eq!(updated_storage_node.props.p2p_port, updated_params.p2p_port); + assert_eq!(updated_storage_node.props.mode, updated_params.mode); + // Only node provider can set params assert_noop!( DdcNodes::set_node_params( @@ -176,6 +246,8 @@ fn set_storage_node_params_works() { NodeParams::StorageParams(StorageNodeParams { mode: StorageNodeMode::Storage, host: vec![1u8; 256], + domain: vec![2u8; 255], + ssl: true, http_port: 35000u16, grpc_port: 25000u16, p2p_port: 15000u16, @@ -184,6 +256,24 @@ fn set_storage_node_params_works() { Error::::HostLenExceedsLimit ); + // Storage domain length exceeds limit + assert_noop!( + DdcNodes::set_node_params( + RuntimeOrigin::signed(1), + NodePubKey::StoragePubKey(node_pub_key.clone()), + NodeParams::StorageParams(StorageNodeParams { + mode: StorageNodeMode::Storage, + host: vec![1u8; 255], + domain: vec![2u8; 256], + ssl: true, + http_port: 35000u16, + grpc_port: 25000u16, + p2p_port: 15000u16, + }) + ), + Error::::DomainLenExceedsLimit + ); + // Checking that event was emitted assert_eq!(System::events().len(), 2); System::assert_last_event( @@ -201,7 +291,9 @@ fn delete_storage_node_works() { let node_pub_key = AccountId32::from(bytes); let storage_node_params = StorageNodeParams { mode: StorageNodeMode::Storage, - host: vec![1u8, 255], + host: vec![1u8; 255], + domain: vec![2u8; 255], + ssl: true, http_port: 35000u16, grpc_port: 25000u16, p2p_port: 15000u16, diff --git a/pallets/ddc-staking/src/benchmarking.rs b/pallets/ddc-staking/src/benchmarking.rs index c1eeff149..4b1138753 100644 --- a/pallets/ddc-staking/src/benchmarking.rs +++ b/pallets/ddc-staking/src/benchmarking.rs @@ -28,7 +28,9 @@ benchmarks! { stash.clone(), NodeParams::StorageParams(StorageNodeParams { mode: StorageNodeMode::Storage, - host: vec![1u8, 255], + host: vec![1u8; 255], + domain: vec![2u8; 256], + ssl: true, http_port: 35000u16, grpc_port: 25000u16, p2p_port: 15000u16, diff --git a/pallets/ddc-staking/src/testing_utils.rs b/pallets/ddc-staking/src/testing_utils.rs index 54454b95d..5b2ffd33b 100644 --- a/pallets/ddc-staking/src/testing_utils.rs +++ b/pallets/ddc-staking/src/testing_utils.rs @@ -63,7 +63,9 @@ pub fn create_stash_controller_node( stash.clone(), NodeParams::StorageParams(StorageNodeParams { mode: StorageNodeMode::Storage, - host: vec![1u8, 255], + host: vec![1u8; 255], + domain: vec![2u8; 256], + ssl: true, http_port: 35000u16, grpc_port: 25000u16, p2p_port: 15000u16, @@ -98,7 +100,9 @@ pub fn create_stash_controller_node_with_balance( stash.clone(), NodeParams::StorageParams(StorageNodeParams { mode: StorageNodeMode::Storage, - host: vec![1u8, 255], + host: vec![1u8; 255], + domain: vec![2u8; 256], + ssl: true, http_port: 35000u16, grpc_port: 25000u16, p2p_port: 15000u16, diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index 1c8fba03d..06a5dcfde 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -104,6 +104,8 @@ pub enum StorageNodeMode { pub struct StorageNodeParams { pub mode: StorageNodeMode, pub host: Vec, + pub domain: Vec, + pub ssl: bool, pub http_port: u16, pub grpc_port: u16, pub p2p_port: u16,