Skip to content

Commit

Permalink
[Java] Update ChannelManagerConstructor
Browse files Browse the repository at this point in the history
This allows PhantomKeysManagers to be used too
  • Loading branch information
keyuebao committed May 25, 2023
1 parent a672396 commit fc1713f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
30 changes: 16 additions & 14 deletions src/main/java/org/ldk/batteries/ChannelManagerConstructor.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ public static class InvalidSerializedDataException extends Exception {
*/
private final ProbabilisticScorer prob_scorer;
private final Logger logger;
private final KeysManager keys_manager;
private final EntropySource entropy_source;
private final NodeSigner node_signer;

/**
* Exposes the `ProbabilisticScorer` wrapped inside a lock. Don't forget to `close` this lock when you're done with
Expand Down Expand Up @@ -132,13 +133,14 @@ Result_RouteLightningErrorZ find_route(byte[] payer_node_id, RouteParameters rou
* @param router_wrapper If provided, routes will be fetched by calling the given router rather than an LDK `DefaultRouter`.
*/
public ChannelManagerConstructor(byte[] channel_manager_serialized, byte[][] channel_monitors_serialized, UserConfig config,
KeysManager keys_manager, FeeEstimator fee_estimator, ChainMonitor chain_monitor,
EntropySource entropy_source, NodeSigner node_signer, SignerProvider signer_provider,
FeeEstimator fee_estimator, ChainMonitor chain_monitor,
@Nullable Filter filter, byte[] net_graph_serialized,
ProbabilisticScoringParameters scoring_params, byte[] probabilistic_scorer_bytes,
@Nullable RouterWrapper router_wrapper,
BroadcasterInterface tx_broadcaster, Logger logger) throws InvalidSerializedDataException {
this.keys_manager = keys_manager;
EntropySource entropy_source = keys_manager.as_EntropySource();
this.entropy_source = entropy_source;
this.node_signer = node_signer;

Result_NetworkGraphDecodeErrorZ graph_res = NetworkGraph.read(net_graph_serialized, logger);
if (!graph_res.is_ok()) {
Expand Down Expand Up @@ -173,7 +175,7 @@ public ChannelManagerConstructor(byte[] channel_manager_serialized, byte[][] cha
this.channel_monitors = new TwoTuple_BlockHashChannelMonitorZ[monitors.length];
HashSet<OutPoint> monitor_funding_set = new HashSet();
for (int i = 0; i < monitors.length; i++) {
Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ res = UtilMethods.C2Tuple_BlockHashChannelMonitorZ_read(channel_monitors_serialized[i], entropy_source, keys_manager.as_SignerProvider());
Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ res = UtilMethods.C2Tuple_BlockHashChannelMonitorZ_read(channel_monitors_serialized[i], entropy_source, signer_provider);
if (res instanceof Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ.Result_C2Tuple_BlockHashChannelMonitorZDecodeErrorZ_Err) {
throw new InvalidSerializedDataException("Serialized ChannelMonitor was corrupt");
}
Expand All @@ -184,8 +186,8 @@ public ChannelManagerConstructor(byte[] channel_manager_serialized, byte[][] cha
throw new InvalidSerializedDataException("Set of ChannelMonitors contained duplicates (ie the same funding_txo was set on multiple monitors)");
}
Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ res =
UtilMethods.C2Tuple_BlockHashChannelManagerZ_read(channel_manager_serialized, keys_manager.as_EntropySource(),
keys_manager.as_NodeSigner(), keys_manager.as_SignerProvider(), fee_estimator, chain_monitor.as_Watch(),
UtilMethods.C2Tuple_BlockHashChannelManagerZ_read(channel_manager_serialized, entropy_source,
node_signer, signer_provider, fee_estimator, chain_monitor.as_Watch(),
tx_broadcaster, router, logger, config, monitors);
if (!res.is_ok()) {
throw new InvalidSerializedDataException("Serialized ChannelManager was corrupt");
Expand All @@ -207,13 +209,13 @@ public ChannelManagerConstructor(byte[] channel_manager_serialized, byte[][] cha
* @param router_wrapper If provided, routes will be fetched by calling the given router rather than an LDK `DefaultRouter`.
*/
public ChannelManagerConstructor(Network network, UserConfig config, byte[] current_blockchain_tip_hash, int current_blockchain_tip_height,
KeysManager keys_manager, FeeEstimator fee_estimator, ChainMonitor chain_monitor,
EntropySource entropy_source, NodeSigner node_signer, SignerProvider signer_provider,
FeeEstimator fee_estimator, ChainMonitor chain_monitor,
NetworkGraph net_graph, ProbabilisticScoringParameters scoring_params,
@Nullable RouterWrapper router_wrapper,
BroadcasterInterface tx_broadcaster, Logger logger) {
this.keys_manager = keys_manager;
EntropySource entropy_source = keys_manager.as_EntropySource();

this.entropy_source = entropy_source;
this.node_signer = node_signer;
this.net_graph = net_graph;
assert(scoring_params != null);
this.prob_scorer = ProbabilisticScorer.of(scoring_params, net_graph, logger);
Expand All @@ -239,7 +241,7 @@ public ChannelManagerConstructor(Network network, UserConfig config, byte[] curr
BestBlock block = BestBlock.of(current_blockchain_tip_hash, current_blockchain_tip_height);
ChainParameters params = ChainParameters.of(network, block);
channel_manager = ChannelManager.of(fee_estimator, chain_monitor.as_Watch(), tx_broadcaster, router, logger,
keys_manager.as_EntropySource(), keys_manager.as_NodeSigner(), keys_manager.as_SignerProvider(), config, params);
entropy_source, node_signer, signer_provider, config, params);
this.logger = logger;
}

Expand Down Expand Up @@ -279,8 +281,8 @@ public void chain_sync_completed(EventHandler event_handler, boolean use_p2p_gra
P2PGossipSync graph_msg_handler = P2PGossipSync.of(net_graph, Option_UtxoLookupZ.none(), logger);
this.peer_manager = PeerManager.of(channel_manager.as_ChannelMessageHandler(),
ignoring_handler.as_RoutingMessageHandler(), ignoring_handler.as_OnionMessageHandler(),
(int)(System.currentTimeMillis() / 1000), this.keys_manager.as_EntropySource().get_secure_random_bytes(),
logger, ignoring_handler.as_CustomMessageHandler(), keys_manager.as_NodeSigner());
(int)(System.currentTimeMillis() / 1000), this.entropy_source.get_secure_random_bytes(),
logger, ignoring_handler.as_CustomMessageHandler(), this.node_signer);

try {
this.nio_peer_handler = new NioPeerHandler(peer_manager);
Expand Down
9 changes: 6 additions & 3 deletions src/test/java/org/ldk/HumanObjectPeerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,8 @@ private void setup_route_handler() {

if (use_chan_manager_constructor) {
this.constructor = new ChannelManagerConstructor(Network.LDKNetwork_Bitcoin, get_config(), new byte[32], 0,
this.explicit_keys_manager, this.fee_estimator, this.chain_monitor, this.net_graph,
this.explicit_keys_manager.as_EntropySource(), this.explicit_keys_manager.as_NodeSigner(), this.explicit_keys_manager.as_SignerProvider(),
this.fee_estimator, this.chain_monitor, this.net_graph,
ProbabilisticScoringParameters.with_default(), (ChannelManagerConstructor.RouterWrapper)
(payer_node_id, route_params, first_hops, inflight_htlcs, payment_hash, payment_id, default_router) -> {
assert payment_hash != null && payment_id != null;
Expand Down Expand Up @@ -466,7 +467,8 @@ public Result_RouteLightningErrorZ find_route(byte[] payer, RouteParameters para
filter_nullable = ((Option_FilterZ.Some) this.filter).some;
}
this.constructor = new ChannelManagerConstructor(serialized, monitors, get_config(),
this.explicit_keys_manager, this.fee_estimator, this.chain_monitor, filter_nullable,
this.explicit_keys_manager.as_EntropySource(), this.explicit_keys_manager.as_NodeSigner(), this.explicit_keys_manager.as_SignerProvider(),
this.fee_estimator, this.chain_monitor, filter_nullable,
serialized_graph, ProbabilisticScoringParameters.with_default(), serialized_scorer, null,
this.tx_broadcaster, this.logger);
try {
Expand All @@ -475,7 +477,8 @@ public Result_RouteLightningErrorZ find_route(byte[] payer, RouteParameters para
monitors_dupd[0] = monitors[0];
monitors_dupd[1] = monitors[0];
ChannelManagerConstructor constr = new ChannelManagerConstructor(serialized, monitors_dupd, get_config(),
this.explicit_keys_manager, this.fee_estimator, this.chain_monitor, filter_nullable,
this.explicit_keys_manager.as_EntropySource(), this.explicit_keys_manager.as_NodeSigner(), this.explicit_keys_manager.as_SignerProvider(),
this.fee_estimator, this.chain_monitor, filter_nullable,
serialized_graph, ProbabilisticScoringParameters.with_default(), serialized_scorer, null,
this.tx_broadcaster, this.logger);
assert false;
Expand Down

0 comments on commit fc1713f

Please sign in to comment.