Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More coin_move adjustments #5043

Merged
merged 10 commits into from
Mar 5, 2022
18 changes: 18 additions & 0 deletions bitcoin/tx_parts.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,19 @@ struct tx_parts *tx_parts_from_wally_tx(const tal_t *ctx,
if (output != -1 && output != i)
continue;
txp->outputs[i] = clone_output(&wtx->outputs[i]);

/* Cheat a bit by also setting the numeric satoshi
* value, otherwise we end up converting a
* number of times */
if (chainparams->is_elements) {
struct amount_asset asset;
struct amount_sat sats;
asset = wally_tx_output_get_amount(txp->outputs[i]);
/* FIXME: non l-btc assets */
assert(amount_asset_is_main(&asset));
sats = amount_asset_to_sat(&asset);
txp->outputs[i]->satoshi = sats.satoshis; /* Raw: wally conversion */
}
}
tal_wally_end(txp);

Expand Down Expand Up @@ -281,6 +294,9 @@ static struct wally_tx_output *fromwire_wally_tx_output(const tal_t *ctx,
surjectionproof, tal_bytelen(surjectionproof),
rangeproof, tal_bytelen(rangeproof),
&out);

/* As a convenience, we sent the value over as satoshis */
out->satoshi = fromwire_u64(cursor, max);
} else {
u64 satoshi;
satoshi = fromwire_u64(cursor, max);
Expand Down Expand Up @@ -348,6 +364,8 @@ static void towire_wally_tx_output(u8 **pptr, const struct wally_tx_output *out)
out->surjectionproof_len);
towire_u32(pptr, out->rangeproof_len);
towire_u8_array(pptr, out->rangeproof, out->rangeproof_len);
/* Copy the value over, as a convenience */
towire_u64(pptr, out->satoshi);
} else {
towire_u64(pptr, out->satoshi);
}
Expand Down
23 changes: 15 additions & 8 deletions common/coin_mvt.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ static struct chain_coin_mvt *new_chain_coin_mvt(const tal_t *ctx,
enum mvt_tag *tags TAKES,
struct amount_msat amount,
bool is_credit,
struct amount_sat output_val)
struct amount_sat output_val,
u32 out_count)
{
struct chain_coin_mvt *mvt = tal(ctx, struct chain_coin_mvt);

Expand All @@ -118,7 +119,9 @@ static struct chain_coin_mvt *new_chain_coin_mvt(const tal_t *ctx,
mvt->debit = amount;
mvt->credit = AMOUNT_MSAT(0);
}

mvt->output_val = output_val;
mvt->output_count = out_count;

return mvt;
}
Expand All @@ -143,7 +146,7 @@ static struct chain_coin_mvt *new_chain_coin_mvt_sat(const tal_t *ctx,
blockheight, tags, amt_msat, is_credit,
/* All amounts that are sat are
* on-chain output values */
amt_sat);
amt_sat, 0);
}

struct chain_coin_mvt *new_onchaind_withdraw(const tal_t *ctx,
Expand Down Expand Up @@ -178,13 +181,15 @@ struct chain_coin_mvt *new_coin_channel_close(const tal_t *ctx,
const struct bitcoin_outpoint *out,
u32 blockheight,
const struct amount_msat amount,
const struct amount_sat output_val)
const struct amount_sat output_val,
u32 output_count)
{
return new_chain_coin_mvt(ctx, NULL, txid,
out, NULL, blockheight,
take(new_tag_arr(NULL, CHANNEL_CLOSE)),
amount, false,
output_val);
output_val,
output_count);
}

struct chain_coin_mvt *new_coin_channel_open(const tal_t *ctx,
Expand All @@ -200,7 +205,7 @@ struct chain_coin_mvt *new_coin_channel_open(const tal_t *ctx,

mvt = new_chain_coin_mvt(ctx, NULL, NULL, out, NULL, blockheight,
take(new_tag_arr(NULL, CHANNEL_OPEN)), amount,
true, output_val);
true, output_val, 0);
mvt->account_name = type_to_string(mvt, struct channel_id, chan_id);

/* If we're the opener, add to the tag list */
Expand Down Expand Up @@ -252,7 +257,7 @@ struct chain_coin_mvt *new_coin_external_spend(const tal_t *ctx,
return new_chain_coin_mvt(ctx, EXTERNAL, txid,
outpoint, NULL, blockheight,
take(new_tag_arr(NULL, tag)),
AMOUNT_MSAT(0), true, amount);
AMOUNT_MSAT(0), true, amount, 0);
}

struct chain_coin_mvt *new_coin_external_deposit(const tal_t *ctx,
Expand Down Expand Up @@ -334,6 +339,7 @@ struct coin_mvt *finalize_chain_mvt(const tal_t *ctx,

mvt->output_val = tal(mvt, struct amount_sat);
*mvt->output_val = chain_mvt->output_val;
mvt->output_count = chain_mvt->output_count;
mvt->fees = NULL;

mvt->timestamp = timestamp;
Expand Down Expand Up @@ -366,11 +372,10 @@ struct coin_mvt *finalize_channel_mvt(const tal_t *ctx,
mvt->credit = chan_mvt->credit;
mvt->debit = chan_mvt->debit;
mvt->output_val = NULL;
mvt->output_count = 0;
mvt->fees = tal(mvt, struct amount_msat);
*mvt->fees = chan_mvt->fees;
mvt->timestamp = timestamp;
/* channel movements don't have a blockheight */
mvt->blockheight = 0;
mvt->version = COIN_MVT_VERSION;
mvt->node_id = tal_dup(mvt, struct node_id, node_id);

Expand Down Expand Up @@ -413,6 +418,7 @@ void towire_chain_coin_mvt(u8 **pptr, const struct chain_coin_mvt *mvt)
towire_amount_msat(pptr, mvt->credit);
towire_amount_msat(pptr, mvt->debit);
towire_amount_sat(pptr, mvt->output_val);
towire_u32(pptr, mvt->output_count);
}

void fromwire_chain_coin_mvt(const u8 **cursor, size_t *max, struct chain_coin_mvt *mvt)
Expand Down Expand Up @@ -455,4 +461,5 @@ void fromwire_chain_coin_mvt(const u8 **cursor, size_t *max, struct chain_coin_m
mvt->credit = fromwire_amount_msat(cursor, max);
mvt->debit = fromwire_amount_msat(cursor, max);
mvt->output_val = fromwire_amount_sat(cursor, max);
mvt->output_count = fromwire_u32(cursor, max);
}
13 changes: 10 additions & 3 deletions common/coin_mvt.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ enum mvt_type {
CHANNEL_MVT = 1,
};

#define NUM_MVT_TAGS (LEASED + 1)
enum mvt_tag {
DEPOSIT = 0,
WITHDRAWAL = 1,
Expand Down Expand Up @@ -73,8 +74,7 @@ struct chain_coin_mvt {
/* label / tag array */
enum mvt_tag *tags;

/* block this transaction is confirmed in
* zero means it's unknown/unconfirmed */
/* block this transaction is confirmed in */
u32 blockheight;

/* only one or the other */
Expand All @@ -87,6 +87,10 @@ struct chain_coin_mvt {
/* When we pay to external accounts, it's useful
* to track which internal account it originated from */
const char *originating_acct;

/* Number of outputs in spending tx; used by the
* `channel_close` event */
u32 output_count;
};

/* differs depending on type!? */
Expand Down Expand Up @@ -123,6 +127,8 @@ struct coin_mvt {
/* Value of the output. May be different than
* our credit/debit amount, eg channel opens */
struct amount_sat *output_val;
/* Really only needed for channel closes */
size_t output_count;

/* Amount of fees collected/paid by channel mvt */
struct amount_msat *fees;
Expand Down Expand Up @@ -170,7 +176,8 @@ struct chain_coin_mvt *new_coin_channel_close(const tal_t *ctx,
const struct bitcoin_outpoint *out,
u32 blockheight,
const struct amount_msat amount,
const struct amount_sat output_val)
const struct amount_sat output_val,
u32 output_count)
NON_NULL_ARGS(2, 3);

struct chain_coin_mvt *new_coin_channel_open(const tal_t *ctx,
Expand Down
16 changes: 16 additions & 0 deletions common/json_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,22 @@ bool json_to_channel_id(const char *buffer, const jsmntok_t *tok,
cid, sizeof(*cid));
}


bool json_to_coin_mvt_tag(const char *buffer, const jsmntok_t *tok,
enum mvt_tag *tag)
{
enum mvt_tag i_tag;
for (size_t i = 0; i < NUM_MVT_TAGS; i++) {
i_tag = (enum mvt_tag) i;
if (json_tok_streq(buffer, tok, mvt_tag_str(i_tag))) {
*tag = i_tag;
return true;
}
}

return false;
}

bool split_tok(const char *buffer, const jsmntok_t *tok,
char split,
jsmntok_t *a,
Expand Down
5 changes: 5 additions & 0 deletions common/json_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#define LIGHTNING_COMMON_JSON_HELPERS_H
#include "config.h"
#include <bitcoin/tx.h>
#include <common/coin_mvt.h>
#include <common/json.h>
#include <wire/wire.h>

Expand Down Expand Up @@ -73,6 +74,10 @@ bool json_to_outpoint(const char *buffer, const jsmntok_t *tok,
bool json_to_channel_id(const char *buffer, const jsmntok_t *tok,
struct channel_id *cid);

/* Extract a coin movement 'tag' from this */
bool json_to_coin_mvt_tag(const char *buffer, const jsmntok_t *tok,
enum mvt_tag *tag);

/* Split a json token into 2 tokens given a splitting character */
bool split_tok(const char *buffer, const jsmntok_t *tok,
char split,
Expand Down
5 changes: 3 additions & 2 deletions common/test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ ALL_TEST_PROGRAMS += $(COMMON_TEST_PROGRAMS)
# Sphinx test wants to decode TLVs.
common/test/run-sphinx: wire/onion$(EXP)_wiregen.o wire/towire.o wire/fromwire.o
common/test/run-blindedpath_enctlv common/test/run-blindedpath_onion: common/base32.o common/wireaddr.o wire/onion$(EXP)_wiregen.o wire/peer$(EXP)_wiregen.o wire/towire.o wire/fromwire.o wire/tlvstream.o
common/test/run-route_blinding_test: wire/onion$(EXP)_wiregen.o wire/peer$(EXP)_wiregen.o wire/towire.o wire/fromwire.o wire/tlvstream.o common/json.o common/json_helpers.o
common/test/run-route_blinding_override_test: common/base32.o common/wireaddr.o wire/onion$(EXP)_wiregen.o wire/peer$(EXP)_wiregen.o wire/towire.o wire/fromwire.o wire/tlvstream.o common/json.o common/json_helpers.o
common/test/run-route_blinding_test: wire/onion$(EXP)_wiregen.o wire/peer$(EXP)_wiregen.o wire/towire.o wire/fromwire.o wire/tlvstream.o common/json.o common/json_helpers.o common/coin_mvt.o
common/test/run-route_blinding_override_test: common/base32.o common/wireaddr.o wire/onion$(EXP)_wiregen.o wire/peer$(EXP)_wiregen.o wire/towire.o wire/fromwire.o wire/tlvstream.o common/json.o common/json_helpers.o common/coin_mvt.o

common/test/run-param \
common/test/run-json: \
common/amount.o \
common/base32.o \
common/bigsize.o \
common/channel_id.o \
common/coin_mvt.o \
common/json.o \
common/json_stream.o \
common/lease_rates.o \
Expand Down
1 change: 1 addition & 0 deletions devtools/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ ALL_PROGRAMS += $(DEVTOOLS)
DEVTOOLS_COMMON_OBJS := \
common/amount.o \
common/autodata.o \
common/coin_mvt.o \
common/base32.o \
common/bech32.o \
common/bech32_util.o \
Expand Down
6 changes: 5 additions & 1 deletion doc/PLUGINS.md
Original file line number Diff line number Diff line change
Expand Up @@ -710,9 +710,10 @@ i.e. only definitively resolved HTLCs or confirmed bitcoin transactions.
"credit":"2000000000msat",
"debit":"0msat",
"output_value": "2000000000msat", // ('chain_mvt' only)
"output_count": 2, // ('chain_mvt' only, typically only channel closes)
"fees": "382msat", // ('channel_mvt' only)
"tags": ["deposit"],
"blockheight":102, // (May be null)
"blockheight":102, // 'chain_mvt' only
"timestamp":1585948198,
"coin_type":"bc"
}
Expand Down Expand Up @@ -757,6 +758,9 @@ multiple times. `channel_mvt` only
channel opens/closes the total output value will not necessarily correspond
to the amount that's credited/debited.

`output_count` is the total outputs to expect for a channel close. Useful
for figuring out when every onchain output for a close has been resolved.

`fees` is an HTLC annotation for the amount of fees either paid or
earned. For "invoice" tagged events, the fees are the total fees
paid to send that payment. The end amount can be found by subtracting
Expand Down
3 changes: 2 additions & 1 deletion doc/lightning-listpeers.7.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ On success, an object containing **peers** is returned. It is an array of objec
- **funding** (object, optional):
- **local_msat** (msat): Amount of channel we funded
- **remote_msat** (msat): Amount of channel they funded
- **pushed_msat** (msat): Amount pushed from opener to peer
- **to_us_msat** (msat, optional): how much of channel is owed to us
- **min_to_us_msat** (msat, optional): least amount owed to us ever
- **max_to_us_msat** (msat, optional): most amount owed to us ever
Expand Down Expand Up @@ -377,4 +378,4 @@ Main web site: <https://github.com/ElementsProject/lightning> Lightning
RFC site (BOLT \#9):
<https://github.com/lightningnetwork/lightning-rfc/blob/master/09-features.md>

[comment]: # ( SHA256STAMP:d45de73a968bcc3e7fb699f0acd7a27a4c70d9e9fded8af8c684a71fe012f1ce)
[comment]: # ( SHA256STAMP:001e3cf495571bb09fe29f74adde8a6e40e69ddb1169934924eaf901a1e5f3c0)
7 changes: 6 additions & 1 deletion doc/schemas/listpeers.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,8 @@
"additionalProperties": false,
"required": [
"local_msat",
"remote_msat"
"remote_msat",
"pushed_msat"
],
"properties": {
"local_msat": {
Expand All @@ -352,6 +353,10 @@
"remote_msat": {
"type": "msat",
"description": "Amount of channel they funded"
},
"pushed_msat": {
"type": "msat",
"description": "Amount pushed from opener to peer"
}
}
},
Expand Down
1 change: 1 addition & 0 deletions gossipd/test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ GOSSIPD_TEST_PROGRAMS := $(GOSSIPD_TEST_OBJS:.o=)
GOSSIPD_TEST_COMMON_OBJS := \
common/amount.o \
common/autodata.o \
common/coin_mvt.o \
common/bigsize.o \
common/blindedpath.o \
common/channel_id.o \
Expand Down
37 changes: 17 additions & 20 deletions lightningd/channel_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,26 +138,23 @@ void channel_record_open(struct channel *channel)
blockheight = short_channel_id_blocknum(channel->scid);

/* If funds were pushed, add/sub them from the starting balance */
if (is_pushed) {
if (channel->opener == LOCAL) {
if (!amount_msat_add(&start_balance,
channel->our_msat, channel->push))
fatal("Unable to add push_msat (%s) + our_msat (%s)",
type_to_string(tmpctx, struct amount_msat,
&channel->push),
type_to_string(tmpctx, struct amount_msat,
&channel->our_msat));
} else {
if (!amount_msat_sub(&start_balance,
channel->our_msat, channel->push))
fatal("Unable to sub our_msat (%s) - push (%s)",
type_to_string(tmpctx, struct amount_msat,
&channel->our_msat),
type_to_string(tmpctx, struct amount_msat,
&channel->push));
}
} else
start_balance = channel->our_msat;
if (channel->opener == LOCAL) {
if (!amount_msat_add(&start_balance,
channel->our_msat, channel->push))
fatal("Unable to add push_msat (%s) + our_msat (%s)",
type_to_string(tmpctx, struct amount_msat,
&channel->push),
type_to_string(tmpctx, struct amount_msat,
&channel->our_msat));
} else {
if (!amount_msat_sub(&start_balance,
channel->our_msat, channel->push))
fatal("Unable to sub our_msat (%s) - push (%s)",
type_to_string(tmpctx, struct amount_msat,
&channel->our_msat),
type_to_string(tmpctx, struct amount_msat,
&channel->push));
}

mvt = new_coin_channel_open(tmpctx,
&channel->cid,
Expand Down
3 changes: 3 additions & 0 deletions lightningd/coin_mvts.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ void send_account_balance_snapshot(struct lightningd *ld, u32 blockheight)
for (size_t i = 0; i < ARRAY_SIZE(utxo_states); i++) {
utxos = wallet_get_utxos(NULL, ld->wallet, utxo_states[i]);
for (size_t j = 0; j < tal_count(utxos); j++) {
/* Don't count unconfirmed utxos! */
if (!utxos[j]->spendheight && !utxos[j]->blockheight)
continue;
if (!amount_msat_add_sat(&bal->balance,
bal->balance, utxos[j]->amount))
fatal("Overflow adding node balance");
Expand Down
Loading