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

feat: add settled_at to invoices #11

Merged
merged 1 commit into from
Dec 30, 2024
Merged
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
8 changes: 4 additions & 4 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -23,7 +23,7 @@ prost = "0.13.4"
rcgen = { version = "0.13.2", features = ["x509-parser"] }
tokio = { version = "1.42.0", features = ["macros", "rt-multi-thread", "sync"] }
tonic = { version = "0.12.3", features = ["prost", "tls", "gzip", "zstd"] }
serde = { version = "1.0.216", features = ["derive"] }
serde = { version = "1.0.217", features = ["derive"] }
serde_json = { version = "1.0.134", features = ["preserve_order"] }
lightning-invoice = { version = "0.32.0", features = ["std"] }
chrono = { version = "0.4.39", features = ["serde"] }
2 changes: 2 additions & 0 deletions migrations/2024-12-29-234029_settled_at/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE invoices
DROP COLUMN settled_at;
2 changes: 2 additions & 0 deletions migrations/2024-12-29-234029_settled_at/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE invoices
ADD COLUMN settled_at TIMESTAMP;
2 changes: 2 additions & 0 deletions migrations_postgres/2024-12-29-234029_settled_at/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE invoices
DROP COLUMN settled_at;
2 changes: 2 additions & 0 deletions migrations_postgres/2024-12-29-234029_settled_at/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE invoices
ADD COLUMN settled_at TIMESTAMP;
2 changes: 2 additions & 0 deletions protos/hold.proto
Original file line number Diff line number Diff line change
@@ -89,6 +89,8 @@ message Invoice {
string bolt11 = 4;
InvoiceState state = 5;
uint64 created_at = 6;
optional uint64 settled_at = 8;

repeated Htlc htlcs = 7;
}

3 changes: 3 additions & 0 deletions src/commands/list.rs
Original file line number Diff line number Diff line change
@@ -43,6 +43,8 @@ struct PrettyHoldInvoice {
pub bolt11: String,
pub state: String,
pub created_at: chrono::NaiveDateTime,
#[serde(skip_serializing_if = "Option::is_none")]
pub settled_at: Option<chrono::NaiveDateTime>,
pub htlcs: Vec<Htlc>,
}

@@ -55,6 +57,7 @@ impl From<HoldInvoice> for PrettyHoldInvoice {
bolt11: value.invoice.bolt11.clone(),
state: value.invoice.state.clone(),
created_at: value.invoice.created_at,
settled_at: value.invoice.settled_at,
htlcs: value.htlcs.clone(),
}
}
18 changes: 14 additions & 4 deletions src/database/helpers/invoice_helper.rs
Original file line number Diff line number Diff line change
@@ -76,10 +76,20 @@ impl InvoiceHelper for InvoiceHelperDatabase {
) -> Result<usize> {
state.validate_transition(new_state)?;

Ok(update(invoices::dsl::invoices)
.filter(invoices::dsl::id.eq(id))
.set(invoices::dsl::state.eq(new_state.to_string()))
.execute(&mut self.pool.get()?)?)
if new_state != InvoiceState::Paid {
Ok(update(invoices::dsl::invoices)
.filter(invoices::dsl::id.eq(id))
.set(invoices::dsl::state.eq(new_state.to_string()))
.execute(&mut self.pool.get()?)?)
} else {
Ok(update(invoices::dsl::invoices)
.filter(invoices::dsl::id.eq(id))
.set((
invoices::dsl::state.eq(new_state.to_string()),
invoices::dsl::settled_at.eq(Some(Utc::now().naive_utc())),
))
.execute(&mut self.pool.get()?)?)
}
}

fn set_invoice_preimage(&self, id: i64, preimage: &[u8]) -> Result<usize> {
3 changes: 3 additions & 0 deletions src/database/model.rs
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@ pub struct Invoice {
pub bolt11: String,
pub state: String,
pub created_at: chrono::NaiveDateTime,
pub settled_at: Option<chrono::NaiveDateTime>,
}

#[derive(Insertable, Debug, PartialEq, Clone)]
@@ -358,6 +359,7 @@ mod test {
bolt11: "".to_string(),
state: "".to_string(),
created_at: Default::default(),
settled_at: None,
},
vec![],
);
@@ -407,6 +409,7 @@ mod test {
bolt11: "".to_string(),
state: "".to_string(),
created_at: Default::default(),
settled_at: None,
},
vec![
Htlc {
1 change: 1 addition & 0 deletions src/database/schema.rs
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@ diesel::table! {
bolt11 -> Text,
state -> Text,
created_at -> Timestamp,
settled_at -> Nullable<Timestamp>,
}
}

4 changes: 4 additions & 0 deletions src/grpc/transformers.rs
Original file line number Diff line number Diff line change
@@ -27,6 +27,10 @@ impl From<HoldInvoice> for hold::Invoice {
InvoiceState::try_from(value.invoice.state.as_str()).unwrap(),
),
created_at: value.invoice.created_at.and_utc().timestamp() as u64,
settled_at: value
.invoice
.settled_at
.map(|t| t.and_utc().timestamp() as u64),
htlcs: value.htlcs.into_iter().map(|htlc| htlc.into()).collect(),
}
}
16 changes: 11 additions & 5 deletions src/handler.rs
Original file line number Diff line number Diff line change
@@ -299,11 +299,12 @@ mod test {
Ok(Some(HoldInvoice {
invoice: Invoice {
id: 0,
payment_hash: vec![],
preimage: None,
settled_at: None,
payment_hash: vec![],
bolt11: "".to_string(),
state: InvoiceState::Paid.to_string(),
created_at: Default::default(),
state: InvoiceState::Paid.to_string(),
},
htlcs: vec![],
}))
@@ -349,8 +350,9 @@ mod test {
Ok(Some(HoldInvoice {
invoice: Invoice {
id: 0,
payment_hash: vec![],
preimage: None,
settled_at: None,
payment_hash: vec![],
bolt11: INVOICE.to_string(),
state: InvoiceState::Unpaid.to_string(),
created_at: Default::default(),
@@ -408,8 +410,9 @@ mod test {
Ok(Some(HoldInvoice {
invoice: Invoice {
id: 0,
payment_hash: vec![],
preimage: None,
settled_at: None,
payment_hash: vec![],
bolt11: INVOICE.to_string(),
state: InvoiceState::Unpaid.to_string(),
created_at: Default::default(),
@@ -470,8 +473,9 @@ mod test {
Ok(Some(HoldInvoice {
invoice: Invoice {
id: 0,
payment_hash: vec![],
preimage: None,
settled_at: None,
payment_hash: vec![],
bolt11: INVOICE.to_string(),
state: InvoiceState::Unpaid.to_string(),
created_at: Default::default(),
@@ -537,6 +541,7 @@ mod test {
invoice: Invoice {
id: 0,
preimage: None,
settled_at: None,
bolt11: INVOICE.to_string(),
created_at: Default::default(),
payment_hash: payment_hash_cp.clone(),
@@ -557,6 +562,7 @@ mod test {
invoice: Invoice {
id: 0,
preimage: None,
settled_at: None,
bolt11: INVOICE.to_string(),
created_at: Default::default(),
state: InvoiceState::Unpaid.to_string(),
2 changes: 0 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -27,8 +27,6 @@ struct State<T, E> {
invoice_helper: T,
}

// TODO: backfill records from old datastore

#[tokio::main]
async fn main() -> Result<()> {
std::env::set_var(
1 change: 1 addition & 0 deletions tests-regtest/hold/regtest_grpc.py
Original file line number Diff line number Diff line change
@@ -305,6 +305,7 @@ def track_states() -> list[InvoiceState]:

invoice_state = cl.List(ListRequest(payment_hash=payment_hash)).invoices[0]
assert invoice_state.state == InvoiceState.PAID
assert invoice_state.settled_at - int(time_now().timestamp()) < 2
assert len(invoice_state.htlcs) == 1
assert invoice_state.htlcs[0].state == InvoiceState.PAID

3 changes: 2 additions & 1 deletion tests-regtest/hold/regtest_rpc.py
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@
import time
from typing import Any

from hold.utils import LndPay, lightning, new_preimage
from hold.utils import LndPay, lightning, new_preimage, time_now


def check_unpaid_invoice(
@@ -84,6 +84,7 @@ def test_settle(self) -> None:

data = lightning("listholdinvoices", payment_hash)["holdinvoices"][0]
assert data["state"] == "paid"
assert data["settled_at"].startswith(time_now().strftime("%Y-%m-%dT%H:%M"))

htlcs = data["htlcs"]
assert len(htlcs) == 1