Browse Source

db: Add a table to track the penalty_bases for revocations

nifty/pset-pre
Christian Decker 5 years ago
committed by Rusty Russell
parent
commit
667a763659
  1. 8
      wallet/db.c
  2. 61
      wallet/wallet.c
  3. 25
      wallet/wallet.h

8
wallet/db.c

@ -599,6 +599,14 @@ static struct migration dbmigrations[] = {
{SQL("ALTER TABLE channel_htlcs ADD localfailmsg BLOB;"), NULL},
{SQL("UPDATE channel_htlcs SET localfailmsg=decode('2002', 'hex') WHERE malformed_onion != 0 AND direction = 1;"), NULL},
{SQL("ALTER TABLE channels ADD our_funding_satoshi BIGINT DEFAULT 0;"), migrate_our_funding},
{SQL("CREATE TABLE penalty_bases ("
" channel_id BIGINT REFERENCES channels(id) ON DELETE CASCADE"
", commitnum BIGINT"
", txid BLOB"
", outnum INTEGER"
", amount BIGINT"
", PRIMARY KEY (channel_id, commitnum)"
");"), NULL},
};
/* Leak tracking. */

61
wallet/wallet.c

@ -3786,3 +3786,64 @@ struct wallet_transaction *wallet_transactions_get(struct wallet *w, const tal_t
tal_free(stmt);
return txs;
}
void wallet_penalty_base_add(struct wallet *w, u64 chan_id,
const struct penalty_base *pb)
{
struct db_stmt *stmt;
stmt = db_prepare_v2(w->db,
SQL("INSERT INTO penalty_bases ("
" channel_id"
", commitnum"
", txid"
", outnum"
", amount"
") VALUES (?, ?, ?, ?, ?);"));
db_bind_u64(stmt, 0, chan_id);
db_bind_u64(stmt, 1, pb->commitment_num);
db_bind_txid(stmt, 2, &pb->txid);
db_bind_int(stmt, 3, pb->outnum);
db_bind_amount_sat(stmt, 4, &pb->amount);
db_exec_prepared_v2(take(stmt));
}
struct penalty_base *wallet_penalty_base_load_for_channel(const tal_t *ctx,
struct wallet *w,
u64 chan_id)
{
struct db_stmt *stmt;
struct penalty_base *res = tal_arr(ctx, struct penalty_base, 0);
stmt = db_prepare_v2(
w->db,
SQL("SELECT commitnum, txid, outnum, amount "
"FROM penalty_bases "
"WHERE channel_id = ?"));
db_bind_u64(stmt, 0, chan_id);
db_query_prepared(stmt);
while (db_step(stmt)) {
struct penalty_base pb;
pb.commitment_num = db_column_u64(stmt, 0);
db_column_txid(stmt, 1, &pb.txid);
pb.outnum = db_column_int(stmt, 2);
db_column_amount_sat(stmt, 3, &pb.amount);
tal_arr_expand(&res, pb);
}
tal_free(stmt);
return res;
}
void wallet_penalty_base_delete(struct wallet *w, u64 chan_id, u64 commitnum)
{
struct db_stmt *stmt;
stmt = db_prepare_v2(
w->db,
SQL("DELETE FROM penalty_bases "
"WHERE channel_id = ? AND commitnum = ?"));
db_bind_u64(stmt, 0, chan_id);
db_bind_u64(stmt, 1, commitnum);
db_exec_prepared_v2(take(stmt));
}

25
wallet/wallet.h

@ -10,6 +10,7 @@
#include <ccan/list/list.h>
#include <ccan/tal/tal.h>
#include <common/channel_config.h>
#include <common/penalty_base.h>
#include <common/utxo.h>
#include <common/wallet.h>
#include <lightningd/bitcoind.h>
@ -1251,4 +1252,28 @@ struct wallet_transaction *wallet_transactions_get(struct wallet *w, const tal_t
*/
void wallet_filteredblock_add(struct wallet *w, const struct filteredblock *fb);
/**
* Store a penalty base in the database.
*
* Required to eventually create a penalty transaction when we get a
* revocation.
*/
void wallet_penalty_base_add(struct wallet *w, u64 chan_id,
const struct penalty_base *pb);
/**
* Retrieve all pending penalty bases for a given channel.
*
* This list should stay relatively small since we remove items from it as we
* get revocations. We retrieve this list whenever we start a new `channeld`.
*/
struct penalty_base *wallet_penalty_base_load_for_channel(const tal_t *ctx,
struct wallet *w,
u64 chan_id);
/**
* Delete a penalty_base, after we created and delivered it to the hook.
*/
void wallet_penalty_base_delete(struct wallet *w, u64 chan_id, u64 commitnum);
#endif /* LIGHTNING_WALLET_WALLET_H */

Loading…
Cancel
Save