Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow coordinator to run full sync via HTTP API #2175

Merged
merged 1 commit into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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),
}
}
Loading