extern "C" {
-#include "include/rust_types.h"
-#include "include/lightning.h"
+#include <lightning.h>
}
#include "include/lightningpp.hpp"
0x00, // transaction count
};
-const LDKThirtyTwoBytes payment_preimage_1 = {
- .data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1 }
-};
-const LDKThirtyTwoBytes payment_hash_1 = {
- .data = {
- 0xdc, 0xb1, 0xac, 0x4a, 0x5d, 0xe3, 0x70, 0xca, 0xd0, 0x91, 0xc1, 0x3f, 0x13, 0xae, 0xe2, 0xf9,
- 0x36, 0xc2, 0x78, 0xfa, 0x05, 0xd2, 0x64, 0x65, 0x3c, 0x0c, 0x13, 0x21, 0x85, 0x2a, 0x35, 0xe8
- }
-};
-
const LDKThirtyTwoBytes genesis_hash = { // We don't care particularly if this is "right"
.data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1 }
};
void ConnectBlock(const uint8_t (*header)[80], uint32_t height, LDKCVec_C2Tuple_usizeTransactionZZ tx_data, LDKBroadcasterInterface broadcast, LDKFeeEstimator fee_est) {
std::unique_lock<std::mutex> l(mut);
for (auto& mon : mons) {
- LDK::CVec_C2Tuple_TxidCVec_C2Tuple_u32TxOutZZZZ res = ChannelMonitor_block_connected(&mon.second, header, tx_data, height, broadcast, fee_est, *logger);
+ LDK::CVec_TransactionOutputsZ res = ChannelMonitor_block_connected(&mon.second, header, tx_data, height, broadcast, fee_est, *logger);
}
}
};
}
}
+struct EventQueue {
+ std::vector<LDK::Event> events;
+};
+void handle_event(const void *this_arg, LDKEvent event) {
+ EventQueue* arg = (EventQueue*) this_arg;
+ arg->events.push_back(std::move(event));
+}
+
+
uintptr_t sock_send_data(void *this_arg, LDKu8slice data, bool resume_read) {
return write((int)((long)this_arg), data.data, data.datalen);
}
LDK::KeysInterface keys_source1 = KeysManager_as_KeysInterface(&keys1);
node_secret1 = keys_source1->get_node_secret(keys_source1->this_arg);
- LDK::ChannelManager cm1 = ChannelManager_new(fee_est, mon1, broadcast, logger1, KeysManager_as_KeysInterface(&keys1), UserConfig_default(), ChainParameters_new(network, chain_tip, 0));
+ LDK::ChannelManager cm1 = ChannelManager_new(fee_est, mon1, broadcast, logger1, KeysManager_as_KeysInterface(&keys1), UserConfig_default(), ChainParameters_new(network, BestBlock_new(chain_tip, 0)));
LDK::CVec_ChannelDetailsZ channels = ChannelManager_list_channels(&cm1);
assert(channels->datalen == 0);
LDK::UserConfig config2 = UserConfig_default();
UserConfig_set_own_channel_config(&config2, std::move(handshake_config2));
- LDK::ChannelManager cm2 = ChannelManager_new(fee_est, mon2, broadcast, logger2, KeysManager_as_KeysInterface(&keys2), std::move(config2), ChainParameters_new(network, chain_tip, 0));
+ LDK::ChannelManager cm2 = ChannelManager_new(fee_est, mon2, broadcast, logger2, KeysManager_as_KeysInterface(&keys2), std::move(config2), ChainParameters_new(network, BestBlock_new(chain_tip, 0)));
LDK::CVec_ChannelDetailsZ channels2 = ChannelManager_list_channels(&cm2);
assert(channels2->datalen == 0);
LDKEventsProvider ev1 = ChannelManager_as_EventsProvider(&cm1);
while (true) {
- LDK::CVec_EventZ events = ev1.get_and_clear_pending_events(ev1.this_arg);
- if (events->datalen == 1) {
- assert(events->data[0].tag == LDKEvent_FundingGenerationReady);
- assert(events->data[0].funding_generation_ready.user_channel_id == 42);
- assert(events->data[0].funding_generation_ready.channel_value_satoshis == 40000);
- assert(events->data[0].funding_generation_ready.output_script.datalen == 34);
- assert(!memcmp(events->data[0].funding_generation_ready.output_script.data, channel_open_block + 58 + 81, 34));
+ EventQueue queue;
+ LDKEventHandler handler = { .this_arg = &queue, .handle_event = handle_event, .free = NULL };
+ ev1.process_pending_events(ev1.this_arg, handler);
+ if (queue.events.size() == 1) {
+ assert(queue.events[0]->tag == LDKEvent_FundingGenerationReady);
+ assert(queue.events[0]->funding_generation_ready.user_channel_id == 42);
+ assert(queue.events[0]->funding_generation_ready.channel_value_satoshis == 40000);
+ assert(queue.events[0]->funding_generation_ready.output_script.datalen == 34);
+ assert(!memcmp(queue.events[0]->funding_generation_ready.output_script.data, channel_open_block + 58 + 81, 34));
LDKTransaction funding_transaction { .data = const_cast<uint8_t*>(channel_open_block + 81), .datalen = sizeof(channel_open_block) - 81, .data_is_owned = false };
- LDK::CResult_NoneAPIErrorZ fund_res = ChannelManager_funding_transaction_generated(&cm1, &events->data[0].funding_generation_ready.temporary_channel_id.data, funding_transaction);
+ LDK::CResult_NoneAPIErrorZ fund_res = ChannelManager_funding_transaction_generated(&cm1, &queue.events[0]->funding_generation_ready.temporary_channel_id.data, funding_transaction);
assert(fund_res->result_ok);
break;
}
// We opened the channel with 1000 push_msat:
assert(ChannelDetails_get_outbound_capacity_msat(channel) == 40000*1000 - 1000);
assert(ChannelDetails_get_inbound_capacity_msat(channel) == 1000);
- assert(ChannelDetails_get_is_live(channel));
+ assert(ChannelDetails_get_is_usable(channel));
break;
}
std::this_thread::yield();
}
- LDK::CVec_ChannelDetailsZ outbound_channels = ChannelManager_list_usable_channels(&cm1);
- LDKThirtyTwoBytes payment_secret;
- memset(payment_secret.data, 0x42, 32);
+ LDKCOption_u64Z min_value = {
+ .tag = LDKCOption_u64Z_Some,
+ .some = 5000,
+ };
+ LDK::CResult_InvoiceSignOrCreationErrorZ invoice = create_invoice_from_channelmanager(&cm2,
+ KeysManager_as_KeysInterface(&keys2),
+ LDKCurrency_Bitcoin, min_value,
+ LDKStr {
+ .chars = (const uint8_t *)"Invoice Description",
+ .len = strlen("Invoice Description"),
+ .chars_is_owned = false
+ });
+ assert(invoice->result_ok);
+ LDKThirtyTwoBytes payment_hash;
+ memcpy(payment_hash.data, Invoice_payment_hash(invoice->contents.result), 32);
+
{
+ LDK::CVec_ChannelDetailsZ outbound_channels = ChannelManager_list_usable_channels(&cm1);
LDK::LockedNetworkGraph graph_2_locked = NetGraphMsgHandler_read_locked_graph(&net_graph2);
LDK::NetworkGraph graph_2_ref = LockedNetworkGraph_graph(&graph_2_locked);
LDK::CResult_RouteLightningErrorZ route = get_route(ChannelManager_get_our_node_id(&cm1), &graph_2_ref, ChannelManager_get_our_node_id(&cm2), LDKInvoiceFeatures {
.inner = NULL, .is_owned = false
- }, &outbound_channels, LDKCVec_RouteHintZ {
+ }, &outbound_channels, LDKCVec_RouteHintHopZ {
.data = NULL, .datalen = 0
- }, 5000, 10, logger1);
+ }, 5000, Invoice_min_final_cltv_expiry(invoice->contents.result), logger1);
assert(route->result_ok);
- LDK::CResult_NonePaymentSendFailureZ send_res = ChannelManager_send_payment(&cm1, route->contents.result, payment_hash_1, payment_secret);
+ LDK::CResult_NonePaymentSendFailureZ send_res = ChannelManager_send_payment(&cm1, route->contents.result, payment_hash, Invoice_payment_secret(invoice->contents.result));
assert(send_res->result_ok);
}
// Check that we received the payment!
LDKEventsProvider ev2 = ChannelManager_as_EventsProvider(&cm2);
while (true) {
- LDK::CVec_EventZ events = ev2.get_and_clear_pending_events(ev2.this_arg);
- if (events->datalen == 1) {
- assert(events->data[0].tag == LDKEvent_PendingHTLCsForwardable);
+ EventQueue queue;
+ LDKEventHandler handler = { .this_arg = &queue, .handle_event = handle_event, .free = NULL };
+ ev2.process_pending_events(ev2.this_arg, handler);
+ if (queue.events.size() == 1) {
+ assert(queue.events[0]->tag == LDKEvent_PendingHTLCsForwardable);
break;
}
std::this_thread::yield();
PeerManager_process_events(&net2);
mons_updated = 0;
+ LDKThirtyTwoBytes payment_preimage;
{
- LDK::CVec_EventZ events = ev2.get_and_clear_pending_events(ev2.this_arg);
- assert(events->datalen == 1);
- assert(events->data[0].tag == LDKEvent_PaymentReceived);
- assert(!memcmp(events->data[0].payment_received.payment_hash.data, payment_hash_1.data, 32));
- assert(!memcmp(events->data[0].payment_received.payment_secret.data, payment_secret.data, 32));
- assert(events->data[0].payment_received.amt == 5000);
- assert(ChannelManager_claim_funds(&cm2, payment_preimage_1, payment_secret, 5000));
+ EventQueue queue;
+ LDKEventHandler handler = { .this_arg = &queue, .handle_event = handle_event, .free = NULL };
+ ev2.process_pending_events(ev2.this_arg, handler);
+ assert(queue.events.size() == 1);
+ assert(queue.events[0]->tag == LDKEvent_PaymentReceived);
+ assert(!memcmp(queue.events[0]->payment_received.payment_hash.data, payment_hash.data, 32));
+ assert(!memcmp(queue.events[0]->payment_received.payment_secret.data, Invoice_payment_secret(invoice->contents.result).data, 32));
+ assert(queue.events[0]->payment_received.amt == 5000);
+ memcpy(payment_preimage.data, queue.events[0]->payment_received.payment_preimage.data, 32);
+ assert(ChannelManager_claim_funds(&cm2, payment_preimage));
}
PeerManager_process_events(&net2);
// Wait until we've passed through a full set of monitor updates (ie new preimage + CS/RAA messages)
std::this_thread::yield();
}
{
- LDK::CVec_EventZ events = ev1.get_and_clear_pending_events(ev1.this_arg);
- assert(events->datalen == 1);
- assert(events->data[0].tag == LDKEvent_PaymentSent);
- assert(!memcmp(events->data[0].payment_sent.payment_preimage.data, payment_preimage_1.data, 32));
+ EventQueue queue;
+ LDKEventHandler handler = { .this_arg = &queue, .handle_event = handle_event, .free = NULL };
+ ev1.process_pending_events(ev1.this_arg, handler);
+ assert(queue.events.size() == 1);
+ assert(queue.events[0]->tag == LDKEvent_PaymentSent);
+ assert(!memcmp(queue.events[0]->payment_sent.payment_preimage.data, payment_preimage.data, 32));
}
conn.stop();