|
1 | 1 | use crate::block_id; |
| 2 | +use bdk_chain::bitcoin::{self, hashes::Hash}; |
2 | 3 | use bdk_chain::{ |
3 | | - bitcoin::{ |
4 | | - absolute, transaction, Amount, OutPoint, ScriptBuf, Transaction, TxIn, TxOut, |
5 | | - Txid, |
6 | | - }, |
7 | | - tx_graph, ConfirmationBlockTime, Merge, |
| 4 | + bitcoin::{absolute, transaction, Amount, OutPoint, ScriptBuf, Transaction, TxIn, TxOut, Txid}, |
| 5 | + indexer::keychain_txout, |
| 6 | + tx_graph, ConfirmationBlockTime, Merge, DescriptorExt |
8 | 7 | }; |
9 | | -use bdk_chain::bitcoin::{self, hashes::Hash}; |
10 | 8 | use std::path::Path; |
11 | 9 | use std::sync::Arc; |
| 10 | +use bdk_chain::miniscript::{Descriptor, DescriptorPublicKey}; |
12 | 11 |
|
13 | 12 | pub fn create_one_inp_one_out_tx(txid: Txid, amount: u64) -> Transaction { |
14 | 13 | Transaction { |
|
99 | 98 | assert_eq!(tx_graph_changeset1, changeset); |
100 | 99 | } |
101 | 100 |
|
| 101 | +pub fn parse_descriptor(descriptor: &str) -> Descriptor<DescriptorPublicKey> { |
| 102 | + let secp = bdk_chain::bitcoin::secp256k1::Secp256k1::signing_only(); |
| 103 | + Descriptor::<DescriptorPublicKey>::parse_descriptor(&secp, descriptor) |
| 104 | + .unwrap() |
| 105 | + .0 |
| 106 | +} |
| 107 | + |
| 108 | +pub fn persist_indexer_changeset<Db, CreateDb, Initialize, Persist>( |
| 109 | + filename: &str, |
| 110 | + create_db: CreateDb, |
| 111 | + initialize: Initialize, |
| 112 | + persist: Persist, |
| 113 | +) where |
| 114 | + CreateDb: Fn(&Path) -> anyhow::Result<Db>, |
| 115 | + Initialize: Fn(&mut Db) -> anyhow::Result<keychain_txout::ChangeSet>, |
| 116 | + Persist: Fn(&mut Db, &keychain_txout::ChangeSet) -> anyhow::Result<()>, |
| 117 | +{ |
| 118 | + let temp_dir = tempfile::tempdir().expect("must create tempdir"); |
| 119 | + let file_path = temp_dir.path().join(filename); |
| 120 | + let mut db = create_db(&file_path).expect("db should get created"); |
| 121 | + |
| 122 | + let descriptor_ids = crate::utils::DESCRIPTORS.map(|d| parse_descriptor(d).descriptor_id()); |
| 123 | + |
| 124 | + let mut keychain_txout_changeset = keychain_txout::ChangeSet { |
| 125 | + last_revealed: [(descriptor_ids[0], 1), (descriptor_ids[1], 100)].into(), |
| 126 | + spk_cache: [ |
| 127 | + ( |
| 128 | + descriptor_ids[0], |
| 129 | + [(0u32, ScriptBuf::from_bytes(vec![1, 2, 3]))].into(), |
| 130 | + ), |
| 131 | + ( |
| 132 | + descriptor_ids[1], |
| 133 | + [ |
| 134 | + (100u32, ScriptBuf::from_bytes(vec![3])), |
| 135 | + (1000u32, ScriptBuf::from_bytes(vec![5, 6, 8])), |
| 136 | + ] |
| 137 | + .into(), |
| 138 | + ), |
| 139 | + ] |
| 140 | + .into(), |
| 141 | + }; |
| 142 | + |
| 143 | + let changeset = initialize(&mut db).expect("should load empty changeset"); |
| 144 | + assert_eq!(changeset, keychain_txout::ChangeSet::default()); |
| 145 | + |
| 146 | + |
| 147 | + persist(&mut db, &keychain_txout_changeset).expect("should persist keychain_txout"); |
| 148 | + |
| 149 | + let changeset = initialize(&mut db).expect("should load persisted changeset"); |
| 150 | + |
| 151 | + assert_eq!(changeset, keychain_txout_changeset); |
| 152 | + |
| 153 | + let keychain_txout_changeset_new = keychain_txout::ChangeSet { |
| 154 | + last_revealed: [(descriptor_ids[0], 2)].into(), |
| 155 | + spk_cache: [( |
| 156 | + descriptor_ids[0], |
| 157 | + [(1u32, ScriptBuf::from_bytes(vec![1, 2, 3]))].into(), |
| 158 | + )] |
| 159 | + .into(), |
| 160 | + }; |
| 161 | + |
| 162 | + persist(&mut db, &keychain_txout_changeset_new).expect("should persist second changeset"); |
| 163 | + |
| 164 | + let changeset_new = initialize(&mut db).expect("should load merged changesets"); |
| 165 | + keychain_txout_changeset.merge(keychain_txout_changeset_new); |
| 166 | + |
| 167 | + assert_eq!(changeset_new, keychain_txout_changeset); |
| 168 | +} |
| 169 | + |
102 | 170 | // perhaps add test for file_store, sqlite, redb here. |
0 commit comments