@@ -147,22 +147,42 @@ func handleValidate(c *gin.Context) {
147147
148148 defer closeWebSocket (conn )
149149
150- msg , err := readWebSocketMessage (conn )
151- if err != nil {
150+ // First, try to read as a batch request
151+ var rawMsg json.RawMessage
152+ if err := conn .ReadJSON (& rawMsg ); err != nil {
153+ log .Error (err )
154+ sendWsResponse (wsError , "Failed to read JSON from WebSocket connection" , "0" , conn )
152155 return
153156 }
154157
155- //peerID, err := getPeerID(msg, conn)
156- //if err != nil {
157- // return
158- //}
158+ // Check if it's a batch request
159+ var batchReq BatchRequest
160+ if err := json .Unmarshal (rawMsg , & batchReq ); err == nil && batchReq .Type == "batch" {
161+ // Handle batch validation
162+ handleBatchValidation (batchReq , conn )
163+ return
164+ }
159165
160- //err = connectToPeer(peerID, conn, msg)
161- //if err != nil {
162- // return
163- //}
166+ // Otherwise, handle as single validation
167+ var msg message
168+ if err := json .Unmarshal (rawMsg , & msg ); err != nil {
169+ log .Error (err )
170+ sendWsResponse (wsError , "Failed to parse validation request" , "0" , conn )
171+ return
172+ }
173+
174+ // Normalize the message to handle both uppercase and lowercase field names
175+ msg .Normalize ()
176+
177+ // Process single validation
178+ processSingleValidation (& msg , conn )
179+ log .Info ("Exiting handleValidate" )
180+ }
164181
182+ // processSingleValidation handles a single validation request
183+ func processSingleValidation (msg * message , conn * websocket.Conn ) {
165184 salt := msg .SALT
185+ var err error
166186 if salt == "" {
167187 salt , err = createRandomHash (conn )
168188 if err != nil {
@@ -187,8 +207,100 @@ func handleValidate(c *gin.Context) {
187207 if err != nil {
188208 return
189209 }
190- sendWsResponse (status , status , formatElapsed (elapsed ), conn )
191- log .Info ("Exiting handleValidate" )
210+
211+ // Send response with context for honeycomb-spkcc compatibility
212+ if msg .Bn > 0 || msg .Name != "" || msg .CID != "" {
213+ sendWsResponseWithContext (status , status , formatElapsed (elapsed ), msg .Name , msg .CID , msg .Bn , conn )
214+ } else {
215+ sendWsResponse (status , status , formatElapsed (elapsed ), conn )
216+ }
217+ }
218+
219+ // handleBatchValidation processes multiple validation requests
220+ func handleBatchValidation (batchReq BatchRequest , conn * websocket.Conn ) {
221+ results := make ([]ExampleResponse , 0 , len (batchReq .Validations ))
222+
223+ for _ , msg := range batchReq .Validations {
224+ // Normalize the message to handle both uppercase and lowercase field names
225+ msg .Normalize ()
226+ salt := msg .SALT
227+ var err error
228+ if salt == "" {
229+ salt , err = createRandomHash (conn )
230+ if err != nil {
231+ results = append (results , ExampleResponse {
232+ Status : wsError ,
233+ Message : "Failed to create hash" ,
234+ Elapsed : "0" ,
235+ Name : msg .Name ,
236+ CID : msg .CID ,
237+ Bn : msg .Bn ,
238+ })
239+ continue
240+ }
241+ } else {
242+ salt = salt + msg .CID + msg .Name
243+ }
244+
245+ proofJson , err := createProofRequest (salt , msg .CID , conn , msg .Name )
246+ if err != nil {
247+ results = append (results , ExampleResponse {
248+ Status : wsError ,
249+ Message : "Failed to create proof request" ,
250+ Elapsed : "0" ,
251+ Name : msg .Name ,
252+ CID : msg .CID ,
253+ Bn : msg .Bn ,
254+ })
255+ continue
256+ }
257+
258+ err = sendProofRequest (salt , proofJson , msg .Name , conn )
259+ if err != nil {
260+ results = append (results , ExampleResponse {
261+ Status : wsError ,
262+ Message : "Failed to send proof request" ,
263+ Elapsed : "0" ,
264+ Name : msg .Name ,
265+ CID : msg .CID ,
266+ Bn : msg .Bn ,
267+ })
268+ continue
269+ }
270+
271+ status , elapsed , err := waitForProofStatus (salt , msg .CID , conn )
272+ if err != nil {
273+ results = append (results , ExampleResponse {
274+ Status : wsError ,
275+ Message : "Validation timeout" ,
276+ Elapsed : "0" ,
277+ Name : msg .Name ,
278+ CID : msg .CID ,
279+ Bn : msg .Bn ,
280+ })
281+ continue
282+ }
283+
284+ results = append (results , ExampleResponse {
285+ Status : status ,
286+ Message : status ,
287+ Elapsed : formatElapsed (elapsed ),
288+ Name : msg .Name ,
289+ CID : msg .CID ,
290+ Bn : msg .Bn ,
291+ })
292+ }
293+
294+ // Send batch response
295+ localdata .Lock .Lock ()
296+ err := conn .WriteJSON (BatchResponse {
297+ Type : "batch" ,
298+ Results : results ,
299+ })
300+ localdata .Lock .Unlock ()
301+ if err != nil {
302+ log .Println ("Error writing batch response:" , err )
303+ }
192304}
193305
194306func handleMessaging (c * gin.Context ) {
0 commit comments