diff --git a/domain/src/main/java/ch/dissem/bitmessage/BitmessageContext.java b/domain/src/main/java/ch/dissem/bitmessage/BitmessageContext.java index 7445cd4..7878ada 100644 --- a/domain/src/main/java/ch/dissem/bitmessage/BitmessageContext.java +++ b/domain/src/main/java/ch/dissem/bitmessage/BitmessageContext.java @@ -32,10 +32,7 @@ import ch.dissem.bitmessage.utils.UnixTime; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.IOException; import java.util.Arrays; -import java.util.Collection; -import java.util.TreeSet; import static ch.dissem.bitmessage.entity.Plaintext.Status.*; import static ch.dissem.bitmessage.entity.Plaintext.Type.BROADCAST; @@ -43,8 +40,7 @@ import static ch.dissem.bitmessage.entity.Plaintext.Type.MSG; import static ch.dissem.bitmessage.utils.UnixTime.DAY; /** - * Use this class if you want to create a Bitmessage client. - *
+ *
Use this class if you want to create a Bitmessage client.
* You'll need the Builder to create a BitmessageContext, and set the following properties: *- * The port defaults to 8444 (the default Bitmessage port) + *
The default implementations in the different module builds can be used.
+ *The port defaults to 8444 (the default Bitmessage port)
*/ public class BitmessageContext { public static final int CURRENT_VERSION = 3; @@ -167,21 +162,17 @@ public class BitmessageContext { } private void send(long stream, ObjectPayload payload, long timeToLive) { - try { - long expires = UnixTime.now(+timeToLive); - LOG.info("Expires at " + expires); - ObjectMessage object = new ObjectMessage.Builder() - .stream(stream) - .expiresTime(expires) - .payload(payload) - .build(); - Security.doProofOfWork(object, ctx.getProofOfWorkEngine(), - ctx.getNetworkNonceTrialsPerByte(), ctx.getNetworkExtraBytes()); - ctx.getInventory().storeObject(object); - ctx.getNetworkHandler().offer(object.getInventoryVector()); - } catch (IOException e) { - throw new RuntimeException(e); - } + long expires = UnixTime.now(+timeToLive); + LOG.info("Expires at " + expires); + ObjectMessage object = new ObjectMessage.Builder() + .stream(stream) + .expiresTime(expires) + .payload(payload) + .build(); + Security.doProofOfWork(object, ctx.getProofOfWorkEngine(), + ctx.getNetworkNonceTrialsPerByte(), ctx.getNetworkExtraBytes()); + ctx.getInventory().storeObject(object); + ctx.getNetworkHandler().offer(object.getInventoryVector()); } public void startup(Listener listener) { diff --git a/domain/src/main/java/ch/dissem/bitmessage/InternalContext.java b/domain/src/main/java/ch/dissem/bitmessage/InternalContext.java index 04e9398..8ac64e0 100644 --- a/domain/src/main/java/ch/dissem/bitmessage/InternalContext.java +++ b/domain/src/main/java/ch/dissem/bitmessage/InternalContext.java @@ -199,20 +199,16 @@ public class InternalContext { } public void requestPubkey(BitmessageAddress contact) { - try { - long expires = UnixTime.now(+2 * DAY); - LOG.info("Expires at " + expires); - ObjectMessage response = new ObjectMessage.Builder() - .stream(contact.getStream()) - .expiresTime(expires) - .payload(new GetPubkey(contact)) - .build(); - Security.doProofOfWork(response, proofOfWorkEngine, networkNonceTrialsPerByte, networkExtraBytes); - inventory.storeObject(response); - networkHandler.offer(response.getInventoryVector()); - } catch (IOException e) { - throw new RuntimeException(e); - } + long expires = UnixTime.now(+2 * DAY); + LOG.info("Expires at " + expires); + ObjectMessage response = new ObjectMessage.Builder() + .stream(contact.getStream()) + .expiresTime(expires) + .payload(new GetPubkey(contact)) + .build(); + Security.doProofOfWork(response, proofOfWorkEngine, networkNonceTrialsPerByte, networkExtraBytes); + inventory.storeObject(response); + networkHandler.offer(response.getInventoryVector()); } public long getClientNonce() { diff --git a/domain/src/main/java/ch/dissem/bitmessage/entity/ObjectMessage.java b/domain/src/main/java/ch/dissem/bitmessage/entity/ObjectMessage.java index 14bd43f..0ef3340 100644 --- a/domain/src/main/java/ch/dissem/bitmessage/entity/ObjectMessage.java +++ b/domain/src/main/java/ch/dissem/bitmessage/entity/ObjectMessage.java @@ -88,7 +88,7 @@ public class ObjectMessage implements MessagePayload { return stream; } - public InventoryVector getInventoryVector() throws IOException { + public InventoryVector getInventoryVector() { return new InventoryVector(Bytes.truncate(Security.doubleSha512(nonce, getPayloadBytesWithoutNonce()), 32)); } @@ -163,14 +163,18 @@ public class ObjectMessage implements MessagePayload { Encode.varInt(stream, out); } - public byte[] getPayloadBytesWithoutNonce() throws IOException { - if (payloadBytes == null) { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - writeHeaderWithoutNonce(out); - payload.write(out); - payloadBytes = out.toByteArray(); + public byte[] getPayloadBytesWithoutNonce() { + try { + if (payloadBytes == null) { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + writeHeaderWithoutNonce(out); + payload.write(out); + payloadBytes = out.toByteArray(); + } + return payloadBytes; + } catch (IOException e) { + throw new RuntimeException(e); } - return payloadBytes; } public static final class Builder { diff --git a/networking/src/main/java/ch/dissem/bitmessage/networking/Connection.java b/networking/src/main/java/ch/dissem/bitmessage/networking/Connection.java index 7dc07b2..0eb0f09 100644 --- a/networking/src/main/java/ch/dissem/bitmessage/networking/Connection.java +++ b/networking/src/main/java/ch/dissem/bitmessage/networking/Connection.java @@ -287,6 +287,8 @@ public class Connection implements Runnable { } catch (IOException e) { LOG.error("Stream " + objectMessage.getStream() + ", object type " + objectMessage.getType() + ": " + e.getMessage(), e); DebugUtils.saveToFile(objectMessage); + } finally { + requestedObjects.remove(objectMessage.getInventoryVector()); } break; case ADDR: