Skip to content

Commit

Permalink
feat: add readme and producer example (#108)
Browse files Browse the repository at this point in the history
* chore: added example for producer

* chore: update the read me
  • Loading branch information
shanithkk authored Mar 24, 2024
1 parent d8c84e6 commit e3c366e
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 7 deletions.
Binary file added docs/runtime-lite.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
58 changes: 58 additions & 0 deletions runtime/lite/Readme.md
Original file line number Diff line number Diff line change
@@ -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 <paste copied invite here>

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.
94 changes: 94 additions & 0 deletions runtime/lite/examples/basic-producer.rs
Original file line number Diff line number Diff line change
@@ -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::<PolkadotConfig>::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::<polkadot::balances::events::Transfer>()
.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 => (),
}
}
}
}
5 changes: 3 additions & 2 deletions runtime/lite/scripts/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

15 changes: 13 additions & 2 deletions runtime/lite/scripts/ssb-up.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -102,15 +103,22 @@ 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() {
service=$1
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
Expand All @@ -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."
;;
Expand Down
3 changes: 0 additions & 3 deletions runtime/lite/src/consumer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ use std::{
sync::{Arc, Mutex},
};

// pub mod context;
// pub use context::*;

pub use runtime;
use runtime::Context;

Expand Down

0 comments on commit e3c366e

Please sign in to comment.