Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove obsolete database_blob type #6196

Merged
merged 2 commits into from
Jul 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .flake8
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
[flake8]
jobs = auto
max-line-length = 120
select=E901,E999,F821,F822,F823,I100,I101,I201,I202,T001,T002,T003,T004
ignore=E203,W503,C0330
Expand Down
13 changes: 8 additions & 5 deletions src/tribler-core/tribler_core/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@
from pathlib import Path
from unittest.mock import Mock

import pytest
from aiohttp import web
from pony.orm import db_session

from ipv8.database import database_blob
from ipv8.keyvault.private.libnaclkey import LibNaCLSK
from ipv8.util import succeed

from pony.orm import db_session

import pytest

from tribler_common.network_utils import NetworkUtils
from tribler_common.simpledefs import DLSTATUS_SEEDING

from tribler_core.config.tribler_config import TriblerConfig
from tribler_core.modules.libtorrent.download import Download
from tribler_core.modules.libtorrent.download_config import DownloadConfig
Expand Down Expand Up @@ -313,6 +316,6 @@ def needle_in_haystack(enable_chant, enable_api, session): # pylint: disable=un
_ = session.mds.ChannelMetadata(title='test', tags='test', subscribed=True, infohash=random_infohash())
for x in range(0, num_hay):
session.mds.TorrentMetadata(title='hay ' + str(x), infohash=random_infohash())
session.mds.TorrentMetadata(title='needle', infohash=database_blob(bytearray(random_infohash())))
session.mds.TorrentMetadata(title='needle2', infohash=database_blob(bytearray(random_infohash())))
session.mds.TorrentMetadata(title='needle', infohash=random_infohash())
session.mds.TorrentMetadata(title='needle2', infohash=random_infohash())
return session
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def pack(self, signature_a=True, signature_b=True) -> bytes:
Encode this block for transport.
:param signature_a: False to pack EMPTY_SIG in the location of signature A, true to pack the signature A field.
:param signature_b: False to pack EMPTY_SIG in the location of signature B, true to pack the signature B field.
:return: the database_blob the data was packed into.
:return: bytes object the data was packed into.
"""
args = [self.sequence_number, self.public_key_a, self.public_key_b,
self.signature_a if signature_a else EMPTY_SIGNATURE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from unittest import mock
from unittest.mock import Mock, PropertyMock, patch

from ipv8.database import database_blob
from ipv8.keyvault.crypto import default_eccrypto
from ipv8.peer import Peer
from ipv8.test.base import TestBase
Expand All @@ -25,7 +24,7 @@
from tribler_core.utilities.path_util import Path
from tribler_core.utilities.random_utils import random_infohash

EMPTY_BLOB = database_blob(b"")
EMPTY_BLOB = b""

# pylint:disable=protected-access

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import asyncio
from asyncio import CancelledError, wait_for

from ipv8.database import database_blob
from ipv8.taskmanager import TaskManager, task

from pony.orm import db_session
Expand Down Expand Up @@ -310,7 +309,7 @@ def updated_my_channel(self, tdef):
Notify the core that we updated our channel.
"""
with db_session:
my_channel = self.session.mds.ChannelMetadata.get(infohash=database_blob(tdef.get_infohash()))
my_channel = self.session.mds.ChannelMetadata.get(infohash=tdef.get_infohash())
if (
my_channel
and my_channel.status == COMMITTED
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
from binascii import unhexlify
from datetime import datetime

from ipv8.database import database_blob

from lz4.frame import LZ4FrameCompressor

from pony import orm
Expand Down Expand Up @@ -246,7 +244,7 @@ class ChannelMetadata(db.TorrentMetadata, db.CollectionNode):
@db_session
def get_my_channels(cls):
return ChannelMetadata.select(
lambda g: g.origin_id == 0 and g.public_key == database_blob(cls._my_key.pub().key_to_bin()[10:])
lambda g: g.origin_id == 0 and g.public_key == cls._my_key.pub().key_to_bin()[10:]
)

@classmethod
Expand All @@ -261,7 +259,7 @@ def create_channel(cls, title, description="", origin_id=0):
"""
my_channel = cls(
origin_id=origin_id,
public_key=database_blob(cls._my_key.pub().key_to_bin()[10:]),
public_key=cls._my_key.pub().key_to_bin()[10:],
title=title,
tags=description,
subscribed=True,
Expand Down Expand Up @@ -447,15 +445,12 @@ def get_channels_by_title(cls, title):
@classmethod
@db_session
def get_channel_with_infohash(cls, infohash):
return cls.get(infohash=database_blob(infohash))
return cls.get(infohash=infohash)

@classmethod
@db_session
def get_recent_channel_with_public_key(cls, public_key):
return (
cls.select(lambda g: g.public_key == database_blob(public_key)).sort_by(lambda g: desc(g.id_)).first()
or None
)
return cls.select(lambda g: g.public_key == public_key).sort_by(lambda g: desc(g.id_)).first() or None

@classmethod
@db_session
Expand Down Expand Up @@ -487,7 +482,7 @@ def get_updated_channels(cls):
if g.subscribed == 1
and g.status != LEGACY_ENTRY
and (g.local_version < g.timestamp)
and g.public_key != database_blob(cls._my_key.pub().key_to_bin()[10:])
and g.public_key != cls._my_key.pub().key_to_bin()[10:]
) # don't simplify `g.subscribed == 1` to bool form, it is used by partial index!

@property
Expand Down Expand Up @@ -553,7 +548,7 @@ def get_channel_name(cls, dl_name, infohash):

if not channel:
return dl_name
if channel.infohash == database_blob(infohash):
if channel.infohash == infohash:
return channel.title
return 'OLD:' + channel.title

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import random
from datetime import datetime

from ipv8.database import database_blob
from ipv8.keyvault.crypto import default_eccrypto

from pony import orm
Expand Down Expand Up @@ -65,7 +64,7 @@ class ChannelNode(db.Entity):
reserved_flags = orm.Optional(int, size=16, default=0)
origin_id = orm.Optional(int, size=64, default=0, index=True)

public_key = orm.Required(database_blob)
public_key = orm.Required(bytes)
id_ = orm.Required(int, size=64)
orm.composite_key(public_key, id_)
orm.composite_index(public_key, origin_id)
Expand All @@ -74,7 +73,7 @@ class ChannelNode(db.Entity):
# Signature is nullable. This means that "None" entries are stored in DB as NULLs instead of empty strings.
# NULLs are not checked for uniqueness and not indexed.
# This is necessary to store unsigned signatures without violating the uniqueness constraints.
signature = orm.Optional(database_blob, unique=True, nullable=True, default=None)
signature = orm.Optional(bytes, unique=True, nullable=True, default=None)

# Local
added_on = orm.Optional(datetime, default=datetime.utcnow)
Expand Down Expand Up @@ -108,7 +107,7 @@ def __init__(self, *args, **kwargs):
# "sign_with" argument given, sign with it
private_key_override = None
if "sign_with" in kwargs:
kwargs["public_key"] = database_blob(kwargs["sign_with"].pub().key_to_bin()[10:])
kwargs["public_key"] = kwargs["sign_with"].pub().key_to_bin()[10:]
private_key_override = kwargs.pop("sign_with")

# Free-for-all entries require special treatment
Expand Down Expand Up @@ -140,8 +139,7 @@ def __init__(self, *args, **kwargs):
if not private_key_override and not skip_key_check:
# No key/signature given, sign with our own key.
if ("signature" not in kwargs) and (
("public_key" not in kwargs)
or (kwargs["public_key"] == database_blob(self._my_key.pub().key_to_bin()[10:]))
("public_key" not in kwargs) or (kwargs["public_key"] == self._my_key.pub().key_to_bin()[10:])
):
private_key_override = self._my_key

Expand Down Expand Up @@ -222,7 +220,7 @@ def to_delete_file(self, filename):
def sign(self, key=None):
if not key:
key = self._my_key
self.public_key = database_blob(key.pub().key_to_bin()[10:])
self.public_key = key.pub().key_to_bin()[10:]
_, self.signature = self._serialized(key)

def has_valid_signature(self):
Expand Down Expand Up @@ -251,7 +249,7 @@ def from_dict(cls, dct):
@property
@db_session
def is_personal(self):
return database_blob(self._my_key.pub().key_to_bin()[10:]) == database_blob(self.public_key)
return self._my_key.pub().key_to_bin()[10:] == self.public_key

@db_session
def soft_delete(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from datetime import datetime

from ipv8.database import database_blob

from pony import orm


Expand All @@ -13,7 +11,7 @@ class ChannelPeer(db.Entity):
"""

rowid = orm.PrimaryKey(int, size=64, auto=True)
public_key = orm.Required(database_blob, unique=True)
public_key = orm.Required(bytes, unique=True)
individual_votes = orm.Set("ChannelVote", reverse='voter')
added_on = orm.Optional(datetime, default=datetime.utcnow)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import os
from pathlib import Path

from ipv8.database import database_blob

from pony import orm
from pony.orm import db_session, select

Expand All @@ -22,7 +20,7 @@
UPDATED,
)
from tribler_core.modules.metadata_store.orm_bindings.torrent_metadata import tdef_to_metadata_dict
from tribler_core.modules.metadata_store.serialization import COLLECTION_NODE, CollectionNodePayload, CHANNEL_TORRENT
from tribler_core.modules.metadata_store.serialization import CHANNEL_TORRENT, COLLECTION_NODE, CollectionNodePayload
from tribler_core.utilities.random_utils import random_infohash

# pylint: disable=too-many-statements
Expand Down Expand Up @@ -57,8 +55,10 @@ def state(self):
return CHANNEL_STATE.PERSONAL.value

toplevel_parent = self.get_parent_nodes()[0]
if (toplevel_parent.metadata_type == CHANNEL_TORRENT and
toplevel_parent.local_version == toplevel_parent.timestamp):
if (
toplevel_parent.metadata_type == CHANNEL_TORRENT
and toplevel_parent.local_version == toplevel_parent.timestamp
):
return CHANNEL_STATE.COMPLETE.value

return CHANNEL_STATE.PREVIEW.value
Expand Down Expand Up @@ -94,7 +94,7 @@ def copy_torrent_from_infohash(self, infohash):
:return: New TorrentMetadata signed with your key.
"""

existing = db.TorrentMetadata.select(lambda g: g.infohash == database_blob(infohash)).first()
existing = db.TorrentMetadata.select(lambda g: g.infohash == infohash).first()

if not existing:
return None
Expand Down Expand Up @@ -298,8 +298,7 @@ def update_node_info(n):
dead_parents.remove(0)
# Delete orphans
db.ChannelNode.select(
lambda g: database_blob(db.ChannelNode._my_key.pub().key_to_bin()[10:]) # pylint: disable=W0212
== g.public_key
lambda g: db.ChannelNode._my_key.pub().key_to_bin()[10:] == g.public_key # pylint: disable=W0212
and g.origin_id in dead_parents
).delete()
orm.flush() # Just in case...
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from datetime import datetime
from struct import unpack

from ipv8.database import database_blob

from pony import orm
from pony.orm import db_session

Expand Down Expand Up @@ -55,7 +53,7 @@ class TorrentMetadata(db.MetadataNode):
_discriminator_ = REGULAR_TORRENT

# Serializable
infohash = orm.Required(database_blob, index=True)
infohash = orm.Required(bytes, index=True)
size = orm.Optional(int, size=64, default=0)
torrent_date = orm.Optional(datetime, default=datetime.utcnow, index=True)
tracker_info = orm.Optional(str, default='')
Expand Down Expand Up @@ -112,8 +110,8 @@ def add_ffa_from_dict(cls, ffa_dict):
# Check that this torrent is yet unknown to GigaChannel, and if there is no duplicate FFA entry.
# Test for a duplicate id_+public_key is necessary to account for a (highly improbable) situation when
# two entries have different infohashes but the same id_. We do not want people to exploit this.
ih_blob = database_blob(ffa_dict["infohash"])
pk_blob = database_blob(b"")
ih_blob = ffa_dict["infohash"]
pk_blob = b""
if cls.exists(lambda g: (g.infohash == ih_blob) or (g.id_ == id_ and g.public_key == pk_blob)):
return None
# Add the torrent as a free-for-all entry if it is unknown to GigaChannel
Expand Down Expand Up @@ -152,7 +150,7 @@ def metadata_conflicting(self, b):
@classmethod
@db_session
def get_with_infohash(cls, infohash):
return cls.select(lambda g: g.infohash == database_blob(infohash)).first()
return cls.select(lambda g: g.infohash == infohash).first()

@classmethod
@db_session
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from ipv8.database import database_blob

from pony import orm


Expand All @@ -10,7 +8,7 @@ class TorrentState(db.Entity):
"""

rowid = orm.PrimaryKey(int, auto=True)
infohash = orm.Required(database_blob, unique=True)
infohash = orm.Required(bytes, unique=True)
seeders = orm.Optional(int, default=0)
leechers = orm.Optional(int, default=0)
last_check = orm.Optional(int, size=64, default=0)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import enum
from dataclasses import dataclass, field

from ipv8.database import database_blob

from pony.orm import db_session

from tribler_core.modules.category_filter.l2_filter import is_forbidden
Expand Down Expand Up @@ -187,9 +185,7 @@ def update_local_node(self):
If we don't have some version of the node locally, CONTINUE control to further checks.
"""
# Check for the older version of the added node
node = self.mds.ChannelNode.get_for_update(
public_key=database_blob(self.payload.public_key), id_=self.payload.id_
)
node = self.mds.ChannelNode.get_for_update(public_key=self.payload.public_key, id_=self.payload.id_)
if not node:
return CONTINUE

Expand Down
Loading