diff --git a/cmd/server/main.go b/cmd/server/main.go index 332497c..32c8713 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -55,7 +55,7 @@ func main() { // Create server configuration serverConfig := &server.Config{ - Address: cfg.Server.Address, + Address: cfg.Server.Address, Timeouts: server.TimeoutConfig{ Read: cfg.Server.Timeouts.Read, Write: cfg.Server.Timeouts.Write, diff --git a/internal/application/application_test.go b/internal/application/application_test.go index 6017a28..ecdabc2 100644 --- a/internal/application/application_test.go +++ b/internal/application/application_test.go @@ -32,9 +32,9 @@ func TestWisdomApplication_HandleMessage_UnsupportedType(t *testing.T) { ctx := context.Background() msg := &protocol.Message{ - Type: protocol.MessageType(0xFF), // Invalid type - PayloadLength: 0, - PayloadStream: nil, + Type: protocol.MessageType(0xFF), // Invalid type + PayloadLength: 0, + PayloadStream: nil, } response, err := app.HandleMessage(ctx, msg) @@ -64,9 +64,9 @@ func TestWisdomApplication_HandleChallengeRequest_Success(t *testing.T) { ctx := context.Background() msg := &protocol.Message{ - Type: protocol.ChallengeRequestType, - PayloadLength: 0, - PayloadStream: nil, + Type: protocol.ChallengeRequestType, + PayloadLength: 0, + PayloadStream: nil, } response, err := app.HandleMessage(ctx, msg) @@ -89,9 +89,9 @@ func TestWisdomApplication_HandleChallengeRequest_ServiceError(t *testing.T) { ctx := context.Background() msg := &protocol.Message{ - Type: protocol.ChallengeRequestType, - PayloadLength: 0, - PayloadStream: nil, + Type: protocol.ChallengeRequestType, + PayloadLength: 0, + PayloadStream: nil, } response, err := app.HandleMessage(ctx, msg) @@ -138,9 +138,9 @@ func TestWisdomApplication_HandleSolutionRequest_Success(t *testing.T) { ctx := context.Background() msg := &protocol.Message{ - Type: protocol.SolutionRequestType, - PayloadLength: uint32(len(payloadJSON)), - PayloadStream: bytes.NewReader(payloadJSON), + Type: protocol.SolutionRequestType, + PayloadLength: uint32(len(payloadJSON)), + PayloadStream: bytes.NewReader(payloadJSON), } response, err := app.HandleMessage(ctx, msg) @@ -161,9 +161,9 @@ func TestWisdomApplication_HandleSolutionRequest_InvalidJSON(t *testing.T) { invalidJSON := []byte("invalid json") ctx := context.Background() msg := &protocol.Message{ - Type: protocol.SolutionRequestType, - PayloadLength: uint32(len(invalidJSON)), - PayloadStream: bytes.NewReader(invalidJSON), + Type: protocol.SolutionRequestType, + PayloadLength: uint32(len(invalidJSON)), + PayloadStream: bytes.NewReader(invalidJSON), } response, err := app.HandleMessage(ctx, msg) @@ -205,9 +205,9 @@ func TestWisdomApplication_HandleSolutionRequest_VerificationFailed(t *testing.T ctx := context.Background() msg := &protocol.Message{ - Type: protocol.SolutionRequestType, - PayloadLength: uint32(len(payloadJSON)), - PayloadStream: bytes.NewReader(payloadJSON), + Type: protocol.SolutionRequestType, + PayloadLength: uint32(len(payloadJSON)), + PayloadStream: bytes.NewReader(payloadJSON), } response, err := app.HandleMessage(ctx, msg) @@ -250,9 +250,9 @@ func TestWisdomApplication_HandleSolutionRequest_QuoteServiceError(t *testing.T) ctx := context.Background() msg := &protocol.Message{ - Type: protocol.SolutionRequestType, - PayloadLength: uint32(len(payloadJSON)), - PayloadStream: bytes.NewReader(payloadJSON), + Type: protocol.SolutionRequestType, + PayloadLength: uint32(len(payloadJSON)), + PayloadStream: bytes.NewReader(payloadJSON), } response, err := app.HandleMessage(ctx, msg) @@ -279,9 +279,9 @@ func TestWisdomApplication_HandleMessage_ContextCancellation(t *testing.T) { mockService.On("GenerateChallenge", mock.Anything, "quotes").Return(nil, context.Canceled) msg := &protocol.Message{ - Type: protocol.ChallengeRequestType, - PayloadLength: 0, - PayloadStream: nil, + Type: protocol.ChallengeRequestType, + PayloadLength: 0, + PayloadStream: nil, } response, err := app.HandleMessage(ctx, msg) diff --git a/internal/config/config.go b/internal/config/config.go index 2c86969..31d1564 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -7,16 +7,16 @@ import ( ) type Config struct { - Server ServerConfig `yaml:"server"` - PoW PoWConfig `yaml:"pow"` - Quotes QuotesConfig `yaml:"quotes"` + Server ServerConfig `yaml:"server"` + PoW PoWConfig `yaml:"pow"` + Quotes QuotesConfig `yaml:"quotes"` Metrics MetricsConfig `yaml:"metrics"` Logging LoggingConfig `yaml:"logging"` } type ServerConfig struct { - Address string `yaml:"address" env:"SERVER_ADDRESS" env-default:":8080"` - Timeouts TimeoutConfig `yaml:"timeouts"` + Address string `yaml:"address" env:"SERVER_ADDRESS" env-default:":8080"` + Timeouts TimeoutConfig `yaml:"timeouts"` } type TimeoutConfig struct { @@ -26,10 +26,10 @@ type TimeoutConfig struct { } type PoWConfig struct { - Difficulty int `yaml:"difficulty" env:"POW_DIFFICULTY" env-default:"4"` - MaxDifficulty int `yaml:"max_difficulty" env:"POW_MAX_DIFFICULTY" env-default:"10"` + Difficulty int `yaml:"difficulty" env:"POW_DIFFICULTY" env-default:"4"` + MaxDifficulty int `yaml:"max_difficulty" env:"POW_MAX_DIFFICULTY" env-default:"10"` TTL time.Duration `yaml:"ttl" env:"POW_TTL" env-default:"5m"` - HMACSecret string `yaml:"hmac_secret" env:"POW_HMAC_SECRET" env-default:"development-secret-change-in-production"` + HMACSecret string `yaml:"hmac_secret" env:"POW_HMAC_SECRET" env-default:"development-secret-change-in-production"` } type QuotesConfig struct { diff --git a/internal/pow/challenge/types.go b/internal/pow/challenge/types.go index 83b8298..4fd58e8 100644 --- a/internal/pow/challenge/types.go +++ b/internal/pow/challenge/types.go @@ -68,8 +68,8 @@ func (c *Challenge) VerifySolution(nonce uint64) bool { // hasLeadingZeroBits checks if hash has the required number of leading zero bits func hasLeadingZeroBits(hash []byte, difficulty int) bool { - full := difficulty >> 3 // number of whole zero bytes - rem := uint(difficulty & 7) // remaining leading zero bits + full := difficulty >> 3 // number of whole zero bytes + rem := uint(difficulty & 7) // remaining leading zero bits for i := range full { if hash[i] != 0 { diff --git a/internal/protocol/constants.go b/internal/protocol/constants.go index 015853c..d611ba3 100644 --- a/internal/protocol/constants.go +++ b/internal/protocol/constants.go @@ -4,8 +4,8 @@ package protocol type MessageType byte const ( - ChallengeRequestType MessageType = 0x01 - SolutionRequestType MessageType = 0x03 + ChallengeRequestType MessageType = 0x01 + SolutionRequestType MessageType = 0x03 // Response types (for responses.go) ChallengeResponseType MessageType = 0x02 QuoteResponseType MessageType = 0x04 @@ -14,14 +14,14 @@ const ( // 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" + 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 diff --git a/internal/protocol/message_decoder.go b/internal/protocol/message_decoder.go index f22441e..0181660 100644 --- a/internal/protocol/message_decoder.go +++ b/internal/protocol/message_decoder.go @@ -43,8 +43,8 @@ func (d *MessageDecoder) Decode(r io.Reader) (*Message, error) { } return &Message{ - Type: msgType, - PayloadLength: payloadLength, - PayloadStream: payloadStream, + Type: msgType, + PayloadLength: payloadLength, + PayloadStream: payloadStream, }, nil } diff --git a/internal/protocol/message_decoder_test.go b/internal/protocol/message_decoder_test.go index 1acc8c1..c98196a 100644 --- a/internal/protocol/message_decoder_test.go +++ b/internal/protocol/message_decoder_test.go @@ -13,11 +13,11 @@ func TestMessageDecoder_Decode_Header(t *testing.T) { decoder := NewMessageDecoder() tests := []struct { - name string - data []byte - wantType MessageType - wantLength uint32 - wantErr string + name string + data []byte + wantType MessageType + wantLength uint32 + wantErr string }{ { name: "challenge request with empty payload", diff --git a/internal/protocol/requests.go b/internal/protocol/requests.go index 588aadd..f825b61 100644 --- a/internal/protocol/requests.go +++ b/internal/protocol/requests.go @@ -7,7 +7,6 @@ import ( "hash-of-wisdom/internal/pow/challenge" ) - // ChallengeRequest is empty (no payload for challenge requests) type ChallengeRequest struct{} diff --git a/internal/protocol/responses.go b/internal/protocol/responses.go index ba59dda..1fb2e49 100644 --- a/internal/protocol/responses.go +++ b/internal/protocol/responses.go @@ -8,7 +8,6 @@ import ( "hash-of-wisdom/internal/quotes" ) - // ChallengeResponse represents a challenge response type ChallengeResponse struct { Challenge *challenge.Challenge diff --git a/internal/protocol/roundtrip_test.go b/internal/protocol/roundtrip_test.go index ef4b5fa..2e34c2b 100644 --- a/internal/protocol/roundtrip_test.go +++ b/internal/protocol/roundtrip_test.go @@ -113,7 +113,6 @@ func TestChallengeRequest_EmptyPayload(t *testing.T) { require.NoError(t, err) } - func TestPayloadStream_LimitedRead(t *testing.T) { decoder := NewMessageDecoder() diff --git a/internal/protocol/types.go b/internal/protocol/types.go index bb2a8ee..5bcb240 100644 --- a/internal/protocol/types.go +++ b/internal/protocol/types.go @@ -4,7 +4,7 @@ import "io" // Message represents a protocol message with type and payload stream type Message struct { - Type MessageType - PayloadLength uint32 - PayloadStream io.Reader + Type MessageType + PayloadLength uint32 + PayloadStream io.Reader } diff --git a/internal/server/tcp.go b/internal/server/tcp.go index 3d3008a..2b50450 100644 --- a/internal/server/tcp.go +++ b/internal/server/tcp.go @@ -30,7 +30,6 @@ type TCPServer struct { // Option is a functional option for configuring TCPServer type option func(*TCPServer) - // WithLogger sets a custom logger func WithLogger(logger *slog.Logger) option { return func(s *TCPServer) { @@ -54,7 +53,6 @@ func NewTCPServer(wisdomService *service.WisdomService, config *Config, opts ... return server } - // Start starts the TCP server func (s *TCPServer) Start(ctx context.Context) error { listener, err := net.Listen("tcp", s.config.Address) diff --git a/internal/service/wisdom.go b/internal/service/wisdom.go index 5cc2ca7..013eae4 100644 --- a/internal/service/wisdom.go +++ b/internal/service/wisdom.go @@ -9,11 +9,11 @@ import ( ) var ( - ErrResourceRequired = errors.New("resource is required") + ErrResourceRequired = errors.New("resource is required") ErrUnsupportedResource = errors.New("unsupported resource") - ErrSolutionRequired = errors.New("solution is required") - ErrInvalidChallenge = errors.New("invalid challenge") - ErrInvalidSolution = errors.New("invalid proof of work solution") + ErrSolutionRequired = errors.New("solution is required") + ErrInvalidChallenge = errors.New("invalid challenge") + ErrInvalidSolution = errors.New("invalid proof of work solution") ) type ChallengeGenerator interface { diff --git a/internal/service/wisdom_test.go b/internal/service/wisdom_test.go index d98766e..46423dc 100644 --- a/internal/service/wisdom_test.go +++ b/internal/service/wisdom_test.go @@ -70,15 +70,15 @@ func TestWisdomService_GenerateChallenge(t *testing.T) { func TestWisdomService_VerifySolution(t *testing.T) { tests := []struct { - name string - solution *challenge.Solution - wantErr error + name string + solution *challenge.Solution + wantErr error setupMocks func(*mocks.MockChallengeVerifier) }{ { - name: "nil solution", - solution: nil, - wantErr: ErrSolutionRequired, + name: "nil solution", + solution: nil, + wantErr: ErrSolutionRequired, setupMocks: func(mv *mocks.MockChallengeVerifier) {}, }, { @@ -93,17 +93,17 @@ func TestWisdomService_VerifySolution(t *testing.T) { }, }, { - name: "invalid solution", + name: "invalid solution", solution: createInvalidSolution(t), - wantErr: ErrInvalidSolution, + wantErr: ErrInvalidSolution, setupMocks: func(mv *mocks.MockChallengeVerifier) { mv.EXPECT().VerifyChallenge(mock.Anything).Return(nil).Once() }, }, { - name: "valid solution", + name: "valid solution", solution: createValidSolution(t), - wantErr: nil, + wantErr: nil, setupMocks: func(mv *mocks.MockChallengeVerifier) { mv.EXPECT().VerifyChallenge(mock.Anything).Return(nil).Once() }, diff --git a/internal/service/workflow_test.go b/internal/service/workflow_test.go index bd0c356..5e62e27 100644 --- a/internal/service/workflow_test.go +++ b/internal/service/workflow_test.go @@ -284,8 +284,8 @@ func TestWisdomService_InvalidSolutions(t *testing.T) { func TestWisdomService_UnsuccessfulFlows(t *testing.T) { tests := []struct { - name string - difficulty int + name string + difficulty int createSolution func(*challenge.Challenge, *challenge.Solution) *challenge.Solution }{ { diff --git a/test/integration/slowloris_test.go b/test/integration/slowloris_test.go index be3dd04..4f8700c 100644 --- a/test/integration/slowloris_test.go +++ b/test/integration/slowloris_test.go @@ -14,7 +14,7 @@ import ( func TestSlowlorisProtection_SlowReader(t *testing.T) { // Setup server with very short read timeout for testing serverConfig := &server.Config{ - Address: ":0", + Address: ":0", Timeouts: server.TimeoutConfig{ Read: 100 * time.Millisecond, Write: 5 * time.Second, @@ -47,11 +47,10 @@ func TestSlowlorisProtection_SlowReader(t *testing.T) { assert.Error(t, err, "Connection should be closed due to slow reading") } - func TestSlowlorisProtection_SlowConnectionTimeout(t *testing.T) { // Setup server with very short connection timeout for testing serverConfig := &server.Config{ - Address: ":0", + Address: ":0", Timeouts: server.TimeoutConfig{ Read: 5 * time.Second, Write: 5 * time.Second, @@ -80,7 +79,7 @@ func TestSlowlorisProtection_SlowConnectionTimeout(t *testing.T) { func TestSlowlorisProtection_MultipleSlowClients(t *testing.T) { // Setup server with short timeouts serverConfig := &server.Config{ - Address: ":0", + Address: ":0", Timeouts: server.TimeoutConfig{ Read: 1 * time.Second, Write: 1 * time.Second, diff --git a/test/integration/timeout_test.go b/test/integration/timeout_test.go index d873c8f..7f5668d 100644 --- a/test/integration/timeout_test.go +++ b/test/integration/timeout_test.go @@ -15,7 +15,7 @@ import ( func TestTCPServer_TimeoutProtection_SlowReader(t *testing.T) { // Setup server with very short read timeout for testing serverConfig := &server.Config{ - Address: ":0", + Address: ":0", Timeouts: server.TimeoutConfig{ Read: 500 * time.Millisecond, Write: 5 * time.Second, @@ -50,7 +50,7 @@ func TestTCPServer_TimeoutProtection_SlowReader(t *testing.T) { func TestTCPServer_TimeoutProtection_ConnectionTimeout(t *testing.T) { // Setup server with very short connection timeout serverConfig := &server.Config{ - Address: ":0", + Address: ":0", Timeouts: server.TimeoutConfig{ Read: 5 * time.Second, Write: 5 * time.Second, @@ -99,7 +99,7 @@ func TestTCPServer_NormalOperation_WithinTimeouts(t *testing.T) { func TestTCPServer_MultipleConnections_IndependentTimeouts(t *testing.T) { serverConfig := &server.Config{ - Address: ":0", + Address: ":0", Timeouts: server.TimeoutConfig{ Read: 1 * time.Second, Write: 5 * time.Second, @@ -148,7 +148,7 @@ func TestTCPServer_MultipleConnections_IndependentTimeouts(t *testing.T) { // Helper function to create test server with default config func setupTestServer(t *testing.T) *server.TCPServer { serverConfig := &server.Config{ - Address: ":0", + Address: ":0", Timeouts: server.TimeoutConfig{ Read: 5 * time.Second, Write: 5 * time.Second,