From 31bf80f579c6009a441db5b298569b8fb5940389 Mon Sep 17 00:00:00 2001 From: tecnovert Date: Fri, 18 Dec 2020 23:04:06 +0200 Subject: [PATCH] Fix fee_src error. --- basicswap/basicswap.py | 9 ++++- basicswap/http_server.py | 5 ++- basicswap/js_server.py | 4 ++ basicswap/network.py | 66 +++++++++++++++++++++++---------- tests/basicswap/test_network.py | 31 ++++++++++++++-- 5 files changed, 89 insertions(+), 26 deletions(-) diff --git a/basicswap/basicswap.py b/basicswap/basicswap.py index 92c25ea..c0ed320 100644 --- a/basicswap/basicswap.py +++ b/basicswap/basicswap.py @@ -2239,7 +2239,7 @@ class BasicSwap(BaseApp): prevout_s = ' in={}:{}'.format(prev_txnid, prev_n) if fee_rate is None: - fee_rate = self.getFeeRateForCoin(coin_type) + fee_rate, fee_src = self.getFeeRateForCoin(coin_type) tx_vsize = self.getContractSpendTxVSize(coin_type) tx_fee = (fee_rate * tx_vsize) / 1000 @@ -2335,7 +2335,7 @@ class BasicSwap(BaseApp): sequence = 1 prevout_s = ' in={}:{}:{}'.format(txjs['txid'], vout, sequence) - fee_rate = self.getFeeRateForCoin(coin_type) + fee_rate, fee_src = self.getFeeRateForCoin(coin_type) tx_vsize = self.getContractSpendTxVSize(coin_type, False) tx_fee = (fee_rate * tx_vsize) / 1000 @@ -4768,3 +4768,8 @@ class BasicSwap(BaseApp): def add_connection(self, host, port, peer_pubkey): self.log.info('add_connection %s %d %s', host, port, peer_pubkey.hex()) self._network.add_connection(host, port, peer_pubkey) + + def get_network_info(self): + if not self._network: + return {'Error': 'Not Initialised'} + return self._network.get_info() diff --git a/basicswap/http_server.py b/basicswap/http_server.py index 82a1f71..ccf9dc9 100644 --- a/basicswap/http_server.py +++ b/basicswap/http_server.py @@ -38,6 +38,7 @@ from .js_server import ( js_sentoffers, js_bids, js_sentbids, + js_network, js_index, ) from .ui import ( @@ -246,13 +247,14 @@ class HttpHandler(BaseHTTPRequestHandler): continue ci = swap_client.ci(k) - fee_rate = swap_client.getFeeRateForCoin(k) + fee_rate, fee_src = swap_client.getFeeRateForCoin(k) est_fee = swap_client.estimateWithdrawFee(k, fee_rate) wallets_formatted.append({ 'name': w['name'], 'version': w['version'], 'cid': str(int(k)), 'fee_rate': ci.format_amount(int(fee_rate * ci.COIN())), + 'fee_rate_src': fee_src, 'est_fee': 'Unknown' if est_fee is None else ci.format_amount(int(est_fee * ci.COIN())), 'balance': w['balance'], 'blocks': w['blocks'], @@ -797,6 +799,7 @@ class HttpHandler(BaseHTTPRequestHandler): 'sentoffers': js_sentoffers, 'bids': js_bids, 'sentbids': js_sentbids, + 'network': js_network, }.get(url_split[2], js_index) return func(self, url_split, post_string) except Exception as ex: diff --git a/basicswap/js_server.py b/basicswap/js_server.py index d699d81..cb4339e 100644 --- a/basicswap/js_server.py +++ b/basicswap/js_server.py @@ -153,5 +153,9 @@ def js_sentbids(self, url_split, post_string): return bytes(json.dumps(self.server.swap_client.listBids(sent=True)), 'UTF-8') +def js_network(self, url_split, post_string): + return bytes(json.dumps(self.server.swap_client.get_network_info()), 'UTF-8') + + def js_index(self, url_split, post_string): return bytes(json.dumps(self.server.swap_client.getSummary()), 'UTF-8') diff --git a/basicswap/network.py b/basicswap/network.py index bd2dcdb..0370b0a 100644 --- a/basicswap/network.py +++ b/basicswap/network.py @@ -99,7 +99,7 @@ class MsgHandshake: class Peer: __slots__ = ( - '_mx', '_pubkey', '_address', '_socket', '_version', '_ready', + '_mx', '_pubkey', '_address', '_socket', '_version', '_ready', '_incoming', '_connected_at', '_last_received_at', '_bytes_sent', '_bytes_received', '_receiving_length', '_receiving_buffer', '_recv_messages', '_misbehaving_score', '_ke', '_km', '_dir', '_sent_nonce', '_recv_nonce', '_last_handshake_at', @@ -111,7 +111,8 @@ class Peer: self._address = address self._socket = socket self._version = None - self._ready = False # True When handshake is complete + self._ready = False # True when handshake is complete + self._incoming = False self._connected_at = time.time() self._last_received_at = 0 self._last_handshake_at = 0 @@ -122,7 +123,7 @@ class Peer: self._receiving_length = 0 self._receiving_buffer = None self._recv_messages = queue.Queue() # Built in mutex - self._misbehaving_score = 0 + self._misbehaving_score = 0 # TODO: Must be persistent - save to db self._ping_nonce = 0 self._last_ping_at = 0 # ms @@ -146,7 +147,9 @@ def listen_thread(cls): if s == cls._socket: peer_socket, address = cls._socket.accept() logging.info('Connection from %s', address) - cls._peers.append(Peer(address, peer_socket, None)) + new_peer = Peer(address, peer_socket, None) + new_peer._incoming = True + cls._peers.append(new_peer) cls._error_sockets.append(peer_socket) cls._read_sockets.append(peer_socket) else: @@ -183,21 +186,22 @@ def msg_thread(cls): while cls._running: processed = False - for peer in cls._peers: - try: - now_us = time.time_ns() // 1000 - if peer._ready is True: - if now_us - peer._last_ping_at >= 5000000: # 5 seconds TODO: Make variable - cls.send_ping(peer) - msg = peer._recv_messages.get(False) - cls.process_message(peer, msg) - processed = True - except queue.Empty: - pass - except Exception as e: - logging.warning('process message error %s', str(e)) - if cls._sc.debug: - traceback.print_exc() + with cls._mx: + for peer in cls._peers: + try: + now_us = time.time_ns() // 1000 + if peer._ready is True: + if now_us - peer._last_ping_at >= 5000000: # 5 seconds TODO: Make variable + cls.send_ping(peer) + msg = peer._recv_messages.get(False) + cls.process_message(peer, msg) + processed = True + except queue.Empty: + pass + except Exception as e: + logging.warning('process message error %s', str(e)) + if cls._sc.debug: + traceback.print_exc() if processed is False: time.sleep(timeout) @@ -572,3 +576,27 @@ class Network: if self._sc.debug: self._sc.log.error('Invalid message received from %s %s', peer._address, str(e)) # TODO: misbehaving + + def test_onion(self, path): + self._sc.log.debug('test_onion packet') + + plaintext = 'test' + + def get_info(self): + rv = {} + + peers = [] + with self._mx: + for peer in self._peers: + peer_info = { + 'pubkey': 'Unknown' if not peer._pubkey else peer._pubkey.hex(), + 'address': '{}:{}'.format(peer._address[0], peer._address[1]), + 'bytessent': peer._bytes_sent, + 'bytesrecv': peer._bytes_received, + 'ready': peer._ready, + 'incoming': peer._incoming, + } + peers.append(peer_info) + + rv['peers'] = peers + return rv diff --git a/tests/basicswap/test_network.py b/tests/basicswap/test_network.py index 5c40dad..50887a3 100644 --- a/tests/basicswap/test_network.py +++ b/tests/basicswap/test_network.py @@ -25,6 +25,7 @@ from basicswap.basicswap import ( from basicswap.util import ( COIN, toWIF, + dumpj, ) from basicswap.rpc import ( callrpc, @@ -42,7 +43,6 @@ from tests.basicswap.common import ( make_rpc_func, checkForks, stopDaemons, - wait_for_offer, delay_for, TEST_HTTP_HOST, TEST_HTTP_PORT, @@ -294,16 +294,39 @@ class Test(unittest.TestCase): super(Test, cls).tearDownClass() - def test_01_part_btc(self): - logging.info('---------- Test PART to BTC') + def wait_for_num_nodes(self, port, expect_nodes, wait_for=20): + for i in range(wait_for): + if delay_event.is_set(): + raise ValueError('Test stopped.') + js = json.loads(urlopen('http://localhost:{}/json/network'.format(port)).read()) + num_nodes = 0 + for p in js['peers']: + if p['ready'] is True: + num_nodes += 1 + if num_nodes >= expect_nodes: + return True + delay_event.wait(1) + raise ValueError('wait_for_num_nodes timed out.') + + def test_01_network(self): + + logging.info('---------- Test Network') swap_clients = self.swap_clients js_1 = json.loads(urlopen('http://localhost:1801/json/wallets').read()) offer_id = swap_clients[0].postOffer(Coins.PART, Coins.BTC, 100 * COIN, 0.1 * COIN, 100 * COIN, SwapTypes.SELLER_FIRST) - wait_for_offer(delay_event, swap_clients[1], offer_id) swap_clients[1].add_connection('127.0.0.1', BASE_P2P_PORT + 0, swap_clients[0]._network._network_pubkey) + swap_clients[2].add_connection('127.0.0.1', BASE_P2P_PORT + 0, swap_clients[0]._network._network_pubkey) + + self.wait_for_num_nodes(1800, 2) + + js_n0 = json.loads(urlopen('http://localhost:1800/json/network').read()) + print(dumpj(js_n0)) + + path = [swap_clients[0]._network._network_pubkey, swap_clients[2]._network._network_pubkey] + swap_clients[1].test_onion(path) delay_for(delay_event, 1000)