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_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_size(self): tester_node = Node("Tester") blockchain = Blockchain() self.assertEqual(1, blockchain.get_ledger_size()) for i in range(100): tester_node.write_data(data=str(i), chain=blockchain) self.assertEqual(101, blockchain.get_ledger_size())
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_blocks_by_range(self): tester_node = Node("Tester") blockchain = Blockchain() for i in range(100): tester_node.write_data(data=str(i), chain=blockchain) blocks = blockchain.get_blocks_by_range(5) data = [block.get_data() for block in blocks] self.assertEqual(['95', '96', '97', '98', '99'], data)
def test_verify_block(self): blockchain = Blockchain() # A block that was not mined on the chain cannot be verified: wrong_block = Block(data="Wrong block", user_addr="Bad person") correct_block = blockchain.mine_block(data="Correct block", node_addr="Good person") self.assertFalse(blockchain.verify_block(wrong_block)) self.assertTrue(blockchain.verify_block(correct_block))
def test_get_difficulty(self): tester_node = Node("Tester") blockchain = Blockchain(diff_threshold=10) self.assertEqual(1, blockchain.get_difficulty()) for i in range(105): tester_node.write_data(data=str(i), chain=blockchain, max_tries=15) # Expected failure if not all blocks could be mined: self.assertTrue(blockchain.get_difficulty() > 9)
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])
def test_read_data_by_range(self): tester_node = Node("Tester") blockchain = Blockchain(g_data="Test", node_addr=tester_node.get_node_addr()) for i in range(100): tester_node.write_data(data=str(i), chain=blockchain) output = tester_node.read_data_by_range(5, chain=blockchain) with self.assertRaises(Exception) as context: tester_node.read_data_by_range(range=102, chain=blockchain) self.assertTrue("exceeds size of ledger." in str(context.exception)) self.assertEqual(['95', '96', '97', '98', '99'], output)
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_read_data_by_meta(self): tester_node = Node("Tester") blockchain = Blockchain() for i in range(10): tester_node.write_data(data=str(i), chain=blockchain, meta_data="Meta " + str(i)) data = tester_node.read_data_by_meta(meta="Meta 9", chain=blockchain) self.assertEqual(["9"], data) with self.assertRaises(Exception) as context: tester_node.read_data_by_meta(meta="Not contained inside", chain=blockchain) self.assertTrue("Ledger does not contain" in str(context.exception))
def test_get_block_by_index(self): tester_node = Node("Tester") blockchain = Blockchain() for i in range(10): tester_node.write_data(data=str(i), chain=blockchain, meta_data="Meta " + str(i)) block = tester_node.get_block_by_index(index=7, chain=blockchain) self.assertEqual('6', block.get_data()) with self.assertRaises(Exception) as context: tester_node.get_block_by_index(index=22, chain=blockchain) self.assertTrue("Requested index exceeds size of ledger." in str( context.exception))
def test_get_blocks_by_meta(self): tester_node = Node("Tester") blockchain = Blockchain() for i in range(10): tester_node.write_data(data=str(i), chain=blockchain, meta_data="Meta " + str(i)) blocks = tester_node.get_blocks_by_meta(meta="Meta 2", chain=blockchain) with self.assertRaises(Exception) as context: tester_node.get_blocks_by_meta(meta="Not contained inside", chain=blockchain) self.assertTrue("Ledger does not contain" in str(context.exception)) self.assertEqual("2", blocks[0].get_data())
from swiftchain import Blockchain, Node # Create a Node with some user name. # The hash generated from this user name is used as the Node address: user = Node("Some user name to be hashed") # Create a blockchain with a difficulty threshold of 7, # meaning that the difficulty is increased every 7 blocks. chain = Blockchain(diff_threshold=7, g_data="Fiat Lux!", node_addr=user.get_node_addr()) # Mine 100 blocks on the above chain. # The amount of time this takes will vary depending on your system, # with some degree of luck thrown in. for i in range(100): user.write_data(data="Hello " + str(i), chain=chain) # Print the content of the last 5 blocks onto the screen: print(user.read_data_by_range(5, chain=chain)) # Output: ['Hello 95', 'Hello 96', 'Hello 97', 'Hello 98', 'Hello 99']
def test_get_redux_time(self): blockchain = Blockchain(redux_time=0.5) self.assertEqual(1800000, blockchain.get_redux_time())
def test_get_try_limit(self): blockchain = Blockchain(try_limit=10000) self.assertEqual(10000, blockchain.get_try_limit())
def test_get_diff_threshold(self): blockchain = Blockchain() self.assertEqual(100, blockchain.get_diff_threshold())
def test_set_difficulty(self): blockchain = Blockchain() blockchain.set_difficulty(100) self.assertEqual(100, blockchain.get_difficulty())
def test_set_diff_threshold(self): blockchain = Blockchain() blockchain.set_diff_threshold(diff_threshold=10) self.assertEqual(10, blockchain.get_diff_threshold())
def test_set_try_limit(self): blockchain = Blockchain() blockchain.set_try_limit(try_limit=10) self.assertEqual(10, blockchain.get_try_limit())
from swiftchain import Blockchain, Node # Create a node, which will mine blocks on the given chains: user = Node("Tester") # Create a blockchain with a difficulty threshold of 10, # meaning that the difficulty is raised every 10 blocks. first_chain = Blockchain(diff_threshold=10, g_data="First Blockchain", node_addr=user.get_node_addr()) # Create a blockchain with a difficulty threshold of 7, # meaning that the difficulty is raised every 7 blocks. second_chain = Blockchain(diff_threshold=7, g_data="Second Blockchain", node_addr=user.get_node_addr()) # Mine 100 blocks on the first chain and 80 blocks on the second chain: for i in range(100): if i < 80: user.write_data(data=str(i), chain=second_chain) user.write_data(data=str(i), chain=first_chain) print( f"Last Block ID of first chain (before consensus): {first_chain.get_last_block().get_block_id()}" ) print( f"Last Block ID of second chain (before consensus): {second_chain.get_last_block().get_block_id()}\n" ) print("Finding consensus...\n")