@Test public void testProofOfWork() throws Exception { // This params accepts any difficulty target. NetworkParameters params = UnitTestParams.get(); Block block = params.getDefaultSerializer().makeBlock(blockBytes); block.setNonce(12346); try { block.verify(Block.BLOCK_HEIGHT_GENESIS, EnumSet.noneOf(Block.VerifyFlag.class)); fail(); } catch (VerificationException e) { // Expected. } // Blocks contain their own difficulty target. The BlockChain verification mechanism is what // stops real blocks // from containing artificially weak difficulties. block.setDifficultyTarget(Block.EASIEST_DIFFICULTY_TARGET); // Now it should pass. block.verify(Block.BLOCK_HEIGHT_GENESIS, EnumSet.noneOf(Block.VerifyFlag.class)); // Break the nonce again at the lower difficulty level so we can try solving for it. block.setNonce(1); try { block.verify(Block.BLOCK_HEIGHT_GENESIS, EnumSet.noneOf(Block.VerifyFlag.class)); fail(); } catch (VerificationException e) { // Expected to fail as the nonce is no longer correct. } // Should find an acceptable nonce. block.solve(); block.verify(Block.BLOCK_HEIGHT_GENESIS, EnumSet.noneOf(Block.VerifyFlag.class)); assertEquals(block.getNonce(), 2); }