Skip to content

Commit

Permalink
add lightning peach grpc client and corresponding module to testenv
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladislav Melnik committed Feb 6, 2019
1 parent 8ea01f5 commit 5704f97
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 13 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions rpc/client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
name = "client"
version = "0.0.0"
authors = ["LightningPeach <contact@lightningpeach.com>"]
edition = "2018"

[[bin]]
name = "client"

[lib]
name = "client"

[dependencies]
grpc = "0.6.*"
tls-api = "0.1.*"
Expand Down
46 changes: 46 additions & 0 deletions rpc/client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use grpc::{Client, ClientStub, Error};
use std::{sync::Arc, net::SocketAddr};

use interface::{
routing_grpc::{RoutingServiceClient, RoutingService},
channel_grpc::{ChannelServiceClient, ChannelService},
payment_grpc::{PaymentServiceClient, PaymentService},
};

pub struct LightningPeach(Arc<Client>);

impl LightningPeach {
pub fn new(socket_address: &SocketAddr) -> Result<Self, Error> {
use tls_api_stub::TlsConnector;
use httpbis::ClientTlsOption;

let host = socket_address.ip().to_string();
let client = Client::new_expl::<TlsConnector>(
&socket_address,
host.as_str(),
ClientTlsOption::Plain,
Default::default()
)?;

Ok(LightningPeach(Arc::new(client)))
}

pub fn local(port: u16) -> Result<Self, Error> {
use std::net::{SocketAddrV4, Ipv4Addr};

let socket_address = SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::new(127, 0, 0, 1), port));
Self::new(&socket_address)
}

pub fn routing(&self) -> impl RoutingService {
RoutingServiceClient::with_client(self.0.clone())
}

pub fn channel(&self) -> impl ChannelService {
ChannelServiceClient::with_client(self.0.clone())
}

pub fn payment(&self) -> impl PaymentService {
PaymentServiceClient::with_client(self.0.clone())
}
}
9 changes: 0 additions & 9 deletions rpc/client/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
extern crate grpc;
extern crate tls_api;
extern crate tls_api_native_tls;
extern crate tls_api_stub;
extern crate httpbis;
extern crate futures;

extern crate interface;

use grpc::Error as GrpcError;
use httpbis::Error as HttpbisError;

Expand Down
1 change: 1 addition & 0 deletions testenv/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ lazycell = "1.2"
hex = "0.3"
bitcoin_rpc_client = { git = "https://github.com/LightningPeach/bitcoinrpc-rust-client.git" }
bitcoin = "0.14.0"
client = { path = "../rpc/client" }
8 changes: 4 additions & 4 deletions testenv/src/ln.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct LnDaemon {
}

pub struct LnRunning {
daemon: LnDaemon,
config: LnDaemon,
instance: Child,
client: LazyCell<LightningClient>,
info: LazyCell<GetInfoResponse>,
Expand Down Expand Up @@ -77,7 +77,7 @@ impl LnDaemon {
.spawn()
.map(|instance| {
LnRunning {
daemon: self,
config: self,
instance: instance,
client: LazyCell::new(),
info: LazyCell::new(),
Expand Down Expand Up @@ -112,7 +112,7 @@ impl LnRunning {
use lnd_rust::tls_certificate::TLSCertificate;
use grpc::ClientStub;

let daemon = &self.daemon;
let daemon = &self.config;

let certificate = TLSCertificate::from_path(daemon.home.public_key_path())
.map_err(grpc::Error::Io)?;
Expand Down Expand Up @@ -167,7 +167,7 @@ impl LnRunning {

pub fn address(&self) -> LightningAddress {
let mut address = LightningAddress::new();
address.set_host(format!("127.0.0.1:{}", self.daemon.peer_port));
address.set_host(format!("127.0.0.1:{}", self.config.peer_port));
address.set_pubkey(self.info().get_identity_pubkey().to_owned());
address
}
Expand Down
86 changes: 86 additions & 0 deletions testenv/src/lp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
use super::{Home, cleanup};
use super::chain::BitcoinConfig;

use client::LightningPeach;
use std::{process::Child, io};
use lazycell::LazyCell;

pub struct LpServer {
peer_port: u16,
rpc_port: u16,
home: Home,
}

pub struct LpRunning {
config: LpServer,
instance: Child,
client: LazyCell<LightningPeach>,
}

impl LpServer {
pub fn name(&self) -> &str {
self.home.name()
}

pub fn new(
peer_port: u16, rpc_port: u16, name: &str
) -> Result<Self, io::Error> {
Ok(LpServer {
peer_port: peer_port,
rpc_port: rpc_port,
home: Home::new(name, false)
.or_else(|e| if e.kind() == io::ErrorKind::AlreadyExists {
cleanup("lpd");
Home::new(name, true)
} else {
Err(e)
})?,
})
}

pub fn run<B>(self, b: &B) -> Result<LpRunning, io::Error>
where
B: BitcoinConfig,
{
use std::process::Command;

// TODO: pass lightning arguments into the node
let _ = b;

Command::new("lpd")
.args(&[
format!("--listen=localhost:{}", self.peer_port),
format!("--rpclisten=localhost:{}", self.rpc_port),
])
.spawn()
.map(|instance| {
LpRunning {
config: self,
instance: instance,
client: LazyCell::new(),
}
})
}

}

impl LpRunning {
/// might panic
pub fn client(&self) -> &LightningPeach {
self.client.borrow().unwrap_or_else(|| {
self.client.fill(LightningPeach::local(self.config.rpc_port).unwrap()).ok().unwrap();
self.client()
})
}
}

impl Drop for LpRunning {
fn drop(&mut self) {
self.instance.kill()
.or_else(|e| match e.kind() {
io::ErrorKind::InvalidInput => Ok(()),
_ => Err(e),
})
.unwrap()
}
}
3 changes: 3 additions & 0 deletions testenv/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ pub use self::chain::*;
mod ln;
pub use self::ln::{LnDaemon, LnRunning};

mod lp;
pub use self::lp::{LpServer, LpRunning};

#[cfg(any(target_os = "linux", target_os = "macos"))]
pub fn cleanup(process: &str) {
use std::process::Command;
Expand Down

0 comments on commit 5704f97

Please sign in to comment.