diff --git a/core/src/test/java/org/bitcoinj/core/BlockChainTest.java b/core/src/test/java/org/bitcoinj/core/BlockChainTest.java index 93322a02..3326a004 100644 --- a/core/src/test/java/org/bitcoinj/core/BlockChainTest.java +++ b/core/src/test/java/org/bitcoinj/core/BlockChainTest.java @@ -55,7 +55,7 @@ public class BlockChainTest { private BlockChain chain; private BlockStore blockStore; private Address coinbaseTo; - private NetworkParameters unitTestParams; + private static final NetworkParameters PARAMS = UnitTestParams.get(); private final StoredBlock[] block = new StoredBlock[1]; private Transaction coinbaseTransaction; @@ -67,7 +67,7 @@ public class BlockChainTest { private static final TweakableTestNet2Params testNet = new TweakableTestNet2Params(); private void resetBlockStore() { - blockStore = new MemoryBlockStore(unitTestParams); + blockStore = new MemoryBlockStore(PARAMS); } @Before @@ -76,9 +76,7 @@ public class BlockChainTest { Context testNetContext = new Context(testNet); testNetChain = new BlockChain(testNet, new Wallet(testNetContext), new MemoryBlockStore(testNet)); Wallet.SendRequest.DEFAULT_FEE_PER_KB = Coin.ZERO; - - unitTestParams = UnitTestParams.get(); - Context context = new Context(unitTestParams); + Context context = new Context(PARAMS); wallet = new Wallet(context) { @Override public void receiveFromBlock(Transaction tx, StoredBlock block, BlockChain.NewBlockType blockType, @@ -93,9 +91,9 @@ public class BlockChainTest { wallet.freshReceiveKey(); resetBlockStore(); - chain = new BlockChain(unitTestParams, wallet, blockStore); + chain = new BlockChain(PARAMS, wallet, blockStore); - coinbaseTo = wallet.currentReceiveKey().toAddress(unitTestParams); + coinbaseTo = wallet.currentReceiveKey().toAddress(PARAMS); } @After @@ -134,9 +132,9 @@ public class BlockChainTest { public void receiveCoins() throws Exception { int height = 1; // Quick check that we can actually receive coins. - Transaction tx1 = createFakeTx(unitTestParams, + Transaction tx1 = createFakeTx(PARAMS, COIN, - wallet.currentReceiveKey().toAddress(unitTestParams)); + wallet.currentReceiveKey().toAddress(PARAMS)); Block b1 = createFakeBlock(blockStore, height, tx1).block; chain.add(b1); assertTrue(wallet.getBalance().signum() > 0); @@ -144,7 +142,7 @@ public class BlockChainTest { @Test public void unconnectedBlocks() throws Exception { - Block b1 = unitTestParams.getGenesisBlock().createNextBlock(coinbaseTo); + Block b1 = PARAMS.getGenesisBlock().createNextBlock(coinbaseTo); Block b2 = b1.createNextBlock(coinbaseTo); Block b3 = b2.createNextBlock(coinbaseTo); // Connected. @@ -161,9 +159,9 @@ public class BlockChainTest { public void difficultyTransitions() throws Exception { // Add a bunch of blocks in a loop until we reach a difficulty transition point. The unit test params have an // artificially shortened period. - Block prev = unitTestParams.getGenesisBlock(); + Block prev = PARAMS.getGenesisBlock(); Utils.setMockClock(System.currentTimeMillis()/1000); - for (int height = 0; height < unitTestParams.getInterval() - 1; height++) { + for (int height = 0; height < PARAMS.getInterval() - 1; height++) { Block newBlock = prev.createNextBlock(coinbaseTo, 1, Utils.currentTimeSeconds(), height); assertTrue(chain.add(newBlock)); prev = newBlock; @@ -172,13 +170,13 @@ public class BlockChainTest { } // Now add another block that has no difficulty adjustment, it should be rejected. try { - chain.add(prev.createNextBlock(coinbaseTo, 1, Utils.currentTimeSeconds(), unitTestParams.getInterval())); + chain.add(prev.createNextBlock(coinbaseTo, 1, Utils.currentTimeSeconds(), PARAMS.getInterval())); fail(); } catch (VerificationException e) { } // Create a new block with the right difficulty target given our blistering speed relative to the huge amount // of time it's supposed to take (set in the unit test network parameters). - Block b = prev.createNextBlock(coinbaseTo, 1, Utils.currentTimeSeconds(), unitTestParams.getInterval() + 1); + Block b = prev.createNextBlock(coinbaseTo, 1, Utils.currentTimeSeconds(), PARAMS.getInterval() + 1); b.setDifficultyTarget(0x201fFFFFL); b.solve(); assertTrue(chain.add(b)); @@ -245,8 +243,8 @@ public class BlockChainTest { private void testDeprecatedBlockVersion(final long deprecatedVersion, final long newVersion) throws Exception { - final BlockStore versionBlockStore = new MemoryBlockStore(unitTestParams); - final BlockChain versionChain = new BlockChain(unitTestParams, versionBlockStore); + final BlockStore versionBlockStore = new MemoryBlockStore(PARAMS); + final BlockChain versionChain = new BlockChain(PARAMS, versionBlockStore); // Build a historical chain of version 3 blocks long timeSeconds = 1231006505; @@ -254,13 +252,13 @@ public class BlockChainTest { FakeTxBuilder.BlockPair chainHead = null; // Put in just enough v2 blocks to be a minority - for (height = 0; height < (unitTestParams.getMajorityWindow() - unitTestParams.getMajorityRejectBlockOutdated()); height++) { + for (height = 0; height < (PARAMS.getMajorityWindow() - PARAMS.getMajorityRejectBlockOutdated()); height++) { chainHead = FakeTxBuilder.createFakeBlock(versionBlockStore, deprecatedVersion, timeSeconds, height); versionChain.add(chainHead.block); timeSeconds += 60; } // Fill the rest of the window with v3 blocks - for (; height < unitTestParams.getMajorityWindow(); height++) { + for (; height < PARAMS.getMajorityWindow(); height++) { chainHead = FakeTxBuilder.createFakeBlock(versionBlockStore, newVersion, timeSeconds, height); versionChain.add(chainHead.block); timeSeconds += 60; @@ -279,7 +277,7 @@ public class BlockChainTest { @Test public void duplicates() throws Exception { // Adding a block twice should not have any effect, in particular it should not send the block to the wallet. - Block b1 = unitTestParams.getGenesisBlock().createNextBlock(coinbaseTo); + Block b1 = PARAMS.getGenesisBlock().createNextBlock(coinbaseTo); Block b2 = b1.createNextBlock(coinbaseTo); Block b3 = b2.createNextBlock(coinbaseTo); assertTrue(chain.add(b1)); @@ -299,13 +297,13 @@ public class BlockChainTest { public void intraBlockDependencies() throws Exception { // Covers issue 166 in which transactions that depend on each other inside a block were not always being // considered relevant. - Address somebodyElse = new ECKey().toAddress(unitTestParams); - Block b1 = unitTestParams.getGenesisBlock().createNextBlock(somebodyElse); + Address somebodyElse = new ECKey().toAddress(PARAMS); + Block b1 = PARAMS.getGenesisBlock().createNextBlock(somebodyElse); ECKey key = wallet.freshReceiveKey(); - Address addr = key.toAddress(unitTestParams); + Address addr = key.toAddress(PARAMS); // Create a tx that gives us some coins, and another that spends it to someone else in the same block. - Transaction t1 = FakeTxBuilder.createFakeTx(unitTestParams, COIN, addr); - Transaction t2 = new Transaction(unitTestParams); + Transaction t1 = FakeTxBuilder.createFakeTx(PARAMS, COIN, addr); + Transaction t2 = new Transaction(PARAMS); t2.addInput(t1.getOutputs().get(0)); t2.addOutput(valueOf(2, 0), somebodyElse); b1.addTransaction(t1); @@ -320,15 +318,15 @@ public class BlockChainTest { // Check that a coinbase transaction is only available to spend after NetworkParameters.getSpendableCoinbaseDepth() blocks. // Create a second wallet to receive the coinbase spend. - Wallet wallet2 = new Wallet(unitTestParams); + Wallet wallet2 = new Wallet(PARAMS); ECKey receiveKey = wallet2.freshReceiveKey(); int height = 1; chain.addWallet(wallet2); - Address addressToSendTo = receiveKey.toAddress(unitTestParams); + Address addressToSendTo = receiveKey.toAddress(PARAMS); // Create a block, sending the coinbase to the coinbaseTo address (which is in the wallet). - Block b1 = unitTestParams.getGenesisBlock().createNextBlockWithCoinbase(Block.BLOCK_VERSION_GENESIS, wallet.currentReceiveKey().getPubKey(), height++); + Block b1 = PARAMS.getGenesisBlock().createNextBlockWithCoinbase(Block.BLOCK_VERSION_GENESIS, wallet.currentReceiveKey().getPubKey(), height++); chain.add(b1); // Check a transaction has been received. @@ -347,10 +345,10 @@ public class BlockChainTest { } // Check that the coinbase is unavailable to spend for the next spendableCoinbaseDepth - 2 blocks. - for (int i = 0; i < unitTestParams.getSpendableCoinbaseDepth() - 2; i++) { + for (int i = 0; i < PARAMS.getSpendableCoinbaseDepth() - 2; i++) { // Non relevant tx - just for fake block creation. - Transaction tx2 = createFakeTx(unitTestParams, COIN, - new ECKey().toAddress(unitTestParams)); + Transaction tx2 = createFakeTx(PARAMS, COIN, + new ECKey().toAddress(PARAMS)); Block b2 = createFakeBlock(blockStore, height++, tx2).block; chain.add(b2); @@ -371,7 +369,7 @@ public class BlockChainTest { } // Give it one more block - should now be able to spend coinbase transaction. Non relevant tx. - Transaction tx3 = createFakeTx(unitTestParams, COIN, new ECKey().toAddress(unitTestParams)); + Transaction tx3 = createFakeTx(PARAMS, COIN, new ECKey().toAddress(PARAMS)); Block b3 = createFakeBlock(blockStore, height++, tx3).block; chain.add(b3); @@ -463,7 +461,7 @@ public class BlockChainTest { public void rollbackBlockStore() throws Exception { // This test simulates an issue on Android, that causes the VM to crash while receiving a block, so that the // block store is persisted but the wallet is not. - Block b1 = unitTestParams.getGenesisBlock().createNextBlock(coinbaseTo); + Block b1 = PARAMS.getGenesisBlock().createNextBlock(coinbaseTo); Block b2 = b1.createNextBlock(coinbaseTo); // Add block 1, no frills. assertTrue(chain.add(b1)); diff --git a/core/src/test/java/org/bitcoinj/core/ChainSplitTest.java b/core/src/test/java/org/bitcoinj/core/ChainSplitTest.java index 6ab9618c..6216d126 100644 --- a/core/src/test/java/org/bitcoinj/core/ChainSplitTest.java +++ b/core/src/test/java/org/bitcoinj/core/ChainSplitTest.java @@ -48,8 +48,7 @@ import static org.junit.Assert.*; public class ChainSplitTest { private static final Logger log = LoggerFactory.getLogger(ChainSplitTest.class); - - private NetworkParameters unitTestParams; + private static final NetworkParameters PARAMS = UnitTestParams.get(); private Wallet wallet; private BlockChain chain; private Address coinsTo; @@ -61,16 +60,15 @@ public class ChainSplitTest { BriefLogFormatter.init(); Utils.setMockClock(); // Use mock clock Wallet.SendRequest.DEFAULT_FEE_PER_KB = Coin.ZERO; - unitTestParams = UnitTestParams.get(); - Context context = new Context(unitTestParams); - MemoryBlockStore blockStore = new MemoryBlockStore(unitTestParams); + Context context = new Context(PARAMS); + MemoryBlockStore blockStore = new MemoryBlockStore(PARAMS); wallet = new Wallet(context); ECKey key1 = wallet.freshReceiveKey(); ECKey key2 = wallet.freshReceiveKey(); - chain = new BlockChain(unitTestParams, wallet, blockStore); - coinsTo = key1.toAddress(unitTestParams); - coinsTo2 = key2.toAddress(unitTestParams); - someOtherGuy = new ECKey().toAddress(unitTestParams); + chain = new BlockChain(PARAMS, wallet, blockStore); + coinsTo = key1.toAddress(PARAMS); + coinsTo2 = key2.toAddress(PARAMS); + someOtherGuy = new ECKey().toAddress(PARAMS); } @Test @@ -94,7 +92,7 @@ public class ChainSplitTest { }); // Start by building a couple of blocks on top of the genesis block. - Block b1 = unitTestParams.getGenesisBlock().createNextBlock(coinsTo); + Block b1 = PARAMS.getGenesisBlock().createNextBlock(coinsTo); Block b2 = b1.createNextBlock(coinsTo); assertTrue(chain.add(b1)); assertTrue(chain.add(b2)); @@ -168,7 +166,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 = unitTestParams.getGenesisBlock().createNextBlock(someOtherGuy); + Block b1 = PARAMS.getGenesisBlock().createNextBlock(someOtherGuy); Block b2 = b1.createNextBlock(someOtherGuy); assertTrue(chain.add(b1)); assertTrue(chain.add(b2)); @@ -186,16 +184,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 = unitTestParams.getGenesisBlock().createNextBlock(coinsTo); + Block b1 = PARAMS.getGenesisBlock().createNextBlock(coinsTo); chain.add(b1); assertEquals(FIFTY_COINS, wallet.getBalance()); - Address dest = new ECKey().toAddress(unitTestParams); + Address dest = new ECKey().toAddress(PARAMS); 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(unitTestParams, InetAddress.getByAddress(new byte[]{1, 2, 3, 4}))); - spend.getConfidence().markBroadcastBy(new PeerAddress(unitTestParams, InetAddress.getByAddress(new byte[]{5,6,7,8}))); + spend.getConfidence().markBroadcastBy(new PeerAddress(PARAMS, InetAddress.getByAddress(new byte[]{1, 2, 3, 4}))); + spend.getConfidence().markBroadcastBy(new PeerAddress(PARAMS, 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); @@ -220,10 +218,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 = unitTestParams.getGenesisBlock().createNextBlock(coinsTo); + Block b1 = PARAMS.getGenesisBlock().createNextBlock(coinsTo); chain.add(b1); assertEquals(FIFTY_COINS, wallet.getBalance()); - Address dest = new ECKey().toAddress(unitTestParams); + Address dest = new ECKey().toAddress(PARAMS); 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. @@ -250,13 +248,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 = unitTestParams.getGenesisBlock().createNextBlock(coinsTo); + Block b1 = PARAMS.getGenesisBlock().createNextBlock(coinsTo); chain.add(b1); final Transaction t = b1.transactions.get(1); assertEquals(FIFTY_COINS, wallet.getBalance()); // genesis -> b1 // -> b2 - Block b2 = unitTestParams.getGenesisBlock().createNextBlock(coinsTo); + Block b2 = PARAMS.getGenesisBlock().createNextBlock(coinsTo); Transaction b2coinbase = b2.transactions.get(0); b2.transactions.clear(); b2.addTransaction(b2coinbase); @@ -274,17 +272,17 @@ public class ChainSplitTest { } private Block roundtrip(Block b2) throws ProtocolException { - return unitTestParams.getDefaultSerializer().makeBlock(b2.bitcoinSerialize()); + return PARAMS.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 main chain too. - Block b1 = unitTestParams.getGenesisBlock().createNextBlock(someOtherGuy); + Block b1 = PARAMS.getGenesisBlock().createNextBlock(someOtherGuy); chain.add(b1); // genesis -> b1 // -> b2 - Block b2 = unitTestParams.getGenesisBlock().createNextBlock(coinsTo); + Block b2 = PARAMS.getGenesisBlock().createNextBlock(coinsTo); chain.add(b2); assertEquals(Coin.ZERO, wallet.getBalance()); // genesis -> b1 -> b3 @@ -310,25 +308,25 @@ public class ChainSplitTest { } }); - Block b1 = unitTestParams.getGenesisBlock().createNextBlock(coinsTo); + Block b1 = PARAMS.getGenesisBlock().createNextBlock(coinsTo); chain.add(b1); Transaction t1 = wallet.createSend(someOtherGuy, valueOf(10, 0)); - Address yetAnotherGuy = new ECKey().toAddress(unitTestParams); + Address yetAnotherGuy = new ECKey().toAddress(PARAMS); Transaction t2 = wallet.createSend(yetAnotherGuy, valueOf(20, 0)); wallet.commitTx(t1); // Receive t1 as confirmed by the network. - Block b2 = b1.createNextBlock(new ECKey().toAddress(unitTestParams)); + Block b2 = b1.createNextBlock(new ECKey().toAddress(PARAMS)); 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(new ECKey().toAddress(unitTestParams)); + Block b3 = b1.createNextBlock(new ECKey().toAddress(PARAMS)); b3.addTransaction(t2); b3.solve(); chain.add(roundtrip(b3)); // Side chain. - Block b4 = b3.createNextBlock(new ECKey().toAddress(unitTestParams)); + Block b4 = b3.createNextBlock(new ECKey().toAddress(PARAMS)); chain.add(b4); // New best chain. Threading.waitForUserCode(); // Should have seen a double spend. @@ -353,15 +351,15 @@ public class ChainSplitTest { }); // Start with 50 coins. - Block b1 = unitTestParams.getGenesisBlock().createNextBlock(coinsTo); + Block b1 = PARAMS.getGenesisBlock().createNextBlock(coinsTo); chain.add(b1); Transaction t1 = checkNotNull(wallet.createSend(someOtherGuy, valueOf(10, 0))); - Address yetAnotherGuy = new ECKey().toAddress(unitTestParams); + Address yetAnotherGuy = new ECKey().toAddress(PARAMS); Transaction t2 = checkNotNull(wallet.createSend(yetAnotherGuy, valueOf(20, 0))); wallet.commitTx(t1); // t1 is still pending ... - Block b2 = b1.createNextBlock(new ECKey().toAddress(unitTestParams)); + Block b2 = b1.createNextBlock(new ECKey().toAddress(PARAMS)); chain.add(b2); assertEquals(ZERO, wallet.getBalance()); assertEquals(valueOf(40, 0), wallet.getBalance(Wallet.BalanceType.ESTIMATED)); @@ -369,11 +367,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(new ECKey().toAddress(unitTestParams)); + Block b3 = b1.createNextBlock(new ECKey().toAddress(PARAMS)); b3.addTransaction(t2); b3.solve(); chain.add(roundtrip(b3)); // Side chain. - Block b4 = b3.createNextBlock(new ECKey().toAddress(unitTestParams)); + Block b4 = b3.createNextBlock(new ECKey().toAddress(PARAMS)); chain.add(b4); // New best chain. Threading.waitForUserCode(); // Should have seen a double spend against the pending pool. @@ -384,9 +382,9 @@ public class ChainSplitTest { assertEquals(valueOf(30, 0), wallet.getBalance()); // ... and back to our own parallel universe. - Block b5 = b2.createNextBlock(new ECKey().toAddress(unitTestParams)); + Block b5 = b2.createNextBlock(new ECKey().toAddress(PARAMS)); chain.add(b5); - Block b6 = b5.createNextBlock(new ECKey().toAddress(unitTestParams)); + Block b6 = b5.createNextBlock(new ECKey().toAddress(PARAMS)); chain.add(b6); // genesis -> b1 -> b2 -> b5 -> b6 [t1 still dead] // \-> b3 [t2 resurrected and now pending] -> b4 @@ -412,7 +410,7 @@ public class ChainSplitTest { }); // Start by building three blocks on top of the genesis block. All send to us. - Block b1 = unitTestParams.getGenesisBlock().createNextBlock(coinsTo); + Block b1 = PARAMS.getGenesisBlock().createNextBlock(coinsTo); BigInteger work1 = b1.getWork(); Block b2 = b1.createNextBlock(coinsTo2); BigInteger work2 = b2.getWork(); @@ -527,15 +525,15 @@ public class ChainSplitTest { // This covers issue 468. // Receive some money to the wallet. - Transaction t1 = FakeTxBuilder.createFakeTx(unitTestParams, COIN, coinsTo); - final Block b1 = FakeTxBuilder.makeSolvedTestBlock(unitTestParams.genesisBlock, t1); + Transaction t1 = FakeTxBuilder.createFakeTx(PARAMS, COIN, coinsTo); + final Block b1 = FakeTxBuilder.makeSolvedTestBlock(PARAMS.genesisBlock, t1); chain.add(b1); // Send a couple of payments one after the other (so the second depends on the change output of the first). wallet.allowSpendingUnconfirmedTransactions(); - Transaction t2 = checkNotNull(wallet.createSend(new ECKey().toAddress(unitTestParams), CENT)); + Transaction t2 = checkNotNull(wallet.createSend(new ECKey().toAddress(PARAMS), CENT)); wallet.commitTx(t2); - Transaction t3 = checkNotNull(wallet.createSend(new ECKey().toAddress(unitTestParams), CENT)); + Transaction t3 = checkNotNull(wallet.createSend(new ECKey().toAddress(PARAMS), CENT)); wallet.commitTx(t3); chain.add(FakeTxBuilder.makeSolvedTestBlock(b1, t2, t3)); @@ -569,7 +567,7 @@ public class ChainSplitTest { } }); - Block b1 = unitTestParams.getGenesisBlock().createNextBlock(someOtherGuy); + Block b1 = PARAMS.getGenesisBlock().createNextBlock(someOtherGuy); final ECKey coinsTo2 = wallet.freshReceiveKey(); Block b2 = b1.createNextBlockWithCoinbase(Block.BLOCK_VERSION_GENESIS, coinsTo2.getPubKey(), 2); Block b3 = b2.createNextBlock(someOtherGuy); @@ -598,12 +596,12 @@ public class ChainSplitTest { // Add blocks to b3 until we can spend the coinbase. Block firstTip = b3; - for (int i = 0; i < unitTestParams.getSpendableCoinbaseDepth() - 2; i++) { + for (int i = 0; i < PARAMS.getSpendableCoinbaseDepth() - 2; i++) { firstTip = firstTip.createNextBlock(someOtherGuy); chain.add(firstTip); } // ... and spend. - Transaction fodder = wallet.createSend(new ECKey().toAddress(unitTestParams), FIFTY_COINS); + Transaction fodder = wallet.createSend(new ECKey().toAddress(PARAMS), FIFTY_COINS); wallet.commitTx(fodder); final AtomicBoolean fodderIsDead = new AtomicBoolean(false); fodder.getConfidence().addEventListener(Threading.SAME_THREAD, new TransactionConfidence.Listener() { @@ -631,7 +629,7 @@ public class ChainSplitTest { assertTrue(chain.add(b6)); Block secondTip = b6; - for (int i = 0; i < unitTestParams.getSpendableCoinbaseDepth() - 2; i++) { + for (int i = 0; i < PARAMS.getSpendableCoinbaseDepth() - 2; i++) { secondTip = secondTip.createNextBlock(someOtherGuy); chain.add(secondTip); } diff --git a/core/src/test/java/org/bitcoinj/core/ParseByteCacheTest.java b/core/src/test/java/org/bitcoinj/core/ParseByteCacheTest.java index 841b8010..dfbacc10 100644 --- a/core/src/test/java/org/bitcoinj/core/ParseByteCacheTest.java +++ b/core/src/test/java/org/bitcoinj/core/ParseByteCacheTest.java @@ -64,7 +64,7 @@ public class ParseByteCacheTest { "c7 36 7a 7a 25 3b c1 13 52 23 ad b9 a4 68 bb 3a"); private BlockStore blockStore; - private NetworkParameters unitTestParams; + private static final NetworkParameters PARAMS = UnitTestParams.get(); private byte[] b1Bytes; private byte[] b1BytesWithHeader; @@ -76,35 +76,34 @@ public class ParseByteCacheTest { private byte[] tx2BytesWithHeader; private void resetBlockStore() { - blockStore = new MemoryBlockStore(unitTestParams); + blockStore = new MemoryBlockStore(PARAMS); } @Before public void setUp() throws Exception { - unitTestParams = UnitTestParams.get(); - Context context = new Context(unitTestParams); + Context context = new Context(PARAMS); Wallet wallet = new Wallet(context); wallet.freshReceiveKey(); resetBlockStore(); - Transaction tx1 = createFakeTx(unitTestParams, + Transaction tx1 = createFakeTx(PARAMS, valueOf(2, 0), - wallet.currentReceiveKey().toAddress(unitTestParams)); + wallet.currentReceiveKey().toAddress(PARAMS)); // add a second input so can test granularity of byte cache. - Transaction prevTx = new Transaction(unitTestParams); - TransactionOutput prevOut = new TransactionOutput(unitTestParams, prevTx, COIN, wallet.currentReceiveKey().toAddress(unitTestParams)); + Transaction prevTx = new Transaction(PARAMS); + TransactionOutput prevOut = new TransactionOutput(PARAMS, prevTx, COIN, wallet.currentReceiveKey().toAddress(PARAMS)); prevTx.addOutput(prevOut); // Connect it. tx1.addInput(prevOut); - Transaction tx2 = createFakeTx(unitTestParams, COIN, - new ECKey().toAddress(unitTestParams)); + Transaction tx2 = createFakeTx(PARAMS, COIN, + new ECKey().toAddress(PARAMS)); Block b1 = createFakeBlock(blockStore, BLOCK_HEIGHT_GENESIS, tx1, tx2).block; - MessageSerializer bs = unitTestParams.getDefaultSerializer(); + MessageSerializer bs = PARAMS.getDefaultSerializer(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); bs.serialize(tx1, bos); @@ -139,15 +138,15 @@ public class ParseByteCacheTest { @Test public void testTransactionsRetain() throws Exception { testTransaction(MainNetParams.get(), txMessage, false, true); - testTransaction(unitTestParams, tx1BytesWithHeader, false, true); - testTransaction(unitTestParams, tx2BytesWithHeader, false, true); + testTransaction(PARAMS, tx1BytesWithHeader, false, true); + testTransaction(PARAMS, tx2BytesWithHeader, false, true); } @Test public void testTransactionsNoRetain() throws Exception { testTransaction(MainNetParams.get(), txMessage, false, false); - testTransaction(unitTestParams, tx1BytesWithHeader, false, false); - testTransaction(unitTestParams, tx2BytesWithHeader, false, false); + testTransaction(PARAMS, tx1BytesWithHeader, false, false); + testTransaction(PARAMS, tx2BytesWithHeader, false, false); } @Test @@ -159,10 +158,10 @@ public class ParseByteCacheTest { public void testBlock(byte[] blockBytes, boolean isChild, boolean retain) throws Exception { // reference serializer to produce comparison serialization output after changes to // message structure. - MessageSerializer bsRef = unitTestParams.getSerializer(false); + MessageSerializer bsRef = PARAMS.getSerializer(false); ByteArrayOutputStream bos = new ByteArrayOutputStream(); - BitcoinSerializer bs = unitTestParams.getSerializer(retain); + BitcoinSerializer bs = PARAMS.getSerializer(retain); Block b1; Block bRef; b1 = (Block) bs.deserialize(ByteBuffer.wrap(blockBytes)); diff --git a/core/src/test/java/org/bitcoinj/utils/VersionTallyTest.java b/core/src/test/java/org/bitcoinj/utils/VersionTallyTest.java index a63b7049..45202210 100644 --- a/core/src/test/java/org/bitcoinj/utils/VersionTallyTest.java +++ b/core/src/test/java/org/bitcoinj/utils/VersionTallyTest.java @@ -30,7 +30,7 @@ import static org.junit.Assert.*; import org.junit.Before; public class VersionTallyTest { - private NetworkParameters unitTestParams; + private static final NetworkParameters PARAMS = UnitTestParams.get(); public VersionTallyTest() { } @@ -38,9 +38,7 @@ public class VersionTallyTest { @Before public void setUp() throws Exception { BriefLogFormatter.initVerbose(); - - unitTestParams = UnitTestParams.get(); - Context context = new Context(unitTestParams); + Context context = new Context(PARAMS); } /** @@ -48,12 +46,12 @@ public class VersionTallyTest { */ @Test public void testNullWhileEmpty() { - VersionTally instance = new VersionTally(unitTestParams); - for (int i = 0; i < unitTestParams.getMajorityWindow(); i++) { + VersionTally instance = new VersionTally(PARAMS); + for (int i = 0; i < PARAMS.getMajorityWindow(); i++) { assertNull(instance.getCountAtOrAbove(1)); instance.add(1); } - assertEquals(unitTestParams.getMajorityWindow(), instance.getCountAtOrAbove(1).intValue()); + assertEquals(PARAMS.getMajorityWindow(), instance.getCountAtOrAbove(1).intValue()); } /** @@ -61,8 +59,8 @@ public class VersionTallyTest { */ @Test public void testSize() { - VersionTally instance = new VersionTally(unitTestParams); - assertEquals(unitTestParams.getMajorityWindow(), instance.size()); + VersionTally instance = new VersionTally(PARAMS); + assertEquals(PARAMS.getMajorityWindow(), instance.size()); } /** @@ -70,46 +68,46 @@ public class VersionTallyTest { */ @Test public void testVersionCounts() { - VersionTally instance = new VersionTally(unitTestParams); + VersionTally instance = new VersionTally(PARAMS); // Fill the tally with 1s - for (int i = 0; i < unitTestParams.getMajorityWindow(); i++) { + for (int i = 0; i < PARAMS.getMajorityWindow(); i++) { assertNull(instance.getCountAtOrAbove(1)); instance.add(1); } - assertEquals(unitTestParams.getMajorityWindow(), instance.getCountAtOrAbove(1).intValue()); + assertEquals(PARAMS.getMajorityWindow(), instance.getCountAtOrAbove(1).intValue()); // Check the count updates as we replace with 2s - for (int i = 0; i < unitTestParams.getMajorityWindow(); i++) { + for (int i = 0; i < PARAMS.getMajorityWindow(); i++) { assertEquals(i, instance.getCountAtOrAbove(2).intValue()); instance.add(2); } // Inject a rogue 1 instance.add(1); - assertEquals(unitTestParams.getMajorityWindow() - 1, instance.getCountAtOrAbove(2).intValue()); + assertEquals(PARAMS.getMajorityWindow() - 1, instance.getCountAtOrAbove(2).intValue()); // Check we accept high values as well instance.add(10); - assertEquals(unitTestParams.getMajorityWindow() - 1, instance.getCountAtOrAbove(2).intValue()); + assertEquals(PARAMS.getMajorityWindow() - 1, instance.getCountAtOrAbove(2).intValue()); } @Test public void testInitialize() throws BlockStoreException { - final BlockStore blockStore = new MemoryBlockStore(unitTestParams); - final BlockChain chain = new BlockChain(unitTestParams, blockStore); + final BlockStore blockStore = new MemoryBlockStore(PARAMS); + final BlockChain chain = new BlockChain(PARAMS, blockStore); // Build a historical chain of version 2 blocks long timeSeconds = 1231006505; StoredBlock chainHead = null; - for (int height = 0; height < unitTestParams.getMajorityWindow(); height++) { + for (int height = 0; height < PARAMS.getMajorityWindow(); height++) { chainHead = FakeTxBuilder.createFakeBlock(blockStore, 2, timeSeconds, height).storedBlock; assertEquals(2, chainHead.getHeader().getVersion()); timeSeconds += 60; } - VersionTally instance = new VersionTally(unitTestParams); + VersionTally instance = new VersionTally(PARAMS); instance.initialize(blockStore, chainHead); - assertEquals(unitTestParams.getMajorityWindow(), instance.getCountAtOrAbove(2).intValue()); + assertEquals(PARAMS.getMajorityWindow(), instance.getCountAtOrAbove(2).intValue()); } }