Example #1
0
def convert_to_text(data: bytes) -> str:
    """

    :param data:
    :return:
    """
    return Web3.toText(data)
Example #2
0
def test_ddo_on_chain(config, web3):
    """Tests chain operations on a DDO."""
    ddo_address = get_contracts_addresses(config.address_file, "ganache")[
        MetadataContract.CONTRACT_NAME
    ]
    dtfactory_address = get_contracts_addresses(config.address_file, "ganache")[
        DTFactory.CONTRACT_NAME
    ]
    ddo_registry = MetadataContract(web3, ddo_address)
    wallet = get_publisher_wallet()

    dtfactory = DTFactory(web3, dtfactory_address)
    tx_id = dtfactory.createToken("", "dt1", "dt1", 1000, wallet)
    dt = DataToken(web3, dtfactory.get_token_address(tx_id))

    # test create ddo
    asset = get_ddo_sample(dt.address)
    old_name = asset.metadata["main"]["name"]
    txid = ddo_registry.create(
        asset.asset_id, b"", lzma.compress(Web3.toBytes(text=asset.as_text())), wallet
    )
    assert ddo_registry.verify_tx(txid), f"create ddo failed: txid={txid}"
    logs = ddo_registry.event_MetadataCreated.processReceipt(
        ddo_registry.get_tx_receipt(web3, txid), errors=DISCARD
    )
    assert logs, f"no logs found for create ddo tx {txid}"
    log = logs[0]
    assert add_0x_prefix(log.args.dataToken) == asset.asset_id
    # read back the asset ddo from the event log
    ddo_text = Web3.toText(lzma.decompress(log.args.data))
    assert ddo_text == asset.as_text(), "ddo text does not match original."

    _asset = V3Asset(json_text=ddo_text)
    assert _asset.did == asset.did, "did does not match."
    name = _asset.metadata["main"]["name"]
    assert name == old_name, f"name does not match: {name} != {old_name}"

    # test_update ddo
    asset.metadata["main"]["name"] = "updated name for test"
    txid = ddo_registry.update(
        asset.asset_id, b"", lzma.compress(Web3.toBytes(text=asset.as_text())), wallet
    )
    assert ddo_registry.verify_tx(txid), f"update ddo failed: txid={txid}"
    logs = ddo_registry.event_MetadataUpdated.processReceipt(
        ddo_registry.get_tx_receipt(web3, txid), errors=DISCARD
    )
    assert logs, f"no logs found for update ddo tx {txid}"
    log = logs[0]
    assert add_0x_prefix(log.args.dataToken) == asset.asset_id
    # read back the asset ddo from the event log
    ddo_text = Web3.toText(lzma.decompress(log.args.data))
    assert ddo_text == asset.as_text(), "ddo text does not match original."
    _asset = V3Asset(json_text=ddo_text)
    assert (
        _asset.metadata["main"]["name"] == "updated name for test"
    ), "name does not seem to be updated."
    assert DataToken(web3, asset.asset_id).contract.caller.isMinter(wallet.address)

    # test update fails from wallet other than the original publisher
    bob = get_consumer_wallet()
    try:
        txid = ddo_registry.update(
            asset.asset_id, b"", lzma.compress(Web3.toBytes(text=asset.as_text())), bob
        )
        assert ddo_registry.verify_tx(txid) is False, f"update ddo failed: txid={txid}"
        logs = ddo_registry.event_MetadataUpdated.processReceipt(
            ddo_registry.get_tx_receipt(web3, txid), errors=DISCARD
        )
        assert (
            not logs
        ), f"should be no logs for MetadataUpdated, but seems there are some logs: tx {txid}, logs {logs}"
    except ValueError:
        print("as expected, only owner can update a published ddo.")

    # test ddoOwner
    assert DataToken(web3, asset.asset_id).contract.caller.isMinter(wallet.address), (
        f"ddo owner does not match the expected publisher address {wallet.address}, "
        f"owner is {DataToken(web3, asset.asset_id).contract.caller.minter(wallet.address)}"
    )