Miner { private void sleep() { try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } }  boolean mine(Block newBlock, byte[] difficulty); void stop();  }
@Test public void startMiningConsumer() throws Exception { SysPropConfig2.props.overrideParams(ConfigFactory.parseString( "peer.listen.port = 30336 \n" + "peer.privateKey = 3ec771c31cac8c0dba77a69e503765701d3c2bb62435888d4ffa38fed60c445c \n" + "peer.networkId = 555 \n" + "peer.active = [" + "{ url = \"enode: "{ url = \"enode: "] \n" + "sync.enabled = true \n" + "genesis = genesis-harder.json \n" + "database.dir = testDB-1 \n")); Ethereum ethereum2 = EthereumFactory.createEthereum(SysPropConfig2.props, SysPropConfig2.class); final CountDownLatch semaphore = new CountDownLatch(1); ethereum2.addListener(new EthereumListenerAdapter() { @Override public void onBlock(Block block, List<TransactionReceipt> receipts) { System.err.println("=== New block: " + blockInfo(block)); System.err.println(block); for (Transaction tx : block.getTransactionsList()) { } } @Override public void onPendingTransactionsReceived(List<Transaction> transactions) { System.err.println("=== Tx: " + transactions); } @Override public void onSyncDone(SyncState state) { semaphore.countDown(); System.err.println("=== Sync Done!"); } }); System.out.println("Waiting for sync..."); semaphore.await(); ECKey senderKey = ECKey.fromPrivate(Hex.decode("6ef8da380c27cea8fdf7448340ea99e8e2268fc2950d79ed47cbf6f85dc977ec")); byte[] receiverAddr = Hex.decode("5db10750e8caff27f906b41c71b3471057dd2004"); for (int i = ethereum2.getRepository().getNonce(senderKey.getAddress()).intValue(), j = 0; j < 200; i++, j++) { { Transaction tx = new Transaction(ByteUtil.intToBytesNoLeadZeroes(i), ByteUtil.longToBytesNoLeadZeroes(50_000_000_000L), ByteUtil.longToBytesNoLeadZeroes(0xfffff), receiverAddr, new byte[]{77}, new byte[0]); tx.sign(senderKey); System.out.println("=== Submitting tx: " + tx); ethereum2.submitTransaction(tx); submittedTxs.put(new ByteArrayWrapper(tx.getHash()), Pair.of(tx, System.currentTimeMillis())); } Thread.sleep(7000); } Thread.sleep(100000000L); }
@Test public void startMiningTest() throws FileNotFoundException, InterruptedException { SysPropConfig1.props.overrideParams(ConfigFactory.parseString( "peer.listen.port = 30335 \n" + "peer.privateKey = 6ef8da380c27cea8fdf7448340ea99e8e2268fc2950d79ed47cbf6f85dc977ec \n" + "peer.networkId = 555 \n" + "peer.active = [{ url = \"enode: "sync.enabled = true \n" + "genesis = genesis-harder.json \n" + "database.dir = testDB-2 \n" + "mine.extraDataHex = cccccccccccccccccccc \n" + "mine.cpuMineThreads = 2")); Ethereum ethereum1 = EthereumFactory.createEthereum(SysPropConfig1.props, SysPropConfig1.class); final CountDownLatch semaphore = new CountDownLatch(1); ethereum1.addListener(new EthereumListenerAdapter() { @Override public void onBlock(Block block, List<TransactionReceipt> receipts) { System.out.println("=== New block: " + blockInfo(block)); } @Override public void onSyncDone(SyncState state) { semaphore.countDown(); } }); System.out.println("=== Waiting for sync ..."); semaphore.await(600, TimeUnit.SECONDS); System.out.println("=== Sync done."); BlockMiner blockMiner = ethereum1.getBlockMiner(); blockMiner.addListener(new MinerListener() { @Override public void miningStarted() { System.out.println("=== MinerTest.miningStarted"); } @Override public void miningStopped() { System.out.println("=== MinerTest.miningStopped"); } @Override public void blockMiningStarted(Block block) { System.out.println("=== MinerTest.blockMiningStarted " + blockInfo(block)); } @Override public void blockMined(Block block) { System.out.println("=== MinerTest.blockMined " + blockInfo(block)); } @Override public void blockMiningCanceled(Block block) { System.out.println("=== MinerTest.blockMiningCanceled " + blockInfo(block)); } }); Ethash.fileCacheEnabled = true; blockMiner.setFullMining(true); blockMiner.startMining(); ECKey senderKey = ECKey.fromPrivate(Hex.decode("3ec771c31cac8c0dba77a69e503765701d3c2bb62435888d4ffa38fed60c445c")); byte[] receiverAddr = Hex.decode("31e2e1ed11951c7091dfba62cd4b7145e947219c"); for (int i = ethereum1.getRepository().getNonce(senderKey.getAddress()).intValue(), j = 0; j < 20000; i++, j++) { { Transaction tx = new Transaction(ByteUtil.intToBytesNoLeadZeroes(i), ByteUtil.longToBytesNoLeadZeroes(50_000_000_000L), ByteUtil.longToBytesNoLeadZeroes(0xfffff), receiverAddr, new byte[]{77}, new byte[0]); tx.sign(senderKey); System.out.println("=== Submitting tx: " + tx); ethereum1.submitTransaction(tx); submittedTxs.put(new ByteArrayWrapper(tx.getHash()), Pair.of(tx, System.currentTimeMillis())); } Thread.sleep(5000); } Thread.sleep(1000000000); ethereum1.close(); System.out.println("Passed."); }