import org.junit.jupiter.api.Test;
import org.ldk.batteries.ChannelManagerConstructor;
import org.ldk.batteries.NioPeerHandler;
-import org.ldk.enums.LDKCurrency;
-import org.ldk.enums.LDKNetwork;
+import org.ldk.enums.Currency;
+import org.ldk.enums.Network;
import org.ldk.impl.bindings;
import org.ldk.structs.*;
import org.ldk.util.TwoTuple;
}
private void bind_nio() {
if (!use_nio_peer_handler) return;
- try { this.nio_peer_handler = new NioPeerHandler(peer_manager); } catch (IOException e) { assert false; }
+ if (use_chan_manager_constructor) {
+ this.nio_peer_handler = this.constructor.nio_peer_handler;
+ } else {
+ try { this.nio_peer_handler = new NioPeerHandler(peer_manager); } catch (IOException e) { assert false; }
+ }
for (short i = 10_000; true; i++) {
try {
nio_peer_handler.bind_listener(new InetSocketAddress("127.0.0.1", i));
Peer(byte seed) {
this(null, seed);
if (use_chan_manager_constructor) {
- try {
- this.constructor = new ChannelManagerConstructor(LDKNetwork.LDKNetwork_Bitcoin, UserConfig.with_default(), new byte[32], 0,
- this.keys_interface, this.fee_estimator, this.chain_monitor, this.tx_broadcaster, this.logger);
- constructor.chain_sync_completed(new ChannelManagerConstructor.ChannelManagerPersister() {
- @Override public void handle_events(Event[] events) {
- synchronized (pending_manager_events) {
- pending_manager_events.addAll(Arrays.asList(events));
- pending_manager_events.notifyAll();
- }
+ this.constructor = new ChannelManagerConstructor(Network.LDKNetwork_Bitcoin, UserConfig.with_default(), new byte[32], 0,
+ this.keys_interface, this.fee_estimator, this.chain_monitor, this.router, this.tx_broadcaster, this.logger);
+ constructor.chain_sync_completed(new ChannelManagerConstructor.ChannelManagerPersister() {
+ @Override public void handle_event(Event event) {
+ synchronized (pending_manager_events) {
+ pending_manager_events.add(event);
+ pending_manager_events.notifyAll();
}
- @Override public void persist_manager(byte[] channel_manager_bytes) { }
- });
- this.chan_manager = constructor.channel_manager;
- must_free_objs.add(new WeakReference<>(this.chan_manager));
- } catch (ChannelManagerConstructor.InvalidSerializedDataException e) {
- assert false;
- }
+ }
+ @Override public void persist_manager(byte[] channel_manager_bytes) { }
+ });
+ this.chan_manager = constructor.channel_manager;
+ this.peer_manager = constructor.peer_manager;
+ must_free_objs.add(new WeakReference<>(this.chan_manager));
} else {
- this.chan_manager = ChannelManager.of(this.fee_estimator, chain_watch, tx_broadcaster, logger, this.keys_interface, UserConfig.with_default(), LDKNetwork.LDKNetwork_Bitcoin, BestBlock.of(new byte[32], 0));
+ ChainParameters params = ChainParameters.of(Network.LDKNetwork_Bitcoin, BestBlock.of(new byte[32], 0));
+ this.chan_manager = ChannelManager.of(this.fee_estimator, chain_watch, tx_broadcaster, logger, this.keys_interface, UserConfig.with_default(), params);
+ byte[] random_data = keys_interface.get_secure_random_bytes();
+ this.peer_manager = PeerManager.of(chan_manager.as_ChannelMessageHandler(), router.as_RoutingMessageHandler(), keys_interface.get_node_secret(), random_data, logger);
}
this.node_id = chan_manager.get_our_node_id();
-
- byte[] random_data = new byte[32];
- for (byte i = 0; i < 32; i++) {
- random_data[i] = (byte) ((i ^ seed) ^ 0xf0);
- }
- this.peer_manager = PeerManager.of(chan_manager.as_ChannelMessageHandler(), router.as_RoutingMessageHandler(), keys_interface.get_node_secret(), random_data, logger);
bind_nio();
System.gc();
}
- byte[] hexStringToByteArray(String s) {
- int len = s.length();
- byte[] data = new byte[len / 2];
- for (int i = 0; i < len; i += 2) {
- data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
- + Character.digit(s.charAt(i+1), 16));
- }
- return data;
- }
-
-
Object ptr_to;
Peer(Peer orig) {
this(null, orig.seed);
byte[] serialized = orig.chan_manager.write();
try {
this.constructor = new ChannelManagerConstructor(serialized, monitors, this.keys_interface,
- this.fee_estimator, this.chain_monitor, this.filter, this.tx_broadcaster, this.logger);
+ this.fee_estimator, this.chain_monitor, this.filter, this.router, this.tx_broadcaster, this.logger);
constructor.chain_sync_completed(new ChannelManagerConstructor.ChannelManagerPersister() {
- @Override public void handle_events(Event[] events) {
+ @Override public void handle_event(Event event) {
synchronized (pending_manager_events) {
- pending_manager_events.addAll(Arrays.asList(events));
+ pending_manager_events.add(event);
pending_manager_events.notifyAll();
}
}
@Override public void persist_manager(byte[] channel_manager_bytes) { }
});
this.chan_manager = constructor.channel_manager;
+ this.peer_manager = constructor.peer_manager;
must_free_objs.add(new WeakReference<>(this.chan_manager));
// If we are using a ChannelManagerConstructor, we may have pending events waiting on the old peer
// which have been removed from the ChannelManager but which we still need to handle.
assert read_res instanceof Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ.Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ_OK;
this.chan_manager = ((Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ.Result_C2Tuple_BlockHashChannelManagerZDecodeErrorZ_OK) read_res).res.b;
this.chain_watch.watch_channel(monitors[0].get_funding_txo().a, monitors[0]);
+ byte[] random_data = keys_interface.get_secure_random_bytes();
+ this.peer_manager = PeerManager.of(chan_manager.as_ChannelMessageHandler(), router.as_RoutingMessageHandler(), keys_interface.get_node_secret(), random_data, logger);
if (!break_cross_peer_refs && (use_manual_watch || use_km_wrapper)) {
// When we pass monitors[0] into chain_watch.watch_channel we create a reference from the new Peer to a
// field in the old peer, preventing freeing of the original Peer until the new Peer is freed. Thus, we
}
}
this.node_id = chan_manager.get_our_node_id();
+ bind_nio();
if (cross_reload_ref_pollution) {
// This really, really needs to be handled at the bindings layer, but its rather complicated -
// the ChannelSigner.
this.ptr_to = orig.chan_manager;
}
-
- byte[] random_data = new byte[32];
- for (byte i = 0; i < 32; i++) {
- random_data[i] = (byte) ((i ^ seed) ^ 0xf0);
- }
- this.peer_manager = PeerManager.of(chan_manager.as_ChannelMessageHandler(), router.as_RoutingMessageHandler(), keys_interface.get_node_secret(), random_data, logger);
- bind_nio();
}
TwoTuple<byte[], TwoTuple<Integer, TxOut>[]>[] connect_block(Block b, int height, long expected_monitor_update_len) {
return res;
}
} else if (chain_monitor != null) {
- return chain_monitor.as_EventsProvider().get_and_clear_pending_events();
+ ArrayList<Event> l = new ArrayList<Event>();
+ chain_monitor.as_EventsProvider().process_pending_events(EventHandler.new_impl(l::add));
+ assert l.size() == expected_len;
+ return l.toArray(new Event[0]);
} else {
synchronized (monitors) {
assert monitors.size() == 1;
}
}
} else {
- res = this.chan_manager.as_EventsProvider().get_and_clear_pending_events();
+ ArrayList<Event> l = new ArrayList<Event>();
+ chan_manager.as_EventsProvider().process_pending_events(EventHandler.new_impl(l::add));
+ return l.toArray(new Event[0]);
}
assert res.length == expected_len;
return res;
}
-
- Route get_route(byte[] dest_node, ChannelDetails[] our_chans) {
- try (LockedNetworkGraph netgraph = this.router.read_locked_graph()) {
- NetworkGraph graph = netgraph.graph();
- long res = bindings.get_route(this.node_id, graph._test_only_get_ptr(), dest_node, 0L, new long[]{our_chans[0]._test_only_get_ptr()},
- new long[0], 1000000, 42, this.logger._test_only_get_ptr());
- assert bindings.LDKCResult_RouteLightningErrorZ_result_ok(res);
- byte[] serialized_route = bindings.Route_write(bindings.LDKCResult_RouteLightningErrorZ_get_ok(res));
- must_free_objs.add(new WeakReference<>(serialized_route));
- Result_RouteDecodeErrorZ copy = Route.read(serialized_route);
- assert copy instanceof Result_RouteDecodeErrorZ.Result_RouteDecodeErrorZ_OK;
- bindings.CResult_RouteLightningErrorZ_free(res);
- return ((Result_RouteDecodeErrorZ.Result_RouteDecodeErrorZ_OK) copy).res;
- }
- }
}
static class DescriptorHolder { SocketDescriptor val; }
if (use_nio_peer_handler) {
peer1.nio_peer_handler.check_events();
peer2.nio_peer_handler.check_events();
- try { Thread.sleep(400); } catch (InterruptedException e) { assert false; }
+ try { Thread.sleep(500); } catch (InterruptedException e) { assert false; }
} else {
synchronized (runqueue) {
ran = false;
assert Arrays.equals(peer1_chans[0].get_channel_id(), funding.getTxId().getReversedBytes());
assert Arrays.equals(peer2_chans[0].get_channel_id(), funding.getTxId().getReversedBytes());
- Result_InvoiceSignOrCreationErrorZ invoice = UtilMethods.invoice_from_channelmanager(peer2.chan_manager, peer2.keys_interface, LDKCurrency.LDKCurrency_Bitcoin, Option_u64Z.none(), "Invoice Description");
+ Result_InvoiceSignOrCreationErrorZ invoice = UtilMethods.create_invoice_from_channelmanager(peer2.chan_manager, peer2.keys_interface, Currency.LDKCurrency_Bitcoin, Option_u64Z.none(), "Invoice Description");
assert invoice instanceof Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK;
System.out.println("Got invoice: " + ((Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK) invoice).res.to_str());
+
Result_InvoiceNoneZ parsed_invoice = Invoice.from_str(((Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK) invoice).res.to_str());
assert parsed_invoice instanceof Result_InvoiceNoneZ.Result_InvoiceNoneZ_OK;
assert Arrays.equals(((Result_InvoiceNoneZ.Result_InvoiceNoneZ_OK) parsed_invoice).res.payment_hash(), ((Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK) invoice).res.payment_hash());
assert description_string.equals("Invoice Description");
byte[] payment_hash = ((Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK) invoice).res.payment_hash();
byte[] payment_secret = ((Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK) invoice).res.payment_secret();
+ byte[] dest_node_id = ((Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK) invoice).res.recover_payee_pub_key();
+ assert Arrays.equals(dest_node_id, peer2.node_id);
+ InvoiceFeatures invoice_features = ((Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK) invoice).res.features();
+ RouteHint[] route_hints = ((Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK) invoice).res.route_hints();
+
+ Route route;
+ try (LockedNetworkGraph netgraph = peer1.router.read_locked_graph()) {
+ NetworkGraph graph = netgraph.graph();
+ Result_RouteLightningErrorZ route_res = UtilMethods.get_route(peer1.chan_manager.get_our_node_id(), graph, peer2.node_id, invoice_features, peer1_chans, route_hints, 1000000, 42, peer1.logger);
+ assert route_res instanceof Result_RouteLightningErrorZ.Result_RouteLightningErrorZ_OK;
+ route = ((Result_RouteLightningErrorZ.Result_RouteLightningErrorZ_OK) route_res).res;
+ }
- Route route = peer1.get_route(peer2.node_id, peer1_chans);
Result_NonePaymentSendFailureZ payment_res = peer1.chan_manager.send_payment(route, payment_hash, payment_secret);
assert payment_res instanceof Result_NonePaymentSendFailureZ.Result_NonePaymentSendFailureZ_OK;
wait_events_processed(peer1, peer2);
state.peer1.constructor.interrupt();
state.peer2.constructor.interrupt();
}
+
+ t.interrupt();
}
java.util.LinkedList<WeakReference<Object>> must_free_objs = new java.util.LinkedList();
// There are no cross refs to break without reloading peers.
continue;
}
- if (use_chan_manager_constructor && use_manual_watch) {
- // ChannelManagerConstructor requires a ChainMonitor as the Watch
+ if (use_chan_manager_constructor && (use_manual_watch || !nio_peer_handler)) {
+ // ChannelManagerConstructor requires a ChainMonitor as the Watch and creates a NioPeerHandler for us.
continue;
}
System.err.println("Running test with flags " + i);