forked from denaro-coin/denaro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminer.py
54 lines (46 loc) · 1.86 KB
/
miner.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
import asyncio
import sys
import time
import denaro
from denaro.constants import ENDIAN
from denaro.manager import get_difficulty, check_block_is_valid, Manager, get_transactions_merkle_tree
from denaro.helpers import sha256, timestamp
from icecream import ic
from denaro.node.main import sync_blockchain, push_block, startup
_print = print
print = ic
async def run():
await startup()
db = denaro.node.main.db
while True:
await sync_blockchain()
difficulty, last_block = await get_difficulty()
last_block['hash'] = last_block['hash'] if 'hash' in last_block else (30_06_2005).to_bytes(32, ENDIAN).hex()
print(difficulty)
Manager.difficulty = None
address = sys.argv[1]
t = time.process_time()
i = 0
a = timestamp()
txs = await db.get_pending_transactions_limit(1000)
merkle_tree = get_transactions_merkle_tree(txs)
prefix = bytes.fromhex(last_block['hash']) + bytes.fromhex(address) + bytes.fromhex(merkle_tree) + a.to_bytes(4, byteorder=ENDIAN) + int(difficulty * 10).to_bytes(2, ENDIAN)
found = True
while not await check_block_is_valid(_hex := prefix + i.to_bytes(4, ENDIAN), (difficulty, last_block)):
i += 1
if i % 1000000 == 0:
elapsed_time = time.process_time() - t
_print(str(i / elapsed_time) + ' hash/s')
print(i)
if elapsed_time > 150:
found = False
break
if found:
await sync_blockchain()
res = await push_block(None, _hex.hex(), [tx.hex() for tx in txs], False)
Manager.difficulty = None
if res['ok'] and txs:
for tx in txs:
await db.remove_pending_transaction(sha256(tx.hex()))
loop = asyncio.get_event_loop()
loop.run_until_complete(run())