コード例 #1
0
    def get_votes_for_peer(self, peer):
        """
        Get votes for peer

        :param peer: public key of peer
        :type peer: bytes
        :return: vote information
        """
        self._logger.debug("persistence: Getting votes for peer (%s)", hexlify(peer))

        sql = "SELECT * FROM module_votes WHERE voter_public_key = ?;"
        res = list(self.execute(sql, (database_blob(peer),)))

        votes = []
        for vote in res:
            voter_public_key = bytes(vote[0])
            creator = bytes(vote[1])
            content_hash = str(vote[2])
            identifier = ModuleIdentifier(creator, content_hash)

            votes.append({
                'voter': voter_public_key,
                'identifier': identifier,
            })
        return votes
コード例 #2
0
    def create_module(self, name):
        """
        Create a module

        :return: None
        """
        module_package_directory = os.path.join(self.working_directory, MODULE_PACKAGE_DIR, name)

        print module_package_directory

        if not os.path.isdir(module_package_directory):
            self._logger.info("module-community: module package (%s) does not exists", name)
            return

        package = self.transport.create_module_package(name)
        info_hash = str(package['info_hash'])
        name = str(package['name'])

        identifier = ModuleIdentifier(self.my_peer.public_key.key_to_bin(), info_hash)
        module = Module(identifier, name)

        self._logger.info("module-community: creating module (%s, %s)", module.id, module.name)

        if self.persistence.has_module_in_catalog(module.id):
            self._logger.info("module-community: module (%s) already exists, not creating new one", module.id)
            return

        self.persistence.add_module_to_catalog(module)
        self.vote_module(module.id)
コード例 #3
0
    def get_votes_for_module(self, module_identifier):
        """
        Get votes for module

        :param module_identifier: module identifier
        :type module_identifier: ModuleIdentifier
        :return: vote information
        """
        self._logger.debug("persistence: Getting votes for module (%s)", module_identifier)

        if not self.has_module_in_catalog(module_identifier):
            return None

        sql = "SELECT * FROM module_votes WHERE public_key = ? AND info_hash = ?;"
        res = list(
            self.execute(sql, (database_blob(module_identifier.creator), database_blob(module_identifier.content_hash),)))

        votes = []
        for vote in res:
            voter_public_key = bytes(vote[0])
            creator = bytes(vote[1])
            content_hash = str(vote[2])
            identifier = ModuleIdentifier(creator, content_hash)

            votes.append({
                'voter': voter_public_key,
                'identifier': identifier,
            })
        return votes
コード例 #4
0
    def get_module_from_catalog(self, module_identifier):
        """
        Get module from the catalog

        :param module_identifier: module identifier
        :type module_identifier: ModuleIdentifier
        :return: module information
        """
        self._logger.debug("persistence: Getting module (%s) from catalog", module_identifier)

        if not self.has_module_in_catalog(module_identifier):
            return None

        sql = "SELECT * FROM module_catalog WHERE public_key = ? AND info_hash = ?;"
        res = list(
            self.execute(sql, (database_blob(module_identifier.creator), database_blob(module_identifier.content_hash),)))

        module = res[0]

        public_key = bytes(module[0])
        content_hash = str(module[1])
        name = str(module[2])
        identifier = ModuleIdentifier(public_key, content_hash)
        votes = int(module[3])

        return Module(identifier, name, votes)
コード例 #5
0
    def get_modules_from_library(self):
        """
        Retrieve all modules from the library

        :return: All modules
        """
        self._logger.debug("persistence: Getting all modules from library")

        sql = "SELECT * FROM module_library;"
        res = list(self.execute(sql))

        modules = []
        for module in res:
            public_key = bytes(module[0])
            content_hash = str(module[1])
            identifier = ModuleIdentifier(public_key, content_hash)

            modules.append(identifier)
        return modules
コード例 #6
0
    def create_module_test(self):
        """
        Create a test module

        :return: None
        """
        info_hash = "0000000000000000000000000000000000000000"
        name = "test"

        identifier = ModuleIdentifier(self.my_peer.public_key.key_to_bin(), info_hash)
        module = Module(identifier, name)

        self._logger.info("module-community: creating test module (%s, %s)", module.id, module.name)

        if self.persistence.has_module_in_catalog(module.id):
            self._logger.info("module-community: test module (%s) already exists, not creating new one", module.id)
            return

        self.persistence.add_module_to_catalog(module)
        self.vote_module(module.id)
コード例 #7
0
    def get_modules_from_catalog(self):
        """
        Retrieve all modules from the catalog

        :return: All modules
        """
        self._logger.debug("persistence: Getting all modules from catalog")

        sql = "SELECT * FROM module_catalog;"
        res = list(self.execute(sql))

        modules = []
        for module in res:
            public_key = bytes(module[0])
            content_hash = str(module[1])
            name = str(module[2])
            identifier = ModuleIdentifier(public_key, content_hash)
            votes = int(module[3])

            modules.append(Module(identifier, name, votes))
        return modules
コード例 #8
0
    def _process_vote_block(self, block):
        """
        Internal function for processing vote blocks

        :param block: The block to be processed
        :type block: ModuleBlock
        :return: None
        """
        block_id = block.block_id

        self._logger.debug("module-community: Received vote block (%s)", block_id)

        if not block.is_valid_vote_block:
            self._logger.debug("module-community: Invalid vote block (%s) received!", block_id)
            return

        public_key = block.public_key  # type: bytes
        tx_dict = block.transaction  # type: dict
        creator = tx_dict[MODULE_BLOCK_TYPE_VOTE_KEY_CREATOR]  # type: bytes
        content_hash = tx_dict[MODULE_BLOCK_TYPE_VOTE_KEY_CONTENT_HASH]  # type: str
        name = tx_dict[MODULE_BLOCK_TYPE_VOTE_KEY_NAME]  # type: str

        identifier = ModuleIdentifier(creator, content_hash)

        # Add module to catalog if it isn't known yet
        if not self.persistence.has_module_in_catalog(identifier):
            self._logger.info("module-community: Adding unknown module to catalog (%s, %s)", identifier, name)

            module = Module(identifier, name)
            self.persistence.add_module_to_catalog(module)

        # Add vote to catalog and votes if it isn't known yet
        if not self.persistence.did_vote(public_key, identifier):
            self._logger.info("module-community: Received vote (%s, %s)", identifier, name)
            self.persistence.add_vote_to_votes(public_key, identifier)
            self.persistence.add_vote_to_module_in_catalog(identifier)
コード例 #9
0
    def get_module_from_library(self, module_identifier):
        """
        Get module from the library

        :param module_identifier: module identifier
        :type module_identifier: ModuleIdentifier
        :return: module information
        """
        self._logger.debug("persistence: Getting module (%s) from library", module_identifier)

        if not self.has_module_in_library(module_identifier):
            return None

        sql = "SELECT * FROM module_library WHERE public_key = ? AND info_hash = ?;"
        res = list(
            self.execute(sql, (database_blob(module_identifier.creator), database_blob(module_identifier.content_hash),)))

        module = res[0]

        public_key = bytes(module[0])
        content_hash = str(module[1])
        identifier = ModuleIdentifier(public_key, content_hash)

        return identifier
コード例 #10
0
 def __init__(self, ipv8, creator, content_hash):
     ModuleEndpoint.__init__(self, ipv8)
     self._creator = creator
     self._content_hash = content_hash
     self._identifier = ModuleIdentifier(creator, content_hash)
コード例 #11
0
    def _check_votes_in_catalog(self):
        """
        Check if the votes in the catalog match the votes in trustchain

        :return: None
        """
        self._logger.info("module-community: Checking votes in catalog")

        blocks = self.trustchain.persistence.get_blocks_with_type(MODULE_BLOCK_TYPE_VOTE)  # type: [TrustChainBlock]

        votes = {}
        voters = {}

        for block in blocks:
            public_key = block.public_key  # type: bytes
            tx_dict = block.transaction  # type: dict
            creator = tx_dict[MODULE_BLOCK_TYPE_VOTE_KEY_CREATOR]  # type: bytes
            content_hash = tx_dict[MODULE_BLOCK_TYPE_VOTE_KEY_CONTENT_HASH]  # type: str
            name = tx_dict[MODULE_BLOCK_TYPE_VOTE_KEY_NAME]  # type: str

            identifier = ModuleIdentifier(creator, content_hash)

            # Check votes database
            if not self.persistence.did_vote(public_key, identifier):
                self.persistence.add_vote_to_votes(public_key, identifier)

            # Check number of votes
            if identifier in votes:
                votes[identifier] = votes[identifier] + 1
            else:
                votes[identifier] = 1

            # Check double votes
            public_key_hex = hexlify(public_key)
            if public_key_hex not in voters:
                voters[public_key_hex] = {}

            voted_modules = voters[public_key_hex]
            if identifier not in voted_modules:
                voted_modules[identifier] = 1
                voters[public_key_hex] = voted_modules
            else:
                self._logger.info("module-community: Double vote for module (%s) by peer (%s)", identifier, public_key_hex)

        modules = self.persistence.get_modules_from_catalog()

        # Compare and fix vote inconsistencies
        for module in modules:
            identifier = module.id
            votes_in_catalog = module.votes

            if identifier in votes and votes[identifier] != votes_in_catalog:
                self._logger.info("module-community: Vote inconsistency for module (%s)", identifier)
                self.persistence.update_module_in_catalog(identifier, votes[identifier])

            votes.pop(identifier)

        if len(votes) != 0:
            self._logger.info("module-community: inconsistent vote db")

        self._logger.info("module-community: Checking votes in catalog is done")