Skip to content

Commit

Permalink
revert-or-fixme: add error log lines
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanchriswhite committed Nov 12, 2023
1 parent 0638e8d commit bb87722
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pkg/appgateserver/jsonrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func (app *appGateServer) handleJSONRPCRelay(
// Read the request body bytes.
payloadBz, err := io.ReadAll(request.Body)
if err != nil {
log.Println("ERROR: Failed reading relay request body")
return err
}

Expand All @@ -34,13 +35,15 @@ func (app *appGateServer) handleJSONRPCRelay(

session, err := app.getCurrentSession(ctx, appAddress, serviceId)
if err != nil {
log.Println("ERROR: Failed getting current session")
return err
}
log.Printf("DEBUG: Current session ID: %s", session.SessionId)

// Get a supplier URL and address for the given service and session.
supplierUrl, supplierAddress, err := app.getRelayerUrl(ctx, serviceId, sharedtypes.RPCType_JSON_RPC, session)
if err != nil {
log.Println("ERROR: Failed getting relayer URL")
return err
}

Expand All @@ -56,25 +59,29 @@ func (app *appGateServer) handleJSONRPCRelay(
// Get the application's signer.
signer, err := app.getRingSingerForAppAddress(ctx, appAddress)
if err != nil {
log.Println("ERROR: Failed getting signer")
return err
}

// Hash and sign the request's signable bytes.
signableBz, err := relayRequest.GetSignableBytes()
if err != nil {
log.Println("ERROR: Failed getting signable bytes")
return err
}

hash := crypto.Sha256(signableBz)
signature, err := signer.Sign(hash)
if err != nil {
log.Println("ERROR: Failed signing relay request")
return err
}
relayRequest.Meta.Signature = signature

// Marshal the relay request to bytes and create a reader to be used as an HTTP request body.
relayRequestBz, err := relayRequest.Marshal()
if err != nil {
log.Println("ERROR: Failed marshaling relay request")
return err
}
relayRequestReader := io.NopCloser(bytes.NewReader(relayRequestBz))
Expand All @@ -91,18 +98,22 @@ func (app *appGateServer) handleJSONRPCRelay(
log.Printf("DEBUG: Sending signed relay request to %s", supplierUrl)
relayHTTPResponse, err := http.DefaultClient.Do(relayHTTPRequest)
if err != nil {
log.Println("ERROR: Failed sending relay request to relayer")
return err
}

// Read the response body bytes.
relayResponseBz, err := io.ReadAll(relayHTTPResponse.Body)
if err != nil {
log.Println("ERROR: Failed reading relay response body")
return err
}

// Unmarshal the response bytes into a RelayResponse.
relayResponse := &types.RelayResponse{}
log.Printf("relayHTTPResponse: %s", string(relayResponseBz))
if err := relayResponse.Unmarshal(relayResponseBz); err != nil {
log.Println("ERROR: Failed unmarshaling relay response")
return err
}

Expand All @@ -114,17 +125,20 @@ func (app *appGateServer) handleJSONRPCRelay(
// failed responses.
log.Println("DEBUG: Verifying signed relay response from...")
if err := app.verifyResponse(ctx, supplierAddress, relayResponse); err != nil {
log.Println("ERROR: Failed verifying relay response signature")
return err
}

// Marshal the response payload to bytes to be sent back to the application.
var responsePayloadBz []byte
if _, err = relayResponse.Payload.MarshalTo(responsePayloadBz); err != nil {
log.Println("ERROR: Failed marshaling relay response payload")
return err
}

// Reply with the RelayResponse payload.
if _, err := writer.Write(relayRequestBz); err != nil {
log.Println("ERROR: Failed writing relay response payload to writer")
return err
}

Expand Down

0 comments on commit bb87722

Please sign in to comment.