Example #1
0
class ExportBlocksJob(BaseJob):
    def __init__(self,
                 start_block,
                 end_block,
                 batch_size,
                 bitcoin_rpc,
                 max_workers,
                 item_exporter,
                 chain,
                 export_blocks=True,
                 export_transactions=True,
                 coin_price_type=CoinPriceType.empty):
        validate_range(start_block, end_block)

        self.start_block = start_block
        self.end_block = end_block

        self.batch_work_executor = BatchWorkExecutor(batch_size, max_workers)
        self.item_exporter = item_exporter

        self.export_blocks = export_blocks
        self.export_transactions = export_transactions
        if not self.export_blocks and not self.export_transactions:
            raise ValueError(
                'At least one of export_blocks or export_transactions must be True'
            )

        self.btc_service = BtcService(bitcoin_rpc, chain, coin_price_type)
        self.block_mapper = BtcBlockMapper()
        self.transaction_mapper = BtcTransactionMapper()

    def _start(self):
        self.item_exporter.open()

    def _export(self):
        self.batch_work_executor.execute(
            range(self.start_block, self.end_block + 1),
            self._export_batch,
            total_items=self.end_block - self.start_block + 1)

    def _export_batch(self, block_number_batch):
        blocks = self.btc_service.get_blocks(block_number_batch,
                                             self.export_transactions)

        for block in blocks:
            self._export_block(block)

    def _export_block(self, block):
        if self.export_blocks:
            self.item_exporter.export_item(
                self.block_mapper.block_to_dict(block))
        if self.export_transactions:
            for tx in block.transactions:
                self.item_exporter.export_item(
                    self.transaction_mapper.transaction_to_dict(tx))

    def _end(self):
        self.batch_work_executor.shutdown()
        self.item_exporter.close()
Example #2
0
class BlockTimestampGraph(object):
    def __init__(self, bitcoin_rpc):
        self._btc_service = BtcService(bitcoin_rpc)

    def get_first_point(self):
        block = self._btc_service.get_genesis_block()
        return block_to_point(block)

    def get_last_point(self):
        block = self._btc_service.get_latest_block()
        return block_to_point(block)

    def get_point(self, block_number):
        block = self._btc_service.get_block(block_number)
        return block_to_point(block)

    def get_points(self, block_numbers):
        blocks = self._btc_service.get_blocks(block_numbers)
        return [block_to_point(block) for block in blocks]