def blockchain_listener(): """ Execute forever listening blockchain operations :return: """ from bitshares.blockchain import Blockchain chain = Blockchain() for block in chain.blocks(): #print("listenet:", block) Redisdb.rpush("bitshares_op", pickle.dumps(block))
def run(): socketio = SocketIO(message_queue='redis://') bitshares = BitShares(node="wss://this.uptick.rocks/") chain = Blockchain(mode="head", bitshares_instance=bitshares) print(chain.bitshares.rpc.url) for block in chain.blocks(): timestamp = int(datetime.strptime(block["timestamp"], '%Y-%m-%dT%H:%M:%S').timestamp()) num_ops = sum([len(tx["operations"]) for tx in block["transactions"]]) num_txs = len(block["transactions"]) BTSBlock(timestamp, block.get("block_num", None), num_txs, num_ops) notice = { "block": block.get("block_num", 0), "timestamp": timestamp, "num_transactions": num_txs, "num_operations": num_ops, } print(notice) socketio.emit( 'notice', notice, namespace=namespace, room=room, broadcast=True)
def run(): socketio = SocketIO(message_queue='redis://') testnet = BitShares(node="wss://node.testnet.bitshares.eu") chain = Blockchain(mode="head", bitshares_instance=testnet) print(chain.bitshares.rpc.url, flush=True) for block in chain.blocks(): timestamp = int(datetime.strptime(block["timestamp"], '%Y-%m-%dT%H:%M:%S').timestamp()) num_ops = sum([len(tx["operations"]) for tx in block["transactions"]]) num_txs = len(block["transactions"]) TestBlock(timestamp, block.get("block_num", None), num_txs, num_ops) notice = { "block": block.get("block_num", 0), "timestamp": timestamp, "num_transactions": num_txs, "num_operations": num_ops, } print(notice, flush=True) socketio.emit( 'notice', notice, namespace=namespace, room=room, broadcast=True)
def listen(self): """ Listen to the blockchain and send blocks to :func:`BlockchainMonitor.process_block` .. note:: Depending on the setting of ``watch_mode`` in the configuration, the listen method has slightly different behavior. Namely, we here have the choice between "head" (the last block) and "irreversible" (the block that is confirmed by 2/3 of all block producers and is thus irreversible) """ last_block = self.storage.get_last_head_block_num() logging.getLogger(__name__).debug("Start listen with start=" + str(self.start_block) + " stop=" + str(self.stop_block) + " last=" + str(last_block)) # if set to true, block numbers may not be consecutively self.allow_block_jump = False if not self.start_block: if last_block > 0: self.start_block = last_block + 1 else: if not self.start_block == self.storage.get_last_head_block_num() + 1: # allow first block to jump self.allow_block_jump = True logging.getLogger(__name__).warning("Force listen with different block than last in storage (storage=" + str(last_block) + ", given=" + str(self.start_block) + ")") retry = True while (retry): retry = False block_listener = Blockchain( mode=self.watch_mode, max_block_wait_repetition=12, bitshares_instance=self.bitshares ) if not block_listener.mode == "last_irreversible_block_num": block_listener.mode = "last_irreversible_block_num" try: for block in block_listener.blocks( start=self.start_block, stop=self.stop_block ): logging.getLogger(__name__).debug("Processing block " + str(block["block_num"])) last_head_block = self.storage.get_last_head_block_num() if last_head_block == 0 or\ block["block_num"] == last_head_block + 1 or\ (self.allow_block_jump and last_head_block < block["block_num"]): self.allow_block_jump = False # no blocks missed self.process_block(block) self.storage.set_last_head_block_num(block["block_num"]) elif block["block_num"] == last_head_block: # possible on connection error, skip block continue else: self.start_block = last_head_block + 1 if self.stop_block is not None and self.start_block > self.stop_block: logging.getLogger(__name__).error("Block was missed, or trying to march backwards. Stop block already reached, shutting down ...") return else: logging.getLogger(__name__).error("Block was missed, or trying to march backwards. Retry with next block " + str(last_head_block + 1)) raise BlockchainMonitorRetryException() except BlockchainMonitorRetryException: retry = True