From 7efa2474caf8c7da4b022178ad593372a9aa2b5f Mon Sep 17 00:00:00 2001 From: Daniel Karzel Date: Thu, 13 Jul 2023 14:05:40 +1000 Subject: [PATCH] fixup! Persist coordinator payments in database --- .../2023-07-10-060138_payments/up.sql | 5 +-- coordinator/src/db/payments.rs | 33 +++++++------------ coordinator/src/schema.rs | 5 +-- 3 files changed, 17 insertions(+), 26 deletions(-) diff --git a/coordinator/migrations/2023-07-10-060138_payments/up.sql b/coordinator/migrations/2023-07-10-060138_payments/up.sql index f1b820173..abc42ef3d 100644 --- a/coordinator/migrations/2023-07-10-060138_payments/up.sql +++ b/coordinator/migrations/2023-07-10-060138_payments/up.sql @@ -9,7 +9,8 @@ CREATE TABLE IF NOT EXISTS "payments" ( htlc_status "Htlc_Status_Type" NOT NULL, amount_msat BIGINT, flow "Payment_Flow_Type" NOT NULL, - created_at BIGINT NOT NULL, - updated_at BIGINT NOT NULL, + payment_timestamp timestamp WITH TIME ZONE NOT NULL, + created_at timestamp WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, + updated_at timestamp WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, description TEXT NOT NULL DEFAULT '' ); diff --git a/coordinator/src/db/payments.rs b/coordinator/src/db/payments.rs index 6813016be..f68e31d6d 100644 --- a/coordinator/src/db/payments.rs +++ b/coordinator/src/db/payments.rs @@ -25,8 +25,9 @@ pub struct Payment { pub htlc_status: HtlcStatus, pub amount_msat: Option, pub flow: PaymentFlow, - pub created_at: i64, - pub updated_at: i64, + pub payment_timestamp: OffsetDateTime, + pub created_at: OffsetDateTime, + pub updated_at: OffsetDateTime, pub description: String, } @@ -71,8 +72,6 @@ pub fn get_all( impl From<(lightning::ln::PaymentHash, ln_dlc_node::PaymentInfo)> for NewPayment { fn from((payment_hash, info): (lightning::ln::PaymentHash, ln_dlc_node::PaymentInfo)) -> Self { - let timestamp = info.timestamp.unix_timestamp(); - Self { payment_hash: payment_hash.0.to_hex(), preimage: info.preimage.map(|preimage| preimage.0.to_hex()), @@ -80,8 +79,7 @@ impl From<(lightning::ln::PaymentHash, ln_dlc_node::PaymentInfo)> for NewPayment htlc_status: info.status.into(), amount_msat: info.amt_msat.to_inner().map(|amt| amt as i64), flow: info.flow.into(), - created_at: timestamp, - updated_at: timestamp, + payment_timestamp: info.timestamp, description: info.description, } } @@ -112,27 +110,19 @@ impl TryFrom for (lightning::ln::PaymentHash, ln_dlc_node::PaymentInfo) }) .transpose()?; - let status = value.htlc_status.into(); - let amt_msat = ln_dlc_node::MillisatAmount::new(value.amount_msat.map(|amount| amount as u64)); - let flow = value.flow.into(); - - let timestamp = OffsetDateTime::from_unix_timestamp(value.created_at)?; - - let description = value.description; - Ok(( payment_hash, ln_dlc_node::PaymentInfo { preimage, secret, - status, + status: value.htlc_status.into(), amt_msat, - flow, - timestamp, - description, + flow: value.flow.into(), + timestamp: value.payment_timestamp, + description: value.description, }, )) } @@ -179,8 +169,7 @@ pub(crate) struct NewPayment { #[diesel(sql_type = Nullable)] pub amount_msat: Option, pub flow: PaymentFlow, - pub created_at: i64, - pub updated_at: i64, + pub payment_timestamp: OffsetDateTime, #[diesel(sql_type = Text)] pub description: String, } @@ -249,8 +238,8 @@ pub fn update( preimage: Option, secret: Option, conn: &mut PgConnection, -) -> Result { - let updated_at = OffsetDateTime::now_utc().unix_timestamp(); +) -> Result { + let updated_at = OffsetDateTime::now_utc(); let preimage = preimage.map(|preimage| preimage.0.to_hex()); let secret = secret.map(|secret| secret.0.to_hex()); diff --git a/coordinator/src/schema.rs b/coordinator/src/schema.rs index cde706f28..1f2638419 100644 --- a/coordinator/src/schema.rs +++ b/coordinator/src/schema.rs @@ -58,8 +58,9 @@ diesel::table! { htlc_status -> HtlcStatusType, amount_msat -> Nullable, flow -> PaymentFlowType, - created_at -> Int8, - updated_at -> Int8, + payment_timestamp -> Timestamptz, + created_at -> Timestamptz, + updated_at -> Timestamptz, description -> Text, } }