Accept addr
To deploy the JsonRpcInterface
and start receiving JSON-RPC requests,
we'll need to configure a JSON-RPC accept address.
Let's return to our functions alice()
and bob()
. To enable Alice and
Bob to connect to JSON-RPC, we'll need to generalize this return a RPC
Url
as well as a Settings
.
Let's define a new struct called AppSettings
that has two fields,
Url
and Settings
.
#[derive(Clone, Debug)]
struct AppSettings {
accept_addr: Url,
net: Settings,
}
impl AppSettings {
pub fn new(accept_addr: Url, net: Settings) -> Self {
Self { accept_addr, net }
}
}
Next, we'll change our alice()
method to return a AppSettings
instead of a Settings
.
fn alice() -> Result<AppSettings> {
let log_level = simplelog::LevelFilter::Debug;
let log_config = simplelog::Config::default();
let log_path = "/tmp/alice.log";
let file = File::create(log_path).unwrap();
WriteLogger::init(log_level, log_config, file)?;
let seed = Url::parse("tcp://127.0.0.1:50515").unwrap();
let inbound = Url::parse("tcp://127.0.0.1:51554").unwrap();
let ext_addr = Url::parse("tcp://127.0.0.1:51554").unwrap();
let net = Settings {
inbound: vec![inbound],
external_addr: vec![ext_addr],
seeds: vec![seed],
localnet: true,
..Default::default()
};
let accept_addr = Url::parse("tcp://127.0.0.1:55054").unwrap();
let settings = AppSettings::new(accept_addr, net);
Ok(settings)
}
And the same for bob()
:
fn bob() -> Result<AppSettings> {
let log_level = simplelog::LevelFilter::Debug;
let log_config = simplelog::Config::default();
let log_path = "/tmp/bob.log";
let file = File::create(log_path).unwrap();
WriteLogger::init(log_level, log_config, file)?;
let seed = Url::parse("tcp://127.0.0.1:50515").unwrap();
let net = Settings {
inbound: vec![],
outbound_connections: 5,
seeds: vec![seed],
localnet: true,
..Default::default()
};
let accept_addr = Url::parse("tcp://127.0.0.1:51054").unwrap();
let settings = AppSettings::new(accept_addr, net);
Ok(settings)
}
Update main()
with the new type:
#[async_std::main]
async fn main() -> Result<()> {
let settings: Result<AppSettings> = match std::env::args().nth(1) {
Some(id) => match id.as_str() {
"a" => alice(),
"b" => bob(),
_ => Err(ErrorMissingSpecifier.into()),
},
None => Err(ErrorMissingSpecifier.into()),
};
let settings = settings?.clone();
let p2p = net::P2p::new(settings.net).await;
//...
}
}