From e7e72bb39240ef2807b9315bd7767370c114ac3b Mon Sep 17 00:00:00 2001 From: Kammerlo Date: Mon, 27 May 2024 11:01:10 +0200 Subject: [PATCH 1/2] feat: added transaction size and scriptSize to TxnEntity. It will be synced from cbor. --- .../transaction/domain/TransactionSize.java | 13 ++ .../yaci/store/transaction/domain/Txn.java | 2 + .../processor/TransactionProcessor.java | 5 + .../storage/impl/model/TxnEntity.java | 7 + .../transaction/util/TransactionSizeUtil.java | 215 ++++++++++++++++++ .../resources/db/store/h2/V0_300_1__init.sql | 4 +- .../db/store/mysql/V0_300_1__init.sql | 4 +- .../db/store/postgresql/V0_300_1__init.sql | 4 +- .../resources/scripts/transaction_data.sql | 20 +- 9 files changed, 261 insertions(+), 13 deletions(-) create mode 100644 stores/transaction/src/main/java/com/bloxbean/cardano/yaci/store/transaction/domain/TransactionSize.java create mode 100644 stores/transaction/src/main/java/com/bloxbean/cardano/yaci/store/transaction/util/TransactionSizeUtil.java diff --git a/stores/transaction/src/main/java/com/bloxbean/cardano/yaci/store/transaction/domain/TransactionSize.java b/stores/transaction/src/main/java/com/bloxbean/cardano/yaci/store/transaction/domain/TransactionSize.java new file mode 100644 index 000000000..6c7ec7bc8 --- /dev/null +++ b/stores/transaction/src/main/java/com/bloxbean/cardano/yaci/store/transaction/domain/TransactionSize.java @@ -0,0 +1,13 @@ +package com.bloxbean.cardano.yaci.store.transaction.domain; + +import lombok.Builder; +import lombok.Data; + +@Data +@Builder +public class TransactionSize { + + int size; + int scriptSize; + +} diff --git a/stores/transaction/src/main/java/com/bloxbean/cardano/yaci/store/transaction/domain/Txn.java b/stores/transaction/src/main/java/com/bloxbean/cardano/yaci/store/transaction/domain/Txn.java index a4d6806a5..8eb422d0b 100644 --- a/stores/transaction/src/main/java/com/bloxbean/cardano/yaci/store/transaction/domain/Txn.java +++ b/stores/transaction/src/main/java/com/bloxbean/cardano/yaci/store/transaction/domain/Txn.java @@ -40,4 +40,6 @@ public class Txn extends BlockAwareDomain { private BigInteger totalCollateral; private List referenceInputs; private Boolean invalid; + private int size; + private int scriptSize; } diff --git a/stores/transaction/src/main/java/com/bloxbean/cardano/yaci/store/transaction/processor/TransactionProcessor.java b/stores/transaction/src/main/java/com/bloxbean/cardano/yaci/store/transaction/processor/TransactionProcessor.java index 2681c2f84..e6fab7a7f 100644 --- a/stores/transaction/src/main/java/com/bloxbean/cardano/yaci/store/transaction/processor/TransactionProcessor.java +++ b/stores/transaction/src/main/java/com/bloxbean/cardano/yaci/store/transaction/processor/TransactionProcessor.java @@ -13,12 +13,14 @@ import com.bloxbean.cardano.yaci.store.events.EventMetadata; import com.bloxbean.cardano.yaci.store.events.TransactionEvent; import com.bloxbean.cardano.yaci.store.transaction.domain.InvalidTransaction; +import com.bloxbean.cardano.yaci.store.transaction.domain.TransactionSize; import com.bloxbean.cardano.yaci.store.transaction.domain.TxWitnessType; import com.bloxbean.cardano.yaci.store.transaction.domain.Txn; import com.bloxbean.cardano.yaci.store.transaction.domain.TxnWitness; import com.bloxbean.cardano.yaci.store.transaction.storage.InvalidTransactionStorage; import com.bloxbean.cardano.yaci.store.transaction.storage.TransactionStorage; import com.bloxbean.cardano.yaci.store.transaction.storage.TransactionWitnessStorage; +import com.bloxbean.cardano.yaci.store.transaction.util.TransactionSizeUtil; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; import lombok.RequiredArgsConstructor; @@ -78,6 +80,7 @@ public void handleTransactionEvent(TransactionEvent event) { .map(transactionInput -> new UtxoKey(transactionInput.getTransactionId(), transactionInput.getIndex())) .collect(Collectors.toList()); } + TransactionSize transactionSizeAndScriptSize = TransactionSizeUtil.getTransactionSizeAndScriptSize(transaction); Txn txn = Txn.builder() .txHash(transaction.getTxHash()) @@ -99,6 +102,8 @@ public void handleTransactionEvent(TransactionEvent event) { .collateralReturn(new UtxoKey(transaction.getTxHash(), outputs.size())) .referenceInputs(referenceInputs) .invalid(transaction.isInvalid()) + .size(transactionSizeAndScriptSize.getSize()) + .scriptSize(transactionSizeAndScriptSize.getScriptSize()) .build(); txList.add(txn); diff --git a/stores/transaction/src/main/java/com/bloxbean/cardano/yaci/store/transaction/storage/impl/model/TxnEntity.java b/stores/transaction/src/main/java/com/bloxbean/cardano/yaci/store/transaction/storage/impl/model/TxnEntity.java index 326d5f73e..d7d386693 100644 --- a/stores/transaction/src/main/java/com/bloxbean/cardano/yaci/store/transaction/storage/impl/model/TxnEntity.java +++ b/stores/transaction/src/main/java/com/bloxbean/cardano/yaci/store/transaction/storage/impl/model/TxnEntity.java @@ -86,4 +86,11 @@ public class TxnEntity extends BlockAwareEntity { @Column(name = "invalid") private Boolean invalid; + + @Column(name = "size") + private int size; + + @Column(name = "script_size") + private int scriptSize; + } diff --git a/stores/transaction/src/main/java/com/bloxbean/cardano/yaci/store/transaction/util/TransactionSizeUtil.java b/stores/transaction/src/main/java/com/bloxbean/cardano/yaci/store/transaction/util/TransactionSizeUtil.java new file mode 100644 index 000000000..e3c5d3a95 --- /dev/null +++ b/stores/transaction/src/main/java/com/bloxbean/cardano/yaci/store/transaction/util/TransactionSizeUtil.java @@ -0,0 +1,215 @@ +package com.bloxbean.cardano.yaci.store.transaction.util; + +import co.nstant.in.cbor.model.Array; +import co.nstant.in.cbor.model.ByteString; +import co.nstant.in.cbor.model.DataItem; +import co.nstant.in.cbor.model.Map; +import co.nstant.in.cbor.model.SimpleValue; +import co.nstant.in.cbor.model.UnsignedInteger; +import com.bloxbean.cardano.client.exception.CborDeserializationException; +import com.bloxbean.cardano.client.exception.CborSerializationException; +import com.bloxbean.cardano.client.transaction.spec.script.NativeScript; +import com.bloxbean.cardano.client.util.HexUtil; +import com.bloxbean.cardano.yaci.core.model.PlutusScript; +import com.bloxbean.cardano.yaci.core.util.CborSerializationUtil; +import com.bloxbean.cardano.yaci.helper.model.Transaction; +import com.bloxbean.cardano.yaci.store.transaction.domain.TransactionSize; +import com.fasterxml.jackson.core.JsonProcessingException; +import lombok.extern.slf4j.Slf4j; +import org.antlr.v4.runtime.misc.Pair; + +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; + +@Slf4j +public class TransactionSizeUtil { + + /** + * This functions reconstructs the original transaction and calculates the size and scriptSize from these. + * @param tx Transaction to be parsed. + * @return Pair containing TransactionSize as A and scriptSize as B + */ + public static TransactionSize getTransactionSizeAndScriptSize(Transaction tx) { + // Reconstructing Transaction based on CDDL specification - https://github.com/IntersectMBO/cardano-ledger/blob/master/eras/alonzo/impl/cddl-files/alonzo.cddl + Map signedTransaction = new Map(); + if(tx.getBody().getCbor() == null) { + // YACIConfig.INSTANCE.setReturnTxBodyCbor is false no cbor body contained + return TransactionSize.builder() + .size(0) + .scriptSize(0) + .build(); + } + DataItem txBody = CborSerializationUtil.deserialize(HexUtil.decodeHexString(tx.getBody().getCbor()))[0]; + signedTransaction.put(new UnsignedInteger(0), txBody); + int scriptSize = addWitnessSetToSignedTransaction(tx, signedTransaction); + + if(tx.getBlockNumber() > 64902L) { // starting from alonzo era + signedTransaction.put(new UnsignedInteger( + 2), SimpleValue.TRUE); + } + + addAuxDataToTransaction(tx, signedTransaction); + + byte[] serialize = CborSerializationUtil.serialize(signedTransaction); + + int txSize = serialize.length; + return TransactionSize.builder() + .size(txSize) + .scriptSize(scriptSize) + .build(); + } + + /** + * Reconstructing the Witness_set from transaction and adding it to signedTransaction + * CDDL Definition: https://github.com/IntersectMBO/cardano-ledger/blob/e6b6d4f85fb72b5cb5b5361e534d3bb71bb9e55e/eras/alonzo/impl/cddl-files/alonzo.cddl#L269 + * @param tx + * @param signedTransaction + * @return + */ + private static int addWitnessSetToSignedTransaction(Transaction tx, Map signedTransaction) { + // adding witnesses to signedTransaction + Map witnessSet = new Map(); + int scriptSize = 0; + addvKeyWitnessToWitness(tx, witnessSet); + addNativeScriptsToWitness(tx, witnessSet); + addBootstrapToWitness(tx, witnessSet); + + scriptSize += addPlutusToWitness(witnessSet, tx.getWitnesses().getPlutusV1Scripts(), + 3); + scriptSize += addPlutusToWitness(witnessSet, tx.getWitnesses().getPlutusV2Scripts(), 6); + scriptSize += addPlutusToWitness(witnessSet, tx.getWitnesses().getPlutusV3Scripts(), 7); + + addDatumToWitness(tx, witnessSet); + addRedeemerToWitness(tx, witnessSet); + + if(!witnessSet.getKeys().isEmpty()) { + signedTransaction.put(new UnsignedInteger(1), witnessSet); + } + return scriptSize; + } + + /** + * Adding the datum to Witness set. CDDL spec: https://github.com/IntersectMBO/cardano-ledger/blob/e6b6d4f85fb72b5cb5b5361e534d3bb71bb9e55e/eras/alonzo/impl/cddl-files/alonzo.cddl#L280 + * @param tx transaction to extract the datum + * @param witnessSet witnessSet to add the datum + */ + private static void addDatumToWitness(Transaction tx, Map witnessSet) { + if(!tx.getWitnesses().getDatums().isEmpty()) { + Array array = new Array(); + // could speed it up by passing an empty array, since we are only interested in the size not the content + tx.getWitnesses().getDatums().forEach(datum -> array.add(new ByteString(HexUtil.decodeHexString(datum.getCbor())))); + witnessSet.put(new UnsignedInteger(4), array); + } + } + + /** + * Adding Plutus Script data to witnessSet. Can be used for V1, V2 and V3. CDDL spec: https://github.com/IntersectMBO/cardano-ledger/blob/e6b6d4f85fb72b5cb5b5361e534d3bb71bb9e55e/eras/alonzo/impl/cddl-files/alonzo.cddl#L278 + * @param witnessSet witnessset to add the data + * @param scripts List of PlutusScripts + * @param witnessSetIndex Index where to add the datum based on cddl spec + * @return + */ + private static int addPlutusToWitness(Map witnessSet, List scripts, int witnessSetIndex) { + AtomicInteger scriptSize = new AtomicInteger(); + Array array = new Array(); + if(!scripts.isEmpty()) { + scripts.forEach(script -> { + scriptSize.addAndGet(script.getContent().length() / 2); // adding have the string length, sinze it's 4bit hex and we need the byte length + array.add(new ByteString(HexUtil.decodeHexString(script.getContent()))); + }); + witnessSet.put(new UnsignedInteger(witnessSetIndex), array); + } + return scriptSize.get(); + } + + /** + * Adding Redemer data to witnessset. CDDL spec: https://github.com/IntersectMBO/cardano-ledger/blob/e6b6d4f85fb72b5cb5b5361e534d3bb71bb9e55e/eras/alonzo/impl/cddl-files/alonzo.cddl#L302 + * @param tx transaction to extract the redeemer data + * @param witnessSet witnesset to add the data based on cddl spec + */ + private static void addRedeemerToWitness(Transaction tx, Map witnessSet) { + if(!tx.getWitnesses().getRedeemers().isEmpty()) { + Array array = new Array(); + // could speed it up by passing an empty array, since we are only interested in the size not the content + tx.getWitnesses().getRedeemers().forEach(redeemer -> array.add(new ByteString(HexUtil.decodeHexString(redeemer.getCbor())))); + witnessSet.put(new UnsignedInteger(5), array); + } + } + + /** + * Extracting bootstrap data and adding it to witnessSet. CDDL spec: https://github.com/IntersectMBO/cardano-ledger/blob/e6b6d4f85fb72b5cb5b5361e534d3bb71bb9e55e/eras/alonzo/impl/cddl-files/alonzo.cddl#L348 + * @param tx Transcation to get the bootstrap from + * @param witnessSet witnessSet to add the data + */ + private static void addBootstrapToWitness(Transaction tx, Map witnessSet) { + if(!tx.getWitnesses().getBootstrapWitnesses().isEmpty()) { + Array array = new Array(); + tx.getWitnesses().getBootstrapWitnesses().forEach(bootstrapWitness -> { + Array witnessArray = new Array(); + witnessArray.add(new ByteString(HexUtil.decodeHexString(bootstrapWitness.getPublicKey()))); + witnessArray.add(new ByteString(HexUtil.decodeHexString(bootstrapWitness.getSignature()))); + witnessArray.add(new ByteString(HexUtil.decodeHexString(bootstrapWitness.getChainCode()))); + witnessArray.add(new ByteString(HexUtil.decodeHexString(bootstrapWitness.getAttributes()))); + array.add(witnessArray); + }); + witnessSet.put(new UnsignedInteger(2), array); + } + } + + /** + * Extracting the Auxiliary data and adding it to the transaction. CDDL spec: https://github.com/IntersectMBO/cardano-ledger/blob/e6b6d4f85fb72b5cb5b5361e534d3bb71bb9e55e/eras/alonzo/impl/cddl-files/alonzo.cddl#L17 + * @param tx Transcation to get the auxdata from + * @param signedTransaction Map to add the auxdata + */ + private static void addAuxDataToTransaction(Transaction tx, Map signedTransaction) { + if(tx.getAuxData() != null) { + Array auxiliaryData = new Array(); + if(tx.getAuxData().getMetadataCbor() != null) { + auxiliaryData.add(CborSerializationUtil.deserialize( + HexUtil.decodeHexString(tx.getAuxData().getMetadataCbor()))[0]); + } + signedTransaction.put(new UnsignedInteger(3), auxiliaryData); + } + } + + /** + * Extracting VKey and adding it to witnessSet. CDDL spec: https://github.com/IntersectMBO/cardano-ledger/blob/e6b6d4f85fb72b5cb5b5361e534d3bb71bb9e55e/eras/alonzo/impl/cddl-files/alonzo.cddl#L346 + * @param tx transaction to extract the data + * @param witnessSet witnessSet to add the data + */ + private static void addvKeyWitnessToWitness(Transaction tx, Map witnessSet) { + if(!tx.getWitnesses().getVkeyWitnesses().isEmpty()) { + Array vKeyWitnessArray = new Array(); + tx.getWitnesses().getVkeyWitnesses().forEach(vkeyWitness -> { + Array vitnessArray = new Array(); + vitnessArray.add(new ByteString(HexUtil.decodeHexString(vkeyWitness.getKey()))); + vitnessArray.add(new ByteString(HexUtil.decodeHexString(vkeyWitness.getSignature()))); // could speed it up by passing an empty array, since we are only interested in the size not the content + vKeyWitnessArray.add(vitnessArray); + }); + witnessSet.put(new UnsignedInteger(0), vKeyWitnessArray); + } + } + + /** + * Extracting NativeScript data and adding it to witnessSet. CDDL spec: https://github.com/IntersectMBO/cardano-ledger/blob/e6b6d4f85fb72b5cb5b5361e534d3bb71bb9e55e/eras/alonzo/impl/cddl-files/alonzo.cddl#L355 + * @param tx transaction to extract the data + * @param witnessSet witnessSet to add the data + */ + private static void addNativeScriptsToWitness(Transaction tx, Map witnessSet) { + if(!tx.getWitnesses().getNativeScripts().isEmpty()) { + Array nativeScripts = new Array(); + tx.getWitnesses().getNativeScripts().forEach(script -> { + NativeScript nativeScript; + try { + nativeScript = NativeScript.deserializeJson(script.getContent()); + nativeScripts.add(new ByteString(nativeScript.getScriptHash())); + } catch (CborDeserializationException | JsonProcessingException | + CborSerializationException e) { + log.error("Can't parse Native script for Transaction: " + tx.getTxHash()); + } + }); + witnessSet.put(new UnsignedInteger(1), nativeScripts); + } + } + +} diff --git a/stores/transaction/src/main/resources/db/store/h2/V0_300_1__init.sql b/stores/transaction/src/main/resources/db/store/h2/V0_300_1__init.sql index 85185fe20..99fc21a8b 100644 --- a/stores/transaction/src/main/resources/db/store/h2/V0_300_1__init.sql +++ b/stores/transaction/src/main/resources/db/store/h2/V0_300_1__init.sql @@ -22,7 +22,9 @@ create table transaction collateral_return_json json, block bigint, block_time bigint, - update_datetime timestamp + update_datetime timestamp, + size smallint, + script_size smallint ); CREATE INDEX idx_transaction_slot diff --git a/stores/transaction/src/main/resources/db/store/mysql/V0_300_1__init.sql b/stores/transaction/src/main/resources/db/store/mysql/V0_300_1__init.sql index 51cfbad0c..430b85c21 100644 --- a/stores/transaction/src/main/resources/db/store/mysql/V0_300_1__init.sql +++ b/stores/transaction/src/main/resources/db/store/mysql/V0_300_1__init.sql @@ -22,7 +22,9 @@ create table transaction collateral_return_json json, block bigint, block_time bigint, - update_datetime timestamp + update_datetime timestamp, + size smallint, + script_size smallint ); CREATE INDEX idx_transaction_slot diff --git a/stores/transaction/src/main/resources/db/store/postgresql/V0_300_1__init.sql b/stores/transaction/src/main/resources/db/store/postgresql/V0_300_1__init.sql index 8639c51fc..c813398c1 100644 --- a/stores/transaction/src/main/resources/db/store/postgresql/V0_300_1__init.sql +++ b/stores/transaction/src/main/resources/db/store/postgresql/V0_300_1__init.sql @@ -22,7 +22,9 @@ create table transaction collateral_return_json jsonb, block bigint, block_time bigint, - update_datetime timestamp + update_datetime timestamp, + size smallint, + script_size smallint ); CREATE INDEX idx_transaction_slot diff --git a/stores/transaction/src/test/resources/scripts/transaction_data.sql b/stores/transaction/src/test/resources/scripts/transaction_data.sql index 5c23abb6f..6f9a6acf4 100644 --- a/stores/transaction/src/test/resources/scripts/transaction_data.sql +++ b/stores/transaction/src/test/resources/scripts/transaction_data.sql @@ -1,14 +1,14 @@ TRUNCATE table transaction; INSERT INTO transaction - (tx_hash,auxiliary_datahash,block_hash,collateral_inputs,collateral_return,fee,inputs,invalid,network_id,outputs,reference_inputs,required_signers,script_datahash,slot,total_collateral,ttl,validity_interval_start,collateral_return_json,block,block_time,update_datetime) + (tx_hash,auxiliary_datahash,block_hash,collateral_inputs,collateral_return,fee,inputs,invalid,network_id,outputs,reference_inputs,required_signers,script_datahash,slot,total_collateral,ttl,validity_interval_start,collateral_return_json,block,block_time,update_datetime,size,script_size) VALUES - ('e43b09f3d95f83aadc4d7436c5c9067ff04ab876b6ed8acbbed1c0ab49743869','3b97d0d972f465789d4040b63c5ba684c7dac533fcae946b00ec51b266b98e20','64bde349c8dd00ee6195217d36e4449f6c64f3b404fed17ad284cad3f810cce2',NULL,'{"tx_hash": "e43b09f3d95f83aadc4d7436c5c9067ff04ab876b6ed8acbbed1c0ab49743869", "output_index": 1}' FORMAT JSON,206685,'[{"tx_hash": "d6aa8534be82c5dfbf84c1cf08e588a8e85cec3f7e7d4579a226c491a5292992", "output_index": 0}]' FORMAT JSON,false,0,'[{"tx_hash": "e43b09f3d95f83aadc4d7436c5c9067ff04ab876b6ed8acbbed1c0ab49743869", "output_index": 0}]' FORMAT JSON,NULL,NULL,NULL,10811653,NULL,10911609,0,NULL,184626,1666494853, TIMESTAMP '2023-11-07 09:09:39.41541'), - ('dbcec01a23519abe2c308f7ad72ec9b25a0f725bf7cccb76b1ca0defd6f19f7b','4062577dd136fefd5a82a943ec32c655fbfd65e42ed1443e8afca3411b731101','e2a540c327b67302c556456d69d4c0b878ccae7e1e961eee3f238769c326fbaf',NULL,'{"tx_hash": "dbcec01a23519abe2c308f7ad72ec9b25a0f725bf7cccb76b1ca0defd6f19f7b", "output_index": 1}' FORMAT JSON,203869,'[{"tx_hash": "d774e4100749dd417b67ac9246af926f56393c13603f18d60b9186467d8daf3b", "output_index": 0}]' FORMAT JSON,false,0,'[{"tx_hash": "dbcec01a23519abe2c308f7ad72ec9b25a0f725bf7cccb76b1ca0defd6f19f7b", "output_index": 0}]' FORMAT JSON,NULL,NULL,NULL,10811083,NULL,10911073,0,NULL,184590,1666494283, TIMESTAMP '2023-11-07 09:09:39.453555'), - ('2e1f7694a9d4f2314a06f05c224776caa0ec70b329706005334ca96ecab71ad4','59eb5881732a1103742fc7eb6601e83e3b38a2cffadfdf215034eac3e0dbf284','d6741535234445188db39a0cb88e72012d62522747e6ffe632116fe0e65661cc',NULL,'{"tx_hash": "2e1f7694a9d4f2314a06f05c224776caa0ec70b329706005334ca96ecab71ad4", "output_index": 1}' FORMAT JSON,204529,'[{"tx_hash": "1bb71ccc2669cac5d175da409e055be91e9dd05eb5f547367db4e7374d109e93", "output_index": 0}]' FORMAT JSON,false,0,'[{"tx_hash": "2e1f7694a9d4f2314a06f05c224776caa0ec70b329706005334ca96ecab71ad4", "output_index": 0}]' FORMAT JSON,NULL,NULL,NULL,10811227,NULL,10911213,0,NULL,184600,1666494427, TIMESTAMP '2023-11-07 09:09:39.457667'), - ('4cb2df838ec05fb74c59688a2eface5f03e8f5c8e8144ebde4ed953b89d8f09c','f70de3c0b43758461d0fedd040fa3703275d52d981bca36a8e2550ac0428b679','c81fe9256d5f7867c935ea92842e7de6b175ab66c42ab7c1131ec155c9ae9e10',NULL,'{"tx_hash": "4cb2df838ec05fb74c59688a2eface5f03e8f5c8e8144ebde4ed953b89d8f09c", "output_index": 1}' FORMAT JSON,205101,'[{"tx_hash": "2eb6b6ca3c874e8c22ade36b63a541a315a25edaf757bbdc0b2bf9369749631b", "output_index": 0}]' FORMAT JSON,false,0,'[{"tx_hash": "4cb2df838ec05fb74c59688a2eface5f03e8f5c8e8144ebde4ed953b89d8f09c", "output_index": 0}]' FORMAT JSON,NULL,NULL,NULL,10811940,NULL,10911867,0,NULL,184640,1666495140, TIMESTAMP '2023-11-07 09:09:39.494264'), - ('a3112f8b81bff1344f965b26e577ad7d3b7ac6ad5447fffd67291a50d9aea054','7447a0f1afd8347e5f20f4a95e15517e50fc26d877b9d446c9c3ac1779eccd4e','babf327f05bc6e1ec28e111d66cf8683d9fd4ea6c5104a841020627854903f67',NULL,'{"tx_hash": "a3112f8b81bff1344f965b26e577ad7d3b7ac6ad5447fffd67291a50d9aea054", "output_index": 1}' FORMAT JSON,206905,'[{"tx_hash": "6a314fbfaa67c639970a0419b5aa0c3de170dbb63016008041f4bab169ad7fae", "output_index": 0}]' FORMAT JSON,false,0,'[{"tx_hash": "a3112f8b81bff1344f965b26e577ad7d3b7ac6ad5447fffd67291a50d9aea054", "output_index": 0}]' FORMAT JSON,NULL,NULL,NULL,10813467,NULL,10913450,0,NULL,184723,1666496667, TIMESTAMP '2023-11-07 09:09:39.519206'), - ('9179b03cacf88c396015016e68493ea4d72dd481fde8fe8a354c4141f74695fa','ce07d6410c5662d327898ef0fb5175f312027051b8e10bcd5476561ac1b0614d','1137dcc405ed1fe7b9f5735c7f72dca2a9ab863b0a7fd9727e8691fe670f4fe9',NULL,'{"tx_hash": "9179b03cacf88c396015016e68493ea4d72dd481fde8fe8a354c4141f74695fa", "output_index": 1}' FORMAT JSON,204485,'[{"tx_hash": "28613318ea4063c937e39adce2aeeac1b075a3fcaba2af8f28cbacdcc949297c", "output_index": 0}]' FORMAT JSON,false,0,'[{"tx_hash": "9179b03cacf88c396015016e68493ea4d72dd481fde8fe8a354c4141f74695fa", "output_index": 0}]' FORMAT JSON,NULL,NULL,NULL,10813800,NULL,10913797,0,NULL,184735,1666497000, TIMESTAMP '2023-11-07 09:09:39.541265'), - ('6a314fbfaa67c639970a0419b5aa0c3de170dbb63016008041f4bab169ad7fae','54564827ee7e404d9aeeff62220eacd3049b53c0a65ad00add55a7994f62e9be','388797d9a8322b7141e2314f56330fa2efb38c961233b6138ded9c6cc371dcd9',NULL,'{"tx_hash": "6a314fbfaa67c639970a0419b5aa0c3de170dbb63016008041f4bab169ad7fae", "output_index": 1}' FORMAT JSON,202813,'[{"tx_hash": "b55baab05fa253ac3b284bc2ef4d1e68ad91c7e64a9b0a18c2f52170e1a9b644", "output_index": 0}]' FORMAT JSON,false,0,'[{"tx_hash": "6a314fbfaa67c639970a0419b5aa0c3de170dbb63016008041f4bab169ad7fae", "output_index": 0}]' FORMAT JSON,NULL,NULL,NULL,10813414,NULL,10913407,0,NULL,184719,1666496614, TIMESTAMP '2023-11-07 09:09:39.563539'), - ('d66be851787d599c270a2de55994a1e43c405911188a04b543ea604b75abb014','a43224f85574182c88bacb5bed8d3b3d07c9d26a7958be7137089426a7b4f292','564bc005c9d926afcf4f3ddda29e83fc4cf7ae65d8194d1b1532e7c91e008ac3',NULL,'{"tx_hash": "d66be851787d599c270a2de55994a1e43c405911188a04b543ea604b75abb014", "output_index": 1}' FORMAT JSON,203649,'[{"tx_hash": "accd675b07d42ea5b944adfe0a776732c305188bae773485bdf8b1f62ffb1297", "output_index": 0}]' FORMAT JSON,false,0,'[{"tx_hash": "d66be851787d599c270a2de55994a1e43c405911188a04b543ea604b75abb014", "output_index": 0}]' FORMAT JSON,NULL,NULL,NULL,10808755,NULL,10908747,0,NULL,184471,1666491955, TIMESTAMP '2023-11-07 09:09:39.192703'), - ('922890de4c23a973501d9b7d12eaa045c4aab818c1a9cbff138a843b54940ca8','92e14585d67fcca2da81444ef71a90480e66ae00988b9daa7b29a01efd29ff43','2a6663a30f21c276c77bc4f5029da6a626075bc5506e2453f2e228ffa5a2c357',NULL,'{"tx_hash": "922890de4c23a973501d9b7d12eaa045c4aab818c1a9cbff138a843b54940ca8", "output_index": 1}' FORMAT JSON,202857,'[{"tx_hash": "42c04d1f92f1d0b81852962ba85f727b4c993a69a3d61742285328e8744dfc99", "output_index": 0}]' FORMAT JSON,false,0,'[{"tx_hash": "922890de4c23a973501d9b7d12eaa045c4aab818c1a9cbff138a843b54940ca8", "output_index": 0}]' FORMAT JSON,NULL,NULL,NULL,10809340,NULL,10909336,0,NULL,184505,1666492540, TIMESTAMP '2023-11-07 09:09:39.196543'); + ('e43b09f3d95f83aadc4d7436c5c9067ff04ab876b6ed8acbbed1c0ab49743869','3b97d0d972f465789d4040b63c5ba684c7dac533fcae946b00ec51b266b98e20','64bde349c8dd00ee6195217d36e4449f6c64f3b404fed17ad284cad3f810cce2',NULL,'{"tx_hash": "e43b09f3d95f83aadc4d7436c5c9067ff04ab876b6ed8acbbed1c0ab49743869", "output_index": 1}' FORMAT JSON,206685,'[{"tx_hash": "d6aa8534be82c5dfbf84c1cf08e588a8e85cec3f7e7d4579a226c491a5292992", "output_index": 0}]' FORMAT JSON,false,0,'[{"tx_hash": "e43b09f3d95f83aadc4d7436c5c9067ff04ab876b6ed8acbbed1c0ab49743869", "output_index": 0}]' FORMAT JSON,NULL,NULL,NULL,10811653,NULL,10911609,0,NULL,184626,1666494853, TIMESTAMP '2023-11-07 09:09:39.41541',0,0), + ('dbcec01a23519abe2c308f7ad72ec9b25a0f725bf7cccb76b1ca0defd6f19f7b','4062577dd136fefd5a82a943ec32c655fbfd65e42ed1443e8afca3411b731101','e2a540c327b67302c556456d69d4c0b878ccae7e1e961eee3f238769c326fbaf',NULL,'{"tx_hash": "dbcec01a23519abe2c308f7ad72ec9b25a0f725bf7cccb76b1ca0defd6f19f7b", "output_index": 1}' FORMAT JSON,203869,'[{"tx_hash": "d774e4100749dd417b67ac9246af926f56393c13603f18d60b9186467d8daf3b", "output_index": 0}]' FORMAT JSON,false,0,'[{"tx_hash": "dbcec01a23519abe2c308f7ad72ec9b25a0f725bf7cccb76b1ca0defd6f19f7b", "output_index": 0}]' FORMAT JSON,NULL,NULL,NULL,10811083,NULL,10911073,0,NULL,184590,1666494283, TIMESTAMP '2023-11-07 09:09:39.453555',0,0), + ('2e1f7694a9d4f2314a06f05c224776caa0ec70b329706005334ca96ecab71ad4','59eb5881732a1103742fc7eb6601e83e3b38a2cffadfdf215034eac3e0dbf284','d6741535234445188db39a0cb88e72012d62522747e6ffe632116fe0e65661cc',NULL,'{"tx_hash": "2e1f7694a9d4f2314a06f05c224776caa0ec70b329706005334ca96ecab71ad4", "output_index": 1}' FORMAT JSON,204529,'[{"tx_hash": "1bb71ccc2669cac5d175da409e055be91e9dd05eb5f547367db4e7374d109e93", "output_index": 0}]' FORMAT JSON,false,0,'[{"tx_hash": "2e1f7694a9d4f2314a06f05c224776caa0ec70b329706005334ca96ecab71ad4", "output_index": 0}]' FORMAT JSON,NULL,NULL,NULL,10811227,NULL,10911213,0,NULL,184600,1666494427, TIMESTAMP '2023-11-07 09:09:39.457667',0,0), + ('4cb2df838ec05fb74c59688a2eface5f03e8f5c8e8144ebde4ed953b89d8f09c','f70de3c0b43758461d0fedd040fa3703275d52d981bca36a8e2550ac0428b679','c81fe9256d5f7867c935ea92842e7de6b175ab66c42ab7c1131ec155c9ae9e10',NULL,'{"tx_hash": "4cb2df838ec05fb74c59688a2eface5f03e8f5c8e8144ebde4ed953b89d8f09c", "output_index": 1}' FORMAT JSON,205101,'[{"tx_hash": "2eb6b6ca3c874e8c22ade36b63a541a315a25edaf757bbdc0b2bf9369749631b", "output_index": 0}]' FORMAT JSON,false,0,'[{"tx_hash": "4cb2df838ec05fb74c59688a2eface5f03e8f5c8e8144ebde4ed953b89d8f09c", "output_index": 0}]' FORMAT JSON,NULL,NULL,NULL,10811940,NULL,10911867,0,NULL,184640,1666495140, TIMESTAMP '2023-11-07 09:09:39.494264',0,0), + ('a3112f8b81bff1344f965b26e577ad7d3b7ac6ad5447fffd67291a50d9aea054','7447a0f1afd8347e5f20f4a95e15517e50fc26d877b9d446c9c3ac1779eccd4e','babf327f05bc6e1ec28e111d66cf8683d9fd4ea6c5104a841020627854903f67',NULL,'{"tx_hash": "a3112f8b81bff1344f965b26e577ad7d3b7ac6ad5447fffd67291a50d9aea054", "output_index": 1}' FORMAT JSON,206905,'[{"tx_hash": "6a314fbfaa67c639970a0419b5aa0c3de170dbb63016008041f4bab169ad7fae", "output_index": 0}]' FORMAT JSON,false,0,'[{"tx_hash": "a3112f8b81bff1344f965b26e577ad7d3b7ac6ad5447fffd67291a50d9aea054", "output_index": 0}]' FORMAT JSON,NULL,NULL,NULL,10813467,NULL,10913450,0,NULL,184723,1666496667, TIMESTAMP '2023-11-07 09:09:39.519206',0,0), + ('9179b03cacf88c396015016e68493ea4d72dd481fde8fe8a354c4141f74695fa','ce07d6410c5662d327898ef0fb5175f312027051b8e10bcd5476561ac1b0614d','1137dcc405ed1fe7b9f5735c7f72dca2a9ab863b0a7fd9727e8691fe670f4fe9',NULL,'{"tx_hash": "9179b03cacf88c396015016e68493ea4d72dd481fde8fe8a354c4141f74695fa", "output_index": 1}' FORMAT JSON,204485,'[{"tx_hash": "28613318ea4063c937e39adce2aeeac1b075a3fcaba2af8f28cbacdcc949297c", "output_index": 0}]' FORMAT JSON,false,0,'[{"tx_hash": "9179b03cacf88c396015016e68493ea4d72dd481fde8fe8a354c4141f74695fa", "output_index": 0}]' FORMAT JSON,NULL,NULL,NULL,10813800,NULL,10913797,0,NULL,184735,1666497000, TIMESTAMP '2023-11-07 09:09:39.541265',0,0), + ('6a314fbfaa67c639970a0419b5aa0c3de170dbb63016008041f4bab169ad7fae','54564827ee7e404d9aeeff62220eacd3049b53c0a65ad00add55a7994f62e9be','388797d9a8322b7141e2314f56330fa2efb38c961233b6138ded9c6cc371dcd9',NULL,'{"tx_hash": "6a314fbfaa67c639970a0419b5aa0c3de170dbb63016008041f4bab169ad7fae", "output_index": 1}' FORMAT JSON,202813,'[{"tx_hash": "b55baab05fa253ac3b284bc2ef4d1e68ad91c7e64a9b0a18c2f52170e1a9b644", "output_index": 0}]' FORMAT JSON,false,0,'[{"tx_hash": "6a314fbfaa67c639970a0419b5aa0c3de170dbb63016008041f4bab169ad7fae", "output_index": 0}]' FORMAT JSON,NULL,NULL,NULL,10813414,NULL,10913407,0,NULL,184719,1666496614, TIMESTAMP '2023-11-07 09:09:39.563539',0,0), + ('d66be851787d599c270a2de55994a1e43c405911188a04b543ea604b75abb014','a43224f85574182c88bacb5bed8d3b3d07c9d26a7958be7137089426a7b4f292','564bc005c9d926afcf4f3ddda29e83fc4cf7ae65d8194d1b1532e7c91e008ac3',NULL,'{"tx_hash": "d66be851787d599c270a2de55994a1e43c405911188a04b543ea604b75abb014", "output_index": 1}' FORMAT JSON,203649,'[{"tx_hash": "accd675b07d42ea5b944adfe0a776732c305188bae773485bdf8b1f62ffb1297", "output_index": 0}]' FORMAT JSON,false,0,'[{"tx_hash": "d66be851787d599c270a2de55994a1e43c405911188a04b543ea604b75abb014", "output_index": 0}]' FORMAT JSON,NULL,NULL,NULL,10808755,NULL,10908747,0,NULL,184471,1666491955, TIMESTAMP '2023-11-07 09:09:39.192703',0,0), + ('922890de4c23a973501d9b7d12eaa045c4aab818c1a9cbff138a843b54940ca8','92e14585d67fcca2da81444ef71a90480e66ae00988b9daa7b29a01efd29ff43','2a6663a30f21c276c77bc4f5029da6a626075bc5506e2453f2e228ffa5a2c357',NULL,'{"tx_hash": "922890de4c23a973501d9b7d12eaa045c4aab818c1a9cbff138a843b54940ca8", "output_index": 1}' FORMAT JSON,202857,'[{"tx_hash": "42c04d1f92f1d0b81852962ba85f727b4c993a69a3d61742285328e8744dfc99", "output_index": 0}]' FORMAT JSON,false,0,'[{"tx_hash": "922890de4c23a973501d9b7d12eaa045c4aab818c1a9cbff138a843b54940ca8", "output_index": 0}]' FORMAT JSON,NULL,NULL,NULL,10809340,NULL,10909336,0,NULL,184505,1666492540, TIMESTAMP '2023-11-07 09:09:39.196543',0,0); From 07812a0adade0ded58795616b52fa4aa1762c861 Mon Sep 17 00:00:00 2001 From: Kammerlo Date: Thu, 30 May 2024 11:20:18 +0200 Subject: [PATCH 2/2] feat: added transactionSize and scriptSize to entity extracted from TransactionEvent --- .../yaci/store/app/YaciStoreApplication.java | 5 + gradle/libs.versions.toml | 2 +- .../transaction/domain/TransactionSize.java | 13 -- .../processor/TransactionProcessor.java | 7 +- .../transaction/util/TransactionSizeUtil.java | 215 ------------------ 5 files changed, 8 insertions(+), 234 deletions(-) delete mode 100644 stores/transaction/src/main/java/com/bloxbean/cardano/yaci/store/transaction/domain/TransactionSize.java delete mode 100644 stores/transaction/src/main/java/com/bloxbean/cardano/yaci/store/transaction/util/TransactionSizeUtil.java diff --git a/applications/all/src/main/java/com/bloxbean/cardano/yaci/store/app/YaciStoreApplication.java b/applications/all/src/main/java/com/bloxbean/cardano/yaci/store/app/YaciStoreApplication.java index 5729890bb..ac264e6fa 100644 --- a/applications/all/src/main/java/com/bloxbean/cardano/yaci/store/app/YaciStoreApplication.java +++ b/applications/all/src/main/java/com/bloxbean/cardano/yaci/store/app/YaciStoreApplication.java @@ -1,5 +1,6 @@ package com.bloxbean.cardano.yaci.store.app; +import com.bloxbean.cardano.yaci.core.config.YaciConfig; import io.swagger.v3.oas.models.Components; import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.info.Info; @@ -16,6 +17,10 @@ public class YaciStoreApplication { public static void main(String[] args) { + + // will be removed before merge + YaciConfig.INSTANCE.setReturnTxSize(true); + SpringApplication.run(YaciStoreApplication.class, args); } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 3d715df6e..64282c1cd 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,5 +1,5 @@ [libraries] -yaci = "com.bloxbean.cardano:yaci:0.3.0-beta14" +yaci = "com.bloxbean.cardano:yaci:0.3.0-beta16-12c9f2b-SNAPSHOT" cardano-client-lib = "com.bloxbean.cardano:cardano-client-lib:0.5.1" cardano-client-backend = "com.bloxbean.cardano:cardano-client-backend:0.5.1" cardano-client-backend-ogmios = "com.bloxbean.cardano:cardano-client-backend-ogmios:0.5.1" diff --git a/stores/transaction/src/main/java/com/bloxbean/cardano/yaci/store/transaction/domain/TransactionSize.java b/stores/transaction/src/main/java/com/bloxbean/cardano/yaci/store/transaction/domain/TransactionSize.java deleted file mode 100644 index 6c7ec7bc8..000000000 --- a/stores/transaction/src/main/java/com/bloxbean/cardano/yaci/store/transaction/domain/TransactionSize.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.bloxbean.cardano.yaci.store.transaction.domain; - -import lombok.Builder; -import lombok.Data; - -@Data -@Builder -public class TransactionSize { - - int size; - int scriptSize; - -} diff --git a/stores/transaction/src/main/java/com/bloxbean/cardano/yaci/store/transaction/processor/TransactionProcessor.java b/stores/transaction/src/main/java/com/bloxbean/cardano/yaci/store/transaction/processor/TransactionProcessor.java index e6fab7a7f..672c0f10f 100644 --- a/stores/transaction/src/main/java/com/bloxbean/cardano/yaci/store/transaction/processor/TransactionProcessor.java +++ b/stores/transaction/src/main/java/com/bloxbean/cardano/yaci/store/transaction/processor/TransactionProcessor.java @@ -13,14 +13,12 @@ import com.bloxbean.cardano.yaci.store.events.EventMetadata; import com.bloxbean.cardano.yaci.store.events.TransactionEvent; import com.bloxbean.cardano.yaci.store.transaction.domain.InvalidTransaction; -import com.bloxbean.cardano.yaci.store.transaction.domain.TransactionSize; import com.bloxbean.cardano.yaci.store.transaction.domain.TxWitnessType; import com.bloxbean.cardano.yaci.store.transaction.domain.Txn; import com.bloxbean.cardano.yaci.store.transaction.domain.TxnWitness; import com.bloxbean.cardano.yaci.store.transaction.storage.InvalidTransactionStorage; import com.bloxbean.cardano.yaci.store.transaction.storage.TransactionStorage; import com.bloxbean.cardano.yaci.store.transaction.storage.TransactionWitnessStorage; -import com.bloxbean.cardano.yaci.store.transaction.util.TransactionSizeUtil; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; import lombok.RequiredArgsConstructor; @@ -80,7 +78,6 @@ public void handleTransactionEvent(TransactionEvent event) { .map(transactionInput -> new UtxoKey(transactionInput.getTransactionId(), transactionInput.getIndex())) .collect(Collectors.toList()); } - TransactionSize transactionSizeAndScriptSize = TransactionSizeUtil.getTransactionSizeAndScriptSize(transaction); Txn txn = Txn.builder() .txHash(transaction.getTxHash()) @@ -102,8 +99,8 @@ public void handleTransactionEvent(TransactionEvent event) { .collateralReturn(new UtxoKey(transaction.getTxHash(), outputs.size())) .referenceInputs(referenceInputs) .invalid(transaction.isInvalid()) - .size(transactionSizeAndScriptSize.getSize()) - .scriptSize(transactionSizeAndScriptSize.getScriptSize()) + .size(transaction.getTxSize()) + .scriptSize(transaction.getTxScriptSize()) .build(); txList.add(txn); diff --git a/stores/transaction/src/main/java/com/bloxbean/cardano/yaci/store/transaction/util/TransactionSizeUtil.java b/stores/transaction/src/main/java/com/bloxbean/cardano/yaci/store/transaction/util/TransactionSizeUtil.java deleted file mode 100644 index e3c5d3a95..000000000 --- a/stores/transaction/src/main/java/com/bloxbean/cardano/yaci/store/transaction/util/TransactionSizeUtil.java +++ /dev/null @@ -1,215 +0,0 @@ -package com.bloxbean.cardano.yaci.store.transaction.util; - -import co.nstant.in.cbor.model.Array; -import co.nstant.in.cbor.model.ByteString; -import co.nstant.in.cbor.model.DataItem; -import co.nstant.in.cbor.model.Map; -import co.nstant.in.cbor.model.SimpleValue; -import co.nstant.in.cbor.model.UnsignedInteger; -import com.bloxbean.cardano.client.exception.CborDeserializationException; -import com.bloxbean.cardano.client.exception.CborSerializationException; -import com.bloxbean.cardano.client.transaction.spec.script.NativeScript; -import com.bloxbean.cardano.client.util.HexUtil; -import com.bloxbean.cardano.yaci.core.model.PlutusScript; -import com.bloxbean.cardano.yaci.core.util.CborSerializationUtil; -import com.bloxbean.cardano.yaci.helper.model.Transaction; -import com.bloxbean.cardano.yaci.store.transaction.domain.TransactionSize; -import com.fasterxml.jackson.core.JsonProcessingException; -import lombok.extern.slf4j.Slf4j; -import org.antlr.v4.runtime.misc.Pair; - -import java.util.List; -import java.util.concurrent.atomic.AtomicInteger; - -@Slf4j -public class TransactionSizeUtil { - - /** - * This functions reconstructs the original transaction and calculates the size and scriptSize from these. - * @param tx Transaction to be parsed. - * @return Pair containing TransactionSize as A and scriptSize as B - */ - public static TransactionSize getTransactionSizeAndScriptSize(Transaction tx) { - // Reconstructing Transaction based on CDDL specification - https://github.com/IntersectMBO/cardano-ledger/blob/master/eras/alonzo/impl/cddl-files/alonzo.cddl - Map signedTransaction = new Map(); - if(tx.getBody().getCbor() == null) { - // YACIConfig.INSTANCE.setReturnTxBodyCbor is false no cbor body contained - return TransactionSize.builder() - .size(0) - .scriptSize(0) - .build(); - } - DataItem txBody = CborSerializationUtil.deserialize(HexUtil.decodeHexString(tx.getBody().getCbor()))[0]; - signedTransaction.put(new UnsignedInteger(0), txBody); - int scriptSize = addWitnessSetToSignedTransaction(tx, signedTransaction); - - if(tx.getBlockNumber() > 64902L) { // starting from alonzo era - signedTransaction.put(new UnsignedInteger( - 2), SimpleValue.TRUE); - } - - addAuxDataToTransaction(tx, signedTransaction); - - byte[] serialize = CborSerializationUtil.serialize(signedTransaction); - - int txSize = serialize.length; - return TransactionSize.builder() - .size(txSize) - .scriptSize(scriptSize) - .build(); - } - - /** - * Reconstructing the Witness_set from transaction and adding it to signedTransaction - * CDDL Definition: https://github.com/IntersectMBO/cardano-ledger/blob/e6b6d4f85fb72b5cb5b5361e534d3bb71bb9e55e/eras/alonzo/impl/cddl-files/alonzo.cddl#L269 - * @param tx - * @param signedTransaction - * @return - */ - private static int addWitnessSetToSignedTransaction(Transaction tx, Map signedTransaction) { - // adding witnesses to signedTransaction - Map witnessSet = new Map(); - int scriptSize = 0; - addvKeyWitnessToWitness(tx, witnessSet); - addNativeScriptsToWitness(tx, witnessSet); - addBootstrapToWitness(tx, witnessSet); - - scriptSize += addPlutusToWitness(witnessSet, tx.getWitnesses().getPlutusV1Scripts(), - 3); - scriptSize += addPlutusToWitness(witnessSet, tx.getWitnesses().getPlutusV2Scripts(), 6); - scriptSize += addPlutusToWitness(witnessSet, tx.getWitnesses().getPlutusV3Scripts(), 7); - - addDatumToWitness(tx, witnessSet); - addRedeemerToWitness(tx, witnessSet); - - if(!witnessSet.getKeys().isEmpty()) { - signedTransaction.put(new UnsignedInteger(1), witnessSet); - } - return scriptSize; - } - - /** - * Adding the datum to Witness set. CDDL spec: https://github.com/IntersectMBO/cardano-ledger/blob/e6b6d4f85fb72b5cb5b5361e534d3bb71bb9e55e/eras/alonzo/impl/cddl-files/alonzo.cddl#L280 - * @param tx transaction to extract the datum - * @param witnessSet witnessSet to add the datum - */ - private static void addDatumToWitness(Transaction tx, Map witnessSet) { - if(!tx.getWitnesses().getDatums().isEmpty()) { - Array array = new Array(); - // could speed it up by passing an empty array, since we are only interested in the size not the content - tx.getWitnesses().getDatums().forEach(datum -> array.add(new ByteString(HexUtil.decodeHexString(datum.getCbor())))); - witnessSet.put(new UnsignedInteger(4), array); - } - } - - /** - * Adding Plutus Script data to witnessSet. Can be used for V1, V2 and V3. CDDL spec: https://github.com/IntersectMBO/cardano-ledger/blob/e6b6d4f85fb72b5cb5b5361e534d3bb71bb9e55e/eras/alonzo/impl/cddl-files/alonzo.cddl#L278 - * @param witnessSet witnessset to add the data - * @param scripts List of PlutusScripts - * @param witnessSetIndex Index where to add the datum based on cddl spec - * @return - */ - private static int addPlutusToWitness(Map witnessSet, List scripts, int witnessSetIndex) { - AtomicInteger scriptSize = new AtomicInteger(); - Array array = new Array(); - if(!scripts.isEmpty()) { - scripts.forEach(script -> { - scriptSize.addAndGet(script.getContent().length() / 2); // adding have the string length, sinze it's 4bit hex and we need the byte length - array.add(new ByteString(HexUtil.decodeHexString(script.getContent()))); - }); - witnessSet.put(new UnsignedInteger(witnessSetIndex), array); - } - return scriptSize.get(); - } - - /** - * Adding Redemer data to witnessset. CDDL spec: https://github.com/IntersectMBO/cardano-ledger/blob/e6b6d4f85fb72b5cb5b5361e534d3bb71bb9e55e/eras/alonzo/impl/cddl-files/alonzo.cddl#L302 - * @param tx transaction to extract the redeemer data - * @param witnessSet witnesset to add the data based on cddl spec - */ - private static void addRedeemerToWitness(Transaction tx, Map witnessSet) { - if(!tx.getWitnesses().getRedeemers().isEmpty()) { - Array array = new Array(); - // could speed it up by passing an empty array, since we are only interested in the size not the content - tx.getWitnesses().getRedeemers().forEach(redeemer -> array.add(new ByteString(HexUtil.decodeHexString(redeemer.getCbor())))); - witnessSet.put(new UnsignedInteger(5), array); - } - } - - /** - * Extracting bootstrap data and adding it to witnessSet. CDDL spec: https://github.com/IntersectMBO/cardano-ledger/blob/e6b6d4f85fb72b5cb5b5361e534d3bb71bb9e55e/eras/alonzo/impl/cddl-files/alonzo.cddl#L348 - * @param tx Transcation to get the bootstrap from - * @param witnessSet witnessSet to add the data - */ - private static void addBootstrapToWitness(Transaction tx, Map witnessSet) { - if(!tx.getWitnesses().getBootstrapWitnesses().isEmpty()) { - Array array = new Array(); - tx.getWitnesses().getBootstrapWitnesses().forEach(bootstrapWitness -> { - Array witnessArray = new Array(); - witnessArray.add(new ByteString(HexUtil.decodeHexString(bootstrapWitness.getPublicKey()))); - witnessArray.add(new ByteString(HexUtil.decodeHexString(bootstrapWitness.getSignature()))); - witnessArray.add(new ByteString(HexUtil.decodeHexString(bootstrapWitness.getChainCode()))); - witnessArray.add(new ByteString(HexUtil.decodeHexString(bootstrapWitness.getAttributes()))); - array.add(witnessArray); - }); - witnessSet.put(new UnsignedInteger(2), array); - } - } - - /** - * Extracting the Auxiliary data and adding it to the transaction. CDDL spec: https://github.com/IntersectMBO/cardano-ledger/blob/e6b6d4f85fb72b5cb5b5361e534d3bb71bb9e55e/eras/alonzo/impl/cddl-files/alonzo.cddl#L17 - * @param tx Transcation to get the auxdata from - * @param signedTransaction Map to add the auxdata - */ - private static void addAuxDataToTransaction(Transaction tx, Map signedTransaction) { - if(tx.getAuxData() != null) { - Array auxiliaryData = new Array(); - if(tx.getAuxData().getMetadataCbor() != null) { - auxiliaryData.add(CborSerializationUtil.deserialize( - HexUtil.decodeHexString(tx.getAuxData().getMetadataCbor()))[0]); - } - signedTransaction.put(new UnsignedInteger(3), auxiliaryData); - } - } - - /** - * Extracting VKey and adding it to witnessSet. CDDL spec: https://github.com/IntersectMBO/cardano-ledger/blob/e6b6d4f85fb72b5cb5b5361e534d3bb71bb9e55e/eras/alonzo/impl/cddl-files/alonzo.cddl#L346 - * @param tx transaction to extract the data - * @param witnessSet witnessSet to add the data - */ - private static void addvKeyWitnessToWitness(Transaction tx, Map witnessSet) { - if(!tx.getWitnesses().getVkeyWitnesses().isEmpty()) { - Array vKeyWitnessArray = new Array(); - tx.getWitnesses().getVkeyWitnesses().forEach(vkeyWitness -> { - Array vitnessArray = new Array(); - vitnessArray.add(new ByteString(HexUtil.decodeHexString(vkeyWitness.getKey()))); - vitnessArray.add(new ByteString(HexUtil.decodeHexString(vkeyWitness.getSignature()))); // could speed it up by passing an empty array, since we are only interested in the size not the content - vKeyWitnessArray.add(vitnessArray); - }); - witnessSet.put(new UnsignedInteger(0), vKeyWitnessArray); - } - } - - /** - * Extracting NativeScript data and adding it to witnessSet. CDDL spec: https://github.com/IntersectMBO/cardano-ledger/blob/e6b6d4f85fb72b5cb5b5361e534d3bb71bb9e55e/eras/alonzo/impl/cddl-files/alonzo.cddl#L355 - * @param tx transaction to extract the data - * @param witnessSet witnessSet to add the data - */ - private static void addNativeScriptsToWitness(Transaction tx, Map witnessSet) { - if(!tx.getWitnesses().getNativeScripts().isEmpty()) { - Array nativeScripts = new Array(); - tx.getWitnesses().getNativeScripts().forEach(script -> { - NativeScript nativeScript; - try { - nativeScript = NativeScript.deserializeJson(script.getContent()); - nativeScripts.add(new ByteString(nativeScript.getScriptHash())); - } catch (CborDeserializationException | JsonProcessingException | - CborSerializationException e) { - log.error("Can't parse Native script for Transaction: " + tx.getTxHash()); - } - }); - witnessSet.put(new UnsignedInteger(1), nativeScripts); - } - } - -}