Skip to content

Commit 18d127c

Browse files
sip-21ca-ruz
authored andcommitted
sauron: add tests
1 parent 6e53004 commit 18d127c

6 files changed

+709
-0
lines changed
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
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["bitcoin"] = utils.BitcoinD
12+
13+
14+
class LightningNode(utils.LightningNode):
15+
def __init__(self, *args, **kwargs):
16+
pyln.testing.utils.TEST_NETWORK = "bitcoin"
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": "bitcoin",
23+
"plugin": os.path.join(os.path.dirname(__file__), "../sauron.py"),
24+
"sauron-api-endpoint": "https://blockstream.info/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", "bitcoin")
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"] == "main"
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": "0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a29ab5f49ffff001d1dac2b7c0101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000",
65+
"blockhash": "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f",
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_response = {
77+
"errmsg": 'sendrawtransaction RPC error: {"code":-22,"message":"TX decode failed. Make sure the tx has at least one input."}',
78+
"success": False,
79+
}
80+
response = ln_node.rpc.call(
81+
"sendrawtransaction",
82+
{"tx": "invalid-raw-tx"},
83+
)
84+
85+
assert response == expected_response
86+
87+
88+
def test_rpc_getutxout(node_factory):
89+
"""
90+
Test getutxout
91+
"""
92+
ln_node = node_factory.get_node()
93+
94+
expected_response = {
95+
"amount": 1000000000,
96+
"script": "4104b5abd412d4341b45056d3e376cd446eca43fa871b51961330deebd84423e740daa520690e1d9e074654c59ff87b408db903649623e86f1ca5412786f61ade2bfac",
97+
}
98+
response = ln_node.rpc.call(
99+
"getutxout",
100+
{
101+
# block 181
102+
"txid": "a16f3ce4dd5deb92d98ef5cf8afeaf0775ebca408f708b2146c4fb42b41e14be",
103+
"vout": 0,
104+
},
105+
)
106+
assert response == expected_response
107+
108+
109+
def test_rpc_estimatefees(node_factory):
110+
"""
111+
Test estimatefees
112+
"""
113+
114+
ln_node = node_factory.get_node()
115+
116+
# Sample response
117+
# {
118+
# "opening": 4477,
119+
# "mutual_close": 4477,
120+
# "unilateral_close": 11929,
121+
# "delayed_to_us": 4477,
122+
# "htlc_resolution": 5652,
123+
# "penalty": 5652,
124+
# "min_acceptable": 1060,
125+
# "max_acceptable": 119290,
126+
# "feerate_floor": 1520,
127+
# "feerates": [
128+
# {"blocks": 2, "feerate": 11929},
129+
# {"blocks": 6, "feerate": 5652},
130+
# {"blocks": 12, "feerate": 4477},
131+
# {"blocks": 144, "feerate": 2120}
132+
# ]
133+
# }
134+
response = ln_node.rpc.call("estimatefees")
135+
136+
137+
138+
expected_response_keys = [
139+
"opening",
140+
"mutual_close",
141+
"unilateral_close",
142+
"delayed_to_us",
143+
"htlc_resolution",
144+
"penalty",
145+
"min_acceptable",
146+
"max_acceptable",
147+
"feerate_floor",
148+
"feerates",
149+
]
150+
assert list(response.keys()) == expected_response_keys
151+
152+
expected_feerates_keys = ("blocks", "feerate")
153+
assert (
154+
list(set([tuple(entry.keys()) for entry in response["feerates"]]))[0]
155+
== expected_feerates_keys
156+
)
157+
158+
expected_feerates_blocks = [2, 6, 12, 144]
159+
assert [
160+
entry["blocks"] for entry in response["feerates"]
161+
] == expected_feerates_blocks
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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

Comments
 (0)