From ead1668301e478e1d1bf61a44422ad49c3249d1b Mon Sep 17 00:00:00 2001 From: Andrey Pfau Date: Sat, 24 Dec 2022 22:21:48 +0300 Subject: [PATCH] fix tests --- gradle/wrapper/gradle-wrapper.properties | 2 +- .../commonMain/kotlin/org/ton/block/Block.kt | 18 +++++----- .../kotlin/org/ton/block/MerkleUpdate.kt | 35 +++++++++---------- .../kotlin/org/ton/block/PrevBlksInfo.kt | 17 +++++---- .../kotlin/org/ton/cell/CellSlice.kt | 2 +- .../kotlin/org/ton/hashmap/HashMapEdge.kt | 6 ++-- .../kotlin/org/ton/hashmap/HashMapNodeFork.kt | 5 ++- .../kotlin/org/ton/hashmap/RootHashMapE.kt | 2 +- .../kotlin/org/ton/hashmap/HashMapEdgeTest.kt | 12 +++---- .../functions/LiteServerSendMessage.kt | 4 +-- .../commonMain/kotlin/org/ton/tlb/CellRef.kt | 26 ++++++++------ 11 files changed, 67 insertions(+), 62 deletions(-) diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 070cb702..ae04661e 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/ton-kotlin-block/src/commonMain/kotlin/org/ton/block/Block.kt b/ton-kotlin-block/src/commonMain/kotlin/org/ton/block/Block.kt index a5e2d529..4503c77a 100644 --- a/ton-kotlin-block/src/commonMain/kotlin/org/ton/block/Block.kt +++ b/ton-kotlin-block/src/commonMain/kotlin/org/ton/block/Block.kt @@ -15,12 +15,12 @@ data class Block( val stateUpdateCell: CellRef>, val extraCell: CellRef ) { - val info by infoCell - val valueFlow by valueFlowCell - val stateUpdate by stateUpdateCell - val extra by extraCell + val info: BlockInfo by infoCell + val valueFlow: ValueFlow by valueFlowCell + val stateUpdate: MerkleUpdate by stateUpdateCell + val extra: BlockExtra by extraCell - companion object : TlbCombinatorProvider by TlbConstructor.asTlbCombinator() + public companion object : TlbCombinatorProvider by TlbConstructor.asTlbCombinator() private object TlbConstructor : org.ton.tlb.TlbConstructor( schema = "block#11ef55aa global_id:int32 " + @@ -35,10 +35,10 @@ data class Block( value: Block ) = cellBuilder { storeInt(value.global_id, 32) - storeRef(value.infoCell.cell) - storeRef(value.valueFlowCell.cell) - storeRef(value.stateUpdateCell.cell) - storeRef(value.extraCell.cell) + storeRef(value.infoCell.toCell(BlockInfo)) + storeRef(value.valueFlowCell.toCell(ValueFlow)) + storeRef(value.stateUpdateCell.toCell(merkleUpdate)) + storeRef(value.extraCell.toCell(BlockExtra)) } override fun loadTlb( diff --git a/ton-kotlin-block/src/commonMain/kotlin/org/ton/block/MerkleUpdate.kt b/ton-kotlin-block/src/commonMain/kotlin/org/ton/block/MerkleUpdate.kt index 1f7f8e45..dfc952fa 100644 --- a/ton-kotlin-block/src/commonMain/kotlin/org/ton/block/MerkleUpdate.kt +++ b/ton-kotlin-block/src/commonMain/kotlin/org/ton/block/MerkleUpdate.kt @@ -6,46 +6,45 @@ import org.ton.bitstring.BitString import org.ton.cell.CellBuilder import org.ton.cell.CellSlice import org.ton.cell.invoke -import org.ton.tlb.CellRef -import org.ton.tlb.TlbCodec -import org.ton.tlb.TlbConstructor -import org.ton.tlb.asRef +import org.ton.tlb.* import kotlin.jvm.JvmStatic @SerialName("merkle_update") @Serializable -data class MerkleUpdate( - val old_hash: BitString, - val new_hash: BitString, +public data class MerkleUpdate( + val oldHash: BitString, + val newHash: BitString, val old: CellRef, val new: CellRef ) { init { - require(old_hash.size == 256) { "required: old_hash.size = 256, actual: ${old_hash.size}" } - require(new_hash.size == 256) { "required: new_hash.size = 256, actual: ${new_hash.size}" } + require(oldHash.size == 256) { "required: old_hash.size = 256, actual: ${oldHash.size}" } + require(newHash.size == 256) { "required: new_hash.size = 256, actual: ${newHash.size}" } } - companion object { + public companion object { @JvmStatic - fun tlbCodec( + public fun tlbCodec( x: TlbCodec ): TlbCodec> = MerkleUpdateTlbConstructor(x) } } private class MerkleUpdateTlbConstructor( - val x: TlbCodec + x: TlbCodec ) : TlbConstructor>( schema = "!merkle_update#02 {X:Type} old_hash:bits256 new_hash:bits256 old:^X new:^X = MERKLE_UPDATE X;" ) { + val xCellRef = CellRef.tlbCodec(x) + override fun storeTlb( cellBuilder: CellBuilder, value: MerkleUpdate ) = cellBuilder { - storeBits(value.old_hash) - storeBits(value.new_hash) - storeRef(value.old.cell) - storeRef(value.new.cell) + storeBits(value.oldHash) + storeBits(value.newHash) + storeTlb(xCellRef, value.old) + storeTlb(xCellRef, value.new) } override fun loadTlb( @@ -53,8 +52,8 @@ private class MerkleUpdateTlbConstructor( ): MerkleUpdate = cellSlice { val oldHash = loadBits(256) val newHash = loadBits(256) - val old = loadRef().asRef(x) - val new = loadRef().asRef(x) + val old = loadTlb(xCellRef) + val new = loadTlb(xCellRef) // TODO: hash check MerkleUpdate(oldHash, newHash, old, new) } diff --git a/ton-kotlin-block/src/commonMain/kotlin/org/ton/block/PrevBlksInfo.kt b/ton-kotlin-block/src/commonMain/kotlin/org/ton/block/PrevBlksInfo.kt index ce7af811..e3b42df2 100644 --- a/ton-kotlin-block/src/commonMain/kotlin/org/ton/block/PrevBlksInfo.kt +++ b/ton-kotlin-block/src/commonMain/kotlin/org/ton/block/PrevBlksInfo.kt @@ -5,38 +5,41 @@ import kotlinx.serialization.Serializable import org.ton.cell.* import org.ton.tlb.CellRef import org.ton.tlb.TlbConstructor +import org.ton.tlb.loadTlb +import org.ton.tlb.storeTlb import kotlin.jvm.JvmStatic @Serializable @SerialName("prev_blks_info") -data class PrevBlksInfo( +public data class PrevBlksInfo( val prev1: CellRef, val prev2: CellRef ) : BlkPrevInfo { override fun prevs(): List = listOf(prev1.value, prev2.value) - companion object { + public companion object { @JvmStatic - fun tlbCodec(): TlbConstructor = PrevBlksInfoTlbConstructor + public fun tlbCodec(): TlbConstructor = PrevBlksInfoTlbConstructor } } private object PrevBlksInfoTlbConstructor : TlbConstructor( schema = "prev_blks_info\$_ prev1:^ExtBlkRef prev2:^ExtBlkRef = BlkPrevInfo 1;" ) { + private val cellRef = CellRef.tlbCodec(ExtBlkRef) override fun storeTlb( cellBuilder: CellBuilder, value: PrevBlksInfo ) = cellBuilder { - storeRef(value.prev1.cell) - storeRef(value.prev2.cell) + storeTlb(cellRef, value.prev1) + storeTlb(cellRef, value.prev2) } override fun loadTlb( cellSlice: CellSlice ): PrevBlksInfo = cellSlice { - val prev1 = CellRef(loadRef(), ExtBlkRef) - val prev2 = CellRef(loadRef(), ExtBlkRef) + val prev1 = loadTlb(cellRef) + val prev2 = loadTlb(cellRef) PrevBlksInfo(prev1, prev2) } } diff --git a/ton-kotlin-cell/src/commonMain/kotlin/org/ton/cell/CellSlice.kt b/ton-kotlin-cell/src/commonMain/kotlin/org/ton/cell/CellSlice.kt index 8cac568a..ff70899f 100644 --- a/ton-kotlin-cell/src/commonMain/kotlin/org/ton/cell/CellSlice.kt +++ b/ton-kotlin-cell/src/commonMain/kotlin/org/ton/cell/CellSlice.kt @@ -93,7 +93,7 @@ private open class CellSliceImpl( override var refsPosition: Int = 0 ) : CellSlice { override fun endParse() = - check(bitsPosition == bits.size) { "bitsPosition: $bitsPosition != bits.length: ${bits.size}" } + check(bitsPosition >= bits.size) { "bitsPosition: $bitsPosition != bits.length: ${bits.size}" } override fun loadRef(): Cell { checkRefsOverflow() diff --git a/ton-kotlin-hashmap/src/commonMain/kotlin/org/ton/hashmap/HashMapEdge.kt b/ton-kotlin-hashmap/src/commonMain/kotlin/org/ton/hashmap/HashMapEdge.kt index 27369263..7b85b9a3 100644 --- a/ton-kotlin-hashmap/src/commonMain/kotlin/org/ton/hashmap/HashMapEdge.kt +++ b/ton-kotlin-hashmap/src/commonMain/kotlin/org/ton/hashmap/HashMapEdge.kt @@ -10,7 +10,7 @@ import kotlin.jvm.JvmStatic @Serializable @SerialName("hm_edge") -public data class HashMapEdge( +public data class HashMapEdge( val label: HashMapLabel, val node: HashMapNode ) : Iterable> { @@ -23,10 +23,10 @@ public data class HashMapEdge( is HashMapNodeLeaf -> sequenceOf(BitString.empty() to node.value) is HashMapNodeFork -> { // Note: left and right branches implicitly contain prefixes '0' and '1' respectively - val left = node.leftCellRef.value.nodes().map { (label, value) -> + val left = node.left.nodes().map { (label, value) -> (BitString(false) + label) to value } - val right = node.rightCellRef.value.nodes().map { (label, value) -> + val right = node.right.nodes().map { (label, value) -> (BitString(true) + label) to value } left + right diff --git a/ton-kotlin-hashmap/src/commonMain/kotlin/org/ton/hashmap/HashMapNodeFork.kt b/ton-kotlin-hashmap/src/commonMain/kotlin/org/ton/hashmap/HashMapNodeFork.kt index 10ca3294..b4e5fe61 100644 --- a/ton-kotlin-hashmap/src/commonMain/kotlin/org/ton/hashmap/HashMapNodeFork.kt +++ b/ton-kotlin-hashmap/src/commonMain/kotlin/org/ton/hashmap/HashMapNodeFork.kt @@ -24,10 +24,9 @@ public data class HashMapNodeFork( public constructor( left: HashMapEdge, right: HashMapEdge, - codec: TlbCodec> ) : this( - leftCellRef = CellRef(left, codec), - rightCellRef = CellRef(right, codec) + leftCellRef = CellRef(left), + rightCellRef = CellRef(right) ) val left: HashMapEdge by leftCellRef diff --git a/ton-kotlin-hashmap/src/commonMain/kotlin/org/ton/hashmap/RootHashMapE.kt b/ton-kotlin-hashmap/src/commonMain/kotlin/org/ton/hashmap/RootHashMapE.kt index 741166d3..64edca06 100644 --- a/ton-kotlin-hashmap/src/commonMain/kotlin/org/ton/hashmap/RootHashMapE.kt +++ b/ton-kotlin-hashmap/src/commonMain/kotlin/org/ton/hashmap/RootHashMapE.kt @@ -15,7 +15,7 @@ public data class RootHashMapE( val rootCellRef: CellRef> ) : HashMapE { public constructor(root: Cell, tlbCodec: TlbCodec>) : this(CellRef(root, tlbCodec)) - public constructor(root: HashMapEdge, tlbCodec: TlbCodec>) : this(CellRef(root, tlbCodec)) + public constructor(root: HashMapEdge) : this(CellRef(root)) val root: HashMapEdge by rootCellRef diff --git a/ton-kotlin-hashmap/src/commonTest/kotlin/org/ton/hashmap/HashMapEdgeTest.kt b/ton-kotlin-hashmap/src/commonTest/kotlin/org/ton/hashmap/HashMapEdgeTest.kt index 7f9cd628..5d0ecd61 100644 --- a/ton-kotlin-hashmap/src/commonTest/kotlin/org/ton/hashmap/HashMapEdgeTest.kt +++ b/ton-kotlin-hashmap/src/commonTest/kotlin/org/ton/hashmap/HashMapEdgeTest.kt @@ -31,27 +31,27 @@ class HashMapEdgeTest { val e = HashMapEdge( label = HashMapLabelShort(UnaryZero, BitString()), node = HashMapNodeFork( - leftCellRef = HashMapEdge( + left = HashMapEdge( label = HashMapLabelLong( 255, BitString("C20BAD98ED5E80064BD29AB119CA237CB7FB76E7686FB8A3D948722FAF487C7B_") ), node = HashMapNodeLeaf(Cell.of("69696969")) ), - rightCellRef = HashMapEdge( + right = HashMapEdge( label = HashMapLabelShort(UnaryZero, BitString()), node = HashMapNodeFork( - leftCellRef = HashMapEdge( + left = HashMapEdge( label = HashMapLabelShort(UnaryZero, BitString()), node = HashMapNodeFork( - leftCellRef = HashMapEdge( + left = HashMapEdge( label = HashMapLabelLong( 253, BitString("151A9BFF86DE73F761AEB4F6E1D0C4F7378BEC179A9D2A9FCD54B6585F1E744C_") ), node = HashMapNodeLeaf(Cell.of("42424242")) ), - rightCellRef = HashMapEdge( + right = HashMapEdge( label = HashMapLabelLong( 253, BitString("BB53E50A9E12338B2C19ADDE844A31A87FE310FD0E28B7389184AEA7FEAE2C0C_") @@ -60,7 +60,7 @@ class HashMapEdgeTest { ) ) ), - rightCellRef = HashMapEdge( + right = HashMapEdge( label = HashMapLabelLong( 254, BitString("2411BDE8DEB43A9F3B9CCD56613E950A260BE2CDF23DEF3B247DEB1C69F34412_") diff --git a/ton-kotlin-liteapi/src/commonMain/kotlin/org/ton/lite/api/liteserver/functions/LiteServerSendMessage.kt b/ton-kotlin-liteapi/src/commonMain/kotlin/org/ton/lite/api/liteserver/functions/LiteServerSendMessage.kt index 3c56c88c..19c3bb30 100644 --- a/ton-kotlin-liteapi/src/commonMain/kotlin/org/ton/lite/api/liteserver/functions/LiteServerSendMessage.kt +++ b/ton-kotlin-liteapi/src/commonMain/kotlin/org/ton/lite/api/liteserver/functions/LiteServerSendMessage.kt @@ -16,8 +16,8 @@ import org.ton.tlb.constructor.AnyTlbConstructor public data class LiteServerSendMessage( val body: BagOfCells ) { - public constructor(body: CellRef>) : this(BagOfCells(body.cell)) - public constructor(body: Message) : this(CellRef(body, Message.tlbCodec(AnyTlbConstructor))) + public constructor(body: CellRef>) : this(BagOfCells(body.toCell(Message.tlbCodec(AnyTlbConstructor)))) + public constructor(body: Message) : this(CellRef(body)) public fun parseBody(): CellRef> = CellRef(body.first(), Message.tlbCodec(AnyTlbConstructor)) diff --git a/ton-kotlin-tlb/src/commonMain/kotlin/org/ton/tlb/CellRef.kt b/ton-kotlin-tlb/src/commonMain/kotlin/org/ton/tlb/CellRef.kt index fb96f2b2..69d507b4 100644 --- a/ton-kotlin-tlb/src/commonMain/kotlin/org/ton/tlb/CellRef.kt +++ b/ton-kotlin-tlb/src/commonMain/kotlin/org/ton/tlb/CellRef.kt @@ -6,15 +6,15 @@ import org.ton.cell.CellSlice import kotlin.jvm.JvmStatic public inline fun CellRef(cell: Cell, codec: TlbCodec): CellRef = CellRef.valueOf(cell, codec) -public inline fun CellRef(value: T, codec: TlbCodec): CellRef = CellRef.valueOf(value, codec) +public inline fun CellRef(value: T): CellRef = CellRef.valueOf(value) public inline fun Cell.asRef(codec: TlbCodec): CellRef = CellRef.valueOf(this, codec) public interface CellRef { - public val cell: Cell - public val codec: TlbCodec public val value: T + public fun toCell(codec: TlbCodec? = null): Cell + public operator fun getValue(thisRef: Any?, property: Any?): T = value public companion object { @@ -22,7 +22,7 @@ public interface CellRef { public fun valueOf(cell: Cell, codec: TlbCodec): CellRef = CellRefImpl(cell, codec) @JvmStatic - public fun valueOf(value: T, codec: TlbCodec): CellRef = CellRefValue(value, codec) + public fun valueOf(value: T): CellRef = CellRefValue(value) @JvmStatic public fun tlbCodec(codec: TlbCodec): TlbCodec> = CellRefTlbConstructor(codec) @@ -30,8 +30,8 @@ public interface CellRef { } private class CellRefImpl( - override val cell: Cell, - override val codec: TlbCodec + val cell: Cell, + val codec: TlbCodec ) : CellRef { override val value: T by lazy(LazyThreadSafetyMode.PUBLICATION) { cell.parse { @@ -39,16 +39,20 @@ private class CellRefImpl( } } + override fun toCell(codec: TlbCodec?): Cell { + return cell + } + override fun toString(): String = "CellRef($cell)" } private class CellRefValue( override val value: T, - override val codec: TlbCodec ) : CellRef { - override val cell: Cell by lazy(LazyThreadSafetyMode.PUBLICATION) { - CellBuilder.createCell { - storeTlb(codec, value) + override fun toCell(codec: TlbCodec?): Cell { + require(codec != null) { "Codec is not specified" } + return CellBuilder.createCell { + codec.storeTlb(this, value) } } @@ -59,7 +63,7 @@ private class CellRefTlbConstructor( val codec: TlbCodec ) : TlbCodec> { override fun storeTlb(cellBuilder: CellBuilder, value: CellRef) { - cellBuilder.storeRef(value.cell) + cellBuilder.storeRef(value.toCell(codec)) } override fun loadTlb(cellSlice: CellSlice): CellRef {