@@ -36,6 +36,7 @@ def __init__(self, log_queue):
3636 # Get some configuration parameters
3737 self .config = BlockchainConfig .config
3838 self .poll_interval = self .config ["explorer" ]["poll_interval" ]
39+ self .batch_size = self .config ["explorer" ]["batch_size" ]
3940 self .addresses_config = self .config ["api" ]["caching" ]["scripts" ]["addresses" ]
4041
4142 # Set up logger
@@ -117,6 +118,52 @@ def insert_block(self, database, block_hash_hex_str, block, epoch, tapi_periods)
117118
118119 return block_json
119120
121+ def batch_insert_blocks (self , database , block_hashes , blocks , epochs , tapi_periods ):
122+ addresses = {}
123+ batched_transactions = {}
124+ for block_hash , block , epoch in zip (block_hashes , blocks , epochs ):
125+ # Create block object and parse it to a JSON object
126+ block = Block (
127+ block = block ,
128+ block_hash = block_hash ,
129+ log_queue = self .log_queue ,
130+ database = self .insert_blocks_database ,
131+ tapi_periods = tapi_periods ,
132+ witnet_node = self .insert_blocks_node ,
133+ transaction_batch = batched_transactions ,
134+ )
135+ block_json = block .process_block ("explorer" )
136+
137+ # Insert block
138+ database .insert_block (block_json )
139+
140+ # Insert transactions
141+ transactions = self .insert_transactions (database , block_json , epoch )
142+ batched_transactions .update (transactions )
143+
144+ # Insert address data
145+ block_addresses = block .process_addresses (as_dict = True )
146+ for address , data in block_addresses .items ():
147+ if address not in addresses :
148+ addresses [address ] = [address , epoch ] + data
149+ else :
150+ addresses [address ][1 ] = epoch
151+ addresses [address ][2 ] += data [0 ]
152+ addresses [address ][3 ] += data [1 ]
153+ addresses [address ][4 ] += data [2 ]
154+ addresses [address ][5 ] += data [3 ]
155+ addresses [address ][6 ] += data [4 ]
156+ addresses [address ][7 ] += data [5 ]
157+ addresses [address ][8 ] += data [6 ]
158+ addresses [address ][9 ] += data [7 ]
159+ addresses [address ][10 ] += data [8 ]
160+
161+ # Insert all address data
162+ database .insert_addresses (list (addresses .values ()))
163+
164+ # Finalize insertions and updates on every block
165+ database .finalize (epochs )
166+
120167 def update_cached_views (self , block_json , logger , caching_server ):
121168 epoch = block_json ["details" ]["epoch" ]
122169
@@ -263,36 +310,48 @@ def update_cached_views(self, block_json, logger, caching_server):
263310 self .try_send_request (logger , caching_server , request )
264311
265312 def insert_transactions (self , database , block_json , epoch ):
313+ transactions = {}
314+
266315 # Insert mint transaction
267316 database .insert_mint_txn (block_json ["transactions" ]["mint" ], epoch )
317+ transactions [block_json ["transactions" ]["mint" ]["hash" ]] = block_json ["transactions" ]["mint" ]
268318
269319 # Insert value transfer transactions
270320 for txn_details in block_json ["transactions" ]["value_transfer" ]:
271321 database .insert_value_transfer_txn (txn_details , epoch )
322+ transactions [txn_details ["hash" ]] = txn_details
272323
273324 # Insert data request transactions
274325 for txn_details in block_json ["transactions" ]["data_request" ]:
275326 database .insert_data_request_txn (txn_details , epoch )
327+ transactions [txn_details ["hash" ]] = txn_details
276328
277329 # Insert commit transactions
278330 for txn_details in block_json ["transactions" ]["commit" ]:
279331 database .insert_commit_txn (txn_details , epoch )
332+ transactions [txn_details ["hash" ]] = txn_details
280333
281334 # Insert reveal transactions
282335 for txn_details in block_json ["transactions" ]["reveal" ]:
283336 database .insert_reveal_txn (txn_details , epoch )
337+ transactions [txn_details ["hash" ]] = txn_details
284338
285339 # Insert tally transactions
286340 for txn_details in block_json ["transactions" ]["tally" ]:
287341 database .insert_tally_txn (txn_details , epoch )
342+ transactions [txn_details ["hash" ]] = txn_details
288343
289344 # Insert tally transactions
290345 for txn_details in block_json ["transactions" ]["stake" ]:
291346 database .insert_stake_txn (txn_details , epoch )
347+ transactions [txn_details ["hash" ]] = txn_details
292348
293349 # Insert tally transactions
294350 for txn_details in block_json ["transactions" ]["unstake" ]:
295351 database .insert_unstake_txn (txn_details , epoch )
352+ transactions [txn_details ["hash" ]] = txn_details
353+
354+ return transactions
296355
297356 def insert_blocks_and_transactions (self , log_queue , unconfirmed_blocks_queue ):
298357 # Set up logger
@@ -347,8 +406,9 @@ def insert_blocks_and_transactions(self, log_queue, unconfirmed_blocks_queue):
347406 else :
348407 blockchain = blockchain ["result" ]
349408
409+ block_hashes , blocks , epochs = [], [], []
350410 for epoch , block_hash_hex_str in blockchain :
351- logger .info (f"Inserting data for epoch { epoch } " )
411+ logger .info (f"Fetching data for epoch { epoch } " )
352412
353413 block = self .insert_blocks_node .get_block (block_hash_hex_str )
354414 # The database entries related to this block have not been modified yet
@@ -361,16 +421,33 @@ def insert_blocks_and_transactions(self, log_queue, unconfirmed_blocks_queue):
361421 block = block ["result" ]
362422
363423 # Insert block
364- block_json = self .insert_block (
365- self .insert_blocks_database ,
366- block_hash_hex_str ,
367- block ,
368- epoch ,
369- tapi_periods ,
370- )
424+ if len (blockchain ) < self .batch_size :
425+ block_json = self .insert_block (
426+ self .insert_blocks_database ,
427+ block_hash_hex_str ,
428+ block ,
429+ epoch ,
430+ tapi_periods ,
431+ )
371432
372- # Update all cached views
373- self .update_cached_views (block_json , logger , caching_server )
433+ # Update all cached views
434+ self .update_cached_views (block_json , logger , caching_server )
435+ # Batch insert block if they older than the batch size
436+ else :
437+ block_hashes .append (block_hash_hex_str )
438+ blocks .append (block )
439+ epochs .append (epoch )
440+ if len (block_hashes ) == self .batch_size :
441+ self .batch_insert_blocks (
442+ self .insert_blocks_database ,
443+ block_hashes ,
444+ blocks ,
445+ epochs ,
446+ tapi_periods ,
447+ )
448+ block_hashes = []
449+ blocks = []
450+ epochs = []
374451
375452 # Check if the block is confirmed and if it isn't track the hash
376453 confirmed = block ["confirmed" ]
0 commit comments