Skip to content

Commit

Permalink
feat(coordinator): Allow to run full sync via HTTP API
Browse files Browse the repository at this point in the history
  • Loading branch information
luckysori committed Mar 6, 2024
1 parent 34751f4 commit 75fed63
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 21 deletions.
18 changes: 1 addition & 17 deletions coordinator/src/admin.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::collaborative_revert;
use crate::db;
use crate::parse_dlc_channel_id;
use crate::routes::empty_string_as_none;
use crate::routes::AppState;
use crate::AppError;
use anyhow::Context;
Expand All @@ -22,14 +23,10 @@ use ln_dlc_node::node::NodeInfo;
use rust_decimal::prelude::FromPrimitive;
use rust_decimal::prelude::ToPrimitive;
use rust_decimal::Decimal;
use serde::de;
use serde::Deserialize;
use serde::Deserializer;
use serde::Serialize;
use std::cmp::Ordering;
use std::fmt;
use std::num::NonZeroU32;
use std::str::FromStr;
use std::sync::Arc;
use time::OffsetDateTime;
use tokio::task::spawn_blocking;
Expand Down Expand Up @@ -370,19 +367,6 @@ pub struct CloseChannelParams {
force: Option<bool>,
}

fn empty_string_as_none<'de, D, T>(de: D) -> Result<Option<T>, D::Error>
where
D: Deserializer<'de>,
T: FromStr,
T::Err: fmt::Display,
{
let opt = Option::<String>::deserialize(de)?;
match opt.as_deref() {
None | Some("") => Ok(None),
Some(s) => FromStr::from_str(s).map_err(de::Error::custom).map(Some),
}
}

#[instrument(skip_all, err(Debug))]
pub async fn close_channel(
Path(channel_id_string): Path<String>,
Expand Down
43 changes: 39 additions & 4 deletions coordinator/src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,11 @@ use ln_dlc_node::node::NodeInfo;
use opentelemetry_prometheus::PrometheusExporter;
use prometheus::Encoder;
use prometheus::TextEncoder;
use serde::de;
use serde::Deserialize;
use serde::Deserializer;
use serde::Serialize;
use std::fmt;
use std::net::SocketAddr;
use std::str::FromStr;
use std::sync::Arc;
Expand Down Expand Up @@ -305,12 +308,31 @@ pub async fn rollover(
Ok(())
}

#[derive(Debug, Deserialize)]
pub struct SyncParams {
#[serde(default, deserialize_with = "empty_string_as_none")]
full: Option<bool>,
#[serde(default, deserialize_with = "empty_string_as_none")]
gap: Option<usize>,
}

/// Internal API for syncing the on-chain wallet and the DLC channels.
#[instrument(skip_all, err(Debug))]
pub async fn post_sync(State(state): State<Arc<AppState>>) -> Result<(), AppError> {
state.node.inner.sync_on_chain_wallet().await.map_err(|e| {
AppError::InternalServerError(format!("Could not sync on-chain wallet: {e:#}"))
})?;
pub async fn post_sync(
State(state): State<Arc<AppState>>,
Query(params): Query<SyncParams>,
) -> Result<(), AppError> {
if params.full.unwrap_or(false) {
let stop_gap = params.gap.unwrap_or(20);

state.node.inner.full_sync(stop_gap).await.map_err(|e| {
AppError::InternalServerError(format!("Could not full-sync on-chain wallet: {e:#}"))
})?;
} else {
state.node.inner.sync_on_chain_wallet().await.map_err(|e| {
AppError::InternalServerError(format!("Could not sync on-chain wallet: {e:#}"))
})?;
}

spawn_blocking(move || {
if let Err(e) = state.node.inner.dlc_manager.periodic_chain_monitor() {
Expand Down Expand Up @@ -667,3 +689,16 @@ pub async fn get_leaderboard(
entries: leader_board,
}))
}

pub fn empty_string_as_none<'de, D, T>(de: D) -> Result<Option<T>, D::Error>
where
D: Deserializer<'de>,
T: FromStr,
T::Err: fmt::Display,
{
let opt = Option::<String>::deserialize(de)?;
match opt.as_deref() {
None | Some("") => Ok(None),
Some(s) => FromStr::from_str(s).map_err(de::Error::custom).map(Some),
}
}

0 comments on commit 75fed63

Please sign in to comment.