From f1388a7fc30d4e5bc84d852290cc44e2aab9f969 Mon Sep 17 00:00:00 2001 From: Chris Guida Date: Tue, 19 Dec 2023 11:46:42 -0600 Subject: [PATCH 1/2] wip test payjoin, waiting on upsteam bkpr fixes --- tests/test_smaug_1.py | 69 +++++++++++++++++++++++++++++++------------ 1 file changed, 50 insertions(+), 19 deletions(-) diff --git a/tests/test_smaug_1.py b/tests/test_smaug_1.py index ae80197..16c1103 100644 --- a/tests/test_smaug_1.py +++ b/tests/test_smaug_1.py @@ -62,10 +62,11 @@ def generate(): ### simple spend ### - # 1 input which is ours (send utxo_spent) + # 1 input which is ours (send utxo_spent for 100M sats) # 2 outputs: - # 1 which is the spend (to an external account) (send utxo_deposit from our account to external) - # 1 which is change (back to our wallet) (send utxo_deposit from our account back to our account) + # 1 which is the 10M sat spend (to an external account) (send utxo_deposit from our account to external) + # 1 which is 89_999_859 sat change (back to our wallet) (send utxo_deposit from our account back to our account) + # fee on regtest is currently 141 sats, not sure if this will change in the future # to do our spend, we send 10M sats to the CLN internal wallet from our smaug wallet # this subtracts 1M sat (+141 sats for fee) from our bitcoind wallet @@ -153,23 +154,53 @@ def generate(): print("internal_descriptor = %s " % internal_descriptor) print("external_descriptor = %s " % external_descriptor) - print("smaug ls result = %s" % l1.rpc.smaug("ls")) - name = l1.rpc.smaug("add", external_descriptor, internal_descriptor)["name"] + # print("smaug ls result = %s" % l1.rpc.smaug("ls")) + # name = l1.rpc.smaug("add", external_descriptor, internal_descriptor)["name"] + + # bkpr_balances = l1.rpc.bkpr_listbalances()["accounts"] + # bitcoind_smaug_balance = get_bkpr_smaug_balance(name, bkpr_balances) + # assert bitcoind_smaug_balance["coin_type"] == "bcrt" + # assert bitcoind_smaug_balance["balance_msat"] == get_bitcoind_wallet_bal_sats(bitcoind) * 10**3 + + # there should now be 12 events in our bkpr_listaccountevents() call: + + all_accountevents = l1.rpc.bkpr_listaccountevents()['events'] + + # 4 for the `wallet` account: + # 1 journal_entry for 0 msats (this entry will be removed from bkpr soon?) + # 2 deposits: + # 10M sats for initial deposit + # 20M sats for payjoin output + # 1 withdrawal: + # 10M sats for payjoin input + + # smaug_accountevents = list(filter(lambda x: x["account"] == "smaug:%s" % name, all_accountevents)) + external_accountevents = list(filter(lambda x: x["account"] == "external", all_accountevents)) + non_cln_accountevents = list(filter(lambda x: x["account"] != "wallet", all_accountevents)) + accountevents = list(filter(lambda x: x.get("outpoint") == "42d4c98bbbdd16c11d150e2f9e038466c52341a13adfd81d33d8385a2ef90113:0", all_accountevents)) + # assert len(smaug_accountevents) == 8 + + # 8 for the `smaug:{name}` account: + # 5 deposits: + # 3 deposits from external into smaug: + # 100M sats for initial deposit from mining wallet + # 89_999_859 sats for change from initial send to CLN wallet + # 79_998_859 sats for change from payjoin + # 2 deposits from smaug to external (mirror for 2 CLN wallet deposits above): + # 10M sats for initial deposit + # 20M sats for payjoin output + # 3 withdrawals: + # 100M sats for initial deposit from mining wallet + # 10M sats for initial CLN wallet deposit + # 20M sats for payjoin output + + # there should also be + + pprint(all_accountevents) + + all_incomeevents = l1.rpc.bkpr_listincome()['income_events'] + pprint(all_incomeevents) - bkpr_balances = l1.rpc.bkpr_listbalances()["accounts"] - bitcoind_smaug_balance = get_bkpr_smaug_balance(name, bkpr_balances) - assert bitcoind_smaug_balance["coin_type"] == "bcrt" - assert bitcoind_smaug_balance["balance_msat"] == get_bitcoind_wallet_bal_sats(bitcoind) * 10**3 - - - - # pprint(events) - # verify new balance - # send funds back from smaug wallet to CLN wallet (sent) - # catch bkpr log - # find event in bkpr events - # verify new balance - # create payjoin between smaug wallet and CLN wallet (shared) # catch bkpr log # find event in bkpr events # verify new balance From a3f3b682dece9c51435d8447d8ebeec075159878 Mon Sep 17 00:00:00 2001 From: fmhoeger <59626853+fmhoeger@users.noreply.github.com> Date: Tue, 19 Dec 2023 17:05:27 -0600 Subject: [PATCH 2/2] 42 test remove rpc (#45) * Add test for wallet remove * Delete smaug db when removing wallet (WIP) * Remove the wallet db file only * Refactor constructing db_path to get_db_path * Refactor constructing db_path to get_db_path /2 * Rename get_db_path -> get_db_file_path * Do not expose db_path, instead retrieve from inside test code --- src/main.rs | 6 +++++- src/wallet.rs | 4 ++++ tests/test_rpc.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ tests/utils.py | 9 +++++++++ 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 tests/test_rpc.py diff --git a/src/main.rs b/src/main.rs index c1ff74d..153faba 100644 --- a/src/main.rs +++ b/src/main.rs @@ -411,10 +411,14 @@ async fn delete( plugin: Plugin, descriptor_name: String, ) -> Result { + let db_dir = plugin.state().lock().await.db_dir.clone(); let wallets = &mut plugin.state().lock().await.wallets; let _removed_item: Option; if wallets.contains_key(&descriptor_name) { - _removed_item = wallets.remove(&descriptor_name); + let removed_item = wallets.remove(&descriptor_name); + let db_path = removed_item.unwrap().get_db_file_path(db_dir).unwrap(); + fs::remove_file(db_path.clone())?; + log::debug!("Deleted smaug db file at {}", db_path); let rpc_file = plugin.configuration().rpc_file; let p = Path::new(&rpc_file); diff --git a/src/wallet.rs b/src/wallet.rs index de32c53..ccd5f78 100644 --- a/src/wallet.rs +++ b/src/wallet.rs @@ -275,6 +275,10 @@ impl DescriptorWallet { )?) } + pub fn get_db_file_path(&self, db_dir: PathBuf) -> Result { + Ok(format!("{}/{}.db", db_dir.display(), self.get_name()?)) + } + pub async fn fetch_wallet<'a>( &mut self, db_dir: PathBuf, diff --git a/tests/test_rpc.py b/tests/test_rpc.py new file mode 100644 index 0000000..4ff6921 --- /dev/null +++ b/tests/test_rpc.py @@ -0,0 +1,44 @@ +from pprint import pprint + +from conftest import SMAUG_PLUGIN +from fixtures import * +from pyln.client import Millisatoshi +from pyln.testing.utils import BITCOIND_CONFIG, only_one, wait_for +from utils import * +import os +def test_rpc_remove(node_factory, bitcoind): + """ + Test RPC remove. + """ + + l1 = node_factory.get_nodes( + 1, + opts={ + "allow_broken_log": True, + "plugin": SMAUG_PLUGIN, + "smaug_brpc_user": BITCOIND_CONFIG["rpcuser"], + "smaug_brpc_pass": BITCOIND_CONFIG["rpcpassword"], + "smaug_brpc_port": BITCOIND_CONFIG["rpcport"], + }, + )[0] + + # get external/internal only_one descriptors + internal_descriptor = get_only_one_descriptor(bitcoind, "wpkh", True) + external_descriptor = get_only_one_descriptor(bitcoind, "wpkh", False) + + # add wallet to smaug + wallet = l1.rpc.smaug("add", external_descriptor, internal_descriptor) + wallet_name = wallet["name"] + db_file_path = f"{str(l1.lightning_dir)}/regtest/.smaug/{wallet_name}.db" + smaug_wallets = l1.rpc.smaug("ls") + assert len(smaug_wallets) == 1 + assert wallet_name in smaug_wallets + assert os.path.isfile(db_file_path) + + # remove wallet from smaug + result = l1.rpc.smaug("remove", wallet_name) + + smaug_wallets = l1.rpc.smaug("ls") + assert len(smaug_wallets) == 0 + assert result == f"Deleted wallet: {wallet_name}" + assert not os.path.isfile(db_file_path) diff --git a/tests/utils.py b/tests/utils.py index 0729e9f..4ed6188 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -33,6 +33,15 @@ def get_descriptor(wpkh_descriptors, internal): list(filter(lambda x: x["internal"] is internal, wpkh_descriptors)) )["desc"] + +def get_only_one_descriptor(bitcoind, script_type, internal): + all_descriptors = bitcoind.rpc.listdescriptors()["descriptors"] + descriptors = list( + filter(lambda x: x["desc"].startswith(script_type), all_descriptors) + ) + return get_descriptor(descriptors, internal) + + def switch_wallet(bitcoind, wallet_name): current_wallets = bitcoind.rpc.listwallets() if wallet_name not in current_wallets: