2025-08-23 08:16:43 +03:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"io"
|
|
|
|
|
|
|
|
|
|
"hash-of-wisdom/internal/pow/challenge"
|
|
|
|
|
)
|
|
|
|
|
|
2025-08-23 09:00:14 +03:00
|
|
|
|
2025-08-23 08:16:43 +03:00
|
|
|
// 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
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-23 09:00:14 +03:00
|
|
|
// Encode writes a challenge request to the writer
|
|
|
|
|
func (r *ChallengeRequest) Encode(w io.Writer) error {
|
|
|
|
|
return encode(w, ChallengeRequestType, nil)
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-23 08:16:43 +03:00
|
|
|
// 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)
|
|
|
|
|
}
|
2025-08-23 09:00:14 +03:00
|
|
|
|
|
|
|
|
// Encode writes a solution request to the writer
|
|
|
|
|
func (r *SolutionRequest) Encode(w io.Writer) error {
|
|
|
|
|
return encode(w, SolutionRequestType, r)
|
|
|
|
|
}
|