From 32c9aa4ec6cc8caf07ff1cb18f69267a6154b264 Mon Sep 17 00:00:00 2001 From: Srie Teja Date: Mon, 16 Oct 2023 16:13:11 +0530 Subject: [PATCH] fix: migrate to a new configKey that stores blocklist --- .../lib/src/config/at_config.dart | 74 +++++++++++++------ 1 file changed, 51 insertions(+), 23 deletions(-) diff --git a/packages/at_persistence_secondary_server/lib/src/config/at_config.dart b/packages/at_persistence_secondary_server/lib/src/config/at_config.dart index 432d84f16..c28ae4e60 100644 --- a/packages/at_persistence_secondary_server/lib/src/config/at_config.dart +++ b/packages/at_persistence_secondary_server/lib/src/config/at_config.dart @@ -1,5 +1,6 @@ import 'dart:convert'; +import 'package:at_commons/at_commons.dart'; import 'package:at_persistence_secondary_server/at_persistence_secondary_server.dart'; import 'package:at_persistence_secondary_server/src/config/configuration.dart'; import 'package:at_persistence_secondary_server/src/keystore/hive_keystore_helper.dart'; @@ -11,7 +12,8 @@ class AtConfig { var logger = AtSignLogger('AtConfig'); ///stores 'Configuration' type under [configkey] in secondary. - String configKey = 'configKey'; + String olConfigKey = 'configKey'; + String configKey = 'private:blocklist'; var keyStoreHelper = HiveKeyStoreHelper.getInstance(); final String? _atSign; AtCommitLog? _commitLog; @@ -27,18 +29,16 @@ class AtConfig { .getHivePersistenceManager()!; } - ///Returns 'success' on adding unique [data] into blocklist. - Future addToBlockList(Set data) async { + ///Returns 'success' on adding unique [blockList] into blocklist. + Future addToBlockList(Set blockList) async { String result; try { - assert(data.isNotEmpty); - var existingData = await get(configKey); - var blockList = await getBlockList(); - var uniqueBlockList = Set.from(blockList); - uniqueBlockList.addAll(data); + assert(blockList.isNotEmpty); + AtData? existingData = await _getExistingData(); + Set uniqueBlockList = await getBlockList(); + uniqueBlockList.addAll(blockList); var config = Configuration(List.from(uniqueBlockList)); result = await prepareAndStoreData(config, existingData); - return result; } on Exception catch (e) { throw DataStoreException( 'Exception adding to commit log:${e.toString()}'); @@ -46,40 +46,40 @@ class AtConfig { throw DataStoreException( 'Hive error adding to commit log:${e.toString()}'); } + return result; } - ///removes [data] from blocklist if satisfies basic conditions. - Future removeFromBlockList(Set data) async { + ///removes [unblockAtsignsList] from blocklist if satisfies basic conditions. + Future removeFromBlockList(Set unblockAtsignsList) async { String? result; try { - assert(data.isNotEmpty); - var existingData = await get(configKey); - if (existingData != null) { - var blockList = await getBlockList(); + assert(unblockAtsignsList.isNotEmpty); + var existingData = await _getExistingData(); + Set blockedAtsignsSet = await getBlockList(); + // remove the atsign in unblockAtsignList from the existing blocklist + if (blockedAtsignsSet.isNotEmpty) { var config = Configuration( - List.from(Set.from(blockList).difference(Set.from(data)))); + List.from(blockedAtsignsSet.difference(Set.from(unblockAtsignsList)))); result = await prepareAndStoreData(config, existingData); } - return result; } on Exception catch (e) { throw DataStoreException( 'Exception adding to commit log:${e.toString()}'); } on HiveError catch (e) { - throw DataStoreException( - 'Hive error adding to commit log:${e.toString()}'); + throw DataStoreException('Hive error adding to commit log:${e.message}'); } + return result; } ///Returns blocklist by fetching from atsign's secondary. Future> getBlockList() async { var result = {}; try { - var existingData = await get(configKey); + var existingData = await _getExistingData(); if (existingData != null) { var config = jsonDecode(existingData.data!); result = Set.from(config['blockList']); } - return result; } on Exception catch (e) { throw DataStoreException( 'Exception adding to commit log:${e.toString()}'); @@ -87,6 +87,8 @@ class AtConfig { throw DataStoreException( 'Hive error adding to commit log:${e.toString()}'); } + + return result; } ///Returns [AtData] value for given [key]. @@ -95,7 +97,6 @@ class AtConfig { try { var hiveKey = keyStoreHelper.prepareKey(key); value = await (persistenceManager.getBox() as LazyBox).get(hiveKey); - return value; } on Exception catch (exception) { logger.severe('HiveKeystore get exception: $exception'); throw DataStoreException('exception in get: ${exception.toString()}'); @@ -103,6 +104,8 @@ class AtConfig { logger.severe('HiveKeystore get error: $error'); throw DataStoreException(error.message); } + + return value; } ///Returns 'true' if blocklist contains [atsign]. @@ -111,7 +114,6 @@ class AtConfig { try { var blockList = await getBlockList(); result = blockList.contains(atsign); - return result; } on Exception catch (e) { throw DataStoreException( 'Exception adding to commit log:${e.toString()}'); @@ -119,6 +121,7 @@ class AtConfig { throw DataStoreException( 'Hive error adding to commit log:${e.toString()}'); } + return result; } ///Returns 'success' after successfully persisting data into secondary. @@ -137,4 +140,29 @@ class AtConfig { result = 'success'; return result; } + + /// Fetches existing Config data from the keystore + /// + /// Tries fetching data with [configKey] which is the new config key + /// + /// For backward-compatability, if data could not be fetched with new key + /// tries fetching data with [oldConfigKey] + Future _getExistingData() async { + AtData? existingData; + try { + // try to fetch data using the new config-key format + existingData = await get(configKey); + } on KeyNotFoundException catch (e) { + logger.finer('Could not fetch data with NEW config-key | ${e.message}'); + } + if (existingData == null) { + try { + existingData = await get(olConfigKey); + await (persistenceManager.getBox() as LazyBox).delete(olConfigKey); + } on KeyNotFoundException catch (e) { + logger.finer('Could not fetch data with OLD config-key | ${e.message}'); + } + } + return existingData; + } }