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

[script store] handle exception when getting script hash #384

Merged
merged 1 commit into from
Oct 17, 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 @@ -177,22 +177,45 @@ private void handleScriptTransaction(EventMetadata metadata, Transaction transac

//Create TxScript entities to save
List<Script> plutusScripts = scriptsMap.values().stream()
.map(plutusScript -> Script.builder()
.scriptHash(getPlutusScriptHash(plutusScript))
.scriptType(ScriptUtil.toPlutusScriptType(plutusScript.getType()))
.content(JsonUtil.getJson(plutusScript))
.build()).collect(Collectors.toList());
.map(plutusScript -> {
String scriptHash;
try {
scriptHash = ScriptUtil.getPlutusScriptHash(plutusScript);
} catch (Exception e) {
log.error("Error getting native script hash. Block hash: " + metadata.getBlockHash(), e);
return null;
}
return Script.builder()
.scriptHash(scriptHash)
.scriptType(ScriptUtil.toPlutusScriptType(plutusScript.getType()))
.content(JsonUtil.getJson(plutusScript))
.build();
}
)
.filter(Objects::nonNull)
.collect(Collectors.toList());
if (plutusScripts != null && plutusScripts.size() > 0)
scriptStorage.saveScripts(plutusScripts);

//Get all native scripts and save
if (transaction.getWitnesses().getNativeScripts() != null) {
List<Script> nativeScripts = transaction.getWitnesses().getNativeScripts().stream()
.map(nativeScript -> Script.builder()
.scriptHash(ScriptUtil.getNativeScriptHash(nativeScript))
.scriptType(ScriptType.NATIVE_SCRIPT)
.content(JsonUtil.getJson(nativeScript))
.build()).collect(Collectors.toList());
.map(nativeScript -> {
String scriptHash;
try {
scriptHash = ScriptUtil.getNativeScriptHash(nativeScript);
} catch (Exception e) {
log.error("Error getting native script hash. Block hash: " + metadata.getBlockHash(), e);
return null;
}
return Script.builder()
.scriptHash(scriptHash)
.scriptType(ScriptType.NATIVE_SCRIPT)
.content(JsonUtil.getJson(nativeScript))
.build();
})
.filter(Objects::nonNull)
.collect(Collectors.toList());
if (nativeScripts != null && nativeScripts.size() > 0)
scriptStorage.saveScripts(nativeScripts);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.bloxbean.cardano.yaci.store.events.TransactionEvent;
import com.bloxbean.cardano.yaci.store.script.domain.Script;
import com.bloxbean.cardano.yaci.store.script.domain.ScriptType;
import com.bloxbean.cardano.yaci.store.script.helper.ScriptUtil;
import com.bloxbean.cardano.yaci.store.script.storage.ScriptStorage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -38,44 +39,51 @@ public void processScriptRefInUtxo(TransactionEvent transactionEvent) {
.map(transactionOutput -> transactionOutput.getScriptRef())
.filter(Objects::nonNull)
.map(scriptRef -> {
com.bloxbean.cardano.client.spec.Script script = null;
com.bloxbean.cardano.client.spec.Script script;
try {
script = ScriptReferenceUtil.deserializeScriptRef(HexUtil.decodeHexString(scriptRef));
} catch (Exception e) {
log.error("Script deserialization failed. Block hash: " + transactionEvent.getMetadata().getBlockHash(), e);
return null;
}
return script;
})
.filter(Objects::nonNull)
.map(script -> {
ScriptType scriptType = toScriptType(script);
String scriptHash = null;

try {
scriptHash = HexUtil.encodeHexString(script.getScriptHash());
} catch (Exception e) {
log.error("Unable to get reference script hash, Block hash: " + transactionEvent.getMetadata().getBlockHash(), e);
}
ScriptType scriptType = toScriptType(script);
String scriptHash;
String content;

String content = null;
if (scriptType == ScriptType.NATIVE_SCRIPT) {
NativeScript nativeScript = NativeScript.builder()
.type(script.getScriptType())
.content(JsonUtil.getJson(script))
.build();

content = JsonUtil.getJson(nativeScript);
try {
scriptHash = ScriptUtil.getNativeScriptHash(nativeScript);
} catch (Exception e) {
log.error("Error getting native script hash, Block hash: " + transactionEvent.getMetadata().getBlockHash(), e);
return null;
}
} else {
PlutusScript plutusScript = toPlutusScript(script);

content = JsonUtil.getJson(plutusScript);

try {
scriptHash = ScriptUtil.getPlutusScriptHash(plutusScript);
} catch (Exception e) {
log.error("Error getting native script hash, Block hash: " + transactionEvent.getMetadata().getBlockHash(), e);
return null;
}
}

return Script.builder()
.scriptHash(scriptHash)
.scriptType(scriptType)
.content(content)
.build();

})
.filter(Objects::nonNull)
.collect(Collectors.toList());

//Save the scripts
Expand Down