Skip to content

Commit 498afb8

Browse files
committed
f: Implement chain::Listen for OffersMessageFlow
1 parent 1a6821e commit 498afb8

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

lightning/src/offers/flow.rs

+36-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use crate::blinded_path::message::{
1515
};
1616
use crate::blinded_path::payment::BlindedPaymentPath;
1717
use crate::chain;
18+
use crate::chain::transaction::TransactionData;
1819
use crate::ln::channelmanager::PaymentId;
1920
use crate::ln::inbound_payment;
2021
use crate::offers::invoice::{Bolt12Invoice, DerivedSigningPubkey, InvoiceBuilder};
@@ -29,13 +30,14 @@ use crate::onion_message::messenger::{Destination, MessageRouter, MessageSendIns
2930
use crate::onion_message::offers::OffersMessage;
3031
use crate::sign::EntropySource;
3132
use crate::sync::Mutex;
33+
use bitcoin::block::Header;
3234
use bitcoin::constants::ChainHash;
3335
use bitcoin::secp256k1::{PublicKey, Secp256k1};
3436
use bitcoin::{secp256k1, Network};
3537
use types::payment::PaymentHash;
3638

3739
use core::ops::Deref;
38-
use core::sync::atomic::AtomicUsize;
40+
use core::sync::atomic::{AtomicUsize, Ordering};
3941
use core::time::Duration;
4042

4143
#[cfg(feature = "dnssec")]
@@ -164,4 +166,37 @@ where
164166
pub fn get_our_node_id(&self) -> PublicKey {
165167
self.our_network_pubkey
166168
}
169+
170+
fn best_block_updated(&self, header: &Header) {
171+
macro_rules! max_time {
172+
($timestamp: expr) => {
173+
loop {
174+
// Update $timestamp to be the max of its current value and the block
175+
// timestamp. This should keep us close to the current time without relying on
176+
// having an explicit local time source.
177+
// Just in case we end up in a race, we loop until we either successfully
178+
// update $timestamp or decide we don't need to.
179+
let old_serial = $timestamp.load(Ordering::Acquire);
180+
if old_serial >= header.time as usize { break; }
181+
if $timestamp.compare_exchange(old_serial, header.time as usize, Ordering::AcqRel, Ordering::Relaxed).is_ok() {
182+
break;
183+
}
184+
}
185+
}
186+
}
187+
188+
max_time!(self.highest_seen_timestamp);
189+
}
167190
}
191+
192+
impl<ES: Deref, MR: Deref> chain::Listen for OffersMessageFlow<ES, MR>
193+
where
194+
ES::Target: EntropySource,
195+
MR::Target: MessageRouter,
196+
{
197+
fn filtered_block_connected(&self, header: &Header, _txdata: &TransactionData, _height: u32) {
198+
self.best_block_updated(header);
199+
}
200+
201+
fn block_disconnected(&self, _header: &Header, _height: u32) {}
202+
}

0 commit comments

Comments
 (0)