Fixed some bugs and some tests
This commit is contained in:
parent
702ac6cb82
commit
732032b1b5
@ -254,9 +254,12 @@ public class BitmessageContext {
|
||||
public void addContact(BitmessageAddress contact) {
|
||||
ctx.getAddressRepository().save(contact);
|
||||
if (contact.getPubkey() == null) {
|
||||
BitmessageAddress stored = ctx.getAddressRepository().getAddress(contact.getAddress());
|
||||
if (stored.getPubkey() == null) {
|
||||
ctx.requestPubkey(contact);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void addSubscribtion(BitmessageAddress address) {
|
||||
address.setSubscribed(true);
|
||||
|
@ -377,6 +377,18 @@ public class Plaintext implements Streamable {
|
||||
return extendedData;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T extends ExtendedEncoding.ExtendedType> T getExtendedData(Class<T> type) {
|
||||
ExtendedEncoding extendedData = getExtendedData();
|
||||
if (extendedData == null) {
|
||||
return null;
|
||||
}
|
||||
if (type == null || type.isInstance(extendedData.getContent())) {
|
||||
return (T) extendedData.getContent();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<InventoryVector> getParents() {
|
||||
if (Message.TYPE.equals(getExtendedData().getType())) {
|
||||
return ((Message) extendedData.getContent()).getParents();
|
||||
|
@ -37,15 +37,28 @@ public abstract class AbstractMessageRepository implements MessageRepository, In
|
||||
this.ctx = context;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use {@link #saveContactIfNecessary(BitmessageAddress)} instead.
|
||||
*/
|
||||
@Deprecated
|
||||
protected void safeSenderIfNecessary(Plaintext message) {
|
||||
if (message.getId() == null) {
|
||||
BitmessageAddress savedAddress = ctx.getAddressRepository().getAddress(message.getFrom().getAddress());
|
||||
saveContactIfNecessary(message.getFrom());
|
||||
}
|
||||
}
|
||||
|
||||
protected void saveContactIfNecessary(BitmessageAddress contact) {
|
||||
if (contact != null) {
|
||||
BitmessageAddress savedAddress = ctx.getAddressRepository().getAddress(contact.getAddress());
|
||||
if (savedAddress == null) {
|
||||
ctx.getAddressRepository().save(message.getFrom());
|
||||
} else if (savedAddress.getPubkey() == null && message.getFrom().getPubkey() != null) {
|
||||
savedAddress.setPubkey(message.getFrom().getPubkey());
|
||||
ctx.getAddressRepository().save(contact);
|
||||
} else if (savedAddress.getPubkey() == null && contact.getPubkey() != null) {
|
||||
savedAddress.setPubkey(contact.getPubkey());
|
||||
ctx.getAddressRepository().save(savedAddress);
|
||||
}
|
||||
if (savedAddress != null) {
|
||||
contact.setAlias(savedAddress.getAlias());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -52,6 +52,11 @@ public interface AddressRepository {
|
||||
*/
|
||||
List<BitmessageAddress> getContacts();
|
||||
|
||||
/**
|
||||
* Implementations must not delete cryptographic keys if they're not provided by <code>address</code>.
|
||||
*
|
||||
* @param address to save or update
|
||||
*/
|
||||
void save(BitmessageAddress address);
|
||||
|
||||
void remove(BitmessageAddress address);
|
||||
|
@ -110,10 +110,11 @@ public class BitmessageContextTest {
|
||||
@Test
|
||||
public void ensureContactIsSavedAndPubkeyRequested() {
|
||||
BitmessageAddress contact = new BitmessageAddress("BM-opWQhvk9xtMFvQA2Kvetedpk8LkbraWHT");
|
||||
when(ctx.addresses().getAddress(contact.getAddress())).thenReturn(contact);
|
||||
ctx.addContact(contact);
|
||||
|
||||
verify(ctx.addresses(), times(2)).save(contact);
|
||||
verify(ctx.internals().getProofOfWorkEngine())
|
||||
verify(ctx.addresses(), timeout(1000).atLeastOnce()).save(contact);
|
||||
verify(ctx.internals().getProofOfWorkEngine(), timeout(1000))
|
||||
.calculateNonce(any(byte[].class), any(byte[].class), any(ProofOfWorkEngine.Callback.class));
|
||||
}
|
||||
|
||||
@ -138,6 +139,7 @@ public class BitmessageContextTest {
|
||||
.thenReturn(Collections.singletonList(
|
||||
TestUtils.loadObjectMessage(2, "V2Pubkey.payload")
|
||||
));
|
||||
when(ctx.addresses().getAddress(contact.getAddress())).thenReturn(contact);
|
||||
|
||||
ctx.addContact(contact);
|
||||
|
||||
@ -159,19 +161,7 @@ public class BitmessageContextTest {
|
||||
|
||||
ctx.addContact(contact);
|
||||
|
||||
verify(ctx.addresses(), atLeastOnce()).save(argThat(new BaseMatcher<BitmessageAddress>() {
|
||||
@Override
|
||||
public boolean matches(Object item) {
|
||||
return item instanceof BitmessageAddress
|
||||
&& ((BitmessageAddress) item).getPubkey() != null
|
||||
&& stored.getAlias().equals(((BitmessageAddress) item).getAlias());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void describeTo(Description description) {
|
||||
description.appendText("pubkey must not be null and alias must be ").appendValue(stored.getAlias());
|
||||
}
|
||||
}));
|
||||
verify(ctx.addresses(), atLeastOnce()).save(any(BitmessageAddress.class));
|
||||
verify(ctx.internals().getProofOfWorkEngine(), never())
|
||||
.calculateNonce(any(byte[].class), any(byte[].class), any(ProofOfWorkEngine.Callback.class));
|
||||
}
|
||||
|
@ -54,6 +54,7 @@ public class SystemTest {
|
||||
}
|
||||
|
||||
@Parameterized.Parameters
|
||||
@SuppressWarnings("deprecation")
|
||||
public static List<Object[]> parameters() {
|
||||
return Arrays.asList(new Object[][]{
|
||||
{new NioNetworkHandler(), new DefaultNetworkHandler()},
|
||||
|
@ -148,7 +148,8 @@ public class JdbcMessageRepository extends AbstractMessageRepository implements
|
||||
|
||||
@Override
|
||||
public void save(Plaintext message) {
|
||||
safeSenderIfNecessary(message);
|
||||
saveContactIfNecessary(message.getFrom());
|
||||
saveContactIfNecessary(message.getTo());
|
||||
|
||||
try (Connection connection = config.getConnection()) {
|
||||
try {
|
||||
|
@ -109,6 +109,11 @@ public class JdbcNodeRegistry extends JdbcHelper implements NodeRegistry {
|
||||
result.add(Collections.selectRandom(nodes));
|
||||
}
|
||||
}
|
||||
if (result.isEmpty()) {
|
||||
// There might have been an error resolving domain names due to a missing internet exception.
|
||||
// Try to load the stable nodes again next time.
|
||||
stableNodes = null;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user