wallet: Add function to annotate transactions aposteriori

Since we add the transactions while processing the blockchain, and before we
have enough context to annotate them correctly, i.e., in the txwatches, we add
them first and then annotate them aposteriori.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
This commit is contained in:
Christian Decker
2019-05-23 22:41:27 +02:00
committed by Rusty Russell
parent 1e2291c40c
commit ad4b9204ab
4 changed files with 68 additions and 1 deletions

View File

@@ -60,7 +60,7 @@ COMMON_SRC_NOGEN := \
COMMON_SRC_GEN := common/gen_status_wire.c common/gen_peer_status_wire.c
COMMON_HEADERS_NOGEN := $(COMMON_SRC_NOGEN:.c=.h) common/overflows.h common/htlc.h common/status_levels.h common/json_command.h common/jsonrpc_errors.h
COMMON_HEADERS_NOGEN := $(COMMON_SRC_NOGEN:.c=.h) common/overflows.h common/htlc.h common/status_levels.h common/json_command.h common/jsonrpc_errors.h common/wallet.h
COMMON_HEADERS_GEN := common/gen_htlc_state_names.h common/gen_status_wire.h common/gen_peer_status_wire.h
COMMON_HEADERS := $(COMMON_HEADERS_GEN) $(COMMON_HEADERS_NOGEN)

25
common/wallet.h Normal file
View File

@@ -0,0 +1,25 @@
#ifndef LIGHTNING_COMMON_WALLET_H
#define LIGHTNING_COMMON_WALLET_H
#include "config.h"
/* Types of transactions we store in the `transactions` table. Mainly used for
* display purposes later. */
enum wallet_tx_type {
TX_UNKNOWN = 0,
TX_THEIRS = 1, /* This only affects their funds in the channel */
TX_WALLET_DEPOSIT = 2,
TX_WALLET_WITHDRAWAL = 4,
TX_CHANNEL_FUNDING = 8,
TX_CHANNEL_CLOSE = 16,
TX_CHANNEL_UNILATERAL = 32,
TX_CHANNEL_SWEEP = 64,
TX_CHANNEL_HTLC_SUCCESS = 128,
TX_CHANNEL_HTLC_TIMEOUT = 256,
TX_CHANNEL_PENALTY = 512,
TX_CHANNEL_CHEAT = 1024,
};
/* Any combination of the above wallet_tx_types */
typedef unsigned short txtypes;
#endif /* LIGHTNING_COMMON_WALLET_H */

View File

@@ -2415,6 +2415,35 @@ void wallet_transaction_add(struct wallet *w, const struct bitcoin_tx *tx,
}
}
void wallet_transaction_annotate(struct wallet *w,
const struct bitcoin_txid *txid, txtypes type,
u64 channel_id)
{
sqlite3_stmt *stmt = db_select_prepare(w->db, "type, channel_id FROM transactions WHERE id=?");
sqlite3_bind_sha256(stmt, 1, &txid->shad.sha);
if (!db_select_step(w->db, stmt))
fatal("Attempting to annotate a transaction we don't have: %s",
type_to_string(tmpctx, struct bitcoin_txid, txid));
type |= sqlite3_column_int(stmt, 0);
if (channel_id == 0)
channel_id = sqlite3_column_int64(stmt, 1);
db_stmt_done(stmt);
stmt = db_prepare(w->db, "UPDATE transactions "
"SET type = ?"
", channel_id = ? "
"WHERE id = ?");
sqlite3_bind_int(stmt, 1, type);
if (channel_id)
sqlite3_bind_int(stmt, 2, channel_id);
else
sqlite3_bind_null(stmt, 2);
sqlite3_bind_sha256(stmt, 3, &txid->shad.sha);
db_exec_prepared(w->db, stmt);
}
u32 wallet_transaction_height(struct wallet *w, const struct bitcoin_txid *txid)
{
u32 blockheight;

View File

@@ -11,6 +11,7 @@
#include <ccan/tal/tal.h>
#include <common/channel_config.h>
#include <common/utxo.h>
#include <common/wallet.h>
#include <lightningd/chaintopology.h>
#include <lightningd/htlc_end.h>
#include <lightningd/invoice.h>
@@ -1028,6 +1029,18 @@ void wallet_utxoset_add(struct wallet *w, const struct bitcoin_tx *tx,
void wallet_transaction_add(struct wallet *w, const struct bitcoin_tx *tx,
const u32 blockheight, const u32 txindex);
/**
* Annotate a transaction in the DB with its type and channel referemce.
*
* We add transactions when filtering the block, but often know its type only
* when we trigger the txwatches, at which point we've already discarded the
* full transaction. This function can be used to annotate the transactions
* after the fact with a channel number for grouping and a type for filtering.
*/
void wallet_transaction_annotate(struct wallet *w,
const struct bitcoin_txid *txid, txtypes type,
u64 channel_id);
/**
* Get the confirmation height of a transaction we are watching by its
* txid. Returns 0 if the transaction was not part of any block.