Skip to content

fix: Add missing priceMem, priceStep in protocol param. Proper error message json for Tx submission and evaluation endpoint #548

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

Merged
merged 1 commit into from
May 5, 2025
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
@@ -0,0 +1,8 @@
package com.bloxbean.cardano.yaci.store.submit.controller;

import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;

@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public record TxErrorResponse(int statusCode, String error, String message) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.core.env.Environment;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestTemplate;

@RestController
Expand All @@ -28,7 +27,7 @@ public TxSubmitController(Environment env) {
}

@PostMapping(value = "submit", consumes = {MediaType.APPLICATION_CBOR_VALUE})
public ResponseEntity<String> submitTx(@RequestBody byte[] txBytes) {
public ResponseEntity<?> submitTx(@RequestBody byte[] txBytes) {
if (log.isDebugEnabled())
log.debug("Submitting tx to : " + submitApiUrl);

Expand All @@ -37,14 +36,14 @@ public ResponseEntity<String> submitTx(@RequestBody byte[] txBytes) {
}

@PostMapping(value = "submit", consumes = {MediaType.TEXT_PLAIN_VALUE})
public ResponseEntity<String> submitTx(@RequestBody String txBytesHex) {
public ResponseEntity<?> submitTx(@RequestBody String txBytesHex) {
if (log.isDebugEnabled())
log.debug("Submitting tx to : " + submitApiUrl);
byte[] txBytes = HexUtil.decodeHexString(txBytesHex);
return invokeSubmitApiUrl(txBytes);
}

ResponseEntity<String> invokeSubmitApiUrl(byte[] cborTx) {
ResponseEntity<?> invokeSubmitApiUrl(byte[] cborTx) {
HttpHeaders headers = new HttpHeaders();
headers.set("Content-Type", "application/cbor");

Expand All @@ -56,10 +55,30 @@ ResponseEntity<String> invokeSubmitApiUrl(byte[] cborTx) {
.exchange(submitApiUrl, HttpMethod.POST, entity, String.class);

return responseEntity;
} catch (HttpClientErrorException | HttpServerErrorException e) {
int statusCode = e.getStatusCode().value();
String error = e.getStatusCode().toString();
String message = e.getResponseBodyAsString();

if (log.isDebugEnabled())
log.debug("Error submitting tx: Status = {} , Error = {} , Message = {}", statusCode, error, message);

TxErrorResponse errorResponse = new TxErrorResponse(statusCode, error, message);
return ResponseEntity.status(statusCode).body(errorResponse);
} catch (Exception e) {
log.error("Error submit tx", e);
return ResponseEntity.badRequest()
.body(e.getMessage());
if (log.isDebugEnabled())
log.error("Unexpected error submitting tx", e);
TxErrorResponse errorResponse = new TxErrorResponse(500, "Internal Server Error", e.getMessage());
return ResponseEntity.status(500).body(errorResponse);
}
}

@ExceptionHandler(Exception.class)
public ResponseEntity<TxErrorResponse> handleException(Exception e) {
if (log.isDebugEnabled())
log.error("Unhandled exception", e);

TxErrorResponse errorResponse = new TxErrorResponse(500, "Internal Server Error", e.getMessage());
return ResponseEntity.status(500).body(errorResponse);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class TxUtilController {

@PostMapping(value = "evaluate", consumes = {MediaType.APPLICATION_CBOR_VALUE})
@Operation(description = "Evaluate a CBOR encoded transaction. Returns the evaluation result")
public ResponseEntity<String> evaluateTx(@RequestBody String cborTx,
public ResponseEntity<?> evaluateTx(@RequestBody String cborTx,
@RequestParam(value = "version",defaultValue = "5")
@Parameter(description = "Optional parameter to specify the version of the Ogmios service to use. Default is 5. Set to 6 to use Ogmios version 6.")
int version) {
Expand All @@ -46,14 +46,14 @@ public ResponseEntity<String> evaluateTx(@RequestBody String cborTx,
@Operation(description = "Evaluate a CBOR encoded transaction. Returns the evaluation result. " +
"Though additional utxos can be provided, it is not currently used in the implementation. " +
"It is there for compatibility with the Blockfrost API")
public ResponseEntity<String> evaluateTx(@RequestBody EvaluateRequest evaluateRequest,
public ResponseEntity<?> evaluateTx(@RequestBody EvaluateRequest evaluateRequest,
@RequestParam(value = "version",defaultValue = "5")
@Parameter(description = "Optional parameter to specify the version of the Ogmios service to use. Default is 5. Set to 6 to use Ogmios version 6.")
int version) {
return doEvaluateTx(evaluateRequest, version);
}

private ResponseEntity<String> doEvaluateTx(EvaluateRequest evaluateRequest, int version) {
private ResponseEntity<?> doEvaluateTx(EvaluateRequest evaluateRequest, int version) {
try {
if (log.isDebugEnabled())
log.debug("Evaluating tx : " + evaluateRequest);
Expand Down Expand Up @@ -84,9 +84,8 @@ private ResponseEntity<String> doEvaluateTx(EvaluateRequest evaluateRequest, int
failureRes = response.getLeft();
}

return ResponseEntity.badRequest()
.contentType(MediaType.APPLICATION_JSON)
.body(JsonUtil.getPrettyJson(failureRes));
TxErrorResponse errorResponse = new TxErrorResponse(400, "Bad Request", JsonUtil.getPrettyJson(failureRes));
return ResponseEntity.badRequest().body(errorResponse);
} else {
return ResponseEntity.badRequest()
.body("Error evaluating tx");
Expand All @@ -96,11 +95,19 @@ private ResponseEntity<String> doEvaluateTx(EvaluateRequest evaluateRequest, int
if (log.isDebugEnabled()) {
log.error("Error evaluating tx: ", e);
}
return ResponseEntity.badRequest()
.body(e.getMessage());
TxErrorResponse errorResponse = new TxErrorResponse(500, "Internal Server Error", e.getMessage());
return ResponseEntity.status(500).body(errorResponse);
}
}

@ExceptionHandler(Exception.class)
public ResponseEntity<TxErrorResponse> handleException(Exception e) {
if (log.isDebugEnabled())
log.error("Unhandled exception", e);
TxErrorResponse errorResponse = new TxErrorResponse(500, "Internal Server Error", e.getMessage());
return ResponseEntity.status(500).body(errorResponse);
}

@Data
@NoArgsConstructor
public static class EvaluateRequest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public ProtocolParamsDto toProtocolParamsDto(ProtocolParams protocolParams) {
List<String> ops = switch (key) {
case PlutusKeys.PLUTUS_V1 -> PlutusOps.getOperations(1);
case PlutusKeys.PLUTUS_V2 -> PlutusOps.getOperations(2);
case PlutusKeys.PLUTUS_V3 -> Collections.emptyList(); //TODO
case PlutusKeys.PLUTUS_V3 -> PlutusOps.getOperations(3);
default -> Collections.emptyList();
};

Expand Down Expand Up @@ -123,6 +123,8 @@ public ProtocolParamsDto toProtocolParamsDto(ProtocolParams protocolParams) {
protocolParamsDto.setRho(safeRatio(protocolParams.getExpansionRate()));
protocolParamsDto.setTau(safeRatio(protocolParams.getTreasuryGrowthRate()));
protocolParamsDto.setDecentralisationParam(safeRatio(protocolParams.getDecentralisationParam()));
protocolParamsDto.setPriceMem(safeRatio(protocolParams.getPriceMem()));
protocolParamsDto.setPriceStep(safeRatio(protocolParams.getPriceStep()));

//pvt
protocolParamsDto.setPvtMotionNoConfidence(safeRatio(protocolParams.getPoolVotingThresholds().getPvtMotionNoConfidence()));
Expand Down
Loading