From 64103d79101b085a3b45884745382582ebbd1cbf Mon Sep 17 00:00:00 2001 From: Satya Date: Thu, 12 Sep 2024 15:59:51 +0800 Subject: [PATCH 1/3] fix: Handle tag (258) in conway era set --- .../model/serializers/util/WitnessUtil.java | 27 +++++++++++++++++-- .../model/serializers/WitnessUtilTest.java | 10 +++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/com/bloxbean/cardano/yaci/core/model/serializers/util/WitnessUtil.java b/core/src/main/java/com/bloxbean/cardano/yaci/core/model/serializers/util/WitnessUtil.java index f1107e36..d52cbbe2 100644 --- a/core/src/main/java/com/bloxbean/cardano/yaci/core/model/serializers/util/WitnessUtil.java +++ b/core/src/main/java/com/bloxbean/cardano/yaci/core/model/serializers/util/WitnessUtil.java @@ -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; @@ -129,10 +130,27 @@ public static List getArrayBytes(byte[] bytes) throws CborException { ByteArrayInputStream stream = new ByteArrayInputStream(bytes); final List 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()) { @@ -164,6 +182,11 @@ public static List 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: diff --git a/core/src/test/java/com/bloxbean/cardano/yaci/core/model/serializers/WitnessUtilTest.java b/core/src/test/java/com/bloxbean/cardano/yaci/core/model/serializers/WitnessUtilTest.java index 35741dbf..ee156c00 100644 --- a/core/src/test/java/com/bloxbean/cardano/yaci/core/model/serializers/WitnessUtilTest.java +++ b/core/src/test/java/com/bloxbean/cardano/yaci/core/model/serializers/WitnessUtilTest.java @@ -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); + } } From 9670627b2498452674865f757a8643056e0946c1 Mon Sep 17 00:00:00 2001 From: Satya Date: Thu, 12 Sep 2024 16:34:07 +0800 Subject: [PATCH 2/3] chore: Add test for fetching block with datum array This commit introduces a new integration test to verify the fetching of blocks with datum arrays containing a 258 tag. --- .../cardano/yaci/helper/BlockFetcherIT.java | 31 ++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/helper/src/integrationTest/java/com/bloxbean/cardano/yaci/helper/BlockFetcherIT.java b/helper/src/integrationTest/java/com/bloxbean/cardano/yaci/helper/BlockFetcherIT.java index 73862332..28a784cf 100644 --- a/helper/src/integrationTest/java/com/bloxbean/cardano/yaci/helper/BlockFetcherIT.java +++ b/helper/src/integrationTest/java/com/bloxbean/cardano/yaci/helper/BlockFetcherIT.java @@ -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; @@ -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 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 { From ebae429498fc52f708fed05391cdf59d1fbcb3b9 Mon Sep 17 00:00:00 2001 From: Satya Date: Sun, 15 Sep 2024 19:40:43 +0800 Subject: [PATCH 3/3] Disable failing test for mainnet in GenesisBlockFinderIT --- .../com/bloxbean/cardano/yaci/helper/GenesisBlockFinderIT.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helper/src/test/java/com/bloxbean/cardano/yaci/helper/GenesisBlockFinderIT.java b/helper/src/test/java/com/bloxbean/cardano/yaci/helper/GenesisBlockFinderIT.java index fbe4d3d2..a5a0a1ad 100644 --- a/helper/src/test/java/com/bloxbean/cardano/yaci/helper/GenesisBlockFinderIT.java +++ b/helper/src/test/java/com/bloxbean/cardano/yaci/helper/GenesisBlockFinderIT.java @@ -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);