forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoin_mvt.c
394 lines (350 loc) · 10.9 KB
/
coin_mvt.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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
#include <assert.h>
#include <ccan/ccan/cast/cast.h>
#include <ccan/tal/str/str.h>
#include <common/coin_mvt.h>
#include <common/type_to_string.h>
static const char *mvt_types[] = { "chain_mvt", "channel_mvt" };
const char *mvt_type_str(enum mvt_type type)
{
return mvt_types[type];
}
static const char *mvt_tags[] = {
"deposit",
"withdrawal",
"chain_fees",
"penalty",
"invoice",
"routed",
"journal_entry",
"onchain_htlc",
"pushed",
"spend_track",
};
const char *mvt_tag_str(enum mvt_tag tag)
{
return mvt_tags[tag];
}
struct channel_coin_mvt *new_channel_coin_mvt(const tal_t *ctx,
struct bitcoin_txid *funding_txid,
u32 funding_outnum,
struct sha256 payment_hash,
u64 *part_id,
struct amount_msat amount,
enum mvt_tag tag,
bool is_credit)
{
struct channel_coin_mvt *mvt = tal(ctx, struct channel_coin_mvt);
derive_channel_id(&mvt->chan_id, funding_txid, funding_outnum);
mvt->payment_hash = tal_dup(mvt, struct sha256, &payment_hash);
mvt->part_id = part_id;
mvt->tag = tag;
if (is_credit) {
mvt->credit = amount;
mvt->debit = AMOUNT_MSAT(0);
} else {
mvt->debit = amount;
mvt->credit = AMOUNT_MSAT(0);
}
return mvt;
}
static struct chain_coin_mvt *new_chain_coin_mvt(const tal_t *ctx,
const char *account_name,
const struct bitcoin_txid *tx_txid,
const struct bitcoin_txid *output_txid,
u32 vout,
const struct sha256 *payment_hash TAKES,
u32 blockheight, enum mvt_tag tag,
struct amount_msat amount,
bool is_credit)
{
struct chain_coin_mvt *mvt = tal(ctx, struct chain_coin_mvt);
if (account_name)
mvt->account_name = tal_strndup(mvt, account_name,
strlen(account_name));
else
mvt->account_name = NULL;
mvt->tx_txid = tx_txid;
mvt->output_txid = output_txid;
mvt->vout = vout;
/* for htlc's that are filled onchain, we also have a
* preimage, NULL otherwise */
if (payment_hash)
mvt->payment_hash = tal_dup(mvt, struct sha256, payment_hash);
else
mvt->payment_hash = NULL;
mvt->blockheight = blockheight;
mvt->tag = tag;
if (is_credit) {
mvt->credit = amount;
mvt->debit = AMOUNT_MSAT(0);
} else {
mvt->debit = amount;
mvt->credit = AMOUNT_MSAT(0);
}
return mvt;
}
static struct chain_coin_mvt *new_chain_coin_mvt_sat(const tal_t *ctx,
const char *account_name,
const struct bitcoin_txid *tx_txid,
const struct bitcoin_txid *output_txid,
u32 vout,
const struct sha256 *payment_hash TAKES,
u32 blockheight, enum mvt_tag tag,
struct amount_sat amt_sat,
bool is_credit)
{
struct amount_msat amt_msat;
bool ok;
ok = amount_sat_to_msat(&amt_msat, amt_sat);
assert(ok);
return new_chain_coin_mvt(ctx, account_name, tx_txid,
output_txid, vout, payment_hash,
blockheight, tag, amt_msat, is_credit);
}
struct chain_coin_mvt *new_coin_withdrawal(const tal_t *ctx,
const char *account_name,
const struct bitcoin_txid *tx_txid,
const struct bitcoin_txid *out_txid,
u32 vout,
u32 blockheight,
struct amount_msat amount)
{
return new_chain_coin_mvt(ctx, account_name, tx_txid,
out_txid, vout, NULL, blockheight,
WITHDRAWAL, amount, false);
}
struct chain_coin_mvt *new_coin_withdrawal_sat(const tal_t *ctx,
const char *account_name,
const struct bitcoin_txid *tx_txid,
const struct bitcoin_txid *out_txid,
u32 vout,
u32 blockheight,
struct amount_sat amount)
{
struct amount_msat amt_msat;
bool ok;
ok = amount_sat_to_msat(&amt_msat, amount);
assert(ok);
return new_coin_withdrawal(ctx, account_name, tx_txid, out_txid, vout,
blockheight, amt_msat);
}
struct chain_coin_mvt *new_coin_chain_fees(const tal_t *ctx,
const char *account_name,
const struct bitcoin_txid *tx_txid,
u32 blockheight,
struct amount_msat amount)
{
return new_chain_coin_mvt(ctx, account_name, tx_txid,
NULL, 0, NULL, blockheight,
CHAIN_FEES, amount, false);
}
struct chain_coin_mvt *new_coin_chain_fees_sat(const tal_t *ctx,
const char *account_name,
const struct bitcoin_txid *tx_txid,
u32 blockheight,
struct amount_sat amount)
{
struct amount_msat amt_msat;
bool ok;
ok = amount_sat_to_msat(&amt_msat, amount);
assert(ok);
return new_coin_chain_fees(ctx, account_name, tx_txid,
blockheight, amt_msat);
}
struct chain_coin_mvt *new_coin_journal_entry(const tal_t *ctx,
const char *account_name,
const struct bitcoin_txid *txid,
const struct bitcoin_txid *out_txid,
u32 vout,
u32 blockheight,
struct amount_msat amount,
bool is_credit)
{
return new_chain_coin_mvt(ctx, account_name, txid,
out_txid, vout, NULL,
blockheight, JOURNAL,
amount, is_credit);
}
struct chain_coin_mvt *new_coin_deposit(const tal_t *ctx,
const char *account_name,
const struct bitcoin_txid *txid,
u32 vout, u32 blockheight,
struct amount_msat amount)
{
return new_chain_coin_mvt(ctx, account_name, txid, txid,
vout, NULL, blockheight, DEPOSIT,
amount, true);
}
struct chain_coin_mvt *new_coin_deposit_sat(const tal_t *ctx,
const char *account_name,
const struct bitcoin_txid *txid,
u32 vout,
u32 blockheight,
struct amount_sat amount)
{
struct amount_msat amt_msat;
bool ok;
ok = amount_sat_to_msat(&amt_msat, amount);
assert(ok);
return new_coin_deposit(ctx, account_name, txid,
vout, blockheight, amt_msat);
}
struct chain_coin_mvt *new_coin_penalty_sat(const tal_t *ctx,
const char *account_name,
const struct bitcoin_txid *txid,
const struct bitcoin_txid *out_txid,
u32 vout,
u32 blockheight,
struct amount_sat amount)
{
struct amount_msat amt_msat;
bool ok;
ok = amount_sat_to_msat(&amt_msat, amount);
assert(ok);
return new_chain_coin_mvt(ctx, account_name,
txid, out_txid,
vout, NULL,
blockheight, PENALTY,
amt_msat, false);
}
struct chain_coin_mvt *new_coin_onchain_htlc_sat(const tal_t *ctx,
const char *account_name,
const struct bitcoin_txid *txid,
const struct bitcoin_txid *out_txid,
u32 vout,
struct sha256 payment_hash,
u32 blockheight,
struct amount_sat amount,
bool is_credit)
{
return new_chain_coin_mvt_sat(ctx, account_name,
txid, out_txid, vout,
take(tal_dup(NULL, struct sha256,
&payment_hash)), blockheight,
ONCHAIN_HTLC, amount, is_credit);
}
struct chain_coin_mvt *new_coin_pushed(const tal_t *ctx,
const char *account_name,
const struct bitcoin_txid *txid,
u32 blockheight,
struct amount_msat amount)
{
return new_chain_coin_mvt(ctx, account_name, txid, NULL, 0,
NULL, blockheight, PUSHED, amount,
false);
}
struct chain_coin_mvt *new_coin_spend_track(const tal_t *ctx,
const struct bitcoin_txid *txid,
const struct bitcoin_txid *out_txid,
u32 vout, u32 blockheight)
{
return new_chain_coin_mvt_sat(ctx, "wallet", txid, out_txid, vout,
NULL, blockheight, SPEND_TRACK, AMOUNT_SAT(0),
false);
}
struct coin_mvt *finalize_chain_mvt(const tal_t *ctx,
const struct chain_coin_mvt *chain_mvt,
const char *bip173_name,
u32 timestamp,
struct node_id *node_id,
s64 count)
{
struct coin_mvt *mvt = tal(ctx, struct coin_mvt);
mvt->account_id = tal_strndup(mvt, chain_mvt->account_name,
strlen(chain_mvt->account_name));
mvt->bip173_name = tal_strndup(mvt, bip173_name, strlen(bip173_name));
mvt->type = CHAIN_MVT;
mvt->id.tx_txid = chain_mvt->tx_txid;
mvt->id.output_txid = chain_mvt->output_txid;
mvt->id.vout = chain_mvt->vout;
mvt->id.payment_hash = chain_mvt->payment_hash;
mvt->tag = chain_mvt->tag;
mvt->credit = chain_mvt->credit;
mvt->debit = chain_mvt->debit;
mvt->timestamp = timestamp;
mvt->blockheight = chain_mvt->blockheight;
mvt->version = COIN_MVT_VERSION;
mvt->node_id = node_id;
mvt->counter = count;
return mvt;
}
struct coin_mvt *finalize_channel_mvt(const tal_t *ctx,
const struct channel_coin_mvt *chan_mvt,
const char *bip173_name,
u32 timestamp, struct node_id *node_id,
s64 count)
{
struct coin_mvt *mvt = tal(ctx, struct coin_mvt);
mvt->account_id = type_to_string(mvt, struct channel_id,
&chan_mvt->chan_id);
mvt->bip173_name = tal_strndup(mvt, bip173_name, strlen(bip173_name));
mvt->type = CHANNEL_MVT;
mvt->id.payment_hash = chan_mvt->payment_hash;
mvt->id.part_id = chan_mvt->part_id;
mvt->id.tx_txid = NULL;
mvt->id.output_txid = NULL;
mvt->id.vout = 0;
mvt->tag = chan_mvt->tag;
mvt->credit = chan_mvt->credit;
mvt->debit = chan_mvt->debit;
mvt->timestamp = timestamp;
/* channel movements don't have a blockheight */
mvt->blockheight = 0;
mvt->version = COIN_MVT_VERSION;
mvt->node_id = node_id;
mvt->counter = count;
return mvt;
}
void towire_chain_coin_mvt(u8 **pptr, const struct chain_coin_mvt *mvt)
{
if (mvt->account_name) {
towire_u16(pptr, strlen(mvt->account_name));
towire_u8_array(pptr, (u8 *)mvt->account_name, strlen(mvt->account_name));
} else
towire_u16(pptr, 0);
towire_bitcoin_txid(pptr, cast_const(struct bitcoin_txid *, mvt->tx_txid));
if (mvt->output_txid) {
towire_bool(pptr, true);
towire_bitcoin_txid(pptr, cast_const(struct bitcoin_txid *, mvt->output_txid));
} else
towire_bool(pptr, false);
towire_u32(pptr, mvt->vout);
if (mvt->payment_hash) {
towire_bool(pptr, true);
towire_sha256(pptr, mvt->payment_hash);
} else
towire_bool(pptr, false);
towire_u32(pptr, mvt->blockheight);
towire_u8(pptr, mvt->tag);
towire_amount_msat(pptr, mvt->credit);
towire_amount_msat(pptr, mvt->debit);
}
void fromwire_chain_coin_mvt(const u8 **cursor, size_t *max, struct chain_coin_mvt *mvt)
{
u16 account_name_len;
account_name_len = fromwire_u16(cursor, max);
if (account_name_len) {
mvt->account_name = tal_arr(mvt, char, account_name_len);
fromwire_u8_array(cursor, max, (u8 *)mvt->account_name, account_name_len);
} else
mvt->account_name = NULL;
mvt->tx_txid = tal(mvt, struct bitcoin_txid);
fromwire_bitcoin_txid(cursor, max,
cast_const(struct bitcoin_txid *, mvt->tx_txid));
if (fromwire_bool(cursor, max)) {
mvt->output_txid = tal(mvt, struct bitcoin_txid);
fromwire_bitcoin_txid(cursor, max,
cast_const(struct bitcoin_txid *, mvt->output_txid));
} else
mvt->output_txid = NULL;
mvt->vout = fromwire_u32(cursor, max);
if (fromwire_bool(cursor, max)) {
mvt->payment_hash = tal(mvt, struct sha256);
fromwire_sha256(cursor, max, mvt->payment_hash);
} else
mvt->payment_hash = NULL;
mvt->blockheight = fromwire_u32(cursor, max);
mvt->tag = fromwire_u8(cursor, max);
mvt->credit = fromwire_amount_msat(cursor, max);
mvt->debit = fromwire_amount_msat(cursor, max);
}