Skip to content

Commit 8ab39e9

Browse files
committed
add useIsolate as an optional constructor param for configuration
1 parent 106de04 commit 8ab39e9

2 files changed

Lines changed: 50 additions & 3 deletions

File tree

packages/ndk/lib/data_layer/repositories/verifiers/bip340_event_verifier.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@ import '../../../domain_layer/repositories/event_verifier.dart';
88
/// Pure dart event verifier using https://pub.dev/packages/bip340
99
/// can be slow on mobile devices
1010
class Bip340EventVerifier implements EventVerifier {
11+
bool useIsolate = true;
12+
13+
Bip340EventVerifier({this.useIsolate = true});
14+
1115
@override
1216
Future<bool> verify(Nip01Event event) async {
13-
return await Isolate.run(() {
17+
return useIsolate? await Isolate.run(() {
1418
return bip340.verify(event.pubKey, event.id, event.sig);
15-
});
19+
}) : bip340.verify(event.pubKey, event.id, event.sig);
1620
}
1721
}

packages/sample-app/lib/query_performance.dart

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import 'package:ndk_rust_verifier/data_layer/repositories/verifiers/rust_event_v
55

66
class MyVerifiers {
77
static final bip340Verifier = Bip340EventVerifier();
8+
static final bip340VerifierNoIsolate = Bip340EventVerifier(useIsolate: false);
89
static final rustVerifier = RustEventVerifier();
910
}
1011

@@ -19,16 +20,25 @@ class QueryPerformancePage extends StatefulWidget {
1920
class _QueryPerformancePageState extends State<QueryPerformancePage> {
2021
int _eventCount = 100;
2122
String _bip340Time = '';
23+
String _bip340NoIsolateTime = '';
2224
String _rustTime = '';
2325
bool _isVerifyingBip340 = false;
26+
bool _isVerifyingBip340NoIsolate = false;
2427
bool _isVerifyingRust = false;
2528

26-
static const relays = ["ws://localhost:10547"];
29+
static const relays = ["wss://relay.primal.net"];
2730

2831
final ndkBip340 = Ndk(NdkConfig(
2932
eventVerifier: MyVerifiers.bip340Verifier,
3033
cache: MemCacheManager(),
3134
bootstrapRelays: relays,
35+
logLevel: LogLevel.warning
36+
));
37+
38+
final ndkBip340NoIsolate = Ndk(NdkConfig(
39+
eventVerifier: MyVerifiers.bip340VerifierNoIsolate,
40+
cache: MemCacheManager(),
41+
bootstrapRelays: relays,
3242
));
3343

3444
final ndkRust = Ndk(NdkConfig(
@@ -53,6 +63,22 @@ class _QueryPerformancePageState extends State<QueryPerformancePage> {
5363
});
5464
}
5565

66+
Future<void> _runBip340NoIsolateQuery() async {
67+
setState(() {
68+
_isVerifyingBip340NoIsolate = true;
69+
_bip340NoIsolateTime = '';
70+
});
71+
72+
final stopwatch = Stopwatch()..start();
73+
await _runQuery(ndkBip340NoIsolate);
74+
stopwatch.stop();
75+
76+
setState(() {
77+
_isVerifyingBip340NoIsolate = false;
78+
_bip340NoIsolateTime = '${stopwatch.elapsedMilliseconds}ms';
79+
});
80+
}
81+
5682
Future<void> _runRustQuery() async {
5783
setState(() {
5884
_isVerifyingRust = true;
@@ -135,6 +161,23 @@ class _QueryPerformancePageState extends State<QueryPerformancePage> {
135161
style: Theme.of(context).textTheme.bodyLarge,
136162
),
137163
const SizedBox(height: 24),
164+
ElevatedButton(
165+
onPressed: _isVerifyingBip340NoIsolate ? null : _runBip340NoIsolateQuery,
166+
child: _isVerifyingBip340NoIsolate
167+
? const SizedBox(
168+
height: 20,
169+
width: 20,
170+
child: CircularProgressIndicator(strokeWidth: 2),
171+
)
172+
: const Text('Run with BIP340 (no isolate)'),
173+
),
174+
const SizedBox(height: 8),
175+
Text(
176+
_bip340NoIsolateTime.isEmpty ? 'Not run yet' : 'Time: $_bip340NoIsolateTime',
177+
textAlign: TextAlign.center,
178+
style: Theme.of(context).textTheme.bodyLarge,
179+
),
180+
const SizedBox(height: 24),
138181
ElevatedButton(
139182
onPressed: _isVerifyingRust ? null : _runRustQuery,
140183
child: _isVerifyingRust

0 commit comments

Comments
 (0)