コード例 #1
0
ファイル: test_v17_to_v18.py プロジェクト: binaryflesh/raiden
def test_upgrade_v17_to_v18(tmp_path):
    old_db_filename = tmp_path / Path('v17_log.db')
    with patch('raiden.utils.upgrades.latest_db_file') as latest_db_file:
        latest_db_file.return_value = str(old_db_filename)
        storage = setup_storage(str(old_db_filename))
        with patch('raiden.storage.sqlite.RAIDEN_DB_VERSION', new=17):
            storage.update_version()
        storage.conn.close()

    web3, _ = create_fake_web3_for_block_hash(number_of_blocks=100)
    db_path = tmp_path / Path('v18_log.db')
    manager = UpgradeManager(db_filename=str(db_path), web3=web3)

    with patch(
            'raiden.utils.upgrades.UPGRADES_LIST',
            new=[UpgradeRecord(from_version=17, function=upgrade_v17_to_v18)],
    ):
        manager.run()

    storage = SQLiteStorage(str(db_path))
    _, snapshot = storage.get_latest_state_snapshot()

    snapshot_data = json.loads(snapshot)
    secrethash = list(
        snapshot_data['payment_mapping']['secrethashes_to_task'].keys())[0]
    mediator_task = snapshot_data['payment_mapping']['secrethashes_to_task'][
        secrethash]
    assert mediator_task['mediator_state']['waiting_transfer'] is not None
    assert mediator_task['mediator_state']['routes']
コード例 #2
0
ファイル: test_upgrade.py プロジェクト: strategist922/Raiden
def test_upgrade_manager_restores_backup(tmp_path, monkeypatch):
    db_path = tmp_path / Path("v17_log.db")

    old_db_filename = tmp_path / Path("v16_log.db")

    storage = setup_storage(old_db_filename)

    with patch("raiden.storage.sqlite.RAIDEN_DB_VERSION", new=16):
        storage.update_version()
        storage.conn.close()

    upgrade_functions = [UpgradeRecord(from_version=16, function=Mock())]

    upgrade_functions[0].function.return_value = 17

    web3, _ = create_fake_web3_for_block_hash(number_of_blocks=1)
    with monkeypatch.context() as m:
        m.setattr(raiden.utils.upgrades, "UPGRADES_LIST", upgrade_functions)
        m.setattr(raiden.utils.upgrades, "RAIDEN_DB_VERSION", 19)
        UpgradeManager(db_filename=db_path, web3=web3).run()

    # Once restored, the state changes written above should be
    # in the restored database
    storage = SQLiteStorage(str(db_path))
    state_change_record = storage.get_latest_state_change_by_data_field(
        {"_type": "raiden.transfer.state_change.ActionInitChain"}
    )
    assert state_change_record.data is not None
コード例 #3
0
def test_upgrade_manager_restores_backup(tmp_path, monkeypatch):
    db_path = tmp_path / Path("v17_log.db")

    old_db_filename = tmp_path / Path("v16_log.db")

    with patch("raiden.storage.sqlite.RAIDEN_DB_VERSION",
               new=16), SQLiteStorage(str(old_db_filename)) as storage:
        state_change = ActionInitChain(
            chain_id=1,
            our_address=factories.make_address(),
            block_number=1,
            block_hash=factories.make_block_hash(),
            pseudo_random_generator=random.Random(),
        )
        action_init_chain_data = JSONSerializer.serialize(state_change)
        storage.write_state_changes(state_changes=[action_init_chain_data])
        storage.update_version()

    upgrade_functions = [UpgradeRecord(from_version=16, function=Mock())]

    upgrade_functions[0].function.return_value = 17

    web3, _ = create_fake_web3_for_block_hash(number_of_blocks=1)
    with monkeypatch.context() as m:
        m.setattr(raiden.utils.upgrades, "UPGRADES_LIST", upgrade_functions)
        m.setattr(raiden.utils.upgrades, "RAIDEN_DB_VERSION", 19)
        UpgradeManager(db_filename=db_path, web3=web3).run()

    # Once restored, the state changes written above should be
    # in the restored database
    with SQLiteStorage(str(db_path)) as storage:
        state_change_record = storage.get_latest_state_change_by_data_field(
            FilteredDBQuery(
                filters=[{
                    "_type":
                    "raiden.transfer.state_change.ActionInitChain"
                }],
                main_operator=Operator.NONE,
                inner_operator=Operator.NONE,
            ))
        assert state_change_record.data is not None
コード例 #4
0
ファイル: test_v16_to_v17.py プロジェクト: binaryflesh/raiden
def test_upgrade_v16_to_v17(tmp_path):
    old_db_filename = tmp_path / Path('v16_log.db')
    with patch('raiden.utils.upgrades.latest_db_file') as latest_db_file:
        latest_db_file.return_value = str(old_db_filename)

        with patch('raiden.storage.sqlite.RAIDEN_DB_VERSION', new=16):
            storage = setup_storage(str(old_db_filename))
            storage.update_version()

        storage.conn.close()

    db_path = tmp_path / Path('v17_log.db')
    web3, _ = create_fake_web3_for_block_hash(number_of_blocks=100)
    manager = UpgradeManager(db_filename=str(db_path), web3=web3)
    with patch(
            'raiden.utils.upgrades.UPGRADES_LIST',
            new=[UpgradeRecord(from_version=16, function=upgrade_v16_to_v17)],
    ):
        manager.run()

    storage = SQLiteStorage(str(db_path))
    snapshot = storage.get_latest_state_snapshot()
    assert snapshot is not None
コード例 #5
0
def test_upgrade_v18_to_v19(tmp_path):
    old_db_filename = tmp_path / Path("v18_log.db")
    with patch("raiden.utils.upgrades.latest_db_file") as latest_db_file:
        latest_db_file.return_value = str(old_db_filename)
        storage = setup_storage(str(old_db_filename))
        with patch("raiden.storage.sqlite.RAIDEN_DB_VERSION", new=18):
            storage.update_version()
        storage.conn.close()

    web3, block_to_blockhash = create_fake_web3_for_block_hash(
        number_of_blocks=100)
    db_path = tmp_path / Path("v19_log.db")
    manager = UpgradeManager(db_filename=str(db_path), web3=web3)
    with patch(
            "raiden.utils.upgrades.UPGRADES_LIST",
            new=[UpgradeRecord(from_version=18, function=upgrade_v18_to_v19)],
    ):
        manager.run()

    storage = SQLiteStorage(str(db_path))
    # Check that all the relevant state changes now have the blockhash attribute
    batch_query = storage.batch_query_state_changes(
        batch_size=500,
        filters=[
            ("_type", "raiden.transfer.state_change.ContractReceive%"),
            ("_type", "raiden.transfer.state_change.ActionInitChain"),
        ],
    )
    for state_changes_batch in batch_query:
        for state_change_record in state_changes_batch:
            data = json.loads(state_change_record.data)
            affected_state_change = (
                "raiden.transfer.state_change.ContractReceive" in data["_type"]
                or "raiden.transfer.state_change.ActionInitChain"
                in data["_type"])
            assert affected_state_change, "filtering did not work correctly"
            assert "block_hash" in data
            block_number = int(data["block_number"])
            assert block_to_blockhash[block_number].hex() == data["block_hash"]

    # Check that all the relevant events now have the triggered_by_blockhash attribute
    event_records = []
    batch_query = storage.batch_query_event_records(
        batch_size=500, filters=[("_type", "%events.ContractSend%")])

    for events_batch in batch_query:
        event_records.extend(events_batch)

    assert len(event_records)
    for event_record in event_records:
        data = json.loads(event_record.data)
        assert "events.ContractSend" in data["_type"]
        assert "triggered_by_block_hash" in data

    # Finally check that the snapshot is updated and that it contains a blockhash and that all
    # pending transactions in the list also contain one
    _, snapshot = storage.get_latest_state_snapshot()
    snapshot_data = json.loads(snapshot)
    assert "block_hash" in snapshot_data
    assert len(snapshot_data["pending_transactions"]) == 2
    for transaction_data in snapshot_data["pending_transactions"]:
        assert "triggered_by_block_hash" in transaction_data