Skip to content

Commit

Permalink
feat(spell): update to new kv restrictions (#2094)
Browse files Browse the repository at this point in the history
* Update to new kv restrictions

* update

* update test

* update test

* update test

* fix tests

* fix fmt

* update spell

* update decider

* update

* update

* chore: cargo.lock

* chore: decider-distro 0.6.3

* fix(deps): spell 0.7.2

* try decider-distro

* update decider

---------

Co-authored-by: Maria Kuklina <maria@fluence.one>
Co-authored-by: Maria Kuklina <101095419+kmd-fl@users.noreply.github.com>
  • Loading branch information
3 people committed Feb 22, 2024
1 parent fc8fea7 commit 7bc9e5a
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 70 deletions.
12 changes: 6 additions & 6 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ types = { path = "crates/types" }
core-manager = { path = "crates/core-manager" }

# spell
fluence-spell-dtos = "=0.7.0"
fluence-spell-distro = "=0.7.0"
fluence-spell-dtos = "=0.7.2"
fluence-spell-distro = "=0.7.2"

# marine
fluence-app-service = { version = "0.33.0" }
Expand Down
91 changes: 43 additions & 48 deletions crates/nox-tests/tests/spells.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ async fn spell_simple_test() {
(seq
(seq
(call %init_peer_id% (spell_id "get_script") [] script)
(call %init_peer_id% (spell_id "get_u32") ["counter"] counter)
(call %init_peer_id% (spell_id "get_u32") ["hw_counter"] counter)
)
(call "{}" ("return" "") [script.$.value counter])
)
Expand Down Expand Up @@ -402,7 +402,7 @@ async fn spell_run_oneshot() {
(seq
(seq
(call relay ("op" "noop") [])
(call worker (spell_id "get_u32") ["counter"] counter)
(call worker (spell_id "get_u32") ["hw_counter"] counter)
)
(call client ("return" "") [counter])
)"#,
Expand Down Expand Up @@ -444,7 +444,7 @@ async fn spell_install_ok_empty_config() {
(seq
(seq
(call relay ("op" "noop") [])
(call worker (spell_id "get_u32") ["counter"] counter)
(call worker (spell_id "get_u32") ["hw_counter"] counter)
)
(call %init_peer_id% ("return" "") [counter])
)"#,
Expand Down Expand Up @@ -479,7 +479,7 @@ async fn spell_install_ok_empty_config() {
(seq
(seq
(call relay ("op" "noop") [])
(call worker (spell_id "get_u32") ["counter"] counter)
(call worker (spell_id "get_u32") ["hw_counter"] counter)
)
(call %init_peer_id% ("return" "") [counter])
)"#,
Expand Down Expand Up @@ -904,7 +904,7 @@ async fn spell_call_by_alias() {
(seq
(seq
(call %init_peer_id% ("srv" "add_alias") ["alias" spell_id])
(call %init_peer_id% ("alias" "get_u32") ["counter"] counter)
(call %init_peer_id% ("alias" "get_u32") ["hw_counter"] counter)
)
(call "{}" ("return" "") [counter.$.value])
Expand Down Expand Up @@ -940,7 +940,7 @@ async fn spell_trigger_connection_pool() {
(seq
(seq
(call %init_peer_id% ("getDataSrv" "spell_id") [] spell_id)
(call %init_peer_id% (spell_id "get_u32") ["counter"] counter)
(call %init_peer_id% (spell_id "get_u32") ["hw_counter"] counter)
)
(call "{}" ("return" "") [spell_id])
)
Expand Down Expand Up @@ -1026,7 +1026,7 @@ async fn spell_timer_trigger_mailbox_test() {
(seq
(seq
(call %init_peer_id% ("getDataSrv" "spell_id") [] spell_id)
(call %init_peer_id% ("getDataSrv" "trigger") [] trigger)
(call %init_peer_id% ("getDataSrv" "hw_trigger") [] trigger)
)
(call "{}" ("return" "") [trigger])
)
Expand Down Expand Up @@ -1067,7 +1067,7 @@ async fn spell_connection_pool_trigger_test() {
(seq
(call %init_peer_id% ("getDataSrv" "spell_id") [] spell_id)
(seq
(call %init_peer_id% ("getDataSrv" "trigger") [] trigger)
(call %init_peer_id% ("getDataSrv" "hw_trigger") [] trigger)
(call %init_peer_id% ("run-console" "print") ["getDataSrv, trigger:" trigger])
)
)
Expand Down Expand Up @@ -1127,56 +1127,51 @@ async fn spell_connection_pool_trigger_test() {

#[tokio::test]
async fn spell_set_u32() {
enable_logs();
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#"(call "{}" ("return" "") ["called"])"#, client.peer_id);
let mut config = TriggerConfig::default();
config.connections.connect = true;

let (spell_id, worker_id) =
create_spell(&mut client, &script, config.clone(), json!({}), None).await;
let mut client = ConnectedClient::connect_with_keypair(
swarms[0].multiaddr.clone(),
Some(swarms[0].management_keypair.clone()),
)
.await
.wrap_err("connect client")
.unwrap();

let data = hashmap! {
"spell_id" => json!(spell_id),
"worker" => json!(worker_id),
"relay" => json!(client.node.to_string()),
"client" => json!(client.peer_id.to_string()),
"config" => json!(config),
};
let mut result = client
.execute_particle(
r#"(seq
let script = format!(
r#"( seq
(seq
(call %init_peer_id% ("getDataSrv" "spell_id") [] spell_id)
(seq
(call %init_peer_id% (spell_id "get_u32") ["test"] absent)
(seq
(call relay ("op" "noop") [])
(call worker (spell_id "get_u32") ["test"] absent)
)
(seq
(call worker (spell_id "set_u32") ["test" 1])
(call %init_peer_id% (spell_id "set_u32") ["test" 1])
(seq
(call worker (spell_id "get_u32") ["test"] one)
(call %init_peer_id% (spell_id "get_u32") ["test"] one)
(seq
(call worker (spell_id "set_u32") ["test" 2])
(call worker (spell_id "get_u32") ["test"] two)
(call %init_peer_id% (spell_id "set_u32") ["test" 2])
(call %init_peer_id% (spell_id "get_u32") ["test"] two)
)
)
)
)
(call %init_peer_id% ("return" "") [absent one two])
)"#,
data,
)
.await
.unwrap();
(call "{}" ("return" "") [absent one two])
)"#,
client.peer_id
);
// oneshot spell
let mut config = TriggerConfig::default();
config.clock.start_sec = 1;
config.clock.period_sec = 0;

let (_spell_id, _worker_id) =
create_spell(&mut client, &script, config.clone(), json!({}), None).await;
let mut result = client.receive_args().await.wrap_err("receive").unwrap();

assert_eq!(result.len(), 3);
let (absent, one, two) = (result.remove(0), result.remove(0), result.remove(0));

assert_eq!(absent["absent"], json!(true));

assert_eq!(one["absent"], json!(false));
assert_eq!(one["value"], json!(1));

Expand Down Expand Up @@ -1221,7 +1216,7 @@ async fn spell_update_config() {
r#"(seq
(seq
(call %init_peer_id% ("getDataSrv" "spell_id") [] spell_id)
(call %init_peer_id% ("getDataSrv" "trigger") [] trigger)
(call %init_peer_id% ("getDataSrv" "hw_trigger") [] trigger)
)
(call "{}" ("return" "") [trigger])
)"#,
Expand Down Expand Up @@ -1311,7 +1306,7 @@ async fn spell_update_config_stopped_spell() {
r#"(seq
(seq
(call %init_peer_id% ("getDataSrv" "spell_id") [] spell_id)
(call %init_peer_id% ("getDataSrv" "trigger") [] trigger)
(call %init_peer_id% ("getDataSrv" "hw_trigger") [] trigger)
)
(call "{}" ("return" "") [trigger])
)"#,
Expand Down Expand Up @@ -1748,7 +1743,7 @@ async fn spell_update_trigger_by_alias() {
(seq
(seq
(call %init_peer_id% ("getDataSrv" "spell_id") [] spell_id)
(call %init_peer_id% (spell_id "get_u32") ["counter"] counter)
(call %init_peer_id% (spell_id "get_u32") ["hw_counter"] counter)
)
(call "{}" ("return" "") [spell_id])
)
Expand Down Expand Up @@ -1959,7 +1954,7 @@ async fn spell_call_by_default_alias() {
(seq
(seq
(seq
(call %init_peer_id% ("spell" "get_u32") ["counter"] counter1)
(call %init_peer_id% ("spell" "get_u32") ["hw_counter"] counter1)
(seq
(call %init_peer_id% ("srv" "resolve_alias") ["spell"] spell_id1)
(xor
Expand All @@ -1969,7 +1964,7 @@ async fn spell_call_by_default_alias() {
)
)
(seq
(call %init_peer_id% ("self" "get_u32") ["counter"] counter2)
(call %init_peer_id% ("self" "get_u32") ["hw_counter"] counter2)
(seq
(call %init_peer_id% ("srv" "resolve_alias") ["self"] spell_id2)
(xor
Expand Down
42 changes: 29 additions & 13 deletions crates/spell-service-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ impl SpellServiceApi {
pub fn get_counter(&self, params: CallParams) -> Result<Option<u32>, CallError> {
let function = Function {
name: "get_u32",
args: vec![json!("counter")],
args: vec![json!("hw_counter")],
};
let result = self.call::<U32Value>(params, function)?;
Ok((!result.absent).then_some(result.value))
Expand All @@ -198,15 +198,15 @@ impl SpellServiceApi {
pub fn set_counter(&self, params: CallParams, counter: u32) -> Result<(), CallError> {
let function = Function {
name: "set_u32",
args: vec![json!("counter"), json!(counter)],
args: vec![json!("hw_counter"), json!(counter)],
};
let _ = self.call::<UnitValue>(params, function)?;

Ok(())
}

pub fn set_trigger_event(&self, params: CallParams, event: String) -> Result<(), CallError> {
self.set_string(params, "trigger".to_string(), event)
self.set_string(params, "hw_trigger".to_string(), event)
}

pub fn store_error(&self, params: CallParams, args: Vec<Value>) -> Result<(), CallError> {
Expand Down Expand Up @@ -299,10 +299,9 @@ mod tests {
}

async fn create_pas(
local_pid: PeerId,
management_pid: PeerId,
base_dir: PathBuf,
) -> (ParticleAppServices, ModuleRepository) {
) -> (ParticleAppServices, ModuleRepository, PeerId) {
let root_key_pair = Keypair::generate_ed25519();
let vault_dir = base_dir.join("..").join("vault");
let keypairs_dir = base_dir.join("..").join("keypairs");
Expand Down Expand Up @@ -333,7 +332,7 @@ mod tests {

let service_memory_limit = server_config::default_service_memory_limit();
let config = ServicesConfig::new(
local_pid,
root_key_pair.get_peer_id(),
base_dir,
vault_dir,
HashMap::new(),
Expand All @@ -358,7 +357,7 @@ mod tests {
workers.clone(),
scope.clone(),
);
(pas, repo)
(pas, repo, root_key_pair.get_peer_id())
}

async fn create_spell(
Expand All @@ -373,9 +372,8 @@ mod tests {

async fn setup() -> (SpellServiceApi, CallParams) {
let base_dir = TempDir::new("test3").unwrap();
let local_pid = create_pid();
let management_pid = create_pid();
let (pas, repo) = create_pas(local_pid, management_pid, base_dir.into_path()).await;
let (pas, repo, local_pid) = create_pas(management_pid, base_dir.into_path()).await;

let api = SpellServiceApi::new(pas.clone());
let (storage, _) = spell_storage::SpellStorage::create(Path::new(""), &pas, &repo).unwrap();
Expand Down Expand Up @@ -404,7 +402,8 @@ mod tests {
let result2 = api.set_counter(params.clone(), new_counter);
assert!(
result2.is_ok(),
"must be able to set a counter of an empty spell"
"must be able to set a counter of an empty spell: {:?}",
result2.unwrap_err()
);
let result3 = api.get_counter(params);
assert!(
Expand Down Expand Up @@ -446,13 +445,30 @@ mod tests {

#[tokio::test]
async fn test_kv() {
let (api, params) = setup().await;
let (api, host_params) = setup().await;
let init_data = hashmap! {
"a1" => json!(1),
"b1" => json!("test"),
};
let result1 = api.update_kv(host_params.clone(), json!(init_data));
assert!(
!result1.is_ok(),
"must NOT be able to update kv without h/hw key prefixes calling from host"
);

let params = CallParams::new(
host_params.init_peer_id,
PeerScope::Host,
host_params.spell_id.clone(),
Some(format!("spell_{}_0", host_params.spell_id)),
host_params.ttl,
);

let result1 = api.update_kv(params.clone(), json!(init_data));
assert!(result1.is_ok(), "must be able to update kv");
assert!(
result1.is_ok(),
"must be able to update kv calling as a spell"
);

let result = api.get_string(params.clone(), "a1".to_string());
assert!(result.is_ok(), "must be able to add get_string");
Expand Down Expand Up @@ -485,7 +501,7 @@ mod tests {

let function = super::Function {
name: "get_string",
args: vec![json!("trigger")],
args: vec![json!("hw_trigger")],
};
let result = api.call::<StringValue>(params, function);
assert!(result.is_ok(), "must be able to add get_string");
Expand Down
2 changes: 1 addition & 1 deletion crates/system-services/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"

[dependencies]
aqua-ipfs-distro = "=0.5.30"
decider-distro = "=0.6.4"
decider-distro = "=0.6.5"
registry-distro = "=0.9.4"
trust-graph-distro = "=0.4.11"

Expand Down

0 comments on commit 7bc9e5a

Please sign in to comment.