Source code for codegrade.models.pow_challenge_response
"""The module that defines the ``PowChallengeResponse`` model.
SPDX-License-Identifier: AGPL-3.0-only OR BSD-3-Clause-Clear
"""
from __future__ import annotations
import typing as t
from dataclasses import dataclass, field
import cg_request_args as rqa
from ..utils import to_dict
[docs]
@dataclass
class PowChallengeResponse:
"""A proof-of-work challenge for the client to solve."""
#: The challenge nonce. Include as `pow_nonce` in the request body.
nonce: str
#: Number of leading zero bits required in SHA-256(nonce + ":" + solution).
difficulty: int
raw_data: t.Optional[t.Dict[str, t.Any]] = field(init=False, repr=False)
data_parser: t.ClassVar[t.Any] = rqa.Lazy(
lambda: rqa.FixedMapping(
rqa.RequiredArgument(
"nonce",
rqa.SimpleValue.str,
doc="The challenge nonce. Include as `pow_nonce` in the request body.",
),
rqa.RequiredArgument(
"difficulty",
rqa.SimpleValue.int,
doc='Number of leading zero bits required in SHA-256(nonce + ":" + solution).',
),
).use_readable_describe(True)
)
def to_dict(self) -> t.Dict[str, t.Any]:
res: t.Dict[str, t.Any] = {
"nonce": to_dict(self.nonce),
"difficulty": to_dict(self.difficulty),
}
return res
@classmethod
def from_dict(
cls: t.Type[PowChallengeResponse], d: t.Dict[str, t.Any]
) -> PowChallengeResponse:
parsed = cls.data_parser.try_parse(d)
res = cls(
nonce=parsed.nonce,
difficulty=parsed.difficulty,
)
res.raw_data = d
return res