From f26f83d82b56aa03bdcc80405785501e3f8a71fe Mon Sep 17 00:00:00 2001 From: "Alisher A. Khassanov" Date: Fri, 27 Oct 2023 18:57:24 +0600 Subject: [PATCH 1/5] Impl conversion from `NodePubKey` variant to `u8` --- primitives/src/lib.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index de23aa1fd..401f5974e 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -17,5 +17,14 @@ pub enum NodePubKey { CDNPubKey(CDNNodePubKey), } +impl NodePubKey { + pub fn variant_as_number(&self) -> u8 { + match self { + NodePubKey::CDNPubKey(_) => 0, + NodePubKey::StoragePubKey(_) => 1, + } + } +} + pub type StorageNodePubKey = AccountId32; pub type CDNNodePubKey = AccountId32; From 139295607fea26a37443b727554857d47330a341 Mon Sep 17 00:00:00 2001 From: "Alisher A. Khassanov" Date: Fri, 27 Oct 2023 19:00:06 +0600 Subject: [PATCH 2/5] Reorder node auth and DDC stake check --- pallets/ddc-clusters/src/lib.rs | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/pallets/ddc-clusters/src/lib.rs b/pallets/ddc-clusters/src/lib.rs index 4da5c814e..972ba9f62 100644 --- a/pallets/ddc-clusters/src/lib.rs +++ b/pallets/ddc-clusters/src/lib.rs @@ -133,22 +133,6 @@ pub mod pallet { .map_err(|_| Error::::AttemptToAddNonExistentNode)?; ensure!(node.get_cluster_id().is_none(), Error::::NodeIsAlreadyAssigned); - // Cluster extension smart contract allows joining. - let is_authorized: bool = pallet_contracts::Pallet::::bare_call( - caller_id, - cluster.props.node_provider_auth_contract, - Default::default(), - EXTENSION_CALL_GAS_LIMIT, - None, - Vec::from(INK_SELECTOR_IS_AUTHORIZED), - false, - ) - .result? - .data - .first() - .is_some_and(|x| *x == 1); - ensure!(is_authorized, Error::::NotAuthorized); - // Sufficient funds are locked at the DDC Staking module. let node_provider_stash = >::nodes(&node_pub_key).ok_or(Error::::NoStake)?; @@ -171,6 +155,22 @@ pub mod pallet { .is_some(); ensure!(!chilling, Error::::ChillingProhibited); + // Cluster extension smart contract allows joining. + let is_authorized: bool = pallet_contracts::Pallet::::bare_call( + caller_id, + cluster.props.node_provider_auth_contract, + Default::default(), + EXTENSION_CALL_GAS_LIMIT, + None, + Vec::from(INK_SELECTOR_IS_AUTHORIZED), + false, + ) + .result? + .data + .first() + .is_some_and(|x| *x == 1); + ensure!(is_authorized, Error::::NotAuthorized); + node.set_cluster_id(Some(cluster_id.clone())); T::NodeRepository::update(node).map_err(|_| Error::::AttemptToAddNonExistentNode)?; ClustersNodes::::insert(cluster_id.clone(), node_pub_key.clone(), true); From 819fbfe9502c9a6e1a4c250d5480a550ac860c5c Mon Sep 17 00:00:00 2001 From: "Alisher A. Khassanov" Date: Fri, 27 Oct 2023 19:01:24 +0600 Subject: [PATCH 3/5] Pass node and provider ids to the auth extension --- pallets/ddc-clusters/src/lib.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pallets/ddc-clusters/src/lib.rs b/pallets/ddc-clusters/src/lib.rs index 972ba9f62..f8a5b35cf 100644 --- a/pallets/ddc-clusters/src/lib.rs +++ b/pallets/ddc-clusters/src/lib.rs @@ -156,13 +156,19 @@ pub mod pallet { ensure!(!chilling, Error::::ChillingProhibited); // Cluster extension smart contract allows joining. + // is_authorized(node_provider: AccountId, node: Vec, node_variant: u8) -> bool + let mut call_data = Vec::new(); + call_data.extend_from_slice(&INK_SELECTOR_IS_AUTHORIZED); + call_data.append(&mut node_provider_stash.encode()); + call_data.append(&mut node_pub_key.encode()); + call_data.push(node_pub_key.variant_as_number()); let is_authorized: bool = pallet_contracts::Pallet::::bare_call( caller_id, cluster.props.node_provider_auth_contract, Default::default(), EXTENSION_CALL_GAS_LIMIT, None, - Vec::from(INK_SELECTOR_IS_AUTHORIZED), + call_data, false, ) .result? From 26f4a33bac7374d02825f1eb1a988381ccd9d9f9 Mon Sep 17 00:00:00 2001 From: "Alisher A. Khassanov" Date: Mon, 30 Oct 2023 12:00:38 +0600 Subject: [PATCH 4/5] Bump `cere-dev` runtime `spec_version` --- runtime/cere-dev/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/cere-dev/src/lib.rs b/runtime/cere-dev/src/lib.rs index 3cf8bb6f9..46ee3b429 100644 --- a/runtime/cere-dev/src/lib.rs +++ b/runtime/cere-dev/src/lib.rs @@ -130,7 +130,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { // and set impl_version to 0. If only runtime // implementation changes and behavior does not, then leave spec_version as // is and increment impl_version. - spec_version: 48010, + spec_version: 48011, impl_version: 0, apis: RUNTIME_API_VERSIONS, transaction_version: 5, From 003a344403b72f69578df642bc4ba091bbfbfc32 Mon Sep 17 00:00:00 2001 From: "Alisher A. Khassanov" Date: Tue, 31 Oct 2023 12:47:04 +0600 Subject: [PATCH 5/5] Fix cluster extension contract call data encoding --- pallets/ddc-clusters/src/lib.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/pallets/ddc-clusters/src/lib.rs b/pallets/ddc-clusters/src/lib.rs index f8a5b35cf..fafa00f9b 100644 --- a/pallets/ddc-clusters/src/lib.rs +++ b/pallets/ddc-clusters/src/lib.rs @@ -156,13 +156,17 @@ pub mod pallet { ensure!(!chilling, Error::::ChillingProhibited); // Cluster extension smart contract allows joining. - // is_authorized(node_provider: AccountId, node: Vec, node_variant: u8) -> bool - let mut call_data = Vec::new(); - call_data.extend_from_slice(&INK_SELECTOR_IS_AUTHORIZED); - call_data.append(&mut node_provider_stash.encode()); - call_data.append(&mut node_pub_key.encode()); - call_data.push(node_pub_key.variant_as_number()); - let is_authorized: bool = pallet_contracts::Pallet::::bare_call( + let call_data = { + // is_authorized(node_provider: AccountId, node: Vec, node_variant: u8) -> bool + let args: ([u8; 4], T::AccountId, Vec, u8) = ( + INK_SELECTOR_IS_AUTHORIZED, + node_provider_stash, + node_pub_key.encode()[1..].to_vec(), // remove the first byte added by SCALE + node_pub_key.variant_as_number(), + ); + args.encode() + }; + let is_authorized = pallet_contracts::Pallet::::bare_call( caller_id, cluster.props.node_provider_auth_contract, Default::default(),