Skip to content

Commit

Permalink
Merge pull request #79 from L2-Technology/batch-channel-opens
Browse files Browse the repository at this point in the history
batch channel opens
  • Loading branch information
johncantrell97 authored Jun 4, 2022
2 parents 4ed8ed4 + 6e0130f commit 97a6f7a
Show file tree
Hide file tree
Showing 24 changed files with 826 additions and 291 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/target
/.sensei-tests
./senseicore/.sensei-tests
/senseicore/.sensei-tests
.DS_Store
*_pb2.py
*_pb2_grpc.py
Expand Down
73 changes: 28 additions & 45 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ bitcoin-bech32 = "0.12"
bech32 = "0.8"
futures = "0.3"
chrono = "0.4"
rand = "0.4"
rand = "0.8.4"
axum = { version = "0.4.2", features = ["headers"] }
http = "0.2"
tower = { version = "0.4", features = ["full"] }
Expand Down Expand Up @@ -64,6 +64,7 @@ tonic-build = "0.6"

[dev-dependencies]
bitcoind = { version = "0.26", features = [ "22_0" ] }
serial_test = "0.6.0"

[[test]]
name = "senseicore"
Expand Down
19 changes: 15 additions & 4 deletions proto/sensei.proto
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ service Node {
rpc StopNode (StopNodeRequest) returns (StopNodeResponse);
rpc GetUnusedAddress (GetUnusedAddressRequest) returns (GetUnusedAddressResponse);
rpc GetBalance (GetBalanceRequest) returns (GetBalanceResponse);
rpc OpenChannel (OpenChannelRequest) returns (OpenChannelResponse);
rpc OpenChannels (OpenChannelsRequest) returns (OpenChannelsResponse);
rpc PayInvoice (PayInvoiceRequest) returns (PayInvoiceResponse);
rpc DecodeInvoice (DecodeInvoiceRequest) returns (DecodeInvoiceResponse);
rpc Keysend (KeysendRequest) returns (KeysendResponse);
Expand Down Expand Up @@ -189,13 +189,24 @@ message GetBalanceResponse {
uint64 balance_satoshis = 1;
}

message OpenChannelRequest {
message OpenChannelInfo {
string node_connection_string = 1;
uint64 amt_satoshis = 2;
bool public = 3;
}
message OpenChannelResponse {
string temp_channel_id = 1;

message OpenChannelResult {
bool error = 1;
optional string error_message = 2;
optional string temp_channel_id = 3;
}

message OpenChannelsRequest {
repeated OpenChannelInfo channels = 1;
}
message OpenChannelsResponse {
repeated OpenChannelInfo channels = 1;
repeated OpenChannelResult results = 2;
}

message PayInvoiceRequest {
Expand Down
3 changes: 2 additions & 1 deletion senseicore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ bitcoin-bech32 = "0.12"
bech32 = "0.8"
futures = "0.3"
chrono = "0.4"
rand = "0.4"
rand = "0.8.4"
axum = { version = "0.4.2", features = ["headers"] }
http = "0.2"
tower = { version = "0.4", features = ["full"] }
Expand Down Expand Up @@ -49,3 +49,4 @@ migration = { path = "../migration" }

[dev-dependencies]
bitcoind = { version = "0.26", features = [ "22_0" ] }
serial_test = "0.6.0"
52 changes: 48 additions & 4 deletions senseicore/src/chain/broadcaster.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,45 @@
use std::sync::{Arc, Mutex};
use std::{
collections::HashMap,
sync::{Arc, Mutex},
};

use crate::events::SenseiEvent;

use super::database::WalletDatabase;
use bitcoin::Transaction;
use bitcoin::{Transaction, Txid};
use lightning::chain::chaininterface::BroadcasterInterface;
use tokio::sync::broadcast;

pub struct SenseiBroadcaster {
pub debounce: Mutex<HashMap<Txid, usize>>,
pub node_id: String,
pub broadcaster: Arc<dyn BroadcasterInterface + Send + Sync>,
pub wallet_database: Arc<Mutex<WalletDatabase>>,
pub event_sender: broadcast::Sender<SenseiEvent>,
}

impl BroadcasterInterface for SenseiBroadcaster {
fn broadcast_transaction(&self, tx: &Transaction) {
impl SenseiBroadcaster {
pub fn new(
node_id: String,
broadcaster: Arc<dyn BroadcasterInterface + Send + Sync>,
wallet_database: Arc<Mutex<WalletDatabase>>,
event_sender: broadcast::Sender<SenseiEvent>,
) -> Self {
Self {
node_id,
broadcaster,
wallet_database,
event_sender,
debounce: Mutex::new(HashMap::new()),
}
}

pub fn set_debounce(&self, txid: Txid, count: usize) {
let mut debounce = self.debounce.lock().unwrap();
debounce.insert(txid, count);
}

pub fn broadcast(&self, tx: &Transaction) {
self.broadcaster.broadcast_transaction(tx);

// TODO: there's a bug here if the broadcast fails
Expand All @@ -31,3 +55,23 @@ impl BroadcasterInterface for SenseiBroadcaster {
.unwrap_or_default();
}
}

impl BroadcasterInterface for SenseiBroadcaster {
fn broadcast_transaction(&self, tx: &Transaction) {
let txid = tx.txid();

let mut debounce = self.debounce.lock().unwrap();

let can_broadcast = match debounce.get_mut(&txid) {
Some(count) => {
*count -= 1;
*count == 0
}
None => true,
};

if can_broadcast {
self.broadcast(tx);
}
}
}
Loading

0 comments on commit 97a6f7a

Please sign in to comment.