@Test
public void badDifficulty() throws Exception {

    assertTrue(testNetChain.add(getBlock1()));
    Block b2 = getBlock2();
    assertTrue(testNetChain.add(b2));
    Block bad = new Block(testNet);
    // Merkle root can be anything here, doesn't matter.
    bad.setMerkleRoot(new Sha256Hash("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
    // Nonce was just some number that made the hash < difficulty limit set below, it can be anything.
    bad.setNonce(140548933);
    bad.setTime(1279242649);
    bad.setPrevBlockHash(b2.getHash());
    // We're going to make this block so easy 50% of solutions will pass, and check it gets rejected for having a
    // bad difficulty target. Unfortunately the encoding mechanism means we cannot make one that accepts all
    // solutions.
    bad.setDifficultyTarget(Block.EASIEST_DIFFICULTY_TARGET);
    try {
        testNetChain.add(bad);
        // The difficulty target above should be rejected on the grounds of being easier than the networks
        // allowable difficulty.
        fail();
    } catch (VerificationException e) {
        assertTrue(e.getMessage(), e.getCause().getMessage().contains("Difficulty target is bad"));
    }
    // Accept any level of difficulty now.
    BigInteger oldVal = testNet.getProofOfWorkLimit();
    testNet.setProofOfWorkLimit(new BigInteger
            ("00ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16));
    try {
        testNetChain.add(bad);
        // We should not get here as the difficulty target should not be changing at this point.
        fail();
    } catch (VerificationException e) {
        assertTrue(e.getMessage(), e.getCause().getMessage().contains("Unexpected change in difficulty"));
    }
    testNet.setProofOfWorkLimit(oldVal);
    // TODO: Test difficulty change is not out of range when a transition period becomes valid.
}