Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: PRT - fixing tendermint node error parser #1761

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (jm JsonrpcMessage) CheckResponseError(data []byte, httpStatusCode int) (ha
utils.LavaFormatWarning("Failed unmarshalling CheckError", err, utils.LogAttr("data", string(data)))
return false, ""
}
if result.Error == nil {
if result.Error == nil { // no error
return false, ""
}
return result.Error.Message != "", result.Error.Message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,37 @@ func (cp TendermintrpcMessage) GetParams() interface{} {
return cp.Params
}

type TendermintMessageResponseBody struct {
Code int `json:"code,omitempty"`
Log string `json:"log,omitempty"`
}

type TendermintMessageResponse struct {
Response TendermintMessageResponseBody `json:"response,omitempty"`
}

// returns if error exists and
func (jm TendermintrpcMessage) CheckResponseError(data []byte, httpStatusCode int) (hasError bool, errorMessage string) {
result := &JsonrpcMessage{}
err := json.Unmarshal(data, result)
if err != nil {
utils.LavaFormatWarning("Failed unmarshalling CheckError", err, utils.LogAttr("data", string(data)))
return false, ""
}

if result.Error == nil { // no error
if result.Result != nil { // check if we got a tendermint error
tendermintResponse := &TendermintMessageResponse{}
err := json.Unmarshal(result.Result, tendermintResponse)
if err == nil {
return (tendermintResponse.Response.Code != 0 && tendermintResponse.Response.Log != ""), tendermintResponse.Response.Log
}
}
return false, ""
}
return result.Error.Message != "", result.Error.Message
}

func (tm TendermintrpcMessage) GetResult() json.RawMessage {
if tm.Error != nil {
utils.LavaFormatWarning("GetResult() Request got an error from the node", nil, utils.Attribute{Key: "error", Value: tm.Error})
Expand Down
3 changes: 3 additions & 0 deletions protocol/rpcconsumer/rpcconsumer_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,9 @@ func (rpccs *RPCConsumerServer) sendRelayToProvider(
errResponse = rpccs.consumerSessionManager.OnSessionDone(singleConsumerSession, latestBlock, chainlib.GetComputeUnits(protocolMessage), relayLatency, singleConsumerSession.CalculateExpectedLatency(expectedRelayTimeoutForQOS), expectedBH, numOfProviders, pairingAddressesLen, protocolMessage.GetApi().Category.HangingApi) // session done successfully
isNodeError, _ := protocolMessage.CheckResponseError(localRelayResult.Reply.Data, localRelayResult.StatusCode)
localRelayResult.IsNodeError = isNodeError
if rpccs.debugRelays {
utils.LavaFormatDebug("Result Code", utils.LogAttr("isNodeError", isNodeError), utils.LogAttr("StatusCode", localRelayResult.StatusCode))
}
if rpccs.cache.CacheActive() && rpcclient.ValidateStatusCodes(localRelayResult.StatusCode, true) == nil {
// in case the error is a node error we don't want to cache
if !isNodeError {
Expand Down
18 changes: 18 additions & 0 deletions scripts/automation_scripts/pure_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import aiohttp
from aiohttp import web
from functools import partial
import json

port_url_map = {
5555: "http://localhost:1317", # Replace with actual target URLs
Expand All @@ -21,13 +22,30 @@ async def proxy_handler(request, server_port):
url += f"?{query_string}"

print(f"Proxying request to: {url}") # Debug print
print(f"Request path: {path}") # Debug print
print(f"Request query: {query_string}") # Debug print
print(f"Request headers: {request.headers}") # Debug print

try:
async with aiohttp.ClientSession() as session:
method = request.method
headers = {k: v for k, v in request.headers.items() if k.lower() not in ('host', 'content-length')}
data = await request.read()

# Print the data, decoding it as JSON if possible
try:
json_data = data.decode('utf-8')
print(f"Request data (raw): {json_data}") # Debug print
print("")
print("Copy for curl:")
print(f'curl -X POST -H "Content-Type: application/json" {target_url} --data {json_data} -v')
print("")
print("")
json_parsed_data = json.loads(json_data)
print(f"Request data (parsed JSON): {json.dumps(json_parsed_data, indent=2)}") # Debug print
except Exception as e:
print(f"Error decoding request data as JSON: {e}")
print(f"Request data (raw bytes): {data}") # Debug print

async with session.request(method, url, headers=headers, data=data, allow_redirects=False) as resp:
print(f"Response status: {resp.status}") # Debug print
Expand Down
Loading