diff --git a/wallet/wallet.c b/wallet/wallet.c index 20aaa9986..053d35a39 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -3018,6 +3018,23 @@ void wallet_payment_delete(struct wallet *wallet, db_exec_prepared_v2(take(stmt)); } +u64 wallet_payment_get_groupid(struct wallet *wallet, + const struct sha256 *payment_hash) +{ + struct db_stmt *stmt; + u64 groupid = 0; + stmt = db_prepare_v2( + wallet->db, SQL("SELECT MAX(groupid) FROM payments WHERE payment_hash = ?")); + + db_bind_sha256(stmt, 0, payment_hash); + db_query_prepared(stmt); + if (db_step(stmt) && !db_column_is_null(stmt, 0)) { + groupid = db_column_u64(stmt, 0); + } + tal_free(stmt); + return groupid; +} + void wallet_payment_delete_by_hash(struct wallet *wallet, const struct sha256 *payment_hash) { diff --git a/wallet/wallet.h b/wallet/wallet.h index 21bd5dd00..400ecb496 100644 --- a/wallet/wallet.h +++ b/wallet/wallet.h @@ -1050,6 +1050,15 @@ wallet_payment_by_hash(const tal_t *ctx, struct wallet *wallet, const struct sha256 *payment_hash, u64 partid); +/** + * Retrieve maximum groupid for a given payment_hash. + * + * Useful to either wait on the latest payment that was iniated with + * the hash or start a new one by incrementing the groupid. + */ +u64 wallet_payment_get_groupid(struct wallet *wallet, + const struct sha256 *payment_hash); + /** * wallet_payment_set_status - Update the status of the payment *