Exemple #1
0
def add_block_number(block_number):
    """
    Adds the block, transactions, uncles, logs and traces of a given block
    number into the db_session

    :param int block_number: The block number to add to the database
    """
    current_session = get_current_session()
    block_task_meta = BlockTaskMeta.update_block_task_meta_from_block_number(
        block_number=block_number, state='STARTED')

    # getting the block_data from the node
    block_data = current_session.w3.eth.getBlock(block_identifier=block_number,
                                                 full_transactions=True)
    timestamp = to_int(block_data['timestamp'])
    iso_timestamp = datetime.utcfromtimestamp(timestamp).isoformat()
    block = Blocks.add_block(block_data=block_data,
                             iso_timestamp=iso_timestamp)
    block_hash = block.block_hash

    if current_session.settings.PARSE_TRACE and \
       current_session.settings.PARSE_STATE_DIFF and \
       block_number != 0:
        block_trace_list = current_session.w3.parity.\
            traceReplayBlockTransactions(block_number,
                                         mode=['trace', 'stateDiff'])
    elif block_number != 0:
        if current_session.settings.PARSE_TRACE:
            block_trace_list = current_session.w3.parity.\
                traceReplayBlockTransactions(block_number,
                                             mode=['trace'])

        if current_session.settings.PARSE_STATE_DIFF:
            block_trace_list = current_session.w3.parity.\
                traceReplayBlockTransactions(block_number,
                                             mode=['stateDiff'])

    # added the block data in the db session
    with current_session.db_session_scope():
        current_session.db_session.add(block)

        uncle_hashes = block_data['uncles']
        uncle_list = []
        for i in range(0, len(uncle_hashes)):
            # Unfortunately there is no command eth_getUncleByHash
            uncle_data = current_session.w3.eth.getUncleByBlock(
                block_number, i)
            uncle = Uncles.add_uncle(uncle_data=uncle_data,
                                     block_number=block_number,
                                     iso_timestamp=iso_timestamp)
            current_session.db_session.add(uncle)
            uncle_list.append(uncle)

        transaction_list = block_data['transactions']
        # loop to get the transaction, receipts, logs and traces of the block
        for index, transaction_data in enumerate(transaction_list):
            transaction = Transactions.add_transaction(
                transaction_data,
                block_number=block_number,
                iso_timestamp=iso_timestamp)
            # added the transaction in the db session
            current_session.db_session.add(transaction)

            receipt_data = current_session.w3.eth.getTransactionReceipt(
                transaction_data['hash'])
            receipt = Receipts.add_receipt(receipt_data,
                                           block_number=block_number,
                                           timestamp=iso_timestamp)
            current_session.db_session.add(receipt)
            fees = int(transaction.gas_price) * int(receipt.gas_used)

            log_list = receipt_data['logs']
            Logs.add_log_list(current_session=current_session,
                              log_list=log_list,
                              block_number=block_number,
                              timestamp=transaction.timestamp)

            if current_session.settings.PARSE_TRACE:
                trace_list = block_trace_list[index]['trace']
                Traces.add_trace_list(
                    current_session=current_session,
                    trace_list=trace_list,
                    transaction_hash=transaction.transaction_hash,
                    transaction_index=transaction.transaction_index,
                    block_number=transaction.block_number,
                    timestamp=transaction.timestamp)

            if current_session.settings.PARSE_STATE_DIFF:
                state_diff_dict = block_trace_list[index]['stateDiff']
                if state_diff_dict is not None:
                    StateDiff.add_state_diff_dict(
                        current_session=current_session,
                        state_diff_dict=state_diff_dict,
                        transaction_hash=transaction.transaction_hash,
                        transaction_index=transaction.transaction_index,
                        block_number=transaction.block_number,
                        timestamp=transaction.timestamp,
                        miner=block.miner,
                        fees=fees)
        if block_number == 0:
            StateDiff.parse_genesis_rewards(current_session=current_session,
                                            block=block)
        else:
            StateDiff.add_mining_rewards(current_session=current_session,
                                         block=block,
                                         uncle_list=uncle_list)
        # updating the meta info table
        MetaInfo.set_last_pushed_block(current_session, block_number)
    logger.info("Commiting block: {} to sql".format(block_number))
    return block_hash
Exemple #2
0
def add_block_number(block_number, ether_sql_session):
    """
    Adds the block, transactions, uncles, logs and traces of a given block
    number into the db_session

    :param int block_number: The block number to add to the database
    """
    # getting the block_data from the node
    block_data = ether_sql_session.w3.eth.getBlock(
        block_identifier=block_number, full_transactions=True)
    timestamp = to_int(block_data['timestamp'])
    iso_timestamp = datetime.utcfromtimestamp(timestamp).isoformat()
    block = Blocks.add_block(block_data=block_data,
                             iso_timestamp=iso_timestamp)
    ether_sql_session.db_session.add(
        block)  # added the block data in the db session

    logger.debug('Reached this spot')
    transaction_list = block_data['transactions']
    # loop to get the transaction, receipts, logs and traces of the block
    for transaction_data in transaction_list:
        transaction = Transactions.add_transaction(transaction_data,
                                                   block_number=block_number,
                                                   iso_timestamp=iso_timestamp)
        # added the transaction in the db session
        ether_sql_session.db_session.add(transaction)

        receipt_data = ether_sql_session.w3.eth.getTransactionReceipt(
            transaction_data['hash'])
        receipt = Receipts.add_receipt(receipt_data,
                                       block_number=block_number,
                                       timestamp=iso_timestamp)

        ether_sql_session.db_session.add(
            receipt)  # added the receipt in the database

        logs_list = receipt_data['logs']
        for dict_log in logs_list:
            log = Logs.add_log(dict_log,
                               block_number=block_number,
                               iso_timestamp=iso_timestamp)
            ether_sql_session.db_session.add(
                log)  # adding the log in db session

        if ether_sql_session.settings.PARSE_TRACE:
            dict_trace_list = ether_sql_session.w3.parity.traceTransaction(
                to_hex(transaction_data['hash']))
            if dict_trace_list is not None:
                for dict_trace in dict_trace_list:
                    trace = Traces.add_trace(dict_trace,
                                             block_number=block_number,
                                             timestamp=iso_timestamp)
                    ether_sql_session.db_session.add(
                        trace)  # added the trace in the db session

    uncle_list = block_data['uncles']
    for i in range(0, len(uncle_list)):
        # Unfortunately there is no command eth_getUncleByHash
        uncle_data = ether_sql_session.w3.eth.getUncleByBlock(block_number, i)
        uncle = Uncles.add_uncle(uncle_data=uncle_data,
                                 block_number=block_number,
                                 iso_timestamp=iso_timestamp)
        ether_sql_session.db_session.add(uncle)

    return ether_sql_session