def test_get_last_block(self): blockchain = Blockchain() g_block = blockchain.get_last_block() test_block = None while not test_block: test_block = blockchain.mine_block(data="Test", node_addr="8367") self.assertEqual(test_block, blockchain.get_last_block()) self.assertNotEqual(g_block, blockchain.get_last_block())
def test_get_block(self): blockchain = Blockchain() g_block = blockchain.get_last_block() self.assertEqual(g_block, blockchain.get_block(g_block.get_block_hash()))
def test_get_blockchain_id(self): blockchain = Blockchain() g_data = blockchain.get_last_block() self.assertEqual(g_data.get_block_hash(), blockchain.get_blockchain_id())
def test_find_consensus(self): tester_node = Node("Tester") blockchain1 = Blockchain(diff_threshold=10, g_data="Fiat Lux!") blockchain2 = Blockchain(diff_threshold=7, g_data="Fiat Lux!") for i in range(100): tester_node.write_data(data=str(i), chain=blockchain1) for i in range(80): tester_node.write_data(data=str(i), chain=blockchain2) # Blockchain 2 has a higher difficulty threshold, thus it will have a # higher cumulative proof-of-work despite mining fewer blocks: self.assertFalse(blockchain2.find_consensus(blockchain1)) self.assertTrue(blockchain1.find_consensus(blockchain2)) self.assertEqual(blockchain1.get_last_block(), blockchain2.get_last_block())
def test_write_data(self): tester_node = Node("Tester") blockchain = Blockchain(g_data="Test", node_addr=tester_node.get_node_addr()) tester_node.write_data(data="Written successfully", chain=blockchain) last_block = blockchain.get_last_block() self.assertEqual("Written successfully", last_block.get_data())
def test_mine_block(self): blockchain = Blockchain() test_block = None while not test_block: test_block = blockchain.mine_block(data="Test", node_addr="8367") self.assertIsNotNone(test_block) self.assertTrue(blockchain.verify_block(test_block)) self.assertEqual(blockchain.get_last_block(), test_block)
def test_get_ledger(self): blockchain = Blockchain() g_block_hash = blockchain.get_last_block().get_block_hash() test_block = None while not test_block: test_block = blockchain.mine_block(data="Test", node_addr="8367") ledger = blockchain.get_ledger() self.assertIsNotNone(ledger[g_block_hash])