fetch server cached anonymity sets for huge increase in firo restore speed

This commit is contained in:
julian 2022-09-08 14:02:07 -06:00
parent f92209c3a5
commit 1e89f4e58b
2 changed files with 24 additions and 11 deletions

View file

@ -1,5 +1,6 @@
import 'package:stackwallet/electrumx_rpc/electrumx.dart'; import 'package:stackwallet/electrumx_rpc/electrumx.dart';
import 'package:stackwallet/hive/db.dart'; import 'package:stackwallet/hive/db.dart';
import 'package:stackwallet/services/coins/firo/firo_wallet.dart';
import 'package:stackwallet/utilities/enums/coin_enum.dart'; import 'package:stackwallet/utilities/enums/coin_enum.dart';
import 'package:stackwallet/utilities/logger.dart'; import 'package:stackwallet/utilities/logger.dart';
import 'package:stackwallet/utilities/prefs.dart'; import 'package:stackwallet/utilities/prefs.dart';
@ -59,6 +60,20 @@ class CachedElectrumX {
"setHash": "", "setHash": "",
"coins": <dynamic>[], "coins": <dynamic>[],
}; };
// try up to 3 times
for (int i = 0; i < 3; i++) {
final result = await getInitialAnonymitySetCache(groupId);
if (result != null) {
set["setHash"] = result["setHash"];
set["blockHash"] = result["blockHash"];
set["coins"] = result["coins"];
Logging.instance.log(
"Populated initial anon set cache for group $groupId",
level: LogLevel.Info);
break;
}
}
} else { } else {
set = Map<String, dynamic>.from(cachedSet); set = Map<String, dynamic>.from(cachedSet);
} }

View file

@ -731,8 +731,10 @@ Future<void> _setTestnetWrapper(bool isTestnet) async {
// setTestnet(isTestnet); // setTestnet(isTestnet);
} }
Future<dynamic> getAnonymity(int groupID) async { Future<Map<String, dynamic>?> getInitialAnonymitySetCache(
Logging.instance.log("getAnonymity", level: LogLevel.Info); String groupID,
) async {
Logging.instance.log("getInitialAnonymitySetCache", level: LogLevel.Info);
final Client client = Client(); final Client client = Client();
try { try {
final uri = Uri.parse("$kStackCommunityNodesEndpoint/getAnonymity"); final uri = Uri.parse("$kStackCommunityNodesEndpoint/getAnonymity");
@ -743,26 +745,22 @@ Future<dynamic> getAnonymity(int groupID) async {
body: jsonEncode({ body: jsonEncode({
"jsonrpc": "2.0", "jsonrpc": "2.0",
"id": "0", "id": "0",
'aset': groupID.toString(), 'aset': groupID,
}), }),
); );
// TODO: should the following be removed for security reasons in production?
Logging.instance
.log(anonSetResult.statusCode.toString(), level: LogLevel.Info);
Logging.instance.log(anonSetResult.body.toString(), level: LogLevel.Info);
final response = jsonDecode(anonSetResult.body.toString()); final response = jsonDecode(anonSetResult.body.toString());
if (response['status'] == 'success') { if (response['status'] == 'success') {
final anonResponse = jsonDecode(response['result'] as String); final anonResponse = jsonDecode(response['result'] as String);
Logging.instance.log(anonResponse, level: LogLevel.Info); final setData = Map<String, dynamic>.from(anonResponse["result"] as Map);
return response; return setData;
} else { } else {
return false; return null;
} }
} catch (e, s) { } catch (e, s) {
Logging.instance.log("$e $s", level: LogLevel.Error); Logging.instance.log("$e $s", level: LogLevel.Error);
return false; return null;
} }
} }