Skip to content

Commit 924271f

Browse files
committed
[Java] Update handwritten batteries and tests to LDK 0.1.0 API
1 parent 7f0994d commit 924271f

File tree

2 files changed

+30
-72
lines changed

2 files changed

+30
-72
lines changed

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

+9-32
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public static class InvalidSerializedDataException extends Exception {
8181
private final EntropySource entropy_source;
8282
private final NodeSigner node_signer;
8383
private final Router router;
84+
private final DefaultMessageRouter msg_router;
8485

8586
/**
8687
* Exposes the `ProbabilisticScorer` wrapped inside a lock. Don't forget to `close` this lock when you're done with
@@ -176,24 +177,11 @@ public Result_RouteLightningErrorZ find_route_with_id(byte[] payer, RouteParamet
176177
public Result_CVec_BlindedPaymentPathZNoneZ create_blinded_payment_paths(byte[] recipient, ChannelDetails[] first_hops, ReceiveTlvs tlvs, long amount_msats) {
177178
return default_router.as_Router().create_blinded_payment_paths(recipient, first_hops, tlvs, amount_msats);
178179
}
179-
}, new MessageRouter.MessageRouterInterface() {
180-
@Override public Result_OnionMessagePathNoneZ find_path(byte[] sender, byte[][] peers, Destination destination) {
181-
return default_router.as_MessageRouter().find_path(sender, peers, destination);
182-
}
183-
184-
@Override
185-
public Result_CVec_BlindedMessagePathZNoneZ create_blinded_paths(byte[] recipient, MessageContext context, byte[][] peers) {
186-
return default_router.as_MessageRouter().create_blinded_paths(recipient, context, peers);
187-
}
188-
189-
@Override
190-
public Result_CVec_BlindedMessagePathZNoneZ create_compact_blinded_paths(byte[] recipient, MessageContext context, MessageForwardNode[] peers) {
191-
return default_router.as_MessageRouter().create_compact_blinded_paths(recipient, context, peers);
192-
}
193180
});
194181
} else {
195182
router = default_router.as_Router();
196183
}
184+
msg_router = DefaultMessageRouter.of(this.net_graph, entropy_source);
197185

198186
final ChannelMonitor[] monitors = new ChannelMonitor[channel_monitors_serialized.length];
199187
this.channel_monitors = new TwoTuple_ThirtyTwoBytesChannelMonitorZ[monitors.length];
@@ -212,7 +200,7 @@ public Result_CVec_BlindedMessagePathZNoneZ create_compact_blinded_paths(byte[]
212200
Result_C2Tuple_ThirtyTwoBytesChannelManagerZDecodeErrorZ res =
213201
UtilMethods.C2Tuple_ThirtyTwoBytesChannelManagerZ_read(channel_manager_serialized, entropy_source,
214202
node_signer, signer_provider, fee_estimator, chain_monitor.as_Watch(),
215-
tx_broadcaster, router, logger, config, monitors);
203+
tx_broadcaster, router, this.msg_router.as_MessageRouter(), logger, config, monitors);
216204
if (!res.is_ok()) {
217205
throw new InvalidSerializedDataException("Serialized ChannelManager was corrupt");
218206
}
@@ -261,31 +249,19 @@ public ChannelManagerConstructor(Network network, UserConfig config, byte[] curr
261249
public Result_CVec_BlindedPaymentPathZNoneZ create_blinded_payment_paths(byte[] recipient, ChannelDetails[] first_hops, ReceiveTlvs tlvs, long amount_msats) {
262250
return default_router.as_Router().create_blinded_payment_paths(recipient, first_hops, tlvs, amount_msats);
263251
}
264-
}, new MessageRouter.MessageRouterInterface() {
265-
@Override public Result_OnionMessagePathNoneZ find_path(byte[] sender, byte[][] peers, Destination destination) {
266-
return default_router.as_MessageRouter().find_path(sender, peers, destination);
267-
}
268-
269-
@Override
270-
public Result_CVec_BlindedMessagePathZNoneZ create_blinded_paths(byte[] recipient, MessageContext context, byte[][] peers) {
271-
return default_router.as_MessageRouter().create_blinded_paths(recipient, context, peers);
272-
}
273-
274-
@Override
275-
public Result_CVec_BlindedMessagePathZNoneZ create_compact_blinded_paths(byte[] recipient, MessageContext context, MessageForwardNode[] peers) {
276-
return default_router.as_MessageRouter().create_compact_blinded_paths(recipient, context, peers);
277-
}
278252
});
279253
} else {
280254
router = default_router.as_Router();
281255
}
256+
msg_router = DefaultMessageRouter.of(this.net_graph, entropy_source);
282257
channel_monitors = new TwoTuple_ThirtyTwoBytesChannelMonitorZ[0];
283258
channel_manager_latest_block_hash = null;
284259
this.chain_monitor = chain_monitor;
285260
BestBlock block = BestBlock.of(current_blockchain_tip_hash, current_blockchain_tip_height);
286261
ChainParameters params = ChainParameters.of(network, block);
287-
channel_manager = ChannelManager.of(fee_estimator, chain_monitor.as_Watch(), tx_broadcaster, router, logger,
288-
entropy_source, node_signer, signer_provider, config, params, (int) (System.currentTimeMillis() / 1000));
262+
channel_manager = ChannelManager.of(fee_estimator, chain_monitor.as_Watch(), tx_broadcaster, router,
263+
this.msg_router.as_MessageRouter(), logger, entropy_source, node_signer, signer_provider, config, params,
264+
(int) (System.currentTimeMillis() / 1000));
289265
this.logger = logger;
290266
}
291267

@@ -329,8 +305,9 @@ public void chain_sync_completed(EventHandler event_handler, boolean use_p2p_gra
329305
else
330306
routing_msg_handler = ignoring_handler.as_RoutingMessageHandler();
331307
OnionMessenger messenger = OnionMessenger.of(this.entropy_source, this.node_signer, this.logger,
332-
this.channel_manager.as_NodeIdLookUp(), this.router.get_message_router(),
308+
this.channel_manager.as_NodeIdLookUp(), this.msg_router.as_MessageRouter(),
333309
channel_manager.as_OffersMessageHandler(), IgnoringMessageHandler.of().as_AsyncPaymentsMessageHandler(),
310+
channel_manager.as_DNSResolverMessageHandler(),
334311
IgnoringMessageHandler.of().as_CustomOnionMessageHandler());
335312
this.peer_manager = PeerManager.of(channel_manager.as_ChannelMessageHandler(),
336313
routing_msg_handler, messenger.as_OnionMessageHandler(),

src/test/java/org/ldk/HumanObjectPeerTest.java

+21-40
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,14 @@ NodeSigner manual_node_signer(KeysManager underlying_km) {
5656
NodeSigner underlying_ns = underlying_km.as_NodeSigner();
5757
must_free_objs.add(new WeakReference<>(underlying_ns));
5858
return NodeSigner.new_impl(new NodeSigner.NodeSignerInterface() {
59-
@Override public byte[] get_inbound_payment_key_material() { return underlying_ns.get_inbound_payment_key_material(); }
59+
@Override public ExpandedKey get_inbound_payment_key() { return underlying_ns.get_inbound_payment_key(); }
6060
@Override public Result_PublicKeyNoneZ get_node_id(Recipient recipient) { return underlying_ns.get_node_id(recipient); }
6161
@Override public Result_ThirtyTwoBytesNoneZ ecdh(Recipient recipient, byte[] other_key, Option_BigEndianScalarZ tweak) {
6262
return underlying_ns.ecdh(recipient, other_key, tweak);
6363
}
6464
@Override public Result_RecoverableSignatureNoneZ sign_invoice(RawBolt11Invoice invoice, Recipient recipient) {
6565
return underlying_ns.sign_invoice(invoice, recipient);
6666
}
67-
@Override public Result_SchnorrSignatureNoneZ sign_bolt12_invoice_request(UnsignedInvoiceRequest invoice_request) {
68-
return underlying_ns.sign_bolt12_invoice_request(invoice_request);
69-
}
7067
@Override public Result_SchnorrSignatureNoneZ sign_bolt12_invoice(UnsignedBolt12Invoice invoice) {
7168
return underlying_ns.sign_bolt12_invoice(invoice);
7269
}
@@ -139,6 +136,9 @@ public EcdsaChannelSigner derive_channel_signer(long channel_value_satoshis, byt
139136
@Override public Result_ECDSASignatureNoneZ sign_channel_announcement_with_funding_key(UnsignedChannelAnnouncement msg) {
140137
return underlying_ecs.sign_channel_announcement_with_funding_key(msg);
141138
}
139+
@Override public Result_ECDSASignatureNoneZ sign_splicing_funding_input(byte[] tx, long input_index, long input_value) {
140+
return underlying_ecs.sign_splicing_funding_input(tx, input_index, input_value);
141+
}
142142
};
143143
EcdsaChannelSigner resp = EcdsaChannelSigner.new_impl(ecsi, csi, underlying_cs.get_pubkeys());
144144
must_free_objs.add(new WeakReference<>(ecsi));
@@ -475,7 +475,8 @@ public Result_RouteLightningErrorZ find_route(byte[] payer, RouteParameters para
475475
ScoreLookUp.new_impl((candidate, usage, params1) -> candidate.source().as_slice()[1]),
476476
ProbabilisticScoringFeeParameters.with_default(), new byte[32]);
477477
}
478-
}, new MessageRouter.MessageRouterInterface() {
478+
});
479+
MessageRouter msg_router = MessageRouter.new_impl(new MessageRouter.MessageRouterInterface() {
479480
@Override public Result_OnionMessagePathNoneZ find_path(byte[] sender, byte[][] peers, Destination destination) {
480481
return Result_OnionMessagePathNoneZ.err();
481482
}
@@ -487,9 +488,9 @@ public Result_RouteLightningErrorZ find_route(byte[] payer, RouteParameters para
487488
}
488489
});
489490
ChainParameters params = ChainParameters.of(Network.LDKNetwork_Bitcoin, BestBlock.of(new byte[32], 0));
490-
this.chan_manager = ChannelManager.of(this.fee_estimator, chain_watch, tx_broadcaster, router, logger,
491-
this.entropy_source, this.node_signer, this.signer_provider, get_config(), params,
492-
(int)(System.currentTimeMillis() / 1000));
491+
this.chan_manager = ChannelManager.of(this.fee_estimator, chain_watch, tx_broadcaster, router,
492+
msg_router, logger, this.entropy_source, this.node_signer, this.signer_provider, get_config(),
493+
params, (int)(System.currentTimeMillis() / 1000));
493494
this.peer_manager = PeerManager.of(chan_manager.as_ChannelMessageHandler(), route_handler.as_RoutingMessageHandler(),
494495
IgnoringMessageHandler.of().as_OnionMessageHandler(), this.custom_message_handler, 0xdeadbeef,
495496
this.entropy_source.get_secure_random_bytes(), logger, this.node_signer);
@@ -596,7 +597,8 @@ public Result_RouteLightningErrorZ find_route(byte[] payer, RouteParameters para
596597
ScoreLookUp.new_impl((candidate, usage, params1) -> candidate.source().as_slice()[0]),
597598
ProbabilisticScoringFeeParameters.with_default(), new byte[32]);
598599
}
599-
}, new MessageRouter.MessageRouterInterface() {
600+
});
601+
MessageRouter msg_router = MessageRouter.new_impl(new MessageRouter.MessageRouterInterface() {
600602
@Override public Result_OnionMessagePathNoneZ find_path(byte[] sender, byte[][] peers, Destination destination) {
601603
return Result_OnionMessagePathNoneZ.err();
602604
}
@@ -622,7 +624,7 @@ public Result_CVec_BlindedMessagePathZNoneZ create_compact_blinded_paths(byte[]
622624
}
623625
byte[] serialized = orig.chan_manager.write();
624626
Result_C2Tuple_ThirtyTwoBytesChannelManagerZDecodeErrorZ read_res =
625-
UtilMethods.C2Tuple_ThirtyTwoBytesChannelManagerZ_read(serialized, this.entropy_source, this.node_signer, this.signer_provider, this.fee_estimator, this.chain_watch, this.tx_broadcaster, router, this.logger, get_config(), monitors);
627+
UtilMethods.C2Tuple_ThirtyTwoBytesChannelManagerZ_read(serialized, this.entropy_source, this.node_signer, this.signer_provider, this.fee_estimator, this.chain_watch, this.tx_broadcaster, router, msg_router, this.logger, get_config(), monitors);
626628
assert read_res instanceof Result_C2Tuple_ThirtyTwoBytesChannelManagerZDecodeErrorZ.Result_C2Tuple_ThirtyTwoBytesChannelManagerZDecodeErrorZ_OK;
627629
this.chan_manager = ((Result_C2Tuple_ThirtyTwoBytesChannelManagerZDecodeErrorZ.Result_C2Tuple_ThirtyTwoBytesChannelManagerZDecodeErrorZ_OK) read_res).res.get_b();
628630
this.chain_watch.watch_channel(monitors[0].get_funding_txo().get_a(), monitors[0]);
@@ -719,7 +721,7 @@ Event[] get_monitor_events(int expected_len) {
719721
res.add(event);
720722
return Result_NoneReplayEventZ.ok();
721723
});
722-
mon.process_pending_events(handler);
724+
mon.process_pending_events(handler, logger);
723725
assert res.size() == expected_len;
724726
return res.toArray(new Event[0]);
725727
}
@@ -988,7 +990,7 @@ TestState do_test_message_handler() throws InterruptedException {
988990
} catch (Exception e) { assert false; }
989991
invoice_description = new String(string_bytes);
990992

991-
Result_Bolt11InvoiceSignOrCreationErrorZ invoice = UtilMethods.create_invoice_from_channelmanager(peer2.chan_manager, peer2.node_signer, peer2.logger, Currency.LDKCurrency_Bitcoin, Option_u64Z.some(10000000), invoice_description, 7200, Option_u16Z.none());
993+
Result_Bolt11InvoiceSignOrCreationErrorZ invoice = UtilMethods.create_invoice_from_channelmanager(peer2.chan_manager, Option_u64Z.some(10000000), invoice_description, 7200, Option_u16Z.none());
992994
assert invoice instanceof Result_Bolt11InvoiceSignOrCreationErrorZ.Result_Bolt11InvoiceSignOrCreationErrorZ_OK;
993995
System.out.println("Got invoice: " + ((Result_Bolt11InvoiceSignOrCreationErrorZ.Result_Bolt11InvoiceSignOrCreationErrorZ_OK) invoice).res.to_str());
994996
mid_test_must_free_objs.add(new WeakReference<>(invoice));
@@ -1041,40 +1043,19 @@ TestState do_test_message_handler() throws InterruptedException {
10411043

10421044
Payee payee = Payee.clear(peer2.node_id, route_hints, invoice_features, 42);
10431045
PaymentParameters pay_params = PaymentParameters.of(payee, Option_u64Z.none(), 6*24*14, (byte)5, (byte)10, (byte)2, new long[0], new long[0]);
1044-
RouteParameters route_params = RouteParameters.from_payment_params_and_value(pay_params, 10000000);
1045-
Result_RouteLightningErrorZ route_res = UtilMethods.find_route(
1046-
peer1.chan_manager.get_our_node_id(), route_params, peer1.net_graph,
1047-
peer1_chans, peer1.logger,
1048-
ProbabilisticScorer.of(ProbabilisticScoringDecayParameters.with_default(), peer1.net_graph, peer1.logger).as_ScoreLookUp(),
1049-
ProbabilisticScoringFeeParameters.with_default(), new byte[32]);
1050-
assert route_res instanceof Result_RouteLightningErrorZ.Result_RouteLightningErrorZ_OK;
1051-
Route route = ((Result_RouteLightningErrorZ.Result_RouteLightningErrorZ_OK) route_res).res;
1046+
RouteParameters route_params = RouteParameters.from_payment_params_and_value(pay_params, 10_000_000);
10521047
mid_test_must_free_objs.add(new WeakReference<>(payee));
10531048
mid_test_must_free_objs.add(new WeakReference<>(pay_params));
10541049
mid_test_must_free_objs.add(new WeakReference<>(route_params));
1055-
mid_test_must_free_objs.add(new WeakReference<>(route));
1056-
assert route.get_paths().length == 1;
1057-
assert route.get_paths()[0].get_hops().length == 1;
1058-
assert route.get_paths()[0].final_value_msat() == 10000000;
10591050

10601051
byte[] payment_id = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31 };
1061-
Result_NonePaymentSendFailureZ payment_res = peer1.chan_manager.send_payment_with_route(route, payment_hash, RecipientOnionFields.secret_only(payment_secret), payment_id);
1062-
assert payment_res instanceof Result_NonePaymentSendFailureZ.Result_NonePaymentSendFailureZ_OK;
1063-
1064-
RouteHop[] hops = new RouteHop[1];
1065-
byte[] hop_pubkey = new byte[33];
1066-
hop_pubkey[0] = 3;
1067-
hop_pubkey[1] = 42;
1068-
NodeFeatures node_features = NodeFeatures.empty();
1069-
ChannelFeatures channel_features = ChannelFeatures.empty();
1070-
hops[0] = RouteHop.of(hop_pubkey, node_features, 42, channel_features, 100, 0, false);
1071-
Path[] paths = new Path[1];
1072-
paths[0] = Path.of(hops, null);
1073-
Route r2 = Route.of(paths, RouteParameters.from_payment_params_and_value(pay_params, 100));
1074-
mid_test_must_free_objs.add(new WeakReference<>(r2));
1075-
payment_res = peer1.chan_manager.send_payment_with_route(r2, payment_hash, RecipientOnionFields.secret_only(payment_secret), payment_id);
1052+
Result_NoneRetryableSendFailureZ payment_res = peer1.chan_manager.send_payment(payment_hash, RecipientOnionFields.secret_only(payment_secret), payment_id, route_params, Retry.attempts(0));
1053+
assert payment_res instanceof Result_NoneRetryableSendFailureZ.Result_NoneRetryableSendFailureZ_OK;
1054+
1055+
route_params.set_final_value_msat(1_000_000_000);
1056+
payment_res = peer1.chan_manager.send_payment(payment_hash, RecipientOnionFields.secret_only(payment_secret), payment_id, route_params, Retry.attempts(0));
10761057
mid_test_must_free_objs.add(new WeakReference<>(payment_res));
1077-
assert payment_res instanceof Result_NonePaymentSendFailureZ.Result_NonePaymentSendFailureZ_Err;
1058+
assert payment_res instanceof Result_NoneRetryableSendFailureZ.Result_NoneRetryableSendFailureZ_Err;
10781059
} else {
10791060
Result_C3Tuple_ThirtyTwoBytesRecipientOnionFieldsRouteParametersZNoneZ pay_params_res =
10801061
UtilMethods.payment_parameters_from_invoice(((Result_Bolt11InvoiceParseOrSemanticErrorZ.Result_Bolt11InvoiceParseOrSemanticErrorZ_OK) parsed_invoice).res);

0 commit comments

Comments
 (0)