forked from Qortal/qortal
Added optional "timeout" feature to MemoryPoW.compute2(), to give up after a specified amount of time.
This commit is contained in:
parent
a232395750
commit
5c607d3367
@ -1,10 +1,25 @@
|
|||||||
package org.qortal.crypto;
|
package org.qortal.crypto;
|
||||||
|
|
||||||
|
import org.qortal.utils.NTP;
|
||||||
|
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
|
import java.util.concurrent.TimeoutException;
|
||||||
|
|
||||||
public class MemoryPoW {
|
public class MemoryPoW {
|
||||||
|
|
||||||
public static Integer compute2(byte[] data, int workBufferLength, long difficulty) {
|
public static Integer compute2(byte[] data, int workBufferLength, long difficulty) {
|
||||||
|
try {
|
||||||
|
return MemoryPoW.compute2(data, workBufferLength, difficulty, null);
|
||||||
|
|
||||||
|
} catch (TimeoutException e) {
|
||||||
|
// This won't happen, because above timeout is null
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Integer compute2(byte[] data, int workBufferLength, long difficulty, Long timeout) throws TimeoutException {
|
||||||
|
long startTime = NTP.getTime();
|
||||||
|
|
||||||
// Hash data with SHA256
|
// Hash data with SHA256
|
||||||
byte[] hash = Crypto.digest(data);
|
byte[] hash = Crypto.digest(data);
|
||||||
|
|
||||||
@ -33,6 +48,13 @@ public class MemoryPoW {
|
|||||||
if (Thread.currentThread().isInterrupted())
|
if (Thread.currentThread().isInterrupted())
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
|
if (timeout != null) {
|
||||||
|
long now = NTP.getTime();
|
||||||
|
if (now > startTime + timeout) {
|
||||||
|
throw new TimeoutException("Timeout reached");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
seed *= seedMultiplier; // per nonce
|
seed *= seedMultiplier; // per nonce
|
||||||
|
|
||||||
state[0] = longHash[0] ^ seed;
|
state[0] = longHash[0] ^ seed;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user