Skip to content

Commit

Permalink
fix : fix rpc result response and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
1xstj committed Jan 10, 2024
1 parent c7fc287 commit cd6aa1a
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 53 deletions.
8 changes: 4 additions & 4 deletions pallets/jobs/rpc/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use parity_scale_codec::Codec;
use sp_runtime::{traits::MaybeDisplay, Serialize};
use sp_std::vec::Vec;
use tangle_primitives::{
jobs::{JobId, RpcResponseJobsData, RpcResponsePhaseOneResult},
jobs::{JobId, PhaseResult, RpcResponseJobsData},
roles::RoleType,
};

Expand Down Expand Up @@ -59,7 +59,7 @@ sp_api::decl_runtime_apis! {
/// An optional `RpcResponseJobsData` containing the account ID of the job.
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.
/// Queries the result of a job by its role_type and ID.
///
/// # Arguments
///
Expand All @@ -68,8 +68,8 @@ 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, BlockNumberOf<Block>>>;
/// An `Option` containing the phase one result of the job, wrapped in an `PhaseResult`.
fn query_job_result(role_type: RoleType, job_id: JobId) -> Option<PhaseResult<AccountId, BlockNumberOf<Block>>>;

/// Queries next job ID.
///
Expand Down
12 changes: 6 additions & 6 deletions pallets/jobs/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use sp_runtime::{
};
use std::sync::Arc;
use tangle_primitives::{
jobs::{JobId, RpcResponseJobsData, RpcResponsePhaseOneResult},
jobs::{JobId, PhaseResult, RpcResponseJobsData},
roles::RoleType,
};

Expand All @@ -56,12 +56,12 @@ pub trait JobsApi<BlockHash, AccountId, BlockNumber> {
) -> RpcResult<Option<RpcResponseJobsData<AccountId, BlockNumber>>>;

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

#[method(name = "jobs_queryNextJobId")]
fn query_next_job_id(&self, at: Option<BlockHash>) -> RpcResult<JobId>;
Expand Down Expand Up @@ -116,16 +116,16 @@ where
}
}

fn query_phase_one_by_id(
fn query_job_result(
&self,
at: Option<<Block as BlockT>::Hash>,
role_type: RoleType,
job_id: JobId,
) -> RpcResult<Option<RpcResponsePhaseOneResult<AccountId, BlockNumberOf<Block>>>> {
) -> RpcResult<Option<PhaseResult<AccountId, BlockNumberOf<Block>>>> {
let api = self.client.runtime_api();
let at = at.unwrap_or_else(|| self.client.info().best_hash);

match api.query_phase_one_by_id(at, role_type, job_id) {
match api.query_job_result(at, role_type, job_id) {
Ok(res) => Ok(res),
Err(e) => Err(runtime_error_into_rpc_err(e)),
}
Expand Down
29 changes: 5 additions & 24 deletions pallets/jobs/src/rpc.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;
pub use tangle_primitives::jobs::RpcResponseJobsData;
use tangle_primitives::{jobs::RpcResponsePhaseOneResult, roles::RoleType};
use tangle_primitives::{jobs::PhaseResult, roles::RoleType};

impl<T: Config> Pallet<T> {
/// Queries jobs associated with a specific validator.
Expand Down Expand Up @@ -73,31 +73,12 @@ impl<T: Config> Pallet<T> {
/// # Returns
///
/// An `Option` containing the phase one result of the job, wrapped in an
/// `RpcResponsePhaseOneResult`.
pub fn query_phase_one_by_id(
/// `PhaseResult`.
pub fn query_job_result(
role_type: RoleType,
job_id: JobId,
) -> Option<RpcResponsePhaseOneResult<T::AccountId, BlockNumberFor<T>>> {
if let Some(job_info) = SubmittedJobs::<T>::get(role_type, job_id) {
if !job_info.job_type.is_phase_one() {
let result = KnownResults::<T>::get(
job_info.job_type.get_role_type(),
job_info.job_type.clone().get_phase_one_id().unwrap(),
)
.unwrap();

let info = RpcResponsePhaseOneResult {
owner: job_info.owner,
result: result.result,
job_type: job_info.job_type,
ttl: job_info.ttl,
};

return Some(info)
}
}

None
) -> Option<PhaseResult<T::AccountId, BlockNumberFor<T>>> {
KnownResults::<T>::get(role_type, job_id)
}

/// Queries next job ID.
Expand Down
104 changes: 103 additions & 1 deletion pallets/jobs/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use sp_runtime::AccountId32;
use tangle_primitives::{
jobs::{
DKGTSSPhaseOneJobType, DKGTSSPhaseTwoJobType, DKGTSSSignatureResult, DigitalSignatureType,
JobSubmission, JobType,
JobSubmission, JobType, RpcResponseJobsData,
},
roles::{RoleType, ThresholdSignatureRoleType},
};
Expand Down Expand Up @@ -201,6 +201,108 @@ fn jobs_submission_e2e_works_for_dkg() {
});
}

#[test]
fn jobs_rpc_tests() {
new_test_ext().execute_with(|| {
System::set_block_number(1);

let participants = vec![ALICE, BOB, CHARLIE, DAVE, EVE];

let threshold_signature_role_type = ThresholdSignatureRoleType::TssGG20;
let submission = JobSubmission {
expiry: 10,
ttl: 200,
job_type: JobType::DKGTSSPhaseOne(DKGTSSPhaseOneJobType {
participants: participants.clone(),
threshold: 3,
permitted_caller: Some(TEN),
role_type: threshold_signature_role_type,
}),
};
assert_ok!(Jobs::submit_job(RuntimeOrigin::signed(TEN), submission));

let stored_job =
SubmittedJobs::<Runtime>::get(RoleType::Tss(threshold_signature_role_type), 0).unwrap();
let expected_rpc_response = RpcResponseJobsData {
job_id: 0,
job_type: stored_job.job_type,
ttl: stored_job.ttl,
expiry: stored_job.expiry,
};

// query jobs by validator should work
for validator in participants {
assert_eq!(
Jobs::query_jobs_by_validator(validator),
Some(vec![expected_rpc_response.clone()])
);
}

assert_eq!(
Jobs::query_job_by_id(RoleType::Tss(threshold_signature_role_type), 0),
Some(expected_rpc_response)
);
assert_eq!(Jobs::query_next_job_id(), 1);

// submit a solution for this job
assert_ok!(Jobs::submit_job_result(
RuntimeOrigin::signed(TEN),
RoleType::Tss(ThresholdSignatureRoleType::TssGG20),
0,
JobResult::DKGPhaseOne(DKGTSSKeySubmissionResult {
signatures: vec![],
threshold: 3,
participants: vec![],
key: vec![],
signature_type: DigitalSignatureType::Ecdsa
})
));

assert_eq!(Jobs::query_job_by_id(RoleType::Tss(threshold_signature_role_type), 0), None);
assert_eq!(Jobs::query_next_job_id(), 1);

let expected_result =
KnownResults::<Runtime>::get(RoleType::Tss(ThresholdSignatureRoleType::TssGG20), 0);
assert_eq!(
Jobs::query_job_result(RoleType::Tss(threshold_signature_role_type), 0),
expected_result
);

let submission = JobSubmission {
expiry: 10,
ttl: 0,
job_type: JobType::DKGTSSPhaseTwo(DKGTSSPhaseTwoJobType {
phase_one_id: 0,
submission: vec![],
role_type: threshold_signature_role_type,
}),
};
assert_ok!(Jobs::submit_job(RuntimeOrigin::signed(TEN), submission));

let stored_job =
SubmittedJobs::<Runtime>::get(RoleType::Tss(threshold_signature_role_type), 1).unwrap();
let expected_rpc_response = RpcResponseJobsData {
job_id: 1,
job_type: stored_job.job_type,
ttl: stored_job.ttl,
expiry: stored_job.expiry,
};

assert_eq!(
Jobs::query_job_by_id(RoleType::Tss(threshold_signature_role_type), 1),
Some(expected_rpc_response)
);
assert_eq!(Jobs::query_next_job_id(), 2);

let expected_result =
KnownResults::<Runtime>::get(RoleType::Tss(ThresholdSignatureRoleType::TssGG20), 1);
assert_eq!(
Jobs::query_job_result(RoleType::Tss(threshold_signature_role_type), 1),
expected_result
);
});
}

// TODO : Integrate after zksaas pallet
// #[test]
// fn jobs_submission_e2e_works_for_zksaas() {
Expand Down
16 changes: 1 addition & 15 deletions primitives/src/jobs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ pub enum JobState {

/// Represents a job submission with specified `AccountId` and `BlockNumber`.
#[derive(PartialEq, Eq, Encode, Decode, RuntimeDebug, TypeInfo, Clone)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub struct PhaseResult<AccountId, BlockNumber> {
/// The owner's account ID.
pub owner: AccountId,
Expand Down Expand Up @@ -256,21 +257,6 @@ pub struct RpcResponseJobsData<AccountId, BlockNumber> {
pub ttl: BlockNumber,
}

#[derive(PartialEq, Eq, Encode, Decode, RuntimeDebug, TypeInfo, Clone)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub struct RpcResponsePhaseOneResult<AccountId, BlockNumber> {
/// The owner's account ID.
pub owner: AccountId,
/// The type of the job result.
pub result: JobResult,
/// The type of the job submission.
pub job_type: JobType<AccountId>,

/// The time-to-live (TTL) for the job, which determines the maximum allowed time for this job
/// to be available. After the TTL expires, the job can no longer be used.
pub ttl: BlockNumber,
}

#[derive(PartialEq, Eq, Encode, Decode, RuntimeDebug, TypeInfo, Clone)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
pub enum JobResult {
Expand Down
6 changes: 3 additions & 3 deletions runtime/testnet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ use sp_version::RuntimeVersion;
use static_assertions::const_assert;
pub use tangle_crypto_primitives::crypto::AuthorityId as RoleKeyId;
use tangle_primitives::{
jobs::{JobId, RpcResponseJobsData, RpcResponsePhaseOneResult},
jobs::{JobId, PhaseResult, RpcResponseJobsData},
roles::RoleType,
};

Expand Down Expand Up @@ -1413,8 +1413,8 @@ impl_runtime_apis! {
Jobs::query_job_by_id(role_type, job_id)
}

fn query_phase_one_by_id(role_type: RoleType, job_id: JobId) -> Option<RpcResponsePhaseOneResult<AccountId, BlockNumberOf<Block>>> {
Jobs::query_phase_one_by_id(role_type, job_id)
fn query_job_result(role_type: RoleType, job_id: JobId) -> Option<PhaseResult<AccountId, BlockNumberOf<Block>>> {
Jobs::query_job_result(role_type, job_id)
}

fn query_next_job_id() -> JobId {
Expand Down

0 comments on commit cd6aa1a

Please sign in to comment.