Debian Codegraph @

tests/toyserver/toynetwork.py

raw symbols SCIP
1 # Copyright (C) 2026 The Electrum developers
2 # Distributed under the MIT software license, see the accompanying
3 # file LICENCE or http://www.opensource.org/licenses/mit-license.php
4
5 import asyncio
6
7 from electrum import blockchain, util
8 from electrum.blockchain import Blockchain
9 from electrum.interface import Interface, ServerAddr
10 from electrum.simple_config import SimpleConfig
11 from electrum.transaction import Transaction
12 from electrum.util import OldTaskGroup
13
14 from .toyserver import ToyServer
15
16
17 class ToyNetwork:
18 """Client-side stand-in for Network, driving a single Interface against a ToyServer.
19
20 Unlike the other MockNetworks in the test suite (tests/lnhelpers.py,
21 tests/test_txbatcher.py), this one has a real chain behind it: the local height,
22 the wallet history and the mempool all come from the server.
23 """
24
25 def __init__(self, *, config: SimpleConfig):
26 self.config = config
27 self.asyncio_loop = util.get_asyncio_loop()
28 self.taskgroup = OldTaskGroup()
29 blockchain.read_blockchains(self.config)
30 blockchain.init_headers_file_for_best_chain()
31 self.proxy = None
32 self.debug = True
33 self.bhi_lock = asyncio.Lock()
34 self.interface = None # type: Interface | None
35 self.relay_fee = None # type: int | None # sat/kbyte, set from the server on connect
36
37 async def connect(self, server: ToyServer, *, client_name: str = None) -> Interface:
38 """connect to server, and wait until we have synced its headers"""
39 assert self.interface is None, "already connected"
40 interface = Interface(network=self, server=ServerAddr(host="127.0.0.1", port=server.server_port, protocol="t"))
41 if client_name is not None: # so that the server can identify this session
42 interface.client_name = lambda: client_name
43 self.interface = interface
44 async with util.async_timeout(5):
45 await interface.ready
46 await interface._blockchain_updated.wait()
47 self.relay_fee = await interface.get_relay_fee()
48 return interface
49
50 async def stop(self) -> None:
51 if self.interface:
52 await self.interface.close()
53
54 async def connection_down(self, interface: Interface):
55 pass
56 def get_network_timeout_seconds(self, request_type) -> int:
57 return 10
58 def check_interface_against_healthy_spread_of_connected_servers(self, iface_to_check: Interface) -> bool:
59 return True
60 def update_fee_estimates(self, *, fee_est: dict[int, int] = None):
61 pass
62 async def switch_unwanted_fork_interface(self):
63 pass
64 async def switch_lagging_interface(self):
65 pass
66 def blockchain(self) -> Blockchain:
67 return self.interface.blockchain
68 def get_local_height(self) -> int:
69 return self.blockchain().height()
70 def is_connected(self) -> bool:
71 return True
72
73 # the wallet and the txbatcher broadcast through the network:
74
75 async def broadcast_transaction(self, tx: Transaction) -> None:
76 await self.interface.broadcast_transaction(tx)
77
78 async def try_broadcasting(self, tx: Transaction, name: str) -> bool:
79 try:
80 await self.broadcast_transaction(tx)
81 except Exception:
82 return False
83 return True