Skip to content

Commit affef24

Browse files
committed
Support async results in TestChainSource, count get_utxo calls
1 parent 8e608ad commit affef24

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
@@ -1928,7 +1928,7 @@ mod tests {
19281928
#[cfg(feature = "std")]
19291929
use crate::ln::features::InitFeatures;
19301930
use crate::routing::gossip::{P2PGossipSync, NetworkGraph, NetworkUpdate, NodeAlias, MAX_EXCESS_BYTES_FOR_RELAY, NodeId, RoutingFees, ChannelUpdateInfo, ChannelInfo, NodeAnnouncementInfo, NodeInfo};
1931-
use crate::routing::gossip_checking::ChainAccessError;
1931+
use crate::routing::gossip_checking::{ChainAccessError, ChainAccessResult};
19321932
use crate::ln::msgs::{RoutingMessageHandler, UnsignedNodeAnnouncement, NodeAnnouncement,
19331933
UnsignedChannelAnnouncement, ChannelAnnouncement, UnsignedChannelUpdate, ChannelUpdate,
19341934
ReplyChannelRange, QueryChannelRange, QueryShortChannelIds, MAX_VALUE_MSAT};
@@ -2160,7 +2160,7 @@ mod tests {
21602160

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

@@ -2173,7 +2173,8 @@ mod tests {
21732173
};
21742174

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

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

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

lightning/src/routing/gossip_checking.rs

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

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::gossip_checking::ChainAccessResult;
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+
ChainAccessResult::Sync(Ok(TxOut { value: 15, script_pubkey: good_script.clone() }));
35303532
gossip_sync.add_chain_access(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/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, ChainAccessError>>,
844+
pub utxo_ret: Mutex<ChainAccessResult>,
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(ChainAccessResult::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 ChainAccess for TestChainSource {
862864
fn get_utxo(&self, genesis_hash: &BlockHash, _short_channel_id: u64) -> ChainAccessResult {
865+
self.get_utxo_call_count.fetch_add(1, Ordering::Relaxed);
863866
if self.genesis_hash != *genesis_hash {
864867
return ChainAccessResult::Sync(Err(ChainAccessError::UnknownChain));
865868
}
866869

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

0 commit comments

Comments
 (0)