public void TestForking1()
{
// Check that if the block chain forks, we end up using the right chain. Only tests inbound transactions
// (receiving coins). Checking that we understand reversed spends is in testForking2.
// TODO: Change this test to not use coinbase transactions as they are special (maturity rules).
var reorgHappened = false;
_wallet.Reorganized += (sender, e) => reorgHappened = true;
// Start by building a couple of blocks on top of the genesis block.
var b1 = _unitTestParams.GenesisBlock.CreateNextBlock(_coinbaseTo);
var b2 = b1.CreateNextBlock(_coinbaseTo);
Assert.IsTrue(_chain.Add(b1));
Assert.IsTrue(_chain.Add(b2));
Assert.IsFalse(reorgHappened);
// We got two blocks which generated 50 coins each, to us.
Assert.AreEqual("100.00", Utils.BitcoinValueToFriendlyString(_wallet.GetBalance()));
// We now have the following chain:
// genesis -> b1 -> b2
//
// so fork like this:
//
// genesis -> b1 -> b2
// \-> b3
//
// Nothing should happen at this point. We saw b2 first so it takes priority.
var b3 = b1.CreateNextBlock(_someOtherGuy);
Assert.IsTrue(_chain.Add(b3));
Assert.IsFalse(reorgHappened); // No re-org took place.
Assert.AreEqual("100.00", Utils.BitcoinValueToFriendlyString(_wallet.GetBalance()));
// Now we add another block to make the alternative chain longer.
Assert.IsTrue(_chain.Add(b3.CreateNextBlock(_someOtherGuy)));
Assert.IsTrue(reorgHappened); // Re-org took place.
reorgHappened = false;
//
// genesis -> b1 -> b2
// \-> b3 -> b4
//
// We lost some coins! b2 is no longer a part of the best chain so our available balance should drop to 50.
Assert.AreEqual("50.00", Utils.BitcoinValueToFriendlyString(_wallet.GetBalance()));
// ... and back to the first chain.
var b5 = b2.CreateNextBlock(_coinbaseTo);
var b6 = b5.CreateNextBlock(_coinbaseTo);
Assert.IsTrue(_chain.Add(b5));
Assert.IsTrue(_chain.Add(b6));
//
// genesis -> b1 -> b2 -> b5 -> b6
// \-> b3 -> b4
//
Assert.IsTrue(reorgHappened);
Assert.AreEqual("200.00", Utils.BitcoinValueToFriendlyString(_wallet.GetBalance()));
}