From b2baef166899b2f1d938bb105d76ed2371a06e98 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 18 Aug 2025 19:15:59 +0930 Subject: [PATCH] db: helper for binding/reading bitcoin_outpoint. Signed-off-by: Rusty Russell --- db/bindings.c | 21 +++++++++++++++++++++ db/bindings.h | 2 ++ 2 files changed, 23 insertions(+) diff --git a/db/bindings.c b/db/bindings.c index 63a224587..ceb251f30 100644 --- a/db/bindings.c +++ b/db/bindings.c @@ -140,6 +140,15 @@ void db_bind_txid(struct db_stmt *stmt, const struct bitcoin_txid *t) db_bind_sha256d(stmt, &t->shad); } +void db_bind_outpoint(struct db_stmt *stmt, const struct bitcoin_outpoint *o) +{ + u8 *ser = tal_arr(stmt, u8, sizeof(o->txid.shad.sha.u.u8) + sizeof(be32)); + be32 be_vout = cpu_to_be32(o->n); + memcpy(ser, o->txid.shad.sha.u.u8, sizeof(o->txid.shad.sha.u.u8)); + memcpy(ser + sizeof(o->txid.shad.sha.u.u8), &be_vout, sizeof(be32)); + db_bind_blob(stmt, ser, tal_bytelen(ser)); +} + void db_bind_channel_id(struct db_stmt *stmt, const struct channel_id *id) { db_bind_blob(stmt, id->id, sizeof(id->id)); @@ -618,6 +627,18 @@ void db_col_txid(struct db_stmt *stmt, const char *colname, struct bitcoin_txid db_col_sha256d(stmt, colname, &t->shad); } +void db_col_outpoint(struct db_stmt *stmt, const char *colname, struct bitcoin_outpoint *o) +{ + be32 be_vout; + size_t col = db_query_colnum(stmt, colname); + const u8 *raw; + assert(db_column_bytes(stmt, col) == sizeof(o->txid.shad.sha.u.u8) + sizeof(be32)); + raw = db_column_blob(stmt, col); + memcpy(o->txid.shad.sha.u.u8, raw, sizeof(o->txid.shad.sha.u.u8)); + memcpy(&be_vout, raw + sizeof(o->txid.shad.sha.u.u8), sizeof(be_vout)); + o->n = be32_to_cpu(be_vout); +} + struct onionreply *db_col_onionreply(const tal_t *ctx, struct db_stmt *stmt, const char *colname) { diff --git a/db/bindings.h b/db/bindings.h index 910b07a31..d6b02fcfe 100644 --- a/db/bindings.h +++ b/db/bindings.h @@ -30,6 +30,7 @@ void db_bind_sha256d(struct db_stmt *stmt, const struct sha256_double *s); void db_bind_secret(struct db_stmt *stmt, const struct secret *s); void db_bind_secret_arr(struct db_stmt *stmt, const struct secret *s); void db_bind_txid(struct db_stmt *stmt, const struct bitcoin_txid *t); +void db_bind_outpoint(struct db_stmt *stmt, const struct bitcoin_outpoint *o); void db_bind_channel_id(struct db_stmt *stmt, const struct channel_id *id); void db_bind_channel_type(struct db_stmt *stmt, const struct channel_type *type); void db_bind_node_id(struct db_stmt *stmt, const struct node_id *ni); @@ -100,6 +101,7 @@ struct timeabs db_col_timeabs(struct db_stmt *stmt, const char *colname); struct bitcoin_tx *db_col_tx(const tal_t *ctx, struct db_stmt *stmt, const char *colname); struct wally_psbt *db_col_psbt(const tal_t *ctx, struct db_stmt *stmt, const char *colname); struct bitcoin_tx *db_col_psbt_to_tx(const tal_t *ctx, struct db_stmt *stmt, const char *colname); +void db_col_outpoint(struct db_stmt *stmt, const char *colname, struct bitcoin_outpoint *o); struct onionreply *db_col_onionreply(const tal_t *ctx, struct db_stmt *stmt, const char *colname);