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

fix: Handle tag (258) in conway era set while parsing datum array bytes #80

Merged
merged 3 commits into from
Sep 16, 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 @@ -3,6 +3,7 @@
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.MajorType;
import co.nstant.in.cbor.model.Special;
import co.nstant.in.cbor.model.UnsignedInteger;
import com.bloxbean.cardano.yaci.core.util.Tuple;
Expand Down Expand Up @@ -129,10 +130,27 @@ public static List<byte[]> getArrayBytes(byte[] bytes) throws CborException {
ByteArrayInputStream stream = new ByteArrayInputStream(bytes);
final List<byte[]> dataItemBytes = new ArrayList<>();
CborDecoder decoder = new CborDecoder(stream);
final var arraySymbol = stream.read();

var arraySymbol = stream.read();

//Check if it's a tag, then read the next byte
int tagSkipBytes = 0;
var type = MajorType.ofByte(arraySymbol);
if (type == MajorType.TAG) {
tagSkipBytes = skipBytes(arraySymbol);

while (tagSkipBytes-- > 0) {
stream.read();
}

arraySymbol = stream.read();
}

//final var arraySymbol = stream.read();
final var dataLength = getLength(arraySymbol,
getSymbolBytes(bytes.length - stream.available(), bytes));
int skipBytes = skipBytes(arraySymbol);
int skipBytes = tagSkipBytes + skipBytes(arraySymbol);


if (dataLength != TransactionBodyExtractor.INFINITY) {
if (dataLength == BigInteger.ONE.intValue()) {
Expand Down Expand Up @@ -164,6 +182,11 @@ public static List<byte[]> getArrayBytes(byte[] bytes) throws CborException {
return dataItemBytes;
}

private static boolean isTag(int code) {
// Major types 6 in CBOR is reserved for tags
return (code & 0b111_00000) == 0b110_00000;
}

private static int skipBytes(int initialByte) throws CborException {
switch (AdditionalInformation.ofByte(initialByte)) {
case DIRECT:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,14 @@ public static Array createRedeemerArray(int tag, int index, BigIntPlutusData dat

return array;
}

@Test
@SneakyThrows
public void parseDatumArrayBytesWithTag() { //Conway
String datumArrayHex = "d901028cd8799f581c57abe9a8de8730b0bbf1962b3e0858019a3f013218eeceac46d43220d8799fd8799f581c57abe9a8de8730b0bbf1962b3e0858019a3f013218eeceac46d43220ffd8799fd8799fd8799f581cadd78874051724b4f838828e579e5ddca7d1d38bf58963890808ba7affffffffd8799f4040ff1a01c9c3801a01c9c380d8799f581c171163f05e4f30b6be3c22668c37978e7d508b84f83558e523133cdf4474454d50ffd8799f1b01400000000000001b00596c9e236c15d5ff58201ea5abe908b30d2e51a7bb0174c8f3a35f925109ca3ca77e84f980233c151dc3d87a80d87a80001a000f42401a000f4240d8799f1a000f42401a00015f9000ff00ffd8799fd8799f5820e86ef9e7cb098acc7b31df42c045320e1f4d171ef0bfdc02dc5384739cc86b66ff00ffd8799f581c57abe9a8de8730b0bbf1962b3e0858019a3f013218eeceac46d43220d8799fd8799f581c57abe9a8de8730b0bbf1962b3e0858019a3f013218eeceac46d43220ffd8799fd8799fd8799f581cadd78874051724b4f838828e579e5ddca7d1d38bf58963890808ba7affffffffd8799f581c171163f05e4f30b6be3c22668c37978e7d508b84f83558e523133cdf4474454d50ff1a01c9c3801a01c9c380d8799f4040ffd8799f1b00e4875b3e1437cb1b0280000000000000ff5820208b42a46ed8cd266834fda53f1a27c2f7598dbe9ee898ba423527fcabddc42dd87a80d87a80001a000f42401a000f4240d8799f1a000f42401a00015f9000ff00ffd8799f581c57abe9a8de8730b0bbf1962b3e0858019a3f013218eeceac46d43220d8799fd8799f581c57abe9a8de8730b0bbf1962b3e0858019a3f013218eeceac46d43220ffd8799fd8799fd8799f581cadd78874051724b4f838828e579e5ddca7d1d38bf58963890808ba7affffffffd8799f4040ff1a01c9c3801a01c9c380d8799f581c171163f05e4f30b6be3c22668c37978e7d508b84f83558e523133cdf4474454d50ffd8799f1b00280000000000001b0009ef9fcb0c026dff5820757e1e9d8c807ae14c7374619a8f55b025cfaf0059befe46d7b0afb8c281ff5fd87a80d87a80001a000f42401a000f4240d8799f1a000f42401a00015f9000ff00ffd8799f581c57abe9a8de8730b0bbf1962b3e0858019a3f013218eeceac46d43220d8799fd8799f581c57abe9a8de8730b0bbf1962b3e0858019a3f013218eeceac46d43220ffd8799fd8799fd8799f581cadd78874051724b4f838828e579e5ddca7d1d38bf58963890808ba7affffffffd8799f4040ff1a01c9c3801a01c9c380d8799f581c171163f05e4f30b6be3c22668c37978e7d508b84f83558e523133cdf4474454d50ffd8799f1b02800000000000001b00a8e99c7bcc293dff5820800c3d9eb1b7ce2a85215b94e8beeedf5e11bf440703f2f9203ef551c13426d4d87a80d87a80001a000f42401a000f4240d8799f1a000f42401a00015f9000ff00ffd8799fd8799f58201038cf4c79b33a52c12febae9611be6a7b29e979864f191c3b0f94b96be8ab44ff00ffd8799f581c57abe9a8de8730b0bbf1962b3e0858019a3f013218eeceac46d43220d8799fd8799f581c57abe9a8de8730b0bbf1962b3e0858019a3f013218eeceac46d43220ffd8799fd8799fd8799f581cadd78874051724b4f838828e579e5ddca7d1d38bf58963890808ba7affffffffd8799f581c171163f05e4f30b6be3c22668c37978e7d508b84f83558e523133cdf4474454d50ff1a01c9c3801a01c9c380d8799f4040ffd8799f1b001dcedf612407471b0050000000000000ff58207adcc7a92f6d1202346bf5b1e753f34421b8036f7b8379e4a0803df105c6ebbdd87a80d87a80001a000f42401a000f4240d8799f1a000f42401a00015f9000ff00ffd8799fd8799f5820f9d5aebf7fc08f7e92e2d75654acffbf8aac7fb45230b3fae3146980440e5edeff00ffd8799f581c57abe9a8de8730b0bbf1962b3e0858019a3f013218eeceac46d43220d8799fd8799f581c57abe9a8de8730b0bbf1962b3e0858019a3f013218eeceac46d43220ffd8799fd8799fd8799f581cadd78874051724b4f838828e579e5ddca7d1d38bf58963890808ba7affffffffd8799f581c171163f05e4f30b6be3c22668c37978e7d508b84f83558e523133cdf4474454d50ff1a01c9c3801a01c9c380d8799f4040ffd8799f1b006d4bddb9841aaf1b0140000000000000ff582009c178687b538a7df7f0934a86aebe386930bd692f6b99662e986b89b82860d2d87a80d87a80001a000f42401a000f4240d8799f1a000f42401a00015f9000ff00ffd8799fd8799f5820d9ed5f4d544d8da842fc2952ee3bd5a84fb45036f83decc7664ac4422ae678f2ff00ffd8799fd8799f582093ed44a05680f8c6978c7dea392c95c78c8f9d909e657c8a7c589566b6d7e8d2ff00ffd8799fd8799f5820eb2442a4862716a687ad2054c5f25595abe1a9be03da0f8ff9264576bb6e24dcff00ff";

var datums = WitnessUtil.getArrayBytes(HexUtil.decodeHexString(datumArrayHex));

assertThat(datums.size()).isEqualTo(12);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import com.bloxbean.cardano.client.util.JsonUtil;
import com.bloxbean.cardano.yaci.core.common.Constants;
import com.bloxbean.cardano.yaci.core.model.Amount;
import com.bloxbean.cardano.yaci.core.model.Block;
import com.bloxbean.cardano.yaci.core.model.PlutusScript;
import com.bloxbean.cardano.yaci.core.model.Redeemer;
import com.bloxbean.cardano.yaci.core.model.*;
import com.bloxbean.cardano.yaci.core.model.byron.ByronMainBlock;
import com.bloxbean.cardano.yaci.core.protocol.blockfetch.BlockfetchAgentListener;
import com.bloxbean.cardano.yaci.core.protocol.chainsync.messages.Point;
Expand Down Expand Up @@ -259,6 +256,32 @@ public void blockFound(Block block) {
assertThat(sb.toString()).isNotEqualTo("null");
}

@Test
public void fetchBlock_datumArrayWith258Tag() throws InterruptedException {
VersionTable versionTable = N2NVersionTableConstant.v4AndAbove(protocolMagic);
BlockFetcher blockFetcher = new BlockFetcher(node, nodePort, versionTable);

CountDownLatch countDownLatch = new CountDownLatch(1);
List<Datum> datums = new ArrayList();

blockFetcher.start(block -> {
var witnessDatums = block.getTransactionWitness().get(1)
.getDatums();
datums.addAll(witnessDatums);
countDownLatch.countDown();
});

Point from = new Point(70336717, "027f4280f95473dc6dd0e79317ae9b259fcf00befd9a60544264bccc1cf0da4e");
Point to = new Point(70336717, "027f4280f95473dc6dd0e79317ae9b259fcf00befd9a60544264bccc1cf0da4e");
blockFetcher.fetch(from, to);

countDownLatch.await(10, TimeUnit.SECONDS);
blockFetcher.shutdown();

assertThat(datums).hasSize(12);
assertThat(datums.get(0).getHash()).isEqualTo("0956d8af620f0b1b0b8cc6fb7860f22251a99cfb919e2f17f38d1878e7a992c4");
}

/** Not able to fetch block 0
@Test
public void fetchGenesisBlockByron() throws InterruptedException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class GenesisBlockFinderIT {

@Test
// @Test
void getGenesisAndFirstBlock_mainnet() {
GenesisBlockFinder genesisBlockFinder = new GenesisBlockFinder(Constants.MAINNET_IOHK_RELAY_ADDR,
Constants.MAINNET_IOHK_RELAY_PORT, Constants.MAINNET_PROTOCOL_MAGIC);
Expand Down
Loading