def test_transaction_verify_transaction_signature_too_large_signature(self): wallet = Wallet() transaction = Transaction(wallet.pubkey, "CityU", 1) transaction.add_signature("3379cc2f08e4cde5d24af02611c32693b18f406d4b58fbcd2bbd0acc67b1d") verify = transaction.verify_transaction_signature() self.assertFalse(verify)
def test_transaction_add_signature(self): wallet = Wallet() transaction = Transaction(wallet.pubkey, "CityU", 1) signature = wallet.sign_transaction(transaction) transaction.add_signature(signature) self.assertTrue(hasattr(transaction, "signature"))
def test_transaction_verify_transaction_signature(self): wallet = Wallet() transaction = Transaction(wallet.pubkey, "CityU", 1) signature = wallet.sign_transaction(transaction) transaction.add_signature(signature) verify = transaction.verify_transaction_signature() self.assertTrue(verify)
def test_block_initialize(self): wallet = Wallet() transaction = Transaction(wallet.pubkey, "CityU", 1) signature = wallet.sign_transaction(transaction) transaction.add_signature(signature) block = Block(0, [transaction], datetime.datetime.now().strftime("%m/%d/%Y, %H:%M:%S"), "0") self.assertIsInstance(block, Block)
def test_blockchain_add_new_transaction_not_enough_balance(self): wallet = Wallet() blockchain = Blockchain(wallet) transaction = Transaction(wallet.pubkey, "CityU", 100) signature = wallet.sign_transaction(transaction) transaction.add_signature(signature) blockchain.add_new_transaction(transaction) self.assertEqual(len(blockchain.unconfirmed_transactions), 0)
def test_block_compute_hash(self): wallet = Wallet() transaction = Transaction(wallet.pubkey, "CityU", 1) signature = wallet.sign_transaction(transaction) transaction.add_signature(signature) block = Block(0, [transaction.to_json()], datetime.datetime.now().strftime("%m/%d/%Y, %H:%M:%S"), "0") hash_val = block.compute_hash() self.assertIsInstance(hash_val, str) self.assertGreater(len(hash_val), 0)
def test_block_to_json(self): wallet = Wallet() transaction = Transaction(wallet.pubkey, "CityU", 1) signature = wallet.sign_transaction(transaction) transaction.add_signature(signature) block = Block(0, [transaction.to_json()], datetime.datetime.now().strftime("%m/%d/%Y, %H:%M:%S"), "0") content = block.to_json() self.assertIsInstance(content, str) self.assertGreater(len(content), 0)
def test_block_to_dict(self): wallet = Wallet() transaction = Transaction(wallet.pubkey, "CityU", 1) signature = wallet.sign_transaction(transaction) transaction.add_signature(signature) block = Block(0, [transaction.to_json()], datetime.datetime.now().strftime("%m/%d/%Y, %H:%M:%S"), "0") content = block.to_dict() self.assertIsInstance(content, dict) self.assertEqual(list(content.keys()), ["index", "timestamp", "previous_hash", "merkle_root", "nonce", "difficulty"])
def test_blockchain_add_block_wrong_previous_hash(self): wallet = Wallet() blockchain = Blockchain(wallet) transaction = Transaction(wallet.pubkey, "CityU", 1) signature = wallet.sign_transaction(transaction) transaction.add_signature(signature) block = Block(0, [transaction.to_json()], datetime.datetime.now().strftime("%m/%d/%Y, %H:%M:%S"), "Wrong") computed_hash = blockchain.proof_of_work(block) result = blockchain.add_block(block, computed_hash) self.assertFalse(result)
def test_blockchain_proof_of_work_bad_block(self): wallet = Wallet() blockchain = Blockchain(wallet) transaction = Transaction(wallet.pubkey, "CityU", 1) signature = wallet.sign_transaction(transaction) transaction.add_signature(signature) previous_hash = blockchain.last_block["hash"] block = Block(0, [transaction.to_json()], datetime.datetime.now().strftime("%m/%d/%Y, %H:%M:%S"), previous_hash) delattr(block, "transaction") self.assertRaises(AttributeError, blockchain.proof_of_work, block)
def test_blockchain_proof_of_work(self): wallet = Wallet() blockchain = Blockchain(wallet) transaction = Transaction(wallet.pubkey, "CityU", 1) signature = wallet.sign_transaction(transaction) transaction.add_signature(signature) previous_hash = blockchain.last_block["hash"] block = Block(0, [transaction.to_json()], datetime.datetime.now().strftime("%m/%d/%Y, %H:%M:%S"), previous_hash) computed_hash = blockchain.proof_of_work(block) self.assertGreater(len(computed_hash), 0)
def new_transaction(): '''This API adds transactions to the transaction pool''' values = request.form required = ['recipient_address', 'value'] # Check that the required fields are in the POST data if not all(k in values for k in required): return 'Missing values', 400 transaction = Transaction(myWallet.pubkey, values['recipient_address'], values['value']) transaction.add_signature(myWallet.sign_transaction(transaction)) transaction_result = blockchain.add_new_transaction(transaction) if transaction_result: response = {'message': 'Transaction will be added to Block'} return jsonify(response), 201 else: response = {'message': 'Invalid Transaction!'} return jsonify(response), 406