Skip to content

Commit

Permalink
fix: client setup for pub (#109)
Browse files Browse the repository at this point in the history
* chore: update test for pubs

* chore: update readme with instruction to send wasm to consumer
  • Loading branch information
shanithkk authored Mar 26, 2024
1 parent a0b09ae commit 7d21eeb
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 32 deletions.
16 changes: 13 additions & 3 deletions runtime/lite/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,26 @@ This runtime harnesses the capabilities of Secure Scuttlebutt, a decentralized m

cargo run

7. Run a local polakdot chain for test purpose
7. Send workflows to the consumer

let mut stream = TcpStream::connect("127.0.0.1:8080").unwrap();
let wasm = fs::read("<wasm-path>").unwrap();
let buf = RequestBody{wasm: wasm,invite: "text".to_string(),pub_id: "<pub-id>".to_string(),input: serde_json::json!({"test": "test"}),allowed_hosts: None,};
let data = serde_json::to_vec(&buf).unwrap();stream.write_all(&data).unwrap();`
stream.shutdown(std::net::Shutdown::Write).unwrap();

*Note:* Get the pub address using script `./ssb-up.sh pub-whoami`

8. Run a local polakdot chain for test purpose

docker run -p 9944:9944 parity/polkadot:v1.0.0 --dev --rpc-external

8. Run the basic example for producer with required environment variables
9. Run the basic example for producer with required environment variables

PUB_ADDRESS="@pjrBmtifFU9P9NhoHRiPbn3O3xGUXtsLWJXhxLEpkug=.ed25519"
PRODUCER_SECRET=./runtime/lite/scripts/secret/consumer_secret
PRODUCER_PORT=8014
*Note:* Get the pub address using script `./ssb-up.sh pub-whoami`

cargo run --example basic-producer
9. Make transfer event on the chain manually.
10. Make transfer event on the chain manually.
6 changes: 3 additions & 3 deletions runtime/lite/src/consumer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ async fn main() {
dotenv().ok();
let db = CoreStorage::new("runtime").unwrap();
let context = Arc::new(Mutex::new(Context::new(
CoreLogger::new(Some("./workflow")),
CoreLogger::new(Some("./runtime.log")),
db,
)));

let secret = std::env::var("SECRET").unwrap_or_else(|_| {
let secret = std::env::var("CONSUMER_SECRET").unwrap_or_else(|_| {
let home_dir = dirs::home_dir().unwrap();
std::format!("{}/.ssb/secret", home_dir.to_string_lossy())
});
let port = std::env::var("PORT").unwrap_or_else(|_| 8008.to_string());
let port = std::env::var("CONSUMER_PORT").unwrap_or_else(|_| 8008.to_string());
let mut file = async_std::fs::File::open(secret).await.unwrap();
let key = read_patchwork_config(&mut file).await.unwrap();

Expand Down
12 changes: 6 additions & 6 deletions runtime/lite/src/modules/kuska_ssb_client/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,18 +214,18 @@ impl Client {
Ok(())
}

pub async fn create_invite(&mut self) -> Result<()> {
pub async fn create_invite(&mut self) -> Result<String> {
let req_id = self.api.invite_create_req_send(1).await?;

self.print_source_until_eof(req_id, invite_create).await?;
let res = self.get_async(req_id, invite_create).await?;

Ok(())
Ok(res)
}
pub async fn accept_invite(&mut self, invite_code: &str) -> Result<()> {
pub async fn accept_invite(&mut self, invite_code: &str) -> Result<Vec<Feed>> {
let req_id = self.api.invite_use_req_send(invite_code).await?;

self.print_source_until_eof(req_id, invite_create).await?;
Ok(())
let res = self.get_async(req_id, invite_accept_res_parse).await?;
Ok(res)
}

pub async fn publish(&mut self, msg: &str, mention: Option<Vec<Mention>>) -> Result<()> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,11 @@ pub fn latest_res_parse(body: &[u8]) -> Result<LatestOut> {
}

pub fn invite_create(body: &[u8]) -> Result<String> {
Ok(std::str::from_utf8(&body)
.map_err(|err| Box::new(err) as Box<dyn std::error::Error>)?
.to_string())
}

pub fn invite_accept_res_parse(body: &[u8]) -> Result<Vec<Feed>> {
Ok(serde_json::from_slice(body)?)
}
77 changes: 57 additions & 20 deletions runtime/lite/src/modules/kuska_ssb_client/client/tests.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#[cfg(test)]
mod tests {
use crate::modules::kuska_ssb_client::client::{types, Client, UserConfig};
use dotenv::dotenv;
use kuska_ssb::keystore::read_patchwork_config;

use crate::modules::kuska_ssb_client::client::{types, Client};
use dotenv::dotenv;

// ssb-server should keep running for testing
/* configure the env variables such as ssb-sercret file path, ip and port where
ssb-server is running in .env file */
Expand All @@ -14,11 +15,11 @@ mod tests {
async fn test_client() {
dotenv().ok();

let secret = std::env::var("SECRET").unwrap_or_else(|_| {
let secret = std::env::var("CONSUMER_SECRET").unwrap_or_else(|_| {
let home_dir = dirs::home_dir().unwrap();
std::format!("{}/.ssb/secret", home_dir.to_string_lossy())
});
let ssb_port = std::env::var("PORT").unwrap_or_else(|_| 8008.to_string());
let ssb_port = std::env::var("CONSUMER_PORT").unwrap_or_else(|_| 8008.to_string());
let ssb_ip = std::env::var("IP").unwrap_or_else(|_| "0.0.0.0".to_string());
let mut file = async_std::fs::File::open(secret).await.unwrap();
let config = read_patchwork_config(&mut file).await.unwrap();
Expand All @@ -31,11 +32,11 @@ mod tests {
async fn test_client_with_config() {
dotenv().ok();

let secret = std::env::var("SECRET").unwrap_or_else(|_| {
let secret = std::env::var("CONSUMER_SECRET").unwrap_or_else(|_| {
let home_dir = dirs::home_dir().unwrap();
std::format!("{}/.ssb/secret", home_dir.to_string_lossy())
});
let ssb_port = std::env::var("PORT").unwrap_or_else(|_| 8008.to_string());
let ssb_port = std::env::var("CONSUMER_PORT").unwrap_or_else(|_| 8008.to_string());
let ssb_ip = std::env::var("IP").unwrap_or_else(|_| "0.0.0.0".to_string());
let mut file = async_std::fs::File::open(secret).await.unwrap();
let config = read_patchwork_config(&mut file).await.unwrap();
Expand All @@ -49,11 +50,11 @@ mod tests {
async fn test_get_secret_key() {
dotenv().ok();

let secret = std::env::var("SECRET").unwrap_or_else(|_| {
let secret = std::env::var("CONSUMER_SECRET").unwrap_or_else(|_| {
let home_dir = dirs::home_dir().unwrap();
std::format!("{}/.ssb/secret", home_dir.to_string_lossy())
});
let ssb_port = std::env::var("PORT").unwrap_or_else(|_| 8008.to_string());
let ssb_port = std::env::var("CONSUMER_PORT").unwrap_or_else(|_| 8008.to_string());
let ssb_ip = std::env::var("IP").unwrap_or_else(|_| "0.0.0.0".to_string());
let mut file = async_std::fs::File::open(secret).await.unwrap();
let config = read_patchwork_config(&mut file).await.unwrap();
Expand All @@ -72,11 +73,11 @@ mod tests {
async fn test_whoami() {
dotenv().ok();

let secret = std::env::var("SECRET").unwrap_or_else(|_| {
let secret = std::env::var("CONSUMER_SECRET").unwrap_or_else(|_| {
let home_dir = dirs::home_dir().unwrap();
std::format!("{}/.ssb/secret", home_dir.to_string_lossy())
});
let ssb_port = std::env::var("PORT").unwrap_or_else(|_| 8008.to_string());
let ssb_port = std::env::var("CONSUMER_PORT").unwrap_or_else(|_| 8008.to_string());
let ssb_ip = std::env::var("IP").unwrap_or_else(|_| "0.0.0.0".to_string());
let mut file = async_std::fs::File::open(secret).await.unwrap();
let config = read_patchwork_config(&mut file).await.unwrap();
Expand All @@ -96,11 +97,11 @@ mod tests {
use types::Event;
dotenv().ok();

let secret = std::env::var("SECRET").unwrap_or_else(|_| {
let secret = std::env::var("CONSUMER_SECRET").unwrap_or_else(|_| {
let home_dir = dirs::home_dir().unwrap();
std::format!("{}/.ssb/secret", home_dir.to_string_lossy())
});
let ssb_port = std::env::var("PORT").unwrap_or_else(|_| 8008.to_string());
let ssb_port = std::env::var("CONSUMER_PORT").unwrap_or_else(|_| 8008.to_string());
let ssb_ip = std::env::var("IP").unwrap_or_else(|_| "0.0.0.0".to_string());
let mut file = async_std::fs::File::open(secret).await.unwrap();
let config = read_patchwork_config(&mut file).await.unwrap();
Expand Down Expand Up @@ -145,11 +146,11 @@ mod tests {
async fn test_close() {
dotenv().ok();

let secret = std::env::var("SECRET").unwrap_or_else(|_| {
let secret = std::env::var("PRODUCER_SECRET").unwrap_or_else(|_| {
let home_dir = dirs::home_dir().unwrap();
std::format!("{}/.ssb/secret", home_dir.to_string_lossy())
});
let ssb_port = std::env::var("PORT").unwrap_or_else(|_| 8008.to_string());
let ssb_port = std::env::var("PRODUCER_PORT").unwrap_or_else(|_| 8008.to_string());
let ssb_ip = std::env::var("IP").unwrap_or_else(|_| "0.0.0.0".to_string());
let mut file = async_std::fs::File::open(secret).await.unwrap();
let config = read_patchwork_config(&mut file).await.unwrap();
Expand All @@ -165,11 +166,11 @@ mod tests {
async fn test_feed() {
dotenv().ok();

let secret = std::env::var("SECRET").unwrap_or_else(|_| {
let secret = std::env::var("PRODUCER_SECRET").unwrap_or_else(|_| {
let home_dir = dirs::home_dir().unwrap();
std::format!("{}/.ssb/secret", home_dir.to_string_lossy())
});
let ssb_port = std::env::var("PORT").unwrap_or_else(|_| 8008.to_string());
let ssb_port = std::env::var("PRODUCER_PORT").unwrap_or_else(|_| 8008.to_string());
let ssb_ip = std::env::var("IP").unwrap_or_else(|_| "0.0.0.0".to_string());
let mut file = async_std::fs::File::open(secret).await.unwrap();
let config = read_patchwork_config(&mut file).await.unwrap();
Expand All @@ -184,11 +185,11 @@ mod tests {
async fn test_publish() {
dotenv().ok();

let secret = std::env::var("SECRET").unwrap_or_else(|_| {
let secret = std::env::var("PRODUCER_SECRET").unwrap_or_else(|_| {
let home_dir = dirs::home_dir().unwrap();
std::format!("{}/.ssb/secret", home_dir.to_string_lossy())
});
let ssb_port = std::env::var("PORT").unwrap_or_else(|_| 8008.to_string());
let ssb_port = std::env::var("PRODUCER_PORT").unwrap_or_else(|_| 8008.to_string());
let ssb_ip = std::env::var("IP").unwrap_or_else(|_| "0.0.0.0".to_string());
let mut file = async_std::fs::File::open(secret).await.unwrap();
let config = read_patchwork_config(&mut file).await.unwrap();
Expand Down Expand Up @@ -234,11 +235,11 @@ mod tests {

dotenv().ok();

let secret = std::env::var("SECRET").unwrap_or_else(|_| {
let secret = std::env::var("CONSUMER_SECRET").unwrap_or_else(|_| {
let home_dir = dirs::home_dir().unwrap();
std::format!("{}/.ssb/secret", home_dir.to_string_lossy())
});
let ssb_port = std::env::var("PORT").unwrap_or_else(|_| 8008.to_string());
let ssb_port = std::env::var("CONSUMER_PORT").unwrap_or_else(|_| 8008.to_string());
let ssb_ip = std::env::var("IP").unwrap_or_else(|_| "0.0.0.0".to_string());
let mut file = async_std::fs::File::open(secret).await.unwrap();
let config = read_patchwork_config(&mut file).await.unwrap();
Expand Down Expand Up @@ -317,4 +318,40 @@ mod tests {
}
}
}

#[async_std::test]
#[ignore]
async fn test_create_invite() {
dotenv::dotenv().ok();
let secret = std::env::var("PUB_SECRET").unwrap_or_else(|_| {
let home_dir = dirs::home_dir().unwrap();
std::format!("{}/.ssb/secret", home_dir.to_string_lossy())
});
let port = std::env::var("PUB_PORT").unwrap_or_else(|_| 8013.to_string());
let mut file = async_std::fs::File::open(secret).await.unwrap();
let key = read_patchwork_config(&mut file).await.unwrap();
let mut client = Client::new(Some(key), "0.0.0.0".to_string(), port)
.await
.unwrap();
let res = client.create_invite().await;
assert!(res.is_ok())
}

#[async_std::test]
#[ignore]
async fn test_accept_invite() {
dotenv::dotenv().ok();
let secret = std::env::var("CONSUMER_SECRET").unwrap_or_else(|_| {
let home_dir = dirs::home_dir().unwrap();
std::format!("{}/.ssb/secret", home_dir.to_string_lossy())
});
let port = std::env::var("CONSUMER_PORT").unwrap_or_else(|_| 8015.to_string());
let mut file = async_std::fs::File::open(secret).await.unwrap();
let key = read_patchwork_config(&mut file).await.unwrap();
let mut client = Client::new(Some(key), "0.0.0.0".to_string(), port)
.await
.unwrap();
let res = client.accept_invite("172.28.0.4:8008:@hkYrVBGtWm5+xeRYDzsL9u6b0cM2rtcYs4NiiZQEVLs=.ed25519~BERengMsq9t2ovXHBZgiFtKDlcvAYQTXSPk/JAw+3zQ=").await;
assert!(res.is_ok())
}
}

0 comments on commit 7d21eeb

Please sign in to comment.