forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate_types.h
293 lines (264 loc) · 8.63 KB
/
state_types.h
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#ifndef LIGHTNING_STATE_TYPES_H
#define LIGHTNING_STATE_TYPES_H
#include "config.h"
/* FIXME: cdump is really dumb, so we put these in their own header. */
#include "lightning.pb-c.h"
#define STATE_CLOSE_HTLCS_BIT 1
#define STATE_CLOSE_STEAL_BIT 2
#define STATE_CLOSE_SPENDTHEM_BIT 4
#define STATE_CLOSE_CLOSE_BIT 8
#define STATE_CLOSE_OURCOMMIT_BIT 16
#define STATE_CLOSE_SPENDOURS_BIT 32
enum state {
STATE_INIT,
/*
* Opening.
*/
STATE_OPEN_WAIT_FOR_OPEN_NOANCHOR,
STATE_OPEN_WAIT_FOR_OPEN_WITHANCHOR,
STATE_OPEN_WAIT_FOR_ANCHOR_CREATE,
STATE_OPEN_WAIT_FOR_ANCHOR,
STATE_OPEN_WAIT_FOR_COMMIT_SIG,
STATE_OPEN_WAITING_OURANCHOR,
STATE_OPEN_WAITING_THEIRANCHOR,
STATE_OPEN_WAITING_OURANCHOR_THEYCOMPLETED,
STATE_OPEN_WAITING_THEIRANCHOR_THEYCOMPLETED,
STATE_OPEN_WAIT_FOR_COMPLETE_OURANCHOR,
STATE_OPEN_WAIT_FOR_COMPLETE_THEIRANCHOR,
/*
* Normal state.
*/
STATE_NORMAL,
STATE_NORMAL_COMMITTING,
/*
* Closing.
*/
/* We told them to clear. */
STATE_US_CLEARING,
/* They told us to clear, or acked our CLEARING. */
STATE_BOTH_CLEARING,
/* We're cleared, waiting for close signature / negotiation */
STATE_WAIT_FOR_CLOSE_SIG,
/* All closed. */
STATE_CLOSED,
/* Just waiting for HTLCs to resolve. */
STATE_CLOSE_WAIT_HTLCS,
/*
* They can broadcast one or more revoked commit tx, or their latest
* commit tx at any time. We respond to revoked commit txs by stealing
* their funds (steal). We respond to their latest commit tx by
* spending (spend_them). They can also (with our help) broadcast
* a mutual close tx (mutual_close).
*
* We can also broadcast one of the following:
* 1) Our latest commit tx (our_commit).
* 2) After delay has passed, spend of our tx (spend_ours).
* 3) Mutual close tx (mutual_close), already covered above.
*
* Thus, we could be waiting for the following combinations:
* - steal
* - spend_them
* - steal + spend_them
* - mutual_close
* - steal + mutual_close
* - spend_them + mutual_close
* - steal + spend_them + mutual_close
*
* - our_commit
* - steal + our_commit
* - spend_them + our_commit
* - steal + spend_them + our_commit
* - mutual_close + our_commit
* - steal + mutual_close + our_commit
* - spend_them + mutual_close + our_commit
* - steal + spend_them + mutual_close + our_commit
*
* - spend_ours
* - steal + spend_ours
* - spend_them + spend_ours
* - steal + spend_them + spend_ours
* - mutual_close + spend_ours
* - steal + mutual_close + spend_ours
* - spend_them + mutual_close + spend_ours
* - steal + spend_them + mutual_close + spend_ours
*
* Each of these has with-HTLC and without-HTLC variants, except:
*
* 1) We never agree to close with HTLCs,
* 2) We don't care about htlcs if we steal (we steal all outputs).
*
* Now, it is possible for us to CLOSE and them to have an HTLC,
* because we could close partway through negotiation. So, any
* commit tx they publish could introduce HTLCs.
*
* Thus, HTLC variants are only possible with SPENDTHEM, OR
* OURCOMMIT/SPENDOURS, and only no CLOSE (since CLOSE implies no HTLCs).
*/
STATE_CLOSE_WAIT_STEAL,
STATE_UNUSED_CLOSE_WAIT_STEAL_WITH_HTLCS,
STATE_CLOSE_WAIT_SPENDTHEM,
STATE_CLOSE_WAIT_SPENDTHEM_WITH_HTLCS,
STATE_CLOSE_WAIT_STEAL_SPENDTHEM,
STATE_CLOSE_WAIT_STEAL_SPENDTHEM_WITH_HTLCS,
STATE_CLOSE_WAIT_CLOSE,
STATE_UNUSED_CLOSE_WAIT_CLOSE_WITH_HTLCS,
STATE_CLOSE_WAIT_STEAL_CLOSE,
STATE_UNUSED_CLOSE_WAIT_STEAL_CLOSE_WITH_HTLCS,
STATE_CLOSE_WAIT_SPENDTHEM_CLOSE,
STATE_CLOSE_WAIT_SPENDTHEM_CLOSE_WITH_HTLCS,
STATE_CLOSE_WAIT_STEAL_SPENDTHEM_CLOSE,
STATE_CLOSE_WAIT_STEAL_SPENDTHEM_CLOSE_WITH_HTLCS,
STATE_CLOSE_WAIT_OURCOMMIT,
STATE_CLOSE_WAIT_OURCOMMIT_WITH_HTLCS,
STATE_CLOSE_WAIT_STEAL_OURCOMMIT,
STATE_CLOSE_WAIT_STEAL_OURCOMMIT_WITH_HTLCS,
STATE_CLOSE_WAIT_SPENDTHEM_OURCOMMIT,
STATE_CLOSE_WAIT_SPENDTHEM_OURCOMMIT_WITH_HTLCS,
STATE_CLOSE_WAIT_STEAL_SPENDTHEM_OURCOMMIT,
STATE_CLOSE_WAIT_STEAL_SPENDTHEM_OURCOMMIT_WITH_HTLCS,
STATE_CLOSE_WAIT_CLOSE_OURCOMMIT,
STATE_UNUSED_CLOSE_WAIT_CLOSE_OURCOMMIT_WITH_HTLCS,
STATE_CLOSE_WAIT_STEAL_CLOSE_OURCOMMIT,
STATE_UNUSED_CLOSE_WAIT_STEAL_CLOSE_OURCOMMIT_WITH_HTLCS,
STATE_CLOSE_WAIT_SPENDTHEM_CLOSE_OURCOMMIT,
STATE_CLOSE_WAIT_SPENDTHEM_CLOSE_OURCOMMIT_WITH_HTLCS,
STATE_CLOSE_WAIT_STEAL_SPENDTHEM_CLOSE_OURCOMMIT,
STATE_CLOSE_WAIT_STEAL_SPENDTHEM_CLOSE_OURCOMMIT_WITH_HTLCS,
STATE_CLOSE_WAIT_SPENDOURS,
STATE_CLOSE_WAIT_SPENDOURS_WITH_HTLCS,
STATE_CLOSE_WAIT_STEAL_SPENDOURS,
STATE_CLOSE_WAIT_STEAL_SPENDOURS_WITH_HTLCS,
STATE_CLOSE_WAIT_SPENDTHEM_SPENDOURS,
STATE_CLOSE_WAIT_SPENDTHEM_SPENDOURS_WITH_HTLCS,
STATE_CLOSE_WAIT_STEAL_SPENDTHEM_SPENDOURS,
STATE_CLOSE_WAIT_STEAL_SPENDTHEM_SPENDOURS_WITH_HTLCS,
STATE_CLOSE_WAIT_CLOSE_SPENDOURS,
STATE_UNUSED_CLOSE_WAIT_CLOSE_SPENDOURS_WITH_HTLCS,
STATE_CLOSE_WAIT_STEAL_CLOSE_SPENDOURS,
STATE_UNUSED_CLOSE_WAIT_STEAL_CLOSE_SPENDOURS_WITH_HTLCS,
STATE_CLOSE_WAIT_SPENDTHEM_CLOSE_SPENDOURS,
STATE_CLOSE_WAIT_SPENDTHEM_CLOSE_SPENDOURS_WITH_HTLCS,
STATE_CLOSE_WAIT_STEAL_SPENDTHEM_CLOSE_SPENDOURS,
STATE_CLOSE_WAIT_STEAL_SPENDTHEM_CLOSE_SPENDOURS_WITH_HTLCS,
/*
* Where angels fear to tread.
*/
/* Their anchor didn't reach blockchain in reasonable time. */
STATE_ERR_ANCHOR_TIMEOUT,
/* Anchor was double-spent, after both considered it sufficient depth. */
STATE_ERR_ANCHOR_LOST,
/* A commitment tx we didn't recognise spent the anchor (impossible) */
STATE_ERR_INFORMATION_LEAK,
/* We ended up in an unexpected state. */
STATE_ERR_INTERNAL,
STATE_MAX
};
enum state_input {
/*
* Packet inputs.
*/
PKT_OPEN = PKT__PKT_OPEN,
PKT_OPEN_ANCHOR = PKT__PKT_OPEN_ANCHOR,
PKT_OPEN_COMMIT_SIG = PKT__PKT_OPEN_COMMIT_SIG,
PKT_OPEN_COMPLETE = PKT__PKT_OPEN_COMPLETE,
/* Updating the commit transaction: new HTLC */
PKT_UPDATE_ADD_HTLC = PKT__PKT_UPDATE_ADD_HTLC,
/* Updating the commit transaction: I have your R value! */
PKT_UPDATE_FULFILL_HTLC = PKT__PKT_UPDATE_FULFILL_HTLC,
/* Updating the commit transaction: your HTLC failed upstream */
PKT_UPDATE_FAIL_HTLC = PKT__PKT_UPDATE_FAIL_HTLC,
/* Committing updates */
PKT_UPDATE_COMMIT = PKT__PKT_UPDATE_COMMIT,
PKT_UPDATE_REVOCATION = PKT__PKT_UPDATE_REVOCATION,
/* Mutual close sequence. */
PKT_CLOSE_CLEARING = PKT__PKT_CLOSE_CLEARING,
PKT_CLOSE_SIGNATURE = PKT__PKT_CLOSE_SIGNATURE,
/* Something unexpected went wrong. */
PKT_ERROR = PKT__PKT_ERROR,
/*
* Non-packet inputs.
*/
INPUT_NONE,
/*
* Bitcoin events
*/
/* Bitcoin anchor tx created. */
BITCOIN_ANCHOR_CREATED,
/* It reached the required depth. */
BITCOIN_ANCHOR_DEPTHOK,
/* It didn't reach the required depth in time. */
BITCOIN_ANCHOR_TIMEOUT,
/* It reached the required depth, then was forked off. */
BITCOIN_ANCHOR_UNSPENT,
/* Anchor was spent by our commit, and we can now spend it. */
BITCOIN_ANCHOR_OURCOMMIT_DELAYPASSED,
/* Anchor was spent by their commit tx. */
BITCOIN_ANCHOR_THEIRSPEND,
/* Anchor was spent by another commit tx (eg. expired). */
BITCOIN_ANCHOR_OTHERSPEND,
/* They spent an HTLC to them (revealing R value). */
BITCOIN_HTLC_TOTHEM_SPENT,
/* HTLC to them timed out, we can get funds now. */
BITCOIN_HTLC_TOTHEM_TIMEOUT,
/* HTLC to us timed out. */
BITCOIN_HTLC_TOUS_TIMEOUT,
/* Our spend of their commit tx is completely buried. */
BITCOIN_SPEND_THEIRS_DONE,
/* Our spend of our own tx is completely buried. */
BITCOIN_SPEND_OURS_DONE,
/* Our spend of their revoked tx is completely buried. */
BITCOIN_STEAL_DONE,
/* Bitcoin close transaction considered completely buried. */
BITCOIN_CLOSE_DONE,
/* Our HTLC spend is completely buried. */
BITCOIN_HTLC_FULFILL_SPEND_DONE,
/* Our HTLC refund spend has is completely buried. */
BITCOIN_HTLC_RETURN_SPEND_DONE,
/* We are not watching any HTLCs any more. */
INPUT_NO_MORE_HTLCS,
/* No more HTLCs in either commitment tx. */
INPUT_HTLCS_CLEARED,
/*
* Timeouts.
*/
INPUT_CLOSE_COMPLETE_TIMEOUT,
/*
* Inject a known R value.
*
* In normal operation, use CMD_SEND_HTLC_FULFILL; this is for
* after a unilateral close.
*/
INPUT_RVALUE,
/* Commands */
CMD_OPEN_WITH_ANCHOR,
CMD_OPEN_WITHOUT_ANCHOR,
CMD_SEND_HTLC_ADD,
CMD_SEND_HTLC_FULFILL,
CMD_SEND_HTLC_FAIL,
CMD_SEND_COMMIT,
CMD_CLOSE,
/* Connection lost/timedout with other node. */
INPUT_CONNECTION_LOST,
INPUT_MAX
};
enum state_peercond {
/* Ready to accept a new command. */
PEER_CMD_OK,
/* Don't send me commands, I'm busy. */
PEER_BUSY,
/* No more commands, I'm closing. */
PEER_CLOSING,
/* No more packets, I'm closed. */
PEER_CLOSED
};
enum command_status {
/* Nothing changed. */
CMD_NONE,
/* Command succeeded. */
CMD_SUCCESS,
/* HTLC-command needs re-issuing (theirs takes preference) */
CMD_REQUEUE,
/* Failed. */
CMD_FAIL
};
#endif /* LIGHTNING_STATE_TYPES_H */