| 209 | } |
| 210 | |
| 211 | static void install_expiration_timer(struct invoices *invoices) |
| 212 | { |
| 213 | bool res; |
| 214 | struct db_stmt *stmt; |
| 215 | struct timerel rel; |
| 216 | struct timeabs expiry; |
| 217 | struct timeabs now = clock_time(); |
| 218 | |
| 219 | assert(!invoices->expiration_timer); |
| 220 | |
| 221 | /* Find unpaid invoice with nearest expiry time */ |
| 222 | stmt = db_prepare_v2(invoices->wallet->db, SQL("SELECT MIN(expiry_time)" |
| 223 | " FROM invoices" |
| 224 | " WHERE state = ?;")); |
| 225 | db_bind_int(stmt, UNPAID); |
| 226 | |
| 227 | db_query_prepared(stmt); |
| 228 | |
| 229 | res = db_step(stmt); |
| 230 | assert(res); |
| 231 | |
| 232 | if (db_col_is_null(stmt, "MIN(expiry_time)")) |
| 233 | /* Nothing to install */ |
| 234 | goto done; |
| 235 | |
| 236 | invoices->min_expiry_time = db_col_u64(stmt, |
| 237 | "MIN(expiry_time)"); |
| 238 | |
| 239 | memset(&expiry, 0, sizeof(expiry)); |
| 240 | expiry.ts.tv_sec = invoices->min_expiry_time; |
| 241 | |
| 242 | /* Hi! On a 32 bit time_t platform with an expiry after 2038? Let's |
| 243 | * not set a timer, assuming you'll upgrade before then! */ |
| 244 | if (expiry.ts.tv_sec != invoices->min_expiry_time) |
| 245 | goto done; |
| 246 | |
| 247 | /* now > expiry */ |
| 248 | if (time_after(now, expiry)) |
| 249 | expiry = now; |
| 250 | |
| 251 | /* rel = expiry - now */ |
| 252 | rel = time_between(expiry, now); |
| 253 | |
| 254 | /* Have it called at indicated timerel. */ |
| 255 | invoices->expiration_timer = new_reltimer(invoices->timers, |
| 256 | invoices, |
| 257 | rel, |
| 258 | &trigger_expiration, |
| 259 | invoices); |
| 260 | done: |
| 261 | tal_free(stmt); |
| 262 | } |
| 263 | |
| 264 | bool invoices_create(struct invoices *invoices, |
| 265 | u64 *inv_dbid, |
no test coverage detected