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

Refacroting #26

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ lightning-persister = { git = "https://github.com/lightningdevkit/rust-lightning
log = "0.4.17"
tokio = { version = "1.7.1", features = ["fs", "rt-multi-thread"] }
tokio-postgres = "0.7.7"
fedimint-tonic-lnd = "0.1.0"
tonic = { version = "0.6.2", features = ["transport", "tls"] }
prost = "0.9.0"
lnd_grpc_rust = "2.3.0"
rustls = { version = "0.19.0", features = ["dangerous_configuration"] }
refinery = { version = "0.8", features = ["tokio-postgres"]}
log4rs = "1.2.0"
Expand All @@ -54,4 +52,4 @@ protobuf-codegen = "3.2.0"

[dev-dependencies]
tokio = { version = "1.7.1", features = ["rt-multi-thread"] }
once_cell = "1.18.0"
once_cell = "1.18.0"
3 changes: 1 addition & 2 deletions client/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
mod client;
mod services;


use bdk::bitcoin::secp256k1::XOnlyPublicKey;
use looper::{
Expand Down
60 changes: 60 additions & 0 deletions docker-compose.network.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
version: '3.3'
name: polar-network-1
services:
backend1:
environment:
USERID: ${USERID:-1000}
GROUPID: ${GROUPID:-1000}
stop_grace_period: 5m
image: polarlightning/bitcoind:25.0
container_name: polar-n1-backend1
hostname: backend1
command: >-
bitcoind -server=1 -regtest=1
-rpcauth=polaruser:5e5e98c21f5c814568f8b55d83b23c1c$$066b03f92df30b11de8e4b1b1cd5b1b4281aa25205bd57df9be82caf97a05526
-debug=1 -zmqpubrawblock=tcp://0.0.0.0:28334
-zmqpubrawtx=tcp://0.0.0.0:28335 -zmqpubhashblock=tcp://0.0.0.0:28336
-txindex=1 -dnsseed=0 -upnp=0 -rpcbind=0.0.0.0 -rpcallowip=0.0.0.0/0
-rpcport=18443 -rest -listen=1 -listenonion=0 -fallbackfee=0.0002
-blockfilterindex=1 -peerblockfilters=1
volumes:
- ./volumes/bitcoind/backend1:/home/bitcoin/.bitcoin
expose:
- '18443'
- '18444'
- '28334'
- '28335'
ports:
- '18444:18443'
- '19445:18444'
- '28335:28334'
- '29336:28335'
alice:
environment:
USERID: ${USERID:-1000}
GROUPID: ${GROUPID:-1000}
stop_grace_period: 2m
image: polarlightning/lnd:0.15.0-beta
container_name: polar-n1-alice
hostname: alice
command: >-
lnd --noseedbackup --trickledelay=5000 --alias=alice --externalip=alice
--tlsextradomain=alice --tlsextradomain=polar-n1-alice
--tlsextradomain=host.docker.internal --listen=0.0.0.0:9735
--rpclisten=0.0.0.0:10009 --restlisten=0.0.0.0:8080 --bitcoin.active
--bitcoin.regtest --bitcoin.node=bitcoind
--bitcoind.rpchost=polar-n1-backend1 --bitcoind.rpcuser=polaruser
--bitcoind.rpcpass=polarpass
--bitcoind.zmqpubrawblock=tcp://polar-n1-backend1:28334
--bitcoind.zmqpubrawtx=tcp://polar-n1-backend1:28335
restart: always
volumes:
- ./volumes/lnd/alice:/home/lnd/.lnd
expose:
- '8080'
- '10009'
- '9735'
ports:
- '8085:8080'
- '10005:10009'
- '9739:9735'
4 changes: 2 additions & 2 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ services:
- POSTGRES_PASSWORD=postgres
- PGDATA=/var/lib/postgresql/data/pgdata
volumes:
- postgres-volume:/var/lib/postgresql/data
- ./init/looper.sql:/docker-entrypoint-initdb.d/looper.sql
restart: on-failure
ports:
- "5432:5432"

volumes:
postgres-volume:
postgres-volume:
1 change: 1 addition & 0 deletions init/looper.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE DATABASE looper;
82 changes: 45 additions & 37 deletions src/lnd/client.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
use crate::{settings, utils};
use hex;
use lnd_grpc_rust::{lnrpc, LndClient};
use std::collections::HashMap;
use std::error::Error;
use std::fmt::Write;
use std::fs;
use serde::{Deserialize, Serialize};
use tokio::sync::{Mutex, MutexGuard};

use tokio::sync::Mutex;

use fedimint_tonic_lnd::{
invoicesrpc,
lnrpc::{self, FeeLimit},
routerrpc, Client,
};

#[derive(Clone)]
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct LNDConfig {
pub address: String,
pub cert_path: String,
Expand All @@ -26,36 +24,39 @@ pub fn get_lnd_config(cfg: &settings::Config) -> Result<LNDConfig, LNDGatewayErr
Err(_) => DEFAULT_INVOICE_LIFETIME,
};

let address = cfg
.get("lnd.address")
.map_err(|e| LNDGatewayError::new(format!("lnd.address not set: {:?}", e.to_string())))?;
let cert_path = cfg
.get("lnd.cert_path")
.map_err(|e| LNDGatewayError::new(format!("lnd.cert_path not set: {:?}", e.to_string())))?;
let macaroon_path = cfg.get("lnd.macaroon_path").map_err(|e| {
LNDGatewayError::new(format!("lnd.macaroon_path not set: {:?}", e.to_string()))
})?;
// extract all files necessary to connect to lnd
let host = cfg.get::<String>("lnd.address").expect("FailedToReadLndHost");
let cert_path = cfg.get::<String>("lnd.cert_path").expect("FailedToReadLndHost");
let macaroon_path = cfg.get::<String>("lnd.macaroon_path").expect("FailedToReadLndHost");

// read the files and convert in bytes
let cert_bytes = fs::read(cert_path).expect("FailedToReadTlsCertFile");
let mac_bytes = fs::read(macaroon_path).expect("FailedToReadMacaroonFile");

// Convert the bytes to a hex string
let cert = buffer_as_hex(cert_bytes);
let macaroon = buffer_as_hex(mac_bytes);

Ok(LNDConfig {
address,
cert_path,
macaroon_path,
cert_path: cert,
macaroon_path: macaroon,
address: host,
invoice_lifetime: invoice_lifetime as i64,
})
}

pub async fn new_client(cfg: LNDConfig) -> Result<Client, fedimint_tonic_lnd::ConnectError> {
fedimint_tonic_lnd::connect(
cfg.address.clone(),
pub async fn new_client(cfg: LNDConfig) -> Result<LndClient, Box<dyn Error>> {
lnd_grpc_rust::connect(
cfg.cert_path.clone(),
cfg.macaroon_path.clone(),
cfg.address.clone(),
)
.await
}

pub struct LNDGateway {
cfg: LNDConfig,
client: Mutex<Client>,
client: Mutex<LndClient>,
}

#[derive(Debug)]
Expand Down Expand Up @@ -84,31 +85,31 @@ impl LNDGateway {
})
}

async fn get_client(&self) -> tokio::sync::MutexGuard<'_, Client> {
async fn get_client(&self) -> MutexGuard<'_, LndClient> {
self.client.lock().await
}

pub async fn get_info(&self) -> Result<lnrpc::GetInfoResponse, fedimint_tonic_lnd::Error> {
/* pub async fn get_info(&self) -> Result<lnrpc::, lnd_grpc_rust::LndClientError> {
let mut client = self.get_client().await;
let resp = client.lightning().get_info(lnrpc::GetInfoRequest {}).await;
match resp {
Ok(resp) => Ok(resp.into_inner()),
Err(e) => Err(e),
}
}
}*/

pub async fn add_invoice(
&self,
value: i64,
) -> Result<AddInvoiceResp, fedimint_tonic_lnd::Error> {
) -> Result<AddInvoiceResp, lnd_grpc_rust::LndClientError> {
let mut client = self.get_client().await;

// TODO: do we have to generate this?
let (preimage, payment_hash) = Self::new_preimage();
let payment_addr = LNDGateway::new_payment_addr();
// resolves lint vs compile error dilemma
#[allow(deprecated)]
let req = lnrpc::Invoice {
let req = lnd_grpc_rust::lnrpc::Invoice {
memo: "looper swap out".to_string(),
r_preimage: preimage.to_vec(),
r_hash: payment_hash.to_vec(),
Expand Down Expand Up @@ -159,11 +160,11 @@ impl LNDGateway {
&self,
value: i64,
cltv_timout: u64,
) -> Result<AddInvoiceResp, fedimint_tonic_lnd::Error> {
) -> Result<AddInvoiceResp, lnd_grpc_rust::LndClientError> {
let mut client = self.get_client().await;
let (preimage, payment_hash) = Self::new_preimage();

let req = invoicesrpc::AddHoldInvoiceRequest {
let req = lnd_grpc_rust::invoicesrpc::AddHoldInvoiceRequest {
memo: "looper swap out".to_string(),
hash: payment_hash.to_vec(),
value,
Expand Down Expand Up @@ -196,11 +197,11 @@ impl LNDGateway {
&self,
invoice: String,
fee_limit: i64,
) -> Result<(), fedimint_tonic_lnd::Error> {
) -> Result<(), lnd_grpc_rust::LndClientError> {
let mut client = self.get_client().await;
// resolves lint vs compile error dilemma
#[allow(deprecated)]
let req = routerrpc::SendPaymentRequest {
let req = lnd_grpc_rust::routerrpc::SendPaymentRequest {
payment_request: invoice,
timeout_seconds: 600,
amt: 0,
Expand Down Expand Up @@ -240,11 +241,11 @@ impl LNDGateway {
&self,
invoice: String,
fee_limit: i64,
) -> Result<lnrpc::SendResponse, fedimint_tonic_lnd::Error> {
) -> Result<lnd_grpc_rust::lnrpc::SendResponse, lnd_grpc_rust::LndClientError> {
let mut client = self.get_client().await;
// resolves lint vs compile error dilemma
#[allow(deprecated)]
let req = lnrpc::SendRequest {
let req = lnd_grpc_rust::lnrpc::SendRequest {
dest: vec![],
dest_string: "".to_string(),
amt: 0,
Expand All @@ -254,7 +255,7 @@ impl LNDGateway {
payment_request: invoice,
// TODO: SET ME to cltv_delta + block height so that we can ensure the invoice can't be held too long
final_cltv_delta: 0,
fee_limit: Some(FeeLimit {
fee_limit: Some(lnrpc::FeeLimit {
limit: Some(lnrpc::fee_limit::Limit::Fixed(fee_limit)),
}),
outgoing_chan_id: 0,
Expand Down Expand Up @@ -287,6 +288,13 @@ impl LNDGateway {
}
}

pub fn buffer_as_hex(bytes: Vec<u8>) -> String {
bytes.iter().fold(String::new(), |mut output, b| {
let _ = write!(output, "{b:02X}");
output
})
}

// TODO: should we make every method return this error or is fedimint_tonic_lnd::Error sufficient?
#[derive(Debug)]
pub struct LNDGatewayError {
Expand Down
1 change: 1 addition & 0 deletions src/lnd/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod client;
pub mod invoice_tracker;
45 changes: 25 additions & 20 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#[macro_use]
extern crate rocket;

// #[macro_use]
// extern crate diesel;
use std::io::{self, BufRead};

// use bdk::bitcoin::secp256k1::PublicKey;
use db::DB;

use crate::lnd::client::LNDGateway;

mod api;
mod db;
Expand All @@ -15,36 +19,37 @@ pub mod settings;
mod utils;
pub mod wallet;

use crate::lnd::client::LNDGateway;
// use bdk::bitcoin::secp256k1::PublicKey;
use db::DB;
use std::io::{self, BufRead};
// use std::str::FromStr;

// use rand::Rng;

//
#[tokio::main]
async fn main() {
run().await;
}

async fn run() {
let cfg = settings::build_config().unwrap();

let db = DB::new(&cfg);

let migration_conn = &mut db.get_conn().unwrap();
db::run_migrations(migration_conn).unwrap();

let wallet = wallet::LooperWallet::new(&cfg).unwrap();

let lndg = LNDGateway::new().await.unwrap();

let loopout_svc = services::loop_out::LoopOutService::new(&cfg, db, wallet, lndg).unwrap();

let server = api::server::LooperServer::new(loopout_svc);
server.start();

let stdin = io::stdin();
let _line = stdin.lock().lines().next().unwrap().unwrap();
match wallet::LooperWallet::new(&cfg) {
Ok(wallet) => match LNDGateway::new().await {
Ok(lndg) => {
let loopout_svc =
services::loop_out::LoopOutService::new(&cfg, db, wallet, lndg).unwrap();
let server = api::server::LooperServer::new(loopout_svc);
server.start();
let stdin = io::stdin();
let _line = stdin.lock().lines().next().unwrap().unwrap();
}
Err(err) => {
panic!("could not start the lndGateway {} ", err.msg);
}
},
Err(err) => {
panic!("could not create the wallet {} ", err.message);
}
}
}
2 changes: 1 addition & 1 deletion src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub fn build_config() -> Result<Config, ConfigError> {
log::info!("loading config for {}", profile);

Config::builder()
.add_source(File::with_name("config/default"))
.add_source(File::with_name("config/example"))
.add_source(File::with_name(&format!("config/{}", profile)).required(false))
.add_source(Environment::with_prefix("RLS").separator("_"))
.build()
Expand Down
Loading