-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor.py
314 lines (281 loc) · 11.6 KB
/
monitor.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
312
313
314
import asyncio
import base64
import os
import sqlite3
import subprocess
import sys
import time
from dataclasses import dataclass
from typing import List, Optional, Tuple, Union
from dotenv import load_dotenv
from loguru import logger
from pytonapi import AsyncTonapi
from pytonapi.async_tonapi.client import json
from pytoniq import LiteBalancer, begin_cell
from pytoniq.contract.wallets import Wallet
from pytoniq_core import Builder
from pytoniq_core.boc import Address, Cell
from pytoniq_core.crypto import keys
from tonsdk.utils import sign_message
from client import TonCenterClient
@dataclass
class WalletInfo:
addr: str
sk: bytes
pk_hex: str
VALID_UNTIL_TIMEOUT = 60
SEND_INTERVAL = 120
Client = Union[LiteBalancer, TonCenterClient, AsyncTonapi]
class TransactionsMonitor:
def init_db(self):
os.makedirs("db/", exist_ok=True)
self.connection = sqlite3.connect(f"db/{self.dbname}.db")
self.cursor = self.connection.cursor()
self.cursor.execute(
"""
CREATE TABLE IF NOT EXISTS txs (
addr TEXT,
utime INTEGER,
is_found BOOLEAN,
executed_in INTEGER,
found_in INTEGER,
PRIMARY KEY (addr, utime)
)
"""
)
self.connection.commit()
def __init__(
self,
client: Client,
wallets_path: str,
dbname: str,
to_send: Optional[int] = None,
extended_message: bool = False,
):
self.dbname = dbname
self.init_db()
self.client = client
self.wallets_path = wallets_path
self.to_send = to_send
self.extended_message = extended_message
self.wallets: List[WalletInfo] = []
self.sent_count = 0
async def init_client(self):
if isinstance(self.client, LiteBalancer):
await self.client.start_up()
elif isinstance(self.client, TonCenterClient):
pass
else:
pass
async def read_wallets(self):
wallets = []
with open(self.wallets_path, "r") as f:
for line in f.readlines():
addr, seed = line.split()
seed = int(seed, 16)
seed_bytes = seed.to_bytes(32, "big")
public_key, private_key = keys.crypto_sign_seed_keypair(seed_bytes)
pk_hex = "0x" + public_key.hex()
wallets.append(WalletInfo(addr=addr, pk_hex=pk_hex, sk=private_key))
self.wallets = wallets
def add_new_tx(self, tx_id: int, addr: str) -> None:
self.cursor.execute(
"INSERT INTO txs (addr, utime, is_found) VALUES (?, ?, 0)",
(addr, tx_id),
)
self.connection.commit()
def get_missing_ids(self) -> List[str]:
self.cursor.execute("SELECT addr, utime FROM txs WHERE is_found = 0")
result = self.cursor.fetchall()
missing_ids = []
for addr, utime in result:
missing_ids.append(f"{utime}:{addr}")
return missing_ids
def make_found(self, tx_full_id: str, executed_in: int, found_in: int) -> None:
utime, addr = tx_full_id.split(":")
self.cursor.execute(
"UPDATE txs SET is_found = 1, executed_in = ?, found_in = ? WHERE addr = ? AND utime = ?",
(executed_in, found_in, addr, utime),
)
self.connection.commit()
async def get_seqno(self, address: str) -> int:
if isinstance(self.client, TonCenterClient):
return await self.client.seqno(address)
elif isinstance(self.client, LiteBalancer):
return (await self.client.run_get_method(address, "seqno", []))[0]
else:
res = await self.client.blockchain.execute_get_method(address, "seqno")
if res.success is False:
raise Exception(f"Error with tonapi get method: {res.exit_code}")
return int(res.decoded["state"]) if res.decoded else 0
async def sendboc(self, boc: bytes):
if isinstance(self.client, LiteBalancer):
await self.client.raw_send_message(boc)
elif isinstance(self.client, TonCenterClient):
await self.client.send(boc)
else:
api_body = {"boc": base64.b64encode(boc).decode()}
await self.client.blockchain.send_message(body=api_body)
await asyncio.sleep(2)
async def parse_and_add_msg(self, msg: Cell, blockutime: int, addr: str) -> bool:
msg_slice = msg.begin_parse()
msg_slice.skip_bits(512) # signature
msg_slice.skip_bits(32) # seqno
valid_until = msg_slice.load_uint(48)
tx_id = valid_until - VALID_UNTIL_TIMEOUT # get sending time
tx_full_id = f"{tx_id}:{addr}"
if tx_full_id in self.get_missing_ids():
current = int(time.time())
logger.info(
f"{self.dbname}: Found tx: {tx_full_id} at {current}. Executed in {blockutime - tx_id} sec. Found in {current - tx_id} sec."
)
executed_in = blockutime - tx_id
found_in = current - tx_id
self.make_found(tx_full_id, executed_in, found_in)
return True
else:
return False
def extend_message_to_1kb(self, body: Builder):
# writing some mash data
extension1 = Builder().store_bits("11" * 499).end_cell()
extension2 = Builder().store_bits("01" * 499).end_cell()
extension3 = Builder().store_bits("10" * 499).end_cell()
body.store_ref(
Builder().store_bits("11" * 500).store_ref(extension1).end_cell()
)
body.store_ref(
Builder().store_bits("01" * 501).store_ref(extension2).end_cell()
)
body.store_ref(
Builder().store_bits("10" * 502).store_ref(extension3).end_cell()
)
body.store_ref(Builder().store_bits("00" * 503).end_cell())
return body
async def send_tx_with_id(self, tx_utime: int, wdata: WalletInfo):
seqno = await self.get_seqno(wdata.addr)
relative_path = "logger-c5.fif"
full_path = os.path.join(os.path.dirname(__file__), relative_path)
new_code_hex = subprocess.check_output(
["fift", "-s", full_path, str(seqno + 1), wdata.pk_hex]
).decode()
new_seqno_code = Cell.from_boc(new_code_hex)[0]
action_set_code = (
begin_cell().store_uint(0xAD4DE08E, 32).store_ref(new_seqno_code).end_cell()
)
actions = (
begin_cell()
.store_ref(begin_cell().end_cell())
.store_slice(action_set_code.to_slice())
.end_cell()
)
body = (
begin_cell()
.store_uint(seqno, 32)
.store_uint(tx_utime + VALID_UNTIL_TIMEOUT, 48)
.store_ref(actions)
)
if self.extended_message:
body = self.extend_message_to_1kb(body)
body = body.end_cell()
signature = sign_message(body.hash, wdata.sk).signature
signed_body = Builder().store_bytes(signature).store_cell(body).end_cell()
addr = Address(wdata.addr)
message = Wallet.create_external_msg(
dest=addr,
body=signed_body,
)
boc = message.serialize().to_boc()
await self.sendboc(boc)
self.add_new_tx(tx_utime, wdata.addr)
tx_full_id = f"{tx_utime}:{wdata.addr}"
self.sent_count += 1
logger.info(f"{self.dbname}: Sent tx {tx_full_id}")
async def start_sending(self):
"""Txs sender. Sends them every `SEND_INTERVAL` seconds to
all the wallets specified in `self.wallets`."""
while self.sent_count < (self.to_send or 100000000): # 400 years by default
for wdata in self.wallets:
tx_id = int(time.time())
try:
await self.send_tx_with_id(tx_id, wdata)
except Exception as e:
logger.warning(
f"Failed to send tx with id {str(tx_id)} from wallet "
+ f"{wdata.addr} error: {str(e)}"
)
await asyncio.sleep(SEND_INTERVAL)
async def watch_transactions(self):
"""Watches for sent tx to be shown up on wallets
and adds them to found_tx_ids."""
while True:
try:
for tx_id in self.get_missing_ids():
addr = tx_id.split(":")[1]
if isinstance(self.client, LiteBalancer):
txs = await self.client.get_transactions(addr, 3, from_lt=0)
for tx in txs:
if tx.in_msg is not None and tx.in_msg.is_external:
await self.parse_and_add_msg(
tx.in_msg.body, tx.now, addr
)
elif isinstance(self.client, TonCenterClient):
txs = await self.client.get_transactions(addr, 3, from_lt=0)
for tx in txs:
if (
"in_msg" in tx
and tx["in_msg"]
# from nowhere:
and tx["in_msg"]["source"] == ""
):
body_b64 = tx["in_msg"]["msg_data"]["body"]
body = Cell.from_boc(body_b64)[0]
await self.parse_and_add_msg(body, tx["utime"], addr)
else:
txs = await self.client.blockchain.get_account_transactions(
addr, limit=3
)
for tx in txs.transactions:
if tx.in_msg is not None and tx.in_msg.source is None:
body = tx.in_msg.raw_body
if isinstance(body, str):
body = Cell.from_boc(body)[0]
await self.parse_and_add_msg(body, tx.utime, addr)
await asyncio.sleep(2)
except Exception as e:
logger.warning(
f"{self.dbname}: watch_transactions failed, retrying: {str(e)}"
)
await asyncio.sleep(4)
async def printer(self):
"""Prints some stats about sent txs every 5 sec."""
while True:
missing_ids = self.get_missing_ids()
found = self.sent_count - len(missing_ids)
rate = max(found, 0) / (self.sent_count or 1)
logger.debug(
f"{self.dbname}: Found/sent: {found}/{self.sent_count}, success rate: {rate}"
)
await asyncio.sleep(5)
async def start_worker(self):
"""This thing inits database and starts txs sending with txs watching.
It should be run for every provider and every db separately."""
logger.warning(
f"Starting worker to db/{self.dbname} of {self.to_send} txs using {self.wallets_path} wallets."
)
await self.init_client()
await self.read_wallets()
asyncio.create_task(self.watch_transactions())
asyncio.create_task(self.printer())
await self.start_sending()
logger.info(f"\n{self.dbname}: Done sending {self.sent_count} txs")
# Usage example
async def main():
load_dotenv()
dbname = "ls"
config = json.loads(open("configs/testnet.json").read())
client = LiteBalancer.from_config(config, timeout=15)
wallets_path = "mywallets/w-c5-1.txt"
monitor = TransactionsMonitor(client, wallets_path, dbname)
await monitor.start_worker()
if __name__ == "__main__":
asyncio.run(main())