Miner { public boolean mine(Block newBlock, byte[] difficulty) { BigInteger max = BigInteger.valueOf(2).pow(256); byte[] target = BigIntegers.asUnsignedByteArray(32, max.divide(new BigInteger(1, difficulty))); byte[] hash = HashUtil.sha3(newBlock.getEncodedWithoutNonce()); byte[] testNonce = new byte[32]; byte[] concat; while(ByteUtil.increment(testNonce)) { concat = Arrays.concatenate(hash, testNonce); byte[] result = HashUtil.sha3(concat); if(FastByteComparisons.compareTo(result, 0, 32, target, 0, 32) < 0) { newBlock.setNonce(testNonce); return true; } } return false; }  boolean mine(Block newBlock, byte[] difficulty);  }
@Test public void testMine() { boolean miningTestEnabled = false; if(miningTestEnabled) { Block block = createBlock(); assertEquals(rlpWithoutNonce, Hex.toHexString(block.getEncodedWithoutNonce())); System.out.println("Searching for nonce of following block: \n" + block.toString()); Miner miner = new Miner(); boolean mined = miner.mine(block, block.getDifficulty()); assertTrue(mined); boolean valid = block.validateNonce(); assertTrue(valid); String expectedHash = "ce7201f6cc5eb1a6f35c7dda8acda111647a0f8a5bf0518e46579b03e29fe14b"; assertEquals(expectedHash, Hex.toHexString(block.getHash())); String expectedNonce = "0000000000000000000000000000000000000000000000001f52ebb192c4ea97"; assertEquals(expectedNonce, Hex.toHexString(block.getNonce())); } }