Skip to content

Commit

Permalink
Merge pull request #1935 from get10101/fix/reject-dlc-channel
Browse files Browse the repository at this point in the history
feat: Reject dlc channel, settle and renew offer
  • Loading branch information
holzeis authored Feb 1, 2024
2 parents be1e1f8 + ca191ca commit 456e6b7
Show file tree
Hide file tree
Showing 23 changed files with 467 additions and 366 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

- Feat(webapp): Show order history
- Fix: Add reject dlc channel, settle and renew offer
- Chore: Change pending offer policy to reject on reconnect

## [1.8.4] - 2024-01-31

Expand Down
26 changes: 19 additions & 7 deletions Cargo.lock

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

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ resolver = "2"
# `p2pderivatives/rust-dlc#feature/ln-dlc-channels`: 4e104b4. This patch ensures backwards
# compatibility for 10101 through the `rust-lightning:0.0.116` upgrade. We will be able to drop it
# once all users have been upgraded and traded once.
dlc-manager = { git = "https://github.com/p2pderivatives/rust-dlc", rev = "69d63e1" }
dlc-messages = { git = "https://github.com/p2pderivatives/rust-dlc", rev = "69d63e1" }
dlc = { git = "https://github.com/p2pderivatives/rust-dlc", rev = "69d63e1" }
p2pd-oracle-client = { git = "https://github.com/p2pderivatives/rust-dlc", rev = "69d63e1" }
dlc-trie = { git = "https://github.com/p2pderivatives/rust-dlc", rev = "69d63e1" }
dlc-manager = { git = "https://github.com/p2pderivatives/rust-dlc", rev = "8c13abf4283244d0e40b042468f0ffea3c0081b4" }
dlc-messages = { git = "https://github.com/p2pderivatives/rust-dlc", rev = "8c13abf4283244d0e40b042468f0ffea3c0081b4" }
dlc = { git = "https://github.com/p2pderivatives/rust-dlc", rev = "8c13abf4283244d0e40b042468f0ffea3c0081b4" }
p2pd-oracle-client = { git = "https://github.com/p2pderivatives/rust-dlc", rev = "8c13abf4283244d0e40b042468f0ffea3c0081b4" }
dlc-trie = { git = "https://github.com/p2pderivatives/rust-dlc", rev = "8c13abf4283244d0e40b042468f0ffea3c0081b4" }

# We should usually track the `p2pderivatives/split-tx-experiment[-10101]` branch. For now we depend on a special fork which removes a panic in rust-lightning
lightning = { git = "https://github.com/bonomat/rust-lightning-p2p-derivatives", rev = "e49030e" }
Expand Down
6 changes: 4 additions & 2 deletions coordinator/src/bin/coordinator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,10 @@ async fn main() -> Result<()> {
)?);

let dlc_handler = DlcHandler::new(pool.clone(), node.clone());
let _handle =
dlc_handler::spawn_handling_dlc_messages(dlc_handler, node_event_handler.subscribe());
let _handle = dlc_handler::spawn_handling_outbound_dlc_messages(
dlc_handler,
node_event_handler.subscribe(),
);

let event_handler = CoordinatorEventHandler::new(node.clone(), Some(node_event_sender));
let running = node.start(event_handler, false)?;
Expand Down
23 changes: 23 additions & 0 deletions coordinator/src/db/positions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,29 @@ impl Position {
Ok(())
}

/// sets the status of the position in state `Closing` to a new state
pub fn update_closing_position(
conn: &mut PgConnection,
trader_pubkey: String,
state: crate::position::models::PositionState,
) -> Result<()> {
let state = PositionState::from(state);
let affected_rows = diesel::update(positions::table)
.filter(positions::trader_pubkey.eq(trader_pubkey.clone()))
.filter(positions::position_state.eq(PositionState::Closing))
.set((
positions::position_state.eq(state),
positions::update_timestamp.eq(OffsetDateTime::now_utc()),
))
.execute(conn)?;

if affected_rows == 0 {
bail!("Could not update position to {state:?} for {trader_pubkey}")
}

Ok(())
}

/// sets the status of all open position to closing (note, we expect that number to be always
/// exactly 1)
pub fn set_open_position_to_closing(
Expand Down
10 changes: 4 additions & 6 deletions coordinator/src/dlc_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,10 @@ impl DlcHandler {
}
}

/// [`spawn_handling_dlc_messages`] handles sending outbound dlc messages as well as keeping track
/// of what dlc messages have already been processed and what was the last outbound dlc message
/// so it can be resend on reconnect.
///
/// It does not handle the incoming dlc messages!
pub fn spawn_handling_dlc_messages(
/// [`spawn_handling_outbound_dlc_messages`] handles sending outbound dlc messages as well as
/// keeping track of what dlc messages have already been processed and what was the last outbound
/// dlc message so it can be resend on reconnect.
pub fn spawn_handling_outbound_dlc_messages(
dlc_handler: DlcHandler,
mut receiver: broadcast::Receiver<NodeEvent>,
) -> RemoteHandle<()> {
Expand Down
65 changes: 64 additions & 1 deletion coordinator/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use diesel::Connection;
use diesel::PgConnection;
use dlc_manager::channel::signed_channel::SignedChannel;
use dlc_manager::channel::signed_channel::SignedChannelState;
use dlc_manager::channel::Channel;
use dlc_manager::contract::contract_input::ContractInput;
use dlc_manager::contract::contract_input::ContractInputInfo;
use dlc_manager::contract::contract_input::OracleInput;
Expand Down Expand Up @@ -663,7 +664,7 @@ impl Node {
let trader_peer_id = trade_params.pubkey;
match self
.inner
.get_dlc_channel_by_counterparty(&trader_peer_id)?
.get_signed_dlc_channel_by_counterparty(&trader_peer_id)?
{
None => {
ensure!(
Expand Down Expand Up @@ -912,6 +913,68 @@ impl Node {
PositionState::Open,
)?;
}
ChannelMessage::Reject(reject) => {
// TODO(holzeis): if an dlc channel gets rejected we have to deal with the
// counterparty as well.

let channel_id_hex_string = reject.channel_id.to_hex();

let channel = self.inner.get_dlc_channel_by_id(&reject.channel_id)?;
let mut connection = self.pool.get()?;

match channel {
Channel::Cancelled(_) => {
tracing::info!(
channel_id = channel_id_hex_string,
node_id = node_id.to_string(),
"DLC Channel offer has been rejected. Setting position to failed."
);

db::positions::Position::update_proposed_position(
&mut connection,
node_id.to_string(),
PositionState::Failed,
)?;
}
Channel::Signed(SignedChannel {
state: SignedChannelState::Established { .. },
..
}) => {
// TODO(holzeis): Reverting the position state back from `Closing`
// to `Open` only works as long as we do not support resizing. This
// logic needs to be adapted when we implement resize.

tracing::info!(
channel_id = channel_id_hex_string,
node_id = node_id.to_string(),
"DLC Channel settle offer has been rejected. Setting position to back to open."
);

db::positions::Position::update_closing_position(
&mut connection,
node_id.to_string(),
PositionState::Open,
)?;
}
Channel::Signed(SignedChannel {
state: SignedChannelState::Settled { .. },
..
}) => {
tracing::info!(
channel_id = channel_id_hex_string,
node_id = node_id.to_string(),
"DLC Channel renew offer has been rejected. Setting position to failed."
);

db::positions::Position::update_proposed_position(
&mut connection,
node_id.to_string(),
PositionState::Failed,
)?;
}
_ => {}
}
}
_ => {}
};

Expand Down
Loading

0 comments on commit 456e6b7

Please sign in to comment.