Skip to content

Commit 11cc7dc

Browse files
committed
Support async results in TestChainSource, count get_utxo calls
1 parent 2ae9980 commit 11cc7dc

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
@@ -1910,7 +1910,7 @@ mod tests {
19101910
#[cfg(feature = "std")]
19111911
use crate::ln::features::InitFeatures;
19121912
use crate::routing::gossip::{P2PGossipSync, NetworkGraph, NetworkUpdate, NodeAlias, MAX_EXCESS_BYTES_FOR_RELAY, NodeId, RoutingFees, ChannelUpdateInfo, ChannelInfo, NodeAnnouncementInfo, NodeInfo};
1913-
use crate::routing::gossip_checking::ChainAccessError;
1913+
use crate::routing::gossip_checking::{ChainAccessError, ChainAccessResult};
19141914
use crate::ln::msgs::{RoutingMessageHandler, UnsignedNodeAnnouncement, NodeAnnouncement,
19151915
UnsignedChannelAnnouncement, ChannelAnnouncement, UnsignedChannelUpdate, ChannelUpdate,
19161916
ReplyChannelRange, QueryChannelRange, QueryShortChannelIds, MAX_VALUE_MSAT};
@@ -2142,7 +2142,7 @@ mod tests {
21422142

21432143
// Test if an associated transaction were not on-chain (or not confirmed).
21442144
let chain_source = test_utils::TestChainSource::new(Network::Testnet);
2145-
*chain_source.utxo_ret.lock().unwrap() = Err(ChainAccessError::UnknownTx);
2145+
*chain_source.utxo_ret.lock().unwrap() = ChainAccessResult::Sync(Err(ChainAccessError::UnknownTx));
21462146
let network_graph = NetworkGraph::new(genesis_hash, &logger);
21472147
gossip_sync = P2PGossipSync::new(&network_graph, Some(&chain_source), &logger);
21482148

@@ -2155,7 +2155,8 @@ mod tests {
21552155
};
21562156

21572157
// Now test if the transaction is found in the UTXO set and the script is correct.
2158-
*chain_source.utxo_ret.lock().unwrap() = Ok(TxOut { value: 0, script_pubkey: good_script.clone() });
2158+
*chain_source.utxo_ret.lock().unwrap() =
2159+
ChainAccessResult::Sync(Ok(TxOut { value: 0, script_pubkey: good_script.clone() }));
21592160
let valid_announcement = get_signed_channel_announcement(|unsigned_announcement| {
21602161
unsigned_announcement.short_channel_id += 2;
21612162
}, node_1_privkey, node_2_privkey, &secp_ctx);
@@ -2173,7 +2174,8 @@ mod tests {
21732174

21742175
// If we receive announcement for the same channel, once we've validated it against the
21752176
// chain, we simply ignore all new (duplicate) announcements.
2176-
*chain_source.utxo_ret.lock().unwrap() = Ok(TxOut { value: 0, script_pubkey: good_script });
2177+
*chain_source.utxo_ret.lock().unwrap() =
2178+
ChainAccessResult::Sync(Ok(TxOut { value: 0, script_pubkey: good_script }));
21772179
match gossip_sync.handle_channel_announcement(&valid_announcement) {
21782180
Ok(_) => panic!(),
21792181
Err(e) => assert_eq!(e.err, "Already have chain-validated channel")
@@ -2247,7 +2249,8 @@ mod tests {
22472249
{
22482250
// Announce a channel we will update
22492251
let good_script = get_channel_script(&secp_ctx);
2250-
*chain_source.utxo_ret.lock().unwrap() = Ok(TxOut { value: amount_sats, script_pubkey: good_script.clone() });
2252+
*chain_source.utxo_ret.lock().unwrap() =
2253+
ChainAccessResult::Sync(Ok(TxOut { value: amount_sats, script_pubkey: good_script.clone() }));
22512254

22522255
let valid_channel_announcement = get_signed_channel_announcement(|_| {}, node_1_privkey, node_2_privkey, &secp_ctx);
22532256
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
@@ -44,6 +44,7 @@ pub enum ChainAccessError {
4444
/// The result of a [`ChainAccess::get_utxo`] call. A call may resolve either synchronously,
4545
/// returning the `Sync` variant, or asynchronously, returning an [`AccessFuture`] in the `Async`
4646
/// variant.
47+
#[derive(Clone)]
4748
pub enum ChainAccessResult {
4849
/// A result which was resolved synchronously. It either includes a [`TxOut`] for the output
4950
/// requested or a [`ChainAccessError`].

lightning/src/routing/router.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2090,6 +2090,7 @@ fn build_route_from_hops_internal<L: Deref>(
20902090
#[cfg(test)]
20912091
mod tests {
20922092
use crate::routing::gossip::{NetworkGraph, P2PGossipSync, NodeId, EffectiveCapacity};
2093+
use crate::routing::gossip_checking::ChainAccessResult;
20932094
use crate::routing::router::{get_route, build_route_from_hops_internal, add_random_cltv_offset, default_node_features,
20942095
PaymentParameters, Route, RouteHint, RouteHintHop, RouteHop, RoutingFees,
20952096
DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA, MAX_PATH_LENGTH_ESTIMATE};
@@ -3504,7 +3505,8 @@ mod tests {
35043505
.push_opcode(opcodes::all::OP_PUSHNUM_2)
35053506
.push_opcode(opcodes::all::OP_CHECKMULTISIG).into_script().to_v0_p2wsh();
35063507

3507-
*chain_monitor.utxo_ret.lock().unwrap() = Ok(TxOut { value: 15, script_pubkey: good_script.clone() });
3508+
*chain_monitor.utxo_ret.lock().unwrap() =
3509+
ChainAccessResult::Sync(Ok(TxOut { value: 15, script_pubkey: good_script.clone() }));
35083510
gossip_sync.add_chain_access(Some(chain_monitor));
35093511

35103512
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
@@ -840,7 +840,8 @@ impl core::fmt::Debug for OnGetShutdownScriptpubkey {
840840

841841
pub struct TestChainSource {
842842
pub genesis_hash: BlockHash,
843-
pub utxo_ret: Mutex<Result<TxOut, ChainAccessError>>,
843+
pub utxo_ret: Mutex<ChainAccessResult>,
844+
pub get_utxo_call_count: AtomicUsize,
844845
pub watched_txn: Mutex<HashSet<(Txid, Script)>>,
845846
pub watched_outputs: Mutex<HashSet<(OutPoint, Script)>>,
846847
}
@@ -850,7 +851,8 @@ impl TestChainSource {
850851
let script_pubkey = Builder::new().push_opcode(opcodes::OP_TRUE).into_script();
851852
Self {
852853
genesis_hash: genesis_block(network).block_hash(),
853-
utxo_ret: Mutex::new(Ok(TxOut { value: u64::max_value(), script_pubkey })),
854+
utxo_ret: Mutex::new(ChainAccessResult::Sync(Ok(TxOut { value: u64::max_value(), script_pubkey }))),
855+
get_utxo_call_count: AtomicUsize::new(0),
854856
watched_txn: Mutex::new(HashSet::new()),
855857
watched_outputs: Mutex::new(HashSet::new()),
856858
}
@@ -859,11 +861,12 @@ impl TestChainSource {
859861

860862
impl ChainAccess for TestChainSource {
861863
fn get_utxo(&self, genesis_hash: &BlockHash, _short_channel_id: u64) -> ChainAccessResult {
864+
self.get_utxo_call_count.fetch_add(1, Ordering::Relaxed);
862865
if self.genesis_hash != *genesis_hash {
863866
return ChainAccessResult::Sync(Err(ChainAccessError::UnknownChain));
864867
}
865868

866-
ChainAccessResult::Sync(self.utxo_ret.lock().unwrap().clone())
869+
self.utxo_ret.lock().unwrap().clone()
867870
}
868871
}
869872

0 commit comments

Comments
 (0)