Remove local author from contacts.

parent 421c9c44
Pipeline #3225 passed with stage
in 9 minutes and 30 seconds
......@@ -18,11 +18,10 @@ public interface ContactGroupFactory {
* Creates a group for the given client to share with the given contact.
*/
Group createContactGroup(ClientId clientId, int majorVersion,
Contact contact);
Contact contact, AuthorId local);
/**
* Creates a group for the given client to share between the given authors
* identified by their AuthorIds.
* Creates a group for the given client to share between the given authors.
*/
Group createContactGroup(ClientId clientId, int majorVersion,
AuthorId authorId1, AuthorId authorId2);
......
package org.briarproject.bramble.api.contact;
import org.briarproject.bramble.api.identity.Author;
import org.briarproject.bramble.api.identity.AuthorId;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import javax.annotation.Nullable;
......@@ -17,16 +16,14 @@ public class Contact {
private final ContactId id;
private final Author author;
private final AuthorId localAuthorId;
@Nullable
private final String alias;
@Nullable
private final byte[] handshakePublicKey;
private final boolean verified;
public Contact(ContactId id, Author author, AuthorId localAuthorId,
@Nullable String alias, @Nullable byte[] handshakePublicKey,
boolean verified) {
public Contact(ContactId id, Author author, @Nullable String alias,
@Nullable byte[] handshakePublicKey, boolean verified) {
if (alias != null) {
int aliasLength = toUtf8(alias).length;
if (aliasLength == 0 || aliasLength > MAX_AUTHOR_NAME_LENGTH)
......@@ -38,7 +35,6 @@ public class Contact {
}
this.id = id;
this.author = author;
this.localAuthorId = localAuthorId;
this.alias = alias;
this.handshakePublicKey = handshakePublicKey;
this.verified = verified;
......@@ -52,10 +48,6 @@ public class Contact {
return author;
}
public AuthorId getLocalAuthorId() {
return localAuthorId;
}
@Nullable
public String getAlias() {
return alias;
......
......@@ -24,33 +24,30 @@ public interface ContactManager {
void registerContactHook(ContactHook hook);
/**
* Stores a contact associated with the given local and remote pseudonyms,
* derives and stores transport keys for each transport, and returns an ID
* for the contact.
* Stores a contact with the given pseudonym, derives and stores transport
* keys for each transport, and returns an ID for the contact.
*
* @param alice true if the local party is Alice
*/
ContactId addContact(Transaction txn, Author remote, AuthorId local,
SecretKey rootKey, long timestamp, boolean alice, boolean verified,
boolean active) throws DbException;
ContactId addContact(Transaction txn, Author a, SecretKey rootKey,
long timestamp, boolean alice, boolean verified, boolean active)
throws DbException;
/**
* Stores a contact associated with the given local and remote pseudonyms
* and returns an ID for the contact.
* Stores a contact with the given pseudonym and returns an ID for the
* contact.
*/
ContactId addContact(Transaction txn, Author remote, AuthorId local,
boolean verified) throws DbException;
ContactId addContact(Transaction txn, Author a, boolean verified)
throws DbException;
/**
* Stores a contact associated with the given local and remote pseudonyms,
* derives and stores transport keys for each transport, and returns an ID
* for the contact.
* Stores a contact with the given pseudonym, derives and stores transport
* keys for each transport, and returns an ID for the contact.
*
* @param alice true if the local party is Alice
*/
ContactId addContact(Author remote, AuthorId local, SecretKey rootKey,
long timestamp, boolean alice, boolean verified, boolean active)
throws DbException;
ContactId addContact(Author a, SecretKey rootKey, long timestamp,
boolean alice, boolean verified, boolean active) throws DbException;
/**
* Returns the static link that needs to be sent to the contact to be added.
......@@ -88,22 +85,14 @@ public interface ContactManager {
Contact getContact(ContactId c) throws DbException;
/**
* Returns the contact with the given remoteAuthorId
* that was added by the LocalAuthor with the given localAuthorId
*
* @throws org.briarproject.bramble.api.db.NoSuchContactException
* Returns the contact with the given ID.
*/
Contact getContact(AuthorId remoteAuthorId, AuthorId localAuthorId)
throws DbException;
Contact getContact(AuthorId a) throws DbException;
/**
* Returns the contact with the given remoteAuthorId
* that was added by the LocalAuthor with the given localAuthorId
*
* @throws org.briarproject.bramble.api.db.NoSuchContactException
* Returns the contact with the given ID.
*/
Contact getContact(Transaction txn, AuthorId remoteAuthorId,
AuthorId localAuthorId) throws DbException;
Contact getContact(Transaction txn, AuthorId a) throws DbException;
/**
* Returns all active contacts.
......@@ -133,16 +122,14 @@ public interface ContactManager {
throws DbException;
/**
* Return true if a contact with this name and public key already exists
* Returns true if a contact with this pseudonym already exists.
*/
boolean contactExists(Transaction txn, AuthorId remoteAuthorId,
AuthorId localAuthorId) throws DbException;
boolean contactExists(Transaction txn, AuthorId a) throws DbException;
/**
* Return true if a contact with this name and public key already exists
* Returns true if a contact with this pseudonym already exists.
*/
boolean contactExists(AuthorId remoteAuthorId, AuthorId localAuthorId)
throws DbException;
boolean contactExists(AuthorId a) throws DbException;
/**
* Returns the {@link AuthorInfo} for the given author.
......
......@@ -102,11 +102,11 @@ public interface DatabaseComponent {
NullableDbCallable<R, E> task) throws DbException, E;
/**
* Stores a contact associated with the given local and remote pseudonyms,
* and returns an ID for the contact.
* Stores a contact with the given pseudonym and returns an ID for the
* contact.
*/
ContactId addContact(Transaction txn, Author remote, AuthorId local,
boolean verified) throws DbException;
ContactId addContact(Transaction txn, Author a, boolean verified)
throws DbException;
/**
* Stores a group.
......@@ -158,13 +158,11 @@ public interface DatabaseComponent {
TransportKeys k) throws DbException;
/**
* Returns true if the database contains the given contact for the given
* local pseudonym.
* Returns true if the database contains the given contact.
* <p/>
* Read-only.
*/
boolean containsContact(Transaction txn, AuthorId remote, AuthorId local)
throws DbException;
boolean containsContact(Transaction txn, AuthorId a) throws DbException;
/**
* Returns true if the database contains the given group.
......@@ -255,27 +253,18 @@ public interface DatabaseComponent {
Contact getContact(Transaction txn, ContactId c) throws DbException;
/**
* Returns all contacts.
* Returns the contact with the given author ID.
* <p/>
* Read-only.
*/
Collection<Contact> getContacts(Transaction txn) throws DbException;
Contact getContact(Transaction txn, AuthorId a) throws DbException;
/**
* Returns a possibly empty collection of contacts with the given author ID.
* <p/>
* Read-only.
*/
Collection<Contact> getContactsByAuthorId(Transaction txn, AuthorId remote)
throws DbException;
/**
* Returns all contacts associated with the given local pseudonym.
* Returns all contacts.
* <p/>
* Read-only.
*/
Collection<ContactId> getContacts(Transaction txn, AuthorId a)
throws DbException;
Collection<Contact> getContacts(Transaction txn) throws DbException;
/**
* Returns the group with the given ID.
......
......@@ -163,19 +163,15 @@ public class TestUtils {
}
public static Contact getContact() {
return getContact(getAuthor(), new AuthorId(getRandomId()),
random.nextBoolean());
return getContact(getAuthor(), random.nextBoolean());
}
public static Contact getContact(Author remote, AuthorId local,
boolean verified) {
return getContact(getContactId(), remote, local, verified);
public static Contact getContact(Author a, boolean verified) {
return getContact(getContactId(), a, verified);
}
public static Contact getContact(ContactId c, Author remote, AuthorId local,
boolean verified) {
return new Contact(c, remote, local,
getRandomString(MAX_AUTHOR_NAME_LENGTH),
public static Contact getContact(ContactId c, Author a, boolean verified) {
return new Contact(c, a, getRandomString(MAX_AUTHOR_NAME_LENGTH),
getRandomBytes(MAX_PUBLIC_KEY_LENGTH), verified);
}
......
......@@ -39,8 +39,7 @@ class ContactGroupFactoryImpl implements ContactGroupFactory {
@Override
public Group createContactGroup(ClientId clientId, int majorVersion,
Contact contact) {
AuthorId local = contact.getLocalAuthorId();
Contact contact, AuthorId local) {
AuthorId remote = contact.getAuthor().getId();
byte[] descriptor = createGroupDescriptor(local, remote);
return groupFactory.createGroup(clientId, majorVersion, descriptor);
......
......@@ -293,8 +293,7 @@ class ContactExchangeTaskImpl extends Thread implements ContactExchangeTask {
throws DbException {
return db.transactionWithResult(false, txn -> {
ContactId contactId = contactManager.addContact(txn, remoteAuthor,
localAuthor.getId(), masterKey, timestamp, alice,
true, true);
masterKey, timestamp, alice, true, true);
transportPropertyManager.addRemoteProperties(txn, contactId,
remoteProperties);
return contactId;
......
......@@ -8,7 +8,6 @@ import org.briarproject.bramble.api.contact.PendingContactId;
import org.briarproject.bramble.api.crypto.SecretKey;
import org.briarproject.bramble.api.db.DatabaseComponent;
import org.briarproject.bramble.api.db.DbException;
import org.briarproject.bramble.api.db.NoSuchContactException;
import org.briarproject.bramble.api.db.Transaction;
import org.briarproject.bramble.api.identity.Author;
import org.briarproject.bramble.api.identity.AuthorId;
......@@ -68,10 +67,10 @@ class ContactManagerImpl implements ContactManager {
}
@Override
public ContactId addContact(Transaction txn, Author remote, AuthorId local,
SecretKey rootKey, long timestamp, boolean alice, boolean verified,
boolean active) throws DbException {
ContactId c = db.addContact(txn, remote, local, verified);
public ContactId addContact(Transaction txn, Author a, SecretKey rootKey,
long timestamp, boolean alice, boolean verified, boolean active)
throws DbException {
ContactId c = db.addContact(txn, a, verified);
keyManager.addContact(txn, c, rootKey, timestamp, alice, active);
Contact contact = db.getContact(txn, c);
for (ContactHook hook : hooks) hook.addingContact(txn, contact);
......@@ -79,20 +78,20 @@ class ContactManagerImpl implements ContactManager {
}
@Override
public ContactId addContact(Transaction txn, Author remote, AuthorId local,
boolean verified) throws DbException {
ContactId c = db.addContact(txn, remote, local, verified);
public ContactId addContact(Transaction txn, Author a, boolean verified)
throws DbException {
ContactId c = db.addContact(txn, a, verified);
Contact contact = db.getContact(txn, c);
for (ContactHook hook : hooks) hook.addingContact(txn, contact);
return c;
}
@Override
public ContactId addContact(Author remote, AuthorId local,
SecretKey rootKey, long timestamp, boolean alice, boolean verified,
boolean active) throws DbException {
public ContactId addContact(Author a, SecretKey rootKey, long timestamp,
boolean alice, boolean verified, boolean active)
throws DbException {
return db.transactionWithResult(false, txn ->
addContact(txn, remote, local, rootKey, timestamp, alice,
addContact(txn, a, rootKey, timestamp, alice,
verified, active));
}
......@@ -144,23 +143,13 @@ class ContactManagerImpl implements ContactManager {
}
@Override
public Contact getContact(AuthorId remoteAuthorId, AuthorId localAuthorId)
throws DbException {
return db.transactionWithResult(true, txn ->
getContact(txn, remoteAuthorId, localAuthorId));
public Contact getContact(AuthorId a) throws DbException {
return db.transactionWithResult(true, txn -> getContact(txn, a));
}
@Override
public Contact getContact(Transaction txn, AuthorId remoteAuthorId,
AuthorId localAuthorId) throws DbException {
Collection<Contact> contacts =
db.getContactsByAuthorId(txn, remoteAuthorId);
for (Contact c : contacts) {
if (c.getLocalAuthorId().equals(localAuthorId)) {
return c;
}
}
throw new NoSuchContactException();
public Contact getContact(Transaction txn, AuthorId a) throws DbException {
return db.getContact(txn, a);
}
@Override
......@@ -191,16 +180,14 @@ class ContactManagerImpl implements ContactManager {
}
@Override
public boolean contactExists(Transaction txn, AuthorId remoteAuthorId,
AuthorId localAuthorId) throws DbException {
return db.containsContact(txn, remoteAuthorId, localAuthorId);
public boolean contactExists(Transaction txn, AuthorId a)
throws DbException {
return db.containsContact(txn, a);
}
@Override
public boolean contactExists(AuthorId remoteAuthorId,
AuthorId localAuthorId) throws DbException {
return db.transactionWithResult(true, txn ->
contactExists(txn, remoteAuthorId, localAuthorId));
public boolean contactExists(AuthorId a) throws DbException {
return db.transactionWithResult(true, txn -> contactExists(txn, a));
}
@Override
......@@ -222,12 +209,12 @@ class ContactManagerImpl implements ContactManager {
LocalAuthor localAuthor = identityManager.getLocalAuthor(txn);
if (localAuthor.getId().equals(authorId))
return new AuthorInfo(OURSELVES);
Collection<Contact> contacts = db.getContactsByAuthorId(txn, authorId);
if (contacts.isEmpty()) return new AuthorInfo(UNKNOWN);
if (contacts.size() > 1) throw new AssertionError();
Contact c = contacts.iterator().next();
if (c.isVerified()) return new AuthorInfo(VERIFIED, c.getAlias());
else return new AuthorInfo(UNVERIFIED, c.getAlias());
if (db.containsContact(txn, authorId)) {
Contact c = db.getContact(txn, authorId);
if (c.isVerified()) return new AuthorInfo(VERIFIED, c.getAlias());
else return new AuthorInfo(UNVERIFIED, c.getAlias());
}
return new AuthorInfo(UNKNOWN);
}
}
......@@ -87,11 +87,10 @@ interface Database<T> {
void commitTransaction(T txn) throws DbException;
/**
* Stores a contact associated with the given local and remote pseudonyms,
* and returns an ID for the contact.
* Stores a contact with the given pseudonym and returns an ID for the
* contact.
*/
ContactId addContact(T txn, Author remote, AuthorId local, boolean verified)
throws DbException;
ContactId addContact(T txn, Author a, boolean verified) throws DbException;
/**
* Stores a group.
......@@ -164,13 +163,11 @@ interface Database<T> {
throws DbException;
/**
* Returns true if the database contains the given contact for the given
* local pseudonym.
* Returns true if the database contains the given contact.
* <p/>
* Read-only.
*/
boolean containsContact(T txn, AuthorId remote, AuthorId local)
throws DbException;
boolean containsContact(T txn, AuthorId a) throws DbException;
/**
* Returns true if the database contains the given contact.
......@@ -253,26 +250,18 @@ interface Database<T> {
Contact getContact(T txn, ContactId c) throws DbException;
/**
* Returns all contacts.
* <p/>
* Read-only.
*/
Collection<Contact> getContacts(T txn) throws DbException;
/**
* Returns a possibly empty collection of contacts with the given author ID.
* Returns the contact with the given author ID.
* <p/>
* Read-only.
*/
Collection<Contact> getContactsByAuthorId(T txn, AuthorId remote)
throws DbException;
Contact getContact(T txn, AuthorId a) throws DbException;
/**
* Returns all contacts associated with the given local pseudonym.
* Returns all contacts.
* <p/>
* Read-only.
*/
Collection<ContactId> getContacts(T txn, AuthorId a) throws DbException;
Collection<Contact> getContacts(T txn) throws DbException;
/**
* Returns the group with the given ID.
......
......@@ -232,18 +232,15 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
}
@Override
public ContactId addContact(Transaction transaction, Author remote,
AuthorId local, boolean verified)
throws DbException {
public ContactId addContact(Transaction transaction, Author a,
boolean verified) throws DbException {
if (transaction.isReadOnly()) throw new IllegalArgumentException();
T txn = unbox(transaction);
if (!db.containsLocalAuthor(txn, local))
throw new NoSuchLocalAuthorException();
if (db.containsLocalAuthor(txn, remote.getId()))
if (db.containsLocalAuthor(txn, a.getId()))
throw new ContactExistsException();
if (db.containsContact(txn, remote.getId(), local))
if (db.containsContact(txn, a.getId()))
throw new ContactExistsException();
ContactId c = db.addContact(txn, remote, local, verified);
ContactId c = db.addContact(txn, a, verified);
transaction.attach(new ContactAddedEvent(c));
return c;
}
......@@ -342,12 +339,10 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
}
@Override
public boolean containsContact(Transaction transaction, AuthorId remote,
AuthorId local) throws DbException {
public boolean containsContact(Transaction transaction, AuthorId a)
throws DbException {
T txn = unbox(transaction);
if (!db.containsLocalAuthor(txn, local))
throw new NoSuchLocalAuthorException();
return db.containsContact(txn, remote, local);
return db.containsContact(txn, a);
}
@Override
......@@ -488,26 +483,19 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
}
@Override
public Collection<Contact> getContacts(Transaction transaction)
public Contact getContact(Transaction transaction, AuthorId a)
throws DbException {
T txn = unbox(transaction);
return db.getContacts(txn);
}
@Override
public Collection<Contact> getContactsByAuthorId(Transaction transaction,
AuthorId remote) throws DbException {
T txn = unbox(transaction);
return db.getContactsByAuthorId(txn, remote);
if (!db.containsContact(txn, a))
throw new NoSuchContactException();
return db.getContact(txn, a);
}
@Override
public Collection<ContactId> getContacts(Transaction transaction,
AuthorId a) throws DbException {
public Collection<Contact> getContacts(Transaction transaction)
throws DbException {
T txn = unbox(transaction);
if (!db.containsLocalAuthor(txn, a))
throw new NoSuchLocalAuthorException();
return db.getContacts(txn, a);
return db.getContacts(txn);
}
@Override
......
......@@ -92,7 +92,7 @@ import static org.briarproject.bramble.util.LogUtils.now;
abstract class JdbcDatabase implements Database<Connection> {
// Package access for testing
static final int CODE_SCHEMA_VERSION = 43;
static final int CODE_SCHEMA_VERSION = 44;
// Time period offsets for incoming transport keys
private static final int OFFSET_PREV = -1;
......@@ -127,12 +127,8 @@ abstract class JdbcDatabase implements Database<Connection> {
+ " alias _STRING," // Null if no alias has been set
+ " publicKey _BINARY NOT NULL,"
+ " handshakePublicKey _BINARY," // Null if key is unknown
+ " localAuthorId _HASH NOT NULL,"
+ " verified BOOLEAN NOT NULL,"
+ " PRIMARY KEY (contactId),"
+ " FOREIGN KEY (localAuthorId)"
+ " REFERENCES localAuthors (authorId)"
+ " ON DELETE CASCADE)";
+ " PRIMARY KEY (contactId))";
private static final String CREATE_GROUPS =
"CREATE TABLE groups"
......@@ -486,7 +482,8 @@ abstract class JdbcDatabase implements Database<Connection> {
new Migration39_40(),
new Migration40_41(dbTypes),
new Migration41_42(dbTypes),
new Migration42_43(dbTypes)
new Migration42_43(dbTypes),
new Migration43_44()
);
}
......@@ -662,23 +659,21 @@ abstract class JdbcDatabase implements Database<Connection> {
}
@Override
public ContactId addContact(Connection txn, Author remote, AuthorId local,
boolean verified) throws DbException {
public ContactId addContact(Connection txn, Author a, boolean verified)
throws DbException {
PreparedStatement ps = null;
ResultSet rs = null;
try {
// Create a contact row
String sql = "INSERT INTO contacts"
+ " (authorId, formatVersion, name, publicKey,"
+ " localAuthorId, verified)"
+ " VALUES (?, ?, ?, ?, ?, ?)";
ps = txn.prepareStatement(sql);
ps.setBytes(1, remote.getId().getBytes());
ps.setInt(2, remote.getFormatVersion());
ps.setString(3, remote.getName());
ps.setBytes(4, remote.getPublicKey());
ps.setBytes(5, local.getBytes());
ps.setBoolean(6, verified);
+ " (authorId, formatVersion, name, publicKey, verified)"
+ " VALUES (?, ?, ?, ?, ?)";
ps = txn.prepareStatement(sql);
ps.setBytes(1, a.getId().getBytes());
ps.setInt(2, a.getFormatVersion());
ps.setString(3, a.getName());
ps.setBytes(4, a.getPublicKey());
ps.setBoolean(5, verified);
int affected = ps.executeUpdate();
if (affected != 1) throw new DbStateException();
ps.close();
......@@ -1180,16 +1175,14 @@ abstract class JdbcDatabase implements Database<Connection> {
}
@Override
public boolean containsContact(Connection txn, AuthorId remote,
AuthorId local) throws DbException {
public boolean containsContact(Connection txn, AuthorId a)
throws DbException {
PreparedStatement ps = null;
ResultSet rs = null;
try {
String sql = "SELECT NULL FROM contacts"
+ " WHERE authorId = ? AND localAuthorId = ?";
String sql = "SELECT NULL FROM contacts WHERE authorId = ?";
ps = txn.prepareStatement(sql);
ps.setBytes(1, remote.getBytes());
ps.setBytes(2, local.getBytes());
ps.setBytes(1, a.getBytes());
rs = ps.executeQuery();
boolean found = rs.next();
if (rs.next()) throw new DbStateException();
......@@ -1432,7 +1425,7 @@ abstract class JdbcDatabase implements Database<Connection> {
ResultSet rs = null;
try {