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.

#![allow(unused)]
fn main() {
#[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.

#![allow(unused)]
fn main() {
fn alice() -> Result<AppSettings> {
    //...
    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],
        ..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():

#![allow(unused)]
fn main() {
fn bob() -> Result<AppSettings> {
    //...
    let net = Settings {
        inbound: vec![],
        outbound_connections: 5,
        seeds: vec![seed],
        ..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;
    //...
}