Exemplo n.º 1
0
class ExportReceiptsJob(BaseJob):
    def __init__(
        self,
        transaction_hashes_iterable,
        batch_size,
        batch_web3_provider,
        max_workers,
        item_exporter,
        export_receipts=True,
        export_logs=True,
    ):
        self.batch_web3_provider = batch_web3_provider
        self.transaction_hashes_iterable = transaction_hashes_iterable

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

        self.export_receipts = export_receipts
        self.export_logs = export_logs
        if not self.export_receipts and not self.export_logs:
            raise ValueError(
                "At least one of export_receipts or export_logs must be True")

        self.receipt_mapper = IcxReceiptMapper()
        self.receipt_log_mapper = IcxReceiptLogMapper()

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

    def _export(self):
        self.batch_work_executor.execute(self.transaction_hashes_iterable,
                                         self._export_receipts)

    def _export_receipts(self, transaction_hashes):
        receipts_rpc = list(generate_get_receipt_json_rpc(transaction_hashes))
        response = self.batch_web3_provider.make_batch_request(
            json.dumps(receipts_rpc))
        results = rpc_response_batch_to_results(response)
        receipts = [
            self.receipt_mapper.json_dict_to_receipt(result)
            for result in results
        ]
        for receipt in receipts:
            self._export_receipt(receipt)

    def _export_receipt(self, receipt):
        if self.export_receipts:
            self.item_exporter.export_item(
                self.receipt_mapper.receipt_to_dict(receipt))
        if self.export_logs:
            for log in receipt.logs:
                self.item_exporter.export_item(
                    self.receipt_log_mapper.receipt_log_to_dict(log))

    def _end(self):
        self.batch_work_executor.shutdown()
        self.item_exporter.close()
Exemplo n.º 2
0
class IcxReceiptMapper(object):
    def __init__(self, receipt_log_mapper=None):
        if receipt_log_mapper is None:
            self.receipt_log_mapper = IcxReceiptLogMapper()
        else:
            self.receipt_log_mapper = receipt_log_mapper

    def json_dict_to_receipt(self, json_dict):
        receipt = IcxReceipt()

        receipt.transaction_hash = json_dict.get("txHash")
        receipt.transaction_index = hex_to_dec(json_dict.get("txIndex"))
        receipt.block_hash = json_dict.get("blockHash")
        receipt.block_number = hex_to_dec(json_dict.get("blockHeight"))
        receipt.cumulative_step_used = hex_to_dec(
            json_dict.get("cumulativeStepUsed"))
        receipt.step_used = hex_to_dec(json_dict.get("stepUsed"))
        receipt.step_price = hex_to_dec(json_dict.get("stepPrice"))
        receipt.score_address = to_normalized_address(
            json_dict.get("scoreAddress"))
        receipt.status = hex_to_dec(json_dict.get("status"))

        if "eventLogs" in json_dict:
            log_count = len(json_dict["eventLogs"])
        else:
            log_count = 0

        if "eventLogs" in json_dict:
            receipt.logs = [
                self.receipt_log_mapper.json_dict_to_receipt_log(
                    log,
                    idx,
                    receipt.transaction_hash,
                    receipt.transaction_index,
                    receipt.block_hash,
                    receipt.block_number,
                    log_count,
                ) for idx, log in enumerate(json_dict["eventLogs"])
            ]

        return receipt

    def receipt_to_dict(self, receipt):
        return {
            "type": "receipt",
            "transaction_hash": receipt.transaction_hash,
            "transaction_index": receipt.transaction_index,
            "block_hash": receipt.block_hash,
            "block_number": receipt.block_number,
            "cumulative_step_used": receipt.cumulative_step_used,
            "step_used": receipt.step_used,
            "step_price": receipt.step_price,
            "score_address": receipt.score_address,
            "status": receipt.status,
        }
Exemplo n.º 3
0
    def __init__(
        self,
        transaction_hashes_iterable,
        batch_size,
        batch_web3_provider,
        max_workers,
        item_exporter,
        export_receipts=True,
        export_logs=True,
    ):
        self.batch_web3_provider = batch_web3_provider
        self.transaction_hashes_iterable = transaction_hashes_iterable

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

        self.export_receipts = export_receipts
        self.export_logs = export_logs
        if not self.export_receipts and not self.export_logs:
            raise ValueError(
                "At least one of export_receipts or export_logs must be True")

        self.receipt_mapper = IcxReceiptMapper()
        self.receipt_log_mapper = IcxReceiptLogMapper()
Exemplo n.º 4
0
 def __init__(self, receipt_log_mapper=None):
     if receipt_log_mapper is None:
         self.receipt_log_mapper = IcxReceiptLogMapper()
     else:
         self.receipt_log_mapper = receipt_log_mapper