-
Notifications
You must be signed in to change notification settings - Fork 28
perf: skip re-verifying and re-decrypting already-unwrapped envelopes #702
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1dbb931
perf: skip re-verifying and re-decrypting already-unwrapped envelopes
grunch 00d3bbd
chore: drop a dead null guard after making the wrapper id non-null
grunch 1e2e644
fix: never advance chat cursors from an unverified envelope
grunch 472af31
Merge branch 'main' into perf/skip-redundant-decrypt
grunch afd497f
fix: let a valid envelope retry when a same-id copy fails
grunch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
143 changes: 143 additions & 0 deletions
143
test/features/chat/chat_room_notifier_redundant_decrypt_test.dart
This file contains hidden or 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,143 @@ | ||
| import 'package:dart_nostr/dart_nostr.dart'; | ||
| import 'package:flutter_riverpod/flutter_riverpod.dart'; | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:mostro_mobile/data/models/chat_room.dart'; | ||
| import 'package:mostro_mobile/data/models/nostr_event.dart'; | ||
| import 'package:mostro_mobile/data/models/peer.dart'; | ||
| import 'package:mostro_mobile/data/models/session.dart'; | ||
| import 'package:mostro_mobile/data/repositories/event_storage.dart'; | ||
| import 'package:mostro_mobile/features/chat/notifiers/chat_room_notifier.dart'; | ||
| import 'package:mostro_mobile/features/chat/notifiers/chat_rooms_notifier.dart'; | ||
| import 'package:mostro_mobile/features/chat/providers/chat_room_providers.dart'; | ||
| import 'package:mostro_mobile/services/chat_cursor_store.dart'; | ||
| import 'package:mostro_mobile/shared/providers/mostro_service_provider.dart'; | ||
| import 'package:mostro_mobile/shared/providers/session_notifier_provider.dart'; | ||
| import 'package:mostro_mobile/shared/utils/chat_keys.dart'; | ||
| import 'package:sembast/sembast_memory.dart'; | ||
| import 'package:shared_preferences/shared_preferences.dart'; | ||
|
|
||
| /// A chat envelope costs ~5 EC multiplications to verify + decrypt. With R | ||
| /// relays each message used to be unwrapped R times (the handler continued | ||
| /// past `alreadyStored`), and every history load re-unwrapped every stored | ||
| /// envelope. Already-verified envelopes are now skipped and unwraps are | ||
| /// cached per outer id. | ||
| class _StubChatRoomsNotifier extends ChatRoomsNotifier { | ||
| _StubChatRoomsNotifier(super.ref); | ||
|
|
||
| @override | ||
| Future<void> loadChats() async {} | ||
|
|
||
| @override | ||
| Future<void> refreshChatList() async {} | ||
| } | ||
|
|
||
| class _FakeSharedPreferencesAsync implements SharedPreferencesAsync { | ||
| final Map<String, int> ints = {}; | ||
|
|
||
| @override | ||
| Future<int?> getInt(String key) async => ints[key]; | ||
|
|
||
| @override | ||
| Future<void> setInt(String key, int value) async { | ||
| ints[key] = value; | ||
| } | ||
|
|
||
| @override | ||
| dynamic noSuchMethod(Invocation invocation) => | ||
| throw UnimplementedError('${invocation.memberName}'); | ||
| } | ||
|
|
||
| void main() { | ||
| TestWidgetsFlutterBinding.ensureInitialized(); | ||
|
|
||
| const orderId = 'a4b7c9e1-0000-4000-8000-redundant-dec'; | ||
| final ownKey = NostrKeyPairs( | ||
| private: | ||
| '0000000000000000000000000000000000000000000000000000000000000001', | ||
| ); | ||
| final peerKey = NostrKeyPairs( | ||
| private: | ||
| '0000000000000000000000000000000000000000000000000000000000000002', | ||
| ); | ||
|
|
||
| late Session session; | ||
| late EventStorage eventStorage; | ||
| late ProviderContainer container; | ||
| late ChatRoomNotifier notifier; | ||
| late ChatKeys chatKeys; | ||
|
|
||
| final chatRoomProvider = StateNotifierProvider<ChatRoomNotifier, ChatRoom>( | ||
| (ref) => ChatRoomNotifier( | ||
| ChatRoom(orderId: orderId, messages: []), | ||
| orderId, | ||
| ref, | ||
| ), | ||
| ); | ||
|
|
||
| setUp(() async { | ||
| session = Session( | ||
| masterKey: ownKey, | ||
| tradeKey: ownKey, | ||
| keyIndex: 1, | ||
| fullPrivacy: false, | ||
| startTime: DateTime.now(), | ||
| orderId: orderId, | ||
| )..peer = Peer(publicKey: peerKey.public); | ||
| chatKeys = ChatKeys.fromSharedKey(session.sharedKey!); | ||
|
|
||
| final db = | ||
| await newDatabaseFactoryMemory().openDatabase('redundant_decrypt.db'); | ||
| eventStorage = EventStorage(db: db); | ||
|
|
||
| container = ProviderContainer(overrides: [ | ||
| sessionProvider(orderId).overrideWith((ref) => session), | ||
| eventStorageProvider.overrideWithValue(eventStorage), | ||
| chatCursorStoreProvider.overrideWithValue( | ||
| ChatCursorStore(_FakeSharedPreferencesAsync(), | ||
| keyPrefix: 'chat_since_'), | ||
| ), | ||
| chatRoomsNotifierProvider | ||
| .overrideWith((ref) => _StubChatRoomsNotifier(ref)), | ||
| ]); | ||
| notifier = container.read(chatRoomProvider.notifier); | ||
| }); | ||
|
|
||
| tearDown(() => container.dispose()); | ||
|
|
||
| Future<NostrEvent> envelope(String text) { | ||
| final rumor = NostrEventExtensions.createChatRumor( | ||
| senderKeys: peerKey, | ||
| content: text, | ||
| ); | ||
| return rumor.chatWrap(chatKeys); | ||
| } | ||
|
|
||
| test('a relay re-delivery of a stored envelope is not unwrapped again', | ||
| () async { | ||
| final event = await envelope('hola'); | ||
|
|
||
| await notifier.handleChatEvent(event); | ||
| expect(notifier.debugUnwrapCount, 1); | ||
| expect(container.read(chatRoomProvider).messages, hasLength(1)); | ||
|
|
||
| // Same envelope from a second relay. | ||
| await notifier.handleChatEvent(event); | ||
|
|
||
| expect(notifier.debugUnwrapCount, 1, | ||
| reason: 'the stored copy was already verified when first accepted'); | ||
| expect(container.read(chatRoomProvider).messages, hasLength(1)); | ||
| }); | ||
|
|
||
| test('history load reuses the unwrap of a live-handled envelope', () async { | ||
| final event = await envelope('hola'); | ||
| await notifier.handleChatEvent(event); | ||
| expect(notifier.debugUnwrapCount, 1); | ||
|
|
||
| await notifier.initialize(); | ||
|
|
||
| expect(notifier.debugUnwrapCount, 1, | ||
| reason: 'reloading history must not re-verify and re-decrypt ' | ||
| 'envelopes already unwrapped in this notifier'); | ||
| expect(container.read(chatRoomProvider).messages, hasLength(1)); | ||
| }); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.