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

Basic operator API #140

Merged
merged 10 commits into from
Feb 16, 2024
Merged
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
76 changes: 51 additions & 25 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ Includes:
### Randomx-rs
[RandomX](https://github.com/tevador/randomx), that [randomx-rs](https://github.com/spacemeshos/randomx-rs) depends on, requires **cmake**. Follow [these instructions](https://github.com/spacemeshos/randomx-rs#build-dependencies) to install it.

## Post Service
Please refer to [service README](service/README.md) for instructions.
14 changes: 12 additions & 2 deletions benches/verifying.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use post::{
initialize::{CpuInitializer, Initialize},
metadata::ProofMetadata,
pow::randomx::{PoW, RandomXFlag},
prove::generate_proof,
prove::{generate_proof, NoopProgressReporter},
verification::{Mode, Verifier},
};
#[cfg(not(windows))]
Expand Down Expand Up @@ -44,7 +44,17 @@ fn verifying(c: &mut Criterion) {
let pow_flags = RandomXFlag::get_recommended_flags();
// Generate a proof
let stop = AtomicBool::new(false);
let proof = generate_proof(datadir.path(), challenge, cfg, 32, 1, pow_flags, stop).unwrap();
let proof = generate_proof(
datadir.path(),
challenge,
cfg,
32,
1,
pow_flags,
stop,
NoopProgressReporter {},
)
.unwrap();
let metadata = ProofMetadata::new(metadata, *challenge);

// Bench verifying the proof
Expand Down
14 changes: 12 additions & 2 deletions certifier/tests/test_certify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use post::{
initialize::{CpuInitializer, Initialize},
metadata::ProofMetadata,
pow::randomx::RandomXFlag,
prove::generate_proof,
prove::{self, generate_proof},
};
use reqwest::StatusCode;
use tokio::net::TcpListener;
Expand Down Expand Up @@ -45,7 +45,17 @@ async fn test_certificate_post_proof() {
// Generate a proof
let pow_flags = RandomXFlag::get_recommended_flags();
let stop = AtomicBool::new(false);
let proof = generate_proof(datadir.path(), challenge, cfg, 32, 1, pow_flags, stop).unwrap();
let proof = generate_proof(
datadir.path(),
challenge,
cfg,
32,
1,
pow_flags,
stop,
prove::NoopProgressReporter {},
)
.unwrap();
let metadata = ProofMetadata::new(metadata, *challenge);

// Spawn the certifier service
Expand Down
11 changes: 10 additions & 1 deletion ffi/src/post_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,16 @@ fn _generate_proof(
let challenge = challenge.try_into()?;

let stop = AtomicBool::new(false);
let proof = prove::generate_proof(datadir, challenge, cfg, nonces, threads, pow_flags, stop)?;
let proof = prove::generate_proof(
datadir,
challenge,
cfg,
nonces,
threads,
pow_flags,
stop,
prove::NoopProgressReporter {},
)?;
Ok(Box::new(Proof::from(proof)))
}

Expand Down
8 changes: 6 additions & 2 deletions service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ path = "src/lib.rs"
[dependencies]
post-rs = { path = "../" }
prost = "0.12.1"
tonic = { version = "0.10.0", features = ["tls"] }
tonic = { version = "0.11.0", features = ["tls"] }
tokio = { version = "1.0", features = [
"rt-multi-thread",
"macros",
Expand All @@ -26,11 +26,15 @@ clap = { version = "4.4.4", features = ["derive"] }
hex = "0.4.3"
mockall = "0.11.4"
sysinfo = "0.29.10"
axum = "0.7.4"
serde = "1.0.196"
range-set = "0.0.11"

[build-dependencies]
tonic-build = "0.10.0"
tonic-build = "0.11.0"

[dev-dependencies]
rcgen = "0.11.3"
reqwest = { version = "0.11.24", features = ["json"] }
rstest = "0.18.2"
tempfile = "3.8.0"
54 changes: 54 additions & 0 deletions service/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Post Service
Post service allows to separate expensive PoST proving from a node by allowing to generate a proof on a different machine. It connects to the node via GRPC (on an address pointed by `--address`) and awaits commands from the node.

## How to run
First of all, the service currently doesn't support initializing PoST data. The data must be initialized separately (presumably using [postcli](https://github.com/spacemeshos/post/tree/develop/cmd/postcli) and placed in a directory pointed to by `--dir`).

#### Example running on an un-encrypted channel, with the default configuration of _threads_ and _nonces_
```sh
service --address=http://my-node-address.org --dir=./post-data
```

#### Example running on an encrypted (mTLS) channel, with the custom _threads_ and _nonces_
For mTLS, you need to pass the certificate and private key of the post-service with `--cert` and `-key`, as well as the CA of the server with `--ca-cert`:
```sh
service --address=https://my-node-address.org --cert=client.pem --key=client-key.pem --ca-cert=server-rootCA.pem --dir=./post-data --threads=8 --nonces=288
```

A full usage/help can be viewed with
```sh
service --help
```

## Operator API
The operator API is a set of HTTP endpoints allowing control of the post service.

It is enabled by providing `--operator-address=<address>`, i.e. `--operator-address=127.0.0.1:50051` CLI argument.

### Example usage
#### Querying post service status
```sh
# Not doing anything
❯ curl http://localhost:50051/status
"Idle"

# Proving
❯ curl http://localhost:50051/status
{"Proving":{"nonces":{"start":0,"end":128},"position":0}}

# Proving, read some data already
❯ curl http://localhost:50051/status
{"Proving":{"nonces":{"start":0,"end":128},"position":10000}}

# Started second pass
❯ curl http://localhost:50051/status
{"Proving":{"nonces":{"start":128,"end":256},"position":10000}}

# Finished proving, but the node has not fetched the proof yet
❯ curl http://localhost:50051/status
"DoneProving"

# Finished proving and the node has fetched the proof
❯ curl http://localhost:50051/status
"Idle"
```
1 change: 1 addition & 0 deletions service/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod client;
pub mod operator;
pub mod service;
17 changes: 15 additions & 2 deletions service/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use std::{fs::read_to_string, path::PathBuf, time::Duration};
use std::{fs::read_to_string, net::SocketAddr, path::PathBuf, sync::Arc, time::Duration};

use clap::{Args, Parser, ValueEnum};
use eyre::Context;
use sysinfo::{Pid, ProcessExt, ProcessStatus, System, SystemExt};
use tokio::net::TcpListener;
use tokio::sync::oneshot::{self, error::TryRecvError, Receiver};
use tonic::transport::{Certificate, Identity};

use post::pow::randomx::RandomXFlag;
use post_service::client;
use post_service::{client, operator};

/// Post Service
#[derive(Parser, Debug)]
Expand Down Expand Up @@ -39,6 +40,11 @@ struct Cli {
/// watch PID and exit if it dies
#[arg(long)]
watch_pid: Option<sysinfo::Pid>,

/// address to listen on for operator service
/// the operator service is disabled if not specified
#[arg(long)]
operator_address: Option<SocketAddr>,
}

#[derive(Args, Debug)]
Expand Down Expand Up @@ -220,6 +226,13 @@ async fn main() -> eyre::Result<()> {
None
};

let service = Arc::new(service);

if let Some(address) = args.operator_address {
let listener = TcpListener::bind(address).await?;
tokio::spawn(operator::run(listener, service.clone()));
}

let client = client::ServiceClient::new(args.address, tls, service)?;
let client_handle = tokio::spawn(client.run(args.max_retries, args.reconnect_interval_s));

Expand Down
Loading
Loading