| 379 | } |
| 380 | |
| 381 | static struct txo_set *find_txo_set(const tal_t *ctx, |
| 382 | struct db *db, |
| 383 | struct bitcoin_txid *txid, |
| 384 | u64 *acct_db_id, |
| 385 | bool *is_complete) |
| 386 | { |
| 387 | struct txo_pair *pr; |
| 388 | struct chain_event **evs; |
| 389 | struct txo_set *txos = tal(ctx, struct txo_set); |
| 390 | |
| 391 | /* In some special cases (the opening tx), we only |
| 392 | * want the outputs that pertain to a given account, |
| 393 | * most other times we want all utxos, regardless of account */ |
| 394 | evs = find_txos_for_tx(ctx, db, txid); |
| 395 | txos->pairs = tal_arr(txos, struct txo_pair *, 0); |
| 396 | txos->txid = tal_dup(txos, struct bitcoin_txid, txid); |
| 397 | |
| 398 | pr = NULL; |
| 399 | |
| 400 | /* If there's nothing for this txid, we're missing data */ |
| 401 | if (is_complete) |
| 402 | *is_complete = tal_count(evs) > 0; |
| 403 | |
| 404 | for (size_t i = 0; i < tal_count(evs); i++) { |
| 405 | struct chain_event *ev = evs[i]; |
| 406 | |
| 407 | if (acct_db_id && ev->acct_db_id != *acct_db_id) |
| 408 | continue; |
| 409 | |
| 410 | if (ev->spending_txid) { |
| 411 | if (!pr) { |
| 412 | /* We're missing data!! */ |
| 413 | pr = new_txo_pair(txos->pairs); |
| 414 | if (is_complete) |
| 415 | *is_complete = false; |
| 416 | } else { |
| 417 | assert(pr->txo); |
| 418 | /* Make sure it's the same txo */ |
| 419 | assert(bitcoin_outpoint_eq(&pr->txo->outpoint, |
| 420 | &ev->outpoint)); |
| 421 | } |
| 422 | |
| 423 | pr->spend = tal_steal(pr, ev); |
| 424 | tal_arr_expand(&txos->pairs, pr); |
| 425 | pr = NULL; |
| 426 | } else { |
| 427 | /* We might not have a spend event |
| 428 | * for everything */ |
| 429 | if (pr) { |
| 430 | /* Disappear "channel_proposed" events */ |
| 431 | if (streq(pr->txo->tag, |
| 432 | mvt_tag_str(CHANNEL_PROPOSED))) |
| 433 | pr = tal_free(pr); |
| 434 | else |
| 435 | tal_arr_expand(&txos->pairs, pr); |
| 436 | } |
| 437 | pr = new_txo_pair(txos->pairs); |
| 438 | pr->txo = tal_steal(pr, ev); |
no test coverage detected