teos.chain_monitor¶
-
class
ChainMonitor(receiving_queues, block_processor, bitcoind_feed_params)[source]¶ Bases:
objectThe
ChainMonitoris in charge of monitoring the blockchain (viabitcoind) to detect new blocks on top of the best chain. If a new best block is spotted, the chain monitor will notify the given queues.The
ChainMonitormonitors the chain using two methods:zmqandpolling. Blocks are only notified once per queue and the notification is triggered by the method that detects the block faster.The
ChainMonitorlifecycle goes through 4 states: idle, listening, active and terminated. When aChainMonitorinstance is created, it is not yet monitoring the chain and thestatusattribute is set toChainMonitorStatus.IDLE. Once themonitor_chainmethod is called, the chain monitor changesstatustoChainMonitorStatus.LISTENING, and starts monitoring the chain for new blocks; it does not yet notify the receiving queues, but keeps the block hashes in the order they where spotted in an internal queue. Once theactivatemethod is called, thestatuschanges toChainMonitorStatus.ACTIVE, and the receiving queues are notified in order for all the block hashes that are in the internal queue or any new one that is detected. Finally, once theterminatemethod is called, thestatusis changed toChainMonitorStatus.TERMINATED, the chain monitor stops monitoring the chain and no receiving queue will be notified about new blocks (including any block that is currently in the internal queue). A final"END"message is sent to all the subscribers.- Parameters
receiving_queues (
list) – a list ofQueueobjects that will be notified when the chain_monitor is active and it received new blocks hashes.block_processor (
BlockProcessor) – aBlockProcessorinstance.bitcoind_feed_params (
dict) – a dict with the feed (ZMQ) connection parameters.
-
logger¶ The logger for this component.
- Type
Logger
-
last_tips¶ A list of last chain tips. Used as a sliding window to avoid notifying about old tips.
- Type
list
-
check_tip¶ An event that is triggered at fixed time intervals and controls the polling thread.
- Type
Event
-
lock¶ A lock used to protect concurrent access to the queues by the zmq and polling threads.
- Type
Condition
-
zmqSubSocket¶ A socket to connect to
bitcoindviazmq.- Type
socket
-
polling_delta¶ Time between polls (in seconds).
- Type
int
-
max_block_window_size¶ Max size of
last_tips.- Type
int
-
queue¶ A queue where blocks are stored before they are processed.
- Type
Queue
-
status¶ The current status of the monitor, either
ChainMonitorStatus.IDLE,ChainMonitorStatus.LISTENING,ChainMonitorStatus.ACTIVEorChainMonitorStatus.TERMINATED.- Type
-
activate()[source]¶ Changes the
statusof theChainMonitorfrom listening to active. It creates a new thread that runs thenotify_subscribersmethod, which is in charge of notifying the receiving queue for each block hash that is added to the internal queue.- Raises
RuntimeError – if the
statuswas notChainMonitor.LISTENINGwhen the method was called.
-
enqueue(block_hash)[source]¶ Adds a new block hash to the internal queue of the
ChainMonitorand the internal state. The state contains the list oflast_tipsto prevent notifying about old blocks.last_tipsis bounded tomax_block_window_size.- Parameters
block_hash (
str) – the new best tip.- Returns
True if the state was successfully updated, False otherwise.
- Return type
bool
-
monitor_chain()[source]¶ Changes the
statusof theChainMonitorfrom idle to listening. It initializes thelast_tipslist to terminate the current best tip (by querying theBlockProcessor) and creates two threads, one per each monitoring approach (zmqandpolling).- Raises
RuntimeError – if the
statuswas notChainMonitor.IDLEwhen the method was called.
-
monitor_chain_polling()[source]¶ Monitors
bitcoindvia polling. Once the method is fired, it keeps monitoring as long as thestatusattribute is notChainMonitorStatus.TERMINATED. Polling is performed once everypolling_deltaseconds. If a new best tip is found, it is added to the internal queue.
-
monitor_chain_zmq()[source]¶ Monitors
bitcoindvia zmq. Once the method is fired, it keeps monitoring as long as thestatusattribute is notChainMonitorStatus.TERMINATED. If a new best tip is found, it is added to the internal queue.
-
notify_subscribers()[source]¶ Once the method is fired, it keeps getting the elements added to the internal queue and notifies the receiving queues about them. It terminates whenever the internal state is set to
ChainMonitorStatus.TERMINATED.
-
terminate()[source]¶ Changes the
statusof theChainMonitorto terminated and sends the “END” message to the internal queue. All the threads will stop as soon as possible.