@@ -914,21 +914,6 @@ mod test {
914914 Store :: new ( db, wallet_name. to_string ( ) ) . unwrap ( )
915915 }
916916
917- #[ test]
918- fn test_network_persistence ( ) {
919- let tmpfile = NamedTempFile :: new ( ) . unwrap ( ) ;
920- let db = create_db ( tmpfile. path ( ) ) ;
921- let store = create_test_store ( Arc :: new ( db) , "wallet1" ) ;
922- store. create_network_table ( ) . unwrap ( ) ;
923- let network_changeset = Some ( Network :: Bitcoin ) ;
924- store. persist_network ( & network_changeset) . unwrap ( ) ;
925-
926- let mut network_changeset = Some ( Network :: Regtest ) ;
927- store. read_network ( & mut network_changeset) . unwrap ( ) ;
928-
929- assert_eq ! ( network_changeset, Some ( Network :: Bitcoin ) ) ;
930- }
931-
932917 #[ test]
933918 fn test_keychains_persistence ( ) {
934919 let tmpfile = NamedTempFile :: new ( ) . unwrap ( ) ;
@@ -1700,68 +1685,7 @@ mod test {
17001685 assert_eq ! ( anchors_read_new, anchors) ;
17011686 }
17021687
1703- #[ test]
1704- fn test_tx_graph_persistence ( ) {
1705- let tmpfile = NamedTempFile :: new ( ) . unwrap ( ) ;
1706- let db = create_db ( tmpfile. path ( ) ) ;
1707- let store = create_test_store ( Arc :: new ( db) , "wallet1" ) ;
1708- let tx1 = Arc :: new ( create_one_inp_one_out_tx (
1709- Txid :: from_byte_array ( [ 0 ; 32 ] ) ,
1710- 30_000 ,
1711- ) ) ;
1712- let tx2 = Arc :: new ( create_one_inp_one_out_tx ( tx1. compute_txid ( ) , 20_000 ) ) ;
1713- let block_id = block_id ! ( 100 , "B" ) ;
1714-
1715- let conf_anchor: ConfirmationBlockTime = ConfirmationBlockTime {
1716- block_id,
1717- confirmation_time : 1 ,
1718- } ;
1719-
1720- let mut tx_graph_changeset1 = tx_graph:: ChangeSet :: < ConfirmationBlockTime > {
1721- txs : [ tx1. clone ( ) ] . into ( ) ,
1722- txouts : [ ] . into ( ) ,
1723- anchors : [ ( conf_anchor, tx1. compute_txid ( ) ) ] . into ( ) ,
1724- last_seen : [ ( tx1. compute_txid ( ) , 100 ) ] . into ( ) ,
1725- first_seen : [ ( tx1. compute_txid ( ) , 50 ) ] . into ( ) ,
1726- last_evicted : [ ( tx1. compute_txid ( ) , 150 ) ] . into ( ) ,
1727- } ;
1728-
1729- store
1730- . create_tx_graph_tables :: < ConfirmationBlockTime > ( )
1731- . unwrap ( ) ;
1732-
1733- store. persist_tx_graph ( & tx_graph_changeset1) . unwrap ( ) ;
1734-
1735- let mut changeset = tx_graph:: ChangeSet :: default ( ) ;
1736- store. read_tx_graph ( & mut changeset) . unwrap ( ) ;
1737- assert_eq ! ( changeset, tx_graph_changeset1) ;
1738-
1739- let block_id = block_id ! ( 101 , "REDB" ) ;
1740-
1741- let conf_anchor: ConfirmationBlockTime = ConfirmationBlockTime {
1742- block_id,
1743- confirmation_time : 1 ,
1744- } ;
1745-
1746- let tx_graph_changeset2 = tx_graph:: ChangeSet :: < ConfirmationBlockTime > {
1747- txs : [ tx2. clone ( ) ] . into ( ) ,
1748- txouts : [ ] . into ( ) ,
1749- anchors : [ ( conf_anchor, tx2. compute_txid ( ) ) ] . into ( ) ,
1750- last_seen : [ ( tx2. compute_txid ( ) , 200 ) ] . into ( ) ,
1751- first_seen : [ ( tx2. compute_txid ( ) , 100 ) ] . into ( ) ,
1752- last_evicted : [ ( tx2. compute_txid ( ) , 150 ) ] . into ( ) ,
1753- } ;
1754-
1755- store. persist_tx_graph ( & tx_graph_changeset2) . unwrap ( ) ;
1756-
1757- let mut changeset = tx_graph:: ChangeSet :: default ( ) ;
1758- store. read_tx_graph ( & mut changeset) . unwrap ( ) ;
1759-
1760- tx_graph_changeset1. merge ( tx_graph_changeset2) ;
1761-
1762- assert_eq ! ( tx_graph_changeset1, changeset) ;
1763- }
1764-
1688+
17651689 fn parse_descriptor ( descriptor : & str ) -> Descriptor < DescriptorPublicKey > {
17661690 let secp = bdk_chain:: bitcoin:: secp256k1:: Secp256k1 :: signing_only ( ) ;
17671691 Descriptor :: < DescriptorPublicKey > :: parse_descriptor ( & secp, descriptor)
@@ -1890,188 +1814,6 @@ mod test {
18901814 assert_eq ! ( spk_cache, spk_cache_read_new) ;
18911815 }
18921816
1893- #[ test]
1894- fn test_indexer_persistence ( ) {
1895- let tmpfile = NamedTempFile :: new ( ) . unwrap ( ) ;
1896- let db = create_db ( tmpfile. path ( ) ) ;
1897- let store = create_test_store ( Arc :: new ( db) , "wallet1" ) ;
1898-
1899- let descriptor_ids = utils:: DESCRIPTORS . map ( |d| parse_descriptor ( d) . descriptor_id ( ) ) ;
1900-
1901- let mut keychain_txout_changeset = keychain_txout:: ChangeSet {
1902- last_revealed : [ ( descriptor_ids[ 0 ] , 1 ) , ( descriptor_ids[ 1 ] , 100 ) ] . into ( ) ,
1903- spk_cache : [
1904- (
1905- descriptor_ids[ 0 ] ,
1906- [ ( 0u32 , ScriptBuf :: from_bytes ( vec ! [ 1 , 2 , 3 ] ) ) ] . into ( ) ,
1907- ) ,
1908- (
1909- descriptor_ids[ 1 ] ,
1910- [
1911- ( 100u32 , ScriptBuf :: from_bytes ( vec ! [ 3 ] ) ) ,
1912- ( 1000u32 , ScriptBuf :: from_bytes ( vec ! [ 5 , 6 , 8 ] ) ) ,
1913- ]
1914- . into ( ) ,
1915- ) ,
1916- ]
1917- . into ( ) ,
1918- } ;
1919-
1920- store. create_indexer_tables ( ) . unwrap ( ) ;
1921- store. persist_indexer ( & keychain_txout_changeset) . unwrap ( ) ;
1922-
1923- let mut changeset = keychain_txout:: ChangeSet :: default ( ) ;
1924- store. read_indexer ( & mut changeset) . unwrap ( ) ;
1925-
1926- let keychain_txout_changeset_new = keychain_txout:: ChangeSet {
1927- last_revealed : [ ( descriptor_ids[ 0 ] , 2 ) ] . into ( ) ,
1928- spk_cache : [ (
1929- descriptor_ids[ 0 ] ,
1930- [ ( 1u32 , ScriptBuf :: from_bytes ( vec ! [ 1 , 2 , 3 ] ) ) ] . into ( ) ,
1931- ) ]
1932- . into ( ) ,
1933- } ;
1934-
1935- store
1936- . persist_indexer ( & keychain_txout_changeset_new)
1937- . unwrap ( ) ;
1938-
1939- let mut changeset_new = keychain_txout:: ChangeSet :: default ( ) ;
1940- store. read_indexer ( & mut changeset_new) . unwrap ( ) ;
1941- keychain_txout_changeset. merge ( keychain_txout_changeset_new) ;
1942-
1943- assert_eq ! ( changeset_new, keychain_txout_changeset) ;
1944- }
1945-
1946- #[ cfg( feature = "wallet" ) ]
1947- #[ test]
1948- fn test_persist_wallet ( ) {
1949- let tmpfile = NamedTempFile :: new ( ) . unwrap ( ) ;
1950- let db = Arc :: new ( create_db ( tmpfile. path ( ) ) ) ;
1951- let store = create_test_store ( db, "wallet1" ) ;
1952-
1953- let descriptor: Descriptor < DescriptorPublicKey > = DESCRIPTORS [ 0 ] . parse ( ) . unwrap ( ) ;
1954- let change_descriptor: Descriptor < DescriptorPublicKey > = DESCRIPTORS [ 1 ] . parse ( ) . unwrap ( ) ;
1955-
1956- let mut blocks: BTreeMap < u32 , Option < BlockHash > > = BTreeMap :: new ( ) ;
1957- blocks. insert ( 0u32 , Some ( hash ! ( "B" ) ) ) ;
1958- blocks. insert ( 1u32 , Some ( hash ! ( "T" ) ) ) ;
1959- blocks. insert ( 2u32 , Some ( hash ! ( "C" ) ) ) ;
1960- let local_chain_changeset = local_chain:: ChangeSet { blocks } ;
1961-
1962- let tx1 = Arc :: new ( create_one_inp_one_out_tx (
1963- Txid :: from_byte_array ( [ 0 ; 32 ] ) ,
1964- 30_000 ,
1965- ) ) ;
1966- let tx2 = Arc :: new ( create_one_inp_one_out_tx ( tx1. compute_txid ( ) , 20_000 ) ) ;
1967-
1968- let block_id = block_id ! ( 1 , "BDK" ) ;
1969-
1970- let conf_anchor: ConfirmationBlockTime = ConfirmationBlockTime {
1971- block_id,
1972- confirmation_time : 123 ,
1973- } ;
1974-
1975- let tx_graph_changeset = tx_graph:: ChangeSet :: < ConfirmationBlockTime > {
1976- txs : [ tx1. clone ( ) ] . into ( ) ,
1977- txouts : [ ] . into ( ) ,
1978- anchors : [ ( conf_anchor, tx1. compute_txid ( ) ) ] . into ( ) ,
1979- last_seen : [ ( tx1. compute_txid ( ) , 100 ) ] . into ( ) ,
1980- first_seen : [ ( tx1. compute_txid ( ) , 80 ) ] . into ( ) ,
1981- last_evicted : [ ( tx1. compute_txid ( ) , 150 ) ] . into ( ) ,
1982- } ;
1983-
1984- let keychain_txout_changeset = keychain_txout:: ChangeSet {
1985- last_revealed : [
1986- ( descriptor. descriptor_id ( ) , 12 ) ,
1987- ( change_descriptor. descriptor_id ( ) , 10 ) ,
1988- ]
1989- . into ( ) ,
1990- spk_cache : [
1991- (
1992- descriptor. descriptor_id ( ) ,
1993- [ ( 0u32 , ScriptBuf :: from_bytes ( vec ! [ 245 , 123 , 112 ] ) ) ] . into ( ) ,
1994- ) ,
1995- (
1996- change_descriptor. descriptor_id ( ) ,
1997- [
1998- ( 100u32 , ScriptBuf :: from_bytes ( vec ! [ 145 , 234 , 98 ] ) ) ,
1999- ( 1000u32 , ScriptBuf :: from_bytes ( vec ! [ 5 , 6 , 8 ] ) ) ,
2000- ]
2001- . into ( ) ,
2002- ) ,
2003- ]
2004- . into ( ) ,
2005- } ;
2006-
2007- let mut changeset = ChangeSet {
2008- descriptor : Some ( descriptor. clone ( ) ) ,
2009- change_descriptor : Some ( change_descriptor. clone ( ) ) ,
2010- network : Some ( Network :: Bitcoin ) ,
2011- local_chain : local_chain_changeset,
2012- tx_graph : tx_graph_changeset,
2013- indexer : keychain_txout_changeset,
2014- } ;
2015-
2016- store. create_tables :: < ConfirmationBlockTime > ( ) . unwrap ( ) ;
2017-
2018- store. persist_wallet ( & changeset) . unwrap ( ) ;
2019- let mut changeset_read = ChangeSet :: default ( ) ;
2020- store. read_wallet ( & mut changeset_read) . unwrap ( ) ;
2021-
2022- assert_eq ! ( changeset, changeset_read) ;
2023-
2024- let mut blocks: BTreeMap < u32 , Option < BlockHash > > = BTreeMap :: new ( ) ;
2025- blocks. insert ( 4u32 , Some ( hash ! ( "RE" ) ) ) ;
2026- blocks. insert ( 5u32 , Some ( hash ! ( "DB" ) ) ) ;
2027- let local_chain_changeset = local_chain:: ChangeSet { blocks } ;
2028-
2029- let block_id = block_id ! ( 2 , "Bitcoin" ) ;
2030-
2031- let conf_anchor: ConfirmationBlockTime = ConfirmationBlockTime {
2032- block_id,
2033- confirmation_time : 214 ,
2034- } ;
2035-
2036- let tx_graph_changeset = tx_graph:: ChangeSet :: < ConfirmationBlockTime > {
2037- txs : [ tx2. clone ( ) ] . into ( ) ,
2038- txouts : [ ] . into ( ) ,
2039- anchors : [ ( conf_anchor, tx2. compute_txid ( ) ) ] . into ( ) ,
2040- last_seen : [ ( tx2. compute_txid ( ) , 200 ) ] . into ( ) ,
2041- first_seen : [ ( tx2. compute_txid ( ) , 160 ) ] . into ( ) ,
2042- last_evicted : [ ( tx2. compute_txid ( ) , 300 ) ] . into ( ) ,
2043- } ;
2044-
2045- let keychain_txout_changeset = keychain_txout:: ChangeSet {
2046- last_revealed : [ ( descriptor. descriptor_id ( ) , 14 ) ] . into ( ) ,
2047- spk_cache : [ (
2048- change_descriptor. descriptor_id ( ) ,
2049- [
2050- ( 102u32 , ScriptBuf :: from_bytes ( vec ! [ 8 , 45 , 78 ] ) ) ,
2051- ( 1001u32 , ScriptBuf :: from_bytes ( vec ! [ 29 , 56 , 47 ] ) ) ,
2052- ]
2053- . into ( ) ,
2054- ) ]
2055- . into ( ) ,
2056- } ;
2057-
2058- let changeset_new = ChangeSet {
2059- descriptor : Some ( descriptor) ,
2060- change_descriptor : Some ( change_descriptor) ,
2061- network : Some ( Network :: Bitcoin ) ,
2062- local_chain : local_chain_changeset,
2063- tx_graph : tx_graph_changeset,
2064- indexer : keychain_txout_changeset,
2065- } ;
2066-
2067- store. persist_wallet ( & changeset_new) . unwrap ( ) ;
2068- let mut changeset_read_new = ChangeSet :: default ( ) ;
2069- store. read_wallet ( & mut changeset_read_new) . unwrap ( ) ;
2070-
2071- changeset. merge ( changeset_new) ;
2072-
2073- assert_eq ! ( changeset, changeset_read_new) ;
2074- }
20751817
20761818 #[ cfg( feature = "wallet" ) ]
20771819 #[ test]
@@ -2282,4 +2024,15 @@ mod test {
22822024 } ,
22832025 ) ;
22842026 }
2027+
2028+ #[ cfg( feature = "wallet" ) ]
2029+ #[ test]
2030+ fn network_is_persisted ( ) {
2031+ use bdk_wallet:: persist_test_utils:: persist_network;
2032+
2033+ persist_network ( "wallet.redb" , |path| {
2034+ let db = redb:: Database :: create ( path) ?;
2035+ Ok ( Store :: new ( Arc :: new ( db) , "wallet" . to_string ( ) ) ?)
2036+ } ) ;
2037+ }
22852038}
0 commit comments