From: Matt Corallo Date: Tue, 6 Oct 2020 22:43:22 +0000 (-0400) Subject: Update PeerTest to test most of the new stuff X-Git-Tag: v0.0.1~132 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=e9fe913798f08e255e4a01948356d7132ac1433e;p=ldk-java Update PeerTest to test most of the new stuff --- diff --git a/src/test/java/org/ldk/PeerTest.java b/src/test/java/org/ldk/PeerTest.java index ce5024c5..c082a7c8 100644 --- a/src/test/java/org/ldk/PeerTest.java +++ b/src/test/java/org/ldk/PeerTest.java @@ -3,68 +3,144 @@ package org.ldk; import org.junit.jupiter.api.Test; import org.ldk.impl.bindings; +import java.util.concurrent.ConcurrentLinkedQueue; + public class PeerTest { - @Test - void test_message_handler() { - long logger = bindings.LDKLogger_new((String arg)-> System.out.println(arg)); + class Peer { + final long logger; + final long fee_estimator; + final long tx_broadcaster; + final long chain_monitor; + final long keys; + final long keys_interface; + final long config; + final long chan_manager; + final long chan_handler; + final long router; + final long route_handler; + final long message_handler; + final long peer_manager; - long fee_estimator = bindings.LDKFeeEstimator_new(new bindings.LDKFeeEstimator() { - @Override - public int get_est_sat_per_1000_weight(bindings.LDKConfirmationTarget confirmation_target) { - return 0; - } - }); - long tx_broadcaster = bindings.LDKBroadcasterInterface_new(new bindings.LDKBroadcasterInterface() { - @Override - public void broadcast_transaction(long tx) { + Peer(byte seed) { + logger = bindings.LDKLogger_new((String arg)-> System.out.println(seed + ": " + arg)); + this.fee_estimator = bindings.LDKFeeEstimator_new(confirmation_target -> 0); + this.tx_broadcaster = bindings.LDKBroadcasterInterface_new(tx -> { // We should broadcast - } + }); + this.chain_monitor = bindings.LDKWatch_new(new bindings.LDKWatch() { + @Override + public long watch_channel(long funding_txo, long monitor) { + return 0; + } + + @Override + public long update_channel(long funding_txo, long update) { + return 0; + } + + @Override + public long release_pending_monitor_events() { + return 0; + } + }); + + byte[] key_seed = new byte[32]; + for (byte i = 0; i < 32; i++) { key_seed[i] = (byte) (i ^ seed); } + this.keys = bindings.KeysManager_new(key_seed, bindings.LDKNetwork.LDKNetwork_Bitcoin, System.currentTimeMillis() / 1000, (int)(System.currentTimeMillis() * 1000) & 0xffffffff); + this.keys_interface = bindings.KeysManager_as_KeysInterface(keys); + this.config = bindings.UserConfig_default(); + this.chan_manager = bindings.ChannelManager_new(bindings.LDKNetwork.LDKNetwork_Bitcoin, fee_estimator, chain_monitor, tx_broadcaster, logger, keys_interface, config, 1); + + this.chan_handler = bindings.ChannelManager_as_ChannelMessageHandler(chan_manager); + this.router = bindings.NetGraphMsgHandler_new(0, logger); + this.route_handler = bindings.NetGraphMsgHandler_as_RoutingMessageHandler(router); + this.message_handler = bindings.MessageHandler_new(chan_handler, route_handler); + + byte[] random_data = new byte[32]; + for (byte i = 0; i < 32; i++) { random_data[i] = (byte) ((i ^ seed) ^ 0xf0); } + this.peer_manager = bindings.PeerManager_new(message_handler, bindings.LDKKeysInterface_call_get_node_secret(keys_interface), random_data, logger); + } + + void free() { + // Note that we can't rely on finalizer order, so don't bother trying to rely on it here + bindings.Logger_free(logger); + bindings.FeeEstimator_free(fee_estimator); + bindings.BroadcasterInterface_free(tx_broadcaster); + bindings.Watch_free(chain_monitor); + bindings.KeysManager_free(keys); + bindings.KeysInterface_free(keys_interface); + bindings.ChannelManager_free(chan_manager); + bindings.ChannelMessageHandler_free(chan_handler); + bindings.NetGraphMsgHandler_free(router); + 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; } + + byte[] do_read_event(ConcurrentLinkedQueue list, long pm, long descriptor, long data) { + byte[] arr = bindings.get_u8_slice_bytes(data); + Thread thread = new Thread(() -> { + long arr_vec = bindings.bytes_to_u8_vec(arr); + long res = bindings.PeerManager_read_event(pm, descriptor, arr_vec); + assert bindings.LDKCResult_boolPeerHandleErrorZ_result_ok(res); + //assert bindings.deref_bool(bindings.LDKCResult_boolPeerHandleErrorZ_get_inner(res)); + bindings.CResult_boolPeerHandleErrorZ_free(res); + bindings.free_heap_ptr(arr_vec); }); - long chain_monitor = bindings.LDKWatch_new(new bindings.LDKWatch() { - @Override - public long watch_channel(long funding_txo, long monitor) { - return 0; - } + thread.start(); + list.add(thread); + return arr; + } + + @Test + void test_message_handler() throws InterruptedException { + Peer peer1 = new Peer((byte) 1); + Peer peer2 = new Peer((byte) 2); + ConcurrentLinkedQueue list = new ConcurrentLinkedQueue(); + LongHolder descriptor1 = new LongHolder(); + LongHolder descriptor1ref = descriptor1; + long descriptor2 = bindings.LDKSocketDescriptor_new(new bindings.LDKSocketDescriptor() { @Override - public long update_channel(long funding_txo, long update) { - return 0; + public long send_data(long data, boolean resume_read) { + return do_read_event(list, peer1.peer_manager, descriptor1ref.val, data).length; } + @Override public void disconnect_socket() { assert false; } + @Override public boolean eq(long other_arg) { return bindings.LDKSocketDescriptor_get_obj_from_jcalls(other_arg).hash() == 2; } + @Override public long hash() { return 2; } + }); + + descriptor1.val = bindings.LDKSocketDescriptor_new(new bindings.LDKSocketDescriptor() { @Override - public long release_pending_monitor_events() { - return 0; + public long send_data(long data, boolean resume_read) { + return do_read_event(list, peer2.peer_manager, descriptor2, data).length; } + + @Override public void disconnect_socket() { assert false; } + @Override public boolean eq(long other_arg) { return bindings.LDKSocketDescriptor_get_obj_from_jcalls(other_arg).hash() == 1; } + @Override public long hash() { return 1; } }); - byte[] key_seed = new byte[32]; - for (byte i = 0; i < 32; i++) { key_seed[i] = i; } - long keys = bindings.KeysManager_new(key_seed, bindings.LDKNetwork.LDKNetwork_Bitcoin, System.currentTimeMillis() / 1000, (int)(System.currentTimeMillis() * 1000) & 0xffffffff); - long keys_interface = bindings.KeysManager_as_KeysInterface(keys); - long config = bindings.UserConfig_default(); - long chan_manager = bindings.ChannelManager_new(bindings.LDKNetwork.LDKNetwork_Bitcoin, fee_estimator, chain_monitor, tx_broadcaster, logger, keys_interface, config, 1); - - long chan_handler = bindings.ChannelManager_as_ChannelMessageHandler(chan_manager); - long router = bindings.NetGraphMsgHandler_new(0, logger); - long route_handler = bindings.NetGraphMsgHandler_as_RoutingMessageHandler(router); - long message_handler = bindings.MessageHandler_new(chan_handler, route_handler); - long our_node_secret = bindings.LDKSecretKey_new(); //TODO: Need LDKSecretKey constructor - byte[] random_data = new byte[32]; - for (byte i = 0; i < 32; i++) { random_data[i] = i; } - - long peer_manager = bindings.PeerManager_new(message_handler, our_node_secret, random_data, logger); - - // Note that we can't rely on finalizer order, so don't bother trying to rely on it here - bindings.Logger_free(logger); - bindings.FeeEstimator_free(fee_estimator); - bindings.BroadcasterInterface_free(tx_broadcaster); - bindings.Watch_free(chain_monitor); - bindings.KeysManager_free(keys); - bindings.KeysInterface_free(keys_interface); - bindings.ChannelManager_free(chan_manager); - bindings.ChannelMessageHandler_free(chan_handler); - bindings.NetGraphMsgHandler_free(router); - bindings.RoutingMessageHandler_free(route_handler); - //bindings.MessageHandler_free(message_handler); - bindings.PeerManager_free(peer_manager); + + long init_vec = bindings.PeerManager_new_outbound_connection(peer1.peer_manager, bindings.ChannelManager_get_our_node_id(peer2.chan_manager), descriptor1.val); + assert(bindings.LDKCResult_CVec_u8ZPeerHandleErrorZ_result_ok(init_vec)); + + long con_res = bindings.PeerManager_new_inbound_connection(peer2.peer_manager, descriptor2); + assert(bindings.LDKCResult_NonePeerHandleErrorZ_result_ok(con_res)); + bindings.CResult_NonePeerHandleErrorZ_free(con_res); + do_read_event(list, peer2.peer_manager, descriptor2, bindings.LDKCResult_CVec_u8ZPeerHandleErrorZ_get_inner(init_vec)); + bindings.CResult_CVec_u8ZPeerHandleErrorZ_free(init_vec); + + while (!list.isEmpty()) { + list.poll().join(); + } + + peer1.free(); + peer2.free(); + bindings.SocketDescriptor_free(descriptor2); + bindings.SocketDescriptor_free(descriptor1.val); } }