예제 #1
0
파일: helper.py 프로젝트: cyrus19901/bond
def _produce(chain_file, config, item) -> bool:
    try:
        production_local_chain = dao.DiskStorage(chain_file)
        last_local_chain_hash = production_local_chain.get_last_hash()
        print('Last Blockchain state:')
        last_remote_state = config.client.last_state(item.origin)
        print(last_remote_state)
        print('----------')
        produced_data = dao.read_production_data(item, last_local_chain_hash,
                                                 last_remote_state)
        file_name_created = production_local_chain.add_to_chain(produced_data)
        print('Sending to Blockchain:')
        print(produced_data.produced.to_dict())
        print('----------')
        tx_receipt = config.client.mint(produced_data.produced, item.origin)
        print('Receipt Block Number: ' + str(tx_receipt['blockNumber']))
        print('-------------------')
        print('New Blockchain state:')
        last_remote_state = config.client.last_state(item.origin)
        print(last_remote_state)
        print('----------')
        print('New Local File:')
        print(file_name_created)
        print('----------\n')
        return True
    except Exception as e:
        print('[ERROR]')
        print(e)
        return False
예제 #2
0
파일: helper.py 프로젝트: yuhisern7/bond
def _produce(chain_file, config, item) -> bool:
    try:
        production_local_chain = dao.DiskStorage(chain_file, PERSISTENCE)
        last_local_chain_hash = production_local_chain.get_last_hash()
        last_remote_state = config.client.last_state(item.origin)
        produced_data = dao.read_production_data(item, last_local_chain_hash,
                                                 last_remote_state)
        created_file = production_local_chain.add_to_chain(produced_data)
        tx_receipt = config.client.mint(produced_data.produced, item.origin)
        class_name = item.energy.__class__.__name__
        data = produced_data.produced
        block_number = str(tx_receipt['blockNumber'])
        msg = '[PROD] meter: {} - {} watts - {} kg of Co2 - block: {}'
        if data.is_meter_down:
            logger.warning(
                msg.format(class_name, data.energy, data.co2_saved,
                           block_number))
        else:
            logger.info(
                msg.format(class_name, data.energy, data.co2_saved,
                           block_number))
        return True
    except Exception as e:
        error_log.exception("[BOND][PROD] meter: {} - stack: {}".format(
            item.energy.__class__.__name__, e))
        return False
예제 #3
0
def _consume(chain_file, config, item):
    try:
        consumption_local_chain = dao.DiskStorage(chain_file, PERSISTENCE)
        last_local_chain_hash = consumption_local_chain.get_last_hash()
        last_remote_state = config.client.last_state(item.origin)
        consumed_data = dao.read_consumption_data(item, last_local_chain_hash, last_remote_state)
        created_file = consumption_local_chain.add_to_chain(consumed_data)
        tx_receipt = config.client.mint(consumed_data.consumed, item.origin)
        class_name = item.energy.__class__.__name__
        data = consumed_data.consumed
        block_number = str(tx_receipt['blockNumber'])
        message = '[COMS] meter: {} - {} watts - block: {}'
        if data.is_meter_down:
            logger.warning(message.format(class_name, data.energy, block_number))
        else:
            logger.info(message.format(class_name, data.energy, block_number))
        return True
    except Exception as e:
        error_log.exception("[BOND][COMS] meter: {} - stack: {}".format(item.energy.__class__.__name__, e))
        return False