coin_moves: add an 'originating_account' field

If a coin move concerns an external account, it's really useful to know
which 'internal' account initiated the transfer.

We're about to add a notification for withdrawals, so we can use this to
track wallet pushes to outside addresses

Changelog-Added: JSONRPC: `coin_movement` to 'external' accounts now include an 'originating_account' field
This commit is contained in:
niftynei 2022-01-24 12:58:39 -06:00 committed by Rusty Russell
parent cf2c4c5dde
commit ff4ae8b5f4
5 changed files with 44 additions and 0 deletions

View File

@ -102,6 +102,7 @@ static struct chain_coin_mvt *new_chain_coin_mvt(const tal_t *ctx,
mvt->tx_txid = tx_txid;
mvt->outpoint = outpoint;
mvt->originating_acct = NULL;
/* for htlc's that are filled onchain, we also have a
* preimage, NULL otherwise */
@ -267,6 +268,11 @@ struct chain_coin_mvt *new_coin_external_deposit(const tal_t *ctx,
AMOUNT_MSAT(0), true, amount);
}
bool chain_mvt_is_external(const struct chain_coin_mvt *mvt)
{
return streq(mvt->account_name, EXTERNAL);
}
struct chain_coin_mvt *new_coin_wallet_deposit(const tal_t *ctx,
const struct bitcoin_outpoint *outpoint,
u32 blockheight,
@ -327,6 +333,11 @@ struct coin_mvt *finalize_chain_mvt(const tal_t *ctx,
struct coin_mvt *mvt = tal(ctx, struct coin_mvt);
mvt->account_id = tal_strdup(mvt, chain_mvt->account_name);
if (chain_mvt->originating_acct)
mvt->originating_acct =
tal_strdup(mvt, chain_mvt->originating_acct);
else
mvt->originating_acct = NULL;
mvt->hrp_name = tal_strdup(mvt, hrp_name);
mvt->type = CHAIN_MVT;
@ -359,6 +370,8 @@ struct coin_mvt *finalize_channel_mvt(const tal_t *ctx,
mvt->account_id = type_to_string(mvt, struct channel_id,
&chan_mvt->chan_id);
/* channel moves don't have external events! */
mvt->originating_acct = NULL;
mvt->hrp_name = tal_strdup(mvt, hrp_name);
mvt->type = CHANNEL_MVT;
mvt->id.payment_hash = chan_mvt->payment_hash;
@ -388,6 +401,12 @@ void towire_chain_coin_mvt(u8 **pptr, const struct chain_coin_mvt *mvt)
} else
towire_bool(pptr, false);
if (mvt->originating_acct) {
towire_bool(pptr, true);
towire_wirestring(pptr, mvt->originating_acct);
} else
towire_bool(pptr, false);
towire_bitcoin_outpoint(pptr, mvt->outpoint);
if (mvt->tx_txid) {
@ -419,6 +438,11 @@ void fromwire_chain_coin_mvt(const u8 **cursor, size_t *max, struct chain_coin_m
} else
mvt->account_name = NULL;
if (fromwire_bool(cursor, max)) {
mvt->originating_acct = fromwire_wirestring(mvt, cursor, max);
} else
mvt->originating_acct = NULL;
/* Read into non-const version */
struct bitcoin_outpoint *outpoint
= tal(mvt, struct bitcoin_outpoint);

View File

@ -83,6 +83,10 @@ struct chain_coin_mvt {
/* total value of output (useful for tracking external outs) */
struct amount_sat output_val;
/* When we pay to external accounts, it's useful
* to track which internal account it originated from */
const char *originating_acct;
};
/* differs depending on type!? */
@ -97,6 +101,9 @@ struct coin_mvt {
/* name of 'account': wallet, external, <channel_id> */
const char *account_id;
/* if account_id is external, the account this 'impacted' */
const char *originating_acct;
/* Chain name: BIP 173, except signet lightning-style: tbs not tb */
const char *hrp_name;
@ -249,6 +256,9 @@ struct coin_mvt *finalize_channel_mvt(const tal_t *ctx,
const struct node_id *node_id)
NON_NULL_ARGS(2, 3, 5);
/* Is this an xternal account? */
bool chain_mvt_is_external(const struct chain_coin_mvt *mvt);
const char *mvt_type_str(enum mvt_type type);
const char *mvt_tag_str(enum mvt_tag tag);

View File

@ -701,6 +701,7 @@ i.e. only definitively resolved HTLCs or confirmed bitcoin transactions.
"node_id":"03a7103a2322b811f7369cbb27fb213d30bbc0b012082fed3cad7e4498da2dc56b",
"type":"chain_mvt",
"account_id":"wallet",
"originating_account": "wallet", // (`chain_mvt` only, optional)
"txid":"0159693d8f3876b4def468b208712c630309381e9d106a9836fa0a9571a28722", // (`chain_mvt` only, optional)
"utxo_txid":"0159693d8f3876b4def468b208712c630309381e9d106a9836fa0a9571a28722", // (`chain_mvt` only)
"vout":1, // (`chain_mvt` only)
@ -731,6 +732,9 @@ notification adheres to.
`account_id` is the name of this account. The node's wallet is named 'wallet',
all channel funds' account are the channel id.
`originating_account` is the account that this movement originated from.
*Only* tagged on external events (deposits/withdrawals to an external party).
`txid` is the transaction id of the bitcoin transaction that triggered this
ledger event. `utxo_txid` and `vout` identify the bitcoin output which triggered
this notification. (`chain_mvt` only). Notifications tagged

View File

@ -470,6 +470,9 @@ static void coin_movement_notification_serialize(struct json_stream *stream,
json_add_node_id(stream, "node_id", mvt->node_id);
json_add_string(stream, "type", mvt_type_str(mvt->type));
json_add_string(stream, "account_id", mvt->account_id);
if (mvt->originating_acct)
json_add_string(stream, "originating_account",
mvt->originating_acct);
json_mvt_id(stream, mvt->type, &mvt->id);
json_add_amount_msat_only(stream, "credit", mvt->credit);
json_add_amount_msat_only(stream, "debit", mvt->debit);

View File

@ -265,6 +265,9 @@ static void handle_onchain_log_coin_move(struct channel *channel, const u8 *msg)
if (!mvt->account_name)
mvt->account_name = type_to_string(mvt, struct channel_id,
&channel->cid);
else if (chain_mvt_is_external(mvt))
mvt->originating_acct = type_to_string(mvt, struct channel_id,
&channel->cid);
notify_chain_mvt(channel->peer->ld, mvt);
tal_free(mvt);
}