Fixed an issue in the POW engine

This commit is contained in:
Christian Basler 2016-01-23 17:18:25 +01:00
parent 9f05af8bb7
commit 07b349563f
2 changed files with 11 additions and 7 deletions

View File

@ -101,14 +101,12 @@ public class MultiThreadedPOWEngine implements ProofOfWorkEngine {
if (!Bytes.lt(target, mda.digest(mda.digest()), 8)) { if (!Bytes.lt(target, mda.digest(mda.digest()), 8)) {
synchronized (callback) { synchronized (callback) {
if (!Thread.interrupted()) { if (!Thread.interrupted()) {
try { for (Worker w : workers) {
callback.onNonceCalculated(initialHash, nonce); w.interrupt();
} finally {
semaphore.release();
for (Worker w : workers) {
w.interrupt();
}
} }
// Clear interrupted flag for callback
Thread.interrupted();
callback.onNonceCalculated(initialHash, nonce);
} }
} }
return; return;
@ -129,8 +127,10 @@ public class MultiThreadedPOWEngine implements ProofOfWorkEngine {
@Override @Override
public void onNonceCalculated(byte[] initialHash, byte[] nonce) { public void onNonceCalculated(byte[] initialHash, byte[] nonce) {
// Prevents the callback from being called twice if two nonces are found simultaneously
synchronized (this) { synchronized (this) {
if (waiting) { if (waiting) {
semaphore.release();
LOG.info("Nonce calculated in " + ((System.currentTimeMillis() - startTime) / 1000) + " seconds"); LOG.info("Nonce calculated in " + ((System.currentTimeMillis() - startTime) / 1000) + " seconds");
waiting = false; waiting = false;
callback.onNonceCalculated(initialHash, nonce); callback.onNonceCalculated(initialHash, nonce);

View File

@ -24,6 +24,10 @@ import static ch.dissem.bitmessage.utils.Bytes.inc;
/** /**
* You should really use the MultiThreadedPOWEngine, but this one might help you grok the other one. * You should really use the MultiThreadedPOWEngine, but this one might help you grok the other one.
* <p>
* <strong>Warning:</strong> implementations probably depend on POW being asynchronous, that's
* another reason not to use this one.
* </p>
*/ */
public class SimplePOWEngine implements ProofOfWorkEngine { public class SimplePOWEngine implements ProofOfWorkEngine {
@Override @Override