| 1 |
import asyncio |
| 2 |
from aiorpcx import RPCError |
| 3 |
|
| 4 |
from electrum import util |
| 5 |
from electrum.bitcoin import COIN |
| 6 |
from electrum.interface import ServerAddr, PaddedRSTransport |
| 7 |
from electrum.util import bfh |
| 8 |
from electrum.simple_config import SimpleConfig |
| 9 |
from electrum.transaction import Transaction, TxOutput |
| 10 |
from electrum.wallet import Abstract_Wallet |
| 11 |
|
| 12 |
from . import ElectrumTestCase |
| 13 |
from . import restore_wallet_from_text__for_unittest |
| 14 |
from .toyserver.toynetwork import ToyNetwork |
| 15 |
from .toyserver.toyserver import ToyServer, ToyServerSession |
| 16 |
|
| 17 |
|
| 18 |
class TestServerAddr(ElectrumTestCase): |
| 19 |
|
| 20 |
def test_from_str(self): |
| 21 |
self.assertEqual(ServerAddr(host="104.198.149.61", port=80, protocol="t"), |
| 22 |
ServerAddr.from_str("104.198.149.61:80:t")) |
| 23 |
self.assertEqual(ServerAddr(host="ecdsa.net", port=110, protocol="s"), |
| 24 |
ServerAddr.from_str("ecdsa.net:110:s")) |
| 25 |
self.assertEqual(ServerAddr(host="2400:6180:0:d1::86b:e001", port=50002, protocol="s"), |
| 26 |
ServerAddr.from_str("[2400:6180:0:d1::86b:e001]:50002:s")) |
| 27 |
self.assertEqual(ServerAddr(host="localhost", port=8080, protocol="s"), |
| 28 |
ServerAddr.from_str("localhost:8080:s")) |
| 29 |
|
| 30 |
def test_from_str_with_inference(self): |
| 31 |
self.assertEqual(None, ServerAddr.from_str_with_inference("104.198.149.61")) |
| 32 |
self.assertEqual(None, ServerAddr.from_str_with_inference("ecdsa.net")) |
| 33 |
self.assertEqual(None, ServerAddr.from_str_with_inference("2400:6180:0:d1::86b:e001")) |
| 34 |
self.assertEqual(None, ServerAddr.from_str_with_inference("[2400:6180:0:d1::86b:e001]")) |
| 35 |
|
| 36 |
self.assertEqual(ServerAddr(host="104.198.149.61", port=80, protocol="s"), |
| 37 |
ServerAddr.from_str_with_inference("104.198.149.61:80")) |
| 38 |
self.assertEqual(ServerAddr(host="ecdsa.net", port=110, protocol="s"), |
| 39 |
ServerAddr.from_str_with_inference("ecdsa.net:110")) |
| 40 |
self.assertEqual(ServerAddr(host="2400:6180:0:d1::86b:e001", port=50002, protocol="s"), |
| 41 |
ServerAddr.from_str_with_inference("[2400:6180:0:d1::86b:e001]:50002")) |
| 42 |
|
| 43 |
self.assertEqual(ServerAddr(host="104.198.149.61", port=80, protocol="t"), |
| 44 |
ServerAddr.from_str_with_inference("104.198.149.61:80:t")) |
| 45 |
self.assertEqual(ServerAddr(host="ecdsa.net", port=110, protocol="s"), |
| 46 |
ServerAddr.from_str_with_inference("ecdsa.net:110:s")) |
| 47 |
self.assertEqual(ServerAddr(host="2400:6180:0:d1::86b:e001", port=50002, protocol="s"), |
| 48 |
ServerAddr.from_str_with_inference("[2400:6180:0:d1::86b:e001]:50002:s")) |
| 49 |
|
| 50 |
def test_to_friendly_name(self): |
| 51 |
self.assertEqual("104.198.149.61:80:t", |
| 52 |
ServerAddr(host="104.198.149.61", port=80, protocol="t").to_friendly_name()) |
| 53 |
self.assertEqual("ecdsa.net:110", |
| 54 |
ServerAddr(host="ecdsa.net", port=110, protocol="s").to_friendly_name()) |
| 55 |
self.assertEqual("ecdsa.net:50001:t", |
| 56 |
ServerAddr(host="ecdsa.net", port=50001, protocol="t").to_friendly_name()) |
| 57 |
self.assertEqual("[2400:6180:0:d1::86b:e001]:50002", |
| 58 |
ServerAddr(host="2400:6180:0:d1::86b:e001", port=50002, protocol="s").to_friendly_name()) |
| 59 |
self.assertEqual("[2400:6180:0:d1::86b:e001]:50001:t", |
| 60 |
ServerAddr(host="2400:6180:0:d1::86b:e001", port=50001, protocol="t").to_friendly_name()) |
| 61 |
|
| 62 |
|
| 63 |
class TestInterface(ElectrumTestCase): |
| 64 |
REGTEST = True |
| 65 |
|
| 66 |
def setUp(self): |
| 67 |
super().setUp() |
| 68 |
self.config = SimpleConfig({'electrum_path': self.electrum_path}) |
| 69 |
self.config.NETWORK_SKIPMERKLECHECK = True |
| 70 |
self._orig_WAIT_FOR_BUFFER_GROWTH_SECONDS = PaddedRSTransport.WAIT_FOR_BUFFER_GROWTH_SECONDS |
| 71 |
PaddedRSTransport.WAIT_FOR_BUFFER_GROWTH_SECONDS = 0 |
| 72 |
|
| 73 |
def tearDown(self): |
| 74 |
PaddedRSTransport.WAIT_FOR_BUFFER_GROWTH_SECONDS = self._orig_WAIT_FOR_BUFFER_GROWTH_SECONDS |
| 75 |
super().tearDown() |
| 76 |
|
| 77 |
async def asyncSetUp(self): |
| 78 |
await super().asyncSetUp() |
| 79 |
self._toyserver = ToyServer() |
| 80 |
await self._toyserver.start() |
| 81 |
self.network = ToyNetwork(config=self.config) |
| 82 |
for _ in range(10): |
| 83 |
await self._toyserver.mine_block() |
| 84 |
await self._toyserver.set_up_faucet(config=self.config) |
| 85 |
|
| 86 |
async def asyncTearDown(self): |
| 87 |
await self.network.stop() |
| 88 |
await self._toyserver.stop() |
| 89 |
await super().asyncTearDown() |
| 90 |
|
| 91 |
async def _start_iface_and_wait_for_sync(self): |
| 92 |
return await self.network.connect(self._toyserver, client_name="alice") |
| 93 |
|
| 94 |
def _get_server_session(self) -> ToyServerSession: |
| 95 |
return self._toyserver.get_session_by_name("alice") |
| 96 |
|
| 97 |
async def test_client_syncs_headers_to_tip(self): |
| 98 |
interface = await self._start_iface_and_wait_for_sync() |
| 99 |
self.assertEqual(self._toyserver.cur_height, interface.tip) |
| 100 |
self.assertFalse(interface.got_disconnected.is_set()) |
| 101 |
|
| 102 |
async def test_transaction_get(self): |
| 103 |
interface = await self._start_iface_and_wait_for_sync() |
| 104 |
|
| 105 |
self._toyserver._add_tx(Transaction("020000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff025100ffffffff0200f2052a010000001600140297bde2689a3c79ffe050583b62f86f2d9dae540000000000000000266a24aa21a9ede2f61c3f71d1defd3fa999dfa36953755c690689799962b48bebd836974e8cf90120000000000000000000000000000000000000000000000000000000000000000000000000")) |
| 106 |
|
| 107 |
with self.assertRaises(RPCError) as ctx: |
| 108 |
await interface.get_transaction("deadbeef"*8) |
| 109 |
self.assertTrue("unknown txid" in ctx.exception.message) |
| 110 |
|
| 111 |
rawtx = await interface.get_transaction("bdae818ad3c1f261317738ae9284159bf54874356f186dbc7afd631dc1527fcb") |
| 112 |
self.assertEqual(rawtx, self._toyserver.txs["bdae818ad3c1f261317738ae9284159bf54874356f186dbc7afd631dc1527fcb"].hex()) |
| 113 |
self.assertEqual(self._get_server_session()._method_counts["blockchain.transaction.get"], 2) |
| 114 |
|
| 115 |
async def test_transaction_broadcast(self): |
| 116 |
interface = await self._start_iface_and_wait_for_sync() |
| 117 |
rawtx1 = "020000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff025200ffffffff0200f2052a010000001600140297bde2689a3c79ffe050583b62f86f2d9dae540000000000000000266a24aa21a9ede2f61c3f71d1defd3fa999dfa36953755c690689799962b48bebd836974e8cf90120000000000000000000000000000000000000000000000000000000000000000000000000" |
| 118 |
tx = Transaction(rawtx1) |
| 119 |
|
| 120 |
await interface.broadcast_transaction(tx) |
| 121 |
self.assertEqual(bfh(rawtx1), self._toyserver.txs.get(tx.txid())) |
| 122 |
|
| 123 |
|
| 124 |
self.assertEqual(self._get_server_session()._method_counts["blockchain.transaction.get"], 0) |
| 125 |
rawtx2 = await interface.get_transaction(tx.txid()) |
| 126 |
self.assertEqual(rawtx1, rawtx2) |
| 127 |
self.assertEqual(self._get_server_session()._method_counts["blockchain.transaction.get"], 0) |
| 128 |
|
| 129 |
async def test_dont_request_gethistory_if_status_change_results_from_mempool_txs_simply_getting_mined(self): |
| 130 |
"""After a new block is mined, we recv "blockchain.scripthash.subscribe" notifs. |
| 131 |
We opportunistically guess the scripthash status changed purely because touching mempool txs just got mined. |
| 132 |
If the guess is correct, we won't call the "blockchain.scripthash.get_history" RPC. |
| 133 |
""" |
| 134 |
interface = await self._start_iface_and_wait_for_sync() |
| 135 |
server_blockheight = interface.tip |
| 136 |
w1 = restore_wallet_from_text__for_unittest("9dk", path=None, config=self.config)['wallet'] |
| 137 |
w1.start_network(self.network) |
| 138 |
await w1.up_to_date_changed_event.wait() |
| 139 |
self.assertEqual(self._get_server_session()._method_counts["blockchain.scripthash.get_history"], 0) |
| 140 |
|
| 141 |
w1_addr = w1.get_receiving_address() |
| 142 |
funding_tx = await self._toyserver.ask_faucet([TxOutput.from_address_and_value(w1_addr, 1 * COIN)]) |
| 143 |
funding_txid = funding_tx.txid() |
| 144 |
await w1.up_to_date_changed_event.wait() |
| 145 |
while not w1.is_up_to_date(): |
| 146 |
await w1.up_to_date_changed_event.wait() |
| 147 |
self.assertEqual(self._get_server_session()._method_counts["blockchain.scripthash.get_history"], 1) |
| 148 |
self.assertEqual( |
| 149 |
w1.adb.get_address_history(w1_addr), |
| 150 |
{funding_txid: 0}) |
| 151 |
|
| 152 |
await self._toyserver.mine_block() |
| 153 |
server_blockheight += 1 |
| 154 |
await w1.up_to_date_changed_event.wait() |
| 155 |
while not w1.is_up_to_date(): |
| 156 |
await w1.up_to_date_changed_event.wait() |
| 157 |
|
| 158 |
self.assertEqual(self._get_server_session()._method_counts["blockchain.scripthash.get_history"], 1) |
| 159 |
self.assertEqual( |
| 160 |
w1.adb.get_address_history(w1_addr), |
| 161 |
{funding_txid: server_blockheight}) |
| 162 |
|
| 163 |
async def test_we_disconnect_on_incoming_request(self): |
| 164 |
"""We don't expect the server to send us any requests of its own.""" |
| 165 |
interface = await self._start_iface_and_wait_for_sync() |
| 166 |
with self.assertLogs('electrum', level='INFO') as logs: |
| 167 |
with self.assertRaises(asyncio.CancelledError): |
| 168 |
await self._get_server_session().send_request('blockchain.block.header', [999]) |
| 169 |
self.assertTrue(any(("Interface.[127.0.0.1:" in msg and "unexpected request. not a notification" in msg) |
| 170 |
for msg in logs.output)) |
| 171 |
|
| 172 |
async def test_we_disconnect_on_incoming_notification_spam(self): |
| 173 |
"""We don't expect the server to send us any requests of its own.""" |
| 174 |
interface = await self._start_iface_and_wait_for_sync() |
| 175 |
|
| 176 |
assert interface.session.cost_hard_limit > 0 |
| 177 |
interface.session.cost_hard_limit /= 30 |
| 178 |
interface.session.cost_soft_limit = interface.session.cost_hard_limit - 1 |
| 179 |
interface.session.bw_cost_per_byte *= 10 |
| 180 |
with self.assertLogs('electrum', level='INFO') as logs: |
| 181 |
|
| 182 |
srv_sess = self._get_server_session() |
| 183 |
headersub_res = (srv_sess._get_headersub_result(),) |
| 184 |
spam_count = 200_000 |
| 185 |
for i in range(spam_count): |
| 186 |
if srv_sess.got_disconnected.is_set(): |
| 187 |
break |
| 188 |
await srv_sess.send_notification('blockchain.headers.subscribe', headersub_res) |
| 189 |
|
| 190 |
await asyncio.sleep(0) |
| 191 |
assert srv_sess.got_disconnected.is_set() |
| 192 |
async with util.async_timeout(1): |
| 193 |
await interface.got_disconnected.wait() |
| 194 |
|
| 195 |
assert interface.session.recv_count < spam_count / 20 |
| 196 |
assert interface.session.cost > interface.session.cost_hard_limit |
| 197 |
self.assertTrue(any(("Interface.[127.0.0.1:" in msg and "closing session over resource usage" in msg) |
| 198 |
for msg in logs.output)) |