|
| 1 | +package solana |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "net/http" |
| 7 | + |
| 8 | + "github.com/pokt-network/poktroll/pkg/polylog" |
| 9 | + sharedtypes "github.com/pokt-network/poktroll/x/shared/types" |
| 10 | + |
| 11 | + "github.com/buildwithgrove/path/gateway" |
| 12 | + pathhttp "github.com/buildwithgrove/path/network/http" |
| 13 | + qosobservations "github.com/buildwithgrove/path/observation/qos" |
| 14 | + "github.com/buildwithgrove/path/protocol" |
| 15 | + "github.com/buildwithgrove/path/qos" |
| 16 | + "github.com/buildwithgrove/path/qos/jsonrpc" |
| 17 | +) |
| 18 | + |
| 19 | +// batchJSONRPCRequestContext provides the support required by the gateway |
| 20 | +// package for handling service requests. |
| 21 | +var _ gateway.RequestQoSContext = &batchJSONRPCRequestContext{} |
| 22 | + |
| 23 | +type endpointJSONRPCResponse struct { |
| 24 | + protocol.EndpointAddr |
| 25 | + jsonrpc.Response |
| 26 | +} |
| 27 | + |
| 28 | +// batchJSONRPCRequestContext provides the functionality required |
| 29 | +// to support QoS for a Solana blockchain service. |
| 30 | +type batchJSONRPCRequestContext struct { |
| 31 | + logger polylog.Logger |
| 32 | + |
| 33 | + // chainID is the chain identifier for the Solana QoS implementation. |
| 34 | + chainID string |
| 35 | + |
| 36 | + // service_id is the identifier for the Solana QoS implementation. |
| 37 | + // It is the "alias" or human readable interpretation of the chain_id. |
| 38 | + // Used in generating observations. |
| 39 | + serviceID protocol.ServiceID |
| 40 | + |
| 41 | + // The length of the request payload in bytes. |
| 42 | + requestPayloadLength uint |
| 43 | + |
| 44 | + endpointStore *EndpointStore |
| 45 | + |
| 46 | + JSONRPCBatchRequest jsonrpc.BatchRequest |
| 47 | + |
| 48 | + // The origin of the request handled by the context. |
| 49 | + // Either: |
| 50 | + // - User: user requests |
| 51 | + // - QoS: requests built by the QoS service to get additional data points on endpoints. |
| 52 | + requestOrigin qosobservations.RequestOrigin |
| 53 | + |
| 54 | + // endpointResponses is the set of responses received from one or |
| 55 | + // more endpoints as part of handling this service request. |
| 56 | + endpointJSONRPCResponses []endpointJSONRPCResponse |
| 57 | +} |
| 58 | + |
| 59 | +// TODO_NEXT(@commoddity): handle batch requests for Solana |
| 60 | +// TODO_MVP(@adshmh): Ensure the JSONRPC request struct |
| 61 | +// can handle all valid service requests. |
| 62 | +func (brc batchJSONRPCRequestContext) GetServicePayloads() []protocol.Payload { |
| 63 | + protocolPayloads := make([]protocol.Payload, len(brc.JSONRPCBatchRequest.Requests)) |
| 64 | + |
| 65 | + for i, jsonrpcRequestPayload := range brc.JSONRPCBatchRequest.GetRequestsPayloads() { |
| 66 | + // TODO_TECHDEBT(@adshmh): Set method-specific timeouts on protocol payload entry. |
| 67 | + protocolPayloads[i] = protocol.Payload{ |
| 68 | + Data: string(jsonrpcRequestPayload), |
| 69 | + Method: http.MethodPost, // Method is alway POST for Solana. |
| 70 | + Path: "", // Path field is not used for Solana. |
| 71 | + RPCType: sharedtypes.RPCType_JSON_RPC, |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return protocolPayloads |
| 76 | +} |
| 77 | + |
| 78 | +// TODO_TECHDEBT(@adshmh): Refactor once the QoS context interface is updated to receive an array of responses. |
| 79 | +// UpdateWithResponse is NOT safe for concurrent use |
| 80 | +func (brc *batchJSONRPCRequestContext) UpdateWithResponse(endpointAddr protocol.EndpointAddr, responseBz []byte) { |
| 81 | + // TODO_TECHDEBT(@adshmh): Refactor this once the QoS context interface is updated to accept all endpoint responses at once. |
| 82 | + // This would make it possible to map each JSONRPC request of a batch to its corresponding endpoint response. |
| 83 | + // This is required to enable request method-specific esponse validation: e.g. format of result field in response to a `getHealth` request. |
| 84 | + // |
| 85 | + // Parse and track the endpoint payload as a JSONRPC response. |
| 86 | + var jsonrpcResponse jsonrpc.Response |
| 87 | + if err := json.Unmarshal(responseBz, &jsonrpcResponse); err != nil { |
| 88 | + // TODO_UPNEXT(@adshmh): Include a preview of malformed payload in the response. |
| 89 | + // |
| 90 | + // Parsing failed, store a generic error JSONRPC response |
| 91 | + jsonrpcResponse = jsonrpc.GetErrorResponse(jsonrpc.ID{}, errCodeUnmarshaling, errMsgUnmarshaling, nil) |
| 92 | + } |
| 93 | + |
| 94 | + // Store the response: will be processed later by the JSONRPC batch request struct. |
| 95 | + brc.endpointJSONRPCResponses = append(brc.endpointJSONRPCResponses, endpointJSONRPCResponse{ |
| 96 | + EndpointAddr: endpointAddr, |
| 97 | + Response: jsonrpcResponse, |
| 98 | + }) |
| 99 | +} |
| 100 | + |
| 101 | +// TODO_MVP(@adshmh): add `Content-Type: application/json` header. |
| 102 | +// GetHTTPResponse builds the HTTP response that should be returned for |
| 103 | +// a Solana blockchain service request. |
| 104 | +func (brc batchJSONRPCRequestContext) GetHTTPResponse() pathhttp.HTTPResponse { |
| 105 | + // TODO_UPNEXT(@adshmh): Return an error response matching the batch of JSONRPC requests. |
| 106 | + // |
| 107 | + // No responses received: this is an internal error: |
| 108 | + // e.g. protocol-level errors like endpoint timing out. |
| 109 | + if len(brc.endpointJSONRPCResponses) == 0 { |
| 110 | + // Build the JSONRPC response indicating a protocol-level error. |
| 111 | + jsonrpcErrorResponse := jsonrpc.NewErrResponseInternalErr(jsonrpc.ID{}, errors.New("protocol-level error: no endpoint responses received")) |
| 112 | + return qos.BuildHTTPResponseFromJSONRPCResponse(brc.logger, jsonrpcErrorResponse) |
| 113 | + } |
| 114 | + |
| 115 | + // assemble the array of JSONRPC responses |
| 116 | + jsonrpcResponses := make([]jsonrpc.Response, len(brc.endpointJSONRPCResponses)) |
| 117 | + for i, jsonrpcResponse := range brc.endpointJSONRPCResponses { |
| 118 | + jsonrpcResponses[i] = jsonrpcResponse.Response |
| 119 | + } |
| 120 | + |
| 121 | + // Use the Batch JSONRPC request to assemble the JSONRPC batch response. |
| 122 | + batchResponseBz := brc.JSONRPCBatchRequest.BuildResponseBytes(jsonrpcResponses) |
| 123 | + |
| 124 | + // TODO_UPNEXT(@adshmh): Adjust HTTP status code according to responses in the batch. |
| 125 | + return jsonrpc.HTTPResponse{ |
| 126 | + ResponsePayload: batchResponseBz, |
| 127 | + // According to the JSON-RPC 2.0 specification, even if individual responses |
| 128 | + // in a batch contain errors, the entire batch should still return HTTP 200 OK. |
| 129 | + HTTPStatusCode: http.StatusOK, |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +// GetObservations returns all the observations contained in the request context. |
| 134 | +// Implements the gateway.RequestQoSContext interface. |
| 135 | +func (rc batchJSONRPCRequestContext) GetObservations() qosobservations.Observations { |
| 136 | + // Set the observation fields common for all requests: successful or failed. |
| 137 | + observations := &qosobservations.SolanaRequestObservations{ |
| 138 | + ChainId: rc.chainID, |
| 139 | + ServiceId: string(rc.serviceID), |
| 140 | + RequestPayloadLength: uint32(rc.requestPayloadLength), |
| 141 | + RequestOrigin: rc.requestOrigin, |
| 142 | + // TODO_UPNEXT(@adshmh): Add a Batch JSONRPC request observation. |
| 143 | + } |
| 144 | + |
| 145 | + // No endpoint responses received. |
| 146 | + // Set request error. |
| 147 | + if len(rc.endpointJSONRPCResponses) == 0 { |
| 148 | + observations.RequestError = qos.GetRequestErrorForProtocolError() |
| 149 | + |
| 150 | + return qosobservations.Observations{ |
| 151 | + ServiceObservations: &qosobservations.Observations_Solana{ |
| 152 | + Solana: observations, |
| 153 | + }, |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + // TODO_UPNEXT(@adshmh): Report batch JSONRPC requests endpoint observations via metrics. |
| 158 | + // |
| 159 | + return qosobservations.Observations{ |
| 160 | + ServiceObservations: &qosobservations.Observations_Solana{ |
| 161 | + Solana: observations, |
| 162 | + }, |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +// GetEndpointSelector is required to satisfy the gateway package's RequestQoSContext interface. |
| 167 | +// The request context is queried for the correct endpoint selector. |
| 168 | +// This allows different endpoint selectors based on the request's context. |
| 169 | +// e.g. the request context for a particular request method can potentially rank endpoints based on their latency when responding to requests with matching method. |
| 170 | +func (rc *batchJSONRPCRequestContext) GetEndpointSelector() protocol.EndpointSelector { |
| 171 | + return rc |
| 172 | +} |
| 173 | + |
| 174 | +// TODO_TECHDEBT(@adshmh): Enhance endpoint selection to consider endpoint quality specific to batch requests. |
| 175 | +// |
| 176 | +// Select chooses an endpoint from the list of supplied endpoints. |
| 177 | +// It uses the perceived state of the Solana chain using other endpoints' responses. |
| 178 | +// It is required to satisfy the protocol package's EndpointSelector interface. |
| 179 | +func (rc *batchJSONRPCRequestContext) Select(allEndpoints protocol.EndpointAddrList) (protocol.EndpointAddr, error) { |
| 180 | + return rc.endpointStore.Select(allEndpoints) |
| 181 | +} |
| 182 | + |
| 183 | +// SelectMultiple chooses multiple endpoints from the list of supplied endpoints. |
| 184 | +// It uses the perceived state of the Solana chain using other endpoints' responses. |
| 185 | +// It is required to satisfy the protocol package's EndpointSelector interface. |
| 186 | +func (rc *batchJSONRPCRequestContext) SelectMultiple(allEndpoints protocol.EndpointAddrList, numEndpoints uint) (protocol.EndpointAddrList, error) { |
| 187 | + return rc.endpointStore.SelectMultiple(allEndpoints, numEndpoints) |
| 188 | +} |
0 commit comments