feat(block2): relay pool — publish to many relays, discover deduped, tolerate failures

Relay-strategy hardening (network-trust §3): don't hang on one relay.
- NostrChannel interface: what the transports need (sign, publish, subscribe,
  reqOnce). Implemented by NostrConnection (single relay) and the new RelayPool.
- RelayPool: connects to all configured relays (skips unreachable ones, throws
  only when none are reachable), publishes to every live relay (accepted if any
  accepts), merges discovery deduped by event id, and survives a relay dropping.
- Transports now depend on NostrChannel, not a concrete connection.
- SocialService.openSession takes ALL configured relays (was relays.first);
  createOffersCubit passes the whole list.

commons_core 63 tests green (3 new pool tests over two in-process relays);
app_seeds social/market 18 green.
This commit is contained in:
vjrj 2026-07-10 10:35:39 +02:00
parent 492bc62025
commit 2a6b4b0947
10 changed files with 246 additions and 24 deletions

View file

@ -36,30 +36,30 @@ class SocialService {
return SocialService(identity: identity, relays: relays);
}
/// Opens a session against [relayUrl]: one shared connection carrying all
/// three transports (offers, messaging, trust). Caller closes it.
Future<SocialSession> openSession(String relayUrl) async {
final connection =
await NostrConnection.connect(relayUrl, identity: identity);
return SocialSession(connection);
/// Opens a session against [relayUrls]: one fault-tolerant pool carrying all
/// three transports (offers, messaging, trust). Relays that are down are
/// skipped; throws only when none are reachable. Caller closes it.
Future<SocialSession> openSession(List<String> relayUrls) async {
final pool = await RelayPool.connect(relayUrls, identity: identity);
return SocialSession(pool);
}
}
/// One live relay connection with the three transports on top the
/// "one connection, three interfaces" shape, at the app layer.
/// One relay channel with the three transports on top the
/// "one channel, three interfaces" shape, at the app layer.
class SocialSession {
SocialSession(this._connection)
: offers = NostrOfferTransport(_connection),
messages = NostrMessageTransport(_connection),
trust = NostrTrustTransport(_connection);
SocialSession(this._channel)
: offers = NostrOfferTransport(_channel),
messages = NostrMessageTransport(_channel),
trust = NostrTrustTransport(_channel);
final NostrConnection _connection;
final NostrChannel _channel;
final OfferTransport offers;
final MessageTransport messages;
final TrustTransport trust;
Future<void> close() => _connection.close();
Future<void> close() => _channel.close();
}
Uint8List _hexToBytes(String hex) {

View file

@ -172,9 +172,9 @@ Future<OffersCubit> createOffersCubit(
final relays = await settings.relayUrls();
if (relays.isEmpty) return OffersCubit(null);
try {
final session = await social.openSession(relays.first);
final session = await social.openSession(relays);
return OffersCubit(session.offers, onDispose: session.close);
} catch (_) {
return OffersCubit(null); // offline / relay unreachable
return OffersCubit(null); // offline / no relay reachable
}
}