Introduced some custom exceptions

This commit is contained in:
Christian Basler 2015-05-23 16:01:18 +02:00
parent df4b67609e
commit b347214b66
4 changed files with 17 additions and 3 deletions

View File

@ -212,7 +212,7 @@ public class Application {
int i = 0;
for (Plaintext message : messages) {
i++;
System.out.print(i + ") From: " + message.getFrom() + "; Subject: " + message.getSubject());
System.out.println(i + ") From: " + message.getFrom() + "; Subject: " + message.getSubject());
}
if (i == 0) {
System.out.println("You have no messages.");

View File

@ -97,6 +97,7 @@ public class Msg extends ObjectPayload implements Encrypted {
@Override
public void write(OutputStream out) throws IOException {
if (encrypted == null) throw new IllegalStateException("Msg must be signed and encrypted before writing it.");
encrypted.write(out);
}
}

View File

@ -0,0 +1,12 @@
package ch.dissem.bitmessage.exception;
import ch.dissem.bitmessage.utils.Strings;
import java.io.IOException;
import java.util.Arrays;
public class InsufficientProofOfWorkException extends IOException {
public InsufficientProofOfWorkException(byte[] target, byte[] hash) {
super("Insufficient proof of work: " + Strings.hex(target) + " required, " + Strings.hex(Arrays.copyOfRange(hash, 0, 8)) + " achieved.");
}
}

View File

@ -18,6 +18,7 @@ package ch.dissem.bitmessage.utils;
import ch.dissem.bitmessage.entity.ObjectMessage;
import ch.dissem.bitmessage.entity.payload.Pubkey;
import ch.dissem.bitmessage.exception.InsufficientProofOfWorkException;
import ch.dissem.bitmessage.factory.Factory;
import ch.dissem.bitmessage.ports.ProofOfWorkEngine;
import org.bouncycastle.asn1.x9.X9ECParameters;
@ -106,13 +107,13 @@ public class Security {
}
/**
* @throws IOException if proof of work doesn't check out
* @throws InsufficientProofOfWorkException if proof of work doesn't check out
*/
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 IOException("Insufficient proof of work: " + Strings.hex(target) + " required, " + Strings.hex(Arrays.copyOfRange(value, 0, 8)) + " achieved.");
throw new InsufficientProofOfWorkException(target, value);
}
}