forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgossip_msg.c
197 lines (171 loc) · 6.19 KB
/
gossip_msg.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include <ccan/array_size/array_size.h>
#include <common/bolt11.h>
#include <common/wireaddr.h>
#include <lightningd/gossip_msg.h>
#include <wire/wire.h>
struct gossip_getnodes_entry *fromwire_gossip_getnodes_entry(const tal_t *ctx,
const u8 **pptr, size_t *max)
{
u8 numaddresses, i;
struct gossip_getnodes_entry *entry;
u16 flen;
entry = tal(ctx, struct gossip_getnodes_entry);
fromwire_node_id(pptr, max, &entry->nodeid);
entry->last_timestamp = fromwire_u64(pptr, max);
if (entry->last_timestamp < 0) {
entry->addresses = NULL;
return entry;
}
flen = fromwire_u16(pptr, max);
entry->globalfeatures = tal_arr(entry, u8, flen);
fromwire_u8_array(pptr, max, entry->globalfeatures, flen);
numaddresses = fromwire_u8(pptr, max);
entry->addresses = tal_arr(entry, struct wireaddr, numaddresses);
for (i=0; i<numaddresses; i++) {
/* Gossipd doesn't hand us addresses we can't understand. */
if (!fromwire_wireaddr(pptr, max, &entry->addresses[i])) {
fromwire_fail(pptr, max);
return NULL;
}
}
fromwire(pptr, max, entry->alias, ARRAY_SIZE(entry->alias));
fromwire(pptr, max, entry->color, ARRAY_SIZE(entry->color));
return entry;
}
void towire_gossip_getnodes_entry(u8 **pptr,
const struct gossip_getnodes_entry *entry)
{
towire_node_id(pptr, &entry->nodeid);
towire_u64(pptr, entry->last_timestamp);
if (entry->last_timestamp < 0)
return;
towire_u16(pptr, tal_count(entry->globalfeatures));
towire_u8_array(pptr, entry->globalfeatures,
tal_count(entry->globalfeatures));
towire_u8(pptr, tal_count(entry->addresses));
for (size_t i = 0; i < tal_count(entry->addresses); i++) {
towire_wireaddr(pptr, &entry->addresses[i]);
}
towire(pptr, entry->alias, ARRAY_SIZE(entry->alias));
towire(pptr, entry->color, ARRAY_SIZE(entry->color));
}
void fromwire_route_hop(const u8 **pptr, size_t *max, struct route_hop *entry)
{
fromwire_node_id(pptr, max, &entry->nodeid);
fromwire_short_channel_id(pptr, max, &entry->channel_id);
entry->direction = fromwire_u8(pptr, max);
entry->amount = fromwire_amount_msat(pptr, max);
entry->delay = fromwire_u32(pptr, max);
}
void towire_route_hop(u8 **pptr, const struct route_hop *entry)
{
towire_node_id(pptr, &entry->nodeid);
towire_short_channel_id(pptr, &entry->channel_id);
towire_u8(pptr, entry->direction);
towire_amount_msat(pptr, entry->amount);
towire_u32(pptr, entry->delay);
}
void fromwire_route_info(const u8 **pptr, size_t *max, struct route_info *entry)
{
fromwire_node_id(pptr, max, &entry->pubkey);
fromwire_short_channel_id(pptr, max, &entry->short_channel_id);
entry->fee_base_msat = fromwire_u32(pptr, max);
entry->fee_proportional_millionths = fromwire_u32(pptr, max);
entry->cltv_expiry_delta = fromwire_u16(pptr, max);
}
void towire_route_info(u8 **pptr, const struct route_info *entry)
{
towire_node_id(pptr, &entry->pubkey);
towire_short_channel_id(pptr, &entry->short_channel_id);
towire_u32(pptr, entry->fee_base_msat);
towire_u32(pptr, entry->fee_proportional_millionths);
towire_u16(pptr, entry->cltv_expiry_delta);
}
static void fromwire_gossip_halfchannel_entry(const u8 **pptr, size_t *max,
struct gossip_halfchannel_entry *entry)
{
entry->message_flags = fromwire_u8(pptr, max);
entry->channel_flags = fromwire_u8(pptr, max);
entry->last_update_timestamp = fromwire_u32(pptr, max);
entry->delay = fromwire_u32(pptr, max);
entry->base_fee_msat = fromwire_u32(pptr, max);
entry->fee_per_millionth = fromwire_u32(pptr, max);
entry->min = fromwire_amount_msat(pptr, max);
entry->max = fromwire_amount_msat(pptr, max);
}
struct gossip_getchannels_entry *
fromwire_gossip_getchannels_entry(const tal_t *ctx,
const u8 **pptr, size_t *max)
{
struct gossip_getchannels_entry *entry;
entry= tal(ctx, struct gossip_getchannels_entry);
fromwire_node_id(pptr, max, &entry->node[0]);
fromwire_node_id(pptr, max, &entry->node[1]);
entry->sat = fromwire_amount_sat(pptr, max);
fromwire_short_channel_id(pptr, max, &entry->short_channel_id);
entry->public = fromwire_bool(pptr, max);
entry->local_disabled = fromwire_bool(pptr, max);
if (fromwire_bool(pptr, max)) {
entry->e[0] = tal(entry, struct gossip_halfchannel_entry);
fromwire_gossip_halfchannel_entry(pptr, max, entry->e[0]);
} else
entry->e[0] = NULL;
if (fromwire_bool(pptr, max)) {
entry->e[1] = tal(entry, struct gossip_halfchannel_entry);
fromwire_gossip_halfchannel_entry(pptr, max, entry->e[1]);
} else
entry->e[1] = NULL;
return entry;
}
static void towire_gossip_halfchannel_entry(u8 **pptr,
const struct gossip_halfchannel_entry *entry)
{
towire_u8(pptr, entry->message_flags);
towire_u8(pptr, entry->channel_flags);
towire_u32(pptr, entry->last_update_timestamp);
towire_u32(pptr, entry->delay);
towire_u32(pptr, entry->base_fee_msat);
towire_u32(pptr, entry->fee_per_millionth);
towire_amount_msat(pptr, entry->min);
towire_amount_msat(pptr, entry->max);
}
void towire_gossip_getchannels_entry(u8 **pptr,
const struct gossip_getchannels_entry *entry)
{
towire_node_id(pptr, &entry->node[0]);
towire_node_id(pptr, &entry->node[1]);
towire_amount_sat(pptr, entry->sat);
towire_short_channel_id(pptr, &entry->short_channel_id);
towire_bool(pptr, entry->public);
towire_bool(pptr, entry->local_disabled);
if (entry->e[0]) {
towire_bool(pptr, true);
towire_gossip_halfchannel_entry(pptr, entry->e[0]);
} else
towire_bool(pptr, false);
if (entry->e[1]) {
towire_bool(pptr, true);
towire_gossip_halfchannel_entry(pptr, entry->e[1]);
} else
towire_bool(pptr, false);
}
struct peer_features *
fromwire_peer_features(const tal_t *ctx, const u8 **pptr, size_t *max)
{
struct peer_features *pf = tal(ctx, struct peer_features);
size_t len;
len = fromwire_u16(pptr, max);
pf->localfeatures = tal_arr(pf, u8, len);
fromwire_u8_array(pptr, max, pf->localfeatures, len);
len = fromwire_u16(pptr, max);
pf->globalfeatures = tal_arr(pf, u8, len);
fromwire_u8_array(pptr, max, pf->globalfeatures, len);
return pf;
}
void towire_peer_features(u8 **pptr, const struct peer_features *pf)
{
towire_u16(pptr, tal_count(pf->localfeatures));
towire_u8_array(pptr, pf->localfeatures, tal_count(pf->localfeatures));
towire_u16(pptr, tal_count(pf->globalfeatures));
towire_u8_array(pptr, pf->globalfeatures, tal_count(pf->globalfeatures));
}