public void TestBadDifficulty()
{
Assert.IsTrue(_testNetChain.Add(GetBlock1()));
var b2 = GetBlock2();
Assert.IsTrue(_testNetChain.Add(b2));
var params2 = NetworkParameters.TestNet();
var bad = new Block(params2);
// Merkle root can be anything here, doesn't matter.
bad.MerkleRoot = new Sha256Hash(Hex.Decode("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"));
// Nonce was just some number that made the hash < difficulty limit set below, it can be anything.
bad.Nonce = 140548933;
bad.TimeSeconds = 1279242649;
bad.PrevBlockHash = b2.Hash;
// 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.DifficultyTarget = Block.EasiestDifficultyTarget;
try
{
_testNetChain.Add(bad);
// The difficulty target above should be rejected on the grounds of being easier than the networks
// allowable difficulty.
Assert.Fail();
}
catch (VerificationException e)
{
Assert.IsTrue(e.Message.IndexOf("Difficulty target is bad") >= 0, e.Message);
}
// Accept any level of difficulty now.
params2.ProofOfWorkLimit = new BigInteger("00ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16);
try
{
_testNetChain.Add(bad);
// We should not get here as the difficulty target should not be changing at this point.
Assert.Fail();
}
catch (VerificationException e)
{
Assert.IsTrue(e.Message.IndexOf("Unexpected change in difficulty") >= 0, e.Message);
}
// TODO: Test difficulty change is not out of range when a transition period becomes valid.
}