mirror of
https://github.com/darkrenaissance/darkfi.git
synced 2026-01-10 07:08:05 -05:00
example: Remove rpc client/server.
They're essentially provided in tests/
This commit is contained in:
11
Cargo.toml
11
Cargo.toml
@@ -95,7 +95,6 @@ darkfi-serial = {path = "src/serial", optional = true}
|
||||
darkfi-derive = {path = "src/serial/derive", optional = true}
|
||||
itertools = {version = "0.11.0", optional = true}
|
||||
lazy_static = {version = "1.4.0", optional = true}
|
||||
# TODO: Test without serde
|
||||
url = {version = "2.4.0", features = ["serde"], optional = true}
|
||||
|
||||
# Misc
|
||||
@@ -297,16 +296,6 @@ name = "zk-inclusion-proof"
|
||||
path = "example/zk-inclusion-proof.rs"
|
||||
required-features = ["zk"]
|
||||
|
||||
[[example]]
|
||||
name = "rpc-server"
|
||||
path = "example/rpc/server.rs"
|
||||
required-features = ["async-runtime", "rpc"]
|
||||
|
||||
[[example]]
|
||||
name = "rpc-client"
|
||||
path = "example/rpc/client.rs"
|
||||
required-features = ["async-runtime", "rpc"]
|
||||
|
||||
[patch.crates-io]
|
||||
halo2_proofs = {git="https://github.com/parazyd/halo2", branch="v4"}
|
||||
halo2_gadgets = {git="https://github.com/parazyd/halo2", branch="v4"}
|
||||
|
||||
@@ -1,69 +0,0 @@
|
||||
/* This file is part of DarkFi (https://dark.fi)
|
||||
*
|
||||
* Copyright (C) 2020-2023 Dyne.org foundation
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
use async_std::sync::Arc;
|
||||
use serde_json::json;
|
||||
use smol::Executor;
|
||||
use url::Url;
|
||||
|
||||
use darkfi::{
|
||||
rpc::{client::RpcClient, jsonrpc::JsonRequest},
|
||||
Result,
|
||||
};
|
||||
|
||||
async fn realmain(ex: Arc<Executor<'_>>) -> Result<()> {
|
||||
//let endpoint = Url::parse("tcp://127.0.0.1:55422").unwrap();
|
||||
let endpoint = Url::parse("unix:///tmp/rpc.sock").unwrap();
|
||||
|
||||
let client = RpcClient::new(endpoint, Some(ex)).await?;
|
||||
|
||||
let req = JsonRequest::new("ping", json!([]));
|
||||
let rep = client.request(req).await?;
|
||||
|
||||
println!("{:#?}", rep);
|
||||
|
||||
let req = JsonRequest::new("kill", json!([]));
|
||||
let rep = client.request(req).await?;
|
||||
|
||||
println!("{:#?}", rep);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
simplelog::TermLogger::init(
|
||||
simplelog::LevelFilter::Debug,
|
||||
simplelog::ConfigBuilder::new().build(),
|
||||
simplelog::TerminalMode::Mixed,
|
||||
simplelog::ColorChoice::Auto,
|
||||
)?;
|
||||
|
||||
let n_threads = std::thread::available_parallelism().unwrap().get();
|
||||
let ex = Arc::new(Executor::new());
|
||||
let (signal, shutdown) = smol::channel::unbounded::<()>();
|
||||
let (_, result) = easy_parallel::Parallel::new()
|
||||
.each(0..n_threads, |_| smol::future::block_on(ex.run(shutdown.recv())))
|
||||
.finish(|| {
|
||||
smol::future::block_on(async {
|
||||
realmain(ex.clone()).await?;
|
||||
drop(signal);
|
||||
Ok::<(), darkfi::Error>(())
|
||||
})
|
||||
});
|
||||
|
||||
result
|
||||
}
|
||||
@@ -1,102 +0,0 @@
|
||||
/* This file is part of DarkFi (https://dark.fi)
|
||||
*
|
||||
* Copyright (C) 2020-2023 Dyne.org foundation
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
use async_std::sync::Arc;
|
||||
use async_trait::async_trait;
|
||||
use serde_json::{json, Value};
|
||||
use smol::{
|
||||
channel::{Receiver, Sender},
|
||||
Executor,
|
||||
};
|
||||
use url::Url;
|
||||
|
||||
use darkfi::{
|
||||
rpc::{
|
||||
jsonrpc::{ErrorCode, JsonError, JsonRequest, JsonResponse, JsonResult},
|
||||
server::{listen_and_serve, RequestHandler},
|
||||
},
|
||||
Result,
|
||||
};
|
||||
|
||||
struct RpcSrv {
|
||||
stop_sub: (Sender<()>, Receiver<()>),
|
||||
}
|
||||
|
||||
impl RpcSrv {
|
||||
async fn pong(&self, id: Value, _params: &[Value]) -> JsonResult {
|
||||
JsonResponse::new(json!("pong"), id).into()
|
||||
}
|
||||
|
||||
async fn kill(&self, id: Value, _params: &[Value]) -> JsonResult {
|
||||
self.stop_sub.0.send(()).await.unwrap();
|
||||
JsonResponse::new(json!("Bye"), id).into()
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl RequestHandler for RpcSrv {
|
||||
async fn handle_request(&self, req: JsonRequest) -> JsonResult {
|
||||
if !req.params.is_array() {
|
||||
return JsonError::new(ErrorCode::InvalidParams, None, req.id).into()
|
||||
}
|
||||
|
||||
let params = req.params.as_array().unwrap();
|
||||
|
||||
match req.method.as_str() {
|
||||
Some("ping") => return self.pong(req.id, params).await,
|
||||
Some("kill") => return self.kill(req.id, params).await,
|
||||
Some(_) | None => return JsonError::new(ErrorCode::MethodNotFound, None, req.id).into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn realmain(ex: Arc<Executor<'_>>) -> Result<()> {
|
||||
let rpcsrv = Arc::new(RpcSrv { stop_sub: smol::channel::unbounded::<()>() });
|
||||
//let rpc_listen = Url::parse("tcp://127.0.0.1:55422").unwrap();
|
||||
let rpc_listen = Url::parse("unix:///tmp/rpc.sock").unwrap();
|
||||
|
||||
let _ex = ex.clone();
|
||||
ex.spawn(listen_and_serve(rpc_listen, rpcsrv.clone(), _ex)).detach();
|
||||
|
||||
rpcsrv.stop_sub.1.recv().await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
simplelog::TermLogger::init(
|
||||
simplelog::LevelFilter::Debug,
|
||||
simplelog::ConfigBuilder::new().build(),
|
||||
simplelog::TerminalMode::Mixed,
|
||||
simplelog::ColorChoice::Auto,
|
||||
)?;
|
||||
|
||||
let n_threads = std::thread::available_parallelism().unwrap().get();
|
||||
let ex = Arc::new(Executor::new());
|
||||
let (signal, shutdown) = smol::channel::unbounded::<()>();
|
||||
let (_, result) = easy_parallel::Parallel::new()
|
||||
.each(0..n_threads, |_| smol::future::block_on(ex.run(shutdown.recv())))
|
||||
.finish(|| {
|
||||
smol::future::block_on(async {
|
||||
realmain(ex.clone()).await?;
|
||||
drop(signal);
|
||||
Ok::<(), darkfi::Error>(())
|
||||
})
|
||||
});
|
||||
|
||||
result
|
||||
}
|
||||
Reference in New Issue
Block a user