| 433 | } |
| 434 | |
| 435 | bool watch_check_tx_outputs(const struct chain_topology *topo, |
| 436 | const struct txlocator *loc, |
| 437 | const struct bitcoin_tx *tx, |
| 438 | const struct bitcoin_txid *txid) |
| 439 | { |
| 440 | bool tx_interesting = false; |
| 441 | |
| 442 | for (size_t outnum = 0; outnum < tx->wtx->num_outputs; outnum++) { |
| 443 | const struct wally_tx_output *txout = &tx->wtx->outputs[outnum]; |
| 444 | const struct script_with_len swl = { txout->script, txout->script_len }; |
| 445 | struct scriptpubkeywatch_hash_iter it; |
| 446 | bool output_matched = false, bad_txid = false, bad_amount = false, bad_outnum = false; |
| 447 | struct amount_asset outasset = bitcoin_tx_output_get_amount(tx, outnum); |
| 448 | |
| 449 | /* Ensure callbacks don't do an insert during iteration! */ |
| 450 | scriptpubkeywatch_hash_lock(topo->scriptpubkeywatches); |
| 451 | for (struct scriptpubkeywatch *w = scriptpubkeywatch_hash_getfirst(topo->scriptpubkeywatches, &swl, &it); |
| 452 | w; |
| 453 | w = scriptpubkeywatch_hash_getnext(topo->scriptpubkeywatches, &swl, &it)) { |
| 454 | if (!bitcoin_txid_eq(&w->expected_outpoint.txid, txid)) { |
| 455 | bad_txid = true; |
| 456 | continue; |
| 457 | } |
| 458 | if (outnum != w->expected_outpoint.n) { |
| 459 | bad_outnum = true; |
| 460 | continue; |
| 461 | } |
| 462 | if (!amount_asset_is_main(&outasset) |
| 463 | || !amount_sat_eq(amount_asset_to_sat(&outasset), w->expected_amount)) { |
| 464 | bad_amount = true; |
| 465 | continue; |
| 466 | } |
| 467 | |
| 468 | w->cb(topo->ld, tx, outnum, loc, w->arg); |
| 469 | output_matched = true; |
| 470 | tx_interesting = true; |
| 471 | } |
| 472 | scriptpubkeywatch_hash_unlock(topo->scriptpubkeywatches); |
| 473 | |
| 474 | /* Only complain about mismatch if we missed all of them. |
| 475 | * This helps diagnose mistakes like wrong txid, see |
| 476 | * https://github.com/ElementsProject/lightning/issues/8892 */ |
| 477 | if (!output_matched && (bad_txid || bad_amount || bad_outnum)) { |
| 478 | const char *addr = encode_scriptpubkey_to_addr(tmpctx, chainparams, |
| 479 | txout->script, txout->script_len); |
| 480 | if (!addr) |
| 481 | addr = tal_fmt(tmpctx, "Scriptpubkey %s", tal_hexstr(tmpctx, txout->script, txout->script_len)); |
| 482 | if (bad_txid) { |
| 483 | log_unusual(topo->ld->log, |
| 484 | "Unexpected spend to %s by unexpected txid %s:%zu", |
| 485 | addr, fmt_bitcoin_txid(tmpctx, txid), outnum); |
| 486 | } |
| 487 | if (bad_amount) { |
| 488 | log_unusual(topo->ld->log, |
| 489 | "Unexpected amount %s to %s by txid %s:%zu", |
| 490 | amount_asset_is_main(&outasset) |
| 491 | ? fmt_amount_sat(tmpctx, amount_asset_to_sat(&outasset)) |
| 492 | : "fee output", |
no test coverage detected