ProofOfWorkService implements ProofOfWorkEngine.Callback, InternalContext.ContextHolder { @Override public void onNonceCalculated(byte[] initialHash, byte[] nonce) { Item item = powRepo.getItem(initialHash); if (item.message == null) { ObjectMessage object = powRepo.getItem(initialHash).object; object.setNonce(nonce); Plaintext plaintext = messageRepo.getMessage(initialHash); if (plaintext != null) { plaintext.setInventoryVector(object.getInventoryVector()); plaintext.updateNextTry(); ctx.getLabeler().markAsSent(plaintext); messageRepo.save(plaintext); } try { ctx.getNetworkListener().receive(object); } catch (IOException e) { LOG.debug(e.getMessage(), e); } ctx.getInventory().storeObject(object); ctx.getNetworkHandler().offer(object.getInventoryVector()); } else { item.message.getAckMessage().setNonce(nonce); final ObjectMessage object = new ObjectMessage.Builder() .stream(item.message.getStream()) .expiresTime(item.expirationTime) .payload(new Msg(item.message)) .build(); if (object.isSigned()) { object.sign(item.message.getFrom().getPrivateKey()); } if (object.getPayload() instanceof Encrypted) { object.encrypt(item.message.getTo().getPubkey()); } doProofOfWork(item.message.getTo(), object); } powRepo.removeObject(initialHash); }  void doMissingProofOfWork(long delayInMilliseconds); void doProofOfWork(ObjectMessage object); void doProofOfWork(BitmessageAddress recipient, ObjectMessage object); void doProofOfWorkWithAck(Plaintext plaintext, long expirationTime); @Override void onNonceCalculated(byte[] initialHash, byte[] nonce); @Override void setContext(InternalContext ctx);  }
@Test public void ensureCalculatedNonceIsStored() throws Exception { BitmessageAddress identity = TestUtils.loadIdentity("BM-2cSqjfJ8xK6UUn5Rw3RpdGQ9RsDkBhWnS8"); BitmessageAddress address = TestUtils.loadContact(); Plaintext plaintext = new Plaintext.Builder(MSG).from(identity).to(address).message("", "").build(); ObjectMessage object = new ObjectMessage.Builder() .payload(new Msg(plaintext)) .build(); object.sign(identity.getPrivateKey()); object.encrypt(address.getPubkey()); byte[] initialHash = new byte[64]; byte[] nonce = new byte[]{1, 2, 3, 4, 5, 6, 7, 8}; when(proofOfWorkRepo.getItem(initialHash)).thenReturn(new ProofOfWorkRepository.Item(object, 1001, 1002)); when(messageRepo.getMessage(initialHash)).thenReturn(plaintext); proofOfWorkService.onNonceCalculated(initialHash, nonce); verify(proofOfWorkRepo).removeObject(eq(initialHash)); verify(inventory).storeObject(eq(object)); verify(networkHandler).offer(eq(object.getInventoryVector())); assertThat(plaintext.getInventoryVector(), equalTo(object.getInventoryVector())); }