Files
buzz/mobile/lib/shared/relay/signed_event_relay.dart
Tom Brow e421ac9870 perf(mobile): trim relay call-site stacks and capture them before async waits (#9949)
## Summary

Two follow-ups to the call-site stacks that escaped relay errors carry:

- **Trim:** Sentry parses a whole stack synchronously and has no cap, so
a call-site stack is now cut to its innermost 64 lines (async gap lines
included). When the kept lines hold no `<asynchronous suspension>` line,
one is added after them, so Sentry's stack parser still reads it as a VM
stack directly instead of first scanning it for other formats. The cut
is made when the stack is first formatted, so an error its caller
discards costs no formatting. One `completeErrorAt` helper next to the
error types applies it at every completion site, and query timeouts
throw with the cut stack too.
- **Capture at entry:** `publish`, `fetchHistory` and
`SignedEventRelay.submit` took their call site after the rate-limit
wait, `mayPublish` or `onSigned`. A caller that doesn't await the call
was already gone from the stack by then, so its frame was missing. The
call site is now taken before the first `await`. `submit` re-stacks
relay call errors from `publish` with its own entry stack, since
`publish` only starts after `onSigned`.

### Validation

Added tests: [stack
trimming](https://github.com/block/buzz/blob/tomb/mobile-relay-stack-trim/mobile/test/shared/relay/relay_call_error_test.dart)
(a deep stack is cut to the cap and keeps the frames nearest the call; a
short one is kept whole; a cut stack keeps exactly one async gap
wherever its first gap falls; the stack is formatted only when read, and
only once), and [capture before the
wait](https://github.com/block/buzz/blob/tomb/mobile-relay-stack-trim/mobile/test/shared/relay/relay_session_test.dart)
for a publish and a history request held by the rate limit and a submit
held by `onSigned`, each checking that the caller's frame is on the
completed error's stack.


Full-app check on an iOS simulator signed in to a Builderlab community:
the channel list loads, opening two channels fetches their history, and
a sent message publishes and shows as sent. No relay call errors logged.

---------

Signed-off-by: Tom Brow <tomb@block.xyz>
Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@localhost>
2026-11-25 15:05:52 -08:00

236 lines
7.5 KiB
Dart

import 'dart:async';
import 'package:nostr/nostr.dart' as nostr;
import '../diagnostics/diagnostic_reporter.dart';
import 'nostr_models.dart';
import 'relay_call_error.dart';
import 'relay_closed_policy.dart';
import 'relay_session.dart';
import 'relay_socket.dart';
import 'package:buzz/shared/crypto/event_signing.dart';
/// Signs and submits Nostr events through the relay WebSocket connection.
class SignedEventRelay {
final RelaySessionNotifier _session;
final String? _nsec;
SignedEventRelay({
required RelaySessionNotifier session,
required String? nsec,
}) : _session = session,
_nsec = nsec;
/// The hex pubkey derived from the signing key, or null if no key.
String? get pubkey {
final nsec = _nsec;
if (nsec == null || nsec.isEmpty) return null;
final privkeyHex = nostr.Nip19.decode(payload: nsec).data;
if (privkeyHex.isEmpty) return null;
return derivePublicKeyHex(privkeyHex);
}
/// Sign and submit an event. Returns the relay's OK response as a [NostrEvent]
/// whose `content` field contains the OK message (e.g. `"response:{...}"`
/// for command kinds). [mayPublish] is asked right before the event goes
/// out, after every wait.
Future<NostrEvent> submit({
required int kind,
required String content,
required List<List<String>> tags,
int? createdAt,
FutureOr<void> Function(NostrEvent event)? onSigned,
Future<bool> Function()? mayPublish,
}) async {
// Taken before the first await, while the caller is still on the stack: a
// caller that doesn't await this call is gone from it afterwards.
final callSite = StackTrace.current;
final nsec = _nsec;
if (nsec == null || nsec.isEmpty) {
throw Exception('Cannot submit event: no signing key available');
}
final privkeyHex = nostr.Nip19.decode(payload: nsec).data;
if (privkeyHex.isEmpty) {
throw Exception('Invalid nsec');
}
final event = signNostrEvent(
kind: kind,
content: content,
tags: tags,
secretKey: privkeyHex,
createdAt: createdAt,
);
final nostrEvent = NostrEvent.fromJson(event.toMap());
// Durable callers must finish journaling the exact signed bytes before
// the relay can accept an event whose acknowledgement might be lost.
await onSigned?.call(nostrEvent);
try {
return await _session.publish(nostrEvent, mayPublish: mayPublish);
} on RelayCallError catch (error) {
// The session's own call site starts after the wait for `onSigned`.
Error.throwWithStackTrace(error, trimCallSite(callSite));
}
}
}
/// The EVENT went out on the socket but the relay's OK never came back, so the
/// relay may or may not have stored it.
class SignedEventUnacknowledged implements Exception {
const SignedEventUnacknowledged(this.cause);
/// Why no OK arrived: a timeout or a dropped connection.
final Object cause;
@override
String toString() => 'Relay did not acknowledge the event: $cause';
}
/// Publishes one signed event over a short-lived authenticated NIP-42 socket.
///
/// This is used for community-removal tombstones because the community being
/// removed is not necessarily the app's active relay session.
///
/// The attempt ends at [timeout] after the call, or at [deadline] when that
/// comes first. At that point the socket closes, and an EVENT not yet sent
/// never is. A failure after the EVENT went out, other than the relay's
/// refusal, is a [SignedEventUnacknowledged].
Future<NostrEvent> submitSignedEventOnce({
required String wsUrl,
required String nsec,
required int kind,
required String content,
required List<List<String>> tags,
int? createdAt,
Duration timeout = const Duration(seconds: 12),
DateTime? deadline,
Map<String, String> headers = const {},
DiagnosticReporter diagnostics = const NoopDiagnosticReporter(),
}) async {
final privateKey = nostr.Nip19.decode(payload: nsec).data;
if (privateKey.isEmpty) throw const FormatException('Invalid nsec');
final signed = signNostrEvent(
kind: kind,
content: content,
tags: tags,
secretKey: privateKey,
createdAt: createdAt,
);
final event = NostrEvent.fromJson(signed.toMap());
// The signer may be a community that isn't active, such as a removed one's
// push tombstone; its reports must not name the active member.
final reporter = SignerDiagnosticReporter(diagnostics, signer: event.pubkey);
final result = Completer<NostrEvent>();
// Errors completed later, from a socket callback or timer, carry the stack
// of the caller, so an escaped error's report reaches app code.
final callSite = StackTrace.current;
var sent = false;
var expired = false;
void fail(Object error) {
if (result.isCompleted) return;
result.completeErrorAt(
sent ? SignedEventUnacknowledged(error) : error,
callSite,
);
}
late final RelaySocket socket;
socket = RelaySocket(
wsUrl: wsUrl,
nsec: nsec,
onMessage: (message) {
if (message case [
'OK',
final String eventId,
final bool accepted,
final String detail,
...,
] when eventId == event.id && !result.isCompleted) {
if (accepted) {
result.complete(
NostrEvent(
id: event.id,
pubkey: event.pubkey,
createdAt: event.createdAt,
kind: event.kind,
tags: event.tags,
content: detail,
sig: event.sig,
),
);
} else {
if (isInvalidRequestRejection(detail)) {
reporter.reportError(
RelayInvalidRequest(),
StackTrace.current,
operation: DiagnosticOperation.relayRejection,
);
}
result.completeErrorAt(_SignedEventRejection(detail), callSite);
}
}
},
onConnected: () {
if (expired || result.isCompleted) return;
sent = true;
socket.send(['EVENT', event.toJson()]);
},
onDisconnected: (error) {
// The socket's own authentication deadline is shorter than [timeout],
// so its plain timeout is typed here as this call's.
if (error is TimeoutException && error is! RelayCallError) {
fail(
RelayTimeoutException(
RelayCall.publish,
error.message ?? 'Relay did not answer',
error.duration,
),
);
return;
}
fail(error ?? Exception('Relay disconnected'));
},
);
socket.headers = headers;
socket.diagnostics = reporter;
var limit = timeout;
if (deadline != null) {
final remaining = deadline.difference(DateTime.now());
if (remaining < limit) limit = remaining;
}
if (limit.isNegative) limit = Duration.zero;
final timer = Timer(limit, () {
expired = true;
fail(
RelayTimeoutException(
RelayCall.publish,
'Relay did not answer within $limit',
limit,
),
);
unawaited(socket.disconnect());
});
try {
unawaited(socket.connect().catchError(fail));
return await result.future;
} finally {
timer.cancel();
await socket.disconnect();
}
}
/// The relay answered a one-shot EVENT with OK false and [detail]. Its text
/// names the refusal for callers; its reason class is the prefix of [detail].
class _SignedEventRejection extends RelayEventRejectedException {
_SignedEventRejection(String detail)
: _reason = RelayReason.fromMessage(detail),
super('Relay rejected event: $detail');
final RelayReason _reason;
@override
RelayReason get reason => _reason;
}