Skip to content

Commit

Permalink
feat!: support new CallParameters and generate particle token [NET-767]…
Browse files Browse the repository at this point in the history
… (#2099)

* Support new CallParameters and generate particle token

* fix unit test

* fix tetraplets test

* refactor A BIT
  • Loading branch information
kmd-fl committed Feb 21, 2024
1 parent 23e556b commit 179d697
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 35 deletions.
104 changes: 86 additions & 18 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 @@ -105,7 +105,7 @@ fluence-spell-dtos = "=0.7.0"
fluence-spell-distro = "=0.7.0"

# marine
fluence-app-service = { version = "0.32.0" }
fluence-app-service = { version = "0.33.0" }
marine-utils = "0.5.1"
marine-it-parser = "0.15.1"

Expand Down
19 changes: 18 additions & 1 deletion aquamarine/src/plumber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

use eyre::eyre;
use fluence_keypair::KeyPair;
use futures::future::BoxFuture;
use futures::FutureExt;
use std::collections::hash_map::Entry;
Expand Down Expand Up @@ -180,7 +181,13 @@ impl<RT: AquaRuntime, F: ParticleFunctionStatic> Plumber<RT, F> {
match entry {
Entry::Occupied(actor) => Ok(actor.into_mut()),
Entry::Vacant(entry) => {
let params = ParticleParams::clone_from(particle.as_ref(), peer_scope);
// TODO: move to a better place
let particle_token = get_particle_token(
&self.key_storage.root_key_pair,
&particle.particle.signature,
)?;
let params =
ParticleParams::clone_from(particle.as_ref(), peer_scope, particle_token);
let functions = Functions::new(params, builtins.clone());
let key_pair = self
.key_storage
Expand Down Expand Up @@ -416,6 +423,16 @@ impl<RT: AquaRuntime, F: ParticleFunctionStatic> Plumber<RT, F> {
}
}

fn get_particle_token(key_pair: &KeyPair, signature: &Vec<u8>) -> eyre::Result<String> {
let particle_token = key_pair.sign(signature.as_slice()).map_err(|err| {
eyre!(
"Could not produce particle token by signing the particle signature: {}",
err
)
})?;
Ok(bs58::encode(particle_token.to_vec()).into_string())
}

/// Implements `now` by taking number of non-leap seconds from `Utc::now()`
mod real_time {
#[allow(dead_code)]
Expand Down
6 changes: 3 additions & 3 deletions crates/nox-tests/tests/tetraplets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ async fn test_tetraplets() {
let tetraplet = &ap_literal_tetraplets[0][0];
assert_eq!(tetraplet.function_name, "");
assert_eq!(tetraplet.peer_pk, client.peer_id.to_base58());
assert_eq!(tetraplet.json_path, "");
assert_eq!(tetraplet.lambda, "");
assert_eq!(tetraplet.service_id, "");

let first_tetraplets = args.next().unwrap();
Expand All @@ -108,7 +108,7 @@ async fn test_tetraplets() {
let tetraplet = &first_tetraplets[0][0];
assert_eq!(tetraplet.function_name, "identity");
assert_eq!(tetraplet.peer_pk, client.node.to_base58());
assert_eq!(tetraplet.json_path, "");
assert_eq!(tetraplet.lambda, "");
assert_eq!(tetraplet.service_id, "op");

let ap_first_tetraplets = args.next().unwrap();
Expand All @@ -127,6 +127,6 @@ async fn test_tetraplets() {
let tetraplet = &second_tetraplets[0][0];
assert_eq!(tetraplet.function_name, "get_tetraplets");
assert_eq!(tetraplet.peer_pk, client.node.to_base58());
assert_eq!(tetraplet.json_path, ".$.[0].[0].peer_pk");
assert_eq!(tetraplet.lambda, ".$.[0].[0].peer_pk");
assert_eq!(tetraplet.service_id, tetraplets_service.id.as_str());
}
8 changes: 3 additions & 5 deletions crates/nox-tests/tests/tetraplets/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ authors = ["Fluence Labs"]
edition = "2021"

[dependencies]
marine-rs-sdk = "0.7.0"
once_cell = { workspace = true }
marine-rs-sdk = "0.13.0"
once_cell = "1.19.0"

[[bin]]
name = "tetraplets"
path = "src/main.rs"

[workspace]
path = "src/main.rs"
Binary file modified crates/nox-tests/tests/tetraplets/artifacts/tetraplets.wasm
Binary file not shown.
1 change: 0 additions & 1 deletion crates/nox-tests/tests/tetraplets/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![feature(once_cell)]
/*
* Copyright 2021 Fluence Labs Limited
*
Expand Down
18 changes: 17 additions & 1 deletion particle-execution/src/particle_params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

use fluence_app_service::ParticleParameters;
use fluence_libp2p::PeerId;
use particle_protocol::Particle;
use types::peer_scope::PeerScope;
Expand All @@ -31,10 +32,12 @@ pub struct ParticleParams {
pub ttl: u32,
pub script: String,
pub signature: Vec<u8>,
// Particle token, `signature` signed with the peer's private key
pub token: String,
}

impl ParticleParams {
pub fn clone_from(particle: &Particle, peer_scope: PeerScope) -> Self {
pub fn clone_from(particle: &Particle, peer_scope: PeerScope, token: String) -> Self {
let Particle {
id,
init_peer_id,
Expand All @@ -53,6 +56,7 @@ impl ParticleParams {
ttl: *ttl,
script: script.clone(),
signature: signature.clone(),
token,
}
}

Expand All @@ -67,4 +71,16 @@ impl ParticleParams {
None
}
}

pub fn to_particle_parameters(self) -> ParticleParameters {
ParticleParameters {
id: self.id,
init_peer_id: self.init_peer_id.to_string(),
timestamp: self.timestamp,
ttl: self.ttl,
script: self.script,
signature: self.signature,
token: self.token,
}
}
}
Loading

0 comments on commit 179d697

Please sign in to comment.