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

Persist SendPay.groupid as TEXT to cover full u64 range #1124

Merged
merged 3 commits into from
Nov 18, 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
6 changes: 3 additions & 3 deletions libs/sdk-core/src/greenlight/node_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ impl Greenlight {
let mut key = hex::encode(&p.payment_hash);
key.push('|');
key.push_str(&p.groupid.to_string());
(key, (p.payment_hash.clone(), p.groupid))
(key, (p.payment_hash.clone(), p.groupid.to_string()))
})
.collect();
let hash_group_values: Vec<_> = hash_groups.values().cloned().collect();
Expand Down Expand Up @@ -840,7 +840,7 @@ impl Greenlight {
for send_pay in send_pays {
let mut key = hex::encode(&send_pay.payment_hash);
key.push('|');
key.push_str(&send_pay.groupid.to_string());
key.push_str(&send_pay.groupid);
let payment = outbound_payments.entry(key).or_insert(SendPayAgg {
state: 0,
created_at: send_pay.created_at,
Expand Down Expand Up @@ -2147,7 +2147,7 @@ impl TryFrom<ListsendpaysPayments> for SendPay {
.created_index
.ok_or(NodeError::generic("missing created index"))?,
updated_index: value.updated_index,
groupid: value.groupid,
groupid: value.groupid.to_string(),
partid: value.partid,
payment_hash: value.payment_hash,
status: value.status.try_into()?,
Expand Down
25 changes: 25 additions & 0 deletions libs/sdk-core/src/persist/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,31 @@ pub(crate) fn current_migrations() -> Vec<&'static str> {
",
"DELETE FROM payments",
"DELETE FROM cached_items WHERE key = 'sync_state'",
// Delete send_pays, re-create it with groupid column as TEXT
"
DROP TABLE send_pays;
CREATE TABLE send_pays (
created_index INTEGER PRIMARY KEY NOT NULL,
updated_index INTEGER,
groupid TEXT NOT NULL,
partid INTEGER,
payment_hash BLOB NOT NULL,
status INTEGER NOT NULL,
amount_msat INTEGER,
destination BLOB,
created_at INTEGER NOT NULL,
amount_sent_msat INTEGER,
label TEXT,
bolt11 TEXT,
description TEXT,
bolt12 TEXT,
payment_preimage BLOB,
erroronion BLOB
) STRICT;
DELETE FROM cached_items WHERE key = 'sync_state';
",
]
}

Expand Down
10 changes: 8 additions & 2 deletions libs/sdk-core/src/persist/send_pays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ pub(crate) enum SendPayStatus {
pub(crate) struct SendPay {
pub created_index: u64,
pub updated_index: Option<u64>,
pub groupid: u64,

// The cln_grpc groupid (u64) can contain 64 bit values (e.g. > 2^63). This leads to errors when
// trying to persist them in an SQLite INTEGER column, which holds signed 64 bit numbers (63 bit
// value + 1 bit for the sign).
// To map the full range of u64 values in SQLite, we treat this field as String, stored as TEXT.
pub groupid: String,

pub partid: Option<u64>,
pub payment_hash: Vec<u8>,
pub status: SendPayStatus,
Expand Down Expand Up @@ -74,7 +80,7 @@ impl SqliteStorage {

pub(crate) fn list_send_pays(
&self,
hash_groups: &[(Vec<u8>, u64)],
hash_groups: &[(Vec<u8>, String)],
) -> PersistResult<Vec<SendPay>> {
let conn = self.get_connection()?;
let mut stmt = conn.prepare(
Expand Down
Loading