Allow generating a set amount of prekeys.

pull/14/head
Mikunj 7 years ago committed by Mikunj Varsani
parent 635dcef2b2
commit d130415973

@ -10,6 +10,7 @@ NS_ASSUME_NONNULL_BEGIN
@interface OWSPrimaryStorage (PreKeyStore) <PreKeyStore> @interface OWSPrimaryStorage (PreKeyStore) <PreKeyStore>
- (NSArray<PreKeyRecord *> *)generatePreKeyRecords; - (NSArray<PreKeyRecord *> *)generatePreKeyRecords;
- (NSArray<PreKeyRecord *> *)generatePreKeyRecords:(int)amount;
- (void)storePreKeyRecords:(NSArray<PreKeyRecord *> *)preKeyRecords NS_SWIFT_NAME(storePreKeyRecords(_:)); - (void)storePreKeyRecords:(NSArray<PreKeyRecord *> *)preKeyRecords NS_SWIFT_NAME(storePreKeyRecords(_:));
@end @end

@ -18,23 +18,23 @@ NS_ASSUME_NONNULL_BEGIN
@implementation OWSPrimaryStorage (PreKeyStore) @implementation OWSPrimaryStorage (PreKeyStore)
- (NSArray<PreKeyRecord *> *)generatePreKeyRecords; - (NSArray<PreKeyRecord *> *)generatePreKeyRecords:(int)batchSize
{ {
NSMutableArray *preKeyRecords = [NSMutableArray array]; NSMutableArray *preKeyRecords = [NSMutableArray array];
@synchronized(self) @synchronized(self)
{ {
int preKeyId = [self nextPreKeyId]; int preKeyId = [self nextPreKeyId:batchSize];
OWSLogInfo(@"building %d new preKeys starting from preKeyId: %d", BATCH_SIZE, preKeyId); OWSLogInfo(@"building %d new preKeys starting from preKeyId: %d", batchSize, preKeyId);
for (int i = 0; i < BATCH_SIZE; i++) { for (int i = 0; i < batchSize; i++) {
ECKeyPair *keyPair = [Curve25519 generateKeyPair]; ECKeyPair *keyPair = [Curve25519 generateKeyPair];
PreKeyRecord *record = [[PreKeyRecord alloc] initWithId:preKeyId keyPair:keyPair]; PreKeyRecord *record = [[PreKeyRecord alloc] initWithId:preKeyId keyPair:keyPair];
[preKeyRecords addObject:record]; [preKeyRecords addObject:record];
preKeyId++; preKeyId++;
} }
[self.dbReadWriteConnection setInt:preKeyId [self.dbReadWriteConnection setInt:preKeyId
forKey:TSNextPrekeyIdKey forKey:TSNextPrekeyIdKey
inCollection:TSStorageInternalSettingsCollection]; inCollection:TSStorageInternalSettingsCollection];
@ -42,6 +42,11 @@ NS_ASSUME_NONNULL_BEGIN
return preKeyRecords; return preKeyRecords;
} }
- (NSArray<PreKeyRecord *> *)generatePreKeyRecords;
{
return [self generatePreKeyRecords:BATCH_SIZE];
}
- (void)storePreKeyRecords:(NSArray<PreKeyRecord *> *)preKeyRecords - (void)storePreKeyRecords:(NSArray<PreKeyRecord *> *)preKeyRecords
{ {
for (PreKeyRecord *record in preKeyRecords) { for (PreKeyRecord *record in preKeyRecords) {
@ -83,15 +88,15 @@ NS_ASSUME_NONNULL_BEGIN
inCollection:OWSPrimaryStoragePreKeyStoreCollection]; inCollection:OWSPrimaryStoragePreKeyStoreCollection];
} }
- (int)nextPreKeyId - (int)nextPreKeyId:(int)batchSize
{ {
int lastPreKeyId = int lastPreKeyId =
[self.dbReadConnection intForKey:TSNextPrekeyIdKey inCollection:TSStorageInternalSettingsCollection]; [self.dbReadConnection intForKey:TSNextPrekeyIdKey inCollection:TSStorageInternalSettingsCollection];
if (lastPreKeyId < 1) { if (lastPreKeyId < 1) {
// One-time prekey ids must be > 0 and < kPreKeyOfLastResortId. // One-time prekey ids must be > 0 and < kPreKeyOfLastResortId.
lastPreKeyId = 1 + arc4random_uniform(kPreKeyOfLastResortId - (BATCH_SIZE + 1)); lastPreKeyId = 1 + arc4random_uniform(kPreKeyOfLastResortId - (batchSize + 1));
} else if (lastPreKeyId > kPreKeyOfLastResortId - BATCH_SIZE) { } else if (lastPreKeyId > kPreKeyOfLastResortId - batchSize) {
// We want to "overflow" to 1 when we reach the "prekey of last resort" id // We want to "overflow" to 1 when we reach the "prekey of last resort" id
// to avoid biasing towards higher values. // to avoid biasing towards higher values.
lastPreKeyId = 1; lastPreKeyId = 1;

Loading…
Cancel
Save