def test_adding_messages_links_them_correctly(self): msg1 = Message("first") msg2 = Message("second") block = Block() block.add_message(msg1) block.add_message(msg2) self.assertEqual(msg2.prev_hash, msg1.hash)
def test_fluent(self): msg1 = Message("first message") msg2 = Message("second message").link(msg1) self.assertEqual("first message", msg1.data) self.assertEqual("second message", msg2.data) self.assertEqual(msg2.prev_hash, msg1.hash)
def test_sealing_linked_message_sets_message_hash_correctly(self): msg1 = Message("some data") msg2 = Message("some more data") msg2.link(msg1) self.assertNotEqual(msg1.hash, msg2.hash) self.assertEqual(self.message_hash(msg2), msg2.hash)
def test_constructor_appends_and_links_messages(self): block = Block(Message("first"), Message("second"), Message("third")) self.assertEqual(3, len(block.messages)) self.assertEqual(block.messages[0].hash, block.messages[1].prev_hash) self.assertEqual(block.messages[1].hash, block.messages[2].prev_hash) self.assertEqual("first", block.messages[0].data) self.assertEqual("second", block.messages[1].data) self.assertEqual("third", block.messages[2].data)
def get_messages(self, *args): content = [] last_msg = None for arg in args: msg = Message(arg) content.append(msg) if last_msg: msg.link(last_msg) last_msg = msg return content
def test_data_message_hashes_only_payload_on_create(self): data = "some data" msg = Message(data) self.assertEqual(msg.data, data) self.assertIsNone(msg.sender) self.assertIsNone(msg.receiver) self.assertIsNotNone(msg.timestamp) self.assertEqual(self.payload_hash(msg), msg.payload_hash)
def test_full_message_hashes_only_payload_on_create(self): data = "some data" alice = "Alice" bob = "Bob" msg = Message(data, alice, bob) self.assertEqual(msg.data, data) self.assertEqual(msg.sender, alice) self.assertEqual(msg.receiver, bob) self.assertIsNotNone(msg.timestamp) self.assertEqual(self.payload_hash(msg), msg.payload_hash)
def get_blocks(self, *args): content = [] last_block = None for arg in args: block = Block() block.add_message(Message(arg)) if last_block: block.link(last_block) last_block = block content.append(block) return content
def handle_client(client, client_address): # Takes client socket as argument. """Handles a single client connection.""" cars = random.choice( ["Toyota", "Honda", "Tesla", "Nissan", "Ford", "Dodge"]) license_plate = "".join(random.sample( string.ascii_letters.upper(), 3)) + "-" + "".join( map(str, random.sample(range(10), 3))) name = cars + " " + license_plate welcome = 'Car %s! If you ever want to quit, type {quit} to exit.' % name format_msg = 'This is the format of the messages: [STATE, LOCATION, SPEED, UUID, TIME, BRAKE]' client.send(bytes(welcome + "\n", "utf8")) client.send(bytes(format_msg + "\n", "utf8")) msg = "%s has joined the highway!" % name broadcast(bytes(msg, "utf8")) clients[client] = name while True: msg = client.recv(BUFSIZ) if msg != bytes("{quit}", "utf8"): print("SERVER RECEIVED: " + msg.decode("utf8")) B1 = Block() B1.add_message(Message(msg.decode("utf8"))) B1.seal() print("SERVER CREATED BLOCK for " + msg.decode("utf8")) chain.add_block(B1) print("ADDED TO BLOCKCHAIN for " + msg.decode("utf8")) msg_chain = {name + ": " + msg.decode("utf8"): chain} data = pickle.dumps(msg_chain, -1) broadcast(data) else: print("IN QUITTING CLIENT") client.close() del clients[client] broadcast(bytes("%s has left the highway." % name, "utf8")) break
def get_block(self, msg): block = Block() block.add_message(Message(msg)) return block
def test_good_linked_message_validates(self): msg1 = Message("first") msg2 = Message("second").link(msg1) # sanity check the hashes self.assertEqual(self.payload_hash(msg2), msg2.payload_hash) self.assertEqual(self.message_hash(msg2), msg2.hash)
def test_good_unlinked_message_validates(self): msg = Message("some data", "Alice", "Bob") # sanity check the hashes self.assertEqual(self.payload_hash(msg), msg.payload_hash) self.assertEqual(self.message_hash(msg), msg.hash)
def test_sealing_unlinked_message_sets_message_hash_correctly(self): msg = Message("some data") self.assertEqual(self.message_hash(msg), msg.hash)
def test_linking_populates_prev_hash_correctly(self): msg1 = Message("some data") msg2 = Message("some more data") msg2.link(msg1) self.assertEqual(msg2.prev_hash, msg1.hash)
def test_add_message_appends_and_links_message(self): msg = Message("test") block = Block() block.add_message(msg) self.assertEqual(1, len(block.messages)) self.assertEqual(msg, block.messages[0])