common/coin_mvt: use enum rather than true/false for credit/debit.
What changed, and why it matters
This commit is a straightforward code cleanup: it replaces a true/false flag with a named enum (COIN_CREDIT/COIN_DEBIT) to make the code easier to read. It does not change any behavior, fix a bug, or address a security issue. The author deliberately reordered function arguments so any missed callers would fail to compile, ensuring the change is complete.
No security action needed. Treat as a normal maintainability refactor.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch refactors common/coin_mvt.c and common/coin_mvt.h to introduce enum coin_mvt_dir { COIN_CREDIT = 1, COIN_DEBIT = 2 } and use it in place of the bool is_credit parameter across new_channel_coin_mvt(), new_chain_coin_mvt(), new_chain_coin_mvt_sat(), and new_coin_channel_push(). All existing call sites in lightningd/channel_control.c and lightningd/coin_mvts.c are updated to pass COIN_CREDIT or COIN_DEBIT in place of true/false, preserving the original semantics. The switch statement includes an abort() default to catch unexpected enum values. No logic or security boundary is altered.
Changed components
common/coin_mvt.ccommon/coin_mvt.hlightningd/channel_control.clightningd/coin_mvts.cInspect captured patch +65 / −52
diff --git a/common/coin_mvt.c b/common/coin_mvt.c
index d1f97322..22b642a5 100644
--- a/common/coin_mvt.c
+++ b/common/coin_mvt.c
@@ -53,9 +53,9 @@ struct channel_coin_mvt *new_channel_coin_mvt(const tal_t *ctx,
const struct sha256 *payment_hash TAKES,
const u64 *part_id,
const u64 *group_id,
+ enum coin_mvt_dir direction,
struct amount_msat amount,
const enum mvt_tag *tags TAKES,
- bool is_credit,
struct amount_msat fees)
{
struct channel_coin_mvt *mvt = tal(ctx, struct channel_coin_mvt);
@@ -73,17 +73,19 @@ struct channel_coin_mvt *new_channel_coin_mvt(const tal_t *ctx,
mvt->tags = tal_dup_talarr(mvt, enum mvt_tag, tags);
- if (is_credit) {
+ mvt->fees = fees;
+ switch (direction) {
+ case COIN_CREDIT:
mvt->credit = amount;
mvt->debit = AMOUNT_MSAT(0);
- } else {
+ return mvt;
+ case COIN_DEBIT:
mvt->debit = amount;
mvt->credit = AMOUNT_MSAT(0);
+ return mvt;
}
- mvt->fees = fees;
-
- return mvt;
+ abort();
}
static struct chain_coin_mvt *new_chain_coin_mvt(const tal_t *ctx,
@@ -93,8 +95,8 @@ static struct chain_coin_mvt *new_chain_coin_mvt(const tal_t *ctx,
const struct sha256 *payment_hash TAKES,
u32 blockheight,
enum mvt_tag *tags,
+ enum coin_mvt_dir direction,
struct amount_msat amount,
- bool is_credit,
struct amount_sat output_val,
u32 out_count)
{
@@ -115,18 +117,20 @@ static struct chain_coin_mvt *new_chain_coin_mvt(const tal_t *ctx,
mvt->tags = tal_dup_talarr(mvt, enum mvt_tag, tags);
- if (is_credit) {
+ mvt->output_val = output_val;
+ mvt->output_count = out_count;
+
+ switch (direction) {
+ case COIN_CREDIT:
mvt->credit = amount;
mvt->debit = AMOUNT_MSAT(0);
- } else {
+ return mvt;
+ case COIN_DEBIT:
mvt->debit = amount;
mvt->credit = AMOUNT_MSAT(0);
+ return mvt;
}
-
- mvt->output_val = output_val;
- mvt->output_count = out_count;
-
- return mvt;
+ abort();
}
static struct chain_coin_mvt *new_chain_coin_mvt_sat(const tal_t *ctx,
@@ -136,8 +140,8 @@ static struct chain_coin_mvt *new_chain_coin_mvt_sat(const tal_t *ctx,
const struct sha256 *payment_hash TAKES,
u32 blockheight,
enum mvt_tag *tags TAKES,
- struct amount_sat amt_sat,
- bool is_credit)
+ enum coin_mvt_dir direction,
+ struct amount_sat amt_sat)
{
struct amount_msat amt_msat;
bool ok;
@@ -146,7 +150,7 @@ static struct chain_coin_mvt *new_chain_coin_mvt_sat(const tal_t *ctx,
return new_chain_coin_mvt(ctx, account_name, tx_txid,
outpoint, payment_hash,
- blockheight, tags, amt_msat, is_credit,
+ blockheight, tags, direction, amt_msat,
/* All amounts that are sat are
* on-chain output values */
amt_sat, 0);
@@ -163,7 +167,7 @@ struct chain_coin_mvt *new_onchaind_withdraw(const tal_t *ctx,
outpoint, NULL,
blockheight,
take(new_tag_arr(NULL, tag)),
- amount, false);
+ COIN_DEBIT, amount);
}
struct chain_coin_mvt *new_onchaind_deposit(const tal_t *ctx,
@@ -176,7 +180,7 @@ struct chain_coin_mvt *new_onchaind_deposit(const tal_t *ctx,
outpoint, NULL,
blockheight,
take(new_tag_arr(NULL, tag)),
- amount, true);
+ COIN_CREDIT, amount);
}
struct chain_coin_mvt *new_coin_channel_close(const tal_t *ctx,
@@ -196,11 +200,11 @@ struct chain_coin_mvt *new_coin_channel_close(const tal_t *ctx,
tal_arr_expand(&tags, SPLICE);
mvt = new_chain_coin_mvt(ctx, NULL, txid,
- out, NULL, blockheight,
- take(tags),
- amount, false,
- output_val,
- output_count);
+ out, NULL, blockheight,
+ take(tags),
+ COIN_DEBIT, amount,
+ output_val,
+ output_count);
if (chan_id)
mvt->account_name = fmt_channel_id(mvt, chan_id);
@@ -220,7 +224,7 @@ struct chain_coin_mvt *new_coin_channel_open_proposed(const tal_t *ctx,
mvt = new_chain_coin_mvt(ctx, NULL, NULL, out, NULL, 0,
take(new_tag_arr(NULL, CHANNEL_PROPOSED)),
- amount, true, output_val, 0);
+ COIN_CREDIT, amount, output_val, 0);
mvt->account_name = fmt_channel_id(mvt, chan_id);
mvt->peer_id = tal_dup(mvt, struct node_id, peer_id);
@@ -247,8 +251,9 @@ struct chain_coin_mvt *new_coin_channel_open(const tal_t *ctx,
struct chain_coin_mvt *mvt;
mvt = new_chain_coin_mvt(ctx, NULL, NULL, out, NULL, blockheight,
- take(new_tag_arr(NULL, CHANNEL_OPEN)), amount,
- true, output_val, 0);
+ take(new_tag_arr(NULL, CHANNEL_OPEN)),
+ COIN_CREDIT, amount,
+ output_val, 0);
mvt->account_name = fmt_channel_id(mvt, chan_id);
mvt->peer_id = tal_dup(mvt, struct node_id, peer_id);
@@ -272,7 +277,7 @@ struct chain_coin_mvt *new_onchain_htlc_deposit(const tal_t *ctx,
outpoint, payment_hash,
blockheight,
take(new_tag_arr(NULL, HTLC_FULFILL)),
- amount, true);
+ COIN_CREDIT, amount);
}
@@ -288,7 +293,7 @@ struct chain_coin_mvt *new_onchain_htlc_withdraw(const tal_t *ctx,
outpoint, payment_hash,
blockheight,
take(new_tag_arr(NULL, HTLC_FULFILL)),
- amount, true);
+ COIN_CREDIT, amount);
}
struct chain_coin_mvt *new_coin_external_spend_tags(const tal_t *ctx,
@@ -301,7 +306,7 @@ struct chain_coin_mvt *new_coin_external_spend_tags(const tal_t *ctx,
return new_chain_coin_mvt(ctx, EXTERNAL, txid,
outpoint, NULL, blockheight,
take(tags),
- AMOUNT_MSAT(0), true, amount, 0);
+ COIN_CREDIT, AMOUNT_MSAT(0), amount, 0);
}
struct chain_coin_mvt *new_coin_external_spend(const tal_t *ctx,
@@ -324,7 +329,7 @@ struct chain_coin_mvt *new_coin_external_deposit_tags(const tal_t *ctx,
{
return new_chain_coin_mvt_sat(ctx, EXTERNAL, NULL, outpoint, NULL,
blockheight, take(tags),
- amount, true);
+ COIN_CREDIT, amount);
}
@@ -336,7 +341,7 @@ struct chain_coin_mvt *new_coin_external_deposit(const tal_t *ctx,
{
return new_chain_coin_mvt_sat(ctx, EXTERNAL, NULL, outpoint, NULL,
blockheight, take(new_tag_arr(NULL, tag)),
- amount, true);
+ COIN_CREDIT, amount);
}
bool chain_mvt_is_external(const struct chain_coin_mvt *mvt)
@@ -353,7 +358,7 @@ struct chain_coin_mvt *new_coin_wallet_deposit(const tal_t *ctx,
return new_chain_coin_mvt_sat(ctx, WALLET, NULL,
outpoint, NULL,
blockheight, take(new_tag_arr(NULL, tag)),
- amount, true);
+ COIN_CREDIT, amount);
}
struct chain_coin_mvt *new_coin_wallet_deposit_tagged(const tal_t *ctx,
@@ -366,7 +371,7 @@ struct chain_coin_mvt *new_coin_wallet_deposit_tagged(const tal_t *ctx,
outpoint, NULL,
blockheight,
take(tags),
- amount, true);
+ COIN_CREDIT, amount);
}
struct chain_coin_mvt *new_coin_wallet_withdraw(const tal_t *ctx,
@@ -379,18 +384,18 @@ struct chain_coin_mvt *new_coin_wallet_withdraw(const tal_t *ctx,
return new_chain_coin_mvt_sat(ctx, WALLET, spend_txid,
outpoint, NULL,
blockheight, take(new_tag_arr(NULL, tag)),
- amount, false);
+ COIN_DEBIT, amount);
}
struct channel_coin_mvt *new_coin_channel_push(const tal_t *ctx,
const struct channel_id *cid,
+ enum coin_mvt_dir direction,
struct amount_msat amount,
- enum mvt_tag tag,
- bool is_credit)
+ enum mvt_tag tag)
{
return new_channel_coin_mvt(ctx, cid, NULL,
- NULL, NULL, amount,
- take(new_tag_arr(NULL, tag)), is_credit,
+ NULL, NULL, direction, amount,
+ take(new_tag_arr(NULL, tag)),
AMOUNT_MSAT(0));
}
diff --git a/common/coin_mvt.h b/common/coin_mvt.h
index 474a9678..aa696844 100644
--- a/common/coin_mvt.h
+++ b/common/coin_mvt.h
@@ -37,6 +37,11 @@ enum mvt_tag {
SPLICE = 23,
};
+enum coin_mvt_dir {
+ COIN_CREDIT = 1,
+ COIN_DEBIT = 2,
+};
+
struct channel_coin_mvt_id {
/* multi-part payments may share a payment hash,
* so we should also record part-id and group-id for them */
@@ -109,9 +114,9 @@ struct channel_coin_mvt *new_channel_coin_mvt(const tal_t *ctx,
const struct sha256 *payment_hash TAKES,
const u64 *part_id,
const u64 *group_id,
+ enum coin_mvt_dir direction,
struct amount_msat amount,
const enum mvt_tag *tags TAKES,
- bool is_credit,
struct amount_msat fees)
NON_NULL_ARGS(2);
@@ -230,9 +235,9 @@ struct chain_coin_mvt *new_coin_external_deposit(const tal_t *ctx,
struct channel_coin_mvt *new_coin_channel_push(const tal_t *ctx,
const struct channel_id *cid,
+ enum coin_mvt_dir direction,
struct amount_msat amount,
- enum mvt_tag tag,
- bool is_credit)
+ enum mvt_tag tag)
NON_NULL_ARGS(2);
/* Is this an xternal account? */
diff --git a/lightningd/channel_control.c b/lightningd/channel_control.c
index 3e341e9d..22386574 100644
--- a/lightningd/channel_control.c
+++ b/lightningd/channel_control.c
@@ -1026,9 +1026,9 @@ void channel_record_open(struct channel *channel, u32 blockheight, bool record_p
if (is_pushed && record_push)
notify_channel_mvt(channel->peer->ld,
new_coin_channel_push(tmpctx, &channel->cid,
+ channel->opener == REMOTE ? COIN_CREDIT : COIN_DEBIT,
channel->push,
- is_leased ? LEASE_FEE : PUSHED,
- channel->opener == REMOTE));
+ is_leased ? LEASE_FEE : PUSHED));
}
void lockin_has_completed(struct channel *channel, bool record_push)
diff --git a/lightningd/coin_mvts.c b/lightningd/coin_mvts.c
index 80cd0b22..26df00b5 100644
--- a/lightningd/coin_mvts.c
+++ b/lightningd/coin_mvts.c
@@ -12,8 +12,9 @@ struct channel_coin_mvt *new_channel_mvt_invoice_hin(const tal_t *ctx,
{
return new_channel_coin_mvt(ctx, &channel->cid,
&hin->payment_hash, NULL, NULL,
- hin->msat, new_tag_arr(ctx, INVOICE),
- true, AMOUNT_MSAT(0));
+ COIN_CREDIT, hin->msat,
+ new_tag_arr(ctx, INVOICE),
+ AMOUNT_MSAT(0));
}
struct channel_coin_mvt *new_channel_mvt_routed_hin(const tal_t *ctx,
@@ -31,8 +32,9 @@ struct channel_coin_mvt *new_channel_mvt_routed_hin(const tal_t *ctx,
return new_channel_coin_mvt(ctx, &channel->cid,
&hin->payment_hash, NULL, NULL,
- hin->msat, new_tag_arr(ctx, ROUTED),
- true, fees_collected);
+ COIN_CREDIT, hin->msat,
+ new_tag_arr(ctx, ROUTED),
+ fees_collected);
}
struct channel_coin_mvt *new_channel_mvt_invoice_hout(const tal_t *ctx,
@@ -43,8 +45,9 @@ struct channel_coin_mvt *new_channel_mvt_invoice_hout(const tal_t *ctx,
&hout->payment_hash,
&hout->partid,
&hout->groupid,
- hout->msat, new_tag_arr(ctx, INVOICE),
- false, hout->fees);
+ COIN_DEBIT, hout->msat,
+ new_tag_arr(ctx, INVOICE),
+ hout->fees);
}
struct channel_coin_mvt *new_channel_mvt_routed_hout(const tal_t *ctx,
@@ -53,8 +56,8 @@ struct channel_coin_mvt *new_channel_mvt_routed_hout(const tal_t *ctx,
{
return new_channel_coin_mvt(ctx, &channel->cid,
&hout->payment_hash, NULL, NULL,
- hout->msat, new_tag_arr(ctx, ROUTED),
- false,
+ COIN_DEBIT, hout->msat,
+ new_tag_arr(ctx, ROUTED),
hout->fees);
}
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.