* given explicitly to the new-object constructor.
*/
@Nullable public final NetworkGraph net_graph;
- @Nullable private final NetGraphMsgHandler graph_msg_handler;
+ @Nullable private final P2PGossipSync graph_msg_handler;
private final Logger logger;
private final byte[] router_rand_bytes;
this.logger = logger;
byte[] random_data = keys_interface.get_secure_random_bytes();
if (net_graph_serialized != null) {
- Result_NetworkGraphDecodeErrorZ graph_res = NetworkGraph.read(net_graph_serialized);
+ Result_NetworkGraphDecodeErrorZ graph_res = NetworkGraph.read(net_graph_serialized, logger);
if (!graph_res.is_ok()) {
throw new InvalidSerializedDataException("Serialized Network Graph was corrupt");
}
assert node_secret.is_ok();
if (net_graph != null) {
//TODO: We really need to expose the Access here to let users prevent DoS issues
- this.graph_msg_handler = NetGraphMsgHandler.of(net_graph, Option_AccessZ.none(), logger);
+ this.graph_msg_handler = P2PGossipSync.of(net_graph, Option_AccessZ.none(), logger);
this.peer_manager = PeerManager.of(channel_manager.as_ChannelMessageHandler(),
graph_msg_handler.as_RoutingMessageHandler(),
((Result_SecretKeyNoneZ.Result_SecretKeyNoneZ_OK)node_secret).res,
assert node_secret.is_ok();
if (net_graph != null) {
//TODO: We really need to expose the Access here to let users prevent DoS issues
- this.graph_msg_handler = NetGraphMsgHandler.of(net_graph, Option_AccessZ.none(), logger);
+ this.graph_msg_handler = P2PGossipSync.of(net_graph, Option_AccessZ.none(), logger);
this.peer_manager = PeerManager.of(channel_manager.as_ChannelMessageHandler(),
graph_msg_handler.as_RoutingMessageHandler(),
((Result_SecretKeyNoneZ.Result_SecretKeyNoneZ_OK)node_secret).res,
void handle_event(Event events);
void persist_manager(byte[] channel_manager_bytes);
void persist_network_graph(byte[] network_graph);
+ void persist_scorer(byte[] scorer_bytes);
}
BackgroundProcessor background_processor = null;
org.ldk.structs.EventHandler ldk_handler = org.ldk.structs.EventHandler.new_impl(event_handler::handle_event);
if (this.net_graph != null && scorer != null) {
Router router = DefaultRouter.of(net_graph, logger, router_rand_bytes).as_Router();
- this.payer = InvoicePayer.of(this.channel_manager.as_Payer(), router, scorer, this.logger, ldk_handler, RetryAttempts.of(3));
+ this.payer = InvoicePayer.of(this.channel_manager.as_Payer(), router, scorer, this.logger, ldk_handler, Retry.attempts(3));
assert this.payer != null;
ldk_handler = this.payer.as_EventHandler();
}
+ GossipSync gossip_sync;
+ if (this.graph_msg_handler == null)
+ gossip_sync = GossipSync.none();
+ else
+ gossip_sync = GossipSync.p2_p(this.graph_msg_handler);
+
background_processor = BackgroundProcessor.start(Persister.new_impl(new Persister.PersisterInterface() {
@Override
public Result_NoneErrorZ persist_manager(ChannelManager channel_manager) {
event_handler.persist_network_graph(network_graph.write());
return Result_NoneErrorZ.ok();
}
- }), ldk_handler, this.chain_monitor, this.channel_manager, this.graph_msg_handler, this.peer_manager, this.logger);
+
+ @Override
+ public Result_NoneErrorZ persist_scorer(MultiThreadedLockableScore scorer) {
+ event_handler.persist_scorer(scorer.write());
+ return Result_NoneErrorZ.ok();
+ }
+ }), ldk_handler, this.chain_monitor, this.channel_manager, gossip_sync, this.peer_manager, this.logger, scorer);
}
/**
}
@Override
- public MonitorEvent[] release_pending_monitor_events() {
+ public TwoTuple_OutPointCVec_MonitorEventZZ[] release_pending_monitor_events() {
synchronized (monitors) {
assert monitors.size() <= 1;
for (ChannelMonitor mon : monitors.values()) {
- return mon.get_and_clear_pending_monitor_events();
+ TwoTuple_OutPointCVec_MonitorEventZZ[] res = new TwoTuple_OutPointCVec_MonitorEventZZ[1];
+ res[0] = TwoTuple_OutPointCVec_MonitorEventZZ.of(mon.get_funding_txo().get_a(), mon.get_and_clear_pending_monitor_events());
+ return res;
}
}
- return new MonitorEvent[0];
+ return new TwoTuple_OutPointCVec_MonitorEventZZ[0];
}
};
Watch watch = Watch.new_impl(watch_impl);
final KeysInterface keys_interface;
final ChainMonitor chain_monitor;
NetworkGraph router;
- NetGraphMsgHandler route_handler;
+ P2PGossipSync route_handler;
final Watch chain_watch;
final HashSet<String> filter_additions;
Option_FilterZ filter;
}
}
private void setup_route_handler() {
- this.route_handler = NetGraphMsgHandler.of(this.router, Option_AccessZ.some(Access.new_impl(new Access.AccessInterface() {
+ this.route_handler = P2PGossipSync.of(this.router, Option_AccessZ.some(Access.new_impl(new Access.AccessInterface() {
@Override
public Result_TxOutAccessErrorZ get_utxo(byte[] genesis_hash, long short_channel_id) {
// We don't exchange any gossip, so should never actually get called, but providing a Some(Access)
}
Peer(byte seed) {
this(null, seed);
- this.router = NetworkGraph.of(new byte[32]);
+ this.router = NetworkGraph.of(new byte[32], this.logger);
this.setup_route_handler();
if (use_chan_manager_constructor) {
this.keys_interface, this.fee_estimator, this.chain_monitor, this.router, this.tx_broadcaster, this.logger);
}
ProbabilisticScoringParameters params = ProbabilisticScoringParameters.with_default();
- ProbabilisticScorer default_scorer = ProbabilisticScorer.of(params, this.router);
- Result_ProbabilisticScorerDecodeErrorZ score_res = ProbabilisticScorer.read(default_scorer.write(), params, this.router);
+ ProbabilisticScorer default_scorer = ProbabilisticScorer.of(params, this.router, this.logger);
+ Result_ProbabilisticScorerDecodeErrorZ score_res = ProbabilisticScorer.read(default_scorer.write(), params, this.router, this.logger);
assert score_res.is_ok();
Score score = ((Result_ProbabilisticScorerDecodeErrorZ.Result_ProbabilisticScorerDecodeErrorZ_OK) score_res).res.as_Score();
MultiThreadedLockableScore scorer = null;
}
@Override public void persist_manager(byte[] channel_manager_bytes) { assert channel_manager_bytes.length > 1; }
@Override public void persist_network_graph(byte[] graph_bytes) { assert graph_bytes.length > 1; }
+ @Override public void persist_scorer(byte[] scorer_bytes) { assert scorer_bytes.length > 1; }
}, scorer);
this.chan_manager = constructor.channel_manager;
this.peer_manager = constructor.peer_manager;
}
}), MultiThreadedLockableScore.of(Score.new_impl(new Score.ScoreInterface() {
@Override public void payment_path_failed(RouteHop[] path, long scid) {}
- @Override public long channel_penalty_msat(long short_channel_id, long send_amt_msat, long channel_capacity_msat, NodeId source, NodeId target) { return 0; }
+ @Override public long channel_penalty_msat(long short_channel_id, NodeId source, NodeId target, ChannelUsage usage) { return 0; }
@Override public void payment_path_successful(RouteHop[] path) {}
@Override public byte[] write() { assert false; return null; }
})), logger, EventHandler.new_impl(new EventHandler.EventHandlerInterface() {
pending_manager_events.notifyAll();
}
}
- }), RetryAttempts.of(0));
+ }), Retry.attempts(0));
}
}
this.router = this.constructor.net_graph;
setup_route_handler();
MultiThreadedLockableScore scorer = null;
- if (use_invoice_payer) { scorer = MultiThreadedLockableScore.of(ProbabilisticScorer.of(ProbabilisticScoringParameters.with_default(), this.router).as_Score()); }
+ if (use_invoice_payer) { scorer = MultiThreadedLockableScore.of(ProbabilisticScorer.of(ProbabilisticScoringParameters.with_default(), this.router, this.logger).as_Score()); }
constructor.chain_sync_completed(new ChannelManagerConstructor.EventHandler() {
@Override public void handle_event(Event event) {
synchronized (pending_manager_events) {
}
@Override public void persist_manager(byte[] channel_manager_bytes) { assert channel_manager_bytes.length > 1; }
@Override public void persist_network_graph(byte[] graph_bytes) { assert graph_bytes.length > 1; }
+ @Override public void persist_scorer(byte[] scorer_bytes) { assert scorer_bytes.length > 1; }
}, scorer);
this.chan_manager = constructor.channel_manager;
this.payer = constructor.payer;
assert false;
}
} else {
- this.router = NetworkGraph.of(new byte[32]);
+ this.router = NetworkGraph.of(new byte[32], this.logger);
this.setup_route_handler();
ChannelMonitor[] monitors = new ChannelMonitor[1];
assert orig.monitors.size() == 1;
return UtilMethods.find_route(payer, params, router, first_hops, logger, scorer, new byte[32]);
}
}), MultiThreadedLockableScore.of(Score.new_impl(new Score.ScoreInterface() {
- @Override public long channel_penalty_msat(long short_channel_id, long send_amt_msat, long channel_capacity_msat, NodeId source, NodeId target) { return 0; }
+ @Override public long channel_penalty_msat(long short_channel_id, NodeId source, NodeId target, ChannelUsage usage) { return 0; }
@Override public void payment_path_failed(RouteHop[] path, long scid) {}
@Override public void payment_path_successful(RouteHop[] path) {}
@Override public byte[] write() { assert false; return null; }
pending_manager_events.notifyAll();
}
}
- }), RetryAttempts.of(0));
+ }), Retry.attempts(0));
}
}
this.node_id = chan_manager.get_our_node_id();
funding.getInputs().get(0).setWitness(new TransactionWitness(2)); // Make sure we don't complain about lack of witness
funding.getInput(0).getWitness().setPush(0, new byte[]{0x1});
funding.addOutput(Coin.SATOSHI.multiply(100000), new Script(funding_spk));
- Result_NoneAPIErrorZ funding_res = peer1.chan_manager.funding_transaction_generated(chan_id, funding.bitcoinSerialize());
+ Result_NoneAPIErrorZ funding_res = peer1.chan_manager.funding_transaction_generated(chan_id, peer2.node_id, funding.bitcoinSerialize());
assert funding_res instanceof Result_NoneAPIErrorZ.Result_NoneAPIErrorZ_OK;
maybe_exchange_peer_messages(peer1, peer2);
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.create_invoice_from_channelmanager(peer2.chan_manager, peer2.keys_interface, Currency.LDKCurrency_Bitcoin, Option_u64Z.some(10000000), "Invoice Description");
+ Result_InvoiceSignOrCreationErrorZ invoice = UtilMethods.create_invoice_from_channelmanager(peer2.chan_manager, peer2.keys_interface, Currency.LDKCurrency_Bitcoin, Option_u64Z.some(10000000), "Invoice Description", 7200);
assert invoice instanceof Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK;
System.out.println("Got invoice: " + ((Result_InvoiceSignOrCreationErrorZ.Result_InvoiceSignOrCreationErrorZ_OK) invoice).res.to_str());
RouteParameters route_params = RouteParameters.of(payee, 10000000, 42);
Result_RouteLightningErrorZ route_res = UtilMethods.find_route(
peer1.chan_manager.get_our_node_id(), route_params, peer1.router,
- peer1_chans, peer1.logger, Scorer.with_default().as_Score(), new byte[32]);
+ peer1_chans, peer1.logger, ProbabilisticScorer.of(ProbabilisticScoringParameters.with_default(), peer1.router, peer1.logger).as_Score(), new byte[32]);
assert route_res instanceof Result_RouteLightningErrorZ.Result_RouteLightningErrorZ_OK;
Route route = ((Result_RouteLightningErrorZ.Result_RouteLightningErrorZ_OK) route_res).res;
assert route.get_paths().length == 1;
assert !Arrays.equals(payment_preimage, new byte[32]);
state.peer2.chan_manager.claim_funds(payment_preimage);
+ events = state.peer2.get_manager_events(1, state.peer1, state.peer2);
+ assert events[0] instanceof Event.PaymentClaimed;
+ assert ((Event.PaymentClaimed)events[0]).purpose instanceof PaymentPurpose.InvoicePayment;
+ payment_preimage = ((PaymentPurpose.InvoicePayment)((Event.PaymentClaimed)events[0]).purpose).payment_preimage;
+ assert !Arrays.equals(payment_preimage, new byte[32]);
+
events = state.peer1.get_manager_events(2, state.peer1, state.peer2);
assert events[0] instanceof Event.PaymentSent;
assert Arrays.equals(((Event.PaymentSent) events[0]).payment_preimage, payment_preimage);
ChannelDetails[] peer1_chans = state.peer1.chan_manager.list_channels();
if (nice_close) {
- Result_NoneAPIErrorZ close_res = state.peer1.chan_manager.close_channel(peer1_chans[0].get_channel_id());
+ Result_NoneAPIErrorZ close_res = state.peer1.chan_manager.close_channel(peer1_chans[0].get_channel_id(), state.peer2.node_id);
assert close_res instanceof Result_NoneAPIErrorZ.Result_NoneAPIErrorZ_OK;
maybe_exchange_peer_messages(state.peer1, state.peer2);
synchronized (state.peer1.broadcast_set) {
synchronized (state.peer2.broadcast_set) {
while (state.peer2.broadcast_set.size() != 1) state.peer2.broadcast_set.wait();
}
-
- assert state.peer1.broadcast_set.size() == 1;
- assert state.peer2.broadcast_set.size() == 1;
} else {
state.peer1.chan_manager.force_close_all_channels();
maybe_exchange_peer_messages(state.peer1, state.peer2);
synchronized (state.peer2.broadcast_set) {
while (state.peer2.broadcast_set.size() != 1) state.peer2.broadcast_set.wait();
}
-
- assert state.peer1.broadcast_set.size() == 1;
- assert state.peer2.broadcast_set.size() == 1;
}
+ assert state.peer1.broadcast_set.size() == 1;
+ assert state.peer2.broadcast_set.size() == 1;
+
events = state.peer2.get_manager_events(1, state.peer1, state.peer2);
assert events[0] instanceof Event.ChannelClosed;
events = state.peer1.get_manager_events(1, state.peer1, state.peer2);
}
@Override
- public void handle_funding_locked(byte[] their_node_id, long msg) {
+ public void handle_channel_ready(byte[] their_node_id, long msg) {
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
public class PeerTest {
synchronized (monitors) {
assert monitors.size() <= 1;
for (Long mon : monitors.values()) {
- return bindings.ChannelMonitor_get_and_clear_pending_monitor_events(mon);
+ long funding_info = bindings.ChannelMonitor_get_funding_txo(mon);
+ long funding_txo = bindings.C2Tuple_OutPointScriptZ_get_a(funding_info);
+ long[] mon_events = bindings.ChannelMonitor_get_and_clear_pending_monitor_events(mon);
+ long funding_mon_tuple = bindings.C2Tuple_OutPointCVec_MonitorEventZZ_new(funding_txo, mon_events);
+ bindings.C2Tuple_OutPointScriptZ_free(funding_info);
+ return new long[] {funding_mon_tuple};
}
}
return new long[0];
this.chan_manager_events = bindings.ChannelManager_as_EventsProvider(chan_manager);
this.chan_handler = bindings.ChannelManager_as_ChannelMessageHandler(chan_manager);
- this.router = bindings.NetworkGraph_new(new byte[32]);
- this.router_wrapper = bindings.NetGraphMsgHandler_new(router, bindings.COption_AccessZ_none(), logger);
- this.route_handler = bindings.NetGraphMsgHandler_as_RoutingMessageHandler(router_wrapper);
+ this.router = bindings.NetworkGraph_new(new byte[32], logger);
+ this.router_wrapper = bindings.P2PGossipSync_new(router, bindings.COption_AccessZ_none(), logger);
+ this.route_handler = bindings.P2PGossipSync_as_RoutingMessageHandler(router_wrapper);
this.message_handler = bindings.MessageHandler_new(chan_handler, route_handler);
this.custom_message_handler = bindings.IgnoringMessageHandler_new();
bindings.EventsProvider_free(chan_manager_events);
bindings.ChannelMessageHandler_free(chan_handler);
bindings.NetworkGraph_free(router);
- bindings.NetGraphMsgHandler_free(router_wrapper);
+ bindings.P2PGossipSync_free(router_wrapper);
bindings.RoutingMessageHandler_free(route_handler);
//MessageHandler was actually moved into the route_handler!: bindings.MessageHandler_free(message_handler);
bindings.PeerManager_free(peer_manager);
}
class LongHolder { long val; }
+ class PendingWrite {
+ long pm;
+ long descriptor;
+ byte[] array;
+ PendingWrite(long pm, long descriptor, byte[] array) { this.pm = pm; this.descriptor = descriptor; this.array = array; }
+ void process() {
+ long res = bindings.PeerManager_read_event(pm, descriptor, array);
+ assert bindings.CResult_boolPeerHandleErrorZ_is_ok(res);
+ assert !bindings.CResult_boolPeerHandleErrorZ_get_ok(res);
+ bindings.CResult_boolPeerHandleErrorZ_free(res);
+ }
+ }
+ ConcurrentLinkedQueue<PendingWrite> pending_writes = new ConcurrentLinkedQueue<>();
void do_read_event(ConcurrentLinkedQueue<Thread> list, long pm, long descriptor, byte[] arr) {
+ pending_writes.add(new PendingWrite(pm, descriptor, arr));
Thread thread = new Thread(() -> {
- long res = bindings.PeerManager_read_event(pm, descriptor, arr);
- assert bindings.CResult_boolPeerHandleErrorZ_is_ok(res);
- //assert bindings.deref_bool(bindings.LDKCResult_boolPeerHandleErrorZ_get_inner(res));
- bindings.CResult_boolPeerHandleErrorZ_free(res);
+ synchronized (pending_writes) {
+ while (true) {
+ PendingWrite write = pending_writes.poll();
+ if (write == null) break;
+ write.process();
+ }
+ }
});
thread.start();
list.add(thread);
funding.getInputs().get(0).setWitness(new TransactionWitness(2)); // Make sure we don't complain about lack of witness
funding.getInput(0).getWitness().setPush(0, new byte[] {0x1});
funding.addOutput(Coin.SATOSHI.multiply(10000), new Script(funding_spk));
- bindings.ChannelManager_funding_transaction_generated(peer1.chan_manager, chan_id, funding.bitcoinSerialize());
+ bindings.ChannelManager_funding_transaction_generated(peer1.chan_manager, chan_id, peer2.node_id, funding.bitcoinSerialize());
deliver_peer_messages(list, peer1.peer_manager, peer2.peer_manager);
assert bindings.CResult_C2Tuple_PaymentHashPaymentSecretZNoneZ_is_ok(inbound_payment);
long payment_tuple = bindings.CResult_C2Tuple_PaymentHashPaymentSecretZNoneZ_get_ok(inbound_payment);
bindings.COption_u64Z_free(no_min_val);
- long scorer = bindings.Scorer_default();
- long scorer_interface = bindings.Scorer_as_Score(scorer);
+
+ long scorer = bindings.ProbabilisticScorer_new(bindings.ProbabilisticScoringParameters_default(), peer1.router, peer1.logger);
+ long scorer_interface = bindings.ProbabilisticScorer_as_Score(scorer);
long no_u64 = bindings.COption_u64Z_none();
long invoice_features = bindings.InvoiceFeatures_known();
bindings.InvoiceFeatures_free(invoice_features);
bindings.COption_u64Z_free(no_u64);
long route_params = bindings.RouteParameters_new(payee, 1000, 42);
- long route = bindings.find_route(peer1.node_id, route_params, peer1.router, peer1_chans,
- peer1.logger, scorer_interface, new byte[32]);
+ long route = bindings.find_route(peer1.node_id, route_params, peer1.router, peer1_chans, peer1.logger,
+ scorer_interface, new byte[32]);
bindings.RouteParameters_free(route_params);
bindings.PaymentParameters_free(payee);
bindings.Score_free(scorer_interface);
- bindings.Scorer_free(scorer);
+ bindings.ProbabilisticScorer_free(scorer);
for (long chan : peer1_chans) bindings.ChannelDetails_free(chan);
assert bindings.CResult_RouteLightningErrorZ_is_ok(route);
assert payment_recvd instanceof bindings.LDKEvent.PaymentReceived;
bindings.LDKPaymentPurpose purpose = bindings.LDKPaymentPurpose_ref_from_ptr(((bindings.LDKEvent.PaymentReceived) payment_recvd).purpose);
assert purpose instanceof bindings.LDKPaymentPurpose.InvoicePayment;
- assert bindings.ChannelManager_claim_funds(peer2.chan_manager, ((bindings.LDKPaymentPurpose.InvoicePayment) purpose).payment_preimage);
+ bindings.ChannelManager_claim_funds(peer2.chan_manager, ((bindings.LDKPaymentPurpose.InvoicePayment) purpose).payment_preimage);
+ bindings.Event_free(events.remove(0));
+
+ bindings.EventsProvider_process_pending_events(peer2.chan_manager_events, handler);
+ assert events.size() == 1;
+ bindings.LDKEvent payment_claimed = bindings.LDKEvent_ref_from_ptr(events.get(0));
+ assert payment_claimed instanceof bindings.LDKEvent.PaymentClaimed;
bindings.Event_free(events.remove(0));
deliver_peer_messages(list, peer1.peer_manager, peer2.peer_manager);