Skip to content

Commit

Permalink
feat(spells): resolve spell_id by 'spell' and 'self' [NET-419] (#1578)
Browse files Browse the repository at this point in the history
  • Loading branch information
justprosh authored Apr 20, 2023
1 parent e4fd400 commit 65b2b26
Show file tree
Hide file tree
Showing 13 changed files with 197 additions and 66 deletions.
33 changes: 17 additions & 16 deletions Cargo.lock

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

64 changes: 63 additions & 1 deletion crates/particle-node-tests/tests/spells.rs
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,7 @@ async fn spell_call_by_alias() {
(call %init_peer_id% ("alias" "get_u32") ["counter"] counter)
)
(call "{}" ("return" "") [counter])
(call "{}" ("return" "") [counter.$.num])
)
)"#,
client.peer_id
Expand Down Expand Up @@ -1841,3 +1841,65 @@ async fn test_spell_list() {
panic!("expected one array result")
}
}

#[tokio::test]
async fn spell_call_by_default_alias() {
let swarms = make_swarms(1).await;

let mut client = ConnectedClient::connect_to(swarms[0].multiaddr.clone())
.await
.wrap_err("connect client")
.unwrap();

let script = format!(
r#"
(seq
(seq
(seq
(call %init_peer_id% ("spell" "get_u32") ["counter"] counter1)
(seq
(call %init_peer_id% ("srv" "resolve_alias") ["spell"] spell_id1)
(xor
(call %init_peer_id% ("srv" "add_alias") ["spell" spell_id1])
(call %init_peer_id% ("op" "identity") [%last_error%.$.message] error1)
)
)
)
(seq
(call %init_peer_id% ("self" "get_u32") ["counter"] counter2)
(seq
(call %init_peer_id% ("srv" "resolve_alias") ["self"] spell_id2)
(xor
(call %init_peer_id% ("srv" "add_alias") ["self" spell_id2])
(call %init_peer_id% ("op" "identity") [%last_error%.$.message] error2)
)
)
)
)
(call "{}" ("return" "") [counter1.$.num spell_id1 error1 counter2.$.num spell_id2 error2])
)"#,
client.peer_id
);

let config = make_clock_config(2, 1, 0);
let (expected_spell_id, _) = create_spell(&mut client, &script, config, json!({}), None).await;

if let [JValue::Number(counter1), JValue::String(spell_id1), JValue::String(error1), JValue::Number(counter2), JValue::String(spell_id2), JValue::String(error2)] =
client
.receive_args()
.await
.wrap_err("receive")
.unwrap()
.as_slice()
{
assert_ne!(counter1.as_i64().unwrap(), 0);
assert_eq!(spell_id1, &expected_spell_id);
assert!(error1.contains("Cannot add alias 'spell' because it is reserved"));

assert_ne!(counter2.as_i64().unwrap(), 0);
assert_eq!(spell_id2, &expected_spell_id);
assert!(error2.contains("Cannot add alias 'self' because it is reserved"));
} else {
panic!("expected (int, str, str, int, str, str) result");
}
}
1 change: 1 addition & 0 deletions crates/peer-metrics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ log = { workspace = true }
bytesize = "1.2.0"
fluence-app-service = { workspace = true }
fluence-libp2p = { workspace = true }
particle-execution = { workspace = true }

tokio = { workspace = true }
tokio-stream = { workspace = true }
Expand Down
3 changes: 2 additions & 1 deletion crates/peer-metrics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub use connectivity::ConnectivityMetrics;
pub use connectivity::Resolution;
pub use dispatcher::DispatcherMetrics;
pub use info::add_info_metrics;
use particle_execution::ParticleParams;
pub use particle_executor::{FunctionKind, ParticleExecutorMetrics};
pub use services_metrics::{
ServiceCallStats, ServiceMemoryStat, ServiceType, ServicesMetrics, ServicesMetricsBackend,
Expand Down Expand Up @@ -41,7 +42,7 @@ pub enum ParticleType {

impl ParticleType {
fn from_particle(particle_id: &str) -> Self {
if particle_id.starts_with("spell_") {
if ParticleParams::is_spell_particle(particle_id) {
ParticleType::Spell
} else {
ParticleType::Common
Expand Down
23 changes: 14 additions & 9 deletions particle-builtins/src/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,7 @@ where
let mut args = args.function_args.into_iter();
let service_id_or_alias: String = Args::next("service_id_or_alias", &mut args)?;
self.services.remove_service(
&params.id,
params.host_id,
service_id_or_alias,
params.init_peer_id,
Expand All @@ -852,7 +853,9 @@ where
fn get_interface(&self, args: Args, params: ParticleParams) -> Result<JValue, JError> {
let mut args = args.function_args.into_iter();
let service_id: String = Args::next("service_id", &mut args)?;
Ok(self.services.get_interface(service_id, params.host_id)?)
Ok(self
.services
.get_interface(&params.id, service_id, params.host_id)?)
}

fn add_alias(&self, args: Args, params: ParticleParams) -> Result<(), JError> {
Expand All @@ -868,17 +871,19 @@ where
fn resolve_alias(&self, args: Args, params: ParticleParams) -> Result<JValue, JError> {
let mut args = args.function_args.into_iter();
let alias: String = Args::next("alias", &mut args)?;
let service_id = self.services.resolve_alias(params.host_id, alias)?;
let service_id = self
.services
.resolve_alias(&params.id, params.host_id, alias)?;

Ok(JValue::String(service_id))
}

fn get_service_info(&self, args: Args, params: ParticleParams) -> Result<JValue, JError> {
let mut args = args.function_args.into_iter();
let service_id_or_alias: String = Args::next("service_id_or_alias", &mut args)?;
let info = self
.services
.get_service_info(params.host_id, service_id_or_alias)?;
let info =
self.services
.get_service_info(&params.id, params.host_id, service_id_or_alias)?;

Ok(info)
}
Expand All @@ -896,17 +901,17 @@ where
let service_id_or_alias: String = Args::next("service_id", &mut args)?;

self.services
.get_service_mem_stats(params.host_id, service_id_or_alias)
.get_service_mem_stats(&params.id, params.host_id, service_id_or_alias)
.map(Array)
}

fn service_stat(&self, args: Args, params: ParticleParams) -> Result<JValue, JError> {
let mut args = args.function_args.into_iter();
let service_id_or_alias: String = Args::next("service_id", &mut args)?;
// Resolve aliases; also checks that the requested service exists.
let service_id = self
.services
.to_service_id(params.host_id, service_id_or_alias)?;
let service_id =
self.services
.to_service_id(&params.id, params.host_id, service_id_or_alias)?;
let metrics = self
.services
.metrics
Expand Down
12 changes: 12 additions & 0 deletions particle-execution/src/particle_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,16 @@ impl ParticleParams {
signature: signature.clone(),
}
}

pub fn is_spell_particle(particle_id: &str) -> bool {
particle_id.starts_with("spell")
}

pub fn get_spell_id(particle_id: &str) -> Option<String> {
if ParticleParams::is_spell_particle(particle_id) {
particle_id.split('_').nth(1).map(|s| s.to_string())
} else {
None
}
}
}
Loading

0 comments on commit 65b2b26

Please sign in to comment.