willexecutor manager improved
This commit is contained in:
194
willexecutors.py
194
willexecutors.py
@@ -1,33 +1,37 @@
|
||||
import json
|
||||
from datetime import datetime
|
||||
from functools import partial
|
||||
import time
|
||||
|
||||
from aiohttp import ClientResponse
|
||||
from electrum import constants
|
||||
from electrum.gui.qt.util import WaitingDialog
|
||||
from electrum.i18n import _
|
||||
from electrum.logging import get_logger
|
||||
from electrum.network import Network
|
||||
|
||||
from .bal import BalPlugin
|
||||
from .util import Util
|
||||
|
||||
DEFAULT_TIMEOUT = 5
|
||||
_logger = get_logger(__name__)
|
||||
|
||||
|
||||
chainname = constants.net.NET_NAME if constants.net.NET_NAME != "mainnet" else "bitcoin"
|
||||
|
||||
|
||||
class Willexecutors:
|
||||
|
||||
def save(bal_plugin, willexecutors):
|
||||
_logger.debug(f"save {willexecutors},{chainname}")
|
||||
aw = bal_plugin.WILLEXECUTORS.get()
|
||||
aw[constants.net.NET_NAME] = willexecutors
|
||||
aw[chainname] = willexecutors
|
||||
bal_plugin.WILLEXECUTORS.set(aw)
|
||||
_logger.debug(f"saved: {aw}")
|
||||
# bal_plugin.WILLEXECUTORS.set(willexecutors)
|
||||
|
||||
def get_willexecutors(
|
||||
bal_plugin, update=False, bal_window=False, force=False, task=True
|
||||
):
|
||||
willexecutors = bal_plugin.WILLEXECUTORS.get()
|
||||
willexecutors = willexecutors.get(constants.net.NET_NAME, {})
|
||||
willexecutors = willexecutors.get(chainname, {})
|
||||
to_del = []
|
||||
for w in willexecutors:
|
||||
if not isinstance(willexecutors[w], dict):
|
||||
@@ -36,12 +40,14 @@ class Willexecutors:
|
||||
Willexecutors.initialize_willexecutor(willexecutors[w], w)
|
||||
for w in to_del:
|
||||
_logger.error(
|
||||
"error Willexecutor to delete type:{} ", type(willexecutor[w]), w
|
||||
"error Willexecutor to delete type:{} {}".format(
|
||||
type(willexecutors[w]), w
|
||||
)
|
||||
)
|
||||
del willexecutors[w]
|
||||
bal = bal_plugin.WILLEXECUTORS.default.get(constants.net.NET_NAME, {})
|
||||
bal = bal_plugin.WILLEXECUTORS.default.get(chainname, {})
|
||||
for bal_url, bal_executor in bal.items():
|
||||
if not bal_url in willexecutors:
|
||||
if bal_url not in willexecutors:
|
||||
_logger.debug(f"force add {bal_url} willexecutor")
|
||||
willexecutors[bal_url] = bal_executor
|
||||
# if update:
|
||||
@@ -75,11 +81,11 @@ class Willexecutors:
|
||||
def is_selected(willexecutor, value=None):
|
||||
if not willexecutor:
|
||||
return False
|
||||
if not value is None:
|
||||
if value is not None:
|
||||
willexecutor["selected"] = value
|
||||
try:
|
||||
return willexecutor["selected"]
|
||||
except:
|
||||
except Exception:
|
||||
willexecutor["selected"] = False
|
||||
return False
|
||||
|
||||
@@ -92,7 +98,7 @@ class Willexecutors:
|
||||
if willexecutor := willitem.we:
|
||||
url = willexecutor["url"]
|
||||
if willexecutor and Willexecutors.is_selected(willexecutor):
|
||||
if not url in willexecutors:
|
||||
if url not in willexecutors:
|
||||
willexecutor["txs"] = ""
|
||||
willexecutor["txsids"] = []
|
||||
willexecutor["broadcast_status"] = _("Waiting...")
|
||||
@@ -102,23 +108,25 @@ class Willexecutors:
|
||||
|
||||
return willexecutors
|
||||
|
||||
def only_selected_list(willexecutors):
|
||||
out = {}
|
||||
for url, v in willexecutors.items():
|
||||
if Willexecutors.is_selected(willexecutor):
|
||||
out[url] = v
|
||||
# def only_selected_list(willexecutors):
|
||||
# out = {}
|
||||
# for url, v in willexecutors.items():
|
||||
# if Willexecutors.is_selected(url):
|
||||
# out[url] = v
|
||||
|
||||
def push_transactions_to_willexecutors(will):
|
||||
willexecutors = get_transactions_to_be_pushed()
|
||||
for url in willexecutors:
|
||||
willexecutor = willexecutors[url]
|
||||
if Willexecutors.is_selected(willexecutor):
|
||||
if "txs" in willexecutor:
|
||||
Willexecutors.push_transactions_to_willexecutor(
|
||||
willexecutors[url]["txs"], url
|
||||
)
|
||||
# def push_transactions_to_willexecutors(will):
|
||||
# willexecutors = Willexecutors.get_transactions_to_be_pushed()
|
||||
# for url in willexecutors:
|
||||
# willexecutor = willexecutors[url]
|
||||
# if Willexecutors.is_selected(willexecutor):
|
||||
# if "txs" in willexecutor:
|
||||
# Willexecutors.push_transactions_to_willexecutor(
|
||||
# willexecutors[url]["txs"], url
|
||||
# )
|
||||
|
||||
def send_request(method, url, data=None, *, timeout=10):
|
||||
def send_request(
|
||||
method, url, data=None, *, timeout=10, handle_response=None, count_reply=0
|
||||
):
|
||||
network = Network.get_instance()
|
||||
if not network:
|
||||
raise Exception("You are offline.")
|
||||
@@ -126,7 +134,8 @@ class Willexecutors:
|
||||
headers = {}
|
||||
headers["user-agent"] = f"BalPlugin v:{BalPlugin.version()}"
|
||||
headers["Content-Type"] = "text/plain"
|
||||
|
||||
if not handle_response:
|
||||
handle_response = Willexecutors.handle_response
|
||||
try:
|
||||
if method == "get":
|
||||
response = Network.send_http_on_proxy(
|
||||
@@ -134,7 +143,7 @@ class Willexecutors:
|
||||
url,
|
||||
params=data,
|
||||
headers=headers,
|
||||
on_finish=Willexecutors.handle_response,
|
||||
on_finish=handle_response,
|
||||
timeout=timeout,
|
||||
)
|
||||
elif method == "post":
|
||||
@@ -143,26 +152,47 @@ class Willexecutors:
|
||||
url,
|
||||
body=data,
|
||||
headers=headers,
|
||||
on_finish=Willexecutors.handle_response,
|
||||
on_finish=handle_response,
|
||||
timeout=timeout,
|
||||
)
|
||||
else:
|
||||
raise Exception(f"unexpected {method=!r}")
|
||||
except TimeoutError:
|
||||
if count_reply < 10:
|
||||
_logger.debug(f"timeout({count_reply}) error: retry in 3 sec...")
|
||||
time.sleep(3)
|
||||
return Willexecutors.send_request(
|
||||
method,
|
||||
url,
|
||||
data,
|
||||
timeout=timeout,
|
||||
handle_response=handle_response,
|
||||
count_reply=count_reply + 1,
|
||||
)
|
||||
else:
|
||||
_logger.debug(f"Too many timeouts: {count_reply}")
|
||||
except Exception as e:
|
||||
_logger.error(f"exception sending request {e}")
|
||||
raise e
|
||||
else:
|
||||
_logger.debug(f"--> {response}")
|
||||
return response
|
||||
|
||||
def get_we_url_from_response(resp):
|
||||
url_slices = str(resp.url).split("/")
|
||||
if len(url_slices) > 2:
|
||||
url_slices = url_slices[:-2]
|
||||
return "/".join(url_slices)
|
||||
|
||||
async def handle_response(resp: ClientResponse):
|
||||
r = await resp.text()
|
||||
try:
|
||||
|
||||
r = json.loads(r)
|
||||
r["status"] = resp.status
|
||||
r["selected"] = Willexecutors.is_selected(willexecutor)
|
||||
r["url"] = url
|
||||
except:
|
||||
# url = Willexecutors.get_we_url_from_response(resp)
|
||||
# r["url"]= url
|
||||
# r["status"]=resp.status
|
||||
except Exception as e:
|
||||
_logger.debug(f"error handling response:{e}")
|
||||
pass
|
||||
return r
|
||||
|
||||
@@ -175,7 +205,7 @@ class Willexecutors:
|
||||
_logger.debug(f"{willexecutor['url']}: {willexecutor['txs']}")
|
||||
if w := Willexecutors.send_request(
|
||||
"post",
|
||||
willexecutor["url"] + "/" + constants.net.NET_NAME + "/pushtxs",
|
||||
willexecutor["url"] + "/" + chainname + "/pushtxs",
|
||||
data=willexecutor["txs"].encode("ascii"),
|
||||
):
|
||||
willexecutor["broadcast_status"] = _("Success")
|
||||
@@ -203,18 +233,14 @@ class Willexecutors:
|
||||
try:
|
||||
_logger.info("GETINFO_WILLEXECUTOR")
|
||||
_logger.debug(url)
|
||||
netname = "bitcoin"
|
||||
if constants.net.NET_NAME != "mainnet":
|
||||
netname = constants.net.NET_NAME
|
||||
w = Willexecutors.send_request("get", url + "/" + netname + "/info")
|
||||
|
||||
willexecutor["url"] = url
|
||||
willexecutor["status"] = w["status"]
|
||||
willexecutor["base_fee"] = w["base_fee"]
|
||||
willexecutor["address"] = w["address"]
|
||||
if not willexecutor["info"]:
|
||||
w = Willexecutors.send_request("get", url + "/" + chainname + "/info")
|
||||
if isinstance(w, dict):
|
||||
willexecutor["url"] = url
|
||||
willexecutor["status"] = 200
|
||||
willexecutor["base_fee"] = w["base_fee"]
|
||||
willexecutor["address"] = w["address"]
|
||||
willexecutor["info"] = w["info"]
|
||||
_logger.debug(f"response_data {w['address']}")
|
||||
_logger.debug(f"response_data {w}")
|
||||
except Exception as e:
|
||||
_logger.error(f"error {e} contacting {url}: {w}")
|
||||
willexecutor["status"] = "KO"
|
||||
@@ -222,24 +248,33 @@ class Willexecutors:
|
||||
willexecutor["last_update"] = datetime.now().timestamp()
|
||||
return willexecutor
|
||||
|
||||
def initialize_willexecutor(willexecutor, url, status=None, selected=None):
|
||||
def initialize_willexecutor(willexecutor, url, status=None, old_willexecutor={}):
|
||||
willexecutor["url"] = url
|
||||
if not status is None:
|
||||
if status is not None:
|
||||
willexecutor["status"] = status
|
||||
willexecutor["selected"] = Willexecutors.is_selected(willexecutor, selected)
|
||||
else:
|
||||
willexecutor["status"] = old_willexecutor.get("status",willexecutor.get("status","Ko"))
|
||||
willexecutor["selected"]=Willexecutors.is_selected(old_willexecutor) or willexecutor.get("selected",False)
|
||||
willexecutor["address"]=old_willexecutor.get("address",willexecutor.get("address",""))
|
||||
willexecutor["promo_code"]=old_willexecutor.get("promo_code",willexecutor.get("promo_code"))
|
||||
|
||||
def download_list(bal_plugin):
|
||||
|
||||
|
||||
def download_list(bal_plugin,old_willexecutors):
|
||||
try:
|
||||
l = Willexecutors.send_request(
|
||||
"get", "https://welist.bitcoin-after.life/data/bitcoin?page=0&limit=100"
|
||||
willexecutors = Willexecutors.send_request(
|
||||
"get",
|
||||
f"https://welist.bitcoin-after.life/data/{chainname}?page=0&limit=100",
|
||||
)
|
||||
del l["status"]
|
||||
for w in l:
|
||||
willexecutor = l[w]
|
||||
Willexecutors.initialize_willexecutor(willexecutor, w, "New", False)
|
||||
# del willexecutors["status"]
|
||||
for w in willexecutors:
|
||||
if w not in ("status", "url"):
|
||||
Willexecutors.initialize_willexecutor(
|
||||
willexecutors[w], w, None, old_willexecutors.get(w,{})
|
||||
)
|
||||
# bal_plugin.WILLEXECUTORS.set(l)
|
||||
# bal_plugin.config.set_key(bal_plugin.WILLEXECUTORS,l,save=True)
|
||||
return l
|
||||
return willexecutors
|
||||
|
||||
except Exception as e:
|
||||
_logger.error(f"Failed to download willexecutors list: {e}")
|
||||
@@ -253,7 +288,7 @@ class Willexecutors:
|
||||
willexecutor = willexecutors[w]
|
||||
Willexecutors.initialize_willexecutor(willexecutor, w, "New", False)
|
||||
# bal_plugin.WILLEXECUTORS.set(willexecutors)
|
||||
return h
|
||||
return willexecutors
|
||||
except Exception as e:
|
||||
_logger.error(f"error opening willexecutors json: {e}")
|
||||
|
||||
@@ -270,14 +305,53 @@ class Willexecutors:
|
||||
_logger.error(f"error contacting {url} for checking txs {e}")
|
||||
raise e
|
||||
|
||||
def compute_id(willexecutor):
|
||||
return "{}-{}".format(willexecutor.get("url"), willexecutor.get("chain"))
|
||||
|
||||
|
||||
class WillExecutor:
|
||||
def __init__(self, url, base_fee, chain, info, version):
|
||||
def __init__(
|
||||
self,
|
||||
url,
|
||||
base_fee,
|
||||
chain,
|
||||
info,
|
||||
version,
|
||||
status,
|
||||
is_selected=False,
|
||||
promo_code="",
|
||||
):
|
||||
self.url = url
|
||||
self.base_fee = base_fee
|
||||
self.chain = chain
|
||||
self.info = info
|
||||
self.version = version
|
||||
self.status = status
|
||||
self.promo_code = promo_code
|
||||
self.is_selected = is_selected
|
||||
self.id = self.compute_id()
|
||||
|
||||
def from_dict(d):
|
||||
we = WillExecutor(d["url"], d["base_fee"], d["chain"], d["info"], d["version"])
|
||||
return WillExecutor(
|
||||
url=d.get("url", "http://localhost:8000"),
|
||||
base_fee=d.get("base_fee", 1000),
|
||||
chain=d.get("chain", chainname),
|
||||
info=d.get("info", ""),
|
||||
version=d.get("version", 0),
|
||||
status=d.get("status", "Ko"),
|
||||
is_selected=d.get("is_selected", "False"),
|
||||
promo_code=d.get("promo_code", ""),
|
||||
)
|
||||
|
||||
def to_dict(self):
|
||||
return {
|
||||
"url": self.url,
|
||||
"base_fee": self.base_fee,
|
||||
"chain": self.chain,
|
||||
"info": self.info,
|
||||
"version": self.version,
|
||||
"promo_code": self.promo_code,
|
||||
}
|
||||
|
||||
def compute_id(self):
|
||||
return f"{self.url}-{self.chain}"
|
||||
|
||||
Reference in New Issue
Block a user