BlockChain extends AbstractBlockChain { @Override public boolean add(FilteredBlock block) throws VerificationException, PrunedException { boolean success = super.add(block); if (success) { trackFilteredTransactions(block.getTransactionCount()); } return success; } BlockChain(Context context, Wallet wallet, BlockStore blockStore);  BlockChain(NetworkParameters params, Wallet wallet, BlockStore blockStore);  BlockChain(Context context, BlockStore blockStore);  BlockChain(NetworkParameters params, BlockStore blockStore);  BlockChain(Context params, List<? extends Wallet> wallets, BlockStore blockStore);  BlockChain(NetworkParameters params, List<? extends Wallet> wallets, BlockStore blockStore); @Override boolean add(FilteredBlock block);  }
@Test public void testBasicChaining() throws Exception { ListenableFuture<StoredBlock> future = testNetChain.getHeightFuture(2); Block b1 = getBlock1(); assertTrue(testNetChain.add(b1)); assertFalse(future.isDone()); Block b2 = getBlock2(); BigInteger n = b2.getNonce(); try { b2.setNonce(BigInteger.valueOf(12345)); testNetChain.add(b2); fail(); } catch (VerificationException e) { b2.setNonce(n); } assertTrue(testNetChain.add(b2)); assertTrue(future.isDone()); assertEquals(2, future.get().getHeight()); }
@Test public void badDifficulty() throws Exception { assertTrue(testNetChain.add(getBlock1())); Block b2 = getBlock2(); assertTrue(testNetChain.add(b2)); Block bad = new Block(TESTNET, Block.BLOCK_VERSION_GENESIS); bad.setMerkleRoot(Sha256Hash.wrap("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); bad.setNonce(BigInteger.valueOf(140548933)); bad.setTime(1279242649); bad.setPrevBlockHash(b2.getHash()); bad.setDifficultyTarget(Block.EASIEST_DIFFICULTY_TARGET); try { testNetChain.add(bad); fail(); } catch (VerificationException e) { assertTrue(e.getMessage(), e.getCause().getMessage().contains("Difficulty target is bad")); } BigInteger oldVal = TESTNET.getMaxTarget(); TESTNET.setMaxTarget(new BigInteger("00ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16)); try { testNetChain.add(bad); fail(); } catch (VerificationException e) { assertTrue(e.getMessage(), e.getCause().getMessage().contains("Unexpected change in difficulty")); } TESTNET.setMaxTarget(oldVal); }
@Test public void coinbaseTransactionAvailability() throws Exception { Wallet wallet2 = new Wallet(UNITTEST); ECKey receiveKey = wallet2.freshReceiveKey(); int height = 1; chain.addWallet(wallet2); Address addressToSendTo = LegacyAddress.fromKey(UNITTEST, receiveKey); Block b1 = UNITTEST.getGenesisBlock().createNextBlockWithCoinbase(Block.BLOCK_VERSION_GENESIS, wallet.currentReceiveKey().getPubKey(), height++); chain.add(b1); assertNotNull(coinbaseTransaction); assertEquals(Coin.ZERO, wallet.getBalance()); assertEquals(wallet.getBalance(BalanceType.ESTIMATED), ONE_COIN); assertTrue(!coinbaseTransaction.isMature()); try { wallet.createSend(addressToSendTo, valueOf(49, 0)); fail(); } catch (InsufficientMoneyException e) { } for (int i = 0; i < UNITTEST.getSpendableCoinbaseDepth() - 2; i++) { Transaction tx2 = createFakeTx(UNITTEST, COIN, LegacyAddress.fromKey(UNITTEST, new ECKey())); Block b2 = createFakeBlock(blockStore, height++, tx2).block; chain.add(b2); assertEquals(Coin.ZERO, wallet.getBalance()); assertEquals(wallet.getBalance(BalanceType.ESTIMATED), ONE_COIN); assertTrue(!coinbaseTransaction.isMature()); try { wallet.createSend(addressToSendTo, valueOf(49, 0)); fail(); } catch (InsufficientMoneyException e) { } } Transaction tx3 = createFakeTx(UNITTEST, COIN, LegacyAddress.fromKey(UNITTEST, new ECKey())); Block b3 = createFakeBlock(blockStore, height++, tx3).block; chain.add(b3); assertEquals(wallet.getBalance(), ONE_COIN); assertEquals(wallet.getBalance(BalanceType.ESTIMATED), ONE_COIN); assertTrue(coinbaseTransaction.isMature()); Transaction coinbaseSend2 = wallet.createSend(addressToSendTo, valueOf(49, 0)); assertNotNull(coinbaseSend2); wallet.commitTx(coinbaseSend2); assertEquals(wallet.getBalance(BalanceType.ESTIMATED), COIN); assertEquals(wallet.getBalance(BalanceType.AVAILABLE), ZERO); Block b4 = createFakeBlock(blockStore, height++, coinbaseSend2).block; chain.add(b4); assertEquals(wallet.getBalance(BalanceType.AVAILABLE), COIN); assertEquals(wallet2.getBalance(BalanceType.ESTIMATED), valueOf(49, 0)); assertEquals(wallet2.getBalance(BalanceType.AVAILABLE), valueOf(49, 0)); }
@Test public void badDifficulty() throws Exception { assertTrue(testNetChain.add(getBlock1())); Block b2 = getBlock2(); assertTrue(testNetChain.add(b2)); Block bad = new Block(testNet, 2 ); bad.setMerkleRoot(Sha256Hash.wrap("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); bad.setNonce(140548933); bad.setTime(1399712874L); bad.setPrevBlockHash(b2.getHash()); bad.setDifficultyTarget(Block.EASIEST_DIFFICULTY_TARGET); try { testNetChain.add(bad); fail(); } catch (VerificationException e) { assertTrue(e.getMessage(), e.getCause().getMessage().contains("Difficulty target is bad")); } BigInteger oldVal = testNet.getMaxTarget(); testNet.setMaxTarget(new BigInteger("00ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16)); try { testNetChain.add(bad); fail(); } catch (VerificationException e) { assertTrue(e.getMessage(), e.getCause().getMessage().contains("Unexpected change in difficulty")); } testNet.setMaxTarget(oldVal); }
@Test public void badDifficulty() throws Exception { assertTrue(testNetChain.add(getBlock1())); Block b2 = getBlock2(); assertTrue(testNetChain.add(b2)); Block bad = new Block(testNet, Block.BLOCK_VERSION_GENESIS); bad.setMerkleRoot(Sha256Hash.wrap("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")); bad.setNonce(140548933); bad.setTime(1279242649); bad.setPrevBlockHash(b2.getHash()); bad.setDifficultyTarget(Block.EASIEST_DIFFICULTY_TARGET); try { testNetChain.add(bad); fail(); } catch (VerificationException e) { assertTrue(e.getMessage(), e.getCause().getMessage().contains("Difficulty target is bad")); } BigInteger oldVal = testNet.getMaxTarget(); testNet.setMaxTarget(new BigInteger("00ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 16)); try { testNetChain.add(bad); } catch (VerificationException e) { assertTrue(e.getMessage(), e.getCause().getMessage().contains("Unexpected change in difficulty")); } testNet.setMaxTarget(oldVal); }