-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #116 from Cerebellum-Network/feat/ddc-traits
Loose coupling between Staking and Clusters pallets
- Loading branch information
Showing
19 changed files
with
570 additions
and
357 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use crate::Config; | ||
use codec::Encode; | ||
use ddc_primitives::{NodePubKey, NodeType}; | ||
use frame_support::weights::Weight; | ||
use pallet_contracts::chain_extension::UncheckedFrom; | ||
use sp_std::prelude::Vec; | ||
|
||
/// ink! 4.x selector for the "is_authorized" message, equals to the first four bytes of the | ||
/// blake2("is_authorized"). See also: https://use.ink/basics/selectors#selector-calculation/, | ||
/// https://use.ink/macros-attributes/selector/. | ||
const INK_SELECTOR_IS_AUTHORIZED: [u8; 4] = [0x96, 0xb0, 0x45, 0x3e]; | ||
|
||
/// The maximum amount of weight that the cluster extension contract call is allowed to consume. | ||
/// See also https://github.com/paritytech/substrate/blob/a3ed0119c45cdd0d571ad34e5b3ee7518c8cef8d/frame/contracts/rpc/src/lib.rs#L63. | ||
const EXTENSION_CALL_GAS_LIMIT: Weight = Weight::from_ref_time(5_000_000_000_000); | ||
|
||
pub struct NodeProviderAuthContract<T: Config> { | ||
contract_id: T::AccountId, | ||
caller_id: T::AccountId, | ||
} | ||
|
||
impl<T: Config> NodeProviderAuthContract<T> | ||
where | ||
T::AccountId: UncheckedFrom<T::Hash> + AsRef<[u8]>, | ||
{ | ||
pub fn is_authorized( | ||
&self, | ||
node_provider_id: T::AccountId, | ||
node_pub_key: NodePubKey, | ||
node_type: NodeType, | ||
) -> Result<bool, NodeProviderAuthContractError> { | ||
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_id, | ||
/* remove the first byte* added by SCALE */ | ||
node_pub_key.encode()[1..].to_vec(), | ||
node_type.into(), | ||
); | ||
args.encode() | ||
}; | ||
|
||
let is_authorized = pallet_contracts::Pallet::<T>::bare_call( | ||
self.caller_id.clone(), | ||
self.contract_id.clone(), | ||
Default::default(), | ||
EXTENSION_CALL_GAS_LIMIT, | ||
None, | ||
call_data, | ||
false, | ||
) | ||
.result | ||
.map_err(|_| NodeProviderAuthContractError::ContractCallFailed)? | ||
.data | ||
.first() | ||
.is_some_and(|x| *x == 1); | ||
|
||
Ok(is_authorized) | ||
} | ||
|
||
pub fn new(contract_id: T::AccountId, caller_id: T::AccountId) -> Self { | ||
Self { contract_id, caller_id } | ||
} | ||
} | ||
|
||
pub enum NodeProviderAuthContractError { | ||
ContractCallFailed, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.