From 9c2d8589bfdad356aec7b168a871358ee8a564ba Mon Sep 17 00:00:00 2001 From: Christian Basler Date: Wed, 28 Oct 2015 16:56:09 +0100 Subject: [PATCH] Improved POW test --- .../ports/MultiThreadedPOWEngine.java | 5 +++-- .../bitmessage/utils/CallbackWaiter.java | 11 ++++++++-- .../ports/ProofOfWorkEngineTest.java | 20 ++++++++----------- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/domain/src/main/java/ch/dissem/bitmessage/ports/MultiThreadedPOWEngine.java b/domain/src/main/java/ch/dissem/bitmessage/ports/MultiThreadedPOWEngine.java index 2907473..ac65d3d 100644 --- a/domain/src/main/java/ch/dissem/bitmessage/ports/MultiThreadedPOWEngine.java +++ b/domain/src/main/java/ch/dissem/bitmessage/ports/MultiThreadedPOWEngine.java @@ -36,8 +36,9 @@ public class MultiThreadedPOWEngine implements ProofOfWorkEngine { private static final Semaphore semaphore = new Semaphore(1, true); /** - * Although it has a callback, this method will block until all pending nonce calculations are done. (It gets very - * inefficient if multiple nonce are calculated at the same time. + * This method will block until all pending nonce calculations are done, but not wait for its own calculation + * to finish. + * (This implementation becomes very inefficient if multiple nonce are calculated at the same time.) * * @param initialHash the SHA-512 hash of the object to send, sans nonce * @param target the target, representing an unsigned long diff --git a/domain/src/main/java/ch/dissem/bitmessage/utils/CallbackWaiter.java b/domain/src/main/java/ch/dissem/bitmessage/utils/CallbackWaiter.java index e664945..775a5f3 100644 --- a/domain/src/main/java/ch/dissem/bitmessage/utils/CallbackWaiter.java +++ b/domain/src/main/java/ch/dissem/bitmessage/utils/CallbackWaiter.java @@ -20,13 +20,16 @@ package ch.dissem.bitmessage.utils; * Waits for a value within a callback method to be set. */ public class CallbackWaiter { + private final long startTime = System.currentTimeMillis(); private volatile boolean isSet; - private volatile T value; + private T value; + private long time; public void setValue(T value) { synchronized (this) { - this.isSet = true; + this.time = System.currentTimeMillis() - startTime; this.value = value; + this.isSet = true; } } @@ -38,4 +41,8 @@ public class CallbackWaiter { return value; } } + + public long getTime() { + return time; + } } diff --git a/domain/src/test/java/ch/dissem/bitmessage/ports/ProofOfWorkEngineTest.java b/domain/src/test/java/ch/dissem/bitmessage/ports/ProofOfWorkEngineTest.java index b4fd194..ba5307d 100644 --- a/domain/src/test/java/ch/dissem/bitmessage/ports/ProofOfWorkEngineTest.java +++ b/domain/src/test/java/ch/dissem/bitmessage/ports/ProofOfWorkEngineTest.java @@ -36,27 +36,24 @@ public class ProofOfWorkEngineTest extends TestBase { } private void testPOW(ProofOfWorkEngine engine) throws InterruptedException { - long time = System.currentTimeMillis(); byte[] initialHash = security().sha512(new byte[]{1, 3, 6, 4}); - byte[] target = {0, 0, -1, -1, -1, -1, -1, -1}; + byte[] target = {0, 0, 0, -1, -1, -1, -1, -1}; - final CallbackWaiter waiter = new CallbackWaiter<>(); + final CallbackWaiter waiter1 = new CallbackWaiter<>(); engine.calculateNonce(initialHash, target, new ProofOfWorkEngine.Callback() { @Override public void onNonceCalculated(byte[] nonce) { - waiter.setValue(nonce); + waiter1.setValue(nonce); } }); - byte[] nonce = waiter.waitForValue(); - time = System.currentTimeMillis() - time; - System.out.println("Calculating nonce took " + time + "ms"); + byte[] nonce = waiter1.waitForValue(); + System.out.println("Calculating nonce took " + waiter1.getTime() + "ms"); assertTrue(Bytes.lt(security().doubleSha512(nonce, initialHash), target, 8)); // Let's add a second (shorter) run to find possible multi threading issues - long time2 = System.currentTimeMillis(); byte[] initialHash2 = security().sha512(new byte[]{1, 3, 6, 5}); - byte[] target2 = {0, -1, -1, -1, -1, -1, -1, -1}; + byte[] target2 = {0, 0, -1, -1, -1, -1, -1, -1}; final CallbackWaiter waiter2 = new CallbackWaiter<>(); engine.calculateNonce(initialHash2, target2, @@ -67,10 +64,9 @@ public class ProofOfWorkEngineTest extends TestBase { } }); byte[] nonce2 = waiter2.waitForValue(); - time2 = System.currentTimeMillis() - time2; - System.out.println("Calculating nonce took " + time2 + "ms"); + System.out.println("Calculating nonce took " + waiter2.getTime() + "ms"); assertTrue(Bytes.lt(security().doubleSha512(nonce2, initialHash2), target2, 8)); - assertTrue("Second nonce must be quicker to find", time > time2); + assertTrue("Second nonce must be quicker to find", waiter1.getTime() > waiter2.getTime()); } }