Skip to content

Commit

Permalink
Mark successful payjoin sessions in storage.
Browse files Browse the repository at this point in the history
Payjoin sessions marked 'complete' do not get spawned. This reduces the
number of isolates spawned on resume or restart.

Mark rather than delete so that the historic status may eventually be
shown in transaction history.
  • Loading branch information
DanGould committed Dec 27, 2024
1 parent 2a16864 commit 4ae408b
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 6 deletions.
44 changes: 38 additions & 6 deletions lib/_pkg/payjoin/manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,14 @@ class PayjoinManager {
completer.complete(err);
return;
}
// Successfully sent payjoin
PayjoinEventBus().emit(
PayjoinBroadcastEvent(
txid: wtxid!.$2,
),
);
await _cleanupSession(pjUri);
await _payjoinStorage.markSenderSessionComplete(pjUri);
completer.complete(null);
}
} else if (message is Err) {
Expand Down Expand Up @@ -244,6 +246,7 @@ class PayjoinManager {
});
case 'proposal_sent':
await _cleanupSession(receiver.id());
await _payjoinStorage.markSenderSessionComplete(receiver.id());
completer.complete(null);
}
} catch (e) {
Expand Down Expand Up @@ -295,10 +298,13 @@ class PayjoinManager {
if (senderErr != null) throw senderErr;

final filteredReceivers = receiverSessions
.where((session) => session.walletId == wallet.id)
.where((session) =>
session.walletId == wallet.id &&
session.status != PayjoinSessionStatus.success)
.toList();
final filteredSenders = senderSessions.where((session) {
return session.walletId == wallet.id;
return session.walletId == wallet.id &&
session.status != PayjoinSessionStatus.success;
}).toList();

final spawnedReceivers = filteredReceivers.map((session) {
Expand Down Expand Up @@ -359,8 +365,19 @@ class PayjoinManager {
}
}

enum PayjoinSessionStatus {
pending,
success,
}

class SendSession {
SendSession(this._isTestnet, this._sender, this._walletId, this._pjUri);
SendSession(
this._isTestnet,
this._sender,
this._walletId,
this._pjUri,
this._status,
);

// Deserialize JSON to Receiver
factory SendSession.fromJson(Map<String, dynamic> json) {
Expand All @@ -369,54 +386,69 @@ class SendSession {
Sender.fromJson(json['sender'] as String),
json['walletId'] as String,
json['pjUri'] as String,
json['status'] != null
? PayjoinSessionStatus.values.byName(json['status'] as String)
: null,
);
}

final bool _isTestnet;
final Sender _sender;
final String _walletId;
final String _pjUri;

final PayjoinSessionStatus? _status;
bool get isTestnet => _isTestnet;
Sender get sender => _sender;
String get walletId => _walletId;
String get pjUri => _pjUri;

PayjoinSessionStatus? get status => _status;
// Serialize Receiver to JSON
Map<String, dynamic> toJson() {
return {
'isTestnet': _isTestnet,
'sender': _sender.toJson(),
'walletId': _walletId,
'pjUri': _pjUri,
'status': _status?.name,
};
}
}

class RecvSession {
RecvSession(this._isTestnet, this._receiver, this._walletId);
RecvSession(
this._isTestnet,
this._receiver,
this._walletId,
this._status,
);

factory RecvSession.fromJson(Map<String, dynamic> json) {
return RecvSession(
json['isTestnet'] as bool,
Receiver.fromJson(json['receiver'] as String),
json['walletId'] as String,
json['status'] != null
? PayjoinSessionStatus.values.byName(json['status'] as String)
: null,
);
}

final bool _isTestnet;
final Receiver _receiver;
final String _walletId;
final PayjoinSessionStatus? _status;

bool get isTestnet => _isTestnet;
Receiver get receiver => _receiver;
String get walletId => _walletId;
PayjoinSessionStatus? get status => _status;

Map<String, dynamic> toJson() {
return {
'isTestnet': isTestnet,
'receiver': receiver.toJson(),
'walletId': walletId,
'status': _status?.name,
};
}
}
Expand Down
47 changes: 47 additions & 0 deletions lib/_pkg/payjoin/storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class PayjoinStorage {
isTestnet,
receiver,
walletId,
PayjoinSessionStatus.pending,
);

await _hiveStorage.saveValue(
Expand Down Expand Up @@ -73,6 +74,28 @@ class PayjoinStorage {
}
}

Future<Err?> markReceiverSessionComplete(String sessionId) async {
try {
final (session, err) = await readReceiverSession(sessionId);
if (err != null) return err;

final updatedSession = RecvSession(
session!.isTestnet,
session.receiver,
session.walletId,
PayjoinSessionStatus.success,
);

await _hiveStorage.saveValue(
key: receiverPrefix + sessionId,
value: jsonEncode(updatedSession.toJson()),
);
return null;
} catch (e) {
return Err(e.toString());
}
}

Future<(List<RecvSession>, Err?)> readAllReceivers() async {
//deleteAllSessions();
try {
Expand Down Expand Up @@ -109,6 +132,7 @@ class PayjoinStorage {
sender,
walletId,
pjUrl,
PayjoinSessionStatus.pending,
);

await _hiveStorage.saveValue(
Expand Down Expand Up @@ -139,6 +163,29 @@ class PayjoinStorage {
}
}

Future<Err?> markSenderSessionComplete(String pjUrl) async {
try {
final (session, err) = await readSenderSession(pjUrl);
if (err != null) return err;

final updatedSession = SendSession(
session!.isTestnet,
session.sender,
session.walletId,
session.pjUri,
PayjoinSessionStatus.success,
);

await _hiveStorage.saveValue(
key: senderPrefix + pjUrl,
value: jsonEncode(updatedSession.toJson()),
);
return null;
} catch (e) {
return Err(e.toString());
}
}

Future<(List<SendSession>, Err?)> readAllSenders() async {
try {
final (allData, err) = await _hiveStorage.getAll();
Expand Down

0 comments on commit 4ae408b

Please sign in to comment.