diff --git a/bramble-api/src/main/java/org/briarproject/bramble/api/db/DatabaseComponent.java b/bramble-api/src/main/java/org/briarproject/bramble/api/db/DatabaseComponent.java index 27e379571ee7617efd65460bb0aed2bf27164dbb..ea05938d49c5a24fe0d0da14bd40d718e2605824 100644 --- a/bramble-api/src/main/java/org/briarproject/bramble/api/db/DatabaseComponent.java +++ b/bramble-api/src/main/java/org/briarproject/bramble/api/db/DatabaseComponent.java @@ -407,11 +407,10 @@ public interface DatabaseComponent { throws DbException; /** - * Increments the outgoing stream counter for the given contact and - * transport in the given rotation period . + * Increments the outgoing stream counter for the given transport keys. */ - void incrementStreamCounter(Transaction txn, ContactId c, TransportId t, - long rotationPeriod) throws DbException; + void incrementStreamCounter(Transaction txn, TransportId t, KeySetId k) + throws DbException; /** * Merges the given metadata with the existing metadata for the given diff --git a/bramble-core/src/main/java/org/briarproject/bramble/db/Database.java b/bramble-core/src/main/java/org/briarproject/bramble/db/Database.java index ddd4860c2c638a34282aa92c2d9f5101a9b496bb..dcf96ea4e116773c97ca99d7ff27b8d474ecd53a 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/db/Database.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/db/Database.java @@ -499,11 +499,10 @@ interface Database<T> { throws DbException; /** - * Increments the outgoing stream counter for the given contact and - * transport in the given rotation period. + * Increments the outgoing stream counter for the given transport keys. */ - void incrementStreamCounter(T txn, ContactId c, TransportId t, - long rotationPeriod) throws DbException; + void incrementStreamCounter(T txn, TransportId t, KeySetId k) + throws DbException; /** * Marks the given messages as not needing to be acknowledged to the diff --git a/bramble-core/src/main/java/org/briarproject/bramble/db/DatabaseComponentImpl.java b/bramble-core/src/main/java/org/briarproject/bramble/db/DatabaseComponentImpl.java index a9748020058df7d2eec5901b6d4ff93125e8ecae..00f0bb62292c029505e98fd07b4496f81b2a6d42 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/db/DatabaseComponentImpl.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/db/DatabaseComponentImpl.java @@ -607,15 +607,13 @@ class DatabaseComponentImpl<T> implements DatabaseComponent { } @Override - public void incrementStreamCounter(Transaction transaction, ContactId c, - TransportId t, long rotationPeriod) throws DbException { + public void incrementStreamCounter(Transaction transaction, TransportId t, + KeySetId k) throws DbException { if (transaction.isReadOnly()) throw new IllegalArgumentException(); T txn = unbox(transaction); - if (!db.containsContact(txn, c)) - throw new NoSuchContactException(); if (!db.containsTransport(txn, t)) throw new NoSuchTransportException(); - db.incrementStreamCounter(txn, c, t, rotationPeriod); + db.incrementStreamCounter(txn, t, k); } @Override diff --git a/bramble-core/src/main/java/org/briarproject/bramble/db/JdbcDatabase.java b/bramble-core/src/main/java/org/briarproject/bramble/db/JdbcDatabase.java index 953a0f1604d5546d87749e0b521d6e4e2bfd13c7..dd5874f6edcca3400a2922d3f1feb9e2db5f2b2f 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/db/JdbcDatabase.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/db/JdbcDatabase.java @@ -2198,17 +2198,15 @@ abstract class JdbcDatabase implements Database<Connection> { } @Override - public void incrementStreamCounter(Connection txn, ContactId c, - TransportId t, long rotationPeriod) throws DbException { + public void incrementStreamCounter(Connection txn, TransportId t, + KeySetId k) throws DbException { PreparedStatement ps = null; try { String sql = "UPDATE outgoingKeys SET stream = stream + 1" - + " WHERE contactId = ? AND transportId = ?" - + " AND rotationPeriod = ?"; + + " WHERE transportId = ? AND keySetId = ?"; ps = txn.prepareStatement(sql); - ps.setInt(1, c.getInt()); - ps.setString(2, t.getString()); - ps.setLong(3, rotationPeriod); + ps.setString(1, t.getString()); + ps.setInt(2, k.getInt()); int affected = ps.executeUpdate(); if (affected != 1) throw new DbStateException(); ps.close(); diff --git a/bramble-core/src/main/java/org/briarproject/bramble/transport/TransportKeyManagerImpl.java b/bramble-core/src/main/java/org/briarproject/bramble/transport/TransportKeyManagerImpl.java index d7ccb49c26ea1ca2e63438ca48abff073b316492..f6abc1c542e75d106479f5f8a7eae35b7ad3303f 100644 --- a/bramble-core/src/main/java/org/briarproject/bramble/transport/TransportKeyManagerImpl.java +++ b/bramble-core/src/main/java/org/briarproject/bramble/transport/TransportKeyManagerImpl.java @@ -288,6 +288,7 @@ class TransportKeyManagerImpl implements TransportKeyManager { if (ks == null) return null; MutableOutgoingKeys outKeys = ks.getTransportKeys().getCurrentOutgoingKeys(); + if (!outKeys.isActive()) throw new AssertionError(); if (outKeys.getStreamCounter() > MAX_32_BIT_UNSIGNED) return null; // Create a stream context StreamContext ctx = new StreamContext(c, transportId, @@ -295,8 +296,7 @@ class TransportKeyManagerImpl implements TransportKeyManager { outKeys.getStreamCounter()); // Increment the stream counter and write it back to the DB outKeys.incrementStreamCounter(); - db.incrementStreamCounter(txn, c, transportId, - outKeys.getRotationPeriod()); + db.incrementStreamCounter(txn, transportId, ks.getKeySetId()); return ctx; } finally { lock.unlock(); diff --git a/bramble-core/src/test/java/org/briarproject/bramble/db/DatabaseComponentImplTest.java b/bramble-core/src/test/java/org/briarproject/bramble/db/DatabaseComponentImplTest.java index d8f1f59fbf2898bdae78c50d7b9b3e85d630c905..f0149bb248948d6bb51d7bd1693a741407d464cc 100644 --- a/bramble-core/src/test/java/org/briarproject/bramble/db/DatabaseComponentImplTest.java +++ b/bramble-core/src/test/java/org/briarproject/bramble/db/DatabaseComponentImplTest.java @@ -285,11 +285,11 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase { throws Exception { context.checking(new Expectations() {{ // Check whether the contact is in the DB (which it's not) - exactly(18).of(database).startTransaction(); + exactly(17).of(database).startTransaction(); will(returnValue(txn)); - exactly(18).of(database).containsContact(txn, contactId); + exactly(17).of(database).containsContact(txn, contactId); will(returnValue(false)); - exactly(18).of(database).abortTransaction(txn); + exactly(17).of(database).abortTransaction(txn); }}); DatabaseComponent db = createDatabaseComponent(database, eventBus, shutdown); @@ -384,16 +384,6 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase { db.endTransaction(transaction); } - transaction = db.startTransaction(false); - try { - db.incrementStreamCounter(transaction, contactId, transportId, 0); - fail(); - } catch (NoSuchContactException expected) { - // Expected - } finally { - db.endTransaction(transaction); - } - transaction = db.startTransaction(false); try { db.getGroupVisibility(transaction, contactId, groupId); @@ -781,7 +771,7 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase { // Check whether the transport is in the DB (which it's not) exactly(6).of(database).startTransaction(); will(returnValue(txn)); - exactly(2).of(database).containsContact(txn, contactId); + oneOf(database).containsContact(txn, contactId); will(returnValue(true)); exactly(6).of(database).containsTransport(txn, transportId); will(returnValue(false)); @@ -822,7 +812,7 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase { transaction = db.startTransaction(false); try { - db.incrementStreamCounter(transaction, contactId, transportId, 0); + db.incrementStreamCounter(transaction, transportId, keySetId); fail(); } catch (NoSuchTransportException expected) { // Expected diff --git a/bramble-core/src/test/java/org/briarproject/bramble/db/JdbcDatabaseTest.java b/bramble-core/src/test/java/org/briarproject/bramble/db/JdbcDatabaseTest.java index 9e4047982d87a4a583a52120033133d421806a99..157b6902be00063bd0215b5b8a7dd011f9cc2837 100644 --- a/bramble-core/src/test/java/org/briarproject/bramble/db/JdbcDatabaseTest.java +++ b/bramble-core/src/test/java/org/briarproject/bramble/db/JdbcDatabaseTest.java @@ -825,8 +825,8 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase { singletonList(new KeySet(keySetId, contactId, keys))); // Increment the stream counter twice and retrieve the transport keys - db.incrementStreamCounter(txn, contactId, transportId, rotationPeriod); - db.incrementStreamCounter(txn, contactId, transportId, rotationPeriod); + db.incrementStreamCounter(txn, transportId, keySetId); + db.incrementStreamCounter(txn, transportId, keySetId); Collection<KeySet> newKeys = db.getTransportKeys(txn, transportId); assertEquals(1, newKeys.size()); KeySet ks = newKeys.iterator().next(); diff --git a/bramble-core/src/test/java/org/briarproject/bramble/transport/TransportKeyManagerImplTest.java b/bramble-core/src/test/java/org/briarproject/bramble/transport/TransportKeyManagerImplTest.java index 8da8eefaa4ea88492af1e067a370ec1c0e99d759..df708df91a4ffb82f0452f5265a866f38018bb5c 100644 --- a/bramble-core/src/test/java/org/briarproject/bramble/transport/TransportKeyManagerImplTest.java +++ b/bramble-core/src/test/java/org/briarproject/bramble/transport/TransportKeyManagerImplTest.java @@ -227,7 +227,7 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase { context.checking(new Expectations() {{ // Increment the stream counter - oneOf(db).incrementStreamCounter(txn, contactId, transportId, 1000); + oneOf(db).incrementStreamCounter(txn, transportId, keySetId); }}); TransportKeyManager transportKeyManager = new TransportKeyManagerImpl( @@ -441,7 +441,7 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase { // Activate the keys oneOf(db).setTransportKeysActive(txn, transportId, keySetId); // Increment the stream counter - oneOf(db).incrementStreamCounter(txn, contactId, transportId, 1000); + oneOf(db).incrementStreamCounter(txn, transportId, keySetId); }}); TransportKeyManager transportKeyManager = new TransportKeyManagerImpl(