diff --git a/docs/runtime-lite.png b/docs/runtime-lite.png new file mode 100644 index 00000000..186fc85c Binary files /dev/null and b/docs/runtime-lite.png differ diff --git a/runtime/lite/Readme.md b/runtime/lite/Readme.md new file mode 100644 index 00000000..b74f0a78 --- /dev/null +++ b/runtime/lite/Readme.md @@ -0,0 +1,58 @@ +# Aurras Lite Runtime +A runtime leveraging Secure Scuttlebutt for event streaming, designed to facilitate the seamless execution of workflows on the user side in response to specific events. +This runtime harnesses the capabilities of Secure Scuttlebutt, a decentralized messaging protocol, to efficiently stream events securely across a network. As events are emitted, the runtime captures and processes them, initiating corresponding workflows tailored to the user's preferences and requirements. + +![alt text](../../docs/runtime-lite.png) + +# Pre-requisites +- [SSB-server](https://github.com/ssbc/ssb-server) +- [Rust](https://www.rust-lang.org/tools/install) + + +## Setup +1. Install the ssb-server and start it. + + ssb-server start +2. Run the client producer or consumer or client + + corgo run --bin consumer + +### Integration test setup +1. Setup a network, got the script folder + + ./ssb-up.sh start + +2. Create an invite + + ./ssb-up.sh create-invite + +3. Copy the invite and run + + ./ssb-up.sh accept-invite + +4. Export the secret for connecting the client + + ./ssb-up.sh copy-secret + *Note:* This will export the secret to secret folder in the current directory. + +5. Move to lite directort and create a .env file and export the secret path port for consumer. + + SECRET=../aurras/runtime/lite/scripts/secret/consumer_secret + PORT=8015 + +6. Run the consumer client + + congo run --bin consumer + +7. 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 + + 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` + +9. Make transfer event on the chain manually. \ No newline at end of file diff --git a/runtime/lite/examples/basic-producer.rs b/runtime/lite/examples/basic-producer.rs new file mode 100644 index 00000000..215eae65 --- /dev/null +++ b/runtime/lite/examples/basic-producer.rs @@ -0,0 +1,94 @@ +use dotenv::dotenv; +use kuska_ssb::{api::dto::content::Mention, keystore::read_patchwork_config}; +use runtime::kuska_ssb_client::client::Client; + +#[tokio::main] +async fn main() { + dotenv().ok(); + println!("start"); + 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 port = std::env::var("PRODUCER_PORT").unwrap_or("8014".to_string()); + let pub_address = std::env::var("PUB_ADDRESS").expect("Pub address must be provided"); + + 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(); + + use subxt::{OnlineClient, PolkadotConfig}; + + #[subxt::subxt(runtime_metadata_path = "./src/modules/utils/polkadot_metadata_small.scale")] + pub mod polkadot {} + + let api = OnlineClient::::new().await.unwrap(); + + // Subscribe to all finalized blocks: + let mut blocks_sub = api.blocks().subscribe_finalized().await.unwrap(); + + // For each block, print a bunch of information about it: + while let Some(block) = blocks_sub.next().await { + let block = block.unwrap(); + + let block_number = block.header().number; + let block_hash = block.hash(); + + println!("Block #{block_number}:"); + println!(" Hash: {block_hash}"); + println!(" Extrinsics:"); + + // Code for automate transfer + // use subxt_signer::sr25519::dev; + // if block_number == 10 { + // let dest = dev::bob().public_key().into(); + // let tx = polkadot::tx().balances().transfer_allow_death(dest, 10_000); + // let from = dev::alice(); + // let _events = api + // .tx() + // .sign_and_submit_then_watch_default(&tx, &from) + // .await + // .unwrap(); + // } + + // Log each of the extrinsic with it's associated events: + let extrinsics = block.extrinsics().await.unwrap(); + for ext in extrinsics.iter() { + let ext = ext.unwrap(); + let events = ext.events().await.unwrap(); + let transfer = events + .find_first::() + .unwrap(); + + match transfer { + Some(transfer) => { + let from_addr = transfer.from.to_string(); + let to_addr = transfer.from.to_string(); + let amount = transfer.amount; + println!("{from_addr:?}"); + + let value = format!( + "{{\"from\":\"{}\",\"to\":\"{}\",\"amount\":\"{}\"}}", + from_addr, to_addr, amount + ); + + let menttion = Mention { + link: pub_address.clone(), + name: None, + }; + + let result = client + .publish(&value.to_string(), Some(vec![menttion])) + .await; + // assert!(result.is_ok()); + // break 'outer; + } + None => (), + } + } + } +} diff --git a/runtime/lite/scripts/docker-compose.yml b/runtime/lite/scripts/docker-compose.yml index 1e5dd0ed..45a65661 100644 --- a/runtime/lite/scripts/docker-compose.yml +++ b/runtime/lite/scripts/docker-compose.yml @@ -16,16 +16,17 @@ services: # cp /tmp/config /home/node/.ssb # entrypoint: ["ssb-server", "start"] command: "start" - restart: unless-stopped + restart: always consumer: image: kkshanith/ssb-server ports: - "8014:8008" # Map container port 8088 to host port - restart: unless-stopped + restart: on-failure producer: image: kkshanith/ssb-server ports: - "8015:8008" # Map container port 8088 to host port 8082 + restart: on-failure \ No newline at end of file diff --git a/runtime/lite/scripts/ssb-up.sh b/runtime/lite/scripts/ssb-up.sh index 69232d57..2a1ec541 100755 --- a/runtime/lite/scripts/ssb-up.sh +++ b/runtime/lite/scripts/ssb-up.sh @@ -90,6 +90,7 @@ accept_invite(){ producer_container=$(find_container "ssb-producer") consumer_accept=$(docker exec -it "$consumer_container" bash -c "ssb-server invite.accept $invite") + sleep 3 producer_accept=$(docker exec -it "$producer_container" bash -c "ssb-server invite.accept $invite") echo $consumer_accept echo $producer_accept @@ -102,8 +103,8 @@ copy_secrets_to(){ mkdir -p ./secret - docker cp $consumer_container:/root/.ssb/secret ./secret/consumer_secret - docker cp $producer_container:/root/.ssb/secret ./secret/producer_secret + docker cp $consumer_container:/root/.ssb/secret ./secret/producer_secret + docker cp $producer_container:/root/.ssb/secret ./secret/consumer_secret docker cp $pubs_container:/home/node/.ssb/secret ./secret/pubs_secret } start_specific_service() { @@ -111,6 +112,13 @@ start_specific_service() { docker-compose --project-name ssb up -d $service } +pub_whoami(){ + container=$(find_container "ssb-pubs") + whoami=$(docker exec -it "$container" bash -c "ssb-server whoami") + echo $whoami +} + + case "$1" in start) initialize @@ -136,6 +144,9 @@ case "$1" in copy-secret) copy_secrets_to ;; + pub-whoami) + pub_whoami + ;; *) echo "Invalid command. Please enter start, copy or stop." ;; diff --git a/runtime/lite/src/consumer.rs b/runtime/lite/src/consumer.rs index 16a70d52..22c8b765 100644 --- a/runtime/lite/src/consumer.rs +++ b/runtime/lite/src/consumer.rs @@ -12,9 +12,6 @@ use std::{ sync::{Arc, Mutex}, }; -// pub mod context; -// pub use context::*; - pub use runtime; use runtime::Context;