From 9a0f237019f3956ea0467e7fb1afee228c90ef3d Mon Sep 17 00:00:00 2001 From: tecnovert Date: Wed, 31 Jul 2019 20:21:41 +0200 Subject: [PATCH] Remember coin on rpc page and check chains are synced to send offers and bids. --- basicswap/basicswap.py | 24 ++++++++++++++++++++++++ basicswap/http_server.py | 4 +++- basicswap/templates/rpc.html | 4 ++-- bin/basicswap_prepare.py | 9 ++++++++- 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/basicswap/basicswap.py b/basicswap/basicswap.py index f94d1ce..d64a302 100644 --- a/basicswap/basicswap.py +++ b/basicswap/basicswap.py @@ -541,6 +541,15 @@ class BasicSwap(): self.log.error('Can\'t connect to %s RPC, exiting.', coin_type) self.stopRunning(1) # systemd will try restart if fail_code != 0 + def checkSynced(self, coin_from, coin_to): + check_coins = [coin_from, coin_to] + for c in check_coins: + if self.coin_clients[c]['connection_type'] != 'rpc': + continue + synced = round(self.callcoinrpc(c, 'getblockchaininfo')['verificationprogress'], 3) + if synced < 1.0: + raise ValueError('{} chain is still syncing, currently at {}.'.format(synced)) + def setIntKV(self, str_key, int_val): session = scoped_session(self.session_factory) kv = session.query(DBKVInt).filter_by(key=str_key).first() @@ -663,6 +672,7 @@ class BasicSwap(): self.mxDB.acquire() try: + self.checkSynced(coin_from_t, coin_to_t) proof_addr, proof_sig = self.getProofOfFunds(coin_from_t, amount) # TODO: require prrof of funds on offers? @@ -976,6 +986,8 @@ class BasicSwap(): coin_from = Coins(offer.coin_from) coin_to = Coins(offer.coin_to) + self.checkSynced(coin_from, coin_to) + contract_count = self.getNewContractId() now = int(time.time()) @@ -1628,6 +1640,18 @@ class BasicSwap(): return self.callcoinrpc(coin_type, 'getblockchaininfo')['blocks'] def lookupUnspentByAddress(self, coin_type, address, sum_output=False, assert_amount=None, assert_txid=None): + + # TODO: Lookup from explorers + + if assert_txid != None: + try: + ro = self.callcoinrpc(coin_type, 'getmempoolentry', [assert_txid]) + self.log.debug('Tx %s found in mempool, fee %s', assert_txid, ro['fee']) + # TODO: Save info + return None + except Exception: + pass + num_blocks = self.callcoinrpc(coin_type, 'getblockchaininfo')['blocks'] sum_unspent = 0 diff --git a/basicswap/http_server.py b/basicswap/http_server.py index 67066dd..c164f57 100644 --- a/basicswap/http_server.py +++ b/basicswap/http_server.py @@ -150,6 +150,7 @@ class HttpHandler(BaseHTTPRequestHandler): swap_client = self.server.swap_client result = None + coin_type = -1 messages = [] form_data = self.checkForm(post_string, 'rpc', messages) if form_data: @@ -160,7 +161,7 @@ class HttpHandler(BaseHTTPRequestHandler): cmd = form_data[b'cmd'][0].decode('utf-8') try: - result = swap_client.callcoincli(coin_type, cmd) + result = cmd + '\n' + swap_client.callcoincli(coin_type, cmd) except Exception as ex: result = str(ex) @@ -169,6 +170,7 @@ class HttpHandler(BaseHTTPRequestHandler): title=self.server.title, h2=self.server.title, coins=listAvailableCoins(swap_client), + coin_type=coin_type, result=result, form_id=os.urandom(8).hex(), ), 'UTF-8') diff --git a/basicswap/templates/rpc.html b/basicswap/templates/rpc.html index 5f3ad69..bc4e240 100644 --- a/basicswap/templates/rpc.html +++ b/basicswap/templates/rpc.html @@ -7,9 +7,9 @@

- {% for c in coins %} - + {% endfor %}

diff --git a/bin/basicswap_prepare.py b/bin/basicswap_prepare.py index 8ca6f56..3e609fd 100644 --- a/bin/basicswap_prepare.py +++ b/bin/basicswap_prepare.py @@ -270,12 +270,19 @@ def make_rpc_func(bin_dir, data_dir, chain): nonlocal bin_dir nonlocal data_dir nonlocal chain + + # Debug ci + try: + os.system('cat ' + os.path.join(data_dir, 'debug.log')) + except Exception as ex: + logger.error(ex) + return callrpc_cli(bin_dir, data_dir, chain, cmd, cfg.PARTICL_CLI) return rpc_func def waitForRPC(rpc_func, wallet=None): - for i in range(5): + for i in range(10): try: rpc_func('getwalletinfo') return