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
|
2022-03-20 23:05:44 +00:00
|
|
|
from queue import Empty
|
2022-03-10 07:10:13 +00:00
|
|
|
|
|
|
|
import blockdb
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from onionrblocks import Block
|
|
|
|
from ..dandelion.phase import DandelionPhase
|
|
|
|
|
2022-03-21 06:03:53 +00:00
|
|
|
from ..blockqueues import gossip_block_queues
|
2022-03-10 07:10:13 +00:00
|
|
|
|
2022-03-21 06:03:53 +00:00
|
|
|
|
|
|
|
def store_blocks(dandelion_phase: 'DandelionPhase'):
|
2022-03-10 07:10:13 +00:00
|
|
|
|
2022-03-20 17:54:57 +00:00
|
|
|
new_queue: "Queue[Block]" = Queue()
|
2022-03-10 07:10:13 +00:00
|
|
|
|
2022-03-20 17:54:57 +00:00
|
|
|
def _watch_queue(block_queue: "Queue[Block]"):
|
2022-03-10 07:10:13 +00:00
|
|
|
# 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()))
|
2022-03-20 23:05:44 +00:00
|
|
|
except Empty:
|
2022-03-10 07:10:13 +00:00
|
|
|
pass
|
|
|
|
|
2022-03-21 06:03:53 +00:00
|
|
|
for block_queue in gossip_block_queues:
|
2022-03-20 23:05:44 +00:00
|
|
|
Thread(target=_watch_queue, args=[block_queue], daemon=True).start()
|
2022-03-10 07:10:13 +00:00
|
|
|
|
|
|
|
while not dandelion_phase.is_stem_phase() \
|
|
|
|
and dandelion_phase.remaining_time() > 1:
|
|
|
|
try:
|
2022-03-13 01:28:18 +00:00
|
|
|
blockdb.add_block_to_db(
|
2022-06-14 16:01:07 +00:00
|
|
|
new_queue.get(timeout=dandelion_phase.remaining_time() + 1)
|
2022-03-10 07:10:13 +00:00
|
|
|
)
|
2022-03-20 23:05:44 +00:00
|
|
|
except Empty:
|
2022-03-10 07:10:13 +00:00
|
|
|
pass
|
|
|
|
|