-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: #402 Add Tx Evaluator implementation to return constant cost
- Loading branch information
Showing
1 changed file
with
80 additions
and
0 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
core-api/src/main/java/com/bloxbean/cardano/client/api/impl/StaticTransactionEvaluator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.bloxbean.cardano.client.api.impl; | ||
|
||
import com.bloxbean.cardano.client.api.TransactionEvaluator; | ||
import com.bloxbean.cardano.client.api.exception.ApiException; | ||
import com.bloxbean.cardano.client.api.model.EvaluationResult; | ||
import com.bloxbean.cardano.client.api.model.Result; | ||
import com.bloxbean.cardano.client.api.model.Utxo; | ||
import com.bloxbean.cardano.client.exception.CborDeserializationException; | ||
import com.bloxbean.cardano.client.plutus.spec.ExUnits; | ||
import com.bloxbean.cardano.client.transaction.spec.Transaction; | ||
import com.bloxbean.cardano.client.util.JsonUtil; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
/** | ||
* A static implementation of {@link TransactionEvaluator} which returns a static list of {@link EvaluationResult} | ||
*/ | ||
public class StaticTransactionEvaluator implements TransactionEvaluator { | ||
private List<ExUnits> exUnits; | ||
|
||
/** | ||
* Constructor | ||
* @param exUnits List of {@link ExUnits} to be returned as evaluation result. The number of ExUnits should match the | ||
* number of redeemers in the transaction in the order they appear in the transaction | ||
*/ | ||
public StaticTransactionEvaluator(List<ExUnits> exUnits) { | ||
this.exUnits = exUnits; | ||
} | ||
|
||
@Override | ||
public Result<List<EvaluationResult>> evaluateTx(byte[] cbor, Set<Utxo> inputUtxos) throws ApiException { | ||
var evaluationResults = getEvaluationResults(cbor); | ||
return Result.success(JsonUtil.getPrettyJson(evaluationResults)).withValue(evaluationResults); | ||
} | ||
|
||
@Override | ||
public Result<List<EvaluationResult>> evaluateTx(byte[] cbor) throws ApiException { | ||
var evaluationResults = getEvaluationResults(cbor); | ||
return Result.success(JsonUtil.getPrettyJson(evaluationResults)).withValue(evaluationResults); | ||
} | ||
|
||
@Override | ||
public Result<List<EvaluationResult>> evaluateTx(Transaction transaction, Set<Utxo> inputUtxos) throws ApiException { | ||
var evaluationResults = getEvaluationResults(transaction); | ||
return Result.success(JsonUtil.getPrettyJson(evaluationResults)).withValue(evaluationResults); | ||
} | ||
|
||
private List<EvaluationResult> getEvaluationResults(byte[] txCbor) { | ||
Transaction transaction = null; | ||
try { | ||
transaction = Transaction.deserialize(txCbor); | ||
} catch (CborDeserializationException e) { | ||
throw new IllegalArgumentException("Invalid transaction cbor. Cost cannot be returned"); | ||
} | ||
return getEvaluationResults(transaction); | ||
} | ||
|
||
private List<EvaluationResult> getEvaluationResults(Transaction transaction) { | ||
if (transaction.getWitnessSet().getRedeemers() == null) | ||
throw new IllegalArgumentException("Transaction doesn't have redeemers. Cost cannot be returned"); | ||
|
||
if (transaction.getWitnessSet().getRedeemers().size() != exUnits.size()) | ||
throw new IllegalArgumentException("Number of redeemers in the transaction doesn't match the number of exUnits provided"); | ||
|
||
List<EvaluationResult> evaluationResults = new ArrayList<>(); | ||
for (int i = 0; i < transaction.getWitnessSet().getRedeemers().size(); i++) { | ||
var redeemer = transaction.getWitnessSet().getRedeemers().get(i); | ||
var evaluationResult = new EvaluationResult(); | ||
evaluationResult.setExUnits(exUnits.get(i)); | ||
evaluationResult.setIndex(redeemer.getIndex().intValue()); | ||
evaluationResult.setRedeemerTag(redeemer.getTag()); | ||
|
||
evaluationResults.add(evaluationResult); | ||
} | ||
|
||
return evaluationResults; | ||
} | ||
} |