bkpr: add a 'consolidate-fees' flag to the income stmt

Defaults to true. This is actually what you want to see -- the fees for
an account's txid collapsed into a single entry.
This commit is contained in:
niftynei
2022-07-19 17:04:37 +09:30
committed by Rusty Russell
parent 83c6cf25d2
commit a7b7ea5d49
6 changed files with 111 additions and 33 deletions

View File

@@ -217,10 +217,47 @@ static struct income_event *maybe_channel_income(const tal_t *ctx,
return channel_to_income(ctx, ev, ev->credit, ev->debit);
}
static struct onchain_fee **find_consolidated_fees(const tal_t *ctx,
struct db *db,
struct onchain_fee **fees TAKES)
{
struct fee_sum **sums;
struct onchain_fee **updated_fees
= tal_arr(ctx, struct onchain_fee *, 0);
sums = calculate_onchain_fee_sums(ctx, db);
for (size_t i = 0; i < tal_count(sums); i++) {
/* Find the last matching feerate's data */
for (size_t j = tal_count(fees); j > 0; j--) {
struct onchain_fee *fee;
if (bitcoin_txid_eq(&fees[j - 1]->txid,
sums[i]->txid)
&& fees[j - 1]->acct_db_id == sums[i]->acct_db_id) {
fee = tal_steal(updated_fees, fees[j - 1]);
fee->credit = sums[i]->fees_paid;
fee->debit = AMOUNT_MSAT(0);
tal_arr_expand(&updated_fees, fee);
fees[j - 1] = NULL;
break;
}
}
/* It's possible there weren't any fee events
* for this txid in the time period we've selected */
}
if (taken(fees))
tal_free(fees);
return updated_fees;
}
struct income_event **list_income_events(const tal_t *ctx,
struct db *db,
u64 start_time,
u64 end_time)
u64 end_time,
bool consolidate_fees)
{
struct channel_event **channel_events;
struct chain_event **chain_events;
@@ -235,6 +272,10 @@ struct income_event **list_income_events(const tal_t *ctx,
onchain_fees = list_chain_fees_timebox(ctx, db, start_time, end_time);
accts = list_accounts(ctx, db);
if (consolidate_fees)
onchain_fees = find_consolidated_fees(ctx, db,
take(onchain_fees));
evs = tal_arr(ctx, struct income_event *, 0);
for (size_t i = 0, j = 0, k = 0;
@@ -302,9 +343,11 @@ struct income_event **list_income_events(const tal_t *ctx,
return evs;
}
struct income_event **list_income_events_all(const tal_t *ctx, struct db *db)
struct income_event **list_income_events_all(const tal_t *ctx, struct db *db,
bool consolidate_fees)
{
return list_income_events(ctx, db, 0, SQLITE_MAX_UINT);
return list_income_events(ctx, db, 0, SQLITE_MAX_UINT,
consolidate_fees);
}
void json_add_income_event(struct json_stream *out, struct income_event *ev)