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

Adding RPC Configuration #531

Merged
merged 5 commits into from
Jul 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 2 additions & 4 deletions forest/src/cli/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ pub struct Config {
pub genesis_file: Option<String>,
pub drand_dist_public: DistPublic,
pub enable_rpc: bool,
pub rpc_port: Option<String>,
pub rpc_listen: Option<String>,
pub rpc_port: String,
}

impl Default for Config {
Expand All @@ -28,8 +27,7 @@ impl Default for Config {
hex::decode("94eebfd53f4ba6a3b8304236400a12e73885e5a781509a5c8d41d2e8b476923d8ea6052649b3c17282f596217f96c5de").unwrap(),
hex::decode("8dc4231e42b4edf39e86ef1579401692480647918275da767d3e558c520d6375ad953530610fd27daf110187877a65d0").unwrap(),]},
enable_rpc : true,
rpc_port: Some("/api".to_string()),
rpc_listen: Some("127.0.0.1:8080".to_string())
rpc_port: "1234".to_string()
}
}
}
10 changes: 10 additions & 0 deletions forest/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ pub struct DaemonOpts {
pub config: Option<String>,
#[structopt(short, long, help = "The genesis CAR file")]
pub genesis: Option<String>,
#[structopt(short, long, help = "Allow rpc to be active or not")]
pub rpc: Option<bool>,
#[structopt(short, long, help = "The port used for communication")]
pub port: Option<String>,
}

impl DaemonOpts {
Expand All @@ -72,6 +76,12 @@ impl DaemonOpts {
if let Some(genesis_file) = &self.genesis {
cfg.genesis_file = Some(genesis_file.to_owned());
}
if self.rpc.unwrap_or(cfg.enable_rpc) {
cfg.enable_rpc = true;
cfg.rpc_port = self.port.to_owned().unwrap_or(cfg.rpc_port);
} else {
cfg.enable_rpc = false;
}
// (where to find these flags, should be easy to do with structops)

Ok(cfg)
Expand Down
25 changes: 7 additions & 18 deletions forest/src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use chain_sync::ChainSyncer;
use db::RocksDb;
use forest_libp2p::{get_keypair, Libp2pService};
use libp2p::identity::{ed25519, Keypair};
use log::{info, trace};
use log::{debug, info, trace};
use rpc::start_rpc;
use std::sync::Arc;
use utils::write_to_file;
Expand All @@ -35,7 +35,7 @@ pub(super) async fn start(config: Config) {
});

// Initialize database
let mut db = RocksDb::new(config.data_dir + "/db");
let mut db = RocksDb::new(config.data_dir.clone() + "/db");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let mut db = RocksDb::new(config.data_dir.clone() + "/db");
let mut db = RocksDb::new(config.data_dir + "/db");

nothing you added should change this, should it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was doing some dumb stuff earlier, before this set up i kept running into partial move issues. I thought my only way out of it was to clone stuff, should've changed this back when i started doing it this way.

db.open().unwrap();
let db = Arc::new(db);
let mut chain_store = ChainStore::new(Arc::clone(&db));
Expand Down Expand Up @@ -76,26 +76,15 @@ pub(super) async fn start(config: Config) {

let db_rpc = Arc::clone(&db);

// Use the provided RPC port and listener or use the default one
let rpc_port = config
.rpc_port
.unwrap_or_else(|| "/api".to_string())
.to_owned();
let rpc_listen = config
.rpc_listen
.unwrap_or_else(|| "127.0.0.1:8080".to_string())
.to_owned();

let rpc_thread = if config.enable_rpc {
task::spawn(async {
info!("RPC enabled");
info!("Using {} as RPC port", rpc_port);
info!("Using {} as RPC listen", rpc_listen);
start_rpc(db_rpc, rpc_port, rpc_listen).await;
let rpc_listen = "127.0.0.1:".to_string() + &config.rpc_port;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let rpc_listen = "127.0.0.1:".to_string() + &config.rpc_port;
let rpc_listen = format!("127.0.0.1:{}", &config.rpc_port);

optional: just a bit more readable

task::spawn(async move {
info!("JSON RPC Endpoint at {}", &rpc_listen);
start_rpc(db_rpc, &rpc_listen).await;
})
} else {
task::spawn(async {
info!("RPC disabled");
debug!("RPC disabled");
})
};

Expand Down
10 changes: 3 additions & 7 deletions node/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ async fn handle_json_rpc(mut req: Request<Server<MapRouter>>) -> tide::Result {
Ok(Response::new(StatusCode::Ok).body_json(&res)?)
}

pub async fn start_rpc<DB: BlockStore + Send + Sync + 'static>(
store: Arc<DB>,
rpc_port: String,
rpc_listen: String,
) {
pub async fn start_rpc<DB: BlockStore + Send + Sync + 'static>(store: Arc<DB>, rpc_endpoint: &str) {
let rpc = Server::new()
.with_data(Data::new(State { store }))
.with_method(
Expand Down Expand Up @@ -57,6 +53,6 @@ pub async fn start_rpc<DB: BlockStore + Send + Sync + 'static>(
.with_method("Filecoin.ChainHead", chain_api::chain_head::<DB>)
.finish_unwrapped();
let mut app = tide::Server::with_state(rpc);
app.at(&rpc_port).post(handle_json_rpc);
app.listen(&rpc_listen).await.unwrap();
app.at("/api").post(handle_json_rpc);
app.listen(rpc_endpoint).await.unwrap();
}