|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +import os |
| 4 | + |
| 5 | +import pyln |
| 6 | +import pytest |
| 7 | +from pyln.testing import utils |
| 8 | +from pyln.testing.fixtures import * # noqa: F403 |
| 9 | +from util import LightningD |
| 10 | + |
| 11 | +pyln.testing.fixtures.network_daemons["signet"] = utils.BitcoinD |
| 12 | + |
| 13 | + |
| 14 | +class LightningNode(utils.LightningNode): |
| 15 | + def __init__(self, *args, **kwargs): |
| 16 | + pyln.testing.utils.TEST_NETWORK = "signet" |
| 17 | + utils.LightningNode.__init__(self, *args, **kwargs) |
| 18 | + lightning_dir = args[1] |
| 19 | + self.daemon = LightningD(lightning_dir, None, port=self.daemon.port) # noqa: F405 |
| 20 | + options = { |
| 21 | + "disable-plugin": "bcli", |
| 22 | + "network": "signet", |
| 23 | + "plugin": os.path.join(os.path.dirname(__file__), "../sauron.py"), |
| 24 | + "sauron-api-endpoint": "https://blockstream.info/signet/api", |
| 25 | + } |
| 26 | + self.daemon.opts.update(options) |
| 27 | + |
| 28 | + # Monkey patch |
| 29 | + def set_feerates(self, feerates, wait_for_effect=True): |
| 30 | + return None |
| 31 | + |
| 32 | + |
| 33 | +@pytest.fixture |
| 34 | +def node_cls(monkeypatch): |
| 35 | + monkeypatch.setenv("TEST_NETWORK", "signet") |
| 36 | + yield LightningNode |
| 37 | + |
| 38 | + |
| 39 | +def test_rpc_getchaininfo(node_factory): |
| 40 | + """ |
| 41 | + Test getchaininfo |
| 42 | + """ |
| 43 | + ln_node = node_factory.get_node() |
| 44 | + |
| 45 | + response = ln_node.rpc.call("getchaininfo") |
| 46 | + |
| 47 | + assert ln_node.daemon.is_in_log("Sauron plugin initialized using Esplora API") |
| 48 | + |
| 49 | + expected_response_keys = ["chain", "blockcount", "headercount", "ibd"] |
| 50 | + assert list(response.keys()) == expected_response_keys |
| 51 | + assert response["chain"] == "signet" |
| 52 | + assert not response["ibd"] |
| 53 | + |
| 54 | + |
| 55 | +def test_rpc_getrawblockbyheight(node_factory): |
| 56 | + """ |
| 57 | + Test getrawblockbyheight |
| 58 | + """ |
| 59 | + ln_node = node_factory.get_node() |
| 60 | + |
| 61 | + response = ln_node.rpc.call("getrawblockbyheight", {"height": 0}) |
| 62 | + |
| 63 | + expected_response = { |
| 64 | + "block": "0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a008f4d5fae77031e8ad222030101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000", |
| 65 | + "blockhash": "00000008819873e925422c1ff0f99f7cc9bbb232af63a077a480a3633bee1ef6", |
| 66 | + } |
| 67 | + assert response == expected_response |
| 68 | + |
| 69 | +@pytest.mark.skip(reason="testing_theory") |
| 70 | +def test_rpc_sendrawtransaction_invalid(node_factory): |
| 71 | + """ |
| 72 | + Test sendrawtransaction |
| 73 | + """ |
| 74 | + ln_node = node_factory.get_node() |
| 75 | + |
| 76 | + expected_error_substring = "RPC error" |
| 77 | + |
| 78 | + response = ln_node.rpc.call( |
| 79 | + "sendrawtransaction", |
| 80 | + {"tx": "invalid-raw-tx"}, |
| 81 | + ) |
| 82 | + |
| 83 | + assert expected_error_substring in response.get("errmsg", ""), "Expected 'RPC error' in errmsg field" |
| 84 | + assert response.get("success") is False, "Expected success to be False" |
| 85 | + |
| 86 | + |
| 87 | +def test_rpc_getutxout(node_factory): |
| 88 | + """ |
| 89 | + Test getutxout |
| 90 | + """ |
| 91 | + ln_node = node_factory.get_node() |
| 92 | + |
| 93 | + expected_response = { |
| 94 | + "amount": 5000000000, |
| 95 | + "script": "4104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac", |
| 96 | + } |
| 97 | + response = ln_node.rpc.call( |
| 98 | + "getutxout", |
| 99 | + { |
| 100 | + # block 217883 |
| 101 | + "txid": "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b", |
| 102 | + "vout": 0, |
| 103 | + }, |
| 104 | + ) |
| 105 | + assert response == expected_response |
| 106 | + |
| 107 | + |
| 108 | +def test_rpc_estimatefees(node_factory): |
| 109 | + """ |
| 110 | + Test estimatefees |
| 111 | + """ |
| 112 | + ln_node = node_factory.get_node() |
| 113 | + |
| 114 | + # Sample response |
| 115 | + # { |
| 116 | + # "opening": 4477, |
| 117 | + # "mutual_close": 4477, |
| 118 | + # "unilateral_close": 11929, |
| 119 | + # "delayed_to_us": 4477, |
| 120 | + # "htlc_resolution": 5652, |
| 121 | + # "penalty": 5652, |
| 122 | + # "min_acceptable": 1060, |
| 123 | + # "max_acceptable": 119290, |
| 124 | + # "feerate_floor": 1520, |
| 125 | + # "feerates": [ |
| 126 | + # {"blocks": 2, "feerate": 11929}, |
| 127 | + # {"blocks": 6, "feerate": 5652}, |
| 128 | + # {"blocks": 12, "feerate": 4477}, |
| 129 | + # {"blocks": 144, "feerate": 2120} |
| 130 | + # ] |
| 131 | + # } |
| 132 | + response = ln_node.rpc.call("estimatefees") |
| 133 | + |
| 134 | + expected_response_keys = [ |
| 135 | + "opening", |
| 136 | + "mutual_close", |
| 137 | + "unilateral_close", |
| 138 | + "delayed_to_us", |
| 139 | + "htlc_resolution", |
| 140 | + "penalty", |
| 141 | + "min_acceptable", |
| 142 | + "max_acceptable", |
| 143 | + "feerate_floor", |
| 144 | + "feerates", |
| 145 | + ] |
| 146 | + assert list(response.keys()) == expected_response_keys |
| 147 | + |
| 148 | + expected_feerates_keys = ("blocks", "feerate") |
| 149 | + assert ( |
| 150 | + list(set([tuple(entry.keys()) for entry in response["feerates"]]))[0] |
| 151 | + == expected_feerates_keys |
| 152 | + ) |
| 153 | + |
| 154 | + expected_feerates_blocks = [2, 6, 12, 144] |
| 155 | + assert [ |
| 156 | + entry["blocks"] for entry in response["feerates"] |
| 157 | + ] == expected_feerates_blocks |
0 commit comments