def test_invalid_transaction(block):
    # Leave tx unsigned
    new_transaction = VaccineTransaction("some vaccine", PUBLIC_KEY)
    block.add_transaction(new_transaction)
    block.sign(PRIVATE_KEY)
    block.update_hash()
    is_valid = validate_block(block, GENESIS)
    assert is_valid is False, "Invalid transaction"
def test_too_many_transactions(block):
    for index in range(CONFIG.block_size):
        new_transaction = VaccineTransaction(str(index), PUBLIC_KEY).sign(PRIVATE_KEY)
        block.add_transaction(new_transaction)
    block.sign(PRIVATE_KEY)
    block.update_hash()
    is_valid = validate_block(block, GENESIS)
    assert is_valid is False, "Too many transactions"
def test_too_few_transactions(block):
    block.transactions = []
    block.sign(PRIVATE_KEY)
    block.update_hash()
    is_valid = validate_block(block, GENESIS)
    # WONTFIX: Actually, a block should never be empty. However, we leave this
    # check disabled for demo purposes.
    # assert is_valid is False, "Too few transactions"
    assert is_valid is True
def test_signature_validity(block):
    block.sign(PRIVATE_KEY)
    block.timestamp = int(time()) - 10
    block.update_hash()
    is_valid = validate_block(block, GENESIS)
    assert is_valid is False, "Invalid signature"
def test_timestamp(block):
    block.timestamp = int(time()) + 50000000
    block.sign(PRIVATE_KEY)
    block.update_hash()
    is_valid = validate_block(block, GENESIS)
    assert is_valid is False, "Did not detect wrong version"
def test_version(block):
    block.version = "0"
    block.sign(PRIVATE_KEY)
    block.update_hash()
    is_valid = validate_block(block, GENESIS)
    assert is_valid is False, "Did not detect wrong version"
def test_previous_block_hash(block):
    block.previous_block = "some random hash"
    block.sign(PRIVATE_KEY)
    block.update_hash()
    is_valid = validate_block(block, GENESIS)
    assert is_valid is False, "Did not detect wrong prev. block hash"
def test_index(block):
    block.index = 2000
    block.sign(PRIVATE_KEY)
    block.update_hash()
    is_valid = validate_block(block, GENESIS)
    assert is_valid is False, "Did not detect wrong index"
def test_no_previous_block_passed(block):
    block.sign(PRIVATE_KEY)
    block.update_hash()
    is_valid = validate_block(block, None)
    assert is_valid is False, "Validated block without previous block"
def test_initial_block_is_valid(block):
    block.sign(PRIVATE_KEY)
    block.update_hash()
    is_valid = validate_block(block, GENESIS)
    assert is_valid is True, "Error in initial block"
def test_wrong_hash(block):
    block.sign(PRIVATE_KEY)
    block.hash = "4886f70d010101050"
    is_valid = validate_block(block, GENESIS)
    assert is_valid is False, "Invalid hash"
def test_duplicate_transactions(block):
    block.add_transaction(block.transactions[0])
    block.sign(PRIVATE_KEY)
    block.update_hash()
    is_valid = validate_block(block, GENESIS)
    assert is_valid is False, "Duplicate transactions"