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
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
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
Expand Up @@ -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"] }
Expand Down
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
Expand Up @@ -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;
}

Expand Down
3 changes: 3 additions & 0 deletions src/commands/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
}

Expand All @@ -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(),
}
}
Expand Down
18 changes: 14 additions & 4 deletions src/database/helpers/invoice_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
3 changes: 3 additions & 0 deletions src/database/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -358,6 +359,7 @@ mod test {
bolt11: "".to_string(),
state: "".to_string(),
created_at: Default::default(),
settled_at: None,
},
vec![],
);
Expand Down Expand Up @@ -407,6 +409,7 @@ mod test {
bolt11: "".to_string(),
state: "".to_string(),
created_at: Default::default(),
settled_at: None,
},
vec![
Htlc {
Expand Down
1 change: 1 addition & 0 deletions src/database/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ diesel::table! {
bolt11 -> Text,
state -> Text,
created_at -> Timestamp,
settled_at -> Nullable<Timestamp>,
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/grpc/transformers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
}
}
Expand Down
16 changes: 11 additions & 5 deletions src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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![],
}))
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand All @@ -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(),
Expand Down
2 changes: 0 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions tests-regtest/hold/regtest_grpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion tests-regtest/hold/regtest_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down
Loading