Skip to content

Commit 75bc9cf

Browse files
Matt CoralloTheBlueMatt
authored andcommitted
Add support for InvoicePayer to ChannelManagerConstructor and test
Note that this adds an additional bit of runs in the HumanObjectPeerTest, bringing leaks for a full run to: 149 allocations remained for 1157302 bytes.
1 parent dc707e8 commit 75bc9cf

File tree

2 files changed

+153
-65
lines changed

2 files changed

+153
-65
lines changed

src/main/java/org/ldk/batteries/ChannelManagerConstructor.java

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,22 @@ public static class InvalidSerializedDataException extends Exception {}
4545
* A NioPeerHandler which manages a background thread to handle socket events and pass them to the peer_manager.
4646
*/
4747
public final NioPeerHandler nio_peer_handler;
48+
/**
49+
* If a `NetworkGraph` is provided to the constructor *and* a `LockableScore` is provided to
50+
* `chain_sync_completed`, this will be non-null after `chain_sync_completed` returns.
51+
*
52+
* It should be used to send payments instead of doing so directly via the `channel_manager`.
53+
*
54+
* When payments are made through this, they are automatically retried and the provided Scorer
55+
* will be updated with payment failure data.
56+
*/
57+
@Nullable public InvoicePayer payer;
4858

4959
private final ChainMonitor chain_monitor;
50-
60+
@Nullable private final NetworkGraph net_graph;
61+
@Nullable private final NetGraphMsgHandler graph_msg_handler;
5162
private final Logger logger;
5263

53-
public final @Nullable NetGraphMsgHandler router;
54-
5564
/**
5665
* Deserializes a channel manager and a set of channel monitors from the given serialized copies and interface implementations
5766
*
@@ -61,7 +70,7 @@ public static class InvalidSerializedDataException extends Exception {}
6170
*/
6271
public ChannelManagerConstructor(byte[] channel_manager_serialized, byte[][] channel_monitors_serialized,
6372
KeysInterface keys_interface, FeeEstimator fee_estimator, ChainMonitor chain_monitor, @Nullable Filter filter,
64-
@Nullable NetGraphMsgHandler router,
73+
@Nullable NetworkGraph net_graph,
6574
BroadcasterInterface tx_broadcaster, Logger logger) throws InvalidSerializedDataException {
6675
final IgnoringMessageHandler no_custom_messages = IgnoringMessageHandler.of();
6776
final ChannelMonitor[] monitors = new ChannelMonitor[channel_monitors_serialized.length];
@@ -84,14 +93,18 @@ public ChannelManagerConstructor(byte[] channel_manager_serialized, byte[][] cha
8493
this.channel_manager = ((Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ.Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ_OK)res).res.get_b();
8594
this.channel_manager_latest_block_hash = ((Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ.Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ_OK)res).res.get_a();
8695
this.chain_monitor = chain_monitor;
87-
this.router = router;
8896
this.logger = logger;
8997
byte[] random_data = keys_interface.get_secure_random_bytes();
90-
if (router != null) {
91-
this.peer_manager = PeerManager.of(channel_manager.as_ChannelMessageHandler(), router.as_RoutingMessageHandler(),
98+
this.net_graph = net_graph;
99+
if (net_graph != null) {
100+
//TODO: We really need to expose the Access here to let users prevent DoS issues
101+
this.graph_msg_handler = NetGraphMsgHandler.of(net_graph, Option_AccessZ.none(), logger);
102+
this.peer_manager = PeerManager.of(channel_manager.as_ChannelMessageHandler(),
103+
graph_msg_handler.as_RoutingMessageHandler(),
92104
keys_interface.get_node_secret(), random_data, logger, no_custom_messages.as_CustomMessageHandler());
93105
} else {
94-
this.peer_manager = PeerManager.of(channel_manager.as_ChannelMessageHandler(), (IgnoringMessageHandler.of()).as_RoutingMessageHandler(),
106+
this.graph_msg_handler = null;
107+
this.peer_manager = PeerManager.of(channel_manager.as_ChannelMessageHandler(), no_custom_messages.as_RoutingMessageHandler(),
95108
keys_interface.get_node_secret(), random_data, logger, no_custom_messages.as_CustomMessageHandler());
96109
}
97110
NioPeerHandler nio_peer_handler = null;
@@ -113,23 +126,27 @@ public ChannelManagerConstructor(byte[] channel_manager_serialized, byte[][] cha
113126
*/
114127
public ChannelManagerConstructor(Network network, UserConfig config, byte[] current_blockchain_tip_hash, int current_blockchain_tip_height,
115128
KeysInterface keys_interface, FeeEstimator fee_estimator, ChainMonitor chain_monitor,
116-
@Nullable NetGraphMsgHandler router,
129+
@Nullable NetworkGraph net_graph,
117130
BroadcasterInterface tx_broadcaster, Logger logger) {
118131
final IgnoringMessageHandler no_custom_messages = IgnoringMessageHandler.of();
119132
channel_monitors = new TwoTuple_BlockHashChannelMonitorZ[0];
120133
channel_manager_latest_block_hash = null;
121134
this.chain_monitor = chain_monitor;
122-
this.router = router;
123135
BestBlock block = BestBlock.of(current_blockchain_tip_hash, current_blockchain_tip_height);
124136
ChainParameters params = ChainParameters.of(network, block);
125137
channel_manager = ChannelManager.of(fee_estimator, chain_monitor.as_Watch(), tx_broadcaster, logger, keys_interface, config, params);
126138
this.logger = logger;
127139
byte[] random_data = keys_interface.get_secure_random_bytes();
128-
if (router != null) {
129-
this.peer_manager = PeerManager.of(channel_manager.as_ChannelMessageHandler(), router.as_RoutingMessageHandler(),
140+
this.net_graph = net_graph;
141+
if (net_graph != null) {
142+
//TODO: We really need to expose the Access here to let users prevent DoS issues
143+
this.graph_msg_handler = NetGraphMsgHandler.of(net_graph, Option_AccessZ.none(), logger);
144+
this.peer_manager = PeerManager.of(channel_manager.as_ChannelMessageHandler(),
145+
graph_msg_handler.as_RoutingMessageHandler(),
130146
keys_interface.get_node_secret(), random_data, logger, no_custom_messages.as_CustomMessageHandler());
131147
} else {
132-
this.peer_manager = PeerManager.of(channel_manager.as_ChannelMessageHandler(), (IgnoringMessageHandler.of()).as_RoutingMessageHandler(),
148+
this.graph_msg_handler = null;
149+
this.peer_manager = PeerManager.of(channel_manager.as_ChannelMessageHandler(), no_custom_messages.as_RoutingMessageHandler(),
133150
keys_interface.get_node_secret(), random_data, logger, no_custom_messages.as_CustomMessageHandler());
134151
}
135152
NioPeerHandler nio_peer_handler = null;
@@ -159,16 +176,23 @@ public interface EventHandler {
159176
* This also spawns a background thread which will call the appropriate methods on the provided
160177
* EventHandler as required.
161178
*/
162-
public void chain_sync_completed(EventHandler event_handler) {
179+
public void chain_sync_completed(EventHandler event_handler, @Nullable LockableScore scorer) {
163180
if (background_processor != null) { return; }
164181
for (TwoTuple_BlockHashChannelMonitorZ monitor: channel_monitors) {
165182
this.chain_monitor.as_Watch().watch_channel(monitor.get_b().get_funding_txo().get_a(), monitor.get_b());
166183
}
184+
org.ldk.structs.EventHandler ldk_handler = org.ldk.structs.EventHandler.new_impl(event_handler::handle_event);
185+
if (this.net_graph != null && scorer != null) {
186+
Router router = DefaultRouter.of(net_graph, logger).as_Router();
187+
this.payer = InvoicePayer.of(this.channel_manager.as_Payer(), router, scorer, this.logger, ldk_handler, RetryAttempts.of(3));
188+
assert this.payer != null;
189+
ldk_handler = this.payer.as_EventHandler();
190+
}
191+
167192
background_processor = BackgroundProcessor.start(org.ldk.structs.ChannelManagerPersister.new_impl(channel_manager -> {
168193
event_handler.persist_manager(channel_manager.write());
169194
return Result_NoneErrorZ.ok();
170-
}), org.ldk.structs.EventHandler.new_impl(event_handler::handle_event),
171-
this.chain_monitor, this.channel_manager, this.router, this.peer_manager, this.logger);
195+
}), ldk_handler, this.chain_monitor, this.channel_manager, this.graph_msg_handler, this.peer_manager, this.logger);
172196
}
173197

174198
/**

0 commit comments

Comments
 (0)