2022-03-11 17:15:18 +00:00
|
|
|
from typing import TYPE_CHECKING, Tuple
|
2022-03-10 07:10:13 +00:00
|
|
|
from threading import Thread
|
|
|
|
from queue import Queue
|
|
|
|
|
|
|
|
import blockdb
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from onionrblocks import Block
|
|
|
|
from ..dandelion.phase import DandelionPhase
|
|
|
|
|
|
|
|
|
|
|
|
def store_blocks(
|
2022-03-11 17:15:18 +00:00
|
|
|
block_queues: Tuple[Queue['Block']],
|
2022-03-10 07:10:13 +00:00
|
|
|
dandelion_phase: 'DandelionPhase'):
|
|
|
|
|
|
|
|
new_queue: Queue['Block'] = Queue()
|
|
|
|
|
|
|
|
def _watch_queue(block_queue: Queue['Block']):
|
|
|
|
# Copy all incoming blocks into 1 queue which gets processed to db
|
|
|
|
while not dandelion_phase.is_stem_phase() \
|
|
|
|
and dandelion_phase.remaining_time() > 1:
|
|
|
|
try:
|
|
|
|
new_queue.put(
|
|
|
|
block_queue.get(timeout=dandelion_phase.remaining_time()))
|
|
|
|
except TimeoutError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
for block_queue in block_queues:
|
|
|
|
Thread(target=_watch_queue, args=block_queue, daemon=True).start()
|
|
|
|
|
|
|
|
while not dandelion_phase.is_stem_phase() \
|
|
|
|
and dandelion_phase.remaining_time() > 1:
|
|
|
|
try:
|
|
|
|
blockdb.store_vdf_block(
|
|
|
|
new_queue.get(timeout=dandelion_phase.remaining_time())
|
|
|
|
)
|
|
|
|
except TimeoutError:
|
|
|
|
pass
|
|
|
|
|