def test_chain_validity_correct(self): blockchain = BlockchainInstance(miner_address, node_identifier, host, port) for transactions in range(blockchain.number_of_transactions * 2): blockchain.create_transaction(from_address, to_address, amount) self.assertTrue(blockchain.is_chain_valid())
def test_create_transaction(self): blockchain = BlockchainInstance(miner_address, node_identifier, host, port) transaction = blockchain.create_transaction(from_address, to_address, amount) self.assertEqual(transaction.fromAddress, from_address) self.assertEqual(transaction.toAddress, to_address) self.assertEqual(transaction.amount, amount) self.assertEqual(len(blockchain.pending_transactions), 1) self.assertEqual(blockchain.pending_transactions[0], transaction)
def test_block_creation_reward_transaction(self): blockchain = BlockchainInstance(miner_address, node_identifier, host, port) for transactions in range(blockchain.number_of_transactions): blockchain.create_transaction(from_address, to_address, amount) self.assertEqual(len(blockchain.pending_transactions), 1) transaction = blockchain.pending_transactions[0] self.assertEqual(transaction.toAddress, blockchain.miner_address) self.assertEqual(transaction.fromAddress, None) self.assertEqual(transaction.amount, blockchain.miningReward)
def test_block_creation_(self): blockchain = BlockchainInstance(miner_address, node_identifier, host, port) transaction_list = [] for transactions in range(blockchain.number_of_transactions): transaction_list.append(blockchain.create_transaction(from_address, to_address, amount)) self.assertEqual(len(blockchain.chain), 2) new_block = blockchain.chain[1] previous_block = blockchain.chain[0] self.assertEqual(new_block.index, previous_block.index+1) self.assertEqual(new_block.previousHash, previous_block.currentHash) self.assertEqual(new_block.transactions, transaction_list)
def test_gen_block_correct(self): gen_block = BlockchainInstance(miner_address, node_identifier, host, port).calculate_gen_block() self.assertEqual(gen_block.index, 0) self.assertEqual(gen_block.previousHash, "0") transaction = gen_block.transactions self.assertTrue(isinstance(transaction, Transaction)) self.assertEqual(transaction.amount, 0.0) self.assertEqual(transaction.fromAddress, None) self.assertEqual(transaction.toAddress, miner_address) self.assertEqual(transaction.node_id, node_identifier)
def test_address_balance_miner_address(self): blockchain = BlockchainInstance(miner_address, node_identifier, host, port) for transactions in range(blockchain.number_of_transactions * 2): blockchain.create_transaction(from_address, to_address, amount) self.assertEqual(blockchain.get_balance(from_address), -(amount * blockchain.number_of_transactions * 2 - 1)) self.assertEqual(blockchain.get_balance(to_address), amount * blockchain.number_of_transactions * 2 - 1) self.assertEqual(blockchain.get_balance(miner_address), blockchain.miningReward)
def test_chain_validity_invalid_block(self): blockchain = BlockchainInstance(miner_address, node_identifier, host, port) for transactions in range(blockchain.number_of_transactions * 2): blockchain.create_transaction(from_address, to_address, amount) blockchain.get_latest_block().index = -1 self.assertFalse(blockchain.is_chain_valid())
if not isinstance(to_address, str) or len(to_address) != 64: raise Exception("To address must be a string of length 64 chars") if not type(amount) is float: raise Exception("Transaction amount must be a float value") if amount < 0: raise Exception("Transaction amount must be greater or equal than 0") if __name__ == '__main__': parser = ArgumentParser(description='Parametrize the blockchain instance.') parser.add_argument('-p', '--port', default=5000, type=int, help='port to listen on') parser.add_argument('-ho', '--host', default='0.0.0.0', type=str, help='server host') parser.add_argument('-a', '--address', default='default_address', type=str, help='miner address') args = parser.parse_args() app.logger.info("Arguments:") app.logger.info(args) port = args.port host = args.host default_address = args.address hash_function = SHA256.new() hash_function.update(default_address.encode()) miner_address = hash_function.hexdigest() blockchain = BlockchainInstance(miner_address, node_identifier, host, port) app.run(host=host, port=port)
from blockchain.blockchain_data_structure import BlockchainInstance from Crypto.Hash import SHA256 hash_function = SHA256.new() hash_function.update("catarina-address".encode()) miner_address = hash_function.hexdigest() hash_function.update("address1".encode()) address1 = hash_function.hexdigest() hash_function.update("address2".encode()) address2 = hash_function.hexdigest() blockchain = BlockchainInstance(miner_address, "test", '0.0.0.0', 5000) print(blockchain) blockchain.create_transaction(address1, address2, 100.0) blockchain.create_transaction(address2, address1, 50.0) blockchain.create_transaction(address2, address1, 20.0) print("Balance of catarina-address is: ", blockchain.get_balance(miner_address)) print("Balance of address-1 is: ", blockchain.get_balance(address1)) print("Balance of address-2 is: ", blockchain.get_balance(address2)) blockchain.create_transaction(address1, address2, 20.0) blockchain.create_transaction(address2, address1, 50.0) print("Balance of catarina-address is: ", blockchain.get_balance(miner_address)) print("Balance of address-1 is: ", blockchain.get_balance(address1))
def test_create_transaction_no_amount(self): with self.assertRaises(Exception): BlockchainInstance(miner_address, node_identifier, host, port)\ .create_transaction(from_address, to_address, None)
def test_constructor_null_port(self): with self.assertRaises(Exception): BlockchainInstance(miner_address, node_identifier, host, None)
def test_constructor_correct(self): blockchain = BlockchainInstance(miner_address, node_identifier, host, port) self.assertEqual(blockchain.miner_address, miner_address) self.assertEqual(blockchain.node_identifier, node_identifier) self.assertEqual(blockchain.address, blockchain_address)
def test_peer_node_register(self): blockchain = BlockchainInstance(miner_address, node_identifier, host, port) blockchain.register_node(peer_node) self.assertEqual(len(blockchain.peer_nodes), 1) self.assertEqual(blockchain.peer_nodes.pop(), peer_node)