Don't let CI fail to check commits b/c git isn't configured in CI
[rust-lightning] / lightning-c-bindings / demo.c
1 #include "include/rust_types.h"
2 #include "include/lightning.h"
3
4 #include <assert.h>
5 #include <stdio.h>
6 #include <string.h>
7
8 void print_log(const void *this_arg, const char *record) {
9         printf("%s", record);
10 }
11
12 uint32_t get_fee(const void *this_arg, LDKConfirmationTarget target) {
13         if (target == LDKConfirmationTarget_Background) {
14                 return 253;
15         } else {
16                 return 507;
17         }
18 }
19
20 void broadcast_tx(const void *this_arg, LDKTransaction tx) {
21         //TODO
22 }
23
24 LDKCResult_NoneChannelMonitorUpdateErrZ add_channel_monitor(const void *this_arg, LDKOutPoint funding_txo, LDKChannelMonitor monitor) {
25         return CResult_NoneChannelMonitorUpdateErrZ_ok();
26 }
27 LDKCResult_NoneChannelMonitorUpdateErrZ update_channel_monitor(const void *this_arg, LDKOutPoint funding_txo, LDKChannelMonitorUpdate monitor) {
28         return CResult_NoneChannelMonitorUpdateErrZ_ok();
29 }
30 LDKCVec_MonitorEventZ monitors_pending_monitor_events(const void *this_arg) {
31         LDKCVec_MonitorEventZ empty_htlc_vec = {
32                 .data = NULL,
33                 .datalen = 0,
34         };
35         return empty_htlc_vec;
36 }
37
38 int main() {
39         uint8_t node_seed[32];
40         memset(node_seed, 0, 32);
41
42         LDKNetwork net = LDKNetwork_Bitcoin;
43
44         LDKLogger logger = {
45                 .this_arg = NULL,
46                 .log = print_log,
47                 .free = NULL,
48         };
49
50         LDKFeeEstimator fee_est = {
51                 .this_arg = NULL,
52                 .get_est_sat_per_1000_weight = get_fee,
53                 .free = NULL
54         };
55
56         LDKManyChannelMonitor mon = {
57                 .this_arg = NULL,
58                 .add_monitor = add_channel_monitor,
59                 .update_monitor = update_channel_monitor,
60                 .get_and_clear_pending_monitor_events = monitors_pending_monitor_events,
61                 .free = NULL,
62         };
63
64         LDKBroadcasterInterface broadcast = {
65                 broadcast.this_arg = NULL,
66                 broadcast.broadcast_transaction = broadcast_tx,
67                 .free = NULL,
68         };
69
70         LDKKeysManager keys = KeysManager_new(&node_seed, net, 0, 0);
71         LDKKeysInterface keys_source = KeysManager_as_KeysInterface(&keys);
72
73         LDKUserConfig config = UserConfig_default();
74
75         LDKChannelManager cm = ChannelManager_new(net, fee_est, mon, broadcast, logger, keys_source, config, 0);
76
77         LDKCVec_ChannelDetailsZ channels = ChannelManager_list_channels(&cm);
78         assert((unsigned long)channels.data < 4096); // There's an offset, but it should still be an offset against null in the 0 page
79         assert(channels.datalen == 0);
80         CVec_ChannelDetailsZ_free(channels);
81
82         LDKEventsProvider prov = ChannelManager_as_EventsProvider(&cm);
83         LDKCVec_EventZ events = (prov.get_and_clear_pending_events)(prov.this_arg);
84         assert((unsigned long)events.data < 4096); // There's an offset, but it should still be an offset against null in the 0 page
85         assert(events.datalen == 0);
86
87         ChannelManager_free(cm);
88         KeysManager_free(keys);
89
90         // Check that passing empty vecs to rust doesn't blow it up:
91         LDKCVec_MonitorEventZ empty_htlc_vec = {
92                 .data = NULL,
93                 .datalen = 0,
94         };
95         CVec_MonitorEventZ_free(empty_htlc_vec);
96 }