X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning-c-bindings%2Fdemo.cpp;h=fa75140a59e9be8c53bc48034513e6f63977e10b;hb=8b697a686e0ddf11c1e57a68f595deb9329ab16b;hp=62692495ae998adc2c89f7efc32863601d970c08;hpb=4a8a65a3455e8164509ac48069a010e7c72fc639;p=ldk-c-bindings diff --git a/lightning-c-bindings/demo.cpp b/lightning-c-bindings/demo.cpp index 6269249..fa75140 100644 --- a/lightning-c-bindings/demo.cpp +++ b/lightning-c-bindings/demo.cpp @@ -167,7 +167,7 @@ LDKCResult_NoneChannelMonitorUpdateErrZ update_channel_monitor(const void *this_ LDKBroadcasterInterface broadcaster = { .broadcast_transaction = broadcast_tx, }; - LDK::CResult_NoneMonitorUpdateErrorZ res = ChannelMonitor_update_monitor(&mon.second, &update, &broadcaster, &fee_est, arg->logger); + LDK::CResult_NoneNoneZ res = ChannelMonitor_update_monitor(&mon.second, &update, &broadcaster, &fee_est, arg->logger); assert(res->result_ok); } } @@ -331,9 +331,9 @@ public: t2 = std::thread(&sock_read_data_thread, pipefds_1_to_2[0], &sock2, &net2); // Note that we have to bind the result to a C++ class to make sure it gets free'd - LDK::CResult_CVec_u8ZPeerHandleErrorZ con_res = PeerManager_new_outbound_connection(&net1, ChannelManager_get_our_node_id(&cm2), sock1); + LDK::CResult_CVec_u8ZPeerHandleErrorZ con_res = PeerManager_new_outbound_connection(&net1, ChannelManager_get_our_node_id(&cm2), sock1, COption_NetAddressZ_none()); assert(con_res->result_ok); - LDK::CResult_NonePeerHandleErrorZ con_res2 = PeerManager_new_inbound_connection(&net2, sock2); + LDK::CResult_NonePeerHandleErrorZ con_res2 = PeerManager_new_inbound_connection(&net2, sock2, COption_NetAddressZ_none()); assert(con_res2->result_ok); auto writelen = write(pipefds_1_to_2[1], con_res->contents.result->data, con_res->contents.result->datalen); @@ -421,17 +421,20 @@ LDKCVec_C2Tuple_PublicKeyTypeZZ create_custom_msg(const void* this_arg) { return ret; } -uint64_t get_chan_score(const void *this_arg, uint64_t scid, const LDKNodeId *src, const LDKNodeId *dst) { return 42; } +uint64_t get_chan_score(const void *this_arg, uint64_t scid, uint64_t htlc_amt, uint64_t chan_capacity, const LDKNodeId *src, const LDKNodeId *dst) { + return 42; +} struct CustomRouteFinderParams { LDKLogger *logger; LDKNetworkGraph *graph_ref; + LDKThirtyTwoBytes random_seed_bytes; }; -struct LDKCResult_RouteLightningErrorZ custom_find_route(const void *this_arg, struct LDKPublicKey payer, const struct LDKRouteParameters *NONNULL_PTR route_params, struct LDKCVec_ChannelDetailsZ *first_hops, const struct LDKScore *NONNULL_PTR scorer) { +struct LDKCResult_RouteLightningErrorZ custom_find_route(const void *this_arg, struct LDKPublicKey payer, const struct LDKRouteParameters *NONNULL_PTR route_params, const uint8_t (*payment_hash)[32], struct LDKCVec_ChannelDetailsZ *first_hops, const struct LDKScore *NONNULL_PTR scorer) { const struct CustomRouteFinderParams *params = (struct CustomRouteFinderParams *)this_arg; assert(first_hops->datalen == 1); assert(ChannelDetails_get_is_usable(&first_hops->data[0])); - return find_route(payer, route_params, params->graph_ref, first_hops, *params->logger, scorer); + return find_route(payer, route_params, params->graph_ref, first_hops, *params->logger, scorer, ¶ms->random_seed_bytes.data); } int main() { @@ -508,7 +511,9 @@ int main() { memset(&node_seed, 0, 32); LDK::KeysManager keys1 = KeysManager_new(&node_seed, 0, 0); LDK::KeysInterface keys_source1 = KeysManager_as_KeysInterface(&keys1); - node_secret1 = keys_source1->get_node_secret(keys_source1->this_arg); + LDK::CResult_SecretKeyNoneZ node_secret1_res = keys_source1->get_node_secret(keys_source1->this_arg, LDKRecipient_Node); + assert(node_secret1_res->result_ok); + node_secret1 = *node_secret1_res->contents.result; LDK::ChannelManager cm1 = ChannelManager_new(fee_est, mon1, broadcast, logger1, KeysManager_as_KeysInterface(&keys1), UserConfig_default(), ChainParameters_new(network, BestBlock_new(chain_tip, 0))); @@ -533,7 +538,9 @@ int main() { memset(&node_seed, 1, 32); LDK::KeysManager keys2 = KeysManager_new(&node_seed, 0, 0); LDK::KeysInterface keys_source2 = KeysManager_as_KeysInterface(&keys2); - node_secret2 = keys_source2->get_node_secret(keys_source2->this_arg); + LDK::CResult_SecretKeyNoneZ node_secret2_res = keys_source2->get_node_secret(keys_source2->this_arg, LDKRecipient_Node); + assert(node_secret2_res->result_ok); + node_secret2 = *node_secret2_res->contents.result; LDK::ChannelHandshakeConfig handshake_config2 = ChannelHandshakeConfig_default(); ChannelHandshakeConfig_set_minimum_depth(&handshake_config2, 2); @@ -691,12 +698,13 @@ int main() { LDK::Score chan_scorer = LDKScore { .this_arg = NULL, .channel_penalty_msat = get_chan_score, .free = NULL }; - LDK::RouteParameters route_params = RouteParameters_new(Payee_new( + LDK::RouteParameters route_params = RouteParameters_new(PaymentParameters_new( ChannelManager_get_our_node_id(&cm2), LDKInvoiceFeatures { .inner = NULL, .is_owned = false - }, Invoice_route_hints(invoice->contents.result), COption_u64Z_none()), + }, Invoice_route_hints(invoice->contents.result), COption_u64Z_none(), 0xffffffff), 5000, Invoice_min_final_cltv_expiry(invoice->contents.result)); - LDK::CResult_RouteLightningErrorZ route = find_route(ChannelManager_get_our_node_id(&cm1), &route_params, &net_graph2, &outbound_channels, logger1, &chan_scorer); + random_bytes = keys_source1->get_secure_random_bytes(keys_source1->this_arg); + LDK::CResult_RouteLightningErrorZ route = find_route(ChannelManager_get_our_node_id(&cm1), &route_params, &net_graph2, &outbound_channels, logger1, &chan_scorer, &random_bytes.data); assert(route->result_ok); LDK::CVec_CVec_RouteHopZZ paths = Route_get_paths(route->contents.result); assert(paths->datalen == 1); @@ -755,10 +763,13 @@ int main() { { 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); + while (queue.events.size() < 2) + ev1.process_pending_events(ev1.this_arg, handler); + assert(queue.events.size() == 2); assert(queue.events[0]->tag == LDKEvent_PaymentSent); assert(!memcmp(queue.events[0]->payment_sent.payment_preimage.data, payment_preimage.data, 32)); + assert(queue.events[1]->tag == LDKEvent_PaymentPathSuccessful); + assert(!memcmp(queue.events[1]->payment_path_successful.payment_hash.data, payment_hash.data, 32)); } conn.stop(); @@ -849,14 +860,15 @@ int main() { struct CustomRouteFinderParams router_params = { .logger = &logger1, .graph_ref = &net_graph1, + .random_seed_bytes = keys_source1->get_secure_random_bytes(keys_source1->this_arg), }; LDKRouter sending_router = { .this_arg = &router_params, .find_route = custom_find_route, .free = NULL, }; - LDK::Scorer scorer = Scorer_default(); - LDK::LockableScore scorer_mtx = LockableScore_new(Scorer_as_Score(&scorer)); + LDK::ProbabilisticScorer scorer = ProbabilisticScorer_new(ProbabilisticScoringParameters_default(), &net_graph1); + LDK::MultiThreadedLockableScore scorer_mtx = MultiThreadedLockableScore_new(ProbabilisticScorer_as_Score(&scorer)); EventQueue queue1; LDKEventHandler handler1 = { .this_arg = &queue1, .handle_event = handle_event, .free = NULL }; LDK::InvoicePayer payer = InvoicePayer_new(ChannelManager_as_Payer(&cm1), sending_router, &scorer_mtx, logger1, handler1, RetryAttempts_new(0)); @@ -909,7 +921,7 @@ int main() { std::this_thread::yield(); } - while (queue1.events.size() == 0) { + while (queue1.events.size() < 2) { PeerManager_process_events(&net2); PeerManager_process_events(&net1); @@ -917,8 +929,9 @@ int main() { LDK::EventHandler evh1 = InvoicePayer_as_EventHandler(&payer); ev1.process_pending_events(std::move(evh1)); } - assert(queue1.events.size() == 1); + assert(queue1.events.size() == 2); assert(queue1.events[0]->tag == LDKEvent_PaymentSent); + assert(queue1.events[1]->tag == LDKEvent_PaymentPathSuccessful); // Actually close the channel num_txs_broadcasted = 0; @@ -942,5 +955,5 @@ int main() { memset(&sk, 42, 32); LDKThirtyTwoBytes kdiv_params; memset(&kdiv_params, 43, 32); - LDK::InMemorySigner signer = InMemorySigner_new(sk, sk, sk, sk, sk, random_bytes, 42, kdiv_params); + LDK::InMemorySigner signer = InMemorySigner_new(sk, sk, sk, sk, sk, sk, random_bytes, 42, kdiv_params); }