Skip to content

Commit

Permalink
feat:task management pallet and spec upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
ZCalz committed Jul 27, 2024
1 parent 0881a22 commit f2ee7b1
Show file tree
Hide file tree
Showing 11 changed files with 1,203 additions and 1 deletion.
Binary file not shown.
18 changes: 18 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions container-chains/runtime-templates/simple/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ runtime-common = { workspace = true }
pallet-worker-registration = { verson = "0.1.0", default-features = false, path = "../../../pallets/worker-registration" }
pallet-faucet = { verson = "1.0.3", default-features = false, path = "../../../pallets/faucet" }
pallet-edge-connect = { verson = "0.1.0", default-features = false, path = "../../../pallets/edge-connect" }
pallet-task-management = { verson = "0.1.0", default-features = false, path = "../../../pallets/task-management" }

# Moonkit
async-backing-primitives = { workspace = true }
Expand Down
9 changes: 8 additions & 1 deletion container-chains/runtime-templates/simple/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ use {
pub use pallet_worker_registration;
pub use pallet_faucet;
pub use pallet_edge_connect;
pub use pallet_task_management;

pub mod xcm_config;

Expand Down Expand Up @@ -222,7 +223,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("container-chain-template"),
impl_name: create_runtime_str!("container-chain-template"),
authoring_version: 1,
spec_version: 701,
spec_version: 704,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down Expand Up @@ -671,6 +672,11 @@ impl pallet_edge_connect::Config for Runtime {
type WeightInfo = ();
}

impl pallet_task_management::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type WeightInfo = ();
}

// implement `CreateSignedTransaction` to allow `create_transaction` of offchain worker for runtime
impl<LocalCall> frame_system::offchain::CreateSignedTransaction<LocalCall> for Runtime
where
Expand Down Expand Up @@ -774,6 +780,7 @@ construct_runtime!(
WorkerRegistration: pallet_worker_registration = 120,

EdgeConnect: pallet_edge_connect = 122,
TaskManagement: pallet_task_management = 123,

// Testing
Faucet: pallet_faucet::{Pallet, Call, Storage, Event<T>} = 200,
Expand Down
53 changes: 53 additions & 0 deletions pallets/task-management/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
[package]
name = "pallet-task-management"
description = "Creation and management of tasks for workers to consume"
version = "0.1.0"
authors.workspace = true
repository.workspace = true
edition = "2021"
publish = false

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
parity-scale-codec = { workspace = true, default-features = false, features = [
"derive",
] }
scale-info = { workspace = true , default-features = false, features = ["derive"] }

# frame deps
frame-benchmarking = { vworkspace = true, optional = true }
frame-support = { workspace = true }
frame-system = { workspace = true }

# Local
pallet-edge-connect = { verson = "0.1.0", default-features = false, path = "../edge-connect" }

sp-core = { workspace = true }
sp-io = { workspace = true }
log = { workspace = true }

[dev-dependencies]
sp-runtime = { workspace = true }
sp-std = { workspace = true }

[features]
default = ["std"]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
]
std = [
"parity-scale-codec/std",
"frame-benchmarking?/std",
"frame-support/std",
"frame-system/std",
"scale-info/std",
"pallet-edge-connect/std",
]
try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
]
158 changes: 158 additions & 0 deletions pallets/task-management/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
//! Benchmarking setup for pallet-task-management
use super::*;
use crate::{Pallet as TaskManagementModule, Config};
use frame_benchmarking::{benchmarks, account, whitelisted_caller};
use frame_support::BoundedVec;
use sp_core::H256;
use frame_system::RawOrigin;

benchmarks! {
task_scheduler {
let s in 0 .. 100;
let caller: T::AccountId = whitelisted_caller();

// Register a worker for the caller
let api_info = pallet_edge_connect::types::WorkerAPI {
ip: None,
domain: Some(BoundedVec::try_from(b"https://api-worker.testing".to_vec()).unwrap())
};
pallet_edge_connect::Pallet::<T>::register_worker(RawOrigin::Signed(caller.clone()).into(), api_info.ip, api_info.domain)?;

let task_data = BoundedVec::try_from(b"some-docker-imgv.0".to_vec()).unwrap();
}: _(RawOrigin::Signed(caller.clone()), task_data.clone())
verify {
let task_id = TaskManagementModule::<T>::next_task_id() - 1;
let task_info = TaskManagementModule::<T>::get_tasks(task_id).unwrap();
assert_eq!(task_info.metadata, task_data);
assert_eq!(task_info.task_owner, caller);
}

submit_completed_task {
let s in 0 .. 100;
let caller: T::AccountId = whitelisted_caller();

// Register a worker for the executor
let executor: T::AccountId = account("executor", 0, 0);
let api_info = pallet_edge_connect::types::WorkerAPI {
ip: None,
domain: Some(BoundedVec::try_from(b"https://api-worker.testing".to_vec()).unwrap())
};
pallet_edge_connect::Pallet::<T>::register_worker(RawOrigin::Signed(executor.clone()).into(), api_info.ip, api_info.domain)?;

// Create a task data
let task_data = BoundedVec::try_from(b"some-docker-imgv.0".to_vec()).unwrap();
TaskManagementModule::<T>::task_scheduler(RawOrigin::Signed(caller.clone()).into(), task_data.clone())?;

// Register a worker for the verifier
let verifier: T::AccountId = account("verifier", 0, 0);
let api_info_verifier = pallet_edge_connect::types::WorkerAPI {
ip: None,
domain: Some(BoundedVec::try_from(b"https://api-worker2.testing".to_vec()).unwrap())
};
pallet_edge_connect::Pallet::<T>::register_worker(RawOrigin::Signed(verifier.clone()).into(), api_info_verifier.ip, api_info_verifier.domain)?;

let task_id = TaskManagementModule::<T>::next_task_id() - 1;
let completed_hash = H256([123; 32]);
}: _(RawOrigin::Signed(executor.clone()), task_id, completed_hash)
verify {
let task_status = TaskManagementModule::<T>::task_status(task_id).unwrap();
assert_eq!(task_status, TaskStatusType::PendingValidation);

let verifications = TaskManagementModule::<T>::task_verifications(task_id).unwrap();
assert_eq!(verifications.executor.account, executor);
assert_eq!(verifications.executor.completed_hash, Some(completed_hash));
assert_eq!(verifications.verifier.clone().unwrap().account, verifier);
assert_eq!(verifications.verifier.clone().unwrap().completed_hash, None);
}

verify_completed_task {
let s in 0 .. 100;
let caller: T::AccountId = whitelisted_caller();
let executor: T::AccountId = account("executor", 0, 0);
let verifier: T::AccountId = account("verifier", 0, 0);

// Register a worker for the executor
let api_info_executor = pallet_edge_connect::types::WorkerAPI {
ip: None,
domain: Some(BoundedVec::try_from(b"https://api-worker.testing".to_vec()).unwrap())
};
pallet_edge_connect::Pallet::<T>::register_worker(RawOrigin::Signed(executor.clone()).into(), api_info_executor.ip, api_info_executor.domain)?;

// Create a task data
let task_data = BoundedVec::try_from(b"some-docker-imgv.0".to_vec()).unwrap();
TaskManagementModule::<T>::task_scheduler(RawOrigin::Signed(caller.clone()).into(), task_data.clone())?;

// Register a worker for the verifier
let api_info_verifier = pallet_edge_connect::types::WorkerAPI {
ip: None,
domain: Some(BoundedVec::try_from(b"https://api-worker2.testing".to_vec()).unwrap())
};
pallet_edge_connect::Pallet::<T>::register_worker(RawOrigin::Signed(verifier.clone()).into(), api_info_verifier.ip, api_info_verifier.domain)?;

let task_id = TaskManagementModule::<T>::next_task_id() - 1;

let completed_hash = H256([123; 32]);
TaskManagementModule::<T>::submit_completed_task(RawOrigin::Signed(executor.clone()).into(), task_id, completed_hash)?;

}: _(RawOrigin::Signed(verifier.clone()), task_id, completed_hash)
verify {
let task_status = TaskManagementModule::<T>::task_status(task_id).unwrap();
assert_eq!(task_status, TaskStatusType::Completed);
}

resolve_completed_task {
let s in 0 .. 100;
let caller: T::AccountId = whitelisted_caller();

let executor: T::AccountId = account("executor", 0, 0);
let verifier: T::AccountId = account("verifier", 0, 0);
let resolver: T::AccountId = account("resolver", 0, 0);

// Register a worker for the executor
let api_info_executor = pallet_edge_connect::types::WorkerAPI {
ip: None,
domain: Some(BoundedVec::try_from(b"https://api-worker.testing".to_vec()).unwrap())
};
pallet_edge_connect::Pallet::<T>::register_worker(RawOrigin::Signed(executor.clone()).into(), api_info_executor.ip, api_info_executor.domain)?;

// Create a task data
let task_data = BoundedVec::try_from(b"some-docker-imgv.0".to_vec()).unwrap();
TaskManagementModule::<T>::task_scheduler(RawOrigin::Signed(caller.clone()).into(), task_data.clone())?;
let task_id = TaskManagementModule::<T>::next_task_id() - 1;

// Register a worker for the verifier
let api_info_verifier = pallet_edge_connect::types::WorkerAPI {
ip: None,
domain: Some(BoundedVec::try_from(b"https://api-worker2.testing".to_vec()).unwrap())
};
pallet_edge_connect::Pallet::<T>::register_worker(RawOrigin::Signed(verifier.clone()).into(), api_info_verifier.ip, api_info_verifier.domain)?;

// Submit completed task by the executor
let completed_hash = H256([4; 32]);
TaskManagementModule::<T>::submit_completed_task(RawOrigin::Signed(executor.clone()).into(), task_id, completed_hash)?;

// Register a worker for the resolver
let api_info_resolver = pallet_edge_connect::types::WorkerAPI {
ip: None,
domain: Some(BoundedVec::try_from(b"https://api-worker3.testing".to_vec()).unwrap())
};
pallet_edge_connect::Pallet::<T>::register_worker(RawOrigin::Signed(resolver.clone()).into(), api_info_resolver.ip, api_info_resolver.domain)?;

let completed_differing_hash = H256([123; 32]);
// Verify completed task by the verifier
TaskManagementModule::<T>::verify_completed_task(RawOrigin::Signed(verifier.clone()).into(), task_id, completed_differing_hash)?;

let verifications = TaskManagementModule::<T>::task_verifications(task_id).unwrap();
assert_eq!(verifications.executor.account, executor);
assert_eq!(verifications.executor.completed_hash, Some(completed_hash));
assert_eq!(verifications.verifier.clone().unwrap().account, verifier);
assert_eq!(verifications.verifier.clone().unwrap().completed_hash, Some(completed_differing_hash));

}: _(RawOrigin::Signed(resolver.clone()), task_id, completed_differing_hash)
verify {
let task_status = TaskManagementModule::<T>::task_status(task_id).unwrap();
assert_eq!(task_status, TaskStatusType::Completed);
}

impl_benchmark_test_suite!(TaskManagementModule, crate::mock::new_test_ext(), crate::mock::Test);
}
Loading

0 comments on commit f2ee7b1

Please sign in to comment.