diff --git a/core/src/main/java/ch/dissem/bitmessage/DefaultMessageListener.java b/core/src/main/java/ch/dissem/bitmessage/DefaultMessageListener.java index c8eed14..e8be606 100644 --- a/core/src/main/java/ch/dissem/bitmessage/DefaultMessageListener.java +++ b/core/src/main/java/ch/dissem/bitmessage/DefaultMessageListener.java @@ -165,8 +165,8 @@ class DefaultMessageListener implements NetworkHandler.MessageListener { listener.receive(msg); updatePubkey(msg.getFrom(), msg.getFrom().getPubkey()); - if (msg.getType() == Type.MSG && identity.has(Pubkey.Feature.DOES_ACK)) { - ObjectMessage ack = plaintext.getAckMessage(); + if (msg.getType() == Plaintext.Type.MSG && msg.getTo().has(Pubkey.Feature.DOES_ACK)) { + ObjectMessage ack = msg.getAckMessage(); if (ack != null) { ctx.getInventory().storeObject(ack); ctx.getNetworkHandler().offer(ack.getInventoryVector()); diff --git a/core/src/main/java/ch/dissem/bitmessage/InternalContext.java b/core/src/main/java/ch/dissem/bitmessage/InternalContext.java index 03c6bfc..586adf4 100644 --- a/core/src/main/java/ch/dissem/bitmessage/InternalContext.java +++ b/core/src/main/java/ch/dissem/bitmessage/InternalContext.java @@ -16,14 +16,10 @@ package ch.dissem.bitmessage; -import ch.dissem.bitmessage.entity.BitmessageAddress; -import ch.dissem.bitmessage.entity.Encrypted; -import ch.dissem.bitmessage.entity.ObjectMessage; -import ch.dissem.bitmessage.entity.payload.*; -import ch.dissem.bitmessage.exception.ApplicationException; import ch.dissem.bitmessage.entity.*; import ch.dissem.bitmessage.entity.payload.*; import ch.dissem.bitmessage.entity.valueobject.Label; +import ch.dissem.bitmessage.exception.ApplicationException; import ch.dissem.bitmessage.ports.*; import ch.dissem.bitmessage.utils.Singleton; import ch.dissem.bitmessage.utils.TTL; @@ -36,7 +32,6 @@ import java.util.Arrays; import java.util.TreeSet; import static ch.dissem.bitmessage.entity.Plaintext.Status.SENT; -import static ch.dissem.bitmessage.utils.UnixTime.DAY; /** * The internal context should normally only be used for port implementations. If you need it in your client @@ -182,11 +177,11 @@ public class InternalContext { if (payload instanceof Msg && recipient.has(Pubkey.Feature.DOES_ACK)) { ObjectMessage ackMessage = ((Msg) payload).getPlaintext().getAckMessage(); messageCallback.proofOfWorkStarted(payload); - security.doProofOfWork(ackMessage, networkNonceTrialsPerByte, networkExtraBytes, new ProofOfWorkEngine.Callback() { + cryptography.doProofOfWork(ackMessage, NETWORK_NONCE_TRIALS_PER_BYTE, NETWORK_EXTRA_BYTES, new ProofOfWorkEngine.Callback() { @Override - public void onNonceCalculated(byte[] nonce) { + public void onNonceCalculated(byte[] initialHash, byte[] nonce) { object.encrypt(recipient.getPubkey()); - proofOfWorkService.doProofOfWork(to, object); + proofOfWorkService.doProofOfWork(recipient, object); } }); } else { @@ -318,7 +313,7 @@ public class InternalContext { } @Override - public void onNonceCalculated(byte[] nonce) { + public void onNonceCalculated(byte[] initialHash, byte[] nonce) { object.setNonce(nonce); messageCallback.proofOfWorkCompleted(payload); if (payload instanceof PlaintextHolder) { diff --git a/core/src/main/java/ch/dissem/bitmessage/entity/Plaintext.java b/core/src/main/java/ch/dissem/bitmessage/entity/Plaintext.java index 01deca8..c7f23ce 100644 --- a/core/src/main/java/ch/dissem/bitmessage/entity/Plaintext.java +++ b/core/src/main/java/ch/dissem/bitmessage/entity/Plaintext.java @@ -175,7 +175,7 @@ public class Plaintext implements Streamable { if (type == Type.MSG) { if (to.has(Pubkey.Feature.DOES_ACK)) { ByteArrayOutputStream ack = new ByteArrayOutputStream(); - ackMessage.write(ack); + getAckMessage().write(ack); byte[] data = ack.toByteArray(); Encode.varInt(data.length, out); out.write(data); diff --git a/domain/src/main/java/ch/dissem/bitmessage/entity/payload/Ack.java b/core/src/main/java/ch/dissem/bitmessage/entity/payload/Ack.java similarity index 100% rename from domain/src/main/java/ch/dissem/bitmessage/entity/payload/Ack.java rename to core/src/main/java/ch/dissem/bitmessage/entity/payload/Ack.java diff --git a/core/src/test/java/ch/dissem/bitmessage/DefaultMessageListenerTest.java b/core/src/test/java/ch/dissem/bitmessage/DefaultMessageListenerTest.java index 710bf98..e225924 100644 --- a/core/src/test/java/ch/dissem/bitmessage/DefaultMessageListenerTest.java +++ b/core/src/test/java/ch/dissem/bitmessage/DefaultMessageListenerTest.java @@ -24,9 +24,7 @@ import ch.dissem.bitmessage.entity.payload.Broadcast; import ch.dissem.bitmessage.entity.payload.GetPubkey; import ch.dissem.bitmessage.entity.payload.Msg; import ch.dissem.bitmessage.factory.Factory; -import ch.dissem.bitmessage.ports.AddressRepository; -import ch.dissem.bitmessage.ports.Labeler; -import ch.dissem.bitmessage.ports.MessageRepository; +import ch.dissem.bitmessage.ports.*; import ch.dissem.bitmessage.utils.Singleton; import ch.dissem.bitmessage.utils.TestBase; import ch.dissem.bitmessage.utils.TestUtils; @@ -51,6 +49,10 @@ public class DefaultMessageListenerTest extends TestBase { private AddressRepository addressRepo; @Mock private MessageRepository messageRepo; + @Mock + private Inventory inventory; + @Mock + private NetworkHandler networkHandler; private InternalContext ctx; private DefaultMessageListener listener; @@ -62,6 +64,8 @@ public class DefaultMessageListenerTest extends TestBase { Singleton.initialize(new BouncyCryptography()); when(ctx.getAddressRepository()).thenReturn(addressRepo); when(ctx.getMessageRepository()).thenReturn(messageRepo); + when(ctx.getInventory()).thenReturn(inventory); + when(ctx.getNetworkHandler()).thenReturn(networkHandler); listener = new DefaultMessageListener(ctx, mock(Labeler.class), mock(BitmessageContext.Listener.class)); } @@ -104,6 +108,7 @@ public class DefaultMessageListenerTest extends TestBase { public void ensureIncomingMessageIsSaved() throws Exception { BitmessageAddress identity = TestUtils.loadIdentity("BM-2cSqjfJ8xK6UUn5Rw3RpdGQ9RsDkBhWnS8"); BitmessageAddress contact = new BitmessageAddress(identity.getAddress()); + contact.setPubkey(identity.getPubkey()); when(addressRepo.getIdentities()).thenReturn(Collections.singletonList(identity));