Exemple #1
0
    async def new_challenge(self,
                            new_challenge: harvester_protocol.NewChallenge):
        """
        The harvester receives a new challenge from the farmer, and looks up the quality
        for any proofs of space that are are found in the plots. If proofs are found, a
        ChallengeResponse message is sent for each of the proofs found.
        """

        challenge_size = len(new_challenge.challenge_hash)
        if challenge_size != 32:
            raise ValueError(
                f"Invalid challenge size {challenge_size}, 32 was expected")
        all_responses = []
        for filename, prover in self.provers.items():
            try:
                quality_strings = prover.get_qualities_for_challenge(
                    new_challenge.challenge_hash)
            except RuntimeError:
                log.error(
                    f"Error using prover object on {filename}. Reinitializing prover object."
                )
                quality_strings = None

                try:
                    self.provers[filename] = DiskProver(filename)
                    quality_strings = prover.get_qualities_for_challenge(
                        new_challenge.challenge_hash)
                except RuntimeError:
                    log.error(
                        f"Retry-Error using prover object on {filename}. Giving up."
                    )
                    quality_strings = None

            if quality_strings is not None:
                for index, quality_str in enumerate(quality_strings):
                    quality = ProofOfSpace.quality_str_to_quality(
                        new_challenge.challenge_hash, quality_str)
                    self.challenge_hashes[quality] = (
                        new_challenge.challenge_hash,
                        filename,
                        uint8(index),
                    )
                    response: harvester_protocol.ChallengeResponse = harvester_protocol.ChallengeResponse(
                        new_challenge.challenge_hash, quality,
                        prover.get_size())
                    all_responses.append(response)
        for response in all_responses:
            yield OutboundMessage(
                NodeType.FARMER,
                Message("challenge_response", response),
                Delivery.RESPOND,
            )
 async def lookup_challenge(
         filename: Path, prover: DiskProver
 ) -> List[harvester_protocol.ChallengeResponse]:
     # Exectures a DiskProverLookup in a threadpool, and returns responses
     all_responses: List[harvester_protocol.ChallengeResponse] = []
     quality_strings = await loop.run_in_executor(
         self.executor, blocking_lookup, filename, prover)
     if quality_strings is not None:
         for index, quality_str in enumerate(quality_strings):
             response: harvester_protocol.ChallengeResponse = harvester_protocol.ChallengeResponse(
                 new_challenge.challenge_hash,
                 str(filename),
                 uint8(index),
                 quality_str,
                 prover.get_size(),
             )
             all_responses.append(response)
     return all_responses