Skip to content

Commit 9d8695b

Browse files
committed
Support async results in TestChainSource, count get_utxo calls
1 parent 497e246 commit 9d8695b

File tree

4 files changed

+18
-9
lines changed

4 files changed

+18
-9
lines changed

lightning/src/routing/gossip.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1927,7 +1927,7 @@ mod tests {
19271927
#[cfg(feature = "std")]
19281928
use crate::ln::features::InitFeatures;
19291929
use crate::routing::gossip::{P2PGossipSync, NetworkGraph, NetworkUpdate, NodeAlias, MAX_EXCESS_BYTES_FOR_RELAY, NodeId, RoutingFees, ChannelUpdateInfo, ChannelInfo, NodeAnnouncementInfo, NodeInfo};
1930-
use crate::routing::utxo::UtxoLookupError;
1930+
use crate::routing::utxo::{UtxoLookupError, UtxoResult};
19311931
use crate::ln::msgs::{RoutingMessageHandler, UnsignedNodeAnnouncement, NodeAnnouncement,
19321932
UnsignedChannelAnnouncement, ChannelAnnouncement, UnsignedChannelUpdate, ChannelUpdate,
19331933
ReplyChannelRange, QueryChannelRange, QueryShortChannelIds, MAX_VALUE_MSAT};
@@ -2159,7 +2159,7 @@ mod tests {
21592159

21602160
// Test if an associated transaction were not on-chain (or not confirmed).
21612161
let chain_source = test_utils::TestChainSource::new(Network::Testnet);
2162-
*chain_source.utxo_ret.lock().unwrap() = Err(UtxoLookupError::UnknownTx);
2162+
*chain_source.utxo_ret.lock().unwrap() = UtxoResult::Sync(Err(UtxoLookupError::UnknownTx));
21632163
let network_graph = NetworkGraph::new(genesis_hash, &logger);
21642164
gossip_sync = P2PGossipSync::new(&network_graph, Some(&chain_source), &logger);
21652165

@@ -2172,7 +2172,8 @@ mod tests {
21722172
};
21732173

21742174
// Now test if the transaction is found in the UTXO set and the script is correct.
2175-
*chain_source.utxo_ret.lock().unwrap() = Ok(TxOut { value: 0, script_pubkey: good_script.clone() });
2175+
*chain_source.utxo_ret.lock().unwrap() =
2176+
UtxoResult::Sync(Ok(TxOut { value: 0, script_pubkey: good_script.clone() }));
21762177
let valid_announcement = get_signed_channel_announcement(|unsigned_announcement| {
21772178
unsigned_announcement.short_channel_id += 2;
21782179
}, node_1_privkey, node_2_privkey, &secp_ctx);
@@ -2190,7 +2191,8 @@ mod tests {
21902191

21912192
// If we receive announcement for the same channel, once we've validated it against the
21922193
// chain, we simply ignore all new (duplicate) announcements.
2193-
*chain_source.utxo_ret.lock().unwrap() = Ok(TxOut { value: 0, script_pubkey: good_script });
2194+
*chain_source.utxo_ret.lock().unwrap() =
2195+
UtxoResult::Sync(Ok(TxOut { value: 0, script_pubkey: good_script }));
21942196
match gossip_sync.handle_channel_announcement(&valid_announcement) {
21952197
Ok(_) => panic!(),
21962198
Err(e) => assert_eq!(e.err, "Already have chain-validated channel")
@@ -2264,7 +2266,8 @@ mod tests {
22642266
{
22652267
// Announce a channel we will update
22662268
let good_script = get_channel_script(&secp_ctx);
2267-
*chain_source.utxo_ret.lock().unwrap() = Ok(TxOut { value: amount_sats, script_pubkey: good_script.clone() });
2269+
*chain_source.utxo_ret.lock().unwrap() =
2270+
UtxoResult::Sync(Ok(TxOut { value: amount_sats, script_pubkey: good_script.clone() }));
22682271

22692272
let valid_channel_announcement = get_signed_channel_announcement(|_| {}, node_1_privkey, node_2_privkey, &secp_ctx);
22702273
short_channel_id = valid_channel_announcement.contents.short_channel_id;

lightning/src/routing/router.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2112,6 +2112,7 @@ fn build_route_from_hops_internal<L: Deref>(
21122112
#[cfg(test)]
21132113
mod tests {
21142114
use crate::routing::gossip::{NetworkGraph, P2PGossipSync, NodeId, EffectiveCapacity};
2115+
use crate::routing::utxo::UtxoResult;
21152116
use crate::routing::router::{get_route, build_route_from_hops_internal, add_random_cltv_offset, default_node_features,
21162117
PaymentParameters, Route, RouteHint, RouteHintHop, RouteHop, RoutingFees,
21172118
DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA, MAX_PATH_LENGTH_ESTIMATE};
@@ -3526,7 +3527,8 @@ mod tests {
35263527
.push_opcode(opcodes::all::OP_PUSHNUM_2)
35273528
.push_opcode(opcodes::all::OP_CHECKMULTISIG).into_script().to_v0_p2wsh();
35283529

3529-
*chain_monitor.utxo_ret.lock().unwrap() = Ok(TxOut { value: 15, script_pubkey: good_script.clone() });
3530+
*chain_monitor.utxo_ret.lock().unwrap() =
3531+
UtxoResult::Sync(Ok(TxOut { value: 15, script_pubkey: good_script.clone() }));
35303532
gossip_sync.add_utxo_lookup(Some(chain_monitor));
35313533

35323534
add_channel(&gossip_sync, &secp_ctx, &privkeys[0], &privkeys[2], ChannelFeatures::from_le_bytes(id_to_feature_flags(3)), 333);

lightning/src/routing/utxo.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ pub enum UtxoLookupError {
4242
/// The result of a [`UtxoLookup::get_utxo`] call. A call may resolve either synchronously,
4343
/// returning the `Sync` variant, or asynchronously, returning an [`UtxoFuture`] in the `Async`
4444
/// variant.
45+
#[derive(Clone)]
4546
pub enum UtxoResult {
4647
/// A result which was resolved synchronously. It either includes a [`TxOut`] for the output
4748
/// requested or a [`UtxoLookupError`].

lightning/src/util/test_utils.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,8 @@ impl core::fmt::Debug for OnGetShutdownScriptpubkey {
841841

842842
pub struct TestChainSource {
843843
pub genesis_hash: BlockHash,
844-
pub utxo_ret: Mutex<Result<TxOut, UtxoLookupError>>,
844+
pub utxo_ret: Mutex<UtxoResult>,
845+
pub get_utxo_call_count: AtomicUsize,
845846
pub watched_txn: Mutex<HashSet<(Txid, Script)>>,
846847
pub watched_outputs: Mutex<HashSet<(OutPoint, Script)>>,
847848
}
@@ -851,7 +852,8 @@ impl TestChainSource {
851852
let script_pubkey = Builder::new().push_opcode(opcodes::OP_TRUE).into_script();
852853
Self {
853854
genesis_hash: genesis_block(network).block_hash(),
854-
utxo_ret: Mutex::new(Ok(TxOut { value: u64::max_value(), script_pubkey })),
855+
utxo_ret: Mutex::new(UtxoResult::Sync(Ok(TxOut { value: u64::max_value(), script_pubkey }))),
856+
get_utxo_call_count: AtomicUsize::new(0),
855857
watched_txn: Mutex::new(HashSet::new()),
856858
watched_outputs: Mutex::new(HashSet::new()),
857859
}
@@ -860,11 +862,12 @@ impl TestChainSource {
860862

861863
impl UtxoLookup for TestChainSource {
862864
fn get_utxo(&self, genesis_hash: &BlockHash, _short_channel_id: u64) -> UtxoResult {
865+
self.get_utxo_call_count.fetch_add(1, Ordering::Relaxed);
863866
if self.genesis_hash != *genesis_hash {
864867
return UtxoResult::Sync(Err(UtxoLookupError::UnknownChain));
865868
}
866869

867-
UtxoResult::Sync(self.utxo_ret.lock().unwrap().clone())
870+
self.utxo_ret.lock().unwrap().clone()
868871
}
869872
}
870873

0 commit comments

Comments
 (0)