/** * Calculates the proof of work. This might take a long time, depending on the hardware, message size and time to * live. * * @param object to do the proof of work for * @param worker doing the actual proof of work * @param nonceTrialsPerByte difficulty * @param extraBytes bytes to add to the object size (makes it more difficult to send small messages) */ public static void doProofOfWork(ObjectMessage object, ProofOfWorkEngine worker, long nonceTrialsPerByte, long extraBytes) { try { if (nonceTrialsPerByte < 1000) nonceTrialsPerByte = 1000; if (extraBytes < 1000) extraBytes = 1000; byte[] initialHash = getInitialHash(object); byte[] target = getProofOfWorkTarget(object, nonceTrialsPerByte, extraBytes); byte[] nonce = worker.calculateNonce(initialHash, target); object.setNonce(nonce); } catch (IOException e) { throw new RuntimeException(e); } }
/** * @param object to be checked * @param nonceTrialsPerByte difficulty * @param extraBytes bytes to add to the object size * @throws InsufficientProofOfWorkException if proof of work doesn't check out (makes it more difficult to send small messages) */ public static void checkProofOfWork(ObjectMessage object, long nonceTrialsPerByte, long extraBytes) throws IOException { byte[] target = getProofOfWorkTarget(object, nonceTrialsPerByte, extraBytes); byte[] value = Security.doubleSha512(object.getNonce(), getInitialHash(object)); if (Bytes.lt(target, value, 8)) { throw new InsufficientProofOfWorkException(target, value); } }