-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
4 changed files
with
115 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Generic implementation of read throught cache pattern | ||
class ReadThrough<T> { | ||
// Consume the value if it's not already stored | ||
Future<T?> Function(String) _fetch; | ||
// Save the value in cache | ||
Future<bool> Function(T) _save; | ||
// Read Value from Cache | ||
Future<T?> Function(String) _read; | ||
|
||
ReadThrough(this._fetch, this._save, this._read); | ||
|
||
Future<bool> _insert(T value) async { | ||
return await _save(value); | ||
} | ||
|
||
// Check if value is stored and call the fetch function if it's not found | ||
Future<T?> 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; | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String> 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<String> 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); | ||
}); | ||
}); | ||
} |