Skip to content

Commit

Permalink
fix: Handle edge cases in redeemer parsing when second byte is length…
Browse files Browse the repository at this point in the history
… (uint) (#105)
  • Loading branch information
satran004 authored Jan 13, 2025
1 parent b14ae5f commit e15d0ad
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,13 @@ private Block deserializeBlock(DataItem di, byte[] blockBody) {

//To fix #37 incorrect redeemer & datum hash due to cbor serialization <--> deserialization issue
//Get redeemer and datum bytes directly without full deserialization
handleWitnessDatumRedeemer(blockHeader.getHeaderBody().getBlockNumber(), witnessesSet, blockBody);
try {
handleWitnessDatumRedeemer(blockHeader.getHeaderBody().getBlockNumber(), witnessesSet, blockBody);
} catch (Exception e) {
//If extraction fails due to some reason
log.error("Extraction of redeemer and datum bytes without serialization/deserialization failed for block : " + blockHeader.getHeaderBody().getBlockNumber());
}

blockBuilder.transactionWitness(witnessesSet);

//auxiliary data
Expand Down Expand Up @@ -175,7 +181,14 @@ private void handleWitnessDatumRedeemer(long block, List<Witnesses> witnesses, b
var majorType = MajorType.ofByte(redeemersBytes[0] & 0xe0);

if (majorType == MajorType.ARRAY) {
var redeemerArrayBytes = getArrayBytes(redeemersBytes);
List<byte[]> redeemerArrayBytes = null;
try {
redeemerArrayBytes = getArrayBytes(redeemersBytes);
} catch (Exception e) {
log.error("Error parsing redeemer array bytes", e);
redeemerArrayBytes = new ArrayList<>();
}

if (redeemerArrayBytes.size() != redeemers.size()) {
log.error("block: {} redeemer does not have the same size", block);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,15 @@ public static List<Tuple<byte[], byte[]>> getRedeemerMapBytes(byte[] redeemerByt
ByteArrayInputStream stream = new ByteArrayInputStream(redeemerBytes);
CborDecoder decoder = new CborDecoder(stream);

//Skip the first byte which represents major type
stream.read();

//Check the type of second byte. If uint, then skip
//The map content should start with an array tag
var secondByte = MajorType.ofByte(redeemerBytes[1]);
if (secondByte == MajorType.UNSIGNED_INTEGER)
stream.read();

while (stream.available() > 0) {
int keyStartFrom = redeemerBytes.length - stream.available();
int previousAvailable = stream.available();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,14 @@ public void parseDatumArrayBytesWithTag() { //Conway

assertThat(datums.size()).isEqualTo(12);
}

@Test
@SneakyThrows
public void parseRedeemerMapBytes_withExtraUintInSecondByte() {
String redeemerHex = "b81e82000082d87d80821a00020ec31a025a2f7982000182d87d80821a000223711a028698fa82000282d87d80821a0002381f1a02b3027b82000382d87d80821a00024ccd1a02df6bfc82000482d87d80821a0002617b1a030bd57d82000582d87d80821a000276291a03383efe82000682d87d80821a00028ad71a0364a87f82000782d87d80821a00029f851a0391120082000882d87d80821a0002b4331a03bd7b8182000982d87d80821a0002c8e11a03e9e50282000a82d87d80821a0002dd8f1a04164e8382000b82d87d80821a0002f23d1a0442b80482000c82d87d80821a000306eb1a046f218582000d82d87d80821a00031b991a049b8b0682000e82d87d80821a000330471a04c7f48782000f82d87d80821a000344f51a04f45e0882001082d87d80821a000359a31a0520c78982001182d87d80821a00036e511a054d310a82001282d87d80821a000382ff1a05799a8b82001382d87d80821a000397ad1a05a6040c82001482d87d80821a0003ac5b1a05d26d8d82001582d87d80821a0003c1091a05fed70e82001682d87d80821a0003d5b71a062b408f82001782d87d80821a0003ea651a0657aa108200181882d87d80821a0003ff131a068413918200181982d87d80821a000413c11a06b07d128200181a82d87d80821a0004286f1a06dce6938200181b82d87d80821a00043d1d1a070950148200181c82d87d80821a000451cb1a0735b9958200181d82d87d80821a000466791a07622316";

var redeemers = WitnessUtil.getRedeemerMapBytes(HexUtil.decodeHexString(redeemerHex));

assertThat(redeemers).hasSize(30);
}
}

0 comments on commit e15d0ad

Please sign in to comment.