From 07b349563ff02bc892227c66cef2cd64da973596 Mon Sep 17 00:00:00 2001 From: Christian Basler Date: Sat, 23 Jan 2016 17:18:25 +0100 Subject: [PATCH] Fixed an issue in the POW engine --- .../bitmessage/ports/MultiThreadedPOWEngine.java | 14 +++++++------- .../dissem/bitmessage/ports/SimplePOWEngine.java | 4 ++++ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/ch/dissem/bitmessage/ports/MultiThreadedPOWEngine.java b/core/src/main/java/ch/dissem/bitmessage/ports/MultiThreadedPOWEngine.java index 5e00e33..790e3b7 100644 --- a/core/src/main/java/ch/dissem/bitmessage/ports/MultiThreadedPOWEngine.java +++ b/core/src/main/java/ch/dissem/bitmessage/ports/MultiThreadedPOWEngine.java @@ -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); diff --git a/core/src/main/java/ch/dissem/bitmessage/ports/SimplePOWEngine.java b/core/src/main/java/ch/dissem/bitmessage/ports/SimplePOWEngine.java index 06d234b..e8d649b 100644 --- a/core/src/main/java/ch/dissem/bitmessage/ports/SimplePOWEngine.java +++ b/core/src/main/java/ch/dissem/bitmessage/ports/SimplePOWEngine.java @@ -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. + *

+ * Warning: implementations probably depend on POW being asynchronous, that's + * another reason not to use this one. + *

*/ public class SimplePOWEngine implements ProofOfWorkEngine { @Override