Browse Source

wallet: Add primitives to retrieve htlc_stubs for channel

We'd like to not keep them in memory and retrieve them on-demand when
`onchaind` is launched. This uses the `channel_htlcs` table as backing
but only fetches the minimal necessary information.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
ppa-0.6.1
Christian Decker 7 years ago
committed by Rusty Russell
parent
commit
49ed7c9ab0
  1. 34
      wallet/wallet.c
  2. 15
      wallet/wallet.h

34
wallet/wallet.c

@ -1202,3 +1202,37 @@ bool wallet_invoice_remove(struct wallet *wallet, struct invoice *inv)
sqlite3_bind_int64(stmt, 1, inv->id);
return db_exec_prepared(wallet->db, stmt) && sqlite3_changes(wallet->db->sql) == 1;
}
struct htlc_stub *wallet_htlc_stubs(tal_t *ctx, struct wallet *wallet,
struct wallet_channel *chan)
{
struct htlc_stub *stubs;
struct sha256 payment_hash;
sqlite3_stmt *stmt = db_prepare(wallet->db,
"SELECT channel_id, direction, cltv_expiry, payment_hash "
"FROM channel_htlcs WHERE channel_id = ?;");
if (!stmt) {
log_broken(wallet->log, "Error preparing select: %s", wallet->db->err);
return NULL;
}
sqlite3_bind_int64(stmt, 1, chan->id);
stubs = tal_arr(ctx, struct htlc_stub, 0);
while (sqlite3_step(stmt) == SQLITE_ROW) {
int n = tal_count(stubs);
tal_resize(&stubs, n+1);
assert(sqlite3_column_int64(stmt, 0) == chan->id);
/* FIXME: merge these two enums */
stubs[n].owner = sqlite3_column_int(stmt, 1)==DIRECTION_INCOMING?REMOTE:LOCAL;
stubs[n].cltv_expiry = sqlite3_column_int(stmt, 2);
sqlite3_column_hexval(stmt, 3, &payment_hash, sizeof(payment_hash));
ripemd160(&stubs[n].ripemd, payment_hash.u.u8, sizeof(payment_hash.u));
}
sqlite3_finalize(stmt);
return stubs;
}

15
wallet/wallet.h

@ -11,6 +11,7 @@
#include <common/utxo.h>
#include <lightningd/htlc_end.h>
#include <lightningd/invoice.h>
#include <onchaind/onchain_wire.h>
#include <wally_bip32.h>
struct lightningd;
@ -336,4 +337,18 @@ bool wallet_invoices_load(struct wallet *wallet, struct invoices *invs);
*/
bool wallet_invoice_remove(struct wallet *wallet, struct invoice *inv);
/**
* wallet_htlc_stubs - Retrieve HTLC stubs for the given channel
*
* Load minimal necessary information about HTLCs for the on-chain
* settlement. This returns a `tal_arr` allocated off of @ctx with the
* necessary size to hold all HTLCs.
*
* @ctx: Allocation context for the return value
* @wallet: Wallet to load from
* @chan: Channel to fetch stubs for
*/
struct htlc_stub *wallet_htlc_stubs(tal_t *ctx, struct wallet *wallet,
struct wallet_channel *chan);
#endif /* WALLET_WALLET_H */

Loading…
Cancel
Save