From f3faadf783a18bfe6ba0790a13e96547db8434a2 Mon Sep 17 00:00:00 2001 From: tommy Date: Wed, 23 Aug 2023 19:04:49 +0200 Subject: [PATCH] feat(wallet_sync): cache hash calls to the explorer During the synchronization process, the wallet was calling multiple times to the hash endpoint of the Witnet Block Explorer. It occurred when the user had multiple accounts and had sent funds between them. During the wallet synchronization, if two accounts of the same wallet had sent funds between them, both would call the explorer with the same hash of a valueTransferOutput. Because of that, the number of repeated calls relied on the number of times accounts of the same wallet have interacted between them. To avoid multiple calls, we have implemented an in-memory cache using a "read through" pattern to fetch the Witnet Block Explorer API only if it wasn't called before. --- lib/bloc/crypto/crypto_bloc.dart | 25 ++++++-- lib/util/storage/cache/read_through.dart | 32 ++++++++++ .../read_through_in_memory_cache_test.dart | 48 -------------- test/storage/read_through_test.dart | 64 +++++++++++++++++++ 4 files changed, 115 insertions(+), 54 deletions(-) create mode 100644 lib/util/storage/cache/read_through.dart delete mode 100644 test/storage/read_through_in_memory_cache_test.dart create mode 100644 test/storage/read_through_test.dart diff --git a/lib/bloc/crypto/crypto_bloc.dart b/lib/bloc/crypto/crypto_bloc.dart index 05e7a2197..105a37bad 100644 --- a/lib/bloc/crypto/crypto_bloc.dart +++ b/lib/bloc/crypto/crypto_bloc.dart @@ -17,6 +17,7 @@ import 'package:my_wit_wallet/bloc/crypto/api_crypto.dart'; import 'package:my_wit_wallet/bloc/explorer/api_explorer.dart'; import 'package:my_wit_wallet/shared/api_database.dart'; import 'package:my_wit_wallet/shared/locator.dart'; +import 'package:my_wit_wallet/util/storage/cache/read_through.dart'; import 'package:my_wit_wallet/util/storage/database/account.dart'; import 'package:my_wit_wallet/util/storage/database/balance_info.dart'; import 'package:my_wit_wallet/util/storage/database/encrypt/password.dart'; @@ -40,7 +41,18 @@ class CryptoBloc extends Bloc { ApiExplorer apiExplorer = Locator.instance.get(); ApiDatabase db = Locator.instance.get(); + // TODO: remove late because is always being defined in the constructor + // Map + late ReadThrough _vttGetThroughBlockExplorer; + CryptoBloc(initialState) : super(initialState) { + _vttGetThroughBlockExplorer = ReadThrough( + (String hash) async => await apiExplorer.hash(hash), + (ValueTransferInfo valueTransferInfo) async => + await db.addVtt(valueTransferInfo), + (String hash) async => await db.getVtt(hash), + ); + on(_cryptoInitializeWalletEvent); on(_cryptoInitWalletDoneEvent); on(_cryptoReadyEvent); @@ -49,7 +61,6 @@ class CryptoBloc extends Bloc { Future _cryptoInitializeWalletEvent( CryptoInitializeWalletEvent event, Emitter emit) async { - print("----------2-----------"); /// setup default default structure for database and unlock it emit( CryptoInitializingWalletState( @@ -237,7 +248,6 @@ class CryptoBloc extends Bloc { ApiDatabase db = Locator.instance(); String key = await db.getKeychain(); final masterKey = key != '' ? key : event.password; - print("----------1-----------"); apiCrypto.setInitialWalletData( event.walletName, event.keyData, @@ -292,10 +302,11 @@ class CryptoBloc extends Bloc { try { for (int i = 0; i < account.vttHashes.length; i++) { String _hash = account.vttHashes.elementAt(i); - var result = await apiExplorer.hash(_hash); - ValueTransferInfo valueTransferInfo = result as ValueTransferInfo; - account.vtts.add(valueTransferInfo); - await db.addVtt(valueTransferInfo); + ValueTransferInfo? valueTransferInfo = + await _vttGetThroughBlockExplorer.get(_hash); + if (valueTransferInfo != null) { + account.vtts.add(valueTransferInfo); + } } return account; } catch (e) { @@ -334,7 +345,9 @@ class CryptoBloc extends Bloc { } Future _syncAccount(Account account) async { + // In this function vttHashes are fullfilled try { + // Is not ignoring repeated value transfers final addressValueTransfers = await apiExplorer.address( value: account.address, tab: 'value_transfers') as AddressValueTransfers; diff --git a/lib/util/storage/cache/read_through.dart b/lib/util/storage/cache/read_through.dart new file mode 100644 index 000000000..452f8377d --- /dev/null +++ b/lib/util/storage/cache/read_through.dart @@ -0,0 +1,32 @@ +// Generic implementation of read throught cache pattern +class ReadThrough { + // Consume the value if it's not already stored + Future Function(String) _fetch; + // Save the value in cache + Future Function(T) _save; + // Read Value from Cache + Future Function(String) _read; + + ReadThrough(this._fetch, this._save, this._read); + + Future _insert(T value) async { + return await _save(value); + } + + // Check if value is stored and call the fetch function if it's not found + Future get(String key) async { + T? storedValue = await _read(key); + + if (storedValue != null) { + return storedValue; + } else { + T? value = await _fetch(key); + + if (value != null) { + await _insert(value); + } + + return value; + } + } +} diff --git a/test/storage/read_through_in_memory_cache_test.dart b/test/storage/read_through_in_memory_cache_test.dart deleted file mode 100644 index 35d9dc70b..000000000 --- a/test/storage/read_through_in_memory_cache_test.dart +++ /dev/null @@ -1,48 +0,0 @@ -import 'package:my_wit_wallet/util/storage/cache/read_through_in_memory_cache.dart'; -import 'package:test/test.dart'; - -void main() { - group('Storage implementation of read through cache pattern in memory', () { - test('Should call fetch callback if it\'s first time getting the value', - () async { - int timesCalled = 0; - String expectedValue = "Potato"; - - ReadThroughInMemoryCache cache = - ReadThroughInMemoryCache((p0) async { - timesCalled = timesCalled += 1; - - await Future.delayed(Duration(seconds: 0)); - - return expectedValue; - }); - - expect(await cache.get("whatever"), expectedValue); - expect(timesCalled, 1); - }); - - test('Should NOT call fetch callback if it\'s the second time getting the value', - () async { - int timesCalled = 0; - String expectedValue = "Potato"; - - ReadThroughInMemoryCache cache = - ReadThroughInMemoryCache((p0) async { - timesCalled = timesCalled += 1; - - await Future.delayed(Duration(seconds: 0)); - - return expectedValue; - }); - - - // call get multiple times - expect(await cache.get("whatever"), expectedValue); - expect(await cache.get("whatever"), expectedValue); - expect(await cache.get("whatever"), expectedValue); - expect(await cache.get("whatever"), expectedValue); - - expect(timesCalled, 1); - }); - }); -} diff --git a/test/storage/read_through_test.dart b/test/storage/read_through_test.dart new file mode 100644 index 000000000..79cdbf102 --- /dev/null +++ b/test/storage/read_through_test.dart @@ -0,0 +1,64 @@ +import 'package:my_wit_wallet/util/storage/cache/read_through.dart'; +import 'package:test/test.dart'; + +void main() { + group('Storage implementation of read through cache pattern in memory', () { + test('Should call fetch callback if it\'s first time getting the value', + () async { + int timesCalled = 0; + String expectedValue = "Potato"; + bool saveCalled = false; + + ReadThrough cache = ReadThrough((p0) async { + timesCalled = timesCalled += 1; + + await Future.delayed(Duration(seconds: 0)); + + return expectedValue; + }, (p1) async { + saveCalled = true; + return true; + }, (p0) async { + return null; + }); + + expect(await cache.get("whatever"), expectedValue); + expect(saveCalled, true); + expect(timesCalled, 1); + }); + + test( + 'Should NOT call fetch callback if it\'s the second time getting the value', + () async { + int timesCalled = 0; + String expectedValue = "Potato"; + bool saveCalled = false; + + ReadThrough cache = ReadThrough((p0) async { + timesCalled = timesCalled += 1; + + await Future.delayed(Duration(seconds: 0)); + + return expectedValue; + }, (p1) async { + saveCalled = true; + return true; + }, (p0) async { + if (timesCalled == 0) { + return null; + } else { + return expectedValue; + } + }); + + // call get multiple times + expect(await cache.get(expectedValue), expectedValue); + expect(await cache.get(expectedValue), expectedValue); + expect(await cache.get(expectedValue), expectedValue); + expect(await cache.get(expectedValue), expectedValue); + expect(saveCalled, true); + + expect(timesCalled, 1); + }); + }); +}