Skip to content

Commit

Permalink
fix(sdk)!: allow for zero-based blueprint_id (#426)
Browse files Browse the repository at this point in the history
  • Loading branch information
shekohex authored Nov 1, 2024
1 parent 2efdcc6 commit bdc4ec6
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 25 deletions.
4 changes: 2 additions & 2 deletions blueprint-manager/src/executor/event_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ impl Debug for VerifiedBlueprint<'_> {
}
}

pub async fn handle_services<'a>(
blueprints: &[VerifiedBlueprint<'a>],
pub async fn handle_services(
blueprints: &[VerifiedBlueprint<'_>],
gadget_config: &GadgetConfig,
blueprint_manager_opts: &BlueprintManagerConfig,
active_gadgets: &mut ActiveGadgets,
Expand Down
4 changes: 2 additions & 2 deletions blueprint-manager/src/sources/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ pub trait BinarySourceFetcher: Send + Sync {
fn name(&self) -> String;
}

pub async fn handle<'a>(
blueprint: &VerifiedBlueprint<'a>,
pub async fn handle(
blueprint: &VerifiedBlueprint<'_>,
gadget_config: &GadgetConfig,
blueprint_manager_opts: &BlueprintManagerConfig,
active_gadgets: &mut ActiveGadgets,
Expand Down
8 changes: 6 additions & 2 deletions macros/context-derive/src/services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,17 @@ pub fn generate_context_impl(
gadget_sdk::config::ProtocolSpecificSettings::Tangle(settings) => settings.service_id,
_ => return Err(subxt::Error::Other("Service instance id is only available for Tangle protocol".to_string())),
};
let service_instance = api::storage().services().instances(service_instance_id);
let service_id = match service_instance_id {
Some(service_instance_id) => service_instance_id,
None => return Err(subxt::Error::Other("Service instance id is not set. Running in Registration mode?".to_string())),
};
let service_instance = api::storage().services().instances(service_id);
let storage = client.storage().at_latest().await?;
let result = storage.fetch(&service_instance).await?;
match result {
Some(instance) => Ok(instance.operators.0),
None => Err(subxt::Error::Other(format!(
"Service instance {service_instance_id} is not created, yet"
"Service instance {service_id} is not created, yet"
))),
}
}
Expand Down
4 changes: 2 additions & 2 deletions sdk/src/config/gadget_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl<RwLock: lock_api::RawRwLock> Default for GadgetConfiguration<RwLock> {
protocol: Protocol::Tangle,
protocol_specific: ProtocolSpecificSettings::Tangle(TangleInstanceSettings {
blueprint_id: 0,
service_id: 0,
service_id: Some(0),
}),
bind_port: 0,
bind_addr: core::net::IpAddr::V4(core::net::Ipv4Addr::new(127, 0, 0, 1)),
Expand Down Expand Up @@ -213,6 +213,6 @@ impl<RwLock: lock_api::RawRwLock> GadgetConfiguration<RwLock> {
pub fn service_id(&self) -> Option<u64> {
let tangle_settings = self.protocol_specific.tangle().ok()?;
let TangleInstanceSettings { service_id, .. } = tangle_settings;
Some(*service_id)
*service_id
}
}
7 changes: 6 additions & 1 deletion sdk/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,12 @@ fn load_inner<RwLock: lock_api::RawRwLock>(
}),
Protocol::Tangle => ProtocolSpecificSettings::Tangle(TangleInstanceSettings {
blueprint_id: blueprint_id.ok_or(Error::MissingBlueprintId)?,
service_id: service_id.ok_or(Error::MissingServiceId)?,
// If we are in registration mode, we don't need a service id
service_id: if !is_registration {
Some(service_id.ok_or(Error::MissingServiceId)?)
} else {
None
},
}),
};

Expand Down
4 changes: 3 additions & 1 deletion sdk/src/config/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ pub struct TangleInstanceSettings {
/// The blueprint ID for the Tangle blueprint
pub blueprint_id: u64,
/// The service ID for the Tangle blueprint instance
pub service_id: u64,
///
/// Note: This will be `None` in case this gadget is running in Registration Mode.
pub service_id: Option<u64>,
}

#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/network/gossip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub struct NetworkService<'a> {
pub span: &'a tracing::Span,
}

impl<'a> NetworkService<'a> {
impl NetworkService<'_> {
/// Handle local requests that are meant to be sent to the network.
pub(crate) fn handle_intra_node_payload(&mut self, msg: IntraNodePayload) {
let _enter = self.span.enter();
Expand Down
14 changes: 0 additions & 14 deletions sdk/src/runners/tangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,6 @@ impl BlueprintConfig for TangleConfig {
}
};

// Ensure blueprint_id is set
if blueprint_id == 0 {
return Err(RunnerError::ConfigError(
crate::config::Error::MissingBlueprintId,
));
}

// Check if the operator is already registered
let client = env.client().await?;
let signer = env.first_sr25519_signer()?;
Expand Down Expand Up @@ -101,13 +94,6 @@ impl BlueprintConfig for TangleConfig {
};
let blueprint_id = blueprint_settings.blueprint_id;

// Ensure blueprint_id is set
if blueprint_id == 0 {
return Err(RunnerError::ConfigError(
crate::config::Error::MissingBlueprintId,
));
}

let xt = api::tx().services().register(
blueprint_id,
services::OperatorPreferences {
Expand Down

0 comments on commit bdc4ec6

Please sign in to comment.