Skip to content

Commit

Permalink
db: add optional column string helper, and make db_col amounts return…
Browse files Browse the repository at this point in the history
… sat/msat.

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Jul 25, 2023
1 parent adee071 commit b6d347a
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 77 deletions.
20 changes: 15 additions & 5 deletions db/bindings.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,17 @@ char *db_col_strdup(const tal_t *ctx,
return tal_strdup(ctx, (char *)stmt->db->config->column_text_fn(stmt, col));
}

char *db_col_strdup_optional(const tal_t *ctx,
struct db_stmt *stmt,
const char *colname)
{
size_t col = db_query_colnum(stmt, colname);
if (db_column_is_null(stmt, col))
return NULL;

return tal_strdup(ctx, (char *)stmt->db->config->column_text_fn(stmt, col));
}

void db_col_preimage(struct db_stmt *stmt, const char *colname,
struct preimage *preimage)
{
Expand Down Expand Up @@ -516,15 +527,14 @@ void db_col_amount_msat_or_default(struct db_stmt *stmt,
msat->millisatoshis = db_col_u64(stmt, colname); /* Raw: low level function */
}

void db_col_amount_msat(struct db_stmt *stmt, const char *colname,
struct amount_msat *msat)
struct amount_msat db_col_amount_msat(struct db_stmt *stmt, const char *colname)
{
msat->millisatoshis = db_col_u64(stmt, colname); /* Raw: low level function */
return amount_msat(db_col_u64(stmt, colname));
}

void db_col_amount_sat(struct db_stmt *stmt, const char *colname, struct amount_sat *sat)
struct amount_sat db_col_amount_sat(struct db_stmt *stmt, const char *colname)
{
sat->satoshis = db_col_u64(stmt, colname); /* Raw: low level function */
return amount_sat(db_col_u64(stmt, colname));
}

struct json_escape *db_col_json_escape(const tal_t *ctx,
Expand Down
8 changes: 6 additions & 2 deletions db/bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,13 @@ const void* db_col_blob(struct db_stmt *stmt, const char *colname);
char *db_col_strdup(const tal_t *ctx,
struct db_stmt *stmt,
const char *colname);
/* string or NULL */
char *db_col_strdup_optional(const tal_t *ctx,
struct db_stmt *stmt,
const char *colname);
void db_col_preimage(struct db_stmt *stmt, const char *colname, struct preimage *preimage);
void db_col_amount_msat(struct db_stmt *stmt, const char *colname, struct amount_msat *msat);
void db_col_amount_sat(struct db_stmt *stmt, const char *colname, struct amount_sat *sat);
struct amount_msat db_col_amount_msat(struct db_stmt *stmt, const char *colname);
struct amount_sat db_col_amount_sat(struct db_stmt *stmt, const char *colname);
struct json_escape *db_col_json_escape(const tal_t *ctx, struct db_stmt *stmt, const char *colname);
void db_col_sha256(struct db_stmt *stmt, const char *colname, struct sha256 *sha);
void db_col_sha256d(struct db_stmt *stmt, const char *colname, struct sha256_double *shad);
Expand Down
40 changes: 20 additions & 20 deletions plugins/bkpr/recorder.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ static struct chain_event *stmt2chain_event(const tal_t *ctx, struct db_stmt *st

e->tag = db_col_strdup(e, stmt, "e.tag");

db_col_amount_msat(stmt, "e.credit", &e->credit);
db_col_amount_msat(stmt, "e.debit", &e->debit);
db_col_amount_msat(stmt, "e.output_value", &e->output_value);
e->credit = db_col_amount_msat(stmt, "e.credit");
e->debit = db_col_amount_msat(stmt, "e.debit");
e->output_value = db_col_amount_msat(stmt, "e.output_value");

e->currency = db_col_strdup(e, stmt, "e.currency");
e->timestamp = db_col_u64(stmt, "e.timestamp");
Expand Down Expand Up @@ -96,9 +96,9 @@ static struct channel_event *stmt2channel_event(const tal_t *ctx, struct db_stmt

e->tag = db_col_strdup(e, stmt, "e.tag");

db_col_amount_msat(stmt, "e.credit", &e->credit);
db_col_amount_msat(stmt, "e.debit", &e->debit);
db_col_amount_msat(stmt, "e.fees", &e->fees);
e->credit = db_col_amount_msat(stmt, "e.credit");
e->debit = db_col_amount_msat(stmt, "e.debit");
e->fees = db_col_amount_msat(stmt, "e.fees");

e->currency = db_col_strdup(e, stmt, "e.currency");
if (!db_col_is_null(stmt, "e.payment_id")) {
Expand Down Expand Up @@ -131,8 +131,8 @@ static struct rebalance *stmt2rebalance(const tal_t *ctx, struct db_stmt *stmt)
r->out_ev_id = db_col_u64(stmt, "out_e.id");
r->in_acct_name = db_col_strdup(r, stmt, "in_acct.name");
r->out_acct_name = db_col_strdup(r, stmt, "out_acct.name");
db_col_amount_msat(stmt, "in_e.credit", &r->rebal_msat);
db_col_amount_msat(stmt, "out_e.fees", &r->fee_msat);
r->rebal_msat = db_col_amount_msat(stmt, "in_e.credit");
r->fee_msat = db_col_amount_msat(stmt, "out_e.fees");

return r;
}
Expand Down Expand Up @@ -289,8 +289,8 @@ struct fee_sum **calculate_onchain_fee_sums(const tal_t *ctx, struct db *db)
sum->acct_db_id = db_col_u64(stmt, "of.account_id");
sum->acct_name = db_col_strdup(sum, stmt, "a.name");
sum->currency = db_col_strdup(sum, stmt, "of.currency");
db_col_amount_msat(stmt, "credit", &sum->fees_paid);
db_col_amount_msat(stmt, "debit", &debit);
sum->fees_paid = db_col_amount_msat(stmt, "credit");
debit = db_col_amount_msat(stmt, "debit");

ok = amount_msat_sub(&sum->fees_paid, sum->fees_paid,
debit);
Expand Down Expand Up @@ -359,8 +359,8 @@ struct fee_sum **find_account_onchain_fees(const tal_t *ctx,
sum->txid = tal(sum, struct bitcoin_txid);
db_col_txid(stmt, "txid", sum->txid);

db_col_amount_msat(stmt, "credit", &sum->fees_paid);
db_col_amount_msat(stmt, "debit", &amt);
sum->fees_paid = db_col_amount_msat(stmt, "credit");
amt = db_col_amount_msat(stmt, "debit");
ok = amount_msat_sub(&sum->fees_paid, sum->fees_paid, amt);
assert(ok);
tal_arr_expand(&sums, sum);
Expand Down Expand Up @@ -818,8 +818,8 @@ char *account_get_balance(const tal_t *ctx,
bal = tal(*balances, struct acct_balance);

bal->currency = db_col_strdup(bal, stmt, "ce.currency");
db_col_amount_msat(stmt, "credit", &bal->credit);
db_col_amount_msat(stmt, "debit", &bal->debit);
bal->credit = db_col_amount_msat(stmt, "credit");
bal->debit = db_col_amount_msat(stmt, "debit");
tal_arr_expand(balances, bal);

if (account_exists)
Expand Down Expand Up @@ -862,13 +862,13 @@ char *account_get_balance(const tal_t *ctx,
tal_arr_expand(balances, bal);
}

db_col_amount_msat(stmt, "credit", &amt);
amt = db_col_amount_msat(stmt, "credit");
if (!amount_msat_add(&bal->credit, bal->credit, amt)) {
tal_free(stmt);
return "overflow adding channel_event credits";
}

db_col_amount_msat(stmt, "debit", &amt);
amt = db_col_amount_msat(stmt, "debit");
if (!amount_msat_add(&bal->debit, bal->debit, amt)) {
tal_free(stmt);
return "overflow adding channel_event debits";
Expand Down Expand Up @@ -992,8 +992,8 @@ static struct onchain_fee *stmt2onchain_fee(const tal_t *ctx,
of->acct_db_id = db_col_u64(stmt, "of.account_id");
of->acct_name = db_col_strdup(of, stmt, "a.name");
db_col_txid(stmt, "of.txid", &of->txid);
db_col_amount_msat(stmt, "of.credit", &of->credit);
db_col_amount_msat(stmt, "of.debit", &of->debit);
of->credit = db_col_amount_msat(stmt, "of.credit");
of->debit = db_col_amount_msat(stmt, "of.debit");
of->currency = db_col_strdup(of, stmt, "of.currency");
of->timestamp = db_col_u64(stmt, "of.timestamp");
of->update_count = db_col_int(stmt, "of.update_count");
Expand Down Expand Up @@ -1490,8 +1490,8 @@ static void insert_chain_fees_diff(struct db *db,
update_count = 0;
while (db_step(stmt)) {
update_count = db_col_int(stmt, "update_count");
db_col_amount_msat(stmt, "credit", &credit);
db_col_amount_msat(stmt, "debit", &debit);
credit = db_col_amount_msat(stmt, "credit");
debit = db_col_amount_msat(stmt, "debit");

/* These should apply perfectly, as we sorted them by
* insert order */
Expand Down
4 changes: 2 additions & 2 deletions wallet/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -1357,7 +1357,7 @@ migrate_inflight_last_tx_to_psbt(struct lightningd *ld, struct db *db)
continue;
}
db_col_node_id(stmt, "p.node_id", &peer_id);
db_col_amount_sat(stmt, "inflight.funding_satoshi", &funding_sat);
funding_sat = db_col_amount_sat(stmt, "inflight.funding_satoshi");
db_col_pubkey(stmt, "c.fundingkey_remote", &remote_funding_pubkey);
db_col_txid(stmt, "inflight.funding_tx_id", &funding_txid);

Expand Down Expand Up @@ -1458,7 +1458,7 @@ void migrate_last_tx_to_psbt(struct lightningd *ld, struct db *db)
}

db_col_node_id(stmt, "p.node_id", &peer_id);
db_col_amount_sat(stmt, "c.funding_satoshi", &funding_sat);
funding_sat = db_col_amount_sat(stmt, "c.funding_satoshi");
db_col_pubkey(stmt, "c.fundingkey_remote", &remote_funding_pubkey);

get_channel_basepoints(ld, &peer_id, cdb_id,
Expand Down
17 changes: 8 additions & 9 deletions wallet/invoices.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,17 @@ static struct invoice_details *wallet_stmt2invoice_details(const tal_t *ctx,

dtl->label = db_col_json_escape(dtl, stmt, "label");

dtl->msat = db_col_optional(dtl, stmt, "msatoshi", amount_msat);
if (db_col_is_null(stmt, "msatoshi"))
dtl->msat = NULL;
else {
dtl->msat = tal(dtl, struct amount_msat);
*dtl->msat = db_col_amount_msat(stmt, "msatoshi");
}
dtl->expiry_time = db_col_u64(stmt, "expiry_time");

if (dtl->state == PAID) {
dtl->pay_index = db_col_u64(stmt, "pay_index");
db_col_amount_msat(stmt, "msatoshi_received", &dtl->received);
dtl->received = db_col_amount_msat(stmt, "msatoshi_received");
dtl->paid_timestamp = db_col_u64(stmt, "paid_timestamp");
} else {
db_col_ignore(stmt, "pay_index");
Expand All @@ -102,13 +107,7 @@ static struct invoice_details *wallet_stmt2invoice_details(const tal_t *ctx,
}

dtl->invstring = db_col_strdup(dtl, stmt, "bolt11");

if (!db_col_is_null(stmt, "description"))
dtl->description = db_col_strdup(dtl, stmt,
"description");
else
dtl->description = NULL;

dtl->description = db_col_strdup_optional(dtl, stmt, "description");
dtl->features = db_col_arr(dtl, stmt, "features", u8);
dtl->local_offer_id = db_col_optional(dtl, stmt, "local_offer_id", sha256);
dtl->created_index = db_col_u64(stmt, "id");
Expand Down
Loading

0 comments on commit b6d347a

Please sign in to comment.