def test_good_linked_message_validates(self): msg1 = Message("first").seal() msg2 = Message("second").link(msg1).seal() msg2.validate() # no exceptions raised # sanity check the hashes self.assertEqual(self.payload_hash(msg2), msg2.payload_hash) self.assertEqual(self.message_hash(msg2), msg2.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) self.assertIsNone(msg2.hash)
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").seal() msg2 = Message("second message").link(msg1).seal() self.assertEqual("first message", msg1.data) self.assertEqual("second message", msg2.data) self.assertEqual(msg2.prev_hash, msg1.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 test_sealing_linked_message_sets_message_hash_correctly(self): msg1 = Message("some data") msg1.seal() msg2 = Message("some more data") msg2.link(msg1) msg2.seal() self.assertNotEqual(msg1.hash, msg2.hash) self.assertEqual(self.message_hash(msg2), msg2.hash)
def get_messages(self, *args): L = [] for arg in args: L.append(Message(arg).seal()) for i, msg in enumerate(L): msg.link(L[i-1]) if i > 0 else None msg.seal() msg.validate() return L
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) self.assertIsNone(msg.hash)
def get_blocks(self, *args): L = [] for arg in args: b = Block() b.add_message(Message(arg)) L.append(b) for i, block in enumerate(L): block.link(L[i-1]) if i > 0 else None block.seal() return L
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) self.assertIsNone(msg.hash)
def test_tampering_invalidates_hashes(self): msg = Message("some data") msg.data = "more data" self.assertNotEqual(self.payload_hash(msg), msg.payload_hash) self.assertNotEqual(self.message_hash(msg), msg.hash) msg = Message("some data", "Alice", "Bob") msg.receiver = "Eve" self.assertNotEqual(self.payload_hash(msg), msg.payload_hash) self.assertNotEqual(self.message_hash(msg), msg.hash) msg = Message("some data", "Alice", "Bob") msg.sender = "Charlie" self.assertNotEqual(self.payload_hash(msg), msg.payload_hash) self.assertNotEqual(self.message_hash(msg), msg.hash) msg = Message("some data") msg.timestamp = time.time() + 100 # force different time self.assertNotEqual(self.payload_hash(msg), msg.payload_hash) self.assertNotEqual(self.message_hash(msg), msg.hash)
def __init__(self, game, position): super().__init__(game, position) self.image = pg.Surface((20, 20)) self.image.fill(WHITE) self.rect = self.image.get_rect() self.speed = randint(2, 3) self.max_force = 0.3 self.friction = 0.75 self.target = vec(0, 0) self.extent = vec(0, 0) self.theta = 0 self.id = random.random() self.b = Block() msg = str(self.id) self.b.add_message(Message(msg)) chain.add_block(self.b) print(self.id)
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])
def test_good_unlinked_message_validates(self): msg = Message("some data", "Alice", "Bob").seal() msg.validate() # no exceptions raised # 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.assertIsNone(msg.hash) msg.seal() self.assertEqual(self.message_hash(msg), msg.hash)
def test_tampering_invalidates_payload_hash(self): msg = Message("some data", "Alice", "Bob") msg.receiver = "Eve" self.assertNotEqual(self.payload_hash(msg), msg.payload_hash)
def test_message_tampering_implies_validation_exception(self): msg = Message("first").seal() msg.data = "changed" self.assertRaises(InvalidMessage, msg.validate)
from blockchain import Message, Block, Blockchain import pickle B1 = Block() B1.add_message(Message("This is the first message")) B1.add_message(Message("Second message", "Alice", "Bob")) B1.add_message(Message("Third message", "Bob", "Alice")) B1.seal() B2 = Block() B2.add_message(Message("Fourth message")) B2.add_message(Message("Fifth message", "Eve", "Steve")) B2.seal() B3 = Block() B3.add_message(Message("Sixth message")) B3.add_message( Message("Seventh Son of a Seventh Son is Iron Maiden's best album", "Me", "Everyone")) B3.seal() B4 = Block() B4.add_message(Message("Eighth message", "Bob", "Charlie")) B4.add_message(Message("Ninth message", "Charlie", "Daniels")) B4.add_message(Message("Tenth message", "Charlie", "Brown")) chain = Blockchain() chain.add_block(B1) chain.add_block(B2) chain.add_block(B3) chain.add_block(B4)
def get_block(self, msg): B = Block() B.add_message(Message(msg)) return B
from blockchain import Message, Block, Blockchain import pickle B1 = Block() B1.add_message(Message("This is the 1st message")) B1.add_message(Message("2nd message", "Alice", "Bob")) B1.add_message(Message("3rd message", "Bob", "Alice")) B1.seal() B2 = Block() B2.add_message(Message("4th message")) B2.add_message(Message("5th message", "Eve", "Steve")) B2.seal() B3 = Block() B3.add_message(Message("6th message")) B3.add_message(Message("7th message", "Me", "Everyone")) B3.seal() chain = Blockchain() chain.add_block(B1) chain.add_block(B2) chain.add_block(B3) print("Validating blockchain...") chain.validate() print("Serializing...") pickle.dump(chain, open('chain.p', 'wb')) print("Deserializing and validating...")