-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add lightning peach grpc client and corresponding module to testenv
- Loading branch information
Vladislav Melnik
committed
Feb 6, 2019
1 parent
8ea01f5
commit 5704f97
Showing
8 changed files
with
145 additions
and
13 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters