JdbcProofOfWorkRepository extends JdbcHelper implements ProofOfWorkRepository, InternalContext.ContextHolder { @Override public Item getItem(byte[] initialHash) { try ( Connection connection = config.getConnection(); PreparedStatement ps = connection.prepareStatement("SELECT data, version, nonce_trials_per_byte, " + "extra_bytes, expiration_time, message_id FROM POW WHERE initial_hash=?") ) { ps.setBytes(1, initialHash); try (ResultSet rs = ps.executeQuery()) { if (rs.next()) { Blob data = rs.getBlob("data"); if (rs.getObject("message_id") == null) { return new Item( Factory.getObjectMessage(rs.getInt("version"), data.getBinaryStream(), (int) data.length()), rs.getLong("nonce_trials_per_byte"), rs.getLong("extra_bytes") ); } else { return new Item( Factory.getObjectMessage(rs.getInt("version"), data.getBinaryStream(), (int) data.length()), rs.getLong("nonce_trials_per_byte"), rs.getLong("extra_bytes"), rs.getLong("expiration_time"), ctx.getMessageRepository().getMessage(rs.getLong("message_id")) ); } } else { throw new IllegalArgumentException("Object requested that we don't have. Initial hash: " + Strings.hex(initialHash)); } } } catch (SQLException e) { LOG.error(e.getMessage(), e); throw new ApplicationException(e); } } JdbcProofOfWorkRepository(JdbcConfig config); @Override Item getItem(byte[] initialHash); @Override List<byte[]> getItems(); @Override void putObject(Item item); @Override void putObject(ObjectMessage object, long nonceTrialsPerByte, long extraBytes); @Override void removeObject(byte[] initialHash); @Override void setContext(InternalContext context);  }
@Test public void ensureItemCanBeRetrieved() { Item item = repo.getItem(initialHash1); assertThat(item, notNullValue()); assertThat(item.object.getPayload(), instanceOf(GetPubkey.class)); assertThat(item.nonceTrialsPerByte, is(1000L)); assertThat(item.extraBytes, is(1000L)); }
@Test public void ensureAckItemCanBeRetrieved() { Item item = repo.getItem(initialHash2); assertThat(item, notNullValue()); assertThat(item.object.getPayload(), instanceOf(GenericPayload.class)); assertThat(item.nonceTrialsPerByte, is(1000L)); assertThat(item.extraBytes, is(1000L)); assertThat(item.expirationTime, not(0)); assertThat(item.message, notNullValue()); assertThat(item.message.getFrom().getPrivateKey(), notNullValue()); assertThat(item.message.getTo().getPubkey(), notNullValue()); }
@Test(expected = RuntimeException.class) public void ensureRetrievingNonexistingItemThrowsException() { repo.getItem(new byte[0]); }