Exemplo n.º 1
0
def test_replace_chain():
    """Test replace a chain"""

    # Generate the genesis block
    block = Block.genesis_block(timestamp=datetime(2000, 1, 1), difficulty=4, mining=True)

    # Generate a old chain
    old_chain = BlockChain(block)

    # Generate longer chain
    blockchain = BlockChain(block)

    # Cannot be replace the chain
    assert old_chain.replace_chain(blockchain) is False

    # Add new block
    assert blockchain.add_candidate(None, timestamp=datetime(2000, 1, 2))
    assert blockchain.candidate_proof(4)

    # Replace the chain
    assert old_chain.replace_chain(blockchain)

    # Check the chain values
    assert old_chain.num_blocks == 2
    assert old_chain.candidate_block is None
Exemplo n.º 2
0
def test_validate_blockchain():
    """Test BlockChain validation"""

    block = Block.genesis_block(timestamp=datetime(2000, 1, 1), difficulty=4, mining=True)
    blockchain = BlockChain(block)

    # Add a new candidate
    assert blockchain.add_candidate(None, timestamp=datetime(2000, 1, 2))
    assert blockchain.candidate_proof(4)

    assert blockchain.is_valid

    # Alter the index
    blockchain.chain[1].index = 2

    assert blockchain.chain[1].is_valid is False
    assert blockchain.is_valid is False

    # Mining new id
    blockchain.chain[1].mining()

    assert blockchain.chain[1].is_valid
    assert blockchain.is_valid is False

    # Change the block
    blockchain.chain[1] = block

    assert blockchain.is_valid is False
Exemplo n.º 3
0
def test_add_candidates_prof():
    """Test to update the proof to a candidate"""

    block = Block.genesis_block(timestamp=datetime(2000, 1, 1), difficulty=4, mining=True)
    blockchain = BlockChain(block)

    # Add a new candidate
    assert blockchain.add_candidate(None, timestamp=datetime(2000, 1, 2))

    # Add proof to candidate
    assert blockchain.candidate_proof(3) is False
    assert blockchain.candidate_proof(4)

    # Add an empty candidate
    assert blockchain.add_candidate(None)

    # Add an invalid candidate (same date)
    assert blockchain.add_candidate(None, timestamp=datetime(2000, 1, 1)) is False

    assert blockchain.candidate_proof(3) is False
    assert blockchain.candidate_proof(4) is False