1
1
from packages .block_collector import IrohaBlockAPI
2
2
from packages .models import Block_V1
3
3
from packages .db import session
4
+ from packages .cronjob import huey
4
5
import os
5
6
import json
6
7
import asyncio
7
8
9
+
10
+ huey .start ()
11
+
8
12
private_key = os .getenv (
9
13
"IROHA_V1_API_SECRET" ,
10
14
"164879bd94411cb7c88308b6423f7dde1e6df19a98961c8aebf7d70990ade5b7" ,
14
18
iroha_block_api = IrohaBlockAPI (
15
19
creator_account = api_user , private_key = private_key , iroha_host = iroha_host ,
16
20
)
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 :
19
23
"""
24
+ Iroha Block Parser
20
25
:param height: Address of the Sender,
21
26
:param return_block: Returns block in JSON Format
22
27
:return: None if return_block is false
@@ -47,39 +52,53 @@ def parse_iroha_blocks_to_db(height:int = 1):
47
52
print (error )
48
53
49
54
50
- def async_parse_iroha_blocks_to_db (height :int = 1 ):
55
+ def parse_genesis_iroha_blocks_to_db (height :int = 1 ):
51
56
"""
52
57
53
58
"""
54
59
try :
55
60
raw_block = iroha_block_api .get_blocks (height = height )
56
61
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" ]
61
64
height = block ["blockResponse" ]["block" ]["blockV1" ]["payload" ]["height" ]
62
65
txNumber = block ["blockResponse" ]["block" ]["blockV1" ]["payload" ]["txNumber" ]
63
-
64
- iroha_block = Block_V1 . add_block (
66
+ Block_V1 . add_block (
67
+ created_time = '0' ,
65
68
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 = [])
71
73
except Exception as error :
72
74
print (error )
73
75
74
- def load_blocks_to_database ():
75
- import time
76
76
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 } ' )
79
100
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 ()
0 commit comments