generatePoW function

Future<String> generatePoW(
  1. Hash hash,
  2. int? difficulty
)

Implementation

Future<String> generatePoW(Hash hash, int? difficulty) async {
  if (_generatePoWFunction == null) {
    initializePoWLinks();
  }

  final port = ReceivePort();
  final args = _GeneratePowFunctionArguments(hash, difficulty, port.sendPort);
  Isolate? isolate = await Isolate.spawn<_GeneratePowFunctionArguments>(
      _generatePowFunction, args,
      onError: port.sendPort, onExit: port.sendPort);
  StreamSubscription? sub;
  // Listening for messages on port
  var completer = Completer<String>();

  sub = port.listen((data) async {
    // Cancel a subscription after message received called
    if (data != null) {
      var ansHex = data.toString();
      completer.complete(ansHex);
      await sub?.cancel();
      logger.info(
          'Generated nonce $ansHex for hash ${hash.toString()} with difficulty $difficulty');
      if (isolate != null) {
        isolate!.kill(priority: Isolate.immediate);
        isolate = null;
      }
    }
  });
  return completer.future;
}