From e5c956c6e5188a38d435360df943c0602b70ae6b Mon Sep 17 00:00:00 2001 From: Christian Basler Date: Wed, 12 Apr 2017 17:18:09 +0200 Subject: [PATCH] Minor improvements --- .../dissem/bitmessage/BitmessageContext.java | 21 +---------- .../dissem/bitmessage/ProofOfWorkService.java | 2 +- .../ch/dissem/bitmessage/entity/Version.java | 37 ++++++++++++++++++- .../entity/valueobject/NetworkAddress.java | 24 ++++++++---- .../bitmessage/factory/V3MessageFactory.java | 2 + 5 files changed, 57 insertions(+), 29 deletions(-) diff --git a/core/src/main/java/ch/dissem/bitmessage/BitmessageContext.java b/core/src/main/java/ch/dissem/bitmessage/BitmessageContext.java index 8d3c0d7..29639e7 100644 --- a/core/src/main/java/ch/dissem/bitmessage/BitmessageContext.java +++ b/core/src/main/java/ch/dissem/bitmessage/BitmessageContext.java @@ -25,7 +25,6 @@ import ch.dissem.bitmessage.exception.DecryptionFailedException; import ch.dissem.bitmessage.factory.Factory; import ch.dissem.bitmessage.ports.*; import ch.dissem.bitmessage.utils.Property; -import ch.dissem.bitmessage.utils.TTL; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -39,7 +38,8 @@ import static ch.dissem.bitmessage.InternalContext.NETWORK_EXTRA_BYTES; import static ch.dissem.bitmessage.InternalContext.NETWORK_NONCE_TRIALS_PER_BYTE; import static ch.dissem.bitmessage.entity.Plaintext.Type.BROADCAST; import static ch.dissem.bitmessage.entity.Plaintext.Type.MSG; -import static ch.dissem.bitmessage.utils.UnixTime.*; +import static ch.dissem.bitmessage.utils.UnixTime.HOUR; +import static ch.dissem.bitmessage.utils.UnixTime.MINUTE; /** *

Use this class if you want to create a Bitmessage client.

@@ -404,23 +404,6 @@ public class BitmessageContext { return this; } - /** - * Time to live in seconds for public keys the client sends. Defaults to the maximum of 28 days, - * but on weak devices smaller values might be desirable. - *

- * Please be aware that this might cause some problems where you can't receive a message (the - * sender can't receive your public key) in some special situations. Also note that it's probably - * not a good idea to set it too low. - *

- * - * @deprecated use {@link TTL#pubkey(long)} instead. - */ - public Builder pubkeyTTL(long days) { - if (days < 0 || days > 28 * DAY) throw new IllegalArgumentException("TTL must be between 1 and 28 days"); - TTL.pubkey(days); - return this; - } - public BitmessageContext build() { nonNull("inventory", inventory); nonNull("nodeRegistry", nodeRegistry); diff --git a/core/src/main/java/ch/dissem/bitmessage/ProofOfWorkService.java b/core/src/main/java/ch/dissem/bitmessage/ProofOfWorkService.java index 7c72bea..14e7c8a 100644 --- a/core/src/main/java/ch/dissem/bitmessage/ProofOfWorkService.java +++ b/core/src/main/java/ch/dissem/bitmessage/ProofOfWorkService.java @@ -81,7 +81,7 @@ public class ProofOfWorkService implements ProofOfWorkEngine.Callback, InternalC public void onNonceCalculated(byte[] initialHash, byte[] nonce) { Item item = powRepo.getItem(initialHash); if (item.message == null) { - ObjectMessage object = powRepo.getItem(initialHash).object; + ObjectMessage object = item.object; object.setNonce(nonce); Plaintext plaintext = messageRepo.getMessage(initialHash); if (plaintext != null) { diff --git a/core/src/main/java/ch/dissem/bitmessage/entity/Version.java b/core/src/main/java/ch/dissem/bitmessage/entity/Version.java index 4d0fd05..3a44366 100644 --- a/core/src/main/java/ch/dissem/bitmessage/entity/Version.java +++ b/core/src/main/java/ch/dissem/bitmessage/entity/Version.java @@ -20,6 +20,8 @@ import ch.dissem.bitmessage.BitmessageContext; import ch.dissem.bitmessage.entity.valueobject.NetworkAddress; import ch.dissem.bitmessage.utils.Encode; import ch.dissem.bitmessage.utils.UnixTime; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.IOException; import java.io.OutputStream; @@ -30,6 +32,7 @@ import java.nio.ByteBuffer; */ public class Version implements MessagePayload { private static final long serialVersionUID = 7219240857343176567L; + private static final Logger LOG = LoggerFactory.getLogger(Version.class); /** * Identifies protocol version being used by the node. Should equal 3. Nodes should disconnect if the remote node's @@ -93,6 +96,10 @@ public class Version implements MessagePayload { return services; } + public boolean provides(Service service) { + return service != null && service.isEnabled(services); + } + public long getTimestamp() { return timestamp; } @@ -159,7 +166,7 @@ public class Version implements MessagePayload { public Builder defaults(long clientNonce) { version = BitmessageContext.CURRENT_VERSION; - services = 1; + services = Service.getServiceFlag(Service.NODE_NETWORK); timestamp = UnixTime.now(); userAgent = "/Jabit:0.0.1/"; streamNumbers = new long[]{1}; @@ -172,6 +179,11 @@ public class Version implements MessagePayload { return this; } + public Builder services(Service... services) { + this.services = Service.getServiceFlag(services); + return this; + } + public Builder services(long services) { this.services = services; return this; @@ -211,4 +223,27 @@ public class Version implements MessagePayload { return new Version(this); } } + + public enum Service { + NODE_NETWORK(1); +// TODO: NODE_SSL(2); + + long flag; + + Service(long flag) { + this.flag = flag; + } + + public boolean isEnabled(long flag) { + return (flag & this.flag) != 0; + } + + public static long getServiceFlag(Service... services) { + long flag = 0; + for (Service service : services) { + flag |= service.flag; + } + return flag; + } + } } diff --git a/core/src/main/java/ch/dissem/bitmessage/entity/valueobject/NetworkAddress.java b/core/src/main/java/ch/dissem/bitmessage/entity/valueobject/NetworkAddress.java index 94bc7c0..fe3bfcb 100644 --- a/core/src/main/java/ch/dissem/bitmessage/entity/valueobject/NetworkAddress.java +++ b/core/src/main/java/ch/dissem/bitmessage/entity/valueobject/NetworkAddress.java @@ -17,6 +17,7 @@ package ch.dissem.bitmessage.entity.valueobject; import ch.dissem.bitmessage.entity.Streamable; +import ch.dissem.bitmessage.entity.Version; import ch.dissem.bitmessage.exception.ApplicationException; import ch.dissem.bitmessage.utils.Encode; import ch.dissem.bitmessage.utils.UnixTime; @@ -75,6 +76,13 @@ public class NetworkAddress implements Streamable { return services; } + public boolean provides(Version.Service service) { + if (service == null) { + return false; + } + return service.isEnabled(services); + } + public long getStream() { return stream; } @@ -194,20 +202,20 @@ public class NetworkAddress implements Streamable { int p08, int p09, int p10, int p11, int p12, int p13, int p14, int p15) { this.ipv6 = new byte[]{ - (byte) p00, (byte) p01, (byte) p02, (byte) p03, - (byte) p04, (byte) p05, (byte) p06, (byte) p07, - (byte) p08, (byte) p09, (byte) p10, (byte) p11, - (byte) p12, (byte) p13, (byte) p14, (byte) p15 + (byte) p00, (byte) p01, (byte) p02, (byte) p03, + (byte) p04, (byte) p05, (byte) p06, (byte) p07, + (byte) p08, (byte) p09, (byte) p10, (byte) p11, + (byte) p12, (byte) p13, (byte) p14, (byte) p15 }; return this; } public Builder ipv4(int p00, int p01, int p02, int p03) { this.ipv6 = new byte[]{ - (byte) 0, (byte) 0, (byte) 0x00, (byte) 0x00, - (byte) 0, (byte) 0, (byte) 0x00, (byte) 0x00, - (byte) 0, (byte) 0, (byte) 0xff, (byte) 0xff, - (byte) p00, (byte) p01, (byte) p02, (byte) p03 + (byte) 0, (byte) 0, (byte) 0x00, (byte) 0x00, + (byte) 0, (byte) 0, (byte) 0x00, (byte) 0x00, + (byte) 0, (byte) 0, (byte) 0xff, (byte) 0xff, + (byte) p00, (byte) p01, (byte) p02, (byte) p03 }; return this; } diff --git a/core/src/main/java/ch/dissem/bitmessage/factory/V3MessageFactory.java b/core/src/main/java/ch/dissem/bitmessage/factory/V3MessageFactory.java index 7b27d13..a702e16 100644 --- a/core/src/main/java/ch/dissem/bitmessage/factory/V3MessageFactory.java +++ b/core/src/main/java/ch/dissem/bitmessage/factory/V3MessageFactory.java @@ -24,6 +24,7 @@ import ch.dissem.bitmessage.entity.valueobject.NetworkAddress; import ch.dissem.bitmessage.exception.NodeException; import ch.dissem.bitmessage.utils.AccessCounter; import ch.dissem.bitmessage.utils.Decode; +import ch.dissem.bitmessage.utils.Strings; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -103,6 +104,7 @@ class V3MessageFactory { payload = Factory.getObjectPayload(objectType, version, stream, dataStream, data.length); } catch (Exception e) { LOG.trace("Could not parse object payload - using generic payload instead", e); + LOG.info(Strings.hex(data).toString()); payload = new GenericPayload(version, stream, data); }