Esempio n. 1
0
    def setUp(self):
        global seedval

        self.accounts = []
        self.feeds = []
        self.privkeys = []

        self.sock = socket.create_connection(("127.0.0.1", "8500"), 20)
        self.agent = pss.Agent("127.0.0.1", 8500, self.sock.fileno())
        self.sock.settimeout(10.0)
        self.bzz = pss.Bzz(self.agent)

        random.seed(pss.now_int() + seedval)

        # create 10 random private keys and create accounts with them
        for i in range(10):
            hx = ""
            for j in range(32):
                hx += "{:02x}".format(random.randint(0, 255))
            self.privkeys.append(hx)
            acc = pss.Account()
            acc.set_key(pss.clean_privkey(self.privkeys[i]).decode("hex"))
            self.accounts.append(acc)

        seedval += 1

        self.coll = FeedCollection("test")
Esempio n. 2
0
	def setUp(self):
		global seedval

		self.accounts = []
		self.feeds = []
		self.privkeys = []

		sys.stderr.write("setup\n")
		self.sock = socket.create_connection(("127.0.0.1", "8500"), 20)
		self.agent = pss.Agent("127.0.0.1", 8500, self.sock.fileno())
		self.bzz = pss.Bzz(self.agent)
	
		random.seed(pss.now_int()+seedval)

		for i in range(10):
			hx = ""
			for j in range(32):
				hx += "{:02x}".format(random.randint(0, 255))
			print "#" + str(i) + " is using " + hx
			self.privkeys.append(hx)
			acc = pss.Account()
			acc.set_key(pss.clean_privkey(self.privkeys[i]).decode("hex"))
			self.accounts.append(acc)
			sys.stderr.write("added random (seed " + str(seedval) + " key " + self.privkeys[i] + " account " + self.accounts[i].address.encode("hex") + " pubkey " + self.accounts[i].publickeybytes.encode("hex") + "\n")

		seedval += 1

		self.coll = FeedCollection()
Esempio n. 3
0
	def test_feed_collection_single_gap(self):
		feed = pss.Feed(self.agent, self.accounts[0], "one", True)

		tim = pss.now_int()
		timebytes = struct.pack(">I", tim)
		bogushsh = ""
		for i in range(32):
			bogushsh += "01"
		lasthsh = bogushsh.decode("hex")

		addr = feed.account.address
		acc = pss.Account()
		acc.set_address(addr)
		outfeed = pss.Feed(self.agent, acc, "one", True)
		print "set addr " +  str(i) + " " + addr.encode("hex")
		for j in range(3):
			hsh = self.bzz.add(lasthsh + timebytes  + chr(j) + hex((i*3)+j))
			lasthsh = hsh.decode("hex")
			feed.update(hsh)
	
		self.coll.add("foo", outfeed)
		# \todo this is not theoretically safe on a busy node, as things may change between, but in controlled test should be ok
		headhsh = feed.head()
		ridx = self.coll.gethead(self.bzz)
		self.coll.retrievals.pop(ridx)
		#ridx = self.coll.gethead(self.bzz)
		#msgs = self.coll.retrievals.pop(ridx)

		try:
			self.assertEqual(self.coll.feeds['foo']['orphans'][headhsh.decode("hex")], bogushsh.decode("hex"))
		except Exception as e:
			self.fail("dict key in test assert fail: " + str(e))
Esempio n. 4
0
	def test_feed_collection_sort(self):
		for i in range(2):
			self.feeds.append(pss.Feed(self.agent, self.accounts[i], "one", True))
		tim = pss.now_int()
		timebytes = struct.pack(">I", tim)
		outfeeds = []
		for i in range(len(self.feeds)):
			lasthsh = zerohsh.decode("hex")
			addr = self.feeds[i].account.address
			acc = pss.Account()
			acc.set_address(addr)
			outfeeds.append(pss.Feed(self.agent, acc, "one", True))
			print "set addr " +  str(i) + " " + addr.encode("hex")
			for j in range(3):
				hsh = self.bzz.add(lasthsh + timebytes  + chr(j) + hex((i*3)+j))
				lasthsh = hsh.decode("hex")
				self.feeds[i].update(hsh)

		self.coll.add("foo", outfeeds[0])
		self.coll.add("bar", outfeeds[1])
		self.coll.gethead(self.bzz)
		msgs = self.coll.get()

		# \todo more elegance, please
		if msgs[0].content == "0x0":
			self.assertEqual(msgs[1].content, "0x3")
			self.assertEqual(msgs[2].content, "0x1")
			self.assertEqual(msgs[3].content, "0x4")
		if msgs[0].content == "0x3":
			self.assertEqual(msgs[1].content, "0x0")
			self.assertEqual(msgs[2].content, "0x4")
			self.assertEqual(msgs[3].content, "0x1")
Esempio n. 5
0
	def test_feed_room(self):

		nicks = [self.accounts[0].address.encode("hex")]
		r = pss.Room(self.bzz, "abc", self.accounts[0])
		r.start("foo")
		for i in range(1, len(self.accounts)):
			addrhx = self.accounts[i].address.encode("hex")
			nicks.append(str(i))
			pubkeyhx = "04"+self.accounts[i].publickeybytes.encode("hex")
			p = Participant(nicks[i], pubkeyhx, addrhx, "04"+pubkey)
			r.add(nicks[i], p)
	
		# get the serialized representation of room	
		serializedroom = r.serialize()

		# save the room 
		savedhsh = r.save()
	
		# retrieve the pubkey from the saved room format	
		# and create account with retrieved public key
		# \todo more intuitive feed injection on load
		unserializedroom = json.loads(serializedroom)
		acc = pss.Account()
		cleanpub = clean_pubkey(unserializedroom['pubkey'])
		acc.set_public_key(cleanpub.decode("hex"))
		return

		# create feed with account from newly (re)created account
		recreatedownerfeed = pss.Feed(self.agent, acc, unserializedroom['name'], False)

		# instantiate room with feed recreated from saved state
		rr = pss.Room(self.bzz, recreatedownerfeed)
		rr.load(r.hsh, self.accounts[0])

		# check that for all"cleanpub: " +  in-feeds (read peer's updates) the feed user field is the address of the peer
		matchesleft = len(self.accounts)
		for f in rr.feedcollection_in.feeds.values():
			matched = False
			for a in self.accounts:

				if f['obj'].account.address == a.address:
					matched = True
					matchesleft -= 1
			if not matched:
				print "key '" + f['obj'].account.publickeybytes.encode("hex") + "'"
				self.fail("found unknown address " + f['obj'].account.address.encode("hex"))
		if matchesleft != 0:
			self.fail("have " + str(matchesleft) + " unmatched addresses")


		# for the outfeed, check that we are owner
		self.assertTrue(rr.feed_out.account.is_owner())
Esempio n. 6
0
    def test_room(self):
        acc = pss.Account()
        acc.set_public_key(self.pubkey[0])
        r = pss.Room(self.bzz, "root", acc)
        #r.start("bar", "foo")
        for i in range(len(self.pubkey)):
            r.participants[str(i)] = Participant(str(i), self.nodekey[i])
            r.participants[str(i)].set_public_key(self.pubkey[i])

        s = r.serialize()
        try:
            roomobj = json.loads(s)
        except ValueError as e:
            self.fail("json deserialize error: " + (str(e)))
Esempio n. 7
0
    def test_feed_collection_single_gap(self):

        feed = pss.Feed(self.bzz, self.accounts[0], "one")

        tim = pss.now_int()
        timebytes = struct.pack(">I", tim)

        # create a (most probably) non-existent hash
        # this will constitute a dead end on retrieval
        bogushsh = ""
        for i in range(32):
            bogushsh += "01"
        lasthsh = bogushsh.decode("hex")

        # set up similar as previous collection tests, but only one feed
        addr = feed.account.get_address()
        acc = pss.Account()
        acc.set_address(addr)
        outfeed = pss.Feed(self.bzz, acc, "one")
        for j in range(3):
            hsh = self.bzz.add(lasthsh + timebytes + chr(j) + hex(j))
            hshbytes = hsh.decode("hex")
            lasthsh = hshbytes
            feed.update(hshbytes)

        self.coll.add("foo", outfeed)

        # retrieve the updates in the collection
        # we also retrieve the hash of the latest update
        # as this is the key for the resulting dead end entry
        # \todo this is not theoretically safe on a busy node, as things may change between, but in controlled test should be ok
        headhsh = feed.head()
        (ridx, fails) = self.coll.gethead(self.bzz)
        self.assertEqual(len(fails), 0)
        upd = self.coll.retrievals.pop(ridx)

        try:
            self.assertEqual(self.coll.feeds['foo'].orphans[headhsh],
                             bogushsh.decode("hex"))
        except Exception as e:
            self.fail("dict key in test assert fail: " + str(e))

        # check that we still got all the updates that we could
        k = self.coll.feeds['foo'].obj.account.get_address()
        msgs = upd[k]
        self.assertEqual(msgs[timebytes + "\x00"].content, "0x0")
        self.assertEqual(msgs[timebytes + "\x01"].content, "0x1")
        self.assertEqual(msgs[timebytes + "\x02"].content, "0x2")
Esempio n. 8
0
    def test_feed_collection_sort(self):

        # same setup as previous test
        for i in range(2):
            self.feeds.append(pss.Feed(self.bzz, self.accounts[i], "one"))

        tim = pss.now_int()
        timebytes = struct.pack(">I", tim)

        outfeeds = []
        for i in range(len(self.feeds)):
            lasthsh = zerohsh
            addr = self.feeds[i].account.get_address()
            acc = pss.Account()
            acc.set_address(addr)
            outfeeds.append(pss.Feed(self.bzz, acc, "one"))
            for j in range(3):
                hsh = self.bzz.add(lasthsh + timebytes + chr(j) +
                                   hex((i * 3) + j))
                byteshsh = hsh.decode("hex")
                lasthsh = byteshsh
                self.feeds[i].update(byteshsh)

        self.coll.add("foo", outfeeds[0])
        self.coll.add("bar", outfeeds[1])
        (_, fails) = self.coll.gethead(self.bzz)
        self.assertEqual(len(fails), 0)

        msgs = self.coll.get()

        # depending on which feed comes first alphabetically
        # the messages should be ordered first by address
        # then by time+serial
        if msgs[0].content == "0x0":
            self.assertEqual(msgs[1].content, "0x3")
            self.assertEqual(msgs[2].content, "0x1")
            self.assertEqual(msgs[3].content, "0x4")
            self.assertEqual(msgs[4].content, "0x2")
            self.assertEqual(msgs[5].content, "0x5")
        if msgs[0].content == "0x3":
            self.assertEqual(msgs[1].content, "0x0")
            self.assertEqual(msgs[2].content, "0x4")
            self.assertEqual(msgs[3].content, "0x1")
            self.assertEqual(msgs[4].content, "0x5")
            self.assertEqual(msgs[5].content, "0x2")
Esempio n. 9
0
	def test_feed_collection_ok(self):
		for i in range(2):
			self.feeds.append(pss.Feed(self.agent, self.accounts[i], "one", True))
		tim = pss.now_int()
		timebytes = struct.pack(">I", tim)
		outfeeds = []
		for i in range(len(self.feeds)):
			lasthsh = zerohsh.decode("hex")
			addr = self.feeds[i].account.address
			acc = pss.Account()
			acc.set_address(addr)
			outfeeds.append(pss.Feed(self.agent, acc, "one", True))
			print "set addr " +  str(i) + " " + addr.encode("hex")
			for j in range(3):
				hsh = self.bzz.add(lasthsh + timebytes  + chr(j) + hex((i*3)+j))
				lasthsh = hsh.decode("hex")
				self.feeds[i].update(hsh)

		self.coll.add("foo", outfeeds[0])
		self.coll.add("bar", outfeeds[1])
		ridx = self.coll.gethead(self.bzz)
		msgs = self.coll.retrievals.pop(ridx)

		i = 0
		for n in ["foo", "bar"]:
			k = self.coll.feeds[n]['obj'].account.address
			print "getting addr " + k.encode("hex")
			v = msgs[k]
			timebyteshx = timebytes.encode("hex")	
			self.assertEqual(v[timebytes + "\x00"].content, "0x" + str(i*3))
			self.assertEqual(v[timebytes + "\x00"].user, k)
			self.assertEqual(v[timebytes + "\x01"].content, "0x" + str((i*3)+1))
			self.assertEqual(v[timebytes + "\x01"].user, k)
			self.assertEqual(v[timebytes + "\x02"].content, "0x" + str((i*3)+2))
			self.assertEqual(v[timebytes + "\x02"].user, k)	
			i += 1
Esempio n. 10
0
    def test_feed_collection_ok(self):

        for i in range(2):
            self.feeds.append(pss.Feed(self.bzz, self.accounts[i], "one"))

        tim = pss.now_int()
        timebytes = struct.pack(">I", tim)

        # holds feeds to read from
        outfeeds = []

        for i in range(len(self.feeds)):

            # first hash in linked list is zerohash
            lasthsh = zerohsh

            # create new feed with address only (no private key) for read
            addr = self.feeds[i].account.get_address()
            acc = pss.Account()
            acc.set_address(addr)
            outfeeds.append(pss.Feed(self.bzz, acc, "one"))

            #print "set addr " +  str(i) + " " + addr.encode("hex")

            # create 3 updates
            # an update is
            # * previous hash (zerohash if first update)
            # * time of update
            # * sequence of update within timestamp (one byte)
            # * data (in this case incrementing counter in hex)
            for j in range(3):
                hsh = self.bzz.add(lasthsh + timebytes + chr(j) +
                                   hex((i * 3) + j))
                hshbytes = hsh.decode("hex")
                lasthsh = hshbytes
                self.feeds[i].update(hshbytes)

        # put the read feeds into the collection
        self.coll.add("foo", outfeeds[0])
        self.coll.add("bar", outfeeds[1])

        # get the latest updates using the collection
        # it iterates all feeds, gets the latest update
        # and follows the linked hashes to retrieve all content
        (ridx, fails) = self.coll.gethead(self.bzz)
        self.assertEqual(len(fails), 0)

        # one element in retrievals array contains all updates
        # retrieved in one "gethead" call
        msgs = self.coll.retrievals.pop(ridx)

        # compare the results
        i = 0
        for n in ["foo", "bar"]:
            k = self.coll.feeds[n].obj.account.get_address()

            # feed updates are indexed on user address, then time and index
            v = msgs[k]
            self.assertEqual(v[timebytes + "\x00"].content, "0x" + str(i * 3))
            self.assertEqual(v[timebytes + "\x00"].user, k)
            self.assertEqual(v[timebytes + "\x01"].content,
                             "0x" + str((i * 3) + 1))
            self.assertEqual(v[timebytes + "\x01"].user, k)
            self.assertEqual(v[timebytes + "\x02"].content,
                             "0x" + str((i * 3) + 2))
            self.assertEqual(v[timebytes + "\x02"].user, k)
            i += 1