Skip to content

Commit 710f6c0

Browse files
committed
Support async results in TestChainSource, count get_utxo calls
1 parent 8653fc2 commit 710f6c0

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
@@ -1955,7 +1955,7 @@ mod tests {
19551955
use crate::ln::chan_utils::make_funding_redeemscript;
19561956
use crate::ln::features::InitFeatures;
19571957
use crate::routing::gossip::{P2PGossipSync, NetworkGraph, NetworkUpdate, NodeAlias, MAX_EXCESS_BYTES_FOR_RELAY, NodeId, RoutingFees, ChannelUpdateInfo, ChannelInfo, NodeAnnouncementInfo, NodeInfo};
1958-
use crate::routing::gossip_checking::ChainAccessError;
1958+
use crate::routing::gossip_checking::{ChainAccessError, ChainAccessResult};
19591959
use crate::ln::msgs::{RoutingMessageHandler, UnsignedNodeAnnouncement, NodeAnnouncement,
19601960
UnsignedChannelAnnouncement, ChannelAnnouncement, UnsignedChannelUpdate, ChannelUpdate,
19611961
ReplyChannelRange, QueryChannelRange, QueryShortChannelIds, MAX_VALUE_MSAT};
@@ -2187,7 +2187,7 @@ mod tests {
21872187

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

@@ -2200,7 +2200,8 @@ mod tests {
22002200
};
22012201

22022202
// Now test if the transaction is found in the UTXO set and the script is correct.
2203-
*chain_source.utxo_ret.lock().unwrap() = Ok(TxOut { value: 0, script_pubkey: good_script.clone() });
2203+
*chain_source.utxo_ret.lock().unwrap() =
2204+
ChainAccessResult::Sync(Ok(TxOut { value: 0, script_pubkey: good_script.clone() }));
22042205
let valid_announcement = get_signed_channel_announcement(|unsigned_announcement| {
22052206
unsigned_announcement.short_channel_id += 2;
22062207
}, node_1_privkey, node_2_privkey, &secp_ctx);
@@ -2218,7 +2219,8 @@ mod tests {
22182219

22192220
// If we receive announcement for the same channel, once we've validated it against the
22202221
// chain, we simply ignore all new (duplicate) announcements.
2221-
*chain_source.utxo_ret.lock().unwrap() = Ok(TxOut { value: 0, script_pubkey: good_script });
2222+
*chain_source.utxo_ret.lock().unwrap() =
2223+
ChainAccessResult::Sync(Ok(TxOut { value: 0, script_pubkey: good_script }));
22222224
match gossip_sync.handle_channel_announcement(&valid_announcement) {
22232225
Ok(_) => panic!(),
22242226
Err(e) => assert_eq!(e.err, "Already have chain-validated channel")
@@ -2292,7 +2294,8 @@ mod tests {
22922294
{
22932295
// Announce a channel we will update
22942296
let good_script = get_channel_script(&secp_ctx);
2295-
*chain_source.utxo_ret.lock().unwrap() = Ok(TxOut { value: amount_sats, script_pubkey: good_script.clone() });
2297+
*chain_source.utxo_ret.lock().unwrap() =
2298+
ChainAccessResult::Sync(Ok(TxOut { value: amount_sats, script_pubkey: good_script.clone() }));
22962299

22972300
let valid_channel_announcement = get_signed_channel_announcement(|_| {}, node_1_privkey, node_2_privkey, &secp_ctx);
22982301
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
@@ -2130,6 +2130,7 @@ fn build_route_from_hops_internal<L: Deref>(
21302130
#[cfg(test)]
21312131
mod tests {
21322132
use crate::routing::gossip::{NetworkGraph, P2PGossipSync, NodeId, EffectiveCapacity};
2133+
use crate::routing::gossip_checking::ChainAccessResult;
21332134
use crate::routing::router::{get_route, build_route_from_hops_internal, add_random_cltv_offset, default_node_features,
21342135
PaymentParameters, Route, RouteHint, RouteHintHop, RouteHop, RoutingFees,
21352136
DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA, MAX_PATH_LENGTH_ESTIMATE};
@@ -3544,7 +3545,8 @@ mod tests {
35443545
.push_opcode(opcodes::all::OP_PUSHNUM_2)
35453546
.push_opcode(opcodes::all::OP_CHECKMULTISIG).into_script().to_v0_p2wsh();
35463547

3547-
*chain_monitor.utxo_ret.lock().unwrap() = Ok(TxOut { value: 15, script_pubkey: good_script.clone() });
3548+
*chain_monitor.utxo_ret.lock().unwrap() =
3549+
ChainAccessResult::Sync(Ok(TxOut { value: 15, script_pubkey: good_script.clone() }));
35483550
gossip_sync.add_chain_access(Some(chain_monitor));
35493551

35503552
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)