|
| 1 | +// This file is Copyright its original authors, visible in version control |
| 2 | +// history. |
| 3 | +// |
| 4 | +// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE |
| 5 | +// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 6 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option. |
| 7 | +// You may not use this file except in accordance with one or both of these |
| 8 | +// licenses. |
| 9 | + |
| 10 | +//! Data structures and methods for caching offers that we interactively build with a static invoice |
| 11 | +//! server as an async recipient. The static invoice server will serve the resulting invoices to |
| 12 | +//! payers on our behalf when we're offline. |
| 13 | +
|
| 14 | +use crate::io; |
| 15 | +use crate::io::Read; |
| 16 | +use crate::ln::msgs::DecodeError; |
| 17 | +use crate::offers::nonce::Nonce; |
| 18 | +use crate::offers::offer::Offer; |
| 19 | +use crate::onion_message::messenger::Responder; |
| 20 | +use crate::prelude::*; |
| 21 | +use crate::util::ser::{Readable, Writeable, Writer}; |
| 22 | +use core::time::Duration; |
| 23 | + |
| 24 | +struct AsyncReceiveOffer { |
| 25 | + offer: Offer, |
| 26 | + /// We determine whether an offer is expiring "soon" based on how far the offer is into its total |
| 27 | + /// lifespan, using this field. |
| 28 | + offer_created_at: Duration, |
| 29 | + |
| 30 | + /// The below fields are used to generate and persist a new static invoice with the invoice |
| 31 | + /// server, if the invoice is expiring prior to the corresponding offer. We support automatically |
| 32 | + /// rotating the invoice for long-lived offers so users don't have to update the offer they've |
| 33 | + /// posted on e.g. their website if fees change or the invoices' payment paths become otherwise |
| 34 | + /// outdated. |
| 35 | + offer_nonce: Nonce, |
| 36 | + update_static_invoice_path: Responder, |
| 37 | + static_invoice_absolute_expiry: Duration, |
| 38 | + invoice_update_attempts: u8, |
| 39 | +} |
| 40 | + |
| 41 | +impl_writeable_tlv_based!(AsyncReceiveOffer, { |
| 42 | + (0, offer, required), |
| 43 | + (2, offer_nonce, required), |
| 44 | + (4, offer_created_at, required), |
| 45 | + (6, update_static_invoice_path, required), |
| 46 | + (8, static_invoice_absolute_expiry, required), |
| 47 | + (10, invoice_update_attempts, (static_value, 0)), |
| 48 | +}); |
| 49 | + |
| 50 | +/// If we are an often-offline recipient, we'll want to interactively build offers and static |
| 51 | +/// invoices with an always-online node that will serve those static invoices to payers on our |
| 52 | +/// behalf when we are offline. |
| 53 | +/// |
| 54 | +/// This struct is used to cache those interactively built offers, and should be passed into |
| 55 | +/// [`OffersMessageFlow`] on startup as well as persisted whenever an offer or invoice is updated |
| 56 | +/// with the static invoice server. |
| 57 | +/// |
| 58 | +/// [`OffersMessageFlow`]: crate::offers::flow::OffersMessageFlow |
| 59 | +pub struct AsyncReceiveOfferCache { |
| 60 | + offers: Vec<AsyncReceiveOffer>, |
| 61 | + /// Used to limit the number of times we request paths for our offer from the static invoice |
| 62 | + /// server. |
| 63 | + #[allow(unused)] // TODO: remove when we get rid of async payments cfg flag |
| 64 | + offer_paths_request_attempts: u8, |
| 65 | + /// Used to determine whether enough time has passed since our last request for offer paths that |
| 66 | + /// more requests should be allowed to go out. |
| 67 | + #[allow(unused)] // TODO: remove when we get rid of async payments cfg flag |
| 68 | + last_offer_paths_request_timestamp: Duration, |
| 69 | +} |
| 70 | + |
| 71 | +impl AsyncReceiveOfferCache { |
| 72 | + /// Creates an empty [`AsyncReceiveOfferCache`] to be passed into [`OffersMessageFlow`]. |
| 73 | + /// |
| 74 | + /// [`OffersMessageFlow`]: crate::offers::flow::OffersMessageFlow |
| 75 | + pub fn new() -> Self { |
| 76 | + Self { |
| 77 | + offers: Vec::new(), |
| 78 | + offer_paths_request_attempts: 0, |
| 79 | + last_offer_paths_request_timestamp: Duration::from_secs(0), |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +impl Writeable for AsyncReceiveOfferCache { |
| 85 | + fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> { |
| 86 | + write_tlv_fields!(w, { |
| 87 | + (0, self.offers, required_vec), |
| 88 | + // offer paths request retry info always resets on restart |
| 89 | + }); |
| 90 | + Ok(()) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +impl Readable for AsyncReceiveOfferCache { |
| 95 | + fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> { |
| 96 | + _init_and_read_len_prefixed_tlv_fields!(r, { |
| 97 | + (0, offers, required_vec), |
| 98 | + }); |
| 99 | + let offers: Vec<AsyncReceiveOffer> = offers; |
| 100 | + Ok(Self { |
| 101 | + offers, |
| 102 | + offer_paths_request_attempts: 0, |
| 103 | + last_offer_paths_request_timestamp: Duration::from_secs(0), |
| 104 | + }) |
| 105 | + } |
| 106 | +} |
0 commit comments