Skip to content

Commit

Permalink
Merge pull request #114 from Cerebellum-Network/feature/auth-extensio…
Browse files Browse the repository at this point in the history
…n-call-parameters

Pass provider and node ids to the auth extension
  • Loading branch information
khssnv authored Oct 31, 2023
2 parents 70ce219 + 003a344 commit 1a1eaaa
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
42 changes: 26 additions & 16 deletions pallets/ddc-clusters/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,22 +133,6 @@ pub mod pallet {
.map_err(|_| Error::<T>::AttemptToAddNonExistentNode)?;
ensure!(node.get_cluster_id().is_none(), Error::<T>::NodeIsAlreadyAssigned);

// Cluster extension smart contract allows joining.
let is_authorized: bool = pallet_contracts::Pallet::<T>::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::<T>::NotAuthorized);

// Sufficient funds are locked at the DDC Staking module.
let node_provider_stash =
<pallet_ddc_staking::Pallet<T>>::nodes(&node_pub_key).ok_or(Error::<T>::NoStake)?;
Expand All @@ -171,6 +155,32 @@ pub mod pallet {
.is_some();
ensure!(!chilling, Error::<T>::ChillingProhibited);

// Cluster extension smart contract allows joining.
let call_data = {
// is_authorized(node_provider: AccountId, node: Vec<u8>, node_variant: u8) -> bool
let args: ([u8; 4], T::AccountId, Vec<u8>, 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::<T>::bare_call(
caller_id,
cluster.props.node_provider_auth_contract,
Default::default(),
EXTENSION_CALL_GAS_LIMIT,
None,
call_data,
false,
)
.result?
.data
.first()
.is_some_and(|x| *x == 1);
ensure!(is_authorized, Error::<T>::NotAuthorized);

node.set_cluster_id(Some(cluster_id.clone()));
T::NodeRepository::update(node).map_err(|_| Error::<T>::AttemptToAddNonExistentNode)?;
ClustersNodes::<T>::insert(cluster_id.clone(), node_pub_key.clone(), true);
Expand Down
9 changes: 9 additions & 0 deletions primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
2 changes: 1 addition & 1 deletion runtime/cere-dev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 1a1eaaa

Please sign in to comment.