Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: restructure to ssri-runner with cli tool #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 102 additions & 1 deletion Cargo.lock

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

7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
[package]
name = "ssri-server"
name = "ssri-runner"
version = "0.1.0"
edition = "2021"

[[bin]]
name = "ssri-cli"
path = "src/cli.rs"

[dependencies]
anyhow = "1.0.86"
ckb-jsonrpc-types = "0.116.1"
Expand All @@ -17,3 +21,4 @@ reqwest = { version = "0.12.5", features = ["json"] }
serde = "1.0.204"
tokio = { version = "1.38.1", features = ["signal"] }
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
clap = { version = "4.5.16", features = ["env"] }
20 changes: 14 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
# SSRI Server
# SSRI Runner

This repo will compile to a binary that can run SSRI scripts and also start a RPC server to run scripts remotely.

Run a script with

```sh
RUST_LOG=ssri_cli=debug cargo run -- run --tx-hash 0x900afcf79235e88f7bdf8a5d320365b7912f8074f4489a68405f43586fc51e5c --index 0 0x58f02409de9de7b1 0x0000000000000000 0x0a00000000000000
```

Start server with

```sh
RUST_LOG=ssri_server=debug cargo run
RUST_LOG=ssri_cli=debug cargo run -- server --ckb-rpc https://testnet.ckbapp.dev/ --server-addr localhost:9090
```

Run a script with
Send request to server with

```sh
echo '{
"id": 2,
"jsonrpc": "2.0",
"method": "run_script",
"method": "run_script_level_code",
"params": ["0x900afcf79235e88f7bdf8a5d320365b7912f8074f4489a68405f43586fc51e5c", 0, ["0x58f02409de9de7b1", "0x0000000000000000", "0x0a00000000000000"]]
}' \
| curl -H 'content-type: application/json' -d @- \
http://localhost:8090
```
http://localhost:9090
```
137 changes: 137 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// a CLI that can run SSRI scripts and also start a RPC server to run scripts remotely.

use ckb_types::H256;
use ssri_runner::{types::Hex, SSRIRunner};
use std::str::FromStr;

mod server;

use clap::{Arg, ArgAction, Command};

fn main() {
let matches = Command::new("SSRI CLI")
.version("1.0")
.about("CLI for executing SSRI scripts")
.subcommand(
Command::new("run")
.about("Run a script")
.arg(
Arg::new("tx_hash")
.long("tx-hash")
.required(true)
.help("Transaction hash"),
)
.arg(
Arg::new("index")
.long("index")
.required(true)
.help("Cell index"),
)
.arg(
Arg::new("ckb_rpc")
.long("ckb-rpc")
.help("CKB RPC URL")
.default_value("https://testnet.ckbapp.dev/"),
)
.arg(
Arg::new("args")
.action(ArgAction::Append)
.help("Script arguments"),
),
)
.subcommand(
Command::new("server")
.about("Start the RPC server")
.arg(
Arg::new("ckb_rpc")
.long("ckb-rpc")
.help("CKB RPC URL")
.default_value("https://testnet.ckbapp.dev/"),
)
.arg(
Arg::new("server_addr")
.long("server-addr")
.help("Server address to listen on")
.default_value("localhost:9090"),
),
)
.get_matches();

match matches.subcommand() {
Some(("run", matches)) => {
let tx_hash = matches
.get_one::<String>("tx_hash")
.expect("Transaction hash is required");
let tx_hash = if let Some(stripped) = tx_hash.strip_prefix("0x") {
H256::from_str(stripped)
} else {
H256::from_str(tx_hash)
}
.expect("Invalid transaction hash");
let index = matches
.get_one::<String>("index")
.unwrap()
.parse::<u32>()
.expect("Invalid index");
let args: Vec<Hex> = matches
.get_many::<String>("args")
.map(|values| values.map(|v| Hex::from(v.as_str())).collect())
.unwrap_or_default();
let ckb_rpc = matches
.get_one::<String>("ckb_rpc")
.unwrap_or(&"https://testnet.ckbapp.dev/".to_string())
.to_string();

tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.unwrap()
.block_on(async {
let runner = SSRIRunner::new(&ckb_rpc);
match runner
.run_script(tx_hash, index, args, None, None, None)
.await
{
Ok(result) => match result {
Some(hex) => println!("Script execution result: {:?}", hex.clone()),
None => println!("Script execution completed without a return value"),
},
Err(e) => eprintln!("Error executing script: {}", e),
}
});
}

Some(("server", matches)) => {
let ckb_rpc = matches
.get_one::<String>("ckb_rpc")
.unwrap_or(&"https://testnet.ckbapp.dev/".to_string())
.to_string();
let server_addr = matches
.get_one::<String>("server_addr")
.unwrap_or(&"0.0.0.0:9090".to_string())
.to_string();

tracing_subscriber::FmtSubscriber::builder()
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
.try_init()
.expect("setting default subscriber failed");

tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()
.unwrap()
.block_on(async {
println!(
"Starting server with CKB RPC: {}, Server address: {}",
ckb_rpc, server_addr
);
match server::run_server(&ckb_rpc, &server_addr).await {
Ok(_) => println!("Server execution completed"),
Err(e) => eprintln!("Server error: {}", e),
}
});
}

_ => unreachable!(),
}
}
Loading