Skip to content
Draft
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
63 changes: 63 additions & 0 deletions Cargo.lock

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

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ members = [
"worker/common",
"worker/native",
"worker/protocol/dart-v1",
"worker/protocol/testing",
"pallets/asset",
"pallets/base",
"pallets/committee",
Expand Down Expand Up @@ -117,6 +118,8 @@ members = [
"pallets/treasury",
"pallets/utility",
"pallets/weights",
"pallets/worker-modules",
"pallets/worker-modules/worker-testing",
"primitives",
"primitives_derive",
"primitives/asset-metadata",
Expand All @@ -142,6 +145,7 @@ polymesh-worker-extension = { path = "worker/extension", default-features = fals
polymesh-worker-common = { path = "worker/common", default-features = false }
polymesh-worker-native = { path = "worker/native", default-features = false }
polymesh-worker-protocol-dart-v1 = { path = "worker/protocol/dart-v1", default-features = false }
polymesh-worker-protocol-testing = { path = "worker/protocol/testing", default-features = false }
schnellru = { version = "0.2.4", default-features = false }
rayon = { version = "1.8" }

Expand Down Expand Up @@ -177,6 +181,8 @@ polymesh-transaction-payment = { path = "pallets/transaction-payment", default-f
pallet-treasury = { path = "pallets/treasury", default-features = false }
pallet-utility = { path = "pallets/utility", default-features = false }
pallet-precompiles = { path = "pallets/precompiles", default-features = false }
pallet-worker-modules = { path = "pallets/worker-modules", default-features = false }
pallet-worker-testing = { path = "pallets/worker-modules/worker-testing", default-features = false }

# Common
polymesh-runtime-common = { path = "pallets/runtime/common", default-features = false }
Expand Down
3 changes: 3 additions & 0 deletions integration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ pub use asset_helper::*;
#[cfg(feature = "current_release")]
pub mod confidential_assets_helper;

#[cfg(feature = "current_release")]
pub mod worker_modules_helper;

#[cfg(feature = "current_release")]
pub mod contracts;

Expand Down
135 changes: 135 additions & 0 deletions integration/src/worker_modules_helper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
use anyhow::Result;

use polymesh_api::{types::polymesh_worker_common::BackendModuleDefinition, Api};
use polymesh_api_tester::{DbAccountSigner, PolymeshTester};

use codec::Encode;
pub use polymesh_api::types::{
pallet_worker_modules::*,
polymesh_worker_common::{BackendModuleKind, Protocol, ProtocolId, ProtocolVersion},
runtime::RuntimeCall,
};
use sp_core::blake2_256;

pub struct WorkerModulesHelper {
pub api: Api,
pub protocol: Protocol,
sudo: DbAccountSigner,
}

impl WorkerModulesHelper {
pub fn new(tester: &PolymeshTester, protocol: Protocol) -> Self {
let api = tester.api.clone();
let sudo = tester
.sudo
.as_ref()
.expect("Sudo account not found")
.clone();
Self {
api,
protocol,
sudo,
}
}

pub fn update_version(&mut self, version: ProtocolVersion) {
self.protocol.version = version;
}

pub async fn sudo_call<C: Into<RuntimeCall>>(&mut self, call: C) -> Result<()> {
let mut res = self
.api
.call()
.sudo()
.sudo(call.into())?
.submit_and_watch(&mut self.sudo)
.await?;
res.wait_finalized().await?;
Ok(())
}

pub async fn register_protocol(
&mut self,
name: &str,
description: &str,
version: ProtocolVersion,
) -> Result<()> {
self.sudo_call(self.api.call().worker_modules().register_protocol(
self.protocol.id.clone(),
ProtocolMetadata {
protocol_name: name.as_bytes().to_vec(),
protocol_description: description.as_bytes().to_vec(),
protocol_version: version,
},
)?)
.await?;

Ok(())
}

pub async fn upload_module_code(&mut self, module_code: Vec<u8>) -> Result<()> {
self.sudo_call(
self.api
.call()
.worker_modules()
.upload_protocol_module_code(self.protocol.clone(), module_code)?,
)
.await?;

Ok(())
}

pub async fn upload_module_context(&mut self, module_context: Vec<u8>) -> Result<()> {
self.sudo_call(
self.api
.call()
.worker_modules()
.upload_protocol_module_context(self.protocol.clone(), module_context)?,
)
.await?;

Ok(())
}

pub async fn upload_config(
&mut self,
init_method: ProtocolInitializationMethod,
modules: Vec<BackendModuleDefinition>,
) -> Result<()> {
let config = ProtocolModuleConfig {
protocol: self.protocol.clone(),
initialization_method: init_method.clone(),
modules: modules.clone(),
};
self.sudo_call(
self.api
.call()
.worker_modules()
.upload_protocol_module_config(config)?,
)
.await?;

Ok(())
}

pub async fn upload_modules_and_config(
&mut self,
init_method: ProtocolInitializationMethod,
modules: Vec<(BackendModuleKind, u32, Vec<u8>)>,
) -> Result<()> {
let mut module_defs = Vec::new();
for (module_kind, module_version, code) in modules.into_iter() {
let code_hash = code.using_encoded(blake2_256);
self.upload_module_code(code).await?;
module_defs.push(BackendModuleDefinition {
module_kind,
module_version: module_version,
code_hash,
});
}

self.upload_config(init_method, module_defs).await?;

Ok(())
}
}
Loading