From 798b871cc916edeead0013bd79a29a090e990a5b Mon Sep 17 00:00:00 2001 From: akwizgran <michael@briarproject.org> Date: Wed, 28 Mar 2018 11:52:14 +0100 Subject: [PATCH] Use key set ID to increment stream counter. --- .../bramble/api/db/DatabaseComponent.java | 7 +++---- .../org/briarproject/bramble/db/Database.java | 7 +++---- .../bramble/db/DatabaseComponentImpl.java | 8 +++----- .../briarproject/bramble/db/JdbcDatabase.java | 12 +++++------ .../transport/TransportKeyManagerImpl.java | 4 ++-- .../bramble/db/DatabaseComponentImplTest.java | 20 +++++-------------- .../bramble/db/JdbcDatabaseTest.java | 4 ++-- .../TransportKeyManagerImplTest.java | 4 ++-- 8 files changed, 25 insertions(+), 41 deletions(-) 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 27e379571e..ea05938d49 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 ddd4860c2c..dcf96ea4e1 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 a974802005..00f0bb6229 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 953a0f1604..dd5874f6ed 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 d7ccb49c26..f6abc1c542 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 d8f1f59fbf..f0149bb248 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 9e4047982d..157b6902be 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 8da8eefaa4..df708df91a 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( -- GitLab