From c147bc7fe4da0d6af3f40dd394c277a3c51d12aa Mon Sep 17 00:00:00 2001 From: Savely Krendelhoff Date: Sat, 23 Aug 2025 12:16:43 +0700 Subject: [PATCH] [PHASE-5] Move requests to separate file and implement decoding --- internal/protocol/requests.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 internal/protocol/requests.go diff --git a/internal/protocol/requests.go b/internal/protocol/requests.go new file mode 100644 index 0000000..3961223 --- /dev/null +++ b/internal/protocol/requests.go @@ -0,0 +1,35 @@ +package protocol + +import ( + "encoding/json" + "io" + + "hash-of-wisdom/internal/pow/challenge" +) + +// ChallengeRequest is empty (no payload for challenge requests) +type ChallengeRequest struct{} + +// Decode reads a challenge request from the payload stream +func (r *ChallengeRequest) Decode(stream io.Reader) error { + // Challenge requests have no payload to decode + return nil +} + +// SolutionRequest contains the client's solution attempt +type SolutionRequest struct { + Challenge challenge.Challenge `json:"challenge"` + Nonce uint64 `json:"nonce"` +} + +// Decode reads a solution request from the payload stream +func (r *SolutionRequest) Decode(stream io.Reader) error { + if stream == nil { + // json.NewDecoder panics on nil reader + return io.EOF + } + + // Parse JSON directly from stream + decoder := json.NewDecoder(stream) + return decoder.Decode(r) +}