-
Notifications
You must be signed in to change notification settings - Fork 29
/
utils.py
311 lines (236 loc) · 8.27 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# -*- coding: utf-8 -*-
# Zilliqa Mining Proxy
# Copyright (C) 2019 Gully Chen
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import re
import yaml
import hashlib
from collections import Mapping
from functools import wraps
from cachetools import TTLCache
from concurrent.futures import ThreadPoolExecutor
from zilpool.pyzil import crypto
from zilpool.pyzil import zilliqa_api
cur_dir = os.path.dirname(os.path.abspath(__file__))
app_dir = os.path.join(cur_dir, "..") # warning: take care
def app_path(*args) -> str:
return os.path.join(app_dir, *args)
class MagicDict(dict):
""" A dict with magic, you can access dict value like attributes. """
def __init__(self, *args, **kwargs):
dict.__init__(self, *args, **kwargs)
self.__dict__ = self
def load_config(conf=app_path("default.conf")) -> MagicDict:
""" Load configs from yaml file.
:param conf: config filename
:return: dict
"""
with open(conf, "rb") as f:
return MagicDict(yaml.load(f))
def merge_config(new_conf=None) -> MagicDict:
""" Merge new configs with default.conf """
config = load_config(app_path("default.conf"))
if new_conf:
new_config = load_config(new_conf)
dict_merge(config, new_config)
return MagicDict(config)
def dict_merge(dct, merge_dct) -> None:
""" Recursive dict merge. Inspired by :meth:``dict.update()``, instead of
updating only top-level keys, dict_merge recurse down into dicts nested
to an arbitrary depth, updating keys. The ``merge_dct`` is merged into
``dct``.
:param dct: dict onto which the merge is executed, change inplace
:param merge_dct: new dct merged into dct
:return: None
"""
for k, v in merge_dct.items():
if (k in dct and isinstance(dct[k], dict)
and isinstance(merge_dct[k], Mapping)):
dict_merge(dct[k], merge_dct[k])
else:
dct[k] = merge_dct[k]
re_valid_str = re.compile(r"^[a-zA-Z0-9_.-]*$")
def is_valid_str(input_str: str) -> bool:
return re_valid_str.match(input_str) is not None
def args_to_lower(func):
@wraps(func)
def wrapper(*args, **kwargs):
args = [arg.lower() if isinstance(arg, str) else arg
for arg in args]
return func(*args, **kwargs)
return wrapper
email_re = re.compile(r"\"?([-a-zA-Z0-9.`?{}]+@\w+\.\w+)\"?")
def valid_email(email: str):
if email_re.match(email) is None:
return None
return email.strip().lower()
def valid_addr(wallet_addr: str):
wallet_addr = wallet_addr.lower().strip()
if not wallet_addr.startswith("0x"):
wallet_addr = "0x" + wallet_addr
if len(wallet_addr) != 2 + crypto.ADDRESS_STR_LENGTH:
return None
# noinspection PyBroadException
try:
crypto.hex_str_to_bytes(wallet_addr)
except:
return None
return wallet_addr
def valid_pub_key(pub_key: str):
# noinspection PyBroadException
try:
key = crypto.ZilKey(str_public=pub_key)
except:
return None
return "0x" + key.keypair_str.public
def block_num_to_list(block_num=None):
if block_num == "":
block_num = None
if block_num is None or isinstance(block_num, int):
blocks = [block_num, ]
elif isinstance(block_num, str):
blocks = range_str_to_list(block_num)
elif isinstance(block_num, (tuple, list)):
blocks = block_num
else:
raise TypeError("Invalid block_num")
return blocks
def range_str_to_list(range_str: str):
range_list = []
parts = range_str.split(",")
for p in parts:
if "-" in p:
start, end = map(int, p.split("-", 2))
step = 1 if start <= end else -1
range_list.extend(range(start, end + step, step))
else:
num = int(p)
range_list.append(num)
return range_list
def iso_format(dt):
if dt is None:
return None
return dt.strftime("%Y-%m-%dT%H:%M:%S") + dt.strftime(".%f")[:4] + "Z"
def date_format(dt):
if dt is None:
return None
return dt.strftime("%Y-%m-%d")
SALT_SEP = "$"
def encrypt_password(password, salt=None, sep=SALT_SEP):
assert isinstance(password, str)
if salt is None:
salt = crypto.rand_string(8)
digest = f"{salt}{sep}{password}"
salt_bytes = salt.encode()
for i in range(10):
digest = hashlib.sha256(salt_bytes + digest.encode()).hexdigest()
return f"{salt}{sep}{digest}"
def verify_password(password, password_hash, sep=SALT_SEP):
salt, digest = password_hash.split(sep, 2)
return encrypt_password(password, salt, sep) == password_hash
def get_client_ip(request):
try:
ips = request.headers["X-Forwarded-For"]
except KeyError:
ips = request.transport.get_extra_info("peername")[0]
return ips.split(',')[0]
_thread_pool = None
def get_thread_pool():
global _thread_pool
if _thread_pool is None:
_thread_pool = ThreadPoolExecutor()
return _thread_pool
def run_in_thread(func):
@wraps(func)
def wrapper(*args, **kwargs):
thread_pool = get_thread_pool()
return thread_pool.submit(func, *args, **kwargs)
return wrapper
class Zilliqa:
conf = None
api = None
cache = TTLCache(maxsize=64, ttl=30)
cur_tx_block = 0
cur_ds_block = 0
shard_difficulty = 0
ds_difficulty = 0
avg_block_time = 25 # from constants.xml
@classmethod
def init(cls, conf):
cls.conf = conf["zilliqa"]
cls.api = zilliqa_api.API(cls.conf["api_endpoint"])
@classmethod
def get_cache(cls, key, func, *args, **kwargs):
val = cls.cache.get(key)
if val is None:
val = func(*args, **kwargs)
try:
cls.cache[key] = val
except KeyError:
pass
return val
@classmethod
def clear_cache(cls, key=None):
if key is None:
cls.cache.clear()
else:
cls.cache.pop(key, None)
@classmethod
def update_avg_block_time(cls, avg_time):
cls.avg_block_time = avg_time
@classmethod
def get_current_txblock(cls):
block = cls.get_cache("txblock", cls.api.GetCurrentMiniEpoch)
block = int(block or 0)
if block > cls.cur_tx_block:
cls.cur_tx_block = block
return block
@classmethod
def get_current_dsblock(cls):
block = cls.get_cache("dsblock", cls.api.GetCurrentDSEpoch)
block = int(block or 0)
if block > cls.cur_ds_block:
cls.cur_ds_block = block
return block
@classmethod
def get_difficulty(cls):
shard_difficulty = cls.get_cache("shard_difficulty",
cls.api.GetPrevDifficulty)
ds_difficulty = cls.get_cache("ds_difficulty",
cls.api.GetPrevDSDifficulty)
if shard_difficulty:
cls.shard_difficulty = shard_difficulty
if ds_difficulty:
cls.ds_difficulty = ds_difficulty
return shard_difficulty, ds_difficulty
@classmethod
def is_pow_window(cls):
if not cls.cur_tx_block:
cls.get_current_txblock()
tx_block = cls.cur_tx_block
block_per_pow = cls.conf["block_per_pow"]
block_in_epoch = tx_block % block_per_pow
return block_in_epoch in [0, block_per_pow - 1]
@classmethod
def secs_to_next_pow(cls):
if not cls.cur_tx_block:
cls.get_current_txblock()
tx_block = cls.cur_tx_block
block_per_pow = cls.conf["block_per_pow"]
block_in_epoch = tx_block % block_per_pow
if block_in_epoch == 0:
return 0 # current pow is running
return (block_per_pow - block_in_epoch) * cls.avg_block_time