def test_constructor_not_genesis_previous_hash(self): with self.assertRaises(Exception): Block(timestamp, transaction, index, genesis_previous_hash)
def test_constructor_transaction_list_with_null(self): with self.assertRaises(Exception): Block(timestamp, [transaction, None], index, previous_hash)
def test_constructor_incorrect_transaction(self): with self.assertRaises(Exception): Block(timestamp, incorrect_transaction, index, previous_hash)
def test_constructor_null_transaction(self): with self.assertRaises(Exception): Block(timestamp, None, index, previous_hash)
def test_constructor_incorrect_argument_transaction(self): for argument in vector_type_checks: with self.assertRaises(Exception): Block(timestamp, argument, index, previous_hash)
def test_constructor_correct(self): block = Block(timestamp, transaction, index, previous_hash) self.assertEqual(block.timestamp, timestamp) self.assertEqual(block.transactions, transaction) self.assertEqual(block.index, index) self.assertEqual(block.previousHash, previous_hash)
def test_constructor_correct_genesis_block(self): block = Block(timestamp, transaction, 0, genesis_previous_hash) self.assertEqual(block.timestamp, timestamp) self.assertEqual(block.transactions, transaction) self.assertEqual(block.index, 0) self.assertEqual(block.previousHash, genesis_previous_hash)
def test_integrity_modified_previous_hash(self): block = Block(timestamp, transaction, index, previous_hash) block.mine_block(2) block.previousHash = "wrongHash" self.assertNotEqual(block.currentHash, block.calculate_hash())
def test_integrity_modified_nonce(self): block = Block(timestamp, transaction, index, previous_hash) block.mine_block(2) block.nonce = -1 # Pray it doesnt it the correct nonce... self.assertNotEqual(block.currentHash, block.calculate_hash())
def test_integrity_modified_transaction_list_null(self): block = Block(timestamp, [transaction, Transaction(from_address, to_address, amount, node_identifier)], index, previous_hash) block.mine_block(2) block.transactions = None self.assertNotEqual(block.currentHash, block.calculate_hash())
def test_integrity_modified_timestamp(self): block = Block(timestamp, transaction, index, previous_hash) block.mine_block(2) block.timestamp = datetime.now() self.assertNotEqual(block.currentHash, block.calculate_hash())
def test_transaction_check_invalid_transaction_list(self): block = Block(timestamp, transaction, index, previous_hash) block.transactions = [transaction, incorrect_transaction] self.assertFalse(block.has_valid_transactions(block.transactions))
def test_transaction_check_correct_list(self): block = Block(timestamp, [transaction, transaction], index, previous_hash) self.assertTrue(block.has_valid_transactions(block.transactions))
def test_mining_correct_difficulty(self): block = Block(timestamp, transaction, index, previous_hash) block.mine_block(difficulty) self.assertEqual(block.currentHash[0:difficulty], "".join((["0"] * difficulty)))