mirror of
https://github.com/ElementsProject/lightning.git
synced 2024-11-19 18:11:28 +01:00
wallet: Migrate HTLC persistence to native sqlite3 binding
This is a preparatory step before we get rid of the hex encoding of blob values. Signed-off-by: Christian Decker <decker.christian@gmail.com>
This commit is contained in:
parent
33da7f50c7
commit
e9cfa65a12
128
wallet/wallet.c
128
wallet/wallet.c
@ -825,21 +825,47 @@ bool wallet_htlc_save_in(struct wallet *wallet,
|
|||||||
{
|
{
|
||||||
bool ok = true;
|
bool ok = true;
|
||||||
tal_t *tmpctx = tal_tmpctx(wallet);
|
tal_t *tmpctx = tal_tmpctx(wallet);
|
||||||
|
sqlite3_stmt *stmt;
|
||||||
|
|
||||||
ok &= db_exec(
|
stmt = db_prepare(
|
||||||
__func__, wallet->db,
|
wallet->db,
|
||||||
"INSERT INTO channel_htlcs "
|
"INSERT INTO channel_htlcs ("
|
||||||
"(channel_id, channel_htlc_id, direction, origin_htlc, msatoshi, cltv_expiry, payment_hash, payment_key, hstate, shared_secret, routing_onion) VALUES "
|
" channel_id,"
|
||||||
"(%" PRIu64 ", %"PRIu64", %d, NULL, %"PRIu64", %d, '%s', %s, %d, '%s', '%s');",
|
" channel_htlc_id, "
|
||||||
chan->id, in->key.id, DIRECTION_INCOMING, in->msatoshi, in->cltv_expiry,
|
" direction,"
|
||||||
tal_hexstr(tmpctx, &in->payment_hash, sizeof(struct sha256)),
|
" msatoshi,"
|
||||||
in->preimage == NULL ? "NULL" : tal_hexstr(tmpctx, &in->preimage,
|
" cltv_expiry,"
|
||||||
sizeof(struct preimage)),
|
" payment_hash, "
|
||||||
in->hstate,
|
" payment_key,"
|
||||||
tal_hexstr(tmpctx, &in->shared_secret, sizeof(struct secret)),
|
" hstate,"
|
||||||
tal_hexstr(tmpctx, &in->onion_routing_packet, sizeof(in->onion_routing_packet))
|
" shared_secret,"
|
||||||
);
|
" routing_onion) VALUES "
|
||||||
|
"(?, ?, ?, ?, ?, ?, ?, ?, ?, ?);");
|
||||||
|
|
||||||
|
sqlite3_bind_int64(stmt, 1, chan->id);
|
||||||
|
sqlite3_bind_int64(stmt, 2, in->key.id);
|
||||||
|
sqlite3_bind_int(stmt, 3, DIRECTION_INCOMING);
|
||||||
|
sqlite3_bind_int64(stmt, 4, in->msatoshi);
|
||||||
|
sqlite3_bind_int(stmt, 5, in->cltv_expiry);
|
||||||
|
sqlite3_bind_blob(stmt, 6, tal_hexstr(tmpctx, &in->payment_hash,
|
||||||
|
sizeof(struct sha256)),
|
||||||
|
2 * sizeof(struct sha256), SQLITE_TRANSIENT);
|
||||||
|
if (in->preimage)
|
||||||
|
sqlite3_bind_blob(
|
||||||
|
stmt, 7,
|
||||||
|
tal_hexstr(tmpctx, in->preimage, sizeof(struct preimage)),
|
||||||
|
2 * sizeof(struct preimage), SQLITE_TRANSIENT);
|
||||||
|
sqlite3_bind_int(stmt, 8, in->hstate);
|
||||||
|
sqlite3_bind_blob(stmt, 9, tal_hexstr(tmpctx, &in->shared_secret,
|
||||||
|
sizeof(struct secret)),
|
||||||
|
2 * sizeof(struct secret), SQLITE_TRANSIENT);
|
||||||
|
|
||||||
|
sqlite3_bind_blob(
|
||||||
|
stmt, 10, tal_hexstr(tmpctx, &in->onion_routing_packet,
|
||||||
|
sizeof(in->onion_routing_packet)),
|
||||||
|
2 * sizeof(in->onion_routing_packet), SQLITE_TRANSIENT);
|
||||||
|
|
||||||
|
ok = db_exec_prepared(wallet->db, stmt);
|
||||||
tal_free(tmpctx);
|
tal_free(tmpctx);
|
||||||
if (ok) {
|
if (ok) {
|
||||||
in->dbid = sqlite3_last_insert_rowid(wallet->db->sql);
|
in->dbid = sqlite3_last_insert_rowid(wallet->db->sql);
|
||||||
@ -853,29 +879,49 @@ bool wallet_htlc_save_out(struct wallet *wallet,
|
|||||||
{
|
{
|
||||||
bool ok = true;
|
bool ok = true;
|
||||||
tal_t *tmpctx = tal_tmpctx(wallet);
|
tal_t *tmpctx = tal_tmpctx(wallet);
|
||||||
|
sqlite3_stmt *stmt;
|
||||||
|
|
||||||
/* We absolutely need the incoming HTLC to be persisted before
|
/* We absolutely need the incoming HTLC to be persisted before
|
||||||
* we can persist it's dependent */
|
* we can persist it's dependent */
|
||||||
assert(out->in == NULL || out->in->dbid != 0);
|
assert(out->in == NULL || out->in->dbid != 0);
|
||||||
out->origin_htlc_id = out->in?out->in->dbid:0;
|
out->origin_htlc_id = out->in?out->in->dbid:0;
|
||||||
|
|
||||||
ok &= db_exec(
|
stmt = db_prepare(
|
||||||
__func__, wallet->db,
|
wallet->db,
|
||||||
"INSERT INTO channel_htlcs "
|
"INSERT INTO channel_htlcs ("
|
||||||
"(channel_id, channel_htlc_id, direction, origin_htlc, msatoshi, cltv_expiry, "
|
" channel_id,"
|
||||||
"payment_hash, payment_key, hstate, shared_secret, routing_onion) VALUES "
|
" channel_htlc_id,"
|
||||||
"(%" PRIu64 ", %" PRIu64 ", %d, %s, %" PRIu64", %d, '%s', %s, %d, NULL, '%s');",
|
" direction,"
|
||||||
chan->id,
|
" origin_htlc,"
|
||||||
out->key.id,
|
" msatoshi,"
|
||||||
DIRECTION_OUTGOING,
|
" cltv_expiry,"
|
||||||
out->in ? tal_fmt(tmpctx, "%" PRIu64, out->in->dbid) : "NULL",
|
" payment_hash,"
|
||||||
out->msatoshi,
|
" payment_key,"
|
||||||
out->cltv_expiry,
|
" hstate,"
|
||||||
tal_hexstr(tmpctx, &out->payment_hash, sizeof(struct sha256)),
|
" routing_onion) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);");
|
||||||
out->preimage ? tal_hexstr(tmpctx, &out->preimage, sizeof(struct preimage)) : "NULL",
|
|
||||||
out->hstate,
|
sqlite3_bind_int64(stmt, 1, chan->id);
|
||||||
tal_hexstr(tmpctx, &out->onion_routing_packet, sizeof(out->onion_routing_packet))
|
sqlite3_bind_int64(stmt, 2, out->key.id);
|
||||||
);
|
sqlite3_bind_int(stmt, 3, DIRECTION_OUTGOING);
|
||||||
|
if (out->in)
|
||||||
|
sqlite3_bind_int64(stmt, 4, out->in->dbid);
|
||||||
|
sqlite3_bind_int64(stmt, 5, out->msatoshi);
|
||||||
|
sqlite3_bind_int(stmt, 6, out->cltv_expiry);
|
||||||
|
sqlite3_bind_blob(stmt, 7, tal_hexstr(tmpctx, &out->payment_hash,
|
||||||
|
sizeof(struct sha256)),
|
||||||
|
2 * sizeof(struct sha256), SQLITE_TRANSIENT);
|
||||||
|
if (out->preimage)
|
||||||
|
sqlite3_bind_blob(
|
||||||
|
stmt, 8,
|
||||||
|
tal_hexstr(tmpctx, out->preimage, sizeof(struct preimage)),
|
||||||
|
2 * sizeof(struct preimage), SQLITE_TRANSIENT);
|
||||||
|
sqlite3_bind_int(stmt, 9, out->hstate);
|
||||||
|
sqlite3_bind_blob(
|
||||||
|
stmt, 10, tal_hexstr(tmpctx, &out->onion_routing_packet,
|
||||||
|
sizeof(out->onion_routing_packet)),
|
||||||
|
2 * sizeof(out->onion_routing_packet), SQLITE_TRANSIENT);
|
||||||
|
|
||||||
|
ok = db_exec_prepared(wallet->db, stmt);
|
||||||
|
|
||||||
tal_free(tmpctx);
|
tal_free(tmpctx);
|
||||||
if (ok) {
|
if (ok) {
|
||||||
@ -890,24 +936,22 @@ bool wallet_htlc_update(struct wallet *wallet, const u64 htlc_dbid,
|
|||||||
{
|
{
|
||||||
bool ok = true;
|
bool ok = true;
|
||||||
tal_t *tmpctx = tal_tmpctx(wallet);
|
tal_t *tmpctx = tal_tmpctx(wallet);
|
||||||
|
sqlite3_stmt *stmt;
|
||||||
|
|
||||||
/* The database ID must be set by a previous call to
|
/* The database ID must be set by a previous call to
|
||||||
* `wallet_htlc_save_*` */
|
* `wallet_htlc_save_*` */
|
||||||
assert(htlc_dbid);
|
assert(htlc_dbid);
|
||||||
if (payment_key) {
|
stmt = db_prepare(
|
||||||
ok &= db_exec(
|
wallet->db, "UPDATE channel_htlcs SET hstate=?, payment_key=? WHERE id=?");
|
||||||
__func__, wallet->db, "UPDATE channel_htlcs SET hstate=%d, "
|
sqlite3_bind_int(stmt, 1, new_state);
|
||||||
"payment_key='%s' WHERE id=%" PRIu64,
|
if (payment_key)
|
||||||
new_state,
|
sqlite3_bind_blob(
|
||||||
|
stmt, 2,
|
||||||
tal_hexstr(tmpctx, payment_key, sizeof(struct preimage)),
|
tal_hexstr(tmpctx, payment_key, sizeof(struct preimage)),
|
||||||
htlc_dbid);
|
2 * sizeof(struct preimage), SQLITE_TRANSIENT);
|
||||||
|
sqlite3_bind_int64(stmt, 3, htlc_dbid);
|
||||||
|
|
||||||
} else {
|
ok = db_exec_prepared(wallet->db, stmt);
|
||||||
ok &= db_exec(
|
|
||||||
__func__, wallet->db,
|
|
||||||
"UPDATE channel_htlcs SET hstate = %d WHERE id=%" PRIu64,
|
|
||||||
new_state, htlc_dbid);
|
|
||||||
}
|
|
||||||
tal_free(tmpctx);
|
tal_free(tmpctx);
|
||||||
return ok;
|
return ok;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user