Skip to content

Commit

Permalink
Merge branch 'main' into salman/store-rolekey
Browse files Browse the repository at this point in the history
  • Loading branch information
salman01zp authored Jan 8, 2024
2 parents 4c85e04 + af3548e commit 7340a1c
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 146 deletions.
4 changes: 2 additions & 2 deletions node/src/chainspec/testnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ pub fn local_testnet_config(chain_id: u64) -> Result<ChainSpec, String> {
let mut properties = sc_chain_spec::Properties::new();
properties.insert("tokenSymbol".into(), "tTNT".into());
properties.insert("tokenDecimals".into(), 18u32.into());
properties.insert("ss58Format".into(), tangle_primitives::TESTNET_SS58_PREFIX.into());
properties.insert("ss58Format".into(), 42.into());

Ok(ChainSpec::from_genesis(
// Name
Expand Down Expand Up @@ -151,7 +151,7 @@ pub fn tangle_testnet_config(chain_id: u64) -> Result<ChainSpec, String> {
let mut properties = sc_chain_spec::Properties::new();
properties.insert("tokenSymbol".into(), "tTNT".into());
properties.insert("tokenDecimals".into(), 18u32.into());
properties.insert("ss58Format".into(), tangle_primitives::TESTNET_SS58_PREFIX.into());
properties.insert("ss58Format".into(), 42.into());

Ok(ChainSpec::from_genesis(
"Tangle Testnet",
Expand Down
11 changes: 7 additions & 4 deletions pallets/jobs/rpc/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ use tangle_primitives::{
roles::RoleType,
};

pub type BlockNumberOf<Block> =
<<Block as sp_runtime::traits::HeaderProvider>::HeaderT as sp_runtime::traits::Header>::Number;

sp_api::decl_runtime_apis! {
pub trait JobsApi<AccountId> where
AccountId: Codec + MaybeDisplay + Serialize,
Expand All @@ -33,7 +36,7 @@ sp_api::decl_runtime_apis! {
///
/// This function takes a `validator` parameter of type `AccountId` and attempts
/// to retrieve a list of jobs associated with the provided validator. If successful,
/// it constructs a vector of `RpcResponseJobsData<AccountId>` containing information
/// it constructs a vector of `RpcResponseJobsData` containing information
/// about the jobs and returns it as a `Result`.
///
/// # Arguments
Expand All @@ -43,7 +46,7 @@ sp_api::decl_runtime_apis! {
/// # Returns
///
/// An optional vec of `RpcResponseJobsData` of jobs assigned to validator
fn query_jobs_by_validator(validator: AccountId) -> Option<Vec<RpcResponseJobsData<AccountId>>>;
fn query_jobs_by_validator(validator: AccountId) -> Option<Vec<RpcResponseJobsData<AccountId, BlockNumberOf<Block>>>>;
/// Queries a job by its key and ID.
///
/// # Arguments
Expand All @@ -54,7 +57,7 @@ sp_api::decl_runtime_apis! {
/// # Returns
///
/// An optional `RpcResponseJobsData` containing the account ID of the job.
fn query_job_by_id(role_type: RoleType, job_id: JobId) -> Option<RpcResponseJobsData<AccountId>>;
fn query_job_by_id(role_type: RoleType, job_id: JobId) -> Option<RpcResponseJobsData<AccountId, BlockNumberOf<Block>>>;

/// Queries the phase one result of a job by its key and ID.
///
Expand All @@ -66,7 +69,7 @@ sp_api::decl_runtime_apis! {
/// # Returns
///
/// An `Option` containing the phase one result of the job, wrapped in an `RpcResponsePhaseOneResult`.
fn query_phase_one_by_id(role_type: RoleType, job_id: JobId) -> Option<RpcResponsePhaseOneResult<AccountId>>;
fn query_phase_one_by_id(role_type: RoleType, job_id: JobId) -> Option<RpcResponsePhaseOneResult<AccountId, BlockNumberOf<Block>>>;

/// Queries next job ID.
///
Expand Down
20 changes: 11 additions & 9 deletions pallets/jobs/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,34 @@ use tangle_primitives::{
roles::RoleType,
};

type BlockNumberOf<Block> =
<<Block as sp_runtime::traits::HeaderProvider>::HeaderT as sp_runtime::traits::Header>::Number;

/// JobsClient RPC methods.
#[rpc(client, server)]
pub trait JobsApi<BlockHash, AccountId> {
pub trait JobsApi<BlockHash, AccountId, BlockNumber> {
#[method(name = "jobs_queryJobsByValidator")]
fn query_jobs_by_validator(
&self,
at: Option<BlockHash>,
validator: AccountId,
) -> RpcResult<Option<Vec<RpcResponseJobsData<AccountId>>>>;
) -> RpcResult<Option<Vec<RpcResponseJobsData<AccountId, BlockNumber>>>>;

#[method(name = "jobs_queryJobById")]
fn query_job_by_id(
&self,
at: Option<BlockHash>,
role_type: RoleType,
job_id: JobId,
) -> RpcResult<Option<RpcResponseJobsData<AccountId>>>;
) -> RpcResult<Option<RpcResponseJobsData<AccountId, BlockNumber>>>;

#[method(name = "jobs_queryPhaseOneById")]
fn query_phase_one_by_id(
&self,
at: Option<BlockHash>,
role_type: RoleType,
job_id: JobId,
) -> RpcResult<Option<RpcResponsePhaseOneResult<AccountId>>>;
) -> RpcResult<Option<RpcResponsePhaseOneResult<AccountId, BlockNumber>>>;

#[method(name = "jobs_queryNextJobId")]
fn query_next_job_id(&self, at: Option<BlockHash>) -> RpcResult<JobId>;
Expand All @@ -77,7 +80,7 @@ impl<C, M, P> JobsClient<C, M, P> {
}
}

impl<C, Block, AccountId> JobsApiServer<<Block as BlockT>::Hash, AccountId>
impl<C, Block, AccountId> JobsApiServer<<Block as BlockT>::Hash, AccountId, BlockNumberOf<Block>>
for JobsClient<C, Block, AccountId>
where
Block: BlockT,
Expand All @@ -89,7 +92,7 @@ where
&self,
at: Option<<Block as BlockT>::Hash>,
validator: AccountId,
) -> RpcResult<Option<Vec<RpcResponseJobsData<AccountId>>>> {
) -> RpcResult<Option<Vec<RpcResponseJobsData<AccountId, BlockNumberOf<Block>>>>> {
let api = self.client.runtime_api();
let at = at.unwrap_or_else(|| self.client.info().best_hash);

Expand All @@ -104,10 +107,9 @@ where
at: Option<<Block as BlockT>::Hash>,
role_type: RoleType,
job_id: JobId,
) -> RpcResult<Option<RpcResponseJobsData<AccountId>>> {
) -> RpcResult<Option<RpcResponseJobsData<AccountId, BlockNumberOf<Block>>>> {
let api = self.client.runtime_api();
let at = at.unwrap_or_else(|| self.client.info().best_hash);

match api.query_job_by_id(at, role_type, job_id) {
Ok(res) => Ok(res),
Err(e) => Err(runtime_error_into_rpc_err(e)),
Expand All @@ -119,7 +121,7 @@ where
at: Option<<Block as BlockT>::Hash>,
role_type: RoleType,
job_id: JobId,
) -> RpcResult<Option<RpcResponsePhaseOneResult<AccountId>>> {
) -> RpcResult<Option<RpcResponsePhaseOneResult<AccountId, BlockNumberOf<Block>>>> {
let api = self.client.runtime_api();
let at = at.unwrap_or_else(|| self.client.info().best_hash);

Expand Down
48 changes: 27 additions & 21 deletions pallets/jobs/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ impl<T: Config> Pallet<T> {

// charge the validator fee for job submission
let job = JobSubmissionOf::<T> {
expiry: phase1.expiry,
expiry: job_info.expiry,
ttl: job_info.ttl,
job_type: job_type.clone(),
};

Expand All @@ -193,7 +194,8 @@ impl<T: Config> Pallet<T> {

let job_info = JobInfo {
owner: phase1.owner.clone(),
expiry: phase1.expiry,
expiry: job_info.expiry,
ttl: job_info.ttl,
job_type,
fee,
};
Expand Down Expand Up @@ -232,7 +234,8 @@ impl<T: Config> Pallet<T> {

// charge the validator fee for job submission
let job = JobSubmissionOf::<T> {
expiry: phase1.expiry,
expiry: job_info.expiry,
ttl: job_info.ttl,
job_type: job_type.clone(),
};

Expand All @@ -246,7 +249,8 @@ impl<T: Config> Pallet<T> {

let job_info = JobInfo {
owner: phase1.owner.clone(),
expiry: phase1.expiry,
expiry: job_info.expiry,
ttl: job_info.ttl,
job_type,
fee,
};
Expand Down Expand Up @@ -297,15 +301,15 @@ impl<T: Config> Pallet<T> {
T::MPCHandler::verify(JobWithResult {
job_type: job_info.job_type.clone(),
phase_one_job_type: None,
result: job_result,
result: job_result.clone(),
})?;

let result = PhaseResult {
owner: job_info.owner.clone(),
expiry: job_info.expiry,
job_type: job_info.job_type.clone(),
ttl: job_info.ttl,
permitted_caller: job_info.job_type.clone().get_permitted_caller(),
result: info.key,
result: job_result,
};
Ok(result)
}
Expand All @@ -330,7 +334,7 @@ impl<T: Config> Pallet<T> {
.ok_or(Error::<T>::PreviousResultNotFound)?;

// Validate existing result
ensure!(phase_one_result.expiry >= now, Error::<T>::ResultExpired);
ensure!(phase_one_result.ttl >= now, Error::<T>::ResultExpired);

// ensure the participants are the expected participants from job
let mut participant_keys: Vec<sp_core::ecdsa::Public> = Default::default();
Expand All @@ -343,11 +347,14 @@ impl<T: Config> Pallet<T> {
.map_err(|_| Error::<T>::InvalidValidator)?;
participant_keys.push(pub_key);
}

let signing_key = match phase_one_result.result {
JobResult::DKGPhaseOne(result) => result.key,
_ => return Err(Error::<T>::InvalidJobPhase.into()),
};
let job_result = JobResult::DKGPhaseTwo(DKGTSSSignatureResult {
signature: info.signature.clone(),
data: info.data,
signing_key: phase_one_result.result,
signing_key,
signature_type: info.signature_type,
});

Expand All @@ -359,15 +366,15 @@ impl<T: Config> Pallet<T> {
T::MPCHandler::verify(JobWithResult {
job_type: job_info.job_type.clone(),
phase_one_job_type: Some(phase_one_job_info.job_type),
result: job_result,
result: job_result.clone(),
})?;

let result = PhaseResult {
owner: job_info.owner.clone(),
expiry: job_info.expiry,
ttl: job_info.ttl,
job_type: job_info.job_type.clone(),
permitted_caller: job_info.job_type.clone().get_permitted_caller(),
result: info.signature,
result: job_result,
};
Ok(result)
}
Expand Down Expand Up @@ -405,15 +412,14 @@ impl<T: Config> Pallet<T> {
T::MPCHandler::verify(JobWithResult {
job_type: job_info.job_type.clone(),
phase_one_job_type: None,
result: job_result,
result: job_result.clone(),
})?;

let result = PhaseResult {
owner: job_info.owner.clone(),
expiry: job_info.expiry,
ttl: job_info.ttl,
job_type: job_info.job_type.clone(),
// No data in the result
result: Vec::new(),
result: job_result,
permitted_caller: job_info.job_type.clone().get_permitted_caller(),
};
Ok(result)
Expand All @@ -440,7 +446,7 @@ impl<T: Config> Pallet<T> {
.ok_or(Error::<T>::PreviousResultNotFound)?;

// Validate existing result
ensure!(phase_one_result.expiry >= now, Error::<T>::ResultExpired);
ensure!(phase_one_result.ttl >= now, Error::<T>::ResultExpired);

let job_result = JobResult::ZkSaaSPhaseTwo(info.clone());

Expand All @@ -452,14 +458,14 @@ impl<T: Config> Pallet<T> {
T::MPCHandler::verify(JobWithResult {
job_type: job_info.job_type.clone(),
phase_one_job_type: Some(phase_one_job_info.job_type),
result: job_result,
result: job_result.clone(),
})?;

let result = PhaseResult {
owner: job_info.owner.clone(),
expiry: job_info.expiry,
ttl: job_info.ttl,
job_type: job_info.job_type.clone(),
result: info.proof(),
result: job_result,
permitted_caller: job_info.job_type.clone().get_permitted_caller(),
};
Ok(result)
Expand Down
15 changes: 5 additions & 10 deletions pallets/jobs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ pub mod module {
}

#[pallet::event]
#[pallet::generate_deposit(fn deposit_event)]
#[pallet::generate_deposit(pub(crate) fn deposit_event)]
pub enum Event<T: Config> {
/// A new job has been submitted
JobSubmitted { job_id: JobId, role_type: RoleType, details: JobSubmissionOf<T> },
Expand All @@ -148,14 +148,8 @@ pub mod module {

#[pallet::storage]
#[pallet::getter(fn known_results)]
pub type KnownResults<T: Config> = StorageDoubleMap<
_,
Twox64Concat,
RoleType,
Blake2_128Concat,
JobId,
PhaseResult<T::AccountId, BlockNumberFor<T>>,
>;
pub type KnownResults<T: Config> =
StorageDoubleMap<_, Twox64Concat, RoleType, Blake2_128Concat, JobId, PhaseResultOf<T>>;

#[pallet::storage]
#[pallet::getter(fn validator_job_id_lookup)]
Expand Down Expand Up @@ -244,7 +238,7 @@ pub mod module {
.ok_or(Error::<T>::PreviousResultNotFound)?;

// Validate existing result
ensure!(result.expiry >= now, Error::<T>::ResultExpired);
ensure!(result.ttl >= now, Error::<T>::ResultExpired);

// Ensure the phase one participants are still validators
let participants = result.participants().ok_or(Error::<T>::InvalidJobPhase)?;
Expand Down Expand Up @@ -279,6 +273,7 @@ pub mod module {
// store the job to pallet
let job_info = JobInfo {
owner: caller.clone(),
ttl: job.ttl,
expiry: job.expiry,
job_type: job.job_type.clone(),
fee,
Expand Down
Loading

0 comments on commit 7340a1c

Please sign in to comment.