Skip to content

Commit ee9ec36

Browse files
jorgecuestaRawthiL
authored andcommitted
Rebase onto main
1 parent e9c1cb6 commit ee9ec36

File tree

1 file changed

+69
-34
lines changed

1 file changed

+69
-34
lines changed

pkg/relayer/cmd/cmd_relay.go

Lines changed: 69 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,12 @@ func runRelay(cmd *cobra.Command, args []string) error {
163163
logger.Error().Err(err).Msg("❌ Error connecting to gRPC")
164164
return err
165165
}
166-
defer grpcConn.Close()
166+
defer func(grpcConn *grpc.ClientConn) {
167+
err := grpcConn.Close()
168+
if err != nil {
169+
logger.Error().Err(err).Msg("❌ Error closing gRPC connection")
170+
}
171+
}(grpcConn)
167172
logger.Info().Msgf("✅ gRPC connection initialized: %v", grpcConn)
168173

169174
// Create a connection to the POKT full node
@@ -342,43 +347,63 @@ func runRelay(cmd *cobra.Command, args []string) error {
342347
}
343348
logger.Info().Msgf("✅ Endpoint URL parsed: %v", reqUrl)
344349

345-
// Create the HTTP request with the relay request body
346-
httpReq := &http.Request{
347-
Method: http.MethodPost,
348-
URL: reqUrl,
349-
Body: io.NopCloser(bytes.NewReader(relayReqBz)),
350-
}
350+
// Send multiple requests sequentially as specified by the count flag
351+
for i := 1; i <= flagRelayRequestCount; i++ {
352+
// Create the HTTP request with the relay request body
353+
httpReq := &http.Request{
354+
Method: http.MethodPost,
355+
URL: reqUrl,
356+
Body: io.NopCloser(bytes.NewReader(relayReqBz)),
357+
}
351358

352-
// Send the request HTTP request containing the signed relay request
353-
httpResp, err := http.DefaultClient.Do(httpReq)
354-
if err != nil {
355-
logger.Error().Err(err).Msg("❌ Error sending relay request")
356-
return err
357-
}
358-
defer httpResp.Body.Close()
359+
// Send the request HTTP request containing the signed relay request
360+
httpResp, err := http.DefaultClient.Do(httpReq)
361+
if err != nil {
362+
logger.Error().Err(err).Msg("❌ Error sending relay request")
363+
proxy.CloseRequestBody(logger, httpResp.Body)
364+
continue
365+
}
359366

360-
// Ensure the supplier operator signature is present
361-
supplierSignerAddress := signedRelayReq.Meta.SupplierOperatorAddress
362-
if supplierSignerAddress == "" {
363-
logger.Error().Msg("❌ Supplier operator signature is missing")
364-
return errors.New("Relay response missing supplier operator signature")
365-
}
366-
// Ensure the supplier operator address matches the expected address
367-
if flagRelaySupplier == "" {
368-
logger.Warn().Msg("⚠️ Supplier operator address not specified, skipping signature check")
369-
} else if supplierSignerAddress != flagRelaySupplier {
370-
logger.Error().Msgf("❌ Supplier operator address %s does not match the expected address %s", supplierSignerAddress, flagRelaySupplier)
371-
return errors.New("Relay response supplier operator signature does not match")
372-
}
367+
bodyCloseErr := httpResp.Body.Close()
368+
if bodyCloseErr != nil {
369+
logger.Error().Err(bodyCloseErr).Msg("❌ Error closing response body")
370+
proxy.CloseRequestBody(logger, httpResp.Body)
371+
continue
372+
}
373373

374-
logger.Info().Msgf("🔍 Backend response header, Content-Type: %s", httpResp.Header.Get("Content-Type"))
374+
// Ensure the supplier operator signature is present
375+
supplierSignerAddress := signedRelayReq.Meta.SupplierOperatorAddress
376+
if supplierSignerAddress == "" {
377+
logger.Error().Msg("❌ Supplier operator signature is missing")
378+
proxy.CloseRequestBody(logger, httpResp.Body)
379+
continue
380+
}
381+
// Ensure the supplier operator address matches the expected address
382+
if flagRelaySupplier == "" {
383+
logger.Warn().Msg("⚠️ Supplier operator address not specified, skipping signature check")
384+
} else if supplierSignerAddress != flagRelaySupplier {
385+
logger.Error().Msgf("❌ Supplier operator address %s does not match the expected address %s", supplierSignerAddress, flagRelaySupplier)
386+
proxy.CloseRequestBody(logger, httpResp.Body)
387+
continue
388+
}
375389

376-
// Handle response according to type
377-
if proxy.IsStreamingResponse(httpResp) {
378-
return processStreamRequest(ctx, httpResp, supplierSignerAddress, accountClient, logger)
379-
} else {
380-
// Normal, non-streaming request
381-
return processNormalRequest(ctx, httpResp, supplierSignerAddress, accountClient, logger)
390+
logger.Info().Msgf("🔍 Backend response header, Content-Type: %s", httpResp.Header.Get("Content-Type"))
391+
392+
// Handle response according to type
393+
if proxy.IsStreamingResponse(httpResp) {
394+
streamErr := processStreamRequest(ctx, httpResp, supplierSignerAddress, accountClient, logger)
395+
proxy.CloseRequestBody(logger, httpResp.Body)
396+
if streamErr != nil {
397+
logger.Error().Err(streamErr).Msg("❌ Stream errored")
398+
}
399+
} else {
400+
// Normal, non-streaming request
401+
reqErr := processNormalRequest(ctx, httpResp, supplierSignerAddress, accountClient, logger)
402+
proxy.CloseRequestBody(logger, httpResp.Body)
403+
if reqErr != nil {
404+
logger.Error().Err(reqErr).Msg("❌ Request errored")
405+
}
406+
}
382407
}
383408

384409
return nil
@@ -471,6 +496,14 @@ func processNormalRequest(ctx context.Context,
471496
}
472497
logger.Info().Msgf("✅ Backend response status code: %v", backendHttpResponse.StatusCode)
473498

499+
// Log response details
500+
if flagRelayRequestCount > 1 {
501+
logger.Info().Msgf("✅ Status code %d, Response size %d bytes", backendHttpResponse.StatusCode, len(respBz))
502+
} else {
503+
logger.Info().Msgf("✅ Backend response status code: %v", backendHttpResponse.StatusCode)
504+
logger.Info().Msgf("✅ Response read %d bytes", len(respBz))
505+
}
506+
474507
err = unmarshalAndPrintResponse(backendHttpResponse.BodyBz, logger)
475508
if err != nil {
476509
return err
@@ -512,6 +545,7 @@ func unmarshalAndPrintResponse(BodyBz []byte, logger polylog.Logger) error {
512545
logger.Error().Err(err).Msg("❌ Error deserializing backend response payload")
513546
return err
514547
}
548+
515549
logger.Info().Msgf("✅ Deserialized response body as JSON map: %+v", jsonMap)
516550

517551
// If "jsonrpc" key exists, try to further deserialize "result"
@@ -535,6 +569,7 @@ func unmarshalAndPrintResponse(BodyBz []byte, logger polylog.Logger) error {
535569

536570
return nil
537571
}
572+
538573
// If a supplier is specified but not in the session, try to fetch it directly.
539574
// TODO_UPNEXT(@olshansk): Add support for sending a relay to a supplier that is not in the session.
540575
// This will require starting a relayminer in debug mode to avoid validating the session header.

0 commit comments

Comments
 (0)