Skip to content

Commit

Permalink
Electrum 4.1 upgrade (#28)
Browse files Browse the repository at this point in the history
* Adapt tests for electrum 4.1

* Disable electrumx RPC

* Also test lnpay is successful in regtest tests

* Wait for lightning peers

* Remove sleep from CI

* Cleanup

* Reset cache
  • Loading branch information
MrNaif2018 authored Sep 21, 2021
1 parent fb2db89 commit eb833a4
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 33 deletions.
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ commands:
- restore_cache:
keys:
- v5-dependencies-{{ .Branch }}-{{ checksum "~/.pyenv/version" }}-{{ checksum "setup.py" }}-{{ checksum "test-requirements.txt" }}-{{ checksum "~/bitcart-daemon/requirements/daemons/btc.txt" }}
- v1-dependencies-{{ .Branch }}-{{ checksum "~/.pyenv/version" }}-{{ checksum "setup.py" }}-{{ checksum "test-requirements.txt" }}-{{ checksum "~/bitcart-daemon/requirements/daemons/btc.txt" }}

- run:
name: install library
Expand All @@ -46,7 +46,7 @@ commands:
- save_cache:
paths:
- ~/venv
key: v5-dependencies-{{ .Branch }}-{{ checksum "~/.pyenv/version" }}-{{ checksum "setup.py" }}-{{ checksum "test-requirements.txt" }}-{{ checksum "~/bitcart-daemon/requirements/daemons/btc.txt" }}
key: v1-dependencies-{{ .Branch }}-{{ checksum "~/.pyenv/version" }}-{{ checksum "setup.py" }}-{{ checksum "test-requirements.txt" }}-{{ checksum "~/bitcart-daemon/requirements/daemons/btc.txt" }}

upload-results:
steps:
Expand Down
15 changes: 10 additions & 5 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
from bitcart.coin import Coin

TEST_XPUB = "zprvAWgYBBk7JR8GkUwcwyhT1qk8FQpym8cHboGyDEdMhHcL1NRe8bioZR3uo5gTSTG47iqQX6VqPL6iHHDNK55taJiV9MEscu6UiqSR1tAiSUq"
REGTEST_XPUB = (
"vprv9DMUxX4ShgxMMQduKMSnoNsX4jZjdqmRRapGRRFbok1XXrjkvFyAnvSVPbs4t8ZDA73fTT9DLzdCyHBh39AZHG8nsP1gEj11EwSdYP8zhKF"
)
# deterministic channels support only seeds
REGTEST_XPUB = "dutch field mango comfort symptom smooth wide senior tongue oyster wash spoon"
REGTEST_XPUB2 = "hungry ordinary similar more spread math general wire jealous valve exhaust emotion"
LIGHTNING_UNSUPPORTED_XPUB = (
"xprv9s21ZrQH143K3tZPHG8CbfZ7uUY5stdHmaEXeSqawGrZuAoBdHPgKHjdkfmHSdxDJSbo29JiU1PcWhzEsgFryqMHQfr2T5TWBPK8EqFjscZ"
)
Expand Down Expand Up @@ -48,8 +48,13 @@ async def regtest_wallet():


@pytest.fixture
async def regtest_node_id():
return await BTC(xpub=REGTEST_XPUB, rpc_url="http://localhost:5110").node_id
async def regtest_node():
return BTC(xpub=REGTEST_XPUB2, rpc_url="http://localhost:5110")


@pytest.fixture
async def regtest_node_id(regtest_node):
return await regtest_node.node_id


@pytest.fixture
Expand Down
59 changes: 34 additions & 25 deletions tests/regtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
pytestmark = pytest.mark.asyncio

BTC_ADDRESS = (
"bcrt1qe0ppfnuz6wjw3vn8jefn8p4fxmyn7tqxkjt557" # can be got by run_shell(["newaddress"]) or regtest_wallet.add_request()
"bcrt1qttft7vh7w3er2akkpr4lu78z2ptdhgfxf739xf" # can be got by run_shell(["newaddress"]) or regtest_wallet.add_request()
)

TEST_PARAMS = [
Expand Down Expand Up @@ -41,15 +41,26 @@ async def wait_for_balance(regtest_wallet):
break


@pytest.fixture
async def wait_for_utxos(regtest_wallet):
def find_open_channel(channels): # there is also BACKUP type
for channel in channels:
if channel["type"] == "CHANNEL" and channel["state"] == "OPEN":
return channel


def find_channel_by_id(channels, channel_point):
for channel in channels:
if channel["channel_point"] == channel_point:
return channel


async def wait_for_channel_opening(regtest_wallet, channel_point):
while True:
utxos = [utxo for utxo in await regtest_wallet.server.listunspent() if utxo["height"] != -2]
# ensure we have nonlocal utxos to use to open channel
if len(utxos) == 0:
await asyncio.sleep(1)
else:
channels = await regtest_wallet.list_channels()
channel = find_channel_by_id(channels, channel_point)
await asyncio.sleep(1)
if channel["state"] == "OPEN":
break
await asyncio.sleep(1)


def check_tx(tx, broadcast):
Expand Down Expand Up @@ -98,59 +109,57 @@ async def test_payment_to_many(regtest_wallet, fee, feerate, broadcast, wait_for
)


async def test_open_channel(regtest_wallet, regtest_node_id, wait_for_utxos):
async def test_open_channel(regtest_wallet, regtest_node_id):
# works only with nonlocal balances
result = await regtest_wallet.open_channel(regtest_node_id, 0.002)
result = await regtest_wallet.open_channel(regtest_node_id, 0.1)
assert isinstance(result, str)
assert len(result) == 66
assert ":" in result
splitted = result.split(":")
assert len(splitted) == 2
int(splitted[1]) # integer
run_shell(["newblocks", "3"])
await wait_for_channel_opening(regtest_wallet, result)


async def test_list_channels(regtest_wallet, regtest_node_id):
pubkey = regtest_node_id.split("@")[0]
result = await regtest_wallet.list_channels()
assert isinstance(result, list)
assert len(result) > 0
channel = result[-1] # last channel is last in the list
channel = find_open_channel(result)
assert "short_channel_id" in channel
assert isinstance(channel["short_channel_id"], str) or channel["short_channel_id"] is None
data_check(channel, "channel_id", str, 64)
data_check(channel, "channel_point", str, 66)
data_check(channel, "peer_state", str)
assert channel["peer_state"] == "GOOD"
data_check(channel, "state", str)
assert channel["state"] == "OPEN"
assert channel["remote_pubkey"] == pubkey
assert channel["local_balance"] == 200000
assert channel["local_balance"] == 10000000
assert channel["remote_balance"] == 0
data_check(channel, "local_reserve", int)
data_check(channel, "remote_reserve", int)
data_check(channel, "local_unsettled_sent", int)
data_check(channel, "remote_unsettled_sent", int)
assert channel["local_reserve"] == channel["remote_reserve"] == 2000
assert channel["local_reserve"] == channel["remote_reserve"] == 100000
assert channel["local_unsettled_sent"] == channel["remote_unsettled_sent"] == 0


async def test_lnpay(regtest_wallet):
async def test_lnpay(regtest_wallet, regtest_node):
with pytest.raises(errors.InvalidLightningInvoiceError):
assert not await regtest_wallet.lnpay("")
response = await regtest_wallet.lnpay((await regtest_wallet.add_invoice(0.1))["invoice"])
invoice = (await regtest_node.add_invoice(0.01))["invoice"]
response = await regtest_wallet.lnpay(invoice)
assert isinstance(response, dict)
assert (
response.items()
> {
"success": False,
"preimage": None,
"log": [["None", "N/A", "No path found"]],
}.items()
)
assert response.keys() == {"payment_hash", "success", "preimage", "log"}
data_check(response, "payment_hash", str)
assert response["success"]
data_check(response, "preimage", str)
data_check(response, "log", list, 1)


async def test_close_channel(regtest_wallet):
channels = await regtest_wallet.list_channels()
channel_id = channels[-1]["channel_point"] # last channel is last in the list
channel_id = find_open_channel(channels)["channel_point"]
assert isinstance(await regtest_wallet.close_channel(channel_id), str)
2 changes: 1 addition & 1 deletion tests/regtest/start_electrumx.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ set -eux pipefail
cd
rm -rf ~/.electrumx_db
mkdir ~/.electrumx_db
COST_SOFT_LIMIT=0 COST_HARD_LIMIT=0 COIN=BitcoinSegwit SERVICES=tcp://:51001,rpc:// NET=regtest DAEMON_URL=http://doggman:[email protected]:18554 DB_DIRECTORY=~/.electrumx_db electrumx_server
COST_SOFT_LIMIT=0 COST_HARD_LIMIT=0 COIN=BitcoinSegwit SERVICES=tcp://:51001 NET=regtest DAEMON_URL=http://doggman:[email protected]:18554 DB_DIRECTORY=~/.electrumx_db electrumx_server

0 comments on commit eb833a4

Please sign in to comment.