Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:isolate';

import 'package:bip340/bip340.dart' as bip340;

import '../../../domain_layer/entities/nip_01_event.dart';
Expand All @@ -6,8 +8,14 @@ import '../../../domain_layer/repositories/event_verifier.dart';
/// Pure dart event verifier using https://pub.dev/packages/bip340
/// can be slow on mobile devices
class Bip340EventVerifier implements EventVerifier {
bool useIsolate = true;

Bip340EventVerifier({this.useIsolate = true});

@override
Future<bool> verify(Nip01Event event) async {
return bip340.verify(event.pubKey, event.id, event.sig);
return useIsolate? await Isolate.run(() {
return bip340.verify(event.pubKey, event.id, event.sig);
}) : bip340.verify(event.pubKey, event.id, event.sig);
}
}
45 changes: 44 additions & 1 deletion packages/sample-app/lib/query_performance.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:ndk_rust_verifier/data_layer/repositories/verifiers/rust_event_v

class MyVerifiers {
static final bip340Verifier = Bip340EventVerifier();
static final bip340VerifierNoIsolate = Bip340EventVerifier(useIsolate: false);
static final rustVerifier = RustEventVerifier();
}

Expand All @@ -19,16 +20,25 @@ class QueryPerformancePage extends StatefulWidget {
class _QueryPerformancePageState extends State<QueryPerformancePage> {
int _eventCount = 100;
String _bip340Time = '';
String _bip340NoIsolateTime = '';
String _rustTime = '';
bool _isVerifyingBip340 = false;
bool _isVerifyingBip340NoIsolate = false;
bool _isVerifyingRust = false;

static const relays = ["ws://localhost:10547"];
static const relays = ["wss://relay.primal.net"];

final ndkBip340 = Ndk(NdkConfig(
eventVerifier: MyVerifiers.bip340Verifier,
cache: MemCacheManager(),
bootstrapRelays: relays,
logLevel: LogLevel.warning
));

final ndkBip340NoIsolate = Ndk(NdkConfig(
eventVerifier: MyVerifiers.bip340VerifierNoIsolate,
cache: MemCacheManager(),
bootstrapRelays: relays,
));

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

Future<void> _runBip340NoIsolateQuery() async {
setState(() {
_isVerifyingBip340NoIsolate = true;
_bip340NoIsolateTime = '';
});

final stopwatch = Stopwatch()..start();
await _runQuery(ndkBip340NoIsolate);
stopwatch.stop();

setState(() {
_isVerifyingBip340NoIsolate = false;
_bip340NoIsolateTime = '${stopwatch.elapsedMilliseconds}ms';
});
}

Future<void> _runRustQuery() async {
setState(() {
_isVerifyingRust = true;
Expand Down Expand Up @@ -135,6 +161,23 @@ class _QueryPerformancePageState extends State<QueryPerformancePage> {
style: Theme.of(context).textTheme.bodyLarge,
),
const SizedBox(height: 24),
ElevatedButton(
onPressed: _isVerifyingBip340NoIsolate ? null : _runBip340NoIsolateQuery,
child: _isVerifyingBip340NoIsolate
? const SizedBox(
height: 20,
width: 20,
child: CircularProgressIndicator(strokeWidth: 2),
)
: const Text('Run with BIP340 (no isolate)'),
),
const SizedBox(height: 8),
Text(
_bip340NoIsolateTime.isEmpty ? 'Not run yet' : 'Time: $_bip340NoIsolateTime',
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.bodyLarge,
),
const SizedBox(height: 24),
ElevatedButton(
onPressed: _isVerifyingRust ? null : _runRustQuery,
child: _isVerifyingRust
Expand Down