diff --git a/core/src/main/java/ch/dissem/bitmessage/entity/ObjectMessage.java b/core/src/main/java/ch/dissem/bitmessage/entity/ObjectMessage.java index fee761c..8e386f7 100644 --- a/core/src/main/java/ch/dissem/bitmessage/entity/ObjectMessage.java +++ b/core/src/main/java/ch/dissem/bitmessage/entity/ObjectMessage.java @@ -96,7 +96,7 @@ public class ObjectMessage implements MessagePayload { } public InventoryVector getInventoryVector() { - return new InventoryVector( + return InventoryVector.fromHash( Bytes.truncate(cryptography().doubleSha512(nonce, getPayloadBytesWithoutNonce()), 32) ); } diff --git a/core/src/main/java/ch/dissem/bitmessage/entity/valueobject/InventoryVector.java b/core/src/main/java/ch/dissem/bitmessage/entity/valueobject/InventoryVector.java index 9a3b258..e1f8f60 100644 --- a/core/src/main/java/ch/dissem/bitmessage/entity/valueobject/InventoryVector.java +++ b/core/src/main/java/ch/dissem/bitmessage/entity/valueobject/InventoryVector.java @@ -52,10 +52,19 @@ public class InventoryVector implements Streamable, Serializable { return hash; } - public InventoryVector(byte[] hash) { + private InventoryVector(byte[] hash) { + if (hash == null) throw new IllegalArgumentException("hash must not be null"); this.hash = hash; } + public static InventoryVector fromHash(byte[] hash) { + if (hash == null) { + return null; + } else { + return new InventoryVector(hash); + } + } + @Override public void write(OutputStream out) throws IOException { out.write(hash); diff --git a/core/src/main/java/ch/dissem/bitmessage/entity/valueobject/extended/Message.java b/core/src/main/java/ch/dissem/bitmessage/entity/valueobject/extended/Message.java index 5bba98b..aea4368 100644 --- a/core/src/main/java/ch/dissem/bitmessage/entity/valueobject/extended/Message.java +++ b/core/src/main/java/ch/dissem/bitmessage/entity/valueobject/extended/Message.java @@ -184,7 +184,7 @@ public class Message implements ExtendedEncoding.ExtendedType { MPArray parents = (MPArray) map.get(mp("parents")); if (parents != null) { for (MPBinary parent : parents) { - builder.addParent(new InventoryVector(parent.getValue())); + builder.addParent(InventoryVector.fromHash(parent.getValue())); } } @SuppressWarnings("unchecked") diff --git a/core/src/main/java/ch/dissem/bitmessage/entity/valueobject/extended/Vote.java b/core/src/main/java/ch/dissem/bitmessage/entity/valueobject/extended/Vote.java index 6ea174f..1c13440 100644 --- a/core/src/main/java/ch/dissem/bitmessage/entity/valueobject/extended/Vote.java +++ b/core/src/main/java/ch/dissem/bitmessage/entity/valueobject/extended/Vote.java @@ -105,7 +105,7 @@ public class Vote implements ExtendedEncoding.ExtendedType { Vote.Builder builder = new Vote.Builder(); MPType msgId = map.get(mp("msgId")); if (msgId instanceof MPBinary) { - builder.msgId(new InventoryVector(((MPBinary) msgId).getValue())); + builder.msgId(InventoryVector.fromHash(((MPBinary) msgId).getValue())); } builder.vote(str(map.get(mp("vote")))); return new Vote(builder); 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 a702e16..378f658 100644 --- a/core/src/main/java/ch/dissem/bitmessage/factory/V3MessageFactory.java +++ b/core/src/main/java/ch/dissem/bitmessage/factory/V3MessageFactory.java @@ -103,8 +103,10 @@ class V3MessageFactory { ByteArrayInputStream dataStream = new ByteArrayInputStream(data); 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()); + if (LOG.isTraceEnabled()) { + LOG.trace("Could not parse object payload - using generic payload instead", e); + LOG.trace(Strings.hex(data).toString()); + } payload = new GenericPayload(version, stream, data); } @@ -165,7 +167,7 @@ class V3MessageFactory { } private static InventoryVector parseInventoryVector(InputStream stream) throws IOException { - return new InventoryVector(Decode.bytes(stream, 32)); + return InventoryVector.fromHash(Decode.bytes(stream, 32)); } private static NetworkAddress parseAddress(InputStream stream, boolean light) throws IOException { diff --git a/core/src/test/java/ch/dissem/bitmessage/BitmessageContextTest.java b/core/src/test/java/ch/dissem/bitmessage/BitmessageContextTest.java index 8051134..0702866 100644 --- a/core/src/test/java/ch/dissem/bitmessage/BitmessageContextTest.java +++ b/core/src/test/java/ch/dissem/bitmessage/BitmessageContextTest.java @@ -72,7 +72,7 @@ public class BitmessageContextTest { @Override public Item getItem(byte[] initialHash) { - return items.get(new InventoryVector(initialHash)); + return items.get(InventoryVector.fromHash(initialHash)); } @Override @@ -86,12 +86,12 @@ public class BitmessageContextTest { @Override public void putObject(Item item) { - items.put(new InventoryVector(cryptography().getInitialHash(item.object)), item); + items.put(InventoryVector.fromHash(cryptography().getInitialHash(item.object)), item); } @Override public void putObject(ObjectMessage object, long nonceTrialsPerByte, long extraBytes) { - items.put(new InventoryVector(cryptography().getInitialHash(object)), new Item(object, nonceTrialsPerByte, extraBytes)); + items.put(InventoryVector.fromHash(cryptography().getInitialHash(object)), new Item(object, nonceTrialsPerByte, extraBytes)); } @Override diff --git a/core/src/test/java/ch/dissem/bitmessage/utils/TestUtils.java b/core/src/test/java/ch/dissem/bitmessage/utils/TestUtils.java index fffaba8..6fadce4 100644 --- a/core/src/test/java/ch/dissem/bitmessage/utils/TestUtils.java +++ b/core/src/test/java/ch/dissem/bitmessage/utils/TestUtils.java @@ -66,7 +66,7 @@ public class TestUtils { public static InventoryVector randomInventoryVector() { byte[] bytes = new byte[32]; RANDOM.nextBytes(bytes); - return new InventoryVector(bytes); + return InventoryVector.fromHash(bytes); } public static InputStream getResource(String resourceName) { diff --git a/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcInventory.java b/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcInventory.java index 13d3725..33b7290 100644 --- a/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcInventory.java +++ b/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcInventory.java @@ -69,7 +69,7 @@ public class JdbcInventory extends JdbcHelper implements Inventory { "WHERE expires > " + now(-5 * MINUTE) + " AND stream = " + stream) ) { while (rs.next()) { - result.put(new InventoryVector(rs.getBytes("hash")), rs.getLong("expires")); + result.put(InventoryVector.fromHash(rs.getBytes("hash")), rs.getLong("expires")); } } catch (SQLException e) { LOG.error(e.getMessage(), e); diff --git a/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcMessageRepository.java b/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcMessageRepository.java index a918456..11c7028 100644 --- a/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcMessageRepository.java +++ b/repositories/src/main/java/ch/dissem/bitmessage/repository/JdbcMessageRepository.java @@ -110,7 +110,7 @@ public class JdbcMessageRepository extends AbstractMessageRepository implements Plaintext.Builder builder = Plaintext.readWithoutSignature(type, data); long id = rs.getLong("id"); builder.id(id); - builder.IV(new InventoryVector(iv)); + builder.IV(InventoryVector.fromHash(iv)); builder.from(ctx.getAddressRepository().getAddress(rs.getString("sender"))); builder.to(ctx.getAddressRepository().getAddress(rs.getString("recipient"))); builder.ackData(rs.getBytes("ack_data"));