Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test add/list rpc #47

Merged
merged 16 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions tests/.pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
repos:
- repo: https://github.com/psf/black
rev: 23.12.0
rev: 23.12.1
hooks:
- id: black
args: [--target-version, py310]
args: [--target-version, py311]
language_version: python3
types: [python]
files: ^(tests|psynet|demos|docs)/
files: ^(tests)/
- repo: https://github.com/pycqa/isort
rev: 5.13.2
hooks:
Expand Down
14 changes: 14 additions & 0 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,17 @@ def bitcoind(directory, teardown_checks):
except Exception:
bitcoind.proc.kill()
bitcoind.proc.wait()


@pytest.fixture
def ln_node(node_factory):
yield 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]
123 changes: 99 additions & 24 deletions tests/test_rpc.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,119 @@
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):
from utils import get_only_one_descriptor


def test_rpc_add(bitcoind, ln_node):
"""
Test RPC add.
"""

# Get external/internal only_one descriptors
external_descriptor_wpkh = get_only_one_descriptor(bitcoind, "wpkh", False)
internal_descriptor_wpkh = get_only_one_descriptor(bitcoind, "wpkh", True)

smaug_wallets = ln_node.rpc.smaug("ls")
assert len(smaug_wallets) == 0

# Add a wallet to smaug
wallet = ln_node.rpc.smaug(
"add", external_descriptor_wpkh, internal_descriptor_wpkh, "821000", "5000"
)
wallet_name = wallet["name"]

asserted = {
"message": f"Wallet with deterministic name {wallet_name} successfully added",
"name": wallet_name,
}

assert len(wallet.keys()) == len(asserted.keys())
for key, value in wallet.items():
assert value == asserted[key]

ln_node.daemon.wait_for_log(
f"Wallet with deterministic name {wallet_name} successfully added"
)

# List smaug wallets
smaug_wallets = ln_node.rpc.smaug("ls")

assert len(smaug_wallets) == 1
assert wallet_name in smaug_wallets

smaug_wallet = smaug_wallets[wallet_name]

assert smaug_wallet["change_descriptor"].startswith("wpkh(")
assert smaug_wallet["descriptor"].startswith("wpkh(")
assert smaug_wallet["birthday"] == 821000
assert smaug_wallet["gap"] == 5000


def test_rpc_list(bitcoind, ln_node):
"""
Test RPC list.
"""

# Get external/internal only_one descriptors
external_descriptor_wpkh = get_only_one_descriptor(bitcoind, "wpkh", False)
internal_descriptor_wpkh = get_only_one_descriptor(bitcoind, "wpkh", True)
external_descriptor_tr = get_only_one_descriptor(bitcoind, "tr", False)
internal_descriptor_tr = get_only_one_descriptor(bitcoind, "tr", True)

# Add two wallets to smaug
wallet1 = ln_node.rpc.smaug(
"add", external_descriptor_wpkh, internal_descriptor_wpkh, "821000", "5000"
)
wallet2 = ln_node.rpc.smaug(
"add", external_descriptor_tr, internal_descriptor_tr, "821001", "5001"
)
wallet1_name = wallet1["name"]
wallet2_name = wallet2["name"]

# List smaug wallets
smaug_wallets = ln_node.rpc.smaug("ls")

assert len(smaug_wallets) == 2
assert wallet1_name in smaug_wallets
assert wallet2_name in smaug_wallets

smaug_wallet_1 = smaug_wallets[wallet1_name]
smaug_wallet_2 = smaug_wallets[wallet2_name]

assert smaug_wallet_1["change_descriptor"].startswith("wpkh(")
assert smaug_wallet_1["descriptor"].startswith("wpkh(")
assert smaug_wallet_1["birthday"] == 821000
assert smaug_wallet_1["gap"] == 5000
assert smaug_wallet_2["change_descriptor"].startswith("tr(")
assert smaug_wallet_2["descriptor"].startswith("tr(")
assert smaug_wallet_2["birthday"] == 821001
assert smaug_wallet_2["gap"] == 5001


def test_rpc_remove(bitcoind, ln_node):
"""
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
# 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)
# Add wallet to smaug
wallet = ln_node.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")
db_file_path = f"{str(ln_node.lightning_dir)}/regtest/.smaug/{wallet_name}.db"

smaug_wallets = ln_node.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)
# Remove wallet from smaug
result = ln_node.rpc.smaug("remove", wallet_name)

smaug_wallets = l1.rpc.smaug("ls")
smaug_wallets = ln_node.rpc.smaug("ls")
assert len(smaug_wallets) == 0
assert result == f"Deleted wallet: {wallet_name}"
assert not os.path.isfile(db_file_path)
Loading