|
| 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::{rng, Rng, 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 rand_suffix: String = |
| 25 | + (0..7).map(|_| rng().sample(rand::distr::Alphanumeric) as char).collect(); |
| 26 | + let store_id = format!("v0_compat_test_{}", rand_suffix); |
| 27 | + let storage_path = common::random_storage_path().to_str().unwrap().to_owned(); |
| 28 | + let mut seed_bytes = [42u8; 64]; |
| 29 | + rand::thread_rng().fill_bytes(&mut seed_bytes); |
| 30 | + let node_entropy = NodeEntropy::from_seed_bytes(seed_bytes); |
| 31 | + |
| 32 | + // Setup a v0.6.2 `Node` persisted with the v0 scheme. |
| 33 | + let (old_balance, old_node_id) = { |
| 34 | + let mut builder_old = ldk_node_062::Builder::new(); |
| 35 | + builder_old.set_network(bitcoin::Network::Regtest); |
| 36 | + builder_old.set_storage_dir_path(storage_path.clone()); |
| 37 | + builder_old.set_entropy_seed_bytes(seed_bytes); |
| 38 | + builder_old.set_chain_source_esplora(esplora_url.clone(), None); |
| 39 | + let node_old = builder_old |
| 40 | + .build_with_vss_store_and_fixed_headers( |
| 41 | + vss_base_url.clone(), |
| 42 | + store_id.clone(), |
| 43 | + HashMap::new(), |
| 44 | + ) |
| 45 | + .unwrap(); |
| 46 | + |
| 47 | + node_old.start().unwrap(); |
| 48 | + let addr_old = node_old.onchain_payment().new_address().unwrap(); |
| 49 | + common::premine_and_distribute_funds( |
| 50 | + &bitcoind.client, |
| 51 | + &electrsd.client, |
| 52 | + vec![addr_old], |
| 53 | + bitcoin::Amount::from_sat(100_000), |
| 54 | + ) |
| 55 | + .await; |
| 56 | + node_old.sync_wallets().unwrap(); |
| 57 | + |
| 58 | + let balance = node_old.list_balances().spendable_onchain_balance_sats; |
| 59 | + assert!(balance > 0); |
| 60 | + let node_id = node_old.node_id(); |
| 61 | + |
| 62 | + // Workaround necessary as v0.6.2's VSS runtime wasn't dropsafe in a tokio context. |
| 63 | + tokio::task::block_in_place(move || { |
| 64 | + node_old.stop().unwrap(); |
| 65 | + drop(node_old); |
| 66 | + }); |
| 67 | + |
| 68 | + (balance, node_id) |
| 69 | + }; |
| 70 | + |
| 71 | + // Now ensure we can still reinit from the same backend. |
| 72 | + let mut builder_new = Builder::new(); |
| 73 | + builder_new.set_network(bitcoin::Network::Regtest); |
| 74 | + builder_new.set_storage_dir_path(storage_path); |
| 75 | + builder_new.set_chain_source_esplora(esplora_url, None); |
| 76 | + |
| 77 | + let node_new = builder_new |
| 78 | + .build_with_vss_store_and_fixed_headers( |
| 79 | + node_entropy, |
| 80 | + vss_base_url, |
| 81 | + store_id, |
| 82 | + HashMap::new(), |
| 83 | + ) |
| 84 | + .unwrap(); |
| 85 | + |
| 86 | + node_new.start().unwrap(); |
| 87 | + node_new.sync_wallets().unwrap(); |
| 88 | + |
| 89 | + let new_balance = node_new.list_balances().spendable_onchain_balance_sats; |
| 90 | + let new_node_id = node_new.node_id(); |
| 91 | + |
| 92 | + assert_eq!(old_node_id, new_node_id); |
| 93 | + assert_eq!(old_balance, new_balance); |
| 94 | + |
| 95 | + node_new.stop().unwrap(); |
| 96 | +} |
0 commit comments