| 4763 | } |
| 4764 | |
| 4765 | struct wallet_transaction *wallet_transactions_get(struct wallet *w, const tal_t *ctx) |
| 4766 | { |
| 4767 | struct db_stmt *stmt; |
| 4768 | size_t count; |
| 4769 | struct wallet_transaction *cur = NULL, *txs = tal_arr(ctx, struct wallet_transaction, 0); |
| 4770 | struct bitcoin_txid last; |
| 4771 | |
| 4772 | /* Make sure we can check for changing txids */ |
| 4773 | memset(&last, 0, sizeof(last)); |
| 4774 | |
| 4775 | stmt = db_prepare_v2( |
| 4776 | w->db, |
| 4777 | SQL("SELECT" |
| 4778 | " t.id" |
| 4779 | ", t.rawtx" |
| 4780 | ", t.blockheight" |
| 4781 | ", t.txindex" |
| 4782 | ", t.type as txtype" |
| 4783 | ", c2.scid as txchan" |
| 4784 | ", a.location" |
| 4785 | ", a.idx as ann_idx" |
| 4786 | ", a.type as annotation_type" |
| 4787 | ", c.scid" |
| 4788 | " FROM" |
| 4789 | " transactions t LEFT JOIN" |
| 4790 | " transaction_annotations a ON (a.txid = t.id) LEFT JOIN" |
| 4791 | " channels c ON (a.channel = c.id) LEFT JOIN" |
| 4792 | " channels c2 ON (t.channel_id = c2.id) " |
| 4793 | "ORDER BY t.blockheight, t.txindex ASC")); |
| 4794 | db_query_prepared(stmt); |
| 4795 | |
| 4796 | for (count = 0; db_step(stmt); count++) { |
| 4797 | struct bitcoin_txid curtxid; |
| 4798 | db_col_txid(stmt, "t.id", &curtxid); |
| 4799 | |
| 4800 | /* If this is a new entry, allocate it in the array and set |
| 4801 | * the common fields (all fields from the transactions table. */ |
| 4802 | if (!bitcoin_txid_eq(&last, &curtxid)) { |
| 4803 | last = curtxid; |
| 4804 | tal_resize(&txs, tal_count(txs) + 1); |
| 4805 | cur = &txs[tal_count(txs) - 1]; |
| 4806 | db_col_txid(stmt, "t.id", &cur->id); |
| 4807 | cur->tx = db_col_tx(txs, stmt, "t.rawtx"); |
| 4808 | cur->rawtx = db_col_arr(txs, stmt, "t.rawtx", u8); |
| 4809 | /* TX may be unconfirmed. */ |
| 4810 | if (!db_col_is_null(stmt, "t.blockheight")) { |
| 4811 | cur->blockheight |
| 4812 | = db_col_int(stmt, "t.blockheight"); |
| 4813 | if (!db_col_is_null(stmt, "t.txindex")) { |
| 4814 | cur->txindex |
| 4815 | = db_col_int(stmt, "t.txindex"); |
| 4816 | } else { |
| 4817 | cur->txindex = 0; |
| 4818 | } |
| 4819 | } else { |
| 4820 | db_col_ignore(stmt, "t.txindex"); |
| 4821 | cur->blockheight = 0; |
| 4822 | cur->txindex = 0; |
no test coverage detected