Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update to latest changes in tangle #367

Merged
merged 6 commits into from
Oct 22, 2024
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
7 changes: 4 additions & 3 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ cargo-tangle = { path = "./cli", version = "0.1.2" }
cargo_metadata = { version = "0.18.1" }

# Tangle-related dependencies
tangle-subxt = { version = "0.3.0", default-features = false }
tangle-subxt = { version = "0.4.0", default-features = false }
subxt-signer = { version = "0.37.0", default-features = false }
subxt = { version = "0.37.0", default-features = false }
subxt-core = { version = "0.37.0", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions blueprint-metadata/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ repository.workspace = true
[dependencies]
typed-builder = { workspace = true }
gadget-blueprint-proc-macro-core = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
rustdoc-types = { workspace = true }
cargo_metadata = { workspace = true }
Expand Down
103 changes: 42 additions & 61 deletions blueprint-metadata/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use std::{
process::{Command, Stdio},
};

use cargo_metadata::{Metadata, Package};
use gadget_blueprint_proc_macro_core::{
FieldType, Gadget, GadgetSource, GadgetSourceFetcher, JobDefinition, JobResultVerifier,
NativeGadget, ServiceBlueprint, ServiceMetadata, ServiceRegistrationHook, ServiceRequestHook,
TestFetcher,
BlueprintManager, FieldType, Gadget, GadgetSource, GadgetSourceFetcher, JobDefinition,
NativeGadget, ServiceBlueprint, ServiceMetadata, TestFetcher,
};

use rustdoc_types::{Crate, Id, Item, ItemEnum, Module};
Expand Down Expand Up @@ -37,7 +37,11 @@ impl Config {
let jobs = extract_jobs(&krate);
eprintln!("Extracted {} job definitions", jobs.len());
let hooks = extract_hooks(&krate);
let gadget = generate_gadget();
let metadata = extract_metadata();
let crate_name = std::env::var("CARGO_PKG_NAME").expect("Failed to get package name");
let package = find_package(&metadata, &crate_name);
let gadget = generate_gadget(package);
let manager = extract_blueprint_manager(package);
eprintln!("Generating blueprint.json to {:?}", output_file);
let blueprint = ServiceBlueprint {
metadata: ServiceMetadata {
Expand All @@ -53,27 +57,14 @@ impl Config {
license: std::env::var("CARGO_PKG_LICENSE").map(Into::into).ok(),
},
jobs,
registration_hook: hooks
.iter()
.find_map(|hook| match hook {
Hook::Registration(hook) => Some(hook.clone()),
_ => None,
})
.unwrap_or_default(),
manager,
registration_params: hooks
.iter()
.find_map(|hook| match hook {
Hook::RegistrationParams(params) => Some(params.clone()),
_ => None,
})
.unwrap_or_default(),
request_hook: hooks
.iter()
.find_map(|hook| match hook {
Hook::Request(hook) => Some(hook.clone()),
_ => None,
})
.unwrap_or_default(),
request_params: hooks
.iter()
.find_map(|hook| match hook {
Expand All @@ -90,9 +81,7 @@ impl Config {
}

enum Hook {
Registration(ServiceRegistrationHook),
RegistrationParams(Vec<FieldType>),
Request(ServiceRequestHook),
RequestParams(Vec<FieldType>),
}

Expand Down Expand Up @@ -156,9 +145,6 @@ fn extract_jobs_from_module<'a>(
serde_json::from_str(&unescape_json_string(&c.expr))
.expect("Failed to deserialize job definition");
job_def.metadata.description = linked_function.docs.as_ref().map(Into::into);
if let JobResultVerifier::Evm(c) = &mut job_def.verifier {
*c = resolve_evm_contract_path_by_name(c).display().to_string();
}
jobs.push(job_def);
}
_ => continue,
Expand All @@ -171,8 +157,6 @@ fn extract_jobs_from_module<'a>(
fn extract_hooks_from_module(_root: &Id, index: &HashMap<Id, Item>, module: &Module) -> Vec<Hook> {
let mut hooks = vec![];
let automatically_derived: String = String::from("#[automatically_derived]");
const REGISTRATION_HOOK: &str = "REGISTRATION_HOOK";
const REQUEST_HOOK: &str = "REQUEST_HOOK";
const REGISTRATION_HOOK_PARAMS: &str = "REGISTRATION_HOOK_PARAMS";
const REQUEST_HOOK_PARAMS: &str = "REQUEST_HOOK_PARAMS";

Expand All @@ -182,38 +166,6 @@ fn extract_hooks_from_module(_root: &Id, index: &HashMap<Id, Item>, module: &Mod
ItemEnum::Module(m) => {
hooks.extend(extract_hooks_from_module(_root, index, m));
}
ItemEnum::Constant { const_: c, .. }
if item.attrs.contains(&automatically_derived)
&& item
.name
.as_ref()
.map(|v| v.eq(REGISTRATION_HOOK))
.unwrap_or(false) =>
{
let mut value = serde_json::from_str(&unescape_json_string(&c.expr))
.expect("Failed to deserialize hook");
if let ServiceRegistrationHook::Evm(c) = &mut value {
*c = resolve_evm_contract_path_by_name(c).display().to_string();
}
hooks.push(Hook::Registration(value));
}

ItemEnum::Constant { const_: c, .. }
if item.attrs.contains(&automatically_derived)
&& item
.name
.as_ref()
.map(|v| v.eq(REQUEST_HOOK))
.unwrap_or(false) =>
{
let mut value = serde_json::from_str(&unescape_json_string(&c.expr))
.expect("Failed to deserialize hook");
if let ServiceRequestHook::Evm(c) = &mut value {
*c = resolve_evm_contract_path_by_name(c).display().to_string();
}
hooks.push(Hook::Request(value));
}

ItemEnum::Constant { const_: c, .. }
if item.attrs.contains(&automatically_derived)
&& item
Expand Down Expand Up @@ -304,10 +256,8 @@ impl Drop for LockFile {
}
}

/// Generates the metadata for the gadget.
fn generate_gadget() -> Gadget<'static> {
fn extract_metadata() -> Metadata {
let root = std::env::var("CARGO_MANIFEST_DIR").expect("Failed to get manifest directory");
let crate_name = std::env::var("CARGO_PKG_NAME").expect("Failed to get package name");
let root = Path::new(&root)
.canonicalize()
.expect("Failed to canonicalize root dir");
Expand All @@ -324,8 +274,39 @@ fn generate_gadget() -> Gadget<'static> {
.no_deps()
.exec()
.expect("Failed to get metadata");
metadata
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
struct BlueprintMetadata {
manager: BlueprintManager,
}

let package = find_package(&metadata, &crate_name);
fn extract_blueprint_manager(package: &Package) -> BlueprintManager {
let Some(blueprint) = package.metadata.get("blueprint") else {
eprintln!("No blueprint metadata found in the Cargo.toml.");
eprintln!("For more information, see:");
eprintln!("<TODO>");
// TODO(@shekohex): make this hard error
return BlueprintManager::Evm(Default::default());
};
let metadata: BlueprintMetadata =
serde_json::from_value(blueprint.clone()).expect("Failed to deserialize gadget.");
match metadata.manager {
BlueprintManager::Evm(manager) => {
let path = resolve_evm_contract_path_by_name(&manager);
BlueprintManager::Evm(path.display().to_string())
}
_ => unreachable!("Unsupported blueprint manager"),
}
}

/// Generates the metadata for the gadget.
fn generate_gadget(package: &Package) -> Gadget<'static> {
let root = std::env::var("CARGO_MANIFEST_DIR").expect("Failed to get manifest directory");
let root = Path::new(&root)
.canonicalize()
.expect("Failed to canonicalize root dir");
let Some(gadget) = package.metadata.get("gadget") else {
eprintln!("No gadget metadata found in the Cargo.toml.");
eprintln!("For more information, see:");
Expand Down
6 changes: 3 additions & 3 deletions blueprint-test-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use api::services::events::JobResultSubmitted;
use blueprint_manager::config::BlueprintManagerConfig;
use blueprint_manager::executor::BlueprintManagerHandle;
use gadget_io::{GadgetConfig, SupportedChains};
use gadget_sdk::clients::tangle::runtime::{TangleClient};
use gadget_sdk::clients::tangle::runtime::TangleClient;
use gadget_sdk::tangle_subxt::tangle_testnet_runtime::api;
use gadget_sdk::tangle_subxt::tangle_testnet_runtime::api::runtime_types;
use gadget_sdk::tangle_subxt::tangle_testnet_runtime::api::services::calls::types::call::{Args, Job};
Expand All @@ -20,7 +20,6 @@ use std::error::Error;
use std::net::IpAddr;
use std::path::{Path, PathBuf};
use std::time::Duration;
use subxt::ext::sp_core::Pair;
use subxt::tx::Signer;
use subxt::utils::AccountId32;
use url::Url;
Expand Down Expand Up @@ -271,8 +270,9 @@ pub async fn register_service(
blueprint_id,
test_nodes.clone(),
test_nodes,
1000,
Default::default(),
Default::default(),
1000,
);
let res = client
.tx()
Expand Down
6 changes: 2 additions & 4 deletions blueprint-test-utils/src/test_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ use blueprint_manager::sdk::entry::SendFuture;
use cargo_tangle::deploy::Opts;
use gadget_sdk::clients::tangle::runtime::TangleClient;
use gadget_sdk::tangle_subxt::subxt::OnlineClient;
use gadget_sdk::tangle_subxt::tangle_testnet_runtime::api::runtime_types;
use gadget_sdk::tangle_subxt::tangle_testnet_runtime::api::runtime_types::tangle_primitives::services::{ApprovalPrefrence, PriceTargets};
use gadget_sdk::tangle_subxt::tangle_testnet_runtime::api::runtime_types::tangle_primitives::services::PriceTargets;
use gadget_sdk::tangle_subxt::tangle_testnet_runtime::api::services::calls::types::register::{Preferences, RegistrationArgs};
use libp2p::Multiaddr;
use std::collections::HashSet;
Expand Down Expand Up @@ -154,11 +153,10 @@ pub async fn new_test_ext_blueprint_manager<

let task = async move {
let keypair = handle.sr25519_id().clone();
let key = runtime_types::sp_core::ecdsa::Public(handle.ecdsa_id().signer().public().0);
let key = handle.ecdsa_id().signer().public().0;

let preferences = Preferences {
key,
approval: ApprovalPrefrence::None,
price_targets: PriceTargets {
cpu: 0,
mem: 0,
Expand Down
3 changes: 3 additions & 0 deletions blueprints/incredible-squaring/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,6 @@ blueprint-metadata = { workspace = true }
[features]
default = ["std"]
std = []

[package.metadata.blueprint]
manager = { evm = "IncredibleSquaringBlueprint" }
2 changes: 1 addition & 1 deletion blueprints/incredible-squaring/contracts/lib/forge-std
Submodule forge-std updated 4 files
+193 −0 CONTRIBUTING.md
+16 −0 README.md
+73 −8 src/Vm.sol
+2 −2 test/Vm.t.sol
Loading
Loading