Skip to content

Commit

Permalink
feat: constructors for tangle and EVM
Browse files Browse the repository at this point in the history
  • Loading branch information
tbraun96 committed Nov 5, 2024
1 parent eacf856 commit 533489c
Show file tree
Hide file tree
Showing 20 changed files with 614 additions and 382 deletions.
583 changes: 410 additions & 173 deletions Cargo.lock

Large diffs are not rendered by default.

Submodule forge-std updated 1 files
+1 −1 package.json
7 changes: 2 additions & 5 deletions blueprints/incredible-squaring-eigenlayer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,8 @@ async fn main() {
client: aggregator_client,
env: env.clone(),
};
let x_square_eigen = XsquareEigenEventHandler {
ctx,
contract: contract.clone(),
contract_instance: Default::default(),
};

let x_square_eigen = XsquareEigenEventHandler::new(contract.clone(), ctx);

let aggregator_context = AggregatorContext::new(
server_address,
Expand Down
6 changes: 1 addition & 5 deletions blueprints/incredible-squaring-symbiotic/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@ async fn main() {
provider,
);

let x_square = blueprint::XsquareEventHandler {
context: blueprint::MyContext {},
contract: contract.clone(),
contract_instance: Default::default(),
};
let x_square = blueprint::XsquareEventHandler::new(contract, blueprint::MyContext {});

info!("~~~ Executing the incredible squaring blueprint ~~~");
let symb_config = SymbioticConfig::default();
Expand Down
2 changes: 1 addition & 1 deletion blueprints/incredible-squaring/contracts/lib/forge-std
Submodule forge-std updated 1 files
+1 −1 package.json
17 changes: 6 additions & 11 deletions blueprints/incredible-squaring/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use color_eyre::{eyre::eyre, Result};
use color_eyre::Result;
use gadget_sdk::info;
use gadget_sdk::runners::tangle::TangleConfig;
use gadget_sdk::runners::BlueprintRunner;
Expand All @@ -7,17 +7,12 @@ use incredible_squaring_blueprint as blueprint;

#[gadget_sdk::main(env)]
async fn main() {
let client = env.client().await.map_err(|e| eyre!(e))?;
let signer = env.first_sr25519_signer().map_err(|e| eyre!(e))?;
let x_square = blueprint::XsquareEventHandler::new(&env, blueprint::MyContext).await?;

info!("Starting the event watcher for {} ...", signer.account_id());

let x_square = blueprint::XsquareEventHandler {
service_id: env.service_id().expect("No service ID found"),
context: blueprint::MyContext,
client,
signer,
};
info!(
"Starting the event watcher for {} ...",
x_square.signer.account_id()
);

info!("~~~ Executing the incredible squaring blueprint ~~~");
let tangle_config = TangleConfig::default();
Expand Down
7 changes: 1 addition & 6 deletions blueprints/tangle-raw-event-listener/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@ async fn main() {

info!("Starting the event watcher for {} ...", signer.account_id());

let x_square = blueprint::RawEventHandler {
service_id: env.service_id().expect("No service ID found"),
context: blueprint::MyContext,
client,
signer,
};
let x_square = blueprint::RawEventHandler::new(&env, blueprint::MyContext).await?;

let tangle_config = TangleConfig::default();

Expand Down
22 changes: 0 additions & 22 deletions blueprints/tangle-raw/Cargo.toml

This file was deleted.

5 changes: 0 additions & 5 deletions blueprints/tangle-raw/build.rs

This file was deleted.

24 changes: 0 additions & 24 deletions blueprints/tangle-raw/src/lib.rs

This file was deleted.

37 changes: 0 additions & 37 deletions blueprints/tangle-raw/src/main.rs

This file was deleted.

51 changes: 0 additions & 51 deletions macros/blueprint-proc-macro/src/event_listener/tangle.rs

This file was deleted.

33 changes: 22 additions & 11 deletions macros/blueprint-proc-macro/src/job.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::event_listener::evm::{
generate_evm_event_handler, get_evm_instance_data, get_evm_job_processor_wrapper,
use crate::shared::{pascal_case, type_to_field_type};
use crate::special_impls::evm::{
generate_evm_specific_impl, get_evm_instance_data, get_evm_job_processor_wrapper,
};
use crate::event_listener::tangle::{
generate_additional_tangle_logic, get_tangle_job_processor_wrapper,
use crate::special_impls::tangle::{
generate_tangle_specific_impl, get_tangle_job_processor_wrapper,
};
use crate::shared::{pascal_case, type_to_field_type};
use gadget_blueprint_proc_macro_core::{FieldType, JobDefinition, JobMetadata};
use indexmap::{IndexMap, IndexSet};
use proc_macro::TokenStream;
Expand Down Expand Up @@ -77,7 +77,13 @@ pub(crate) fn job_impl(args: &JobArgs, input: &ItemFn) -> syn::Result<TokenStrea
proc_macro2::TokenStream::default()
} else {
// Specialized code for the event workflow or otherwise
generate_additional_logic(input, args, SUFFIX)
generate_additional_logic(
input,
&args.event_listener,
SUFFIX,
&param_map,
&args.params,
)
};

let autogen_struct = if args.skip_codegen {
Expand Down Expand Up @@ -626,16 +632,21 @@ fn get_asyncness(input: &ItemFn) -> proc_macro2::TokenStream {
#[allow(clippy::too_many_lines)]
pub fn generate_additional_logic(
input: &ItemFn,
job_args: &JobArgs,
event_listener_args: &EventListenerArgs,
suffix: &str,
param_map: &IndexMap<Ident, Type>,
job_params: &[Ident],
) -> proc_macro2::TokenStream {
let (_fn_name, _fn_name_string, struct_name) = generate_fn_name_and_struct(input, suffix);
let event_listener_args = &job_args.event_listener;

match job_args.event_listener.get_event_listener().listener_type {
ListenerType::Evm => generate_evm_event_handler(&struct_name, event_listener_args),
match event_listener_args.get_event_listener().listener_type {
ListenerType::Evm => {
generate_evm_specific_impl(&struct_name, event_listener_args, param_map, job_params)
}

ListenerType::Tangle => generate_additional_tangle_logic(&struct_name),
ListenerType::Tangle => {
generate_tangle_specific_impl(&struct_name, param_map, job_params, event_listener_args)
}

ListenerType::Custom => proc_macro2::TokenStream::default(),
}
Expand Down
2 changes: 1 addition & 1 deletion macros/blueprint-proc-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ mod report;
/// Shared utilities for the Blueprint Macros
mod shared;

mod event_listener;
mod special_impls;

/// Utilities for Tangle Blueprint macro generation
mod tangle;
Expand Down
29 changes: 18 additions & 11 deletions macros/blueprint-proc-macro/src/report.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::job::{
declared_params_to_field_types, generate_autogen_struct, get_current_call_id_field_name,
get_job_id_field_name, get_result_type, EventListenerArgs, ResultsKind,
declared_params_to_field_types, generate_additional_logic, generate_autogen_struct,
get_current_call_id_field_name, get_job_id_field_name, get_result_type, EventListenerArgs,
ResultsKind,
};
use crate::shared::{pascal_case, type_to_field_type};
use gadget_blueprint_proc_macro_core::{
Expand Down Expand Up @@ -131,15 +132,20 @@ pub(crate) fn report_impl(args: &ReportArgs, input: &ItemFn) -> syn::Result<Toke
suffix,
&event_listener_calls,
);
/*let event_handler_gen = if args.skip_codegen {
proc_macro2::TokenStream::new()

// Generate Event Workflow, if not being skipped
let additional_specific_logic = if args.skip_codegen {
proc_macro2::TokenStream::default()
} else {
if let ReportType::QoS = args.report_type {
generate_qos_report_event_handler(args, input, &params_type, &result_type)
} else {
proc_macro2::TokenStream::new()
}
};*/
// Specialized code for the event workflow or otherwise
generate_additional_logic(
input,
&args.event_listeners,
suffix,
&param_types,
&args.params,
)
};

let call_id_static_name = get_current_call_id_field_name(input);
let job_id = if let Some(job_id) = job_id {
Expand Down Expand Up @@ -171,6 +177,8 @@ pub(crate) fn report_impl(args: &ReportArgs, input: &ItemFn) -> syn::Result<Toke
#[allow(unused_variables)]
#input

#additional_specific_logic

#(#event_listener_gen)*
};

Expand Down Expand Up @@ -203,7 +211,6 @@ pub(crate) struct ReportArgs {
verifier: Verifier,
/// Optional: Event handler type for the report.
/// `#[report(event_handler_type = "tangle")]`
#[allow(dead_code)]
pub(crate) event_listeners: EventListenerArgs,
/// Optional: Skip code generation for this report.
/// `#[report(skip_codegen)]`
Expand Down
26 changes: 12 additions & 14 deletions macros/blueprint-proc-macro/src/shared.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use gadget_blueprint_proc_macro_core::FieldType;
use indexmap::IndexMap;
use quote::ToTokens;
use syn::{Ident, Type};

Expand Down Expand Up @@ -132,20 +133,17 @@ pub fn path_to_field_type(path: &syn::Path) -> syn::Result<FieldType> {
}
}

#[allow(dead_code)]
pub fn parse_struct_fields(fields: &syn::Fields) -> syn::Result<Vec<(String, FieldType)>> {
fields
.iter()
.map(|field| {
let name = field
.ident
.as_ref()
.ok_or_else(|| syn::Error::new_spanned(field, "Unnamed fields are not supported"))?
.to_string();
let field_type = type_to_field_type(&field.ty)?;
Ok((name, field_type))
})
.collect()
/// Returns the set of arguments which are not job-related arguments. These typically go into the
/// autogenerated job struct
pub fn get_non_job_arguments(
param_map: &IndexMap<Ident, Type>,
job_params: &[Ident],
) -> IndexMap<Ident, Type> {
param_map
.clone()
.into_iter()
.filter(|r| !job_params.contains(&r.0))
.collect::<IndexMap<Ident, Type>>()
}

#[cfg(test)]
Expand Down
Loading

0 comments on commit 533489c

Please sign in to comment.