Use key set ID to increment stream counter.

This commit is contained in:
akwizgran
2018-03-28 11:52:14 +01:00
parent 6787d29f11
commit 798b871cc9
8 changed files with 25 additions and 41 deletions

View File

@@ -407,11 +407,10 @@ public interface DatabaseComponent {
throws DbException; throws DbException;
/** /**
* Increments the outgoing stream counter for the given contact and * Increments the outgoing stream counter for the given transport keys.
* transport in the given rotation period .
*/ */
void incrementStreamCounter(Transaction txn, ContactId c, TransportId t, void incrementStreamCounter(Transaction txn, TransportId t, KeySetId k)
long rotationPeriod) throws DbException; throws DbException;
/** /**
* Merges the given metadata with the existing metadata for the given * Merges the given metadata with the existing metadata for the given

View File

@@ -499,11 +499,10 @@ interface Database<T> {
throws DbException; throws DbException;
/** /**
* Increments the outgoing stream counter for the given contact and * Increments the outgoing stream counter for the given transport keys.
* transport in the given rotation period.
*/ */
void incrementStreamCounter(T txn, ContactId c, TransportId t, void incrementStreamCounter(T txn, TransportId t, KeySetId k)
long rotationPeriod) throws DbException; throws DbException;
/** /**
* Marks the given messages as not needing to be acknowledged to the * Marks the given messages as not needing to be acknowledged to the

View File

@@ -607,15 +607,13 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
} }
@Override @Override
public void incrementStreamCounter(Transaction transaction, ContactId c, public void incrementStreamCounter(Transaction transaction, TransportId t,
TransportId t, long rotationPeriod) throws DbException { KeySetId k) throws DbException {
if (transaction.isReadOnly()) throw new IllegalArgumentException(); if (transaction.isReadOnly()) throw new IllegalArgumentException();
T txn = unbox(transaction); T txn = unbox(transaction);
if (!db.containsContact(txn, c))
throw new NoSuchContactException();
if (!db.containsTransport(txn, t)) if (!db.containsTransport(txn, t))
throw new NoSuchTransportException(); throw new NoSuchTransportException();
db.incrementStreamCounter(txn, c, t, rotationPeriod); db.incrementStreamCounter(txn, t, k);
} }
@Override @Override

View File

@@ -2198,17 +2198,15 @@ abstract class JdbcDatabase implements Database<Connection> {
} }
@Override @Override
public void incrementStreamCounter(Connection txn, ContactId c, public void incrementStreamCounter(Connection txn, TransportId t,
TransportId t, long rotationPeriod) throws DbException { KeySetId k) throws DbException {
PreparedStatement ps = null; PreparedStatement ps = null;
try { try {
String sql = "UPDATE outgoingKeys SET stream = stream + 1" String sql = "UPDATE outgoingKeys SET stream = stream + 1"
+ " WHERE contactId = ? AND transportId = ?" + " WHERE transportId = ? AND keySetId = ?";
+ " AND rotationPeriod = ?";
ps = txn.prepareStatement(sql); ps = txn.prepareStatement(sql);
ps.setInt(1, c.getInt()); ps.setString(1, t.getString());
ps.setString(2, t.getString()); ps.setInt(2, k.getInt());
ps.setLong(3, rotationPeriod);
int affected = ps.executeUpdate(); int affected = ps.executeUpdate();
if (affected != 1) throw new DbStateException(); if (affected != 1) throw new DbStateException();
ps.close(); ps.close();

View File

@@ -288,6 +288,7 @@ class TransportKeyManagerImpl implements TransportKeyManager {
if (ks == null) return null; if (ks == null) return null;
MutableOutgoingKeys outKeys = MutableOutgoingKeys outKeys =
ks.getTransportKeys().getCurrentOutgoingKeys(); ks.getTransportKeys().getCurrentOutgoingKeys();
if (!outKeys.isActive()) throw new AssertionError();
if (outKeys.getStreamCounter() > MAX_32_BIT_UNSIGNED) return null; if (outKeys.getStreamCounter() > MAX_32_BIT_UNSIGNED) return null;
// Create a stream context // Create a stream context
StreamContext ctx = new StreamContext(c, transportId, StreamContext ctx = new StreamContext(c, transportId,
@@ -295,8 +296,7 @@ class TransportKeyManagerImpl implements TransportKeyManager {
outKeys.getStreamCounter()); outKeys.getStreamCounter());
// Increment the stream counter and write it back to the DB // Increment the stream counter and write it back to the DB
outKeys.incrementStreamCounter(); outKeys.incrementStreamCounter();
db.incrementStreamCounter(txn, c, transportId, db.incrementStreamCounter(txn, transportId, ks.getKeySetId());
outKeys.getRotationPeriod());
return ctx; return ctx;
} finally { } finally {
lock.unlock(); lock.unlock();

View File

@@ -285,11 +285,11 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
throws Exception { throws Exception {
context.checking(new Expectations() {{ context.checking(new Expectations() {{
// Check whether the contact is in the DB (which it's not) // 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)); will(returnValue(txn));
exactly(18).of(database).containsContact(txn, contactId); exactly(17).of(database).containsContact(txn, contactId);
will(returnValue(false)); will(returnValue(false));
exactly(18).of(database).abortTransaction(txn); exactly(17).of(database).abortTransaction(txn);
}}); }});
DatabaseComponent db = createDatabaseComponent(database, eventBus, DatabaseComponent db = createDatabaseComponent(database, eventBus,
shutdown); shutdown);
@@ -384,16 +384,6 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
db.endTransaction(transaction); 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); transaction = db.startTransaction(false);
try { try {
db.getGroupVisibility(transaction, contactId, groupId); 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) // Check whether the transport is in the DB (which it's not)
exactly(6).of(database).startTransaction(); exactly(6).of(database).startTransaction();
will(returnValue(txn)); will(returnValue(txn));
exactly(2).of(database).containsContact(txn, contactId); oneOf(database).containsContact(txn, contactId);
will(returnValue(true)); will(returnValue(true));
exactly(6).of(database).containsTransport(txn, transportId); exactly(6).of(database).containsTransport(txn, transportId);
will(returnValue(false)); will(returnValue(false));
@@ -822,7 +812,7 @@ public class DatabaseComponentImplTest extends BrambleMockTestCase {
transaction = db.startTransaction(false); transaction = db.startTransaction(false);
try { try {
db.incrementStreamCounter(transaction, contactId, transportId, 0); db.incrementStreamCounter(transaction, transportId, keySetId);
fail(); fail();
} catch (NoSuchTransportException expected) { } catch (NoSuchTransportException expected) {
// Expected // Expected

View File

@@ -825,8 +825,8 @@ public abstract class JdbcDatabaseTest extends BrambleTestCase {
singletonList(new KeySet(keySetId, contactId, keys))); singletonList(new KeySet(keySetId, contactId, keys)));
// Increment the stream counter twice and retrieve the transport keys // Increment the stream counter twice and retrieve the transport keys
db.incrementStreamCounter(txn, contactId, transportId, rotationPeriod); db.incrementStreamCounter(txn, transportId, keySetId);
db.incrementStreamCounter(txn, contactId, transportId, rotationPeriod); db.incrementStreamCounter(txn, transportId, keySetId);
Collection<KeySet> newKeys = db.getTransportKeys(txn, transportId); Collection<KeySet> newKeys = db.getTransportKeys(txn, transportId);
assertEquals(1, newKeys.size()); assertEquals(1, newKeys.size());
KeySet ks = newKeys.iterator().next(); KeySet ks = newKeys.iterator().next();

View File

@@ -227,7 +227,7 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
context.checking(new Expectations() {{ context.checking(new Expectations() {{
// Increment the stream counter // Increment the stream counter
oneOf(db).incrementStreamCounter(txn, contactId, transportId, 1000); oneOf(db).incrementStreamCounter(txn, transportId, keySetId);
}}); }});
TransportKeyManager transportKeyManager = new TransportKeyManagerImpl( TransportKeyManager transportKeyManager = new TransportKeyManagerImpl(
@@ -441,7 +441,7 @@ public class TransportKeyManagerImplTest extends BrambleMockTestCase {
// Activate the keys // Activate the keys
oneOf(db).setTransportKeysActive(txn, transportId, keySetId); oneOf(db).setTransportKeysActive(txn, transportId, keySetId);
// Increment the stream counter // Increment the stream counter
oneOf(db).incrementStreamCounter(txn, contactId, transportId, 1000); oneOf(db).incrementStreamCounter(txn, transportId, keySetId);
}}); }});
TransportKeyManager transportKeyManager = new TransportKeyManagerImpl( TransportKeyManager transportKeyManager = new TransportKeyManagerImpl(