|
| 1 | +// This file is Copyright its original authors, visible in version control history. |
| 2 | +// |
| 3 | +// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 4 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or |
| 5 | +// http://opensource.org/licenses/MIT>, at your option. You may not use this file except in |
| 6 | +// accordance with one or both of these licenses. |
| 7 | + |
| 8 | +#![cfg(vss_test)] |
| 9 | + |
| 10 | +mod common; |
| 11 | + |
| 12 | +use std::collections::HashMap; |
| 13 | + |
| 14 | +use ldk_node::entropy::NodeEntropy; |
| 15 | +use ldk_node::Builder; |
| 16 | +use rand::RngCore; |
| 17 | + |
| 18 | +#[tokio::test(flavor = "multi_thread", worker_threads = 1)] |
| 19 | +async fn vss_v0_schema_backwards_compatibility() { |
| 20 | + let (bitcoind, electrsd) = common::setup_bitcoind_and_electrsd(); |
| 21 | + let esplora_url = format!("http://{}", electrsd.esplora_url.as_ref().unwrap()); |
| 22 | + let vss_base_url = std::env::var("TEST_VSS_BASE_URL").unwrap(); |
| 23 | + |
| 24 | + let storage_path = common::random_storage_path().to_str().unwrap().to_owned(); |
| 25 | + let mut seed_bytes = [42u8; 64]; |
| 26 | + rand::thread_rng().fill_bytes(&mut seed_bytes); |
| 27 | + let node_entropy = NodeEntropy::from_seed_bytes(seed_bytes); |
| 28 | + |
| 29 | + // Setup a v0.6.2 `Node` persisted with the v0 scheme. |
| 30 | + let (old_balance, old_node_id) = { |
| 31 | + let mut builder_old = ldk_node_062::Builder::new(); |
| 32 | + builder_old.set_network(bitcoin::Network::Regtest); |
| 33 | + builder_old.set_storage_dir_path(storage_path.clone()); |
| 34 | + builder_old.set_entropy_seed_bytes(seed_bytes); |
| 35 | + builder_old.set_chain_source_esplora(esplora_url.clone(), None); |
| 36 | + let node_old = builder_old |
| 37 | + .build_with_vss_store(vss_base_url.clone(), "".to_owned(), HashMap::new()) |
| 38 | + .unwrap(); |
| 39 | + |
| 40 | + node_old.start().unwrap(); |
| 41 | + let addr_old = node_old.onchain_payment().new_address().unwrap(); |
| 42 | + common::premine_and_distribute_funds( |
| 43 | + &bitcoind.client, |
| 44 | + &electrsd.client, |
| 45 | + vec![addr_old], |
| 46 | + bitcoin::Amount::from_sat(100_000), |
| 47 | + ) |
| 48 | + .await; |
| 49 | + node_old.sync_wallets().unwrap(); |
| 50 | + |
| 51 | + let balance = node_old.list_balances().spendable_onchain_balance_sats; |
| 52 | + assert!(balance > 0); |
| 53 | + let node_id = node_old.node_id(); |
| 54 | + |
| 55 | + // Workaround necessary as v0.6.2's VSS runtime wasn't dropsafe in a tokio context. |
| 56 | + tokio::task::block_in_place(move || { |
| 57 | + node_old.stop().unwrap(); |
| 58 | + drop(node_old); |
| 59 | + }); |
| 60 | + |
| 61 | + (balance, node_id) |
| 62 | + }; |
| 63 | + |
| 64 | + // Now ensure we can still reinit from the same backend. |
| 65 | + let mut builder_new = Builder::new(); |
| 66 | + builder_new.set_network(bitcoin::Network::Regtest); |
| 67 | + builder_new.set_storage_dir_path(storage_path); |
| 68 | + builder_new.set_chain_source_esplora(esplora_url, None); |
| 69 | + |
| 70 | + let node_new = builder_new |
| 71 | + .build_with_vss_store(node_entropy, vss_base_url, "".to_owned(), HashMap::new()) |
| 72 | + .unwrap(); |
| 73 | + |
| 74 | + node_new.start().unwrap(); |
| 75 | + node_new.sync_wallets().unwrap(); |
| 76 | + |
| 77 | + let new_balance = node_new.list_balances().spendable_onchain_balance_sats; |
| 78 | + let new_node_id = node_new.node_id(); |
| 79 | + |
| 80 | + assert_eq!(old_node_id, new_node_id); |
| 81 | + assert_eq!(old_balance, new_balance); |
| 82 | + |
| 83 | + node_new.stop().unwrap(); |
| 84 | +} |
0 commit comments