Skip to content
This repository has been archived by the owner on Feb 21, 2024. It is now read-only.

feat: added public keys argument to acknowledge_match extrinsic #80

Merged
merged 4 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 42 additions & 6 deletions pallets/marketplace/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ use frame_support::{
};
use frame_system::RawOrigin;
use sp_core::*;
use sp_runtime::traits::ConstU32;
use sp_runtime::BoundedVec;
use sp_runtime::{traits::ConstU32, DispatchError};
use sp_std::prelude::*;

pub use pallet::Config;
use pallet_acurast::{
Event as AcurastEvent, JobModules, JobRegistrationFor, MultiOrigin, Pallet as Acurast,
Event as AcurastEvent, JobId, JobModules, JobRegistrationFor, MultiOrigin, Pallet as Acurast,
Schedule, Script,
};

Expand Down Expand Up @@ -106,14 +106,14 @@ pub fn script() -> Script {
SCRIPT_BYTES.to_vec().try_into().unwrap()
}

fn token_22_funded_account<T: Config>() -> T::AccountId
fn token_22_funded_account<T: Config>(index: u32) -> T::AccountId
where
T: pallet_assets::Config,
<T as pallet_assets::Config>::AssetId: From<u32>,
<T as pallet_assets::Config>::Balance: From<u128>,
{
use pallet_assets::Pallet as Assets;
let caller: T::AccountId = account("token_account", 0, SEED);
let caller: T::AccountId = account("token_account", index, SEED);
whitelist_account!(caller);
let pallet_account: T::AccountId = <T as Config>::PalletId::get().into_account_truncating();
let pallet_origin: T::RuntimeOrigin = RawOrigin::Signed(pallet_account.clone()).into();
Expand Down Expand Up @@ -148,7 +148,7 @@ where
<T as pallet_assets::Config>::Balance: From<u128>,
RewardFor<T>: From<MockAsset>,
{
let caller: T::AccountId = token_22_funded_account::<T>();
let caller: T::AccountId = token_22_funded_account::<T>(0);
whitelist_account!(caller);

let ad = advertisement::<T>(10000, 5);
Expand All @@ -173,7 +173,7 @@ where
<T as pallet_assets::Config>::Balance: From<u128>,
RewardFor<T>: From<MockAsset>,
{
let caller: T::AccountId = token_22_funded_account::<T>();
let caller: T::AccountId = token_22_funded_account::<T>(0);
whitelist_account!(caller);

let job = job_registration_with_reward::<T>(script(), 2, 20100);
Expand All @@ -187,6 +187,37 @@ where
(caller, job)
}

fn acknowledge_match_helper<T: Config>(
) -> Result<(T::AccountId, JobId<T::AccountId>), DispatchError>
where
T: pallet_assets::Config,
<T as Config>::AssetId: From<u32>,
<T as pallet_assets::Config>::AssetId: From<u32>,
<T as pallet_assets::Config>::Balance: From<u128>,
RewardFor<T>: From<MockAsset>,
{
let consumer: T::AccountId = token_22_funded_account::<T>(0);
let processor: T::AccountId = token_22_funded_account::<T>(1);
let ad = advertisement::<T>(1, 1000);
assert_ok!(AcurastMarketplace::<T>::advertise(
RawOrigin::Signed(processor.clone()).into(),
ad.clone(),
));
let job = job_registration_with_reward::<T>(script(), 100, 1000);
assert_ok!(Acurast::<T>::register(
RawOrigin::Signed(consumer.clone()).into(),
job.clone()
));

Ok((
processor,
(
MultiOrigin::Acurast(consumer),
Acurast::<T>::job_id_sequence(),
),
))
}

benchmarks! {
where_clause { where
T: pallet_assets::Config + pallet_acurast::Config,
Expand Down Expand Up @@ -242,5 +273,10 @@ benchmarks! {
).into());
}

acknowledge_match {
let (processor, job_id) = acknowledge_match_helper::<T>()?;
let pub_keys: PubKeys = vec![PubKey::SECP256r1([0u8; 33].to_vec().try_into().unwrap()), PubKey::SECP256k1([0u8; 33].to_vec().try_into().unwrap())].try_into().unwrap();
}: _(RawOrigin::Signed(processor.clone()), job_id.clone(), pub_keys)

impl_benchmark_test_suite!(AcurastMarketplace, mock::ExtBuilder::default().build(), mock::Test);
}
15 changes: 14 additions & 1 deletion pallets/marketplace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub(crate) use pallet::STORAGE_VERSION;
pub mod pallet {
use frame_support::{
dispatch::DispatchResultWithPostInfo, ensure, pallet_prelude::*, traits::UnixTime,
Blake2_128, PalletId,
Blake2_128, Blake2_128Concat, PalletId,
};
use frame_system::pallet_prelude::*;
use itertools::Itertools;
Expand Down Expand Up @@ -172,6 +172,11 @@ pub mod pallet {
AssignmentFor<T>,
>;

#[pallet::storage]
#[pallet::getter(fn stored_matches_reverse_index)]
pub type StoredMatchesReverseIndex<T: Config> =
gitsimon marked this conversation as resolved.
Show resolved Hide resolved
StorageMap<_, Blake2_128, JobId<T::AccountId>, T::AccountId>;

#[pallet::event]
#[pallet::generate_deposit(pub (super) fn deposit_event)]
pub enum Event<T: Config> {
Expand Down Expand Up @@ -369,6 +374,7 @@ pub mod pallet {
pub fn acknowledge_match(
origin: OriginFor<T>,
job_id: JobId<T::AccountId>,
pub_keys: PubKeys,
) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin)?;

Expand All @@ -382,6 +388,7 @@ pub mod pallet {
.ok_or(Error::<T>::CannotAcknowledgeWhenNotMatched)?;
let changed = !assignment.acknowledged;
assignment.acknowledged = true;
assignment.pub_keys = pub_keys;
Ok((changed, assignment.to_owned()))
},
)?;
Expand Down Expand Up @@ -574,6 +581,7 @@ pub mod pallet {
// removed completed job from all storage points (completed SLA gets still deposited in event below)
<StoredMatches<T>>::remove(&who, &job_id);
<StoredJobStatus<T>>::remove(&job_id.0, &job_id.1);
<StoredMatchesReverseIndex<T>>::remove(&job_id);

// increase capacity
<StoredStorageCapacity<T>>::mutate(&who, |c| {
Expand Down Expand Up @@ -905,13 +913,18 @@ pub mod pallet {
total: execution_count,
met: 0,
},
pub_keys: PubKeys::default(),
});
Ok(())
}
}?;
Ok(())
},
)?;
<StoredMatchesReverseIndex<T>>::insert(
&m.job_id,
planned_execution.source.clone(),
);
<StoredStorageCapacity<T>>::set(
&planned_execution.source,
capacity.checked_sub(registration.storage.into()),
Expand Down
11 changes: 10 additions & 1 deletion pallets/marketplace/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ use pallet_acurast::{
};
use reputation::{BetaReputation, ReputationEngine};

use crate::stub::*;
use crate::{
mock::*, AdvertisementRestriction, Assignment, Error, ExecutionResult, JobStatus, Match, SLA,
};
use crate::{stub::*, PubKeys};
use crate::{JobRequirements, PlannedExecution};

#[test]
Expand Down Expand Up @@ -122,6 +122,7 @@ fn test_match() {
assert_ok!(AcurastMarketplace::acknowledge_match(
RuntimeOrigin::signed(processor_account_id()).into(),
job_id.clone(),
PubKeys::default(),
));
assert_eq!(
Some(JobStatus::Assigned(1)),
Expand Down Expand Up @@ -159,6 +160,7 @@ fn test_match() {
},
acknowledged: true,
sla: SLA { total: 2, met: 1 },
pub_keys: PubKeys::default(),
}),
AcurastMarketplace::stored_matches(processor_account_id(), job_id.clone()),
);
Expand Down Expand Up @@ -259,6 +261,7 @@ fn test_match() {
},
acknowledged: true,
sla: SLA { total: 2, met: 0 },
pub_keys: PubKeys::default(),
}
)),
RuntimeEvent::MockPallet(mock_pallet::Event::PayReward(MockAsset {
Expand All @@ -281,6 +284,7 @@ fn test_match() {
},
acknowledged: true,
sla: SLA { total: 2, met: 1 },
pub_keys: PubKeys::default(),
}
)),
RuntimeEvent::MockPallet(mock_pallet::Event::PayReward(MockAsset {
Expand All @@ -303,6 +307,7 @@ fn test_match() {
},
acknowledged: true,
sla: SLA { total: 2, met: 2 },
pub_keys: PubKeys::default(),
}
)),
RuntimeEvent::AcurastMarketplace(crate::Event::JobFinalized(job_id.clone(),)),
Expand Down Expand Up @@ -614,6 +619,7 @@ fn test_more_reports_than_expected() {
assert_ok!(AcurastMarketplace::acknowledge_match(
RuntimeOrigin::signed(processor_account_id()).into(),
job_id.clone(),
PubKeys::default(),
));

// report twice with success
Expand Down Expand Up @@ -679,6 +685,7 @@ fn test_more_reports_than_expected() {
},
acknowledged: true,
sla: SLA { total: 2, met: 0 },
pub_keys: PubKeys::default(),
}
)),
RuntimeEvent::MockPallet(mock_pallet::Event::PayReward(MockAsset {
Expand All @@ -701,6 +708,7 @@ fn test_more_reports_than_expected() {
},
acknowledged: true,
sla: SLA { total: 2, met: 1 },
pub_keys: PubKeys::default(),
}
)),
RuntimeEvent::MockPallet(mock_pallet::Event::PayReward(MockAsset {
Expand All @@ -723,6 +731,7 @@ fn test_more_reports_than_expected() {
},
acknowledged: true,
sla: SLA { total: 2, met: 2 },
pub_keys: PubKeys::default(),
}
)),
]
Expand Down
15 changes: 15 additions & 0 deletions pallets/marketplace/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,21 @@ pub struct Assignment<Reward> {
pub acknowledged: bool,
/// Keeps track of the SLA.
pub sla: SLA,
/// Processor Pub Keys
pub pub_keys: PubKeys,
}

pub const NUMBER_OF_PUB_KEYS: u32 = 2;
pub const PUB_KEYS_MAX_LENGTH: u32 = 33;

/// The public keys of the processor releaved when a job is acknowledge.
pub type PubKeys = BoundedVec<PubKey, ConstU32<NUMBER_OF_PUB_KEYS>>;

/// The public key revealed by a processor.
#[derive(RuntimeDebug, Encode, Decode, MaxEncodedLen, TypeInfo, Clone, PartialEq)]
pub enum PubKey {
SECP256r1(BoundedVec<u8, ConstU32<PUB_KEYS_MAX_LENGTH>>),
SECP256k1(BoundedVec<u8, ConstU32<PUB_KEYS_MAX_LENGTH>>),
}

pub type AssignmentFor<T> = Assignment<RewardFor<T>>;
Expand Down