Skip to content

Commit 04c3ef3

Browse files
committed
fix serialization of pruned branch
Signed-off-by: andreypfau <andreypfau@ton.org>
1 parent ab9b176 commit 04c3ef3

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

cell/src/boc/BagOfCellSerializer.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package org.ton.sdk.cell.boc
22

33
import kotlinx.io.Sink
44
import kotlinx.io.buffered
5+
import kotlinx.io.writeIntLe
56
import org.ton.sdk.bitstring.unsafe.UnsafeBitStringApi
67
import org.ton.sdk.bitstring.unsafe.UnsafeBitStringOperations
78
import org.ton.sdk.cell.Cell
@@ -10,7 +11,6 @@ import org.ton.sdk.cell.CellType
1011
import org.ton.sdk.cell.LoadedCell
1112
import org.ton.sdk.cell.boc.internal.DigestSink
1213
import org.ton.sdk.cell.boc.internal.writeLong
13-
import org.ton.sdk.cell.internal.DataCell
1414
import org.ton.sdk.cell.internal.LibraryCell
1515
import org.ton.sdk.cell.internal.PrunedCell
1616
import org.ton.sdk.crypto.CRC32C
@@ -308,7 +308,7 @@ internal class BagOfCellSerializer(
308308
)
309309
}
310310

311-
public fun serialize(
311+
fun serialize(
312312
sink: Sink,
313313
options: BagOfCells.EncodeOptions
314314
) {
@@ -394,7 +394,7 @@ internal class BagOfCellSerializer(
394394
val withHash = (options.withInternalHashes && cellInfo.isSpecial) ||
395395
(options.withTopHashes && cellInfo.isRootCell)
396396
val cell = cellInfo.cell ?: batchCells[indexInBatch++]
397-
if (i == 67) {
397+
if (i == 75) {
398398
println("catch")
399399
}
400400
val bytes = cell.serialize(buf, withHash)
@@ -411,7 +411,8 @@ internal class BagOfCellSerializer(
411411
output.flush()
412412
if (crC32c != null) {
413413
val crc = crC32c.intDigest()
414-
output.writeInt(crc)
414+
output.writeIntLe(crc)
415+
output.flush()
415416
}
416417
}
417418

@@ -448,7 +449,7 @@ internal class BagOfCellSerializer(
448449

449450
is PrunedCell -> {
450451
buf[offset++] = CellType.PRUNED_BRANCH.value.toByte()
451-
buf[offset++] = descriptor.levelMask.level.toByte()
452+
buf[offset++] = mask.mask.toByte()
452453
for (i in 0 until level) {
453454
val hash = hash(i).value
454455
hash.copyInto(buf, offset)

cell/test/BocTest.kt

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import kotlinx.io.bytestring.hexToByteString
22
import org.ton.sdk.bitstring.BitString
3+
import org.ton.sdk.cell.Cell
34
import org.ton.sdk.cell.CellBuilder
45
import org.ton.sdk.cell.LevelMask
56
import org.ton.sdk.cell.boc.BagOfCellSerializer
@@ -8,6 +9,7 @@ import org.ton.sdk.crypto.HashBytes
89
import utils.XorShift128Plus
910
import utils.genRandomCell
1011
import kotlin.io.encoding.Base64
12+
import kotlin.random.Random
1113
import kotlin.test.Test
1214
import kotlin.test.assertContentEquals
1315
import kotlin.test.assertEquals
@@ -38,25 +40,32 @@ class BocTest {
3840
}
3941
}
4042

41-
fun randomEncodeOption(random: XorShift128Plus): BagOfCells.EncodeOptions {
43+
fun randomEncodeOption(random: Random): BagOfCells.EncodeOptions {
4244
return encodeOptions[random.nextInt(encodeOptions.size)]
4345
}
4446

4547
@Test
4648
fun testBoc() {
47-
val random = XorShift128Plus(12311)
49+
val random = XorShift128Plus(1231112)
4850
repeat(1) {
4951
val cell = genRandomCell(random.nextInt(1, 1001), random)
50-
val cellHash = cell.hash()
51-
val encodeOption = randomEncodeOption(random)
52-
val serialized = BagOfCells.encodeToByteArray(cell, encodeOption)
52+
testBoc(cell, random)
53+
}
54+
}
5355

54-
val loadedCell = BagOfCells.decodeFromByteArray(serialized).first()
55-
assertEquals(cellHash, loadedCell.hash())
56+
fun testBoc(cell: Cell, random: Random) {
57+
val cellHash = cell.hash()
58+
val encodeOption = randomEncodeOption(random)
59+
val serialized = BagOfCells.encodeToByteArray(cell, encodeOption)
5660

57-
val newSerialized = BagOfCells.encodeToByteArray(loadedCell, encodeOption)
58-
assertContentEquals(serialized, newSerialized)
59-
}
61+
println("serialized size: ${serialized.size}")
62+
println("serialized hex: ${serialized.toHexString()}")
63+
64+
val loadedCell = BagOfCells.decodeFromByteArray(serialized).first()
65+
assertEquals(cellHash, loadedCell.hash())
66+
67+
val newSerialized = BagOfCells.encodeToByteArray(loadedCell, encodeOption)
68+
assertContentEquals(serialized, newSerialized)
6069
}
6170

6271
@Test

0 commit comments

Comments
 (0)