def get_state_change_with_balance_proof_by_locksroot( storage: SerializedSQLiteStorage, canonical_identifier: CanonicalIdentifier, locksroot: Locksroot, sender: Address, ) -> Optional[StateChangeRecord]: """ Returns the state change which contains the corresponding balance proof. Use this function to find a balance proof for a call to unlock, which only happens after settle, so the channel has the unblinded version of the balance proof. """ filters: List[Dict[str, Any]] = list() filters.append({ "balance_proof.canonical_identifier.chain_identifier": str(canonical_identifier.chain_identifier), "balance_proof.canonical_identifier.token_network_address": to_checksum_address(canonical_identifier.token_network_address), "balance_proof.canonical_identifier.channel_identifier": str(canonical_identifier.channel_identifier), "balance_proof.locksroot": to_hex(locksroot), "balance_proof.sender": to_checksum_address(sender), }) query = FilteredDBQuery(filters=filters, main_operator=Operator.NONE, inner_operator=Operator.AND) return storage.get_latest_state_change_by_data_field(query)
def get_state_change_with_balance_proof_by_balance_hash( storage: SerializedSQLiteStorage, canonical_identifier: CanonicalIdentifier, balance_hash: BalanceHash, sender: Address, ) -> Optional[StateChangeRecord]: """Returns the state change which contains the corresponding balance proof. Use this function to find a balance proof for a call to settle, which only has the blinded balance proof data. """ filters: List[Dict[str, Any]] = list() filters.append({ "balance_proof.canonical_identifier.chain_identifier": str(canonical_identifier.chain_identifier), "balance_proof.canonical_identifier.token_network_address": to_hex_address(canonical_identifier.token_network_address), "balance_proof.canonical_identifier.channel_identifier": str(canonical_identifier.channel_identifier), "balance_proof.balance_hash": to_hex(balance_hash), "balance_proof.sender": to_hex_address(sender), }) query = FilteredDBQuery(filters=filters, main_operator=Operator.NONE, inner_operator=Operator.AND) return storage.get_latest_state_change_by_data_field(query)
def get_state_change_with_transfer_by_secrethash( storage: SerializedSQLiteStorage, secrethash: SecretHash) -> Optional[StateChangeRecord]: filters = [ { "from_transfer.lock.secrethash": to_hex(secrethash) }, { "transfer.lock.secrethash": to_hex(secrethash) }, ] query = FilteredDBQuery(filters=filters, main_operator=Operator.OR, inner_operator=Operator.NONE) return storage.get_latest_state_change_by_data_field(query)
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
def get_event_with_balance_proof_by_balance_hash( storage: SerializedSQLiteStorage, canonical_identifier: CanonicalIdentifier, balance_hash: BalanceHash, recipient: Address, ) -> Optional[EventRecord]: """ Returns the event which contains the corresponding balance proof. Use this function to find a balance proof for a call to settle, which only has the blinded balance proof data. """ filters: List[Dict[str, Any]] = list() filter_items = { "canonical_identifier.chain_identifier": str(canonical_identifier.chain_identifier), "canonical_identifier.token_network_address": to_checksum_address(canonical_identifier.token_network_address), "canonical_identifier.channel_identifier": str(canonical_identifier.channel_identifier), "balance_hash": to_hex(balance_hash), } balance_proof_filters = balance_proof_query_from_keys(prefix="", filters=filter_items) balance_proof_filters["recipient"] = to_checksum_address(recipient) filters.append(balance_proof_filters) transfer_filters = balance_proof_query_from_keys(prefix="transfer.", filters=filter_items) transfer_filters["recipient"] = to_checksum_address(recipient) filters.append(transfer_filters) query = FilteredDBQuery(filters=filters, main_operator=Operator.OR, inner_operator=Operator.AND) event = storage.get_latest_event_by_data_field(query) return event