Skip to content

Commit 1c65a51

Browse files
committed
Support async results in TestChainSource, count get_utxo calls
1 parent 9762dc8 commit 1c65a51

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
@@ -1950,7 +1950,7 @@ mod tests {
19501950
use crate::ln::chan_utils::make_funding_redeemscript;
19511951
use crate::ln::features::InitFeatures;
19521952
use crate::routing::gossip::{P2PGossipSync, NetworkGraph, NetworkUpdate, NodeAlias, MAX_EXCESS_BYTES_FOR_RELAY, NodeId, RoutingFees, ChannelUpdateInfo, ChannelInfo, NodeAnnouncementInfo, NodeInfo};
1953-
use crate::routing::gossip_checking::ChainAccessError;
1953+
use crate::routing::gossip_checking::{ChainAccessError, ChainAccessResult};
19541954
use crate::ln::msgs::{RoutingMessageHandler, UnsignedNodeAnnouncement, NodeAnnouncement,
19551955
UnsignedChannelAnnouncement, ChannelAnnouncement, UnsignedChannelUpdate, ChannelUpdate,
19561956
ReplyChannelRange, QueryChannelRange, QueryShortChannelIds, MAX_VALUE_MSAT};
@@ -2182,7 +2182,7 @@ mod tests {
21822182

21832183
// Test if an associated transaction were not on-chain (or not confirmed).
21842184
let chain_source = test_utils::TestChainSource::new(Network::Testnet);
2185-
*chain_source.utxo_ret.lock().unwrap() = Err(ChainAccessError::UnknownTx);
2185+
*chain_source.utxo_ret.lock().unwrap() = ChainAccessResult::Sync(Err(ChainAccessError::UnknownTx));
21862186
let network_graph = NetworkGraph::new(genesis_hash, &logger);
21872187
gossip_sync = P2PGossipSync::new(&network_graph, Some(&chain_source), &logger);
21882188

@@ -2195,7 +2195,8 @@ mod tests {
21952195
};
21962196

21972197
// Now test if the transaction is found in the UTXO set and the script is correct.
2198-
*chain_source.utxo_ret.lock().unwrap() = Ok(TxOut { value: 0, script_pubkey: good_script.clone() });
2198+
*chain_source.utxo_ret.lock().unwrap() =
2199+
ChainAccessResult::Sync(Ok(TxOut { value: 0, script_pubkey: good_script.clone() }));
21992200
let valid_announcement = get_signed_channel_announcement(|unsigned_announcement| {
22002201
unsigned_announcement.short_channel_id += 2;
22012202
}, node_1_privkey, node_2_privkey, &secp_ctx);
@@ -2213,7 +2214,8 @@ mod tests {
22132214

22142215
// If we receive announcement for the same channel, once we've validated it against the
22152216
// chain, we simply ignore all new (duplicate) announcements.
2216-
*chain_source.utxo_ret.lock().unwrap() = Ok(TxOut { value: 0, script_pubkey: good_script });
2217+
*chain_source.utxo_ret.lock().unwrap() =
2218+
ChainAccessResult::Sync(Ok(TxOut { value: 0, script_pubkey: good_script }));
22172219
match gossip_sync.handle_channel_announcement(&valid_announcement) {
22182220
Ok(_) => panic!(),
22192221
Err(e) => assert_eq!(e.err, "Already have chain-validated channel")
@@ -2287,7 +2289,8 @@ mod tests {
22872289
{
22882290
// Announce a channel we will update
22892291
let good_script = get_channel_script(&secp_ctx);
2290-
*chain_source.utxo_ret.lock().unwrap() = Ok(TxOut { value: amount_sats, script_pubkey: good_script.clone() });
2292+
*chain_source.utxo_ret.lock().unwrap() =
2293+
ChainAccessResult::Sync(Ok(TxOut { value: amount_sats, script_pubkey: good_script.clone() }));
22912294

22922295
let valid_channel_announcement = get_signed_channel_announcement(|_| {}, node_1_privkey, node_2_privkey, &secp_ctx);
22932296
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
@@ -35,6 +35,7 @@ pub enum ChainAccessError {
3535
/// The result of a [`ChainAccess::get_utxo`] call. A call may resolve either synchronously,
3636
/// returning the `Sync` variant, or asynchronously, returning an [`AccessFuture`] in the `Async`
3737
/// variant.
38+
#[derive(Clone)]
3839
pub enum ChainAccessResult {
3940
/// A result which was resolved synchronously. It either includes a [`TxOut`] for the output
4041
/// requested or a [`ChainAccessError`].

lightning/src/routing/router.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2133,6 +2133,7 @@ fn build_route_from_hops_internal<L: Deref>(
21332133
#[cfg(test)]
21342134
mod tests {
21352135
use crate::routing::gossip::{NetworkGraph, P2PGossipSync, NodeId, EffectiveCapacity};
2136+
use crate::routing::gossip_checking::ChainAccessResult;
21362137
use crate::routing::router::{get_route, build_route_from_hops_internal, add_random_cltv_offset, default_node_features,
21372138
PaymentParameters, Route, RouteHint, RouteHintHop, RouteHop, RoutingFees,
21382139
DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA, MAX_PATH_LENGTH_ESTIMATE};
@@ -3547,7 +3548,8 @@ mod tests {
35473548
.push_opcode(opcodes::all::OP_PUSHNUM_2)
35483549
.push_opcode(opcodes::all::OP_CHECKMULTISIG).into_script().to_v0_p2wsh();
35493550

3550-
*chain_monitor.utxo_ret.lock().unwrap() = Ok(TxOut { value: 15, script_pubkey: good_script.clone() });
3551+
*chain_monitor.utxo_ret.lock().unwrap() =
3552+
ChainAccessResult::Sync(Ok(TxOut { value: 15, script_pubkey: good_script.clone() }));
35513553
gossip_sync.add_chain_access(Some(chain_monitor));
35523554

35533555
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
@@ -820,7 +820,8 @@ impl core::fmt::Debug for OnGetShutdownScriptpubkey {
820820

821821
pub struct TestChainSource {
822822
pub genesis_hash: BlockHash,
823-
pub utxo_ret: Mutex<Result<TxOut, ChainAccessError>>,
823+
pub utxo_ret: Mutex<ChainAccessResult>,
824+
pub get_utxo_call_count: AtomicUsize,
824825
pub watched_txn: Mutex<HashSet<(Txid, Script)>>,
825826
pub watched_outputs: Mutex<HashSet<(OutPoint, Script)>>,
826827
}
@@ -830,7 +831,8 @@ impl TestChainSource {
830831
let script_pubkey = Builder::new().push_opcode(opcodes::OP_TRUE).into_script();
831832
Self {
832833
genesis_hash: genesis_block(network).block_hash(),
833-
utxo_ret: Mutex::new(Ok(TxOut { value: u64::max_value(), script_pubkey })),
834+
utxo_ret: Mutex::new(ChainAccessResult::Sync(Ok(TxOut { value: u64::max_value(), script_pubkey }))),
835+
get_utxo_call_count: AtomicUsize::new(0),
834836
watched_txn: Mutex::new(HashSet::new()),
835837
watched_outputs: Mutex::new(HashSet::new()),
836838
}
@@ -839,11 +841,12 @@ impl TestChainSource {
839841

840842
impl ChainAccess for TestChainSource {
841843
fn get_utxo(&self, genesis_hash: &BlockHash, _short_channel_id: u64) -> ChainAccessResult {
844+
self.get_utxo_call_count.fetch_add(1, Ordering::Relaxed);
842845
if self.genesis_hash != *genesis_hash {
843846
return ChainAccessResult::Sync(Err(ChainAccessError::UnknownChain));
844847
}
845848

846-
ChainAccessResult::Sync(self.utxo_ret.lock().unwrap().clone())
849+
self.utxo_ret.lock().unwrap().clone()
847850
}
848851
}
849852

0 commit comments

Comments
 (0)