ChainSplitTest: use TestNet3Params, rather than UnitTestParams

This commit is contained in:
Andreas Schildbach 2022-08-01 11:57:17 +02:00
parent 1ae9ba3fcf
commit b0442721c8

View File

@ -21,7 +21,7 @@ import org.bitcoinj.base.Coin;
import org.bitcoinj.base.ScriptType;
import org.bitcoinj.base.Sha256Hash;
import org.bitcoinj.core.TransactionConfidence.ConfidenceType;
import org.bitcoinj.params.UnitTestParams;
import org.bitcoinj.params.TestNet3Params;
import org.bitcoinj.store.MemoryBlockStore;
import org.bitcoinj.testing.FakeTxBuilder;
import org.bitcoinj.utils.BriefLogFormatter;
@ -54,7 +54,7 @@ import static org.junit.Assert.fail;
public class ChainSplitTest {
private static final Logger log = LoggerFactory.getLogger(ChainSplitTest.class);
private static final NetworkParameters UNITTEST = UnitTestParams.get();
private static final NetworkParameters TESTNET = TestNet3Params.get();
private Wallet wallet;
private BlockChain chain;
private Address coinsTo;
@ -65,15 +65,15 @@ public class ChainSplitTest {
public void setUp() throws Exception {
BriefLogFormatter.init();
Utils.setMockClock(); // Use mock clock
Context.propagate(new Context(100, Coin.ZERO, false, false));
MemoryBlockStore blockStore = new MemoryBlockStore(UNITTEST);
wallet = Wallet.createDeterministic(UNITTEST, ScriptType.P2PKH);
Context.propagate(new Context(100, Coin.ZERO, false, true));
MemoryBlockStore blockStore = new MemoryBlockStore(TESTNET);
wallet = Wallet.createDeterministic(TESTNET, ScriptType.P2PKH);
ECKey key1 = wallet.freshReceiveKey();
ECKey key2 = wallet.freshReceiveKey();
chain = new BlockChain(UNITTEST, wallet, blockStore);
coinsTo = LegacyAddress.fromKey(UNITTEST, key1);
coinsTo2 = LegacyAddress.fromKey(UNITTEST, key2);
someOtherGuy = LegacyAddress.fromKey(UNITTEST, new ECKey());
chain = new BlockChain(TESTNET, wallet, blockStore);
coinsTo = LegacyAddress.fromKey(TESTNET, key1);
coinsTo2 = LegacyAddress.fromKey(TESTNET, key2);
someOtherGuy = LegacyAddress.fromKey(TESTNET, new ECKey());
}
@Test
@ -86,7 +86,7 @@ public class ChainSplitTest {
wallet.addChangeEventListener(wallet -> walletChanged.incrementAndGet());
// Start by building a couple of blocks on top of the genesis block.
Block b1 = UNITTEST.getGenesisBlock().createNextBlock(coinsTo);
Block b1 = TESTNET.getGenesisBlock().createNextBlock(coinsTo);
Block b2 = b1.createNextBlock(coinsTo);
assertTrue(chain.add(b1));
assertTrue(chain.add(b2));
@ -160,7 +160,7 @@ public class ChainSplitTest {
public void testForking2() throws Exception {
// Check that if the chain forks and new coins are received in the alternate chain our balance goes up
// after the re-org takes place.
Block b1 = UNITTEST.getGenesisBlock().createNextBlock(someOtherGuy);
Block b1 = TESTNET.getGenesisBlock().createNextBlock(someOtherGuy);
Block b2 = b1.createNextBlock(someOtherGuy);
assertTrue(chain.add(b1));
assertTrue(chain.add(b2));
@ -178,16 +178,16 @@ public class ChainSplitTest {
@Test
public void testForking3() throws Exception {
// Check that we can handle our own spends being rolled back by a fork.
Block b1 = UNITTEST.getGenesisBlock().createNextBlock(coinsTo);
Block b1 = TESTNET.getGenesisBlock().createNextBlock(coinsTo);
chain.add(b1);
assertEquals(FIFTY_COINS, wallet.getBalance());
Address dest = LegacyAddress.fromKey(UNITTEST, new ECKey());
Address dest = LegacyAddress.fromKey(TESTNET, new ECKey());
Transaction spend = wallet.createSend(dest, valueOf(10, 0));
wallet.commitTx(spend);
// Waiting for confirmation ... make it eligible for selection.
assertEquals(Coin.ZERO, wallet.getBalance());
spend.getConfidence().markBroadcastBy(new PeerAddress(UNITTEST, InetAddress.getByAddress(new byte[]{1, 2, 3, 4})));
spend.getConfidence().markBroadcastBy(new PeerAddress(UNITTEST, InetAddress.getByAddress(new byte[]{5,6,7,8})));
spend.getConfidence().markBroadcastBy(new PeerAddress(TESTNET, InetAddress.getByAddress(new byte[]{1, 2, 3, 4})));
spend.getConfidence().markBroadcastBy(new PeerAddress(TESTNET, InetAddress.getByAddress(new byte[]{5,6,7,8})));
assertEquals(ConfidenceType.PENDING, spend.getConfidence().getConfidenceType());
assertEquals(valueOf(40, 0), wallet.getBalance());
Block b2 = b1.createNextBlock(someOtherGuy);
@ -212,10 +212,10 @@ public class ChainSplitTest {
// Check that we can handle external spends on an inactive chain becoming active. An external spend is where
// we see a transaction that spends our own coins but we did not broadcast it ourselves. This happens when
// keys are being shared between wallets.
Block b1 = UNITTEST.getGenesisBlock().createNextBlock(coinsTo);
Block b1 = TESTNET.getGenesisBlock().createNextBlock(coinsTo);
chain.add(b1);
assertEquals(FIFTY_COINS, wallet.getBalance());
Address dest = LegacyAddress.fromKey(UNITTEST, new ECKey());
Address dest = LegacyAddress.fromKey(TESTNET, new ECKey());
Transaction spend = wallet.createSend(dest, FIFTY_COINS);
// We do NOT confirm the spend here. That means it's not considered to be pending because createSend is
// stateless. For our purposes it is as if some other program with our keys created the tx.
@ -242,13 +242,13 @@ public class ChainSplitTest {
@Test
public void testForking5() throws Exception {
// Test the standard case in which a block containing identical transactions appears on a side chain.
Block b1 = UNITTEST.getGenesisBlock().createNextBlock(coinsTo);
Block b1 = TESTNET.getGenesisBlock().createNextBlock(coinsTo);
chain.add(b1);
final Transaction t = b1.transactions.get(1);
assertEquals(FIFTY_COINS, wallet.getBalance());
// genesis -> b1
// -> b2
Block b2 = UNITTEST.getGenesisBlock().createNextBlock(coinsTo);
Block b2 = TESTNET.getGenesisBlock().createNextBlock(coinsTo);
Transaction b2coinbase = b2.transactions.get(0);
b2.transactions.clear();
b2.addTransaction(b2coinbase);
@ -266,17 +266,17 @@ public class ChainSplitTest {
}
private Block roundtrip(Block b2) throws ProtocolException {
return UNITTEST.getDefaultSerializer().makeBlock(b2.bitcoinSerialize());
return TESTNET.getDefaultSerializer().makeBlock(b2.bitcoinSerialize());
}
@Test
public void testForking6() throws Exception {
// Test the case in which a side chain block contains a tx, and then it appears in the best chain too.
Block b1 = UNITTEST.getGenesisBlock().createNextBlock(someOtherGuy);
Block b1 = TESTNET.getGenesisBlock().createNextBlock(someOtherGuy);
chain.add(b1);
// genesis -> b1
// -> b2
Block b2 = UNITTEST.getGenesisBlock().createNextBlock(coinsTo);
Block b2 = TESTNET.getGenesisBlock().createNextBlock(coinsTo);
chain.add(b2);
assertEquals(Coin.ZERO, wallet.getBalance());
// genesis -> b1 -> b3
@ -299,25 +299,25 @@ public class ChainSplitTest {
eventCalled[0] = true;
});
Block b1 = UNITTEST.getGenesisBlock().createNextBlock(coinsTo);
Block b1 = TESTNET.getGenesisBlock().createNextBlock(coinsTo);
chain.add(b1);
Transaction t1 = wallet.createSend(someOtherGuy, valueOf(10, 0));
Address yetAnotherGuy = LegacyAddress.fromKey(UNITTEST, new ECKey());
Address yetAnotherGuy = LegacyAddress.fromKey(TESTNET, new ECKey());
Transaction t2 = wallet.createSend(yetAnotherGuy, valueOf(20, 0));
wallet.commitTx(t1);
// Receive t1 as confirmed by the network.
Block b2 = b1.createNextBlock(LegacyAddress.fromKey(UNITTEST, new ECKey()));
Block b2 = b1.createNextBlock(LegacyAddress.fromKey(TESTNET, new ECKey()));
b2.addTransaction(t1);
b2.solve();
chain.add(roundtrip(b2));
// Now we make a double spend become active after a re-org.
Block b3 = b1.createNextBlock(LegacyAddress.fromKey(UNITTEST, new ECKey()));
Block b3 = b1.createNextBlock(LegacyAddress.fromKey(TESTNET, new ECKey()));
b3.addTransaction(t2);
b3.solve();
chain.add(roundtrip(b3)); // Side chain.
Block b4 = b3.createNextBlock(LegacyAddress.fromKey(UNITTEST, new ECKey()));
Block b4 = b3.createNextBlock(LegacyAddress.fromKey(TESTNET, new ECKey()));
chain.add(b4); // New best chain.
Threading.waitForUserCode();
// Should have seen a double spend.
@ -339,15 +339,15 @@ public class ChainSplitTest {
});
// Start with 50 coins.
Block b1 = UNITTEST.getGenesisBlock().createNextBlock(coinsTo);
Block b1 = TESTNET.getGenesisBlock().createNextBlock(coinsTo);
chain.add(b1);
Transaction t1 = checkNotNull(wallet.createSend(someOtherGuy, valueOf(10, 0)));
Address yetAnotherGuy = LegacyAddress.fromKey(UNITTEST, new ECKey());
Address yetAnotherGuy = LegacyAddress.fromKey(TESTNET, new ECKey());
Transaction t2 = checkNotNull(wallet.createSend(yetAnotherGuy, valueOf(20, 0)));
wallet.commitTx(t1);
// t1 is still pending ...
Block b2 = b1.createNextBlock(LegacyAddress.fromKey(UNITTEST, new ECKey()));
Block b2 = b1.createNextBlock(LegacyAddress.fromKey(TESTNET, new ECKey()));
chain.add(b2);
assertEquals(ZERO, wallet.getBalance());
assertEquals(valueOf(40, 0), wallet.getBalance(Wallet.BalanceType.ESTIMATED));
@ -355,11 +355,11 @@ public class ChainSplitTest {
// Now we make a double spend become active after a re-org.
// genesis -> b1 -> b2 [t1 pending]
// \-> b3 (t2) -> b4
Block b3 = b1.createNextBlock(LegacyAddress.fromKey(UNITTEST, new ECKey()));
Block b3 = b1.createNextBlock(LegacyAddress.fromKey(TESTNET, new ECKey()));
b3.addTransaction(t2);
b3.solve();
chain.add(roundtrip(b3)); // Side chain.
Block b4 = b3.createNextBlock(LegacyAddress.fromKey(UNITTEST, new ECKey()));
Block b4 = b3.createNextBlock(LegacyAddress.fromKey(TESTNET, new ECKey()));
chain.add(b4); // New best chain.
Threading.waitForUserCode();
// Should have seen a double spend against the pending pool.
@ -370,9 +370,9 @@ public class ChainSplitTest {
assertEquals(valueOf(30, 0), wallet.getBalance());
// ... and back to our own parallel universe.
Block b5 = b2.createNextBlock(LegacyAddress.fromKey(UNITTEST, new ECKey()));
Block b5 = b2.createNextBlock(LegacyAddress.fromKey(TESTNET, new ECKey()));
chain.add(b5);
Block b6 = b5.createNextBlock(LegacyAddress.fromKey(UNITTEST, new ECKey()));
Block b6 = b5.createNextBlock(LegacyAddress.fromKey(TESTNET, new ECKey()));
chain.add(b6);
// genesis -> b1 -> b2 -> b5 -> b6 [t1 still dead]
// \-> b3 [t2 resurrected and now pending] -> b4
@ -393,7 +393,7 @@ public class ChainSplitTest {
wallet.addCoinsReceivedEventListener((wallet, tx, prevBalance, newBalance) -> txns.add(tx));
// Start by building three blocks on top of the genesis block. All send to us.
Block b1 = UNITTEST.getGenesisBlock().createNextBlock(coinsTo);
Block b1 = TESTNET.getGenesisBlock().createNextBlock(coinsTo);
BigInteger work1 = b1.getWork();
Block b2 = b1.createNextBlock(coinsTo2);
BigInteger work2 = b2.getWork();
@ -508,14 +508,14 @@ public class ChainSplitTest {
// This covers issue 468.
// Receive some money to the wallet.
Transaction t1 = FakeTxBuilder.createFakeTx(UNITTEST, COIN, coinsTo);
final Block b1 = FakeTxBuilder.makeSolvedTestBlock(UNITTEST.getGenesisBlock(), t1);
Transaction t1 = FakeTxBuilder.createFakeTx(TESTNET, COIN, coinsTo);
final Block b1 = FakeTxBuilder.makeSolvedTestBlock(TESTNET.getGenesisBlock(), t1);
chain.add(b1);
// Send a couple of payments one after the other (so the second depends on the change output of the first).
Transaction t2 = checkNotNull(wallet.createSend(LegacyAddress.fromKey(UNITTEST, new ECKey()), CENT, true));
Transaction t2 = checkNotNull(wallet.createSend(LegacyAddress.fromKey(TESTNET, new ECKey()), CENT, true));
wallet.commitTx(t2);
Transaction t3 = checkNotNull(wallet.createSend(LegacyAddress.fromKey(UNITTEST, new ECKey()), CENT, true));
Transaction t3 = checkNotNull(wallet.createSend(LegacyAddress.fromKey(TESTNET, new ECKey()), CENT, true));
wallet.commitTx(t3);
chain.add(FakeTxBuilder.makeSolvedTestBlock(b1, t2, t3));
@ -544,7 +544,7 @@ public class ChainSplitTest {
final ArrayList<Transaction> txns = new ArrayList<>(3);
wallet.addCoinsReceivedEventListener(Threading.SAME_THREAD, (wallet, tx, prevBalance, newBalance) -> txns.add(tx));
Block b1 = UNITTEST.getGenesisBlock().createNextBlock(someOtherGuy);
Block b1 = TESTNET.getGenesisBlock().createNextBlock(someOtherGuy);
final ECKey coinsTo2 = wallet.freshReceiveKey();
Block b2 = b1.createNextBlockWithCoinbase(Block.BLOCK_VERSION_GENESIS, coinsTo2.getPubKey(), 2);
Block b3 = b2.createNextBlock(someOtherGuy);
@ -573,12 +573,12 @@ public class ChainSplitTest {
// Add blocks to b3 until we can spend the coinbase.
Block firstTip = b3;
for (int i = 0; i < UNITTEST.getSpendableCoinbaseDepth() - 2; i++) {
for (int i = 0; i < TESTNET.getSpendableCoinbaseDepth() - 2; i++) {
firstTip = firstTip.createNextBlock(someOtherGuy);
chain.add(firstTip);
}
// ... and spend.
Transaction fodder = wallet.createSend(LegacyAddress.fromKey(UNITTEST, new ECKey()), FIFTY_COINS);
Transaction fodder = wallet.createSend(LegacyAddress.fromKey(TESTNET, new ECKey()), FIFTY_COINS);
wallet.commitTx(fodder);
final AtomicBoolean fodderIsDead = new AtomicBoolean(false);
fodder.getConfidence().addEventListener(Threading.SAME_THREAD, (confidence, reason) -> fodderIsDead.set(confidence.getConfidenceType() == ConfidenceType.DEAD));
@ -601,7 +601,7 @@ public class ChainSplitTest {
assertTrue(chain.add(b6));
Block secondTip = b6;
for (int i = 0; i < UNITTEST.getSpendableCoinbaseDepth() - 2; i++) {
for (int i = 0; i < TESTNET.getSpendableCoinbaseDepth() - 2; i++) {
secondTip = secondTip.createNextBlock(someOtherGuy);
chain.add(secondTip);
}