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