Skip to content

Commit 73acaae

Browse files
committed
added genesis start up and continue from last block, huey added for cronjob /scheduling WIP
1 parent 3f3da9e commit 73acaae

File tree

6 files changed

+343
-29
lines changed

6 files changed

+343
-29
lines changed

main.py

+43-24
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
from packages.block_collector import IrohaBlockAPI
22
from packages.models import Block_V1
33
from packages.db import session
4+
from packages.cronjob import huey
45
import os
56
import json
67
import asyncio
78

9+
10+
huey.start()
11+
812
private_key = os.getenv(
913
"IROHA_V1_API_SECRET",
1014
"164879bd94411cb7c88308b6423f7dde1e6df19a98961c8aebf7d70990ade5b7",
@@ -14,9 +18,10 @@
1418
iroha_block_api = IrohaBlockAPI(
1519
creator_account=api_user, private_key=private_key, iroha_host=iroha_host,
1620
)
17-
18-
def parse_iroha_blocks_to_db(height:int = 1):
21+
@huey.task()
22+
def parse_iroha_blocks_to_db(height:int = 1) -> None:
1923
"""
24+
Iroha Block Parser
2025
:param height: Address of the Sender,
2126
:param return_block: Returns block in JSON Format
2227
:return: None if return_block is false
@@ -47,39 +52,53 @@ def parse_iroha_blocks_to_db(height:int = 1):
4752
print(error)
4853

4954

50-
def async_parse_iroha_blocks_to_db(height:int = 1):
55+
def parse_genesis_iroha_blocks_to_db(height:int = 1):
5156
"""
5257
5358
"""
5459
try:
5560
raw_block = iroha_block_api.get_blocks(height=height)
5661
block = json.loads(raw_block)
57-
payload = block["blockResponse"]["block"]["blockV1"]["payload"]["transactions"][0][
58-
"payload"
59-
]["reducedPayload"]["commands"]
60-
block_hash = block["blockResponse"]["block"]["blockV1"]["payload"]["prevBlockHash"]
62+
transactions = block["blockResponse"]["block"]["blockV1"]["payload"]["transactions"]
63+
prev_block_hash = block["blockResponse"]["block"]["blockV1"]["payload"]["prevBlockHash"]
6164
height = block["blockResponse"]["block"]["blockV1"]["payload"]["height"]
6265
txNumber = block["blockResponse"]["block"]["blockV1"]["payload"]["txNumber"]
63-
64-
iroha_block = Block_V1.add_block(
66+
Block_V1.add_block(
67+
created_time='0',
6568
height=height,
66-
block_hash=block_hash,
67-
payload=payload,
68-
tx_number=txNumber
69-
)
70-
print(iroha_block)
69+
prev_block_hash=prev_block_hash,
70+
transactions=transactions,
71+
rejected_transactions_hashes=[],
72+
signatures=[])
7173
except Exception as error:
7274
print(error)
7375

74-
def load_blocks_to_database():
75-
import time
7676

77-
start_time = time.time()
78-
cpu_start_time = time.process_time()
77+
def start_up():
78+
# get last block from db if none start from 1
79+
try:
80+
parse_genesis_iroha_blocks_to_db()
81+
except:
82+
print('Genesis block already exists')
83+
finally:
84+
last_block = Block_V1.get_last_block()
85+
return last_block
86+
87+
88+
#res = load_blocks_to_database.schedule(args=(10, 20), delay=2)
89+
#print(res())
90+
91+
# Stop the scheduler. Not strictly necessary, but a good idea.
92+
93+
def start_parser(last_height: int, scan_range: int =100):
94+
curent_height = last_height
95+
end_height = curent_height + scan_range
96+
while curent_height < end_height:
97+
parse_iroha_blocks_to_db(height=curent_height)
98+
curent_height += 1
99+
print(f'parsed block {curent_height}')
79100

80-
for i in range(1, 350):
81-
parse_iroha_blocks_to_db(height=i)
82-
print(f'parsed block {i}')
83-
#total_real_time = time.time() - start_time
84-
total_cpu_time = time.process_time() - cpu_start_time
85-
print(total_cpu_time)
101+
last_block = start_up()
102+
last_height = last_block["height"]
103+
start_parser(last_height,100)
104+
huey.stop()

packages/cronjob.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#add huey or mini huey
2+
3+
4+
from gevent import monkey; monkey.patch_all()
5+
import gevent
6+
7+
from huey.contrib.mini import MiniHuey
8+
9+
10+
huey = MiniHuey()
11+
12+
# If we want to support scheduling tasks for execution in the future, or for
13+
# periodic execution (e.g. cron), then we need to call `huey.start()` which
14+
# starts a scheduler thread.

packages/models.py

+13-4
Original file line numberDiff line numberDiff line change
@@ -35,25 +35,34 @@ def __repr__(self):
3535
def add_block(prev_block_hash, created_time ,height, transactions, rejected_transactions_hashes, signatures):
3636
# add Iroha block to database
3737
block = Block_V1(prev_block_hash, created_time ,height, transactions, rejected_transactions_hashes, signatures)
38-
session.add(block)
39-
session.commit()
38+
39+
try:
40+
session.add(block)
41+
session.commit()
42+
except:
43+
session.rollback()
44+
raise
45+
finally:
46+
session.close()
4047

4148
@staticmethod
4249
def get_block_by_height(height: int = 1,):
4350
# check whether auth token has been blacklisted
4451
block = session.query(Block_V1).filter_by(height=height)
4552
return block.__dict__
4653

54+
4755
@staticmethod
4856
def get_block_by_hash(block_hash):
4957
# check whether auth token has been blacklisted
5058
block = session.query(Block_V1).filter_by(height=block_hash)
5159
return block.__dict__
5260

5361
@staticmethod
54-
def get_tx_by_command(command: str):
62+
def get_last_block():
5563
# check whether auth token has been blacklisted
56-
block = session.query(Block_V1).filter_by(height=command)
64+
block = session.query(Block_V1).order_by(Block_V1.height.desc()).first()
65+
print(block.height)
5766
return block.__dict__
5867

5968

0 commit comments

Comments
 (0)