Skip to content

Commit b0231a5

Browse files
committed
common: put "external" and "wallet" strings, and test functions into common/coin_mvt.h
They're scattered and reproduced in many places: unify them. Signed-off-by: Rusty Russell <[email protected]>
1 parent 841a8bd commit b0231a5

File tree

12 files changed

+71
-73
lines changed

12 files changed

+71
-73
lines changed

common/coin_mvt.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
#include <common/node_id.h>
99
#include <wire/wire.h>
1010

11-
#define EXTERNAL "external"
12-
1311
static const char *mvt_tags[] = {
1412
"deposit",
1513
"withdrawal",
@@ -368,7 +366,7 @@ struct chain_coin_mvt *new_onchain_htlc_withdraw(const tal_t *ctx,
368366
{
369367
/* An onchain htlc fulfillment to peer is a *deposit* of
370368
* that output into their (external) account */
371-
return new_chain_coin_mvt_sat(ctx, NULL, EXTERNAL, NULL,
369+
return new_chain_coin_mvt_sat(ctx, NULL, ACCOUNT_NAME_EXTERNAL, NULL,
372370
outpoint, payment_hash,
373371
blockheight,
374372
tag_to_mvt_tags(MVT_HTLC_FULFILL),
@@ -382,7 +380,7 @@ struct chain_coin_mvt *new_coin_external_spend(const tal_t *ctx,
382380
struct amount_sat amount,
383381
struct mvt_tags tags)
384382
{
385-
return new_chain_coin_mvt(ctx, NULL, EXTERNAL,
383+
return new_chain_coin_mvt(ctx, NULL, ACCOUNT_NAME_EXTERNAL,
386384
time_now().ts.tv_sec, txid,
387385
outpoint, NULL, blockheight,
388386
tags,
@@ -395,14 +393,14 @@ struct chain_coin_mvt *new_coin_external_deposit(const tal_t *ctx,
395393
struct amount_sat amount,
396394
struct mvt_tags tags)
397395
{
398-
return new_chain_coin_mvt_sat(ctx, NULL, EXTERNAL, NULL, outpoint, NULL,
396+
return new_chain_coin_mvt_sat(ctx, NULL, ACCOUNT_NAME_EXTERNAL, NULL, outpoint, NULL,
399397
blockheight, tags,
400398
COIN_CREDIT, amount);
401399
}
402400

403401
bool chain_mvt_is_external(const struct chain_coin_mvt *mvt)
404402
{
405-
return mvt->account.alt_account && streq(mvt->account.alt_account, EXTERNAL);
403+
return mvt->account.alt_account && is_external_account(mvt->account.alt_account);
406404
}
407405

408406
struct chain_coin_mvt *new_coin_wallet_deposit(const tal_t *ctx,
@@ -411,7 +409,7 @@ struct chain_coin_mvt *new_coin_wallet_deposit(const tal_t *ctx,
411409
struct amount_sat amount,
412410
struct mvt_tags tags)
413411
{
414-
return new_chain_coin_mvt_sat(ctx, NULL, WALLET, NULL,
412+
return new_chain_coin_mvt_sat(ctx, NULL, ACCOUNT_NAME_WALLET, NULL,
415413
outpoint, NULL,
416414
blockheight, tags,
417415
COIN_CREDIT, amount);
@@ -424,7 +422,7 @@ struct chain_coin_mvt *new_coin_wallet_withdraw(const tal_t *ctx,
424422
struct amount_sat amount,
425423
struct mvt_tags tags)
426424
{
427-
return new_chain_coin_mvt_sat(ctx, NULL, WALLET, spend_txid,
425+
return new_chain_coin_mvt_sat(ctx, NULL, ACCOUNT_NAME_WALLET, spend_txid,
428426
outpoint, NULL,
429427
blockheight, tags,
430428
COIN_DEBIT, amount);

common/coin_mvt.h

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
#include <common/utils.h>
99

1010
#define COIN_MVT_VERSION 2
11-
#define WALLET "wallet"
11+
#define ACCOUNT_NAME_WALLET "wallet"
12+
#define ACCOUNT_NAME_EXTERNAL "external"
1213

1314
enum mvt_tag {
1415
MVT_DEPOSIT = 0,
@@ -251,6 +252,28 @@ struct channel_coin_mvt *new_coin_channel_push(const tal_t *ctx,
251252
struct mvt_tags tags)
252253
NON_NULL_ARGS(2);
253254

255+
/* There are three standard accounts:
256+
* "wallet" for our internal wallet,
257+
* "external" for other bitcoin sources,
258+
* <channelid> for lightning channels.
259+
*
260+
* Exactly one of these is true:
261+
*/
262+
static inline bool is_wallet_account(const char *acctname)
263+
{
264+
return streq(acctname, ACCOUNT_NAME_WALLET);
265+
}
266+
267+
static inline bool is_external_account(const char *acctname)
268+
{
269+
return streq(acctname, ACCOUNT_NAME_EXTERNAL);
270+
}
271+
272+
static inline bool is_channel_account(const char *acctname)
273+
{
274+
return !is_wallet_account(acctname) && !is_external_account(acctname);
275+
}
276+
254277
/* Is this an xternal account? */
255278
bool chain_mvt_is_external(const struct chain_coin_mvt *mvt);
256279

lightningd/coin_mvts.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ void send_account_balance_snapshot(struct lightningd *ld)
102102
snap->accts = tal_arr(snap, struct account_balance *, 1);
103103
bal = tal(snap, struct account_balance);
104104
bal->balance = AMOUNT_MSAT(0);
105-
bal->acct_id = WALLET;
105+
bal->acct_id = ACCOUNT_NAME_WALLET;
106106
bal->bip173_name = chainparams->lightning_hrp;
107107

108108
utxos = wallet_get_unspent_utxos(NULL, ld->wallet);

plugins/bkpr/account.c

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ struct account *new_account(const tal_t *ctx,
1414

1515
a->name = tal_strdup(a, name);
1616
a->peer_id = peer_id;
17-
a->is_wallet = streq(a->name, WALLET);
17+
a->is_wallet = is_wallet_account(a->name);
1818
a->we_opened = false;
1919
a->leased = false;
2020
a->onchain_resolved_block = 0;
@@ -24,14 +24,3 @@ struct account *new_account(const tal_t *ctx,
2424

2525
return a;
2626
}
27-
28-
bool is_channel_account(const struct account *acct)
29-
{
30-
return !streq(acct->name, WALLET)
31-
&& !streq(acct->name, "external");
32-
}
33-
34-
bool is_external_account(const struct account *acct)
35-
{
36-
return streq(acct->name, "external");
37-
}

plugins/bkpr/account.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,4 @@ struct account *new_account(const tal_t *ctx,
4444
const char *name STEALS,
4545
struct node_id *peer_id);
4646

47-
/* Is this a channel account? */
48-
bool is_channel_account(const struct account *acct);
49-
/* is this the 'external' account */
50-
bool is_external_account(const struct account *acct);
5147
#endif /* LIGHTNING_PLUGINS_BKPR_ACCOUNT_H */

plugins/bkpr/bookkeeper.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,7 @@ static struct command_result *json_inspect(struct command *cmd,
239239
NULL))
240240
return command_param_failed();
241241

242-
if (streq(acct_name, WALLET_ACCT)
243-
|| streq(acct_name, EXTERNAL_ACCT))
242+
if (!is_channel_account(acct_name))
244243
return command_fail(cmd, PLUGIN_ERROR,
245244
"`inspect` not supported for"
246245
" non-channel accounts");
@@ -298,7 +297,7 @@ static struct command_result *json_inspect(struct command *cmd,
298297
continue;
299298
} else if (pr->txo->acct_db_id != acct->db_id
300299
/* We make an exception for wallet events */
301-
&& !streq(pr->txo->acct_name, WALLET_ACCT))
300+
&& !is_wallet_account(pr->txo->acct_name))
302301
continue;
303302
} else if (pr->spend
304303
&& pr->spend->acct_db_id != acct->db_id)
@@ -585,7 +584,7 @@ static struct command_result *json_list_balances(struct command *cmd,
585584

586585
/* Skip the external acct balance, it's effectively
587586
* meaningless */
588-
if (streq(accts[i]->name, EXTERNAL_ACCT))
587+
if (is_external_account(accts[i]->name))
589588
continue;
590589

591590
/* Add it to the result data */
@@ -1009,9 +1008,9 @@ static char *do_account_close_checks(const tal_t *ctx,
10091008
db_begin_transaction(db);
10101009

10111010
/* If is an external acct event, might be close channel related */
1012-
if (!is_channel_account(acct) && e->origin_acct) {
1011+
if (!is_channel_account(acct->name) && e->origin_acct) {
10131012
closed_acct = find_account(ctx, db, e->origin_acct);
1014-
} else if (!is_channel_account(acct) && !e->spending_txid)
1013+
} else if (!is_channel_account(acct->name) && !e->spending_txid)
10151014
closed_acct = find_close_account(ctx, db, &e->outpoint.txid);
10161015
else
10171016
/* Get most up to date account entry */
@@ -1154,7 +1153,7 @@ static struct command_result *json_balance_snapshot(struct command *cmd,
11541153
/* If we're entering a channel account,
11551154
* from a balance entry, we need to
11561155
* go find the channel open info*/
1157-
if (!existed && is_channel_account(acct)) {
1156+
if (!existed && is_channel_account(acct->name)) {
11581157
struct new_account_info *info;
11591158
u64 timestamp_now;
11601159

@@ -1630,8 +1629,8 @@ parse_and_log_chain_move(struct command *cmd,
16301629
/* If this is a channel account event, it's possible
16311630
* that we *never* got the open event. (This happens
16321631
* if you add the plugin *after* you've closed the channel) */
1633-
if ((!acct->open_event_db_id && is_channel_account(acct))
1634-
|| (orig_acct && is_channel_account(orig_acct)
1632+
if ((!acct->open_event_db_id && is_channel_account(acct->name))
1633+
|| (orig_acct && is_channel_account(orig_acct->name)
16351634
&& !orig_acct->open_event_db_id)) {
16361635
/* Find the channel open info for this peer */
16371636
struct out_req *req;
@@ -1645,7 +1644,7 @@ parse_and_log_chain_move(struct command *cmd,
16451644
info = tal(cmd, struct event_info);
16461645
info->ev = tal_steal(info, e);
16471646
info->acct = tal_steal(info,
1648-
is_channel_account(acct) ?
1647+
is_channel_account(acct->name) ?
16491648
acct : orig_acct);
16501649

16511650
req = jsonrpc_request_start(cmd,

plugins/bkpr/channelsapy.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <ccan/array_size/array_size.h>
44
#include <ccan/asort/asort.h>
55
#include <ccan/tal/str/str.h>
6+
#include <common/coin_mvt.h>
67
#include <common/json_stream.h>
78
#include <common/lease_rates.h>
89
#include <db/bindings.h>
@@ -171,7 +172,7 @@ struct channel_apy **compute_channel_apys(const tal_t *ctx, struct db *db,
171172
bool ok;
172173

173174
if (!acct || acct->db_id != ev->acct_db_id) {
174-
if (acct && is_channel_account(acct)) {
175+
if (acct && is_channel_account(acct->name)) {
175176
fillin_apy_acct_details(db, acct,
176177
current_blockheight,
177178
apy);
@@ -184,7 +185,7 @@ struct channel_apy **compute_channel_apys(const tal_t *ctx, struct db *db,
184185
}
185186

186187
/* No entry for external or wallet accts */
187-
if (!is_channel_account(acct))
188+
if (!is_channel_account(acct->name))
188189
continue;
189190

190191
/* Accumulate routing stats */
@@ -229,7 +230,7 @@ struct channel_apy **compute_channel_apys(const tal_t *ctx, struct db *db,
229230
* relevant fee data attached to them */
230231
}
231232

232-
if (acct && is_channel_account(acct)) {
233+
if (acct && is_channel_account(acct->name)) {
233234
fillin_apy_acct_details(db, acct,
234235
current_blockheight,
235236
apy);

plugins/bkpr/incomestmt.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ static struct income_event *maybe_chain_income(const tal_t *ctx,
122122
struct chain_event *ev)
123123
{
124124
if (streq(ev->tag, "htlc_fulfill")) {
125-
if (streq(ev->acct_name, EXTERNAL_ACCT))
125+
if (is_external_account(ev->acct_name))
126126
/* Swap the credit/debit as it went to external */
127127
return chain_to_income(ctx, ev,
128128
ev->origin_acct,
@@ -151,7 +151,7 @@ static struct income_event *maybe_chain_income(const tal_t *ctx,
151151
struct db_stmt *stmt;
152152

153153
/* deposit to external is cost to us */
154-
if (streq(ev->acct_name, EXTERNAL_ACCT)) {
154+
if (is_external_account(ev->acct_name)) {
155155
struct income_event *iev;
156156

157157
/* External deposits w/o a blockheight
@@ -674,7 +674,7 @@ static char *income_event_harmony_type(const struct income_event *ev)
674674
return "fee:network";
675675

676676
if (!amount_msat_is_zero(ev->credit)) {
677-
if (streq(WALLET_ACCT, ev->acct_name))
677+
if (is_wallet_account(ev->acct_name))
678678
return tal_fmt(ev, "transfer:%s", ev->tag);
679679

680680
return tal_fmt(ev, "income:%s", ev->tag);
@@ -684,7 +684,7 @@ static char *income_event_harmony_type(const struct income_event *ev)
684684
if (streq("penalty", ev->tag)) {
685685
return "loss:penalty";
686686
}
687-
if (streq(WALLET_ACCT, ev->acct_name))
687+
if (is_wallet_account(ev->acct_name))
688688
return tal_fmt(ev, "transfer:%s", ev->tag);
689689

690690
/* FIXME: add "fee:transfer" to invoice routing fees */

plugins/bkpr/recorder.c

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -466,12 +466,6 @@ static struct txo_set *find_txo_set(const tal_t *ctx,
466466
return txos;
467467
}
468468

469-
static bool is_channel_acct(struct chain_event *ev)
470-
{
471-
return !streq(ev->acct_name, WALLET_ACCT)
472-
&& !streq(ev->acct_name, EXTERNAL_ACCT);
473-
}
474-
475469
static bool txid_in_list(struct bitcoin_txid **list,
476470
struct bitcoin_txid *txid)
477471
{
@@ -527,7 +521,7 @@ bool find_txo_chain(const tal_t *ctx,
527521

528522
/* Has this been resolved? */
529523
if ((pr->txo
530-
&& is_channel_acct(pr->txo))
524+
&& is_channel_account(pr->txo->acct_name))
531525
&& !pr->spend)
532526
is_complete = false;
533527

@@ -1734,7 +1728,7 @@ char *update_channel_onchain_fees(const tal_t *ctx,
17341728
- anchors (already exlc from output)
17351729
- to_external (if !htlc_fulfill)
17361730
*/
1737-
if (is_channel_acct(ev)
1731+
if (is_channel_account(ev->acct_name)
17381732
&& streq("htlc_fulfill", ev->tag))
17391733
continue;
17401734

@@ -1745,7 +1739,7 @@ char *update_channel_onchain_fees(const tal_t *ctx,
17451739
* the peer's account (external),
17461740
* except for fulfilled htlcs (which originated
17471741
* in our balance) */
1748-
if (streq(ev->acct_name, EXTERNAL_ACCT)
1742+
if (is_external_account(ev->acct_name)
17491743
&& !streq("htlc_fulfill", ev->tag))
17501744
continue;
17511745

@@ -1954,8 +1948,8 @@ char *maybe_update_onchain_fees(const tal_t *ctx, struct db *db,
19541948

19551949
/* Find all the deposits/withdrawals for this txid */
19561950
events = find_chain_events_bytxid(inner_ctx, db, txid);
1957-
wallet_id = find_acct_id(db, WALLET_ACCT);
1958-
extern_id = find_acct_id(db, EXTERNAL_ACCT);
1951+
wallet_id = find_acct_id(db, ACCOUNT_NAME_WALLET);
1952+
extern_id = find_acct_id(db, ACCOUNT_NAME_EXTERNAL);
19591953

19601954
/* If we don't even have two events, skip */
19611955
if (tal_count(events) < 2)
@@ -2129,7 +2123,7 @@ void maybe_closeout_external_deposits(struct db *db,
21292123
/* Blockheight for unconfirmeds is zero */
21302124
db_bind_int(stmt, 0);
21312125
db_bind_txid(stmt, txid);
2132-
db_bind_text(stmt, EXTERNAL_ACCT);
2126+
db_bind_text(stmt, ACCOUNT_NAME_EXTERNAL);
21332127
db_query_prepared(stmt);
21342128

21352129
while (db_step(stmt)) {

plugins/bkpr/recorder.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ struct db;
1212
enum mvt_tag;
1313
struct onchain_fee;
1414

15-
#define EXTERNAL_ACCT "external"
16-
#define WALLET_ACCT WALLET
1715
#define SQLITE_MAX_UINT 0x7FFFFFFFFFFFFFFF
1816

1917
struct acct_balance {

0 commit comments

Comments
 (0)