mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-03-01 17:47:30 +01:00
wallet: Add primitives to store onchaind transactions in the DB
Signed-off-by: Christian Decker <decker.christian@gmail.com>
This commit is contained in:
parent
d2dc93e3cb
commit
876d698f3c
2 changed files with 95 additions and 1 deletions
|
@ -11,6 +11,7 @@
|
|||
#include <lightningd/log.h>
|
||||
#include <lightningd/peer_control.h>
|
||||
#include <lightningd/peer_htlcs.h>
|
||||
#include <onchaind/gen_onchain_wire.h>
|
||||
#include <string.h>
|
||||
|
||||
#define SQLITE_MAX_UINT 0x7FFFFFFFFFFFFFFF
|
||||
|
@ -2277,3 +2278,77 @@ struct bitcoin_txid *wallet_transactions_by_height(const tal_t *ctx,
|
|||
return txids;
|
||||
}
|
||||
|
||||
void wallet_channeltxs_add(struct wallet *w, struct channel *chan,
|
||||
const int type, const struct bitcoin_txid *txid,
|
||||
const u32 input_num, const u32 blockheight)
|
||||
{
|
||||
sqlite3_stmt *stmt;
|
||||
stmt = db_prepare(w->db, "INSERT INTO channeltxs ("
|
||||
" channel_id"
|
||||
", type"
|
||||
", transaction_id"
|
||||
", input_num"
|
||||
", blockheight"
|
||||
") VALUES (?, ?, ?, ?, ?);");
|
||||
sqlite3_bind_int(stmt, 1, chan->dbid);
|
||||
sqlite3_bind_int(stmt, 2, type);
|
||||
sqlite3_bind_sha256(stmt, 3, &txid->shad.sha);
|
||||
sqlite3_bind_int(stmt, 4, input_num);
|
||||
sqlite3_bind_int(stmt, 5, blockheight);
|
||||
|
||||
db_exec_prepared(w->db, stmt);
|
||||
}
|
||||
|
||||
u32 *wallet_onchaind_channels(struct wallet *w,
|
||||
const tal_t *ctx)
|
||||
{
|
||||
sqlite3_stmt *stmt;
|
||||
size_t count = 0;
|
||||
u32 *channel_ids = tal_arr(ctx, u32, 0);
|
||||
stmt = db_prepare(w->db, "SELECT DISTINCT(channel_id) FROM channeltxs WHERE type = ?;");
|
||||
sqlite3_bind_int(stmt, 1, WIRE_ONCHAIN_INIT);
|
||||
|
||||
while (sqlite3_step(stmt) == SQLITE_ROW) {
|
||||
count++;
|
||||
tal_resize(&channel_ids, count);
|
||||
channel_ids[count-1] = sqlite3_column_int64(stmt, 0);
|
||||
}
|
||||
|
||||
return channel_ids;
|
||||
}
|
||||
|
||||
struct channeltx *wallet_channeltxs_get(struct wallet *w, const tal_t *ctx,
|
||||
u32 channel_id)
|
||||
{
|
||||
sqlite3_stmt *stmt;
|
||||
size_t count = 0;
|
||||
struct channeltx *res = tal_arr(ctx, struct channeltx, 0);
|
||||
stmt = db_prepare(w->db,
|
||||
"SELECT"
|
||||
" c.type"
|
||||
", c.blockheight"
|
||||
", t.rawtx"
|
||||
", c.input_num"
|
||||
", c.blockheight - t.blockheight + 1 AS depth"
|
||||
", t.id as txid "
|
||||
"FROM channeltxs c "
|
||||
"JOIN transactions t ON t.id == c.transaction_id "
|
||||
"WHERE channel_id = ? "
|
||||
"ORDER BY c.id ASC;");
|
||||
sqlite3_bind_int(stmt, 1, channel_id);
|
||||
|
||||
while (sqlite3_step(stmt) == SQLITE_ROW) {
|
||||
count++;
|
||||
tal_resize(&res, count);
|
||||
|
||||
res[count-1].channel_id = channel_id;
|
||||
res[count-1].type = sqlite3_column_int(stmt, 0);
|
||||
res[count-1].blockheight = sqlite3_column_int(stmt, 1);
|
||||
res[count-1].tx = sqlite3_column_tx(ctx, stmt, 2);
|
||||
res[count-1].input_num = sqlite3_column_int(stmt, 3);
|
||||
res[count-1].depth = sqlite3_column_int(stmt, 4);
|
||||
sqlite3_column_sha256(stmt, 5, &res[count-1].txid.shad.sha);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
|
|
@ -128,7 +128,7 @@ struct channel_stats {
|
|||
u64 out_msatoshi_offered, out_msatoshi_fulfilled;
|
||||
};
|
||||
|
||||
struct onchaindtx {
|
||||
struct channeltx {
|
||||
u32 channel_id;
|
||||
int type;
|
||||
u32 blockheight;
|
||||
|
@ -846,4 +846,23 @@ struct bitcoin_txid *wallet_transactions_by_height(const tal_t *ctx,
|
|||
struct wallet *w,
|
||||
const u32 blockheight);
|
||||
|
||||
/**
|
||||
* Store transactions of interest in the database to replay on restart
|
||||
*/
|
||||
void wallet_channeltxs_add(struct wallet *w, struct channel *chan,
|
||||
const int type, const struct bitcoin_txid *txid,
|
||||
const u32 input_num, const u32 blockheight);
|
||||
|
||||
/**
|
||||
* List channels for which we had an onchaind running
|
||||
*/
|
||||
u32 *wallet_onchaind_channels(struct wallet *w,
|
||||
const tal_t *ctx);
|
||||
|
||||
/**
|
||||
* Get transactions that we'd like to replay for a channel.
|
||||
*/
|
||||
struct channeltx *wallet_channeltxs_get(struct wallet *w, const tal_t *ctx,
|
||||
u32 channel_id);
|
||||
|
||||
#endif /* LIGHTNING_WALLET_WALLET_H */
|
||||
|
|
Loading…
Add table
Reference in a new issue