From ffc23c362bdda5fec5b12fe7bb630c77af78bc42 Mon Sep 17 00:00:00 2001 From: Savely Krendelhoff Date: Fri, 22 Aug 2025 21:03:42 +0700 Subject: [PATCH] Define presentation layer types --- internal/protocol/types.go | 64 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 internal/protocol/types.go diff --git a/internal/protocol/types.go b/internal/protocol/types.go new file mode 100644 index 0000000..4afd17f --- /dev/null +++ b/internal/protocol/types.go @@ -0,0 +1,64 @@ +package protocol + +import ( + "hash-of-wisdom/internal/pow/challenge" + "hash-of-wisdom/internal/quotes" +) + +// MessageType represents the type of protocol message +type MessageType byte + +const ( + ChallengeRequest MessageType = 0x01 + ChallengeResponse MessageType = 0x02 + SolutionRequest MessageType = 0x03 + QuoteResponse MessageType = 0x04 + ErrorResponse MessageType = 0x05 +) + +// Message represents a protocol message with type and payload +type Message struct { + Type MessageType + Payload []byte +} + +// ChallengeRequestPayload is empty (no payload for challenge requests) +type ChallengeRequestPayload struct{} + +// ChallengeResponsePayload is the direct challenge object (not wrapped) +type ChallengeResponsePayload challenge.Challenge + +// SolutionRequestPayload contains the client's solution attempt +type SolutionRequestPayload struct { + Challenge challenge.Challenge `json:"challenge"` + Nonce uint64 `json:"nonce"` +} + +// QuoteResponsePayload is the direct quote object (not wrapped) +type QuoteResponsePayload quotes.Quote + +// ErrorResponsePayload contains error information +type ErrorResponsePayload struct { + Code string `json:"code"` + Message string `json:"message"` + RetryAfter int `json:"retry_after,omitempty"` + Details map[string]string `json:"details,omitempty"` +} + +// Error codes as defined in protocol specification +const ( + ErrMalformedMessage = "MALFORMED_MESSAGE" + ErrInvalidChallenge = "INVALID_CHALLENGE" + ErrInvalidSolution = "INVALID_SOLUTION" + ErrExpiredChallenge = "EXPIRED_CHALLENGE" + ErrRateLimited = "RATE_LIMITED" + ErrServerError = "SERVER_ERROR" + ErrTooManyConnections = "TOO_MANY_CONNECTIONS" + ErrDifficultyTooHigh = "DIFFICULTY_TOO_HIGH" +) + +// Protocol constants +const ( + MaxPayloadSize = 8 * 1024 // 8KB maximum payload size + HeaderSize = 5 // 1 byte type + 4 bytes length +)