Skip to content

Commit

Permalink
Update to ldk v0.0.116
Browse files Browse the repository at this point in the history
  • Loading branch information
Tibo-lg committed Aug 22, 2023
1 parent f8716ae commit deb3348
Show file tree
Hide file tree
Showing 20 changed files with 246 additions and 140 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ members = [
]

[patch.crates-io]
lightning = { git = "https://github.com/p2pderivatives/rust-lightning/", rev = "47c8479" }
lightning-net-tokio = { git = "https://github.com/p2pderivatives/rust-lightning/", rev = "47c8479" }
lightning-persister = { git = "https://github.com/p2pderivatives/rust-lightning/", rev = "47c8479" }
lightning = { git = "https://github.com/p2pderivatives/rust-lightning/", rev = "b11794d6" }
lightning-net-tokio = { git = "https://github.com/p2pderivatives/rust-lightning/", rev = "b11794d6" }
lightning-persister = { git = "https://github.com/p2pderivatives/rust-lightning/", rev = "b11794d6" }
2 changes: 1 addition & 1 deletion bitcoin-rpc-provider/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ bitcoin = {version = "0.29.2"}
bitcoincore-rpc = {version = "0.16.0"}
bitcoincore-rpc-json = {version = "0.16.0"}
dlc-manager = {path = "../dlc-manager"}
lightning = {version = "0.0.114"}
lightning = {version = "0.0.116"}
log = "0.4.14"
rust-bitcoin-coin-selection = {version = "0.1.0", git = "https://github.com/p2pderivatives/rust-bitcoin-coin-selection", features = ["rand"]}
simple-wallet = {path = "../simple-wallet"}
54 changes: 17 additions & 37 deletions bitcoin-rpc-provider/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,11 @@ use rust_bitcoin_coin_selection::select_coins;
/// The minimum feerate we are allowed to send, as specify by LDK.
const MIN_FEERATE: u32 = 253;

#[derive(Clone, Eq, Hash, PartialEq)]
pub enum Target {
Background,
Normal,
HighPriority,
}

pub struct BitcoinCoreProvider {
client: Arc<Mutex<Client>>,
// Used to implement the FeeEstimator interface, heavily inspired by
// https://github.com/lightningdevkit/ldk-sample/blob/main/src/bitcoind_client.rs#L26
fees: Arc<HashMap<Target, AtomicU32>>,
fees: Arc<HashMap<ConfirmationTarget, AtomicU32>>,
}

#[derive(Debug)]
Expand Down Expand Up @@ -107,10 +100,10 @@ impl BitcoinCoreProvider {

pub fn new_from_rpc_client(rpc_client: Client) -> Self {
let client = Arc::new(Mutex::new(rpc_client));
let mut fees: HashMap<Target, AtomicU32> = HashMap::new();
fees.insert(Target::Background, AtomicU32::new(MIN_FEERATE));
fees.insert(Target::Normal, AtomicU32::new(2000));
fees.insert(Target::HighPriority, AtomicU32::new(5000));
let mut fees: HashMap<ConfirmationTarget, AtomicU32> = HashMap::new();
fees.insert(ConfirmationTarget::Background, AtomicU32::new(MIN_FEERATE));
fees.insert(ConfirmationTarget::Normal, AtomicU32::new(2000));
fees.insert(ConfirmationTarget::HighPriority, AtomicU32::new(5000));
let fees = Arc::new(fees);
poll_for_fee_estimates(client.clone(), fees.clone());
BitcoinCoreProvider { client, fees }
Expand Down Expand Up @@ -372,35 +365,22 @@ impl Blockchain for BitcoinCoreProvider {
}

impl FeeEstimator for BitcoinCoreProvider {
fn get_est_sat_per_1000_weight(
&self,
confirmation_target: lightning::chain::chaininterface::ConfirmationTarget,
) -> u32 {
match confirmation_target {
ConfirmationTarget::Background => self
.fees
.get(&Target::Background)
.unwrap()
.load(Ordering::Acquire),
ConfirmationTarget::Normal => self
.fees
.get(&Target::Normal)
.unwrap()
.load(Ordering::Acquire),
ConfirmationTarget::HighPriority => self
.fees
.get(&Target::HighPriority)
.unwrap()
.load(Ordering::Acquire),
}
fn get_est_sat_per_1000_weight(&self, confirmation_target: ConfirmationTarget) -> u32 {
self.fees
.get(&confirmation_target)
.unwrap()
.load(Ordering::Acquire)
}
}

fn poll_for_fee_estimates(client: Arc<Mutex<Client>>, fees: Arc<HashMap<Target, AtomicU32>>) {
fn poll_for_fee_estimates(
client: Arc<Mutex<Client>>,
fees: Arc<HashMap<ConfirmationTarget, AtomicU32>>,
) {
std::thread::spawn(move || loop {
match query_fee_estimate(&client, 144, EstimateMode::Economical) {
Ok(fee_rate) => {
fees.get(&Target::Background)
fees.get(&ConfirmationTarget::Background)
.unwrap()
.store(fee_rate, Ordering::Release);
}
Expand All @@ -410,7 +390,7 @@ fn poll_for_fee_estimates(client: Arc<Mutex<Client>>, fees: Arc<HashMap<Target,
};
match query_fee_estimate(&client, 18, EstimateMode::Conservative) {
Ok(fee_rate) => {
fees.get(&Target::Normal)
fees.get(&ConfirmationTarget::Normal)
.unwrap()
.store(fee_rate, Ordering::Release);
}
Expand All @@ -420,7 +400,7 @@ fn poll_for_fee_estimates(client: Arc<Mutex<Client>>, fees: Arc<HashMap<Target,
};
match query_fee_estimate(&client, 6, EstimateMode::Conservative) {
Ok(fee_rate) => {
fees.get(&Target::HighPriority)
fees.get(&ConfirmationTarget::HighPriority)
.unwrap()
.store(fee_rate, Ordering::Release);
}
Expand Down
6 changes: 3 additions & 3 deletions dlc-manager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ bitcoin = {version = "0.29.2"}
dlc = {version = "0.4.0", path = "../dlc"}
dlc-messages = {version = "0.4.0", path = "../dlc-messages"}
dlc-trie = {version = "0.4.0", path = "../dlc-trie"}
lightning = {version = "0.0.114"}
lightning = {version = "0.0.116"}
log = "0.4.14"
rand_chacha = {version = "0.3.1", optional = true}
secp256k1-zkp = {version = "0.7.0", features = ["bitcoin_hashes", "rand", "rand-std"]}
Expand All @@ -37,8 +37,8 @@ dlc-manager = {path = ".", features = ["use-serde"]}
dlc-messages = {path = "../dlc-messages", features = ["serde"]}
electrs-blockchain-provider = {path = "../electrs-blockchain-provider"}
env_logger = "0.9.1"
lightning-persister = {version = "0.0.114"}
lightning-transaction-sync = {version = "0.0.114", features=["esplora-blocking"]}
lightning-persister = {version = "0.0.116"}
lightning-transaction-sync = {version = "0.0.116", features=["esplora-blocking"]}
mocks = {path = "../mocks"}
secp256k1-zkp = {version = "0.7.0", features = ["bitcoin_hashes", "rand", "rand-std", "global-context", "use-serde"]}
serde = "1.0"
Expand Down
100 changes: 92 additions & 8 deletions dlc-manager/src/sub_channel_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ use dlc_messages::{
FundingSignatures, SubChannelMessage,
};
use lightning::{
chain::{chaininterface::FeeEstimator, keysinterface::ChannelSigner},
chain::chaininterface::FeeEstimator,
events::{ClosureReason, MessageSendEventsProvider},
ln::{
chan_utils::{
build_commitment_secret, derive_private_key, derive_private_revocation_key,
Expand All @@ -25,9 +26,9 @@ use lightning::{
channelmanager::ChannelDetails,
msgs::{ChannelMessageHandler, DecodeError, RevokeAndACK},
},
util::{errors::APIError, events::MessageSendEventsProvider},
sign::ChannelSigner,
util::{
events::ClosureReason,
errors::APIError,
ser::{Readable, Writeable, Writer},
},
};
Expand Down Expand Up @@ -3865,6 +3866,92 @@ where
self.ln_channel_manager
.provided_init_features(their_node_id)
}

fn handle_open_channel_v2(
&self,
their_node_id: &PublicKey,
msg: &lightning::ln::msgs::OpenChannelV2,
) {
self.ln_channel_manager
.handle_open_channel_v2(their_node_id, msg)
}

fn handle_accept_channel_v2(
&self,
their_node_id: &PublicKey,
msg: &lightning::ln::msgs::AcceptChannelV2,
) {
self.ln_channel_manager
.handle_accept_channel_v2(their_node_id, msg)
}

fn handle_tx_add_input(
&self,
their_node_id: &PublicKey,
msg: &lightning::ln::msgs::TxAddInput,
) {
self.ln_channel_manager
.handle_tx_add_input(their_node_id, msg)
}

fn handle_tx_add_output(
&self,
their_node_id: &PublicKey,
msg: &lightning::ln::msgs::TxAddOutput,
) {
self.ln_channel_manager
.handle_tx_add_output(their_node_id, msg)
}

fn handle_tx_remove_input(
&self,
their_node_id: &PublicKey,
msg: &lightning::ln::msgs::TxRemoveInput,
) {
self.ln_channel_manager
.handle_tx_remove_input(their_node_id, msg)
}

fn handle_tx_remove_output(
&self,
their_node_id: &PublicKey,
msg: &lightning::ln::msgs::TxRemoveOutput,
) {
self.ln_channel_manager
.handle_tx_remove_output(their_node_id, msg)
}

fn handle_tx_complete(&self, their_node_id: &PublicKey, msg: &lightning::ln::msgs::TxComplete) {
self.ln_channel_manager
.handle_tx_complete(their_node_id, msg)
}

fn handle_tx_signatures(
&self,
their_node_id: &PublicKey,
msg: &lightning::ln::msgs::TxSignatures,
) {
self.ln_channel_manager
.handle_tx_signatures(their_node_id, msg)
}

fn handle_tx_init_rbf(&self, their_node_id: &PublicKey, msg: &lightning::ln::msgs::TxInitRbf) {
self.ln_channel_manager
.handle_tx_init_rbf(their_node_id, msg)
}

fn handle_tx_ack_rbf(&self, their_node_id: &PublicKey, msg: &lightning::ln::msgs::TxAckRbf) {
self.ln_channel_manager
.handle_tx_ack_rbf(their_node_id, msg)
}

fn handle_tx_abort(&self, their_node_id: &PublicKey, msg: &lightning::ln::msgs::TxAbort) {
self.ln_channel_manager.handle_tx_abort(their_node_id, msg)
}

fn get_genesis_hashes(&self) -> Option<Vec<bitcoin::blockdata::constants::ChainHash>> {
self.ln_channel_manager.get_genesis_hashes()
}
}

impl<
Expand Down Expand Up @@ -3892,14 +3979,11 @@ where
F::Target: FeeEstimator,
SP::Target: LnDlcSignerProvider<LCS>,
{
fn get_and_clear_pending_msg_events(&self) -> Vec<lightning::util::events::MessageSendEvent> {
fn get_and_clear_pending_msg_events(&self) -> Vec<lightning::events::MessageSendEvent> {
let mut msg_events = self.ln_channel_manager.get_and_clear_pending_msg_events();

for event in msg_events.iter_mut() {
if let lightning::util::events::MessageSendEvent::SendChannelReestablish {
msg, ..
} = event
{
if let lightning::events::MessageSendEvent::SendChannelReestablish { msg, .. } = event {
match self.dlc_channel_manager.get_store().get_sub_channel(msg.channel_id) {
Err(e) => error!("Unexpected error {} trying to retrieve sub channel {:?} during sending of reestablish.", e, msg.channel_id),
Ok(None) => trace!("No sub channel with id {:?} to reestablish", msg.channel_id),
Expand Down
4 changes: 2 additions & 2 deletions dlc-manager/src/subchannel/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use lightning::{
chain::{
chaininterface::{BroadcasterInterface, FeeEstimator},
chainmonitor::ChainMonitor,
keysinterface::{EntropySource, NodeSigner, SignerProvider, WriteableEcdsaChannelSigner},
Watch,
},
ln::{
Expand All @@ -18,6 +17,7 @@ use lightning::{
msgs::{ChannelMessageHandler, CommitmentSigned, RevokeAndACK},
},
routing::router::Router,
sign::{EntropySource, NodeSigner, SignerProvider, WriteableEcdsaChannelSigner},
util::{errors::APIError, logger::Logger},
};
use secp256k1_zkp::{ecdsa::Signature, EcdsaAdaptorSignature, PublicKey, SecretKey};
Expand Down Expand Up @@ -376,7 +376,7 @@ pub struct ClosingSubChannel {
/// Provides the ability to access and update Lightning Network channels.
pub trait LNChannelManager<SP>: ChannelMessageHandler
where
SP: lightning::chain::keysinterface::ChannelSigner,
SP: lightning::sign::ChannelSigner,
{
/// Returns the details of the channel with given `channel_id` if found.
fn get_channel_details(&self, channel_id: &ChannelId) -> Option<ChannelDetails>;
Expand Down
Loading

0 comments on commit deb3348

Please sign in to comment.