Improved POW test

This commit is contained in:
Christian Basler 2015-10-28 16:56:09 +01:00
parent b496f81b20
commit 9c2d8589bf
3 changed files with 20 additions and 16 deletions

View File

@ -36,8 +36,9 @@ public class MultiThreadedPOWEngine implements ProofOfWorkEngine {
private static final Semaphore semaphore = new Semaphore(1, true); 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 * This method will block until all pending nonce calculations are done, but not wait for its own calculation
* inefficient if multiple nonce are calculated at the same time. * 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 initialHash the SHA-512 hash of the object to send, sans nonce
* @param target the target, representing an unsigned long * @param target the target, representing an unsigned long

View File

@ -20,13 +20,16 @@ package ch.dissem.bitmessage.utils;
* Waits for a value within a callback method to be set. * Waits for a value within a callback method to be set.
*/ */
public class CallbackWaiter<T> { public class CallbackWaiter<T> {
private final long startTime = System.currentTimeMillis();
private volatile boolean isSet; private volatile boolean isSet;
private volatile T value; private T value;
private long time;
public void setValue(T value) { public void setValue(T value) {
synchronized (this) { synchronized (this) {
this.isSet = true; this.time = System.currentTimeMillis() - startTime;
this.value = value; this.value = value;
this.isSet = true;
} }
} }
@ -38,4 +41,8 @@ public class CallbackWaiter<T> {
return value; return value;
} }
} }
public long getTime() {
return time;
}
} }

View File

@ -36,27 +36,24 @@ public class ProofOfWorkEngineTest extends TestBase {
} }
private void testPOW(ProofOfWorkEngine engine) throws InterruptedException { private void testPOW(ProofOfWorkEngine engine) throws InterruptedException {
long time = System.currentTimeMillis();
byte[] initialHash = security().sha512(new byte[]{1, 3, 6, 4}); 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<byte[]> waiter = new CallbackWaiter<>(); final CallbackWaiter<byte[]> waiter1 = new CallbackWaiter<>();
engine.calculateNonce(initialHash, target, engine.calculateNonce(initialHash, target,
new ProofOfWorkEngine.Callback() { new ProofOfWorkEngine.Callback() {
@Override @Override
public void onNonceCalculated(byte[] nonce) { public void onNonceCalculated(byte[] nonce) {
waiter.setValue(nonce); waiter1.setValue(nonce);
} }
}); });
byte[] nonce = waiter.waitForValue(); byte[] nonce = waiter1.waitForValue();
time = System.currentTimeMillis() - time; System.out.println("Calculating nonce took " + waiter1.getTime() + "ms");
System.out.println("Calculating nonce took " + time + "ms");
assertTrue(Bytes.lt(security().doubleSha512(nonce, initialHash), target, 8)); assertTrue(Bytes.lt(security().doubleSha512(nonce, initialHash), target, 8));
// Let's add a second (shorter) run to find possible multi threading issues // 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[] 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<byte[]> waiter2 = new CallbackWaiter<>(); final CallbackWaiter<byte[]> waiter2 = new CallbackWaiter<>();
engine.calculateNonce(initialHash2, target2, engine.calculateNonce(initialHash2, target2,
@ -67,10 +64,9 @@ public class ProofOfWorkEngineTest extends TestBase {
} }
}); });
byte[] nonce2 = waiter2.waitForValue(); byte[] nonce2 = waiter2.waitForValue();
time2 = System.currentTimeMillis() - time2; System.out.println("Calculating nonce took " + waiter2.getTime() + "ms");
System.out.println("Calculating nonce took " + time2 + "ms");
assertTrue(Bytes.lt(security().doubleSha512(nonce2, initialHash2), target2, 8)); 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());
} }
} }