|
| 1 | +// This file is Copyright its original authors, visible in version control history. |
| 2 | +// |
| 3 | +// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 4 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or |
| 5 | +// http://opensource.org/licenses/MIT>, at your option. You may not use this file except in |
| 6 | +// accordance with one or both of these licenses. |
| 7 | + |
| 8 | +use bitcoin::Txid; |
| 9 | +use lightning::ln::channelmanager::PaymentId; |
| 10 | +use lightning::ln::msgs::DecodeError; |
| 11 | +use lightning::util::ser::{Readable, Writeable}; |
| 12 | +use lightning::{_init_and_read_len_prefixed_tlv_fields, write_tlv_fields}; |
| 13 | + |
| 14 | +use crate::data_store::{StorableObject, StorableObjectId, StorableObjectUpdate}; |
| 15 | + |
| 16 | +/// Details of an on-chain transaction that has replaced a previous transaction (e.g., via RBF). |
| 17 | +#[derive(Clone, Debug, PartialEq, Eq)] |
| 18 | +pub struct ReplacedOnchainTransactionDetails { |
| 19 | + /// The new transaction ID. |
| 20 | + pub new_txid: Txid, |
| 21 | + /// The original transaction ID that was replaced. |
| 22 | + pub original_txid: Txid, |
| 23 | + /// The payment ID associated with the transaction. |
| 24 | + pub payment_id: PaymentId, |
| 25 | +} |
| 26 | + |
| 27 | +impl Writeable for ReplacedOnchainTransactionDetails { |
| 28 | + fn write<W: lightning::util::ser::Writer>( |
| 29 | + &self, writer: &mut W, |
| 30 | + ) -> Result<(), lightning::io::Error> { |
| 31 | + write_tlv_fields!(writer, { |
| 32 | + (0, self.new_txid, required), |
| 33 | + (2, self.original_txid, required), |
| 34 | + (4, self.payment_id, required), |
| 35 | + }); |
| 36 | + Ok(()) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +impl Readable for ReplacedOnchainTransactionDetails { |
| 41 | + fn read<R: lightning::io::Read>( |
| 42 | + reader: &mut R, |
| 43 | + ) -> Result<ReplacedOnchainTransactionDetails, DecodeError> { |
| 44 | + _init_and_read_len_prefixed_tlv_fields!(reader, { |
| 45 | + (0, new_txid, required), |
| 46 | + (2, original_txid, required), |
| 47 | + (4, payment_id, required), |
| 48 | + }); |
| 49 | + |
| 50 | + let new_txid: Txid = new_txid.0.ok_or(DecodeError::InvalidValue)?; |
| 51 | + let original_txid: Txid = original_txid.0.ok_or(DecodeError::InvalidValue)?; |
| 52 | + let payment_id: PaymentId = payment_id.0.ok_or(DecodeError::InvalidValue)?; |
| 53 | + |
| 54 | + Ok(ReplacedOnchainTransactionDetails { new_txid, original_txid, payment_id }) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +impl StorableObjectId for Txid { |
| 59 | + fn encode_to_hex_str(&self) -> String { |
| 60 | + self.to_string() |
| 61 | + } |
| 62 | +} |
| 63 | +impl StorableObject for ReplacedOnchainTransactionDetails { |
| 64 | + type Id = Txid; |
| 65 | + type Update = ReplacedOnchainTransactionDetailsUpdate; |
| 66 | + |
| 67 | + fn id(&self) -> Self::Id { |
| 68 | + self.new_txid |
| 69 | + } |
| 70 | + |
| 71 | + fn update(&mut self, _update: &Self::Update) -> bool { |
| 72 | + // We don't update, we delete on confirmation |
| 73 | + false |
| 74 | + } |
| 75 | + |
| 76 | + fn to_update(&self) -> Self::Update { |
| 77 | + self.into() |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +#[derive(Clone, Debug, PartialEq, Eq)] |
| 82 | +pub(crate) struct ReplacedOnchainTransactionDetailsUpdate { |
| 83 | + pub id: Txid, |
| 84 | +} |
| 85 | + |
| 86 | +impl From<&ReplacedOnchainTransactionDetails> for ReplacedOnchainTransactionDetailsUpdate { |
| 87 | + fn from(value: &ReplacedOnchainTransactionDetails) -> Self { |
| 88 | + Self { id: value.new_txid } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +impl StorableObjectUpdate<ReplacedOnchainTransactionDetails> |
| 93 | + for ReplacedOnchainTransactionDetailsUpdate |
| 94 | +{ |
| 95 | + fn id(&self) -> <ReplacedOnchainTransactionDetails as StorableObject>::Id { |
| 96 | + self.id |
| 97 | + } |
| 98 | +} |
0 commit comments