Skip to content

Commit

Permalink
more cleanup, update to ldk 106
Browse files Browse the repository at this point in the history
  • Loading branch information
johncantrell97 committed Apr 3, 2022
1 parent b6f2b9a commit 819e1c2
Show file tree
Hide file tree
Showing 17 changed files with 223 additions and 148 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
./target
28 changes: 20 additions & 8 deletions Cargo.lock

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

12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ name = "senseid"
path = "src/main.rs"

[dependencies]
lightning = { version = "0.0.104", features = ["max_level_trace"], path = "/Users/developer/Development/rust-lightning/lightning" }
lightning-block-sync = { version = "0.0.104", features = [ "rpc-client" ], path = "/Users/developer/Development/rust-lightning/lightning-block-sync" }
lightning-invoice = { version = "0.12.0", path = "/Users/developer/Development/rust-lightning/lightning-invoice" }
lightning-net-tokio = { version = "0.0.104", path = "/Users/developer/Development/rust-lightning/lightning-net-tokio" }
lightning-persister = { version = "0.0.104", path = "/Users/developer/Development/rust-lightning/lightning-persister" }
lightning-background-processor = { version = "0.0.104", path = "/Users/developer/Development/rust-lightning/lightning-background-processor" }
lightning = { version = "0.0.106", features = ["max_level_trace"] }
lightning-block-sync = { version = "0.0.106", features = [ "rpc-client" ] }
lightning-invoice = { version = "0.14.0" }
lightning-net-tokio = { version = "0.0.106" }
lightning-persister = { version = "0.0.106" }
lightning-background-processor = { version = "0.0.106" }

base64 = "0.13.0"
bitcoin = "0.27"
Expand Down
19 changes: 19 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM rust:1.56 as build

WORKDIR /senseid

# copy your source tree
COPY . .

RUN rustup component add rustfmt

RUN cargo build --verbose --release

# our final base
FROM debian:buster-slim

# copy the build artifact from the build stage
COPY --from=build /senseid/target/release/senseid .

# set the startup command to run your binary
CMD ["./senseid"]
2 changes: 1 addition & 1 deletion src/chain/listener_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl ListenerDatabase {
let mut outputs_sum: u64 = 0;

// look for our own inputs
for (i, input) in tx.input.iter().enumerate() {
for (_i, input) in tx.input.iter().enumerate() {
if let Some(previous_output) = database
.get_previous_output(&input.previous_output)
.unwrap()
Expand Down
4 changes: 1 addition & 3 deletions src/database/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
// You may not use this file except in accordance with one or both of these
// licenses.

use std::time::SystemTime;

use super::Error;
use crate::utils::{self, seconds_since_epoch};
use crate::utils::{self};
use crate::{
hex_utils,
services::{PaginationRequest, PaginationResponse},
Expand Down
1 change: 0 additions & 1 deletion src/database/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use crate::{
services::{PaginationRequest, PaginationResponse, PaymentsFilter},
};
use bitcoin::consensus::encode::{deserialize, serialize};
use std::str::FromStr;

use super::Error;
use bitcoin::BlockHash;
Expand Down
72 changes: 49 additions & 23 deletions src/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,28 @@
// You may not use this file except in accordance with one or both of these
// licenses.

use crate::node;
use crate::chain::broadcaster::SenseiBroadcaster;
use crate::chain::fee_estimator::SenseiFeeEstimator;
use crate::node::{self, ChannelManager, ChainMonitor};
use bitcoin::secp256k1::key::PublicKey;
use bitcoin::BlockHash;
use chrono::Utc;
use lightning::chain::keysinterface::{InMemorySigner, KeysManager};
use lightning::routing::network_graph::NetworkGraph;
use lightning::routing::scoring::Scorer;
use lightning::routing::scoring::{ProbabilisticScorer, ProbabilisticScoringParameters};
use lightning::util::logger::{Logger, Record};
use lightning::util::ser::{Readable, Writeable, Writer};
use lightning::util::ser::{Readable, ReadableArgs, Writeable, Writer};
use lightning_background_processor::{Persister};
use lightning_persister::FilesystemPersister;


use std::collections::HashMap;
use std::fs;
use std::fs::File;
use std::io::{BufRead, BufReader, BufWriter};
use std::net::SocketAddr;
use std::path::Path;
use std::sync::Arc;

pub struct FilesystemLogger {
data_dir: String,
Expand Down Expand Up @@ -86,22 +94,6 @@ pub fn read_channel_peer_data(
Ok(peer_data)
}

pub fn persist_network(path: &Path, network_graph: &NetworkGraph) -> std::io::Result<()> {
let mut tmp_path = path.to_path_buf().into_os_string();
tmp_path.push(".tmp");
let file = fs::OpenOptions::new()
.write(true)
.create(true)
.open(&tmp_path)?;
let write_res = network_graph.write(&mut BufWriter::new(file));
if let Err(e) = write_res.and_then(|_| fs::rename(&tmp_path, path)) {
let _ = fs::remove_file(&tmp_path);
Err(e)
} else {
Ok(())
}
}

pub fn read_network(path: &Path, genesis_hash: BlockHash) -> NetworkGraph {
if let Ok(file) = File::open(path) {
if let Ok(graph) = NetworkGraph::read(&mut BufReader::new(file)) {
Expand All @@ -111,7 +103,7 @@ pub fn read_network(path: &Path, genesis_hash: BlockHash) -> NetworkGraph {
NetworkGraph::new(genesis_hash)
}

pub fn persist_scorer(path: &Path, scorer: &Scorer) -> std::io::Result<()> {
pub fn persist_scorer(path: &Path, scorer: &ProbabilisticScorer<Arc<NetworkGraph>>) -> std::io::Result<()> {
let mut tmp_path = path.to_path_buf().into_os_string();
tmp_path.push(".tmp");
let file = fs::OpenOptions::new()
Expand All @@ -127,11 +119,45 @@ pub fn persist_scorer(path: &Path, scorer: &Scorer) -> std::io::Result<()> {
}
}

pub fn read_scorer(path: &Path) -> Scorer {
pub fn read_scorer(path: &Path, graph: Arc<NetworkGraph>) -> ProbabilisticScorer<Arc<NetworkGraph>> {
let params = ProbabilisticScoringParameters::default();
if let Ok(file) = File::open(path) {
if let Ok(scorer) = Scorer::read(&mut BufReader::new(file)) {
if let Ok(scorer) =
ProbabilisticScorer::read(&mut BufReader::new(file), (params, Arc::clone(&graph))) {
return scorer;
}
}
Scorer::default()
ProbabilisticScorer::new(params, graph)
}

pub struct DataPersister {
pub data_dir: String,
pub external_router: bool
}

impl
Persister<
InMemorySigner,
Arc<ChainMonitor>,
Arc<SenseiBroadcaster>,
Arc<KeysManager>,
Arc<SenseiFeeEstimator>,
Arc<FilesystemLogger>,
> for DataPersister
{
fn persist_manager(&self, channel_manager: &ChannelManager) -> Result<(), std::io::Error> {
FilesystemPersister::persist_manager(self.data_dir.clone(), channel_manager)
}

fn persist_graph(&self, network_graph: &NetworkGraph) -> Result<(), std::io::Error> {
if !self.external_router {
if FilesystemPersister::persist_network_graph(self.data_dir.clone(), network_graph).is_err()
{
// Persistence errors here are non-fatal as we can just fetch the routing graph
// again later, but they may indicate a disk error which could be fatal elsewhere.
eprintln!("Warning: Failed to persist network graph, check your disk and permissions");
}
}
Ok(())
}
}
5 changes: 4 additions & 1 deletion src/event_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ pub struct LightningNodeEventHandler {
impl EventHandler for LightningNodeEventHandler {
fn handle_event(&self, event: &Event) {
match event {
Event::OpenChannelRequest { .. } => {
// Unreachable, we don't set manually_accept_inbound_channels
},
Event::FundingGenerationReady {
temporary_channel_id,
channel_value_satoshis,
Expand All @@ -68,7 +71,7 @@ impl EventHandler for LightningNodeEventHandler {
let wallet = self.wallet.lock().unwrap();

let mut tx_builder = wallet.build_tx();
let fee_sats_per_1000_wu = self
let _fee_sats_per_1000_wu = self
.chain_manager
.bitcoind_client
.get_est_sat_per_1000_weight(ConfirmationTarget::Normal);
Expand Down
1 change: 0 additions & 1 deletion src/grpc/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use super::sensei::{
use crate::{
database::admin::AccessToken,
services::admin::{AdminRequest, AdminResponse},
utils,
};
use tonic::{metadata::MetadataMap, Response, Status};

Expand Down
15 changes: 15 additions & 0 deletions src/hex_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,22 @@ pub fn hex_str(value: &[u8]) -> String {
res
}

pub fn sanitize_string(bytes: &[u8]) -> String {
let mut ret = String::with_capacity(bytes.len());
// We should really support some sane subset of UTF-8 here, but limiting to printable ASCII
// instead makes this trivial.
for b in bytes {
if *b >= 0x20 && *b <= 0x7e {
ret.push(*b as char);
}
}
ret
}

pub fn to_compressed_pubkey(hex: &str) -> Option<PublicKey> {
if hex.len() != 33 * 2 {
return None;
}
let data = match to_vec(&hex[0..33 * 2]) {
Some(bytes) => bytes,
None => return None,
Expand Down
Loading

0 comments on commit 819e1c2

Please sign in to comment.