From 36e5154452d7583496053259e2c6b93b9baeaf8a Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Tue, 6 Oct 2020 19:25:17 -0400 Subject: [PATCH] Go all the way through to channel creation in PeerTest --- genbindings.py | 12 +++++++++++ src/main/java/org/ldk/impl/bindings.java | 1 + src/main/jni/bindings.c | 11 ++++++++++ src/main/jni/org_ldk_impl_bindings.h | 8 ++++++++ src/test/java/org/ldk/PeerTest.java | 26 ++++++++++++++++++------ 5 files changed, 52 insertions(+), 6 deletions(-) diff --git a/genbindings.py b/genbindings.py index a230ca09..b4f9323d 100755 --- a/genbindings.py +++ b/genbindings.py @@ -545,6 +545,7 @@ void __attribute__((destructor)) check_leaks() { public static native byte[] get_u8_slice_bytes(long slice_ptr); public static native long bytes_to_u8_vec(byte[] bytes); public static native long vec_slice_len(long vec); + public static native long new_empty_slice_vec(); """) out_c.write(""" @@ -585,6 +586,17 @@ JNIEXPORT jlong JNICALL Java_org_ldk_impl_bindings_vec_1slice_1len (JNIEnv * env LDKCVec_u8Z *vec = (LDKCVec_u8Z*)ptr; return (long)vec->datalen; } +JNIEXPORT long JNICALL Java_org_ldk_impl_bindings_new_1empty_1slice_1vec (JNIEnv * _env, jclass _b) { + // Check sizes of a few Vec types are all consistent as we're meant to be generic across types + _Static_assert(sizeof(LDKCVec_u8Z) == sizeof(LDKCVec_SignatureZ), "Vec<*> needs to be mapped identically"); + _Static_assert(sizeof(LDKCVec_u8Z) == sizeof(LDKCVec_MessageSendEventZ), "Vec<*> needs to be mapped identically"); + _Static_assert(sizeof(LDKCVec_u8Z) == sizeof(LDKCVec_EventZ), "Vec<*> needs to be mapped identically"); + _Static_assert(sizeof(LDKCVec_u8Z) == sizeof(LDKCVec_C2Tuple_usizeTransactionZZ), "Vec<*> needs to be mapped identically"); + LDKCVec_u8Z *vec = (LDKCVec_u8Z*)MALLOC(sizeof(LDKCVec_u8Z), "Empty LDKCVec"); + vec->data = NULL; + vec->datalen = 0; + return (long)vec; +} // We assume that CVec_u8Z and u8slice are the same size and layout (and thus pointers to the two can be mixed) _Static_assert(sizeof(LDKCVec_u8Z) == sizeof(LDKu8slice), "Vec and [u8] need to have been mapped identically"); diff --git a/src/main/java/org/ldk/impl/bindings.java b/src/main/java/org/ldk/impl/bindings.java index 4e4ba083..6ff047a1 100644 --- a/src/main/java/org/ldk/impl/bindings.java +++ b/src/main/java/org/ldk/impl/bindings.java @@ -14,6 +14,7 @@ public class bindings { public static native byte[] get_u8_slice_bytes(long slice_ptr); public static native long bytes_to_u8_vec(byte[] bytes); public static native long vec_slice_len(long vec); + public static native long new_empty_slice_vec(); public static native long LDKSecretKey_new(); diff --git a/src/main/jni/bindings.c b/src/main/jni/bindings.c index a1a0506d..dcbd2cc5 100644 --- a/src/main/jni/bindings.c +++ b/src/main/jni/bindings.c @@ -86,6 +86,17 @@ JNIEXPORT jlong JNICALL Java_org_ldk_impl_bindings_vec_1slice_1len (JNIEnv * env LDKCVec_u8Z *vec = (LDKCVec_u8Z*)ptr; return (long)vec->datalen; } +JNIEXPORT long JNICALL Java_org_ldk_impl_bindings_new_1empty_1slice_1vec (JNIEnv * _env, jclass _b) { + // Check sizes of a few Vec types are all consistent as we're meant to be generic across types + _Static_assert(sizeof(LDKCVec_u8Z) == sizeof(LDKCVec_SignatureZ), "Vec<*> needs to be mapped identically"); + _Static_assert(sizeof(LDKCVec_u8Z) == sizeof(LDKCVec_MessageSendEventZ), "Vec<*> needs to be mapped identically"); + _Static_assert(sizeof(LDKCVec_u8Z) == sizeof(LDKCVec_EventZ), "Vec<*> needs to be mapped identically"); + _Static_assert(sizeof(LDKCVec_u8Z) == sizeof(LDKCVec_C2Tuple_usizeTransactionZZ), "Vec<*> needs to be mapped identically"); + LDKCVec_u8Z *vec = (LDKCVec_u8Z*)MALLOC(sizeof(LDKCVec_u8Z), "Empty LDKCVec"); + vec->data = NULL; + vec->datalen = 0; + return (long)vec; +} // We assume that CVec_u8Z and u8slice are the same size and layout (and thus pointers to the two can be mixed) _Static_assert(sizeof(LDKCVec_u8Z) == sizeof(LDKu8slice), "Vec and [u8] need to have been mapped identically"); diff --git a/src/main/jni/org_ldk_impl_bindings.h b/src/main/jni/org_ldk_impl_bindings.h index a68656f6..c7b69658 100644 --- a/src/main/jni/org_ldk_impl_bindings.h +++ b/src/main/jni/org_ldk_impl_bindings.h @@ -63,6 +63,14 @@ JNIEXPORT jlong JNICALL Java_org_ldk_impl_bindings_bytes_1to_1u8_1vec JNIEXPORT jlong JNICALL Java_org_ldk_impl_bindings_vec_1slice_1len (JNIEnv *, jclass, jlong); +/* + * Class: org_ldk_impl_bindings + * Method: new_empty_slice_vec + * Signature: ()J + */ +JNIEXPORT jlong JNICALL Java_org_ldk_impl_bindings_new_1empty_1slice_1vec + (JNIEnv *, jclass); + /* * Class: org_ldk_impl_bindings * Method: LDKSecretKey_new diff --git a/src/test/java/org/ldk/PeerTest.java b/src/test/java/org/ldk/PeerTest.java index c082a7c8..3420de0b 100644 --- a/src/test/java/org/ldk/PeerTest.java +++ b/src/test/java/org/ldk/PeerTest.java @@ -30,17 +30,17 @@ public class PeerTest { this.chain_monitor = bindings.LDKWatch_new(new bindings.LDKWatch() { @Override public long watch_channel(long funding_txo, long monitor) { - return 0; + return bindings.CResult_NoneChannelMonitorUpdateErrZ_ok(); } @Override public long update_channel(long funding_txo, long update) { - return 0; + return bindings.CResult_NoneChannelMonitorUpdateErrZ_ok(); } @Override public long release_pending_monitor_events() { - return 0; + return bindings.new_empty_slice_vec(); } }); @@ -134,9 +134,23 @@ public class PeerTest { 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(); - } + while (!list.isEmpty()) { list.poll().join(); } + + long cc_res = bindings.ChannelManager_create_channel(peer1.chan_manager, bindings.ChannelManager_get_our_node_id(peer2.chan_manager), 10000, 1000, 0, bindings.LDKUserConfig_optional_none()); + assert bindings.LDKCResult_NoneAPIErrorZ_result_ok(cc_res); + bindings.CResult_NoneAPIErrorZ_free(cc_res); + + bindings.PeerManager_process_events(peer1.peer_manager); + while (!list.isEmpty()) { list.poll().join(); } + bindings.PeerManager_process_events(peer2.peer_manager); + while (!list.isEmpty()) { list.poll().join(); } + + long peer1_chans = bindings.ChannelManager_list_channels(peer1.chan_manager); + long peer2_chans = bindings.ChannelManager_list_channels(peer2.chan_manager); + assert bindings.vec_slice_len(peer1_chans) == 1; + assert bindings.vec_slice_len(peer2_chans) == 1; + bindings.CVec_ChannelDetailsZ_free(peer1_chans); + bindings.CVec_ChannelDetailsZ_free(peer2_chans); peer1.free(); peer2.free(); -- 2.39.5