Ejemplo n.º 1
0
    def __init__(self, cid, master_public_key):
        super(SimpleDispersyTestCommunity, self).__init__(cid, master_public_key)
        if __debug__: dprint(self._cid.encode("HEX"))

        # ensure that two of the hardcoder members (A, B, or C) has been picked
        cluster = self.get_meta_message(u"last-1-subjective-sync").destination.cluster
        subjective_set = self.get_subjective_set(self._my_member, cluster)
        assert subjective_set
        assert self._my_member.public_key in subjective_set
        def count():
            counter = 0
            for name, public_key in self.hardcoded_member_public_keys.iteritems():
                if public_key in subjective_set:
                    if __debug__: dprint("hardcoded member ", name, " found in my subjective set")
                    counter += 1
            return counter
        # if (1) we are not one of the hardcoded members and (2) we did not yet pick hardcoded
        # members for our subjective set
        if not self._my_member.public_key in self.hardcoded_member_public_keys.values() and count() < 2:
            assert count() == 0
            assert len(self.hardcoded_member_public_keys) == 3
            keys = self.hardcoded_member_public_keys.values()
            shuffle(keys)
            self.create_dispersy_subjective_set(cluster, [self._my_member, self.get_member(keys[0]), self.get_member(keys[1])])
            subjective_set = self.get_subjective_set(self._my_member, cluster)
            assert count() == 2

        self._status = get_status_holder("dispersy-simple-dispersy-test")
        self._status.add_reporter(TUDelftReporter(REPORTER_NAME, 300, self._my_member.public_key))
        self._status.create_and_add_event("__init__^" + self._cid.encode("HEX"), ["last-1-subjective-sync"])
        self._status.create_and_add_event("info^" + self._cid.encode("HEX"), [self._dispersy.info()])
        self._status.create_and_add_event("subjective_set^" + self._cid.encode("HEX"), [(name, public_key in subjective_set) for name, public_key in self.hardcoded_member_public_keys.iteritems()])
        self._status.report_now()
        self._dispersy.callback.register(self._periodically_info, delay=60.0)
Ejemplo n.º 2
0
    def _unload_communities(self):
        def is_active(community, now):
            # check 1: does the community have any active candidates
            if community.update_strikes(now) < 3:
                return True

            # check 2: does the community have any cached messages waiting to be processed
            for meta in self._batch_cache.iterkeys():
                if meta.community == community:
                    return True

            # the community is inactive
            return False

        while True:
            yield 180.0
            now = time()
            inactive = [
                community for community in self._communities.itervalues()
                if not is_active(community, now)
            ]
            dprint("cleaning ", len(inactive), "/", len(self._communities),
                   " communities")
            for community in inactive:
                community.unload_community()
Ejemplo n.º 3
0
 def count():
     counter = 0
     for name, public_key in self.hardcoded_member_public_keys.iteritems():
         if public_key in subjective_set:
             if __debug__: dprint("hardcoded member ", name, " found in my subjective set")
             counter += 1
     return counter
Ejemplo n.º 4
0
 def check_full_sync(self, messages):
     if __debug__: dprint(self._cid.encode("HEX"))
     for message in messages:
         if not self._timeline.check(message):
             yield DropMessage(message, "TODO: implement delay by proof")
             continue
         yield message
Ejemplo n.º 5
0
    def check_permissions(self):
        """
        One or more dispersy-authorize messages are required to allow my member to create the
        messages for the test.  If we can not obtain the authorize messages we will create them.
        """
        metas = [self._community.get_meta_message(u"last-1-subjective-sync"), self._community.get_meta_message(u"dispersy-destroy-community")]
        sync_meta = self._community.get_meta_message(u"dispersy-sync")
        wait = 30
        for i in xrange(1, wait + 1):
            allowed, proofs = self._community._timeline._check(self._community.my_member, self._community.global_time, [(meta, u"permit") for meta in metas])
            if allowed:
                dprint("my member is allowed to create messages")
                break

            messages = [sync_meta.implement(sync_meta.authentication.implement(self._community.my_member),
                                            sync_meta.distribution.implement(self._community.global_time),
                                            sync_meta.destination.implement(),
                                            sync_meta.payload.implement(time_low, time_high, bloom_filter))
                        for time_low, time_high, bloom_filter
                        in self._community.dispersy_sync_bloom_filters]
            self._dispersy.store_update_forward(messages, False, False, True)
            dprint("syncing. ", i, "/", wait, "...")
            yield 1.0

        else:
            # Used in the 3.5.8 and 3.5.9 test
            #
            # dprint("creating authorizations")
            # permission_triplets = []
            # for message in self._community.get_meta_messages():
            #     if not isinstance(message.resolution, PublicResolution):
            #         for allowed in (u"authorize", u"revoke", u"permit"):
            #             permission_triplets.append((self._community.my_member, message, allowed))
            # if permission_triplets:
            #     self._community.create_dispersy_authorize(permission_triplets, sign_with_master=True)

            wait = 30
            for i in xrange(1, wait + 1):
                dprint("checking for permissions on disk. ", i, "/", wait, "...")
                try:
                    packet = open(expanduser("~/simpledispersytest_permission_packet"), "r").read()
                except:
                    yield 1.0
                else:
                    dprint("use existing permissions from disk")
                    self._community.dispersy.on_incoming_packets([(("130.161.158.222", 6711), packet)])
                    break
            else:
                dprint("creating authorizations")
                permission_triplets = []
                for message in self._community.get_meta_messages():
                    if not isinstance(message.resolution, PublicResolution):
                        for allowed in (u"authorize", u"revoke", u"permit"):
                            for public_key in self._community.hardcoded_member_public_keys.itervalues():
                                permission_triplets.append((Member.get_instance(public_key), message, allowed))
                if permission_triplets:
                    message = self._community.create_dispersy_authorize(permission_triplets, sign_with_master=True)
                    open(expanduser("~/simpledispersytest_permission_packet"), "w+").write(message.packet)

        yield 1.0
Ejemplo n.º 6
0
 def check_last_1_subjective_sync(self, messages):
     if __debug__: dprint(self._cid.encode("HEX"))
     for message in messages:
         allowed, proofs = self._timeline.check(message)
         if allowed:
             yield message
         else:
             yield DelayMessageByProof(message)
Ejemplo n.º 7
0
 def check_votecast(self, messages):
     for message in messages:
         if __debug__: dprint(message)
         
         community = self._get_channel_community(message.payload.cid)
         if not community._channel_id:
             yield DelayMessageReqChannelMessage(message, community, includeSnapshot = message.payload.vote > 0) #request torrents if positive vote
         else:
             yield message
Ejemplo n.º 8
0
 def __init__(self, rawserver, dispersy, port, ip="0.0.0.0"):
     while True:
         if __debug__: dprint("Dispersy listening at ", port)
         try:
             self.socket = rawserver.create_udpsocket(port, ip)
         except socket.error, error:
             port += 1
             continue
         break
Ejemplo n.º 9
0
 def warning(self):
     """
     BE CAREFULL!  WE CAN NOT UNDO THIS!
     """
     wait = 60
     for i in xrange(1, wait + 1):
         dprint("BE CAREFULL!  WE CAN NOT UNDO THIS!  ", i, "/", wait, "...")
         yield 1.0
     yield 1.0
Ejemplo n.º 10
0
    def set_online(self):
        """ Restore on_incoming_packets and _send functions of
        dispersy back to normal.

        This simulates a node coming online, since it's able to send
        and receive messages.
        """
        dprint("Going online")
        self._dispersy.on_incoming_packets = self.original_on_incoming_packets
        self._dispersy._send = self.original_send
Ejemplo n.º 11
0
        def filter_non_bootstrap_nodes():
            for candidate, packet in packets:
                cid = packet[2:22]

                if not cid in self._communities and candidate.sock_addr[0] in self._non_autoload:
                    if __debug__: dprint("drop a ", len(packet), " byte packet (received from non-autoload node) from ", candidate, level="warning", force=1)
                    self._statistics.drop("_convert_packets_into_batch:from bootstrap node for unloaded community", len(packet))
                    continue

                yield candidate, packet
Ejemplo n.º 12
0
 def create_last_1_sync(self, text):
     if __debug__: dprint(self._cid.encode("HEX"))
     assert isinstance(text, unicode)
     meta = self.get_meta_message(u"last-1-sync")
     message = meta.implement(meta.authentication.implement(self._my_member),
                              meta.distribution.implement(self.claim_global_time()),
                              meta.destination.implement(),
                              meta.payload.implement(text))
     self._dispersy.store_update_forward([message], True, True, True)
     return message
Ejemplo n.º 13
0
    def __init__(self, cid, master_public_key):
        super(SimpleDispersyTestCommunity, self).__init__(cid, master_public_key)
        if __debug__: dprint(self._cid.encode("HEX"))

        self._status = get_status_holder("dispersy-simple-dispersy-test")
        self._status.add_reporter(TUDelftReporter(REPORTER_NAME, 300, self._my_member.public_key))
        self._status.create_and_add_event("__init__^" + self._cid.encode("HEX"), ["full-sync", "last-1-sync"])
        self._status.create_and_add_event("info^" + self._cid.encode("HEX"), [self._dispersy.info()])
        self._status.report_now()
        self._dispersy.callback.register(self._periodically_info, delay=60.0)
Ejemplo n.º 14
0
    def dispersy_cleanup_community(self, message):
        if __debug__: dprint(self._cid.encode("HEX"))
        self._status.create_and_add_event("dispersy_cleanup_community^" + self._cid.encode("HEX") , [{"is_soft_kill":message.payload.is_soft_kill, "is_hard_kill":message.payload.is_hard_kill}])
        self._status.report_now()
        self._status.get_reporter(REPORTER_NAME).stop()
        self._status.remove_reporter(REPORTER_NAME)
        self._status.add_reporter(NullReporter("Periodically remove all events", 60))

        # will reclassify to SoftKilledCommunity or HardKilledCommunity
        return super(SimpleDispersyTestCommunity, self).dispersy_cleanup_community(message)
Ejemplo n.º 15
0
    def set_offline(self):
        """ Replace on_incoming_packets and _sends functions of
        dispersy with dummies

        This simulates a node going offline, since it's not able to
        send or receive any messages
        """
        def dummy_function(*params):
            return
        dprint("Going offline")
        self._dispersy.on_incoming_packets = dummy_function
        self._dispersy._send = dummy_function
Ejemplo n.º 16
0
 def _get_channel_community(self, cid):
     assert isinstance(cid, str)
     assert len(cid) == 20
     
     try:
         return self._dispersy.get_community(cid, True)
     except KeyError:
         if self.auto_join_channel:
             if __debug__: dprint("join channel community ", cid.encode("HEX"))
             return ChannelCommunity.join_community(Member.get_instance(cid, public_key_available=False), self._my_member, self.integrate_with_tribler)
         else:
             if __debug__: dprint("join preview community ", cid.encode("HEX"))
             return PreviewChannelCommunity.join_community(Member.get_instance(cid, public_key_available=False), self._my_member, self.integrate_with_tribler)
Ejemplo n.º 17
0
    def __init__(self, rawserver, dispersy, port, ip="0.0.0.0"):
        while True:
            try:
                self.socket = rawserver.create_udpsocket(port, ip)
                if __debug__: dprint("Dispersy listening at ", port, force=True)
            except socket.error:
                port += 1
                continue
            break

        self.rawserver = rawserver
        self.rawserver.start_listening_udp(self.socket, self)
        self.dispersy = dispersy
        self.sendqueue = []
Ejemplo n.º 18
0
    def on_signature_response(self, message, request, retry):
        """ Handle a newly created double signed message or a timeout while signing

        When request for signing times out I just return. When I receive the signature for the
        message that I created then I send it to myself and then store and forward it to others of
        the community
        """
        if __debug__: dprint(message)

        if message:
            assert message.name == u"barter-record"
            assert message.authentication.is_signed

            if __debug__: dprint(message)

            # store, update, and forward to the community
            self._dispersy.store_update_forward([message], True, True, self.barter_forward_record_on_creation)
            log("dispersy.log", "created-barter-record") # TODO: maybe move to barter.log

        elif retry < 5:
            # signature timeout
            # retry
            if __debug__:
                log("barter.log", "barter-community-signature-request-timeout", retry=retry)
                dprint("Signature request timeout. Retry!")
                self.create_dispersy_signature_request(request, self.on_signature_response, (request, retry + 1))

        else:
            # close the transfer
            if __debug__:
                log("barter.log", "barter-community-signature-request-timeout", retry=retry)
                dprint("Signature request timeout")
Ejemplo n.º 19
0
    def allow_signature_request(self, message):
        """ Decide whether to reply or not to a signature request

        Currently I always sign for testing proposes
        """
        assert message.name == u"barter-record"
        assert not message.authentication.is_signed
        if __debug__: dprint(message)

        is_signed, other_member = message.authentication.signed_members[0]
        if other_member == self._my_member:
            if __debug__: dprint("refuse signature: we should be the first signer", level="warning")
            return False

        if not is_signed:
            if __debug__: dprint("refuse signature: the other member did not sign it", level="warning")
            return False

        my_member = message.authentication.members[1]
        if not my_member == self._my_member:
            if __debug__: dprint("refuse signature: we should be the second signer")
            return False

        # todo: decide if we want to use add our signature to this # record
        if True:
            # recalculate the limits of what we want to upload to member.authentication.members[0]
            return True

        # we will not add our signature
        return False
Ejemplo n.º 20
0
    def create_message_batch(self):
        """
        We will create 1000 last-1-sync messages.
        """
        meta = self._community.get_meta_message(u"full-sync")
        total = 1000
        for i in xrange(1, total + 1):
            # create a new full-sync message every hour
            count, = self._dispersy_database.execute(u"SELECT COUNT(1) FROM sync WHERE community = ? AND user = ? AND name = ?",
                                                     (self._community.database_id, self._community.my_member.database_id, meta.database_id)).next()
            dprint("there are ", count, " ", meta.name, " messages in our database")
            self._community.create_full_sync(u"full-sync; why:batch; start:%f; at:%f; nr:%d" % (self._start_time, time(), count + 1))
            yield 0.1

        yield 1.0
Ejemplo n.º 21
0
    def on_barter_record(self, messages):
        """ I handle received barter records

        I create or update a row in my database
        """
        if __debug__: dprint("storing ", len(messages), " records")
        with self._database as execute:
            for message in messages:
                if __debug__: log("dispersy.log", "handled-barter-record") # TODO: maybe move to barter.log
                # check if there is already a record about this pair
                try:
                    first_member, second_member, global_time = \
                                  execute(u"SELECT first_member, second_member, global_time FROM \
                                  record WHERE (first_member = ? AND second_member = ?) OR \
                                  (first_member = ? AND second_member = ?)",
                                          (message.authentication.members[0].database_id,
                                           message.authentication.members[1].database_id,
                                           message.authentication.members[1].database_id,
                                           message.authentication.members[0].database_id)).next()
                except StopIteration:
                    global_time = -1
                    first_member = message.authentication.members[0].database_id
                    second_member = message.authentication.members[1].database_id

                if global_time >= message.distribution.global_time:
                    # ignore the message
                    if __debug__:
                        dprint("Ignoring older message")
                else:
                    self._database.execute(u"INSERT OR REPLACE INTO \
                    record(community, global_time, first_member, second_member, \
                    upload_first_member, upload_second_member) \
                    VALUES(?, ?, ?, ?, ?, ?)",
                                           (self._database_id,
                                            message.distribution.global_time,
                                            first_member,
                                            second_member,
                                            message.payload.first_upload,
                                            message.payload.second_upload))
                    if __debug__:
                        peer1_id = self._peer_ids.get(message.authentication.members[0].public_key, -1)
                        peer2_id = self._peer_ids.get(message.authentication.members[1].public_key, -1)
                        peer1_upload = message.payload.first_upload
                        peer2_upload = message.payload.second_upload
                        if peer1_id > peer2_id:
                            peer1_id, peer2_id = peer2_id, peer1_id
                            peer1_upload, peer2_upload = peer2_upload, peer1_upload
                        log("barter.log", "barter-record", first=peer1_id, second=peer2_id, first_upload=peer1_upload, second_upload=peer2_upload)
Ejemplo n.º 22
0
    def __init__(self, cid, master_public_key):
        super(GossipLearningCommunity, self).__init__(cid, master_public_key, INITIALDELAY)
        if __debug__: dprint('gossiplearningcommunity' + self._cid.encode("HEX"))

        # Stats
        self._msg_count = 0

        # They should be loaded from a database.

        # x and y are stored only locally
        self._x = None
        self._y = None

        # Initial model
        self._model = AdalinePerceptronModel()
        self._model2 = LogisticRegressionModel()
Ejemplo n.º 23
0
    def on_votecast(self, messages):
        if self.integrate_with_tribler:
            for message in messages:
                if __debug__: dprint(message)
                dispersy_id = message.packet_id
                
                authentication_member = message.authentication.member
                if authentication_member == self._my_member:
                    peer_id = None
                else:
                    peer_id = self._peer_db.addOrGetPeerID(authentication_member.public_key)

                community = self._get_channel_community(message.payload.cid)
                self._votecast_db.on_vote_from_dispersy(community._channel_id, peer_id, dispersy_id, message.payload.vote, message.payload.timestamp)
                
                if DEBUG:
                    print >> sys.stderr, "AllChannelCommunity: got votecast message"
Ejemplo n.º 24
0
 def sync(self):
     """
     Perform a few sync cycles, if we end up creating new identity messages this will increase
     the chance that they will not clash.
     """
     sync_meta = self._community.get_meta_message(u"dispersy-sync")
     wait = 10
     for i in xrange(1, wait + 1):
         messages = [sync_meta.implement(sync_meta.authentication.implement(self._community.my_member),
                                         sync_meta.distribution.implement(self._community.global_time),
                                         sync_meta.destination.implement(),
                                         sync_meta.payload.implement(time_low, time_high, bloom_filter))
                     for time_low, time_high, bloom_filter
                     in self._community.dispersy_sync_bloom_filters]
         self._dispersy.store_update_forward(messages, False, False, True)
         dprint("syncing. ", i, "/", wait, "...")
         yield 1.0
     yield 1.0
Ejemplo n.º 25
0
    def setup(self):
        """
        We either need to join the community or semi-create one.
        """
        # we will use the below member identifier to create messages for our test.  the private key
        # can be found on disk, but will not be submitted to SVN for obvious reasons
        assert "hardcoded_member" in self._kargs, ("give --script-args hardcoded_member=A|B|C", self._kargs)
        assert self._kargs["hardcoded_member"] in SimpleDispersyTestCommunity.hardcoded_member_public_keys, "give --script-args hardcoded_member=A|B|C"

        member_name = self._kargs["hardcoded_member"]
        hardcoded_public_key = SimpleDispersyTestCommunity.hardcoded_member_public_keys[member_name]
        hardcoded_mid = sha1(hardcoded_public_key).digest()

        try:
            dprint("load_hardcoded_community")
            self._community = SimpleDispersyTestCommunity.load_hardcoded_community()

        except ValueError, e:
            dprint("failed to load; joining instead [", e, "]")
            # COPIED FROM dispersy/community.py:create_community.  IF ANYTHING CHANGES THERE IT
            # NEEDS TO BE REFLECTED HERE ASWELL

            # obtain the hardcoded_private_key for my_member from disk
            pem = open(expanduser("~/simpledispersytest_ec_private_key_%s" % member_name), "r").read()
            ec = ec_from_private_pem(pem)
            private_key = ec_to_private_bin(ec)
            my_member = MyMember(hardcoded_public_key, private_key)

            # obtain the master_private_key for the master_member from disk
            pem = open(expanduser("~/simpledispersytest_ec_master_private_key"), "r").read()
            ec = ec_from_private_pem(pem)
            master_private_key = ec_to_private_bin(ec)

            # insert entries in the dispersy database to join the community
            with self._dispersy_database as database:
                database.execute(u"INSERT INTO community (user, classification, cid, public_key) VALUES(?, ?, ?, ?)", (my_member.database_id, SimpleDispersyTestCommunity.get_classification(), buffer(SimpleDispersyTestCommunity.hardcoded_cid), buffer(SimpleDispersyTestCommunity.hardcoded_master_public_key)))
                database_id = self._dispersy_database.last_insert_rowid
                database.execute(u"INSERT INTO user (mid, public_key) VALUES(?, ?)", (buffer(SimpleDispersyTestCommunity.hardcoded_cid), buffer(SimpleDispersyTestCommunity.hardcoded_master_public_key)))
                database.execute(u"INSERT INTO key (public_key, private_key) VALUES(?, ?)", (buffer(SimpleDispersyTestCommunity.hardcoded_master_public_key), buffer(master_private_key)))
                database.execute(u"INSERT INTO candidate (community, host, port, incoming_time, outgoing_time) SELECT ?, host, port, incoming_time, outgoing_time FROM candidate WHERE community = 0", (database_id,))

            self._community = SimpleDispersyTestCommunity.load_community(SimpleDispersyTestCommunity.hardcoded_cid, SimpleDispersyTestCommunity.hardcoded_master_public_key)
Ejemplo n.º 26
0
    def create_messages(self):
        """
        We will create a new last-1-sync message every 3 minutes and a new full-sync message every
        20 last-1-sync messages.
        """
        while True:
            # create a new full-sync message every hour
            meta = self._community.get_meta_message(u"full-sync")
            count, = self._dispersy_database.execute(u"SELECT COUNT(1) FROM sync WHERE community = ? AND user = ? AND name = ?",
                                                     (self._community.database_id, self._community.my_member.database_id, meta.database_id)).next()
            dprint("there are ", count, " ", meta.name, " messages in our database")
            self._community.create_full_sync(u"full-sync; why:periodic; start:%f; at:%f; nr:%d" % (self._start_time, time(), count + 1))

            # create a new last-1-sync message every 60 seconds
            for i in xrange(20):
                self._community.create_last_1_sync(u"last-1-sync; start:%f; at:%f" % (self._start_time, time()))

                yield 60.0 * 3

        yield 1.0
Ejemplo n.º 27
0
        def filter_non_bootstrap_nodes():
            for candidate, packet in packets:
                cid = packet[2:22]

                if not cid in self._communities and candidate.sock_addr[
                        0] in self._non_autoload:
                    if __debug__:
                        dprint(
                            "drop a ",
                            len(packet),
                            " byte packet (received from non-autoload node) from ",
                            candidate,
                            level="warning",
                            force=1)
                        self._statistics.drop(
                            "_convert_packets_into_batch:from bootstrap node for unloaded community",
                            len(packet))
                    continue

                yield candidate, packet
    def _unload_communities(self):
        def is_active(community, now):
            # check 1: does the community have any active candidates
            if community.update_strikes(now) < 3:
                return True

            # check 2: does the community have any cached messages waiting to be processed
            for meta in self._batch_cache.iterkeys():
                if meta.community == community:
                    return True

            # the community is inactive
            return False

        while True:
            yield 180.0
            now = time()
            inactive = [community for community in self._communities.itervalues() if not is_active(community, now)]
            dprint("cleaning ", len(inactive), "/", len(self._communities), " communities")
            for community in inactive:
                community.unload_community()
Ejemplo n.º 29
0
    def check_permissions(self):
        """
        One or more dispersy-authorize messages are required to allow my member to create the
        messages for the test.  If we can not obtain the authorize messages we will create them.
        """
        metas = [self._community.get_meta_message(u"full-sync"), self._community.get_meta_message(u"last-1-sync"), self._community.get_meta_message(u"dispersy-destroy-community")]
        sync_meta = self._community.get_meta_message(u"dispersy-sync")
        wait = 30
        for i in xrange(1, wait + 1):
            if self._community._timeline._check(self._community.my_member, self._community.global_time, [(meta, u"permit") for meta in metas]):
                dprint("my member is allowed to create messages")
                break


            messages = [sync_meta.implement(sync_meta.authentication.implement(self._community.my_member),
                                            sync_meta.distribution.implement(self._community.global_time),
                                            sync_meta.destination.implement(),
                                            sync_meta.payload.implement(time_low, time_high, bloom_filter))
                        for time_low, time_high, bloom_filter
                        in self._community.dispersy_sync_bloom_filters]
            self._dispersy.store_update_forward(messages, False, False, True)
            dprint("syncing. ", i, "/", wait, "...")
            yield 1.0

        else:
            dprint("creating authorizations")
            permission_triplets = []
            for message in self._community.get_meta_messages():
                if not isinstance(message.resolution, PublicResolution):
                    for allowed in (u"authorize", u"revoke", u"permit"):
                        permission_triplets.append((self._community.my_member, message, allowed))
            if permission_triplets:
                self._community.create_dispersy_authorize(permission_triplets, sign_with_master=True)

        yield 1.0
Ejemplo n.º 30
0
    def check_my_member_identity(self):
        """
        The dispersy-identity message for my member may already exist.  If we can't find it, we can
        create it.
        """
        meta = self._community.get_meta_message(u"dispersy-identity")
        wait = 30
        for i in xrange(1, wait + 1):
            try:
                self._dispersy_database.execute(u"SELECT id FROM sync WHERE community = ? AND user = ? AND name = ?",
                                                (self._community.database_id, self._community.my_member.database_id, meta.database_id)).next()

            except StopIteration:
                pass

            else:
                dprint("dispersy-identity for my member is available")
                break

            dprint("requesting dispersy-identity for my member.  ", i, "/", wait, "...")
            addresses = [candidate.address for candidate in self._dispersy.yield_mixed_candidates(self._community, 10)]
            self._dispersy.create_identity_request(self._community, self._community.my_member.mid, addresses)
            yield 1.0

        else:
            dprint("creating first (or new) dispersy-identity for my member")
            self._community.create_dispersy_identity()

        yield 1.0
Ejemplo n.º 31
0
    def check_master_identity(self):
        """
        The dispersy-identity message for the master member may already exist.  If we can't find it,
        we can create it.
        """
        meta = self._community.get_meta_message(u"dispersy-identity")
        wait = 30
        for i in xrange(1, wait + 1):
            try:
                self._dispersy_database.execute(u"SELECT id FROM sync WHERE community = ? AND user = ? AND name = ?",
                                                (self._community.database_id, self._community.master_member.database_id, meta.database_id)).next()

            except StopIteration:
                pass

            else:
                dprint("dispersy-identity for the master member is available")
                break

            dprint("requesting dispersy-identity for the master member.  ", i, "/", wait, "...")
            addresses = [candidate.address for candidate in self._dispersy.yield_mixed_candidates(self._community, 10)]
            self._dispersy.create_identity_request(self._community, self._community.master_member.mid, addresses)
            yield 1.0

        else:
            dprint("creating first (or new) dispersy-identity for the master member")
            message = meta.implement(meta.authentication.implement(self._community.master_member),
                                     meta.distribution.implement(self._community.claim_global_time()),
                                     meta.destination.implement(),
                                     meta.payload.implement(("0.0.0.0", 0)))
            self._community.dispersy.store_update_forward([message], True, False, True)

        yield 1.0
Ejemplo n.º 32
0
    def setup(self):
        """
        We either need to join the community or semi-create one.
        """
        # we will use the below member identifier to create messages for our test.  the private key
        # can be found on disk, but will not be submitted to SVN for obvious reasons
        hardcoded_mid = "6bc9e18b346d9f879a90bbf51a495f3cb8b32957".decode("HEX")
        hardcoded_public_key = "3052301006072a8648ce3d020106052b8104001a033e000400d1f668eb495c76f6a899a46017e8c6ad57c90e85d84fa1524fe7a0e76f0183a3d290a63e80ae14060acc1d1e606f451811bab3f77c8dd318689695".decode("HEX")

        try:
            dprint("load_hardcoded_community")
            self._community = SimpleDispersyTestCommunity.load_hardcoded_community()

        except ValueError, e:
            dprint("failed to load; joining instead [", e, "]")
            # COPIED FROM dispersy/community.py:create_community.  IF ANYTHING CHANGES THERE IT
            # NEEDS TO BE REFLECTED HERE ASWELL

            # obtain the hardcoded_private_key for my_member from disk
            pem = open(expanduser("~/simpledispersytest_ec_private_key"), "r").read()
            ec = ec_from_private_pem(pem)
            private_key = ec_to_private_bin(ec)
            my_member = MyMember(hardcoded_public_key, private_key)

            # obtain the master_private_key for the master_member from disk
            pem = open(expanduser("~/simpledispersytest_ec_master_private_key"), "r").read()
            ec = ec_from_private_pem(pem)
            master_private_key = ec_to_private_bin(ec)

            # insert entries in the dispersy database to join the community
            with self._dispersy_database as execute:
                execute(u"INSERT INTO community (user, classification, cid, public_key) VALUES(?, ?, ?, ?)", (my_member.database_id, SimpleDispersyTestCommunity.get_classification(), buffer(SimpleDispersyTestCommunity.hardcoded_cid), buffer(SimpleDispersyTestCommunity.hardcoded_master_public_key)))
                database_id = self._dispersy_database.last_insert_rowid
                execute(u"INSERT INTO user (mid, public_key) VALUES(?, ?)", (buffer(SimpleDispersyTestCommunity.hardcoded_cid), buffer(SimpleDispersyTestCommunity.hardcoded_master_public_key)))
                execute(u"INSERT INTO key (public_key, private_key) VALUES(?, ?)", (buffer(SimpleDispersyTestCommunity.hardcoded_master_public_key), buffer(master_private_key)))
                execute(u"INSERT INTO candidate (community, host, port, incoming_time, outgoing_time) SELECT ?, host, port, incoming_time, outgoing_time FROM candidate WHERE community = 0", (database_id,))

            self._community = SimpleDispersyTestCommunity.load_community(SimpleDispersyTestCommunity.hardcoded_cid, SimpleDispersyTestCommunity.hardcoded_master_public_key)