Improved POW to use all cores. Interestingly, speed up factor seems to be greater than the number of cores (but still quite slow for real word application)

This commit is contained in:
2015-04-17 11:17:39 +02:00
parent 388a10fe8a
commit 8d7b9f6457
11 changed files with 228 additions and 21 deletions

View File

@@ -0,0 +1,48 @@
/*
* Copyright 2015 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.bitmessage.ports;
import ch.dissem.bitmessage.utils.Bytes;
import ch.dissem.bitmessage.utils.Security;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
/**
* Created by chris on 17.04.15.
*/
public class ProofOfWorkEngineTest {
@Test
public void testSimplePOWEngine() {
testPOW(new SimplePOWEngine());
}
@Test
public void testThreadedPOWEngine() {
testPOW(new MultiThreadedPOWEngine());
}
private void testPOW(ProofOfWorkEngine engine) {
long time = System.currentTimeMillis();
byte[] initialHash = Security.sha512(new byte[]{1, 3, 6, 4});
byte[] target = {0, 0, 0, -1, -1, -1, -1, -1};
byte[] nonce = engine.calculateNonce(initialHash, target);
System.out.println("Calculating nonce took " + (System.currentTimeMillis() - time) + "ms");
assertTrue(Bytes.lt(Security.doubleSha512(nonce, initialHash), target, 8));
}
}

View File

@@ -18,7 +18,6 @@ package ch.dissem.bitmessage.utils;
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Random;
@@ -30,19 +29,28 @@ import static org.junit.Assert.assertEquals;
* Created by chris on 10.04.15.
*/
public class BytesTest {
public static final Random rnd = new Random();
@Test
public void testIncrement() throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Encode.int16(256, out);
byte[] bytes = {0, -1};
Bytes.inc(bytes);
assertArrayEquals(out.toByteArray(), bytes);
assertArrayEquals(TestUtils.int16(256), bytes);
}
@Test
public void testIncrementByValue() throws IOException {
for (int v = 0; v < 256; v++) {
for (int i = 1; i < 256; i++) {
byte[] bytes = {0, (byte) v};
Bytes.inc(bytes, (byte) i);
assertArrayEquals("value = " + v + "; inc = " + i + "; expected = " + (v + i), TestUtils.int16(v + i), bytes);
}
}
}
@Test
public void testLowerThan() {
Random rnd = new Random();
for (int i = 0; i < 1000; i++) {
BigInteger a = BigInteger.valueOf(rnd.nextLong()).pow((rnd.nextInt(5) + 1)).abs();
BigInteger b = BigInteger.valueOf(rnd.nextLong()).pow((rnd.nextInt(5) + 1)).abs();
@@ -53,7 +61,6 @@ public class BytesTest {
@Test
public void testLowerThanBounded() {
Random rnd = new Random();
for (int i = 0; i < 1000; i++) {
BigInteger a = BigInteger.valueOf(rnd.nextLong()).pow((rnd.nextInt(5) + 1)).abs();
BigInteger b = BigInteger.valueOf(rnd.nextLong()).pow((rnd.nextInt(5) + 1)).abs();

View File

@@ -18,7 +18,7 @@ package ch.dissem.bitmessage.utils;
import ch.dissem.bitmessage.entity.ObjectMessage;
import ch.dissem.bitmessage.entity.payload.GenericPayload;
import ch.dissem.bitmessage.ports.SimplePOWEngine;
import ch.dissem.bitmessage.ports.MultiThreadedPOWEngine;
import org.junit.Test;
import javax.xml.bind.DatatypeConverter;
@@ -86,8 +86,8 @@ public class SecurityTest {
.expiresTime(expires.getTimeInMillis() / 1000)
.payload(new GenericPayload(1, new byte[0]))
.build();
Security.doProofOfWork(objectMessage, new SimplePOWEngine(), 10, 10);
Security.checkProofOfWork(objectMessage, 10, 10);
Security.doProofOfWork(objectMessage, new MultiThreadedPOWEngine(), 1000, 1000);
Security.checkProofOfWork(objectMessage, 1000, 1000);
}
@Test

View File

@@ -0,0 +1,31 @@
/*
* Copyright 2015 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.bitmessage.utils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
/**
* If there's ever a need for this in production code, it should be rewritten to be more efficient.
*/
public class TestUtils {
public static byte[] int16(int number) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Encode.int16(number, out);
return out.toByteArray();
}
}