Example #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")
Example #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()
Example #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))
Example #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")
Example #5
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")
Example #6
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")
Example #7
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
Example #8
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