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

feat: Implemented txSize calculation to BlockSerializer #69

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 @@ -20,6 +20,8 @@ public class Block {
private List<Witnesses> transactionWitness = new ArrayList<>();
private Map<Integer, AuxData> auxiliaryDataMap = new LinkedHashMap();
private List<Integer> invalidTransactions = new ArrayList<>();
private List<Integer> txSizes = new ArrayList<>();
private List<Integer> txScriptSizes = new ArrayList<>();

private String cbor;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.bloxbean.cardano.yaci.core.model.serializers;

import co.nstant.in.cbor.model.*;
import com.bloxbean.cardano.client.util.Triple;
import com.bloxbean.cardano.yaci.core.common.EraUtil;
import com.bloxbean.cardano.yaci.core.config.YaciConfig;
import com.bloxbean.cardano.yaci.core.model.*;
Expand Down Expand Up @@ -60,21 +61,32 @@ private Block deserializeBlock(DataItem di, byte[] blockBody) {
**/

//Extract transaction bodies from block bytes directly to keep the tx hash same
List<Tuple<DataItem, byte[]>> txBodyTuples = TransactionBodyExtractor.getTxBodiesFromBlock(blockBody);
List<Triple<DataItem, byte[], Integer>> txBodyTriples = TransactionBodyExtractor.getTxBodiesFromBlock(blockBody);
List<TransactionBody> txnBodies = new ArrayList<>();
for (var tuple: txBodyTuples) {
TransactionBody txBody = TransactionBodySerializer.INSTANCE.deserializeDI(tuple._1, tuple._2);
List<Integer> transactionSizes = new ArrayList<>();
List<Integer> scriptSizes = new ArrayList<>();

for (var triple: txBodyTriples) {
TransactionBody txBody = TransactionBodySerializer.INSTANCE.deserializeDI(triple._1, triple._2);
transactionSizes.add(triple._3);
txnBodies.add(txBody);
}
blockBuilder.transactionBodies(txnBodies);

//witnesses
List<Witnesses> witnessesSet = new ArrayList<>();
Array witnessesListArr = (Array) blockArray.getDataItems().get(2);
for (DataItem witnessesDI: witnessesListArr.getDataItems()) {
for (int i = 0; i < witnessesListArr.getDataItems().size(); i++) {
DataItem witnessesDI = witnessesListArr.getDataItems().get(i);
if (witnessesDI == SimpleValue.BREAK)
continue;
Witnesses witnesses = WitnessesSerializer.INSTANCE.deserializeDI(witnessesDI);

// serializing only the witness we need to add it to transaction size
int witnessByteSize = CborSerializationUtil.serialize(witnessesDI).length;
transactionSizes.add(i, transactionSizes.get(i) + witnessByteSize);
scriptSizes.add(WitnessUtil.getScriptSizes(witnessesDI));

witnessesSet.add(witnesses);
}

Expand All @@ -90,7 +102,12 @@ private Block deserializeBlock(DataItem di, byte[] blockBody) {
if (txIdDI == SimpleValue.BREAK)
continue;
AuxData auxData = AuxDataSerializer.INSTANCE.deserializeDI(auxDataMapDI.get(txIdDI));
auxDataMap.put(toInt(txIdDI), auxData);
int auxDataIndex = toInt(txIdDI);
auxDataMap.put(auxDataIndex, auxData);

// Getting the Size and adding it to the respective transaction size
int auxDataSize = CborSerializationUtil.serialize(auxDataMapDI.get(txIdDI)).length;
transactionSizes.add(auxDataIndex, transactionSizes.get(auxDataIndex) + auxDataSize);
}
blockBuilder.auxiliaryDataMap(auxDataMap);

Expand All @@ -115,6 +132,9 @@ private Block deserializeBlock(DataItem di, byte[] blockBody) {
blockBuilder.cbor(HexUtil.encodeHexString(blockBody));
}

blockBuilder.txSizes(transactionSizes);
blockBuilder.txScriptSizes(scriptSizes);

return blockBuilder.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import co.nstant.in.cbor.model.AdditionalInformation;
import co.nstant.in.cbor.model.DataItem;
import co.nstant.in.cbor.model.Special;
import com.bloxbean.cardano.client.util.Triple;
import com.bloxbean.cardano.yaci.core.util.Tuple;
import lombok.SneakyThrows;

Expand All @@ -17,9 +18,14 @@ public class TransactionBodyExtractor {

public static final int INFINITY = -1;

/**
* Extracting TxBodies from raw block body.
* @param blockBody raw block body
* @return Triple: Deserialized Dataitem, Raw Bytes and Size
*/
@SneakyThrows
public static List<Tuple<DataItem, byte[]>> getTxBodiesFromBlock(byte[] blockBody) {
List<Tuple<DataItem, byte[]>> txBodyTuples = new ArrayList<>();
public static List<Triple<DataItem, byte[], Integer>> getTxBodiesFromBlock(byte[] blockBody) {
List<Triple<DataItem, byte[], Integer>> txBodyTuples = new ArrayList<>();
ByteArrayInputStream bais = new ByteArrayInputStream(blockBody);
CborDecoder decoder = new CborDecoder(bais);

Expand All @@ -35,14 +41,6 @@ public static List<Tuple<DataItem, byte[]>> getTxBodiesFromBlock(byte[] blockBod

long length = getLength(arrTxBodySize,getSymbolBytes(blockBody.length - bais.available(),blockBody));
int start = blockBody.length - bais.available();
for(int i = 0 ; i < length; i++){
int previous = bais.available();
DataItem dataItem = decoder.decodeNext();
byte[] txBodyRaw = new byte[previous - bais.available()];
System.arraycopy(blockBody,start,txBodyRaw,0,txBodyRaw.length);
txBodyTuples.add(new Tuple<>(dataItem, txBodyRaw));
start = blockBody.length - bais.available();
}
if(AdditionalInformation.INDEFINITE.equals(AdditionalInformation.ofByte(arrTxBodySize))) {
for (;;) {
int previous = bais.available();
Expand All @@ -53,12 +51,24 @@ public static List<Tuple<DataItem, byte[]>> getTxBodiesFromBlock(byte[] blockBod
if (Special.BREAK.equals(dataItem)) {
break;
}
int txSize = previous - bais.available();
byte[] txBodyRaw = new byte[previous - bais.available()];
System.arraycopy(blockBody, start, txBodyRaw, 0, txBodyRaw.length);
txBodyTuples.add(new Tuple<>(dataItem, txBodyRaw));
txBodyTuples.add(new Triple<>(dataItem, txBodyRaw, txSize));
start = blockBody.length - bais.available();
}
} else {
for (int i = 0; i < length; i++) {
int previous = bais.available();
DataItem dataItem = decoder.decodeNext();
int txSize = previous - bais.available();
byte[] txBodyRaw = new byte[txSize];
System.arraycopy(blockBody, start, txBodyRaw, 0, txBodyRaw.length);
txBodyTuples.add(new Triple<>(dataItem, txBodyRaw, txSize));
start = blockBody.length - bais.available();
}
}

return txBodyTuples;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
import co.nstant.in.cbor.CborDecoder;
import co.nstant.in.cbor.CborException;
import co.nstant.in.cbor.model.AdditionalInformation;
import co.nstant.in.cbor.model.DataItem;
import co.nstant.in.cbor.model.Map;
import co.nstant.in.cbor.model.Special;
import co.nstant.in.cbor.model.UnsignedInteger;
import com.bloxbean.cardano.yaci.core.util.CborSerializationUtil;
import com.bloxbean.cardano.yaci.core.util.Tuple;

import java.io.ByteArrayInputStream;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -211,4 +215,22 @@ public static List<byte[]> getRedeemerFields(byte[] redeemer) throws CborExcepti

return insideRedeemer;
}

public static int getScriptSize(Map witnessMap, int index) {
Collection<DataItem> keys = witnessMap.getKeys();
UnsignedInteger unsignedInteger = new UnsignedInteger(BigInteger.valueOf(index));
if(keys.contains(unsignedInteger)) {
return CborSerializationUtil.serialize(witnessMap.get(unsignedInteger)).length;
} else {
return 0;
}
}

public static int getScriptSizes(DataItem witnessesDI) {
Map witnessMap = (Map) witnessesDI;
int scriptSize = getScriptSize(witnessMap, 3);
scriptSize += getScriptSize(witnessMap, 6);
scriptSize += getScriptSize(witnessMap, 7);
return scriptSize;
}
}

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
group = com.bloxbean.cardano
artifactId = yaci
version = 0.3.0-beta15-SNAPSHOT
version = 0.3.0-beta16-SNAPSHOT
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public void blockFound(Block block) {
.witnesses(witnesses)
.auxData(auxData)
.invalid(invalidTxn)
.txSize(block.getTxSizes() != null ? block.getTxSizes().get(i) : 0)
.txScriptSize(block.getTxScriptSizes() != null ? block.getTxScriptSizes().get(i) : 0)
.build();

transactionEvents.add(transactionEvent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ public class Transaction {
private Witnesses witnesses;
private AuxData auxData;
private boolean invalid;
private int txSize;
private int txScriptSize;
}
Loading