forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
htlc_wire.c
162 lines (143 loc) · 4.38 KB
/
htlc_wire.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
#include <bitcoin/tx.h>
#include <ccan/array_size/array_size.h>
#include <ccan/crypto/shachain/shachain.h>
#include <common/htlc_wire.h>
#include <common/memleak.h>
#include <wire/wire.h>
/* FIXME: We could adapt tools/generate-wire.py to generate structures
* and code like this. */
void towire_added_htlc(u8 **pptr, const struct added_htlc *added)
{
towire_u64(pptr, added->id);
towire_u64(pptr, added->amount_msat);
towire_sha256(pptr, &added->payment_hash);
towire_u32(pptr, added->cltv_expiry);
towire(pptr, added->onion_routing_packet,
sizeof(added->onion_routing_packet));
}
void towire_fulfilled_htlc(u8 **pptr, const struct fulfilled_htlc *fulfilled)
{
towire_u64(pptr, fulfilled->id);
towire_preimage(pptr, &fulfilled->payment_preimage);
}
void towire_failed_htlc(u8 **pptr, const struct failed_htlc *failed)
{
/* Only one can be set. */
assert(failed->failcode || tal_count(failed->failreason));
assert(!failed->failcode || !tal_count(failed->failreason));
towire_u64(pptr, failed->id);
towire_u16(pptr, failed->failcode);
if (failed->failcode & UPDATE) {
assert(!failed->failreason);
towire_short_channel_id(pptr, failed->scid);
} else {
assert(!failed->scid);
if (!failed->failcode) {
assert(failed->failreason);
towire_u16(pptr, tal_count(failed->failreason));
towire_u8_array(pptr, failed->failreason,
tal_count(failed->failreason));
}
}
}
void towire_htlc_state(u8 **pptr, const enum htlc_state hstate)
{
towire_u8(pptr, hstate);
}
void towire_changed_htlc(u8 **pptr, const struct changed_htlc *changed)
{
towire_htlc_state(pptr, changed->newstate);
towire_u64(pptr, changed->id);
}
void towire_side(u8 **pptr, const enum side side)
{
towire_u8(pptr, side);
}
void towire_shachain(u8 **pptr, const struct shachain *shachain)
{
size_t i;
towire_u64(pptr, shachain->min_index);
towire_u32(pptr, shachain->num_valid);
for (i = 0; i < shachain->num_valid; i++) {
towire_u64(pptr, shachain->known[i].index);
towire_sha256(pptr, &shachain->known[i].hash);
}
}
void fromwire_added_htlc(const u8 **cursor, size_t *max,
struct added_htlc *added)
{
added->id = fromwire_u64(cursor, max);
added->amount_msat = fromwire_u64(cursor, max);
fromwire_sha256(cursor, max, &added->payment_hash);
added->cltv_expiry = fromwire_u32(cursor, max);
fromwire(cursor, max, added->onion_routing_packet,
sizeof(added->onion_routing_packet));
}
void fromwire_fulfilled_htlc(const u8 **cursor, size_t *max,
struct fulfilled_htlc *fulfilled)
{
fulfilled->id = fromwire_u64(cursor, max);
fromwire_preimage(cursor, max, &fulfilled->payment_preimage);
}
struct failed_htlc *fromwire_failed_htlc(const tal_t *ctx, const u8 **cursor, size_t *max)
{
struct failed_htlc *failed = tal(ctx, struct failed_htlc);
failed->id = fromwire_u64(cursor, max);
failed->failcode = fromwire_u16(cursor, max);
if (failed->failcode == 0) {
u16 failreason_len;
failed->scid = NULL;
failreason_len = fromwire_u16(cursor, max);
failed->failreason = tal_arr(failed, u8, failreason_len);
fromwire_u8_array(cursor, max, failed->failreason,
failreason_len);
} else {
failed->failreason = NULL;
if (failed->failcode & UPDATE) {
failed->scid = tal(failed, struct short_channel_id);
fromwire_short_channel_id(cursor, max, failed->scid);
} else {
failed->scid = NULL;
}
}
return failed;
}
enum htlc_state fromwire_htlc_state(const u8 **cursor, size_t *max)
{
enum htlc_state hstate = fromwire_u8(cursor, max);
if (hstate >= HTLC_STATE_INVALID) {
hstate = HTLC_STATE_INVALID;
fromwire_fail(cursor, max);
}
return hstate;
}
void fromwire_changed_htlc(const u8 **cursor, size_t *max,
struct changed_htlc *changed)
{
changed->newstate = fromwire_htlc_state(cursor, max);
changed->id = fromwire_u64(cursor, max);
}
enum side fromwire_side(const u8 **cursor, size_t *max)
{
enum side side = fromwire_u8(cursor, max);
if (side >= NUM_SIDES) {
side = NUM_SIDES;
fromwire_fail(cursor, max);
}
return side;
}
void fromwire_shachain(const u8 **cursor, size_t *max,
struct shachain *shachain)
{
size_t i;
shachain->min_index = fromwire_u64(cursor, max);
shachain->num_valid = fromwire_u32(cursor, max);
if (shachain->num_valid > ARRAY_SIZE(shachain->known)) {
fromwire_fail(cursor, max);
return;
}
for (i = 0; i < shachain->num_valid; i++) {
shachain->known[i].index = fromwire_u64(cursor, max);
fromwire_sha256(cursor, max, &shachain->known[i].hash);
}
}