| 206 | } |
| 207 | |
| 208 | static void install_expiration_timer(struct invoices *invoices) |
| 209 | { |
| 210 | bool res; |
| 211 | struct db_stmt *stmt; |
| 212 | struct timerel rel; |
| 213 | struct timeabs expiry; |
| 214 | struct timeabs now = time_now(); |
| 215 | |
| 216 | assert(!invoices->expiration_timer); |
| 217 | |
| 218 | /* Find unpaid invoice with nearest expiry time */ |
| 219 | stmt = db_prepare_v2(invoices->db, SQL("SELECT MIN(expiry_time)" |
| 220 | " FROM invoices" |
| 221 | " WHERE state = ?;")); |
| 222 | db_bind_int(stmt, 0, UNPAID); |
| 223 | |
| 224 | db_query_prepared(stmt); |
| 225 | |
| 226 | res = db_step(stmt); |
| 227 | assert(res); |
| 228 | |
| 229 | if (db_col_is_null(stmt, "MIN(expiry_time)")) |
| 230 | /* Nothing to install */ |
| 231 | goto done; |
| 232 | |
| 233 | invoices->min_expiry_time = db_col_u64(stmt, |
| 234 | "MIN(expiry_time)"); |
| 235 | |
| 236 | memset(&expiry, 0, sizeof(expiry)); |
| 237 | expiry.ts.tv_sec = invoices->min_expiry_time; |
| 238 | |
| 239 | /* now > expiry */ |
| 240 | if (time_after(now, expiry)) |
| 241 | expiry = now; |
| 242 | |
| 243 | /* rel = expiry - now */ |
| 244 | rel = time_between(expiry, now); |
| 245 | |
| 246 | /* Have it called at indicated timerel. */ |
| 247 | invoices->expiration_timer = new_reltimer(invoices->timers, |
| 248 | invoices, |
| 249 | rel, |
| 250 | &trigger_expiration, |
| 251 | invoices); |
| 252 | done: |
| 253 | tal_free(stmt); |
| 254 | } |
| 255 | |
| 256 | bool invoices_create(struct invoices *invoices, |
| 257 | struct invoice *pinvoice, |
no test coverage detected