Skip to content

Commit

Permalink
42 test remove rpc (#45)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
fmhoeger authored Dec 19, 2023
1 parent f1388a7 commit a3f3b68
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,10 +411,14 @@ async fn delete(
plugin: Plugin<State>,
descriptor_name: String,
) -> Result<serde_json::Value, Error> {
let db_dir = plugin.state().lock().await.db_dir.clone();
let wallets = &mut plugin.state().lock().await.wallets;
let _removed_item: Option<DescriptorWallet>;
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);

Expand Down
4 changes: 4 additions & 0 deletions src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,10 @@ impl DescriptorWallet {
)?)
}

pub fn get_db_file_path(&self, db_dir: PathBuf) -> Result<String, Error> {
Ok(format!("{}/{}.db", db_dir.display(), self.get_name()?))
}

pub async fn fetch_wallet<'a>(
&mut self,
db_dir: PathBuf,
Expand Down
44 changes: 44 additions & 0 deletions tests/test_rpc.py
Original file line number Diff line number Diff line change
@@ -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)
9 changes: 9 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit a3f3b68

Please sign in to comment.