Skip to content

Commit 29b1b1d

Browse files
authored
0.4.3 (#130)
1 parent b306a73 commit 29b1b1d

File tree

18 files changed

+894
-31
lines changed

18 files changed

+894
-31
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,28 @@
1010

1111
### Core components
1212

13-
* `org.ton.kotlin:ton-kotlin-tvm:0.4.2` - TVM Primitives (Cells, BOC, etc.)
14-
* `org.ton.kotlin:ton-kotlin-crypto:0.4.2` - Crypto primitives for TON (ED25519, SHA, etc.)
15-
* `org.ton.kotlin:ton-kotlin-adnl:0.4.2` - ADNL (Abstract Datagram Network Layer) TON Network implementation
13+
* `org.ton.kotlin:ton-kotlin-tvm:0.4.3` - TVM Primitives (Cells, BOC, etc.)
14+
* `org.ton.kotlin:ton-kotlin-crypto:0.4.3` - Crypto primitives for TON (ED25519, SHA, etc.)
15+
* `org.ton.kotlin:ton-kotlin-adnl:0.4.3` - ADNL (Abstract Datagram Network Layer) TON Network implementation
1616

1717
### API Interfaces
1818

19-
* `org.ton.kotlin:ton-kotlin-contract:0.4.2` - Smart-contracts API interface
20-
* `org.ton.kotlin:ton-kotlin-liteclient:0.4.2` - Lite-client API implementation
19+
* `org.ton.kotlin:ton-kotlin-contract:0.4.3` - Smart-contracts API interface
20+
* `org.ton.kotlin:ton-kotlin-liteclient:0.4.3` - Lite-client API implementation
2121

2222
### TL-B (TL-Binary)
2323

24-
* `org.ton.kotlin:ton-kotlin-tlb:0.4.2` - TON TL-B (TL-Binary) serialization/deserialization
25-
* `org.ton.kotlin:ton-kotlin-block-tlb:0.4.2` - Pre-generated TL-B schemas for TON Blockchain
26-
* `org.ton.kotlin:ton-kotlin-hashmap-tlb:0.4.2` - Pre-generated TL-B schemas for TON Hashmap (also known as Dictionary)
24+
* `org.ton.kotlin:ton-kotlin-tlb:0.4.3` - TON TL-B (TL-Binary) serialization/deserialization
25+
* `org.ton.kotlin:ton-kotlin-block-tlb:0.4.3` - Pre-generated TL-B schemas for TON Blockchain
26+
* `org.ton.kotlin:ton-kotlin-hashmap-tlb:0.4.3` - Pre-generated TL-B schemas for TON Hashmap (also known as Dictionary)
2727

2828
## Documentation
2929

3030
https://github.com/andreypfau/ton-kotlin/wiki/TON-Kotlin-documentation
3131

3232
<!-- Badges -->
3333

34-
[maven-central]: https://central.sonatype.com/artifact/org.ton/ton-kotlin-tvm/0.4.2
34+
[maven-central]: https://central.sonatype.com/artifact/org.ton/ton-kotlin-tvm/0.4.3
3535

3636
[license]: LICENSE
3737

bitstring/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ kotlin {
99
dependencies {
1010
api(projects.tonKotlinCrypto)
1111
implementation(libs.serialization.core)
12+
implementation(libs.kotlinx.io)
1213
}
1314
}
1415
}

bitstring/src/BitString.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
package org.ton.bitstring
44

5+
import kotlinx.io.bytestring.ByteString
56
import kotlinx.serialization.Serializable
67
import kotlin.contracts.ExperimentalContracts
78
import kotlin.contracts.contract
@@ -19,6 +20,8 @@ public inline fun BitString(hex: String): BitString = BitString.parse(hex)
1920
public inline fun Iterable<Boolean>.toBitString(): BitString = BitString(this)
2021
public inline fun BooleanArray.toBitString(): BitString = BitString(*this)
2122
public inline fun ByteArray.toBitString(size: Int = this.size * Byte.SIZE_BITS): BitString = BitString(this, size)
23+
public inline fun ByteString.toBitString(size: Int = this.size * Byte.SIZE_BITS): BitString =
24+
BitString(this.toByteArray(), size)
2225

2326
@Serializable(with = FiftHexBitStringSerializer::class)
2427
public interface BitString : Iterable<Boolean>, Comparable<BitString> {

bitstring/src/ByteBackedBitString.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,15 @@ public open class ByteBackedBitString protected constructor(
4545
if (augment && (size % 8 != 0)) {
4646
appendAugmentTag(bytes, size)
4747
} else {
48-
bytes.copyOf()
48+
bytes.copyOf((size + 7) ushr 3)
4949
}
5050

5151
override fun toBooleanArray(): BooleanArray = toList().toBooleanArray()
5252

53-
override fun toMutableBitString(): MutableBitString = ByteBackedMutableBitString.of(bytes.copyOf(), size)
53+
override fun toMutableBitString(): MutableBitString =
54+
ByteBackedMutableBitString.of(bytes.copyOf((size + 7) ushr 3), size)
5455

55-
override fun toBitString(): BitString = ByteBackedBitString(size, bytes.copyOf())
56+
override fun toBitString(): BitString = ByteBackedBitString(size, bytes.copyOf((size + 7) ushr 3))
5657

5758
override fun iterator(): Iterator<Boolean> = BitStringIterator(this)
5859

block-tlb/src/AddrStd.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.ton.block
22

3+
import kotlinx.io.bytestring.ByteString
34
import kotlinx.serialization.SerialName
45
import org.ton.bitstring.BitString
56
import org.ton.bitstring.toBitString
@@ -33,6 +34,7 @@ public data class AddrStd(
3334
public constructor() : this(0, BitString(256))
3435
public constructor(workchainId: Int, address: BitString) : this(null, workchainId, address)
3536
public constructor(workchainId: Int, address: ByteArray) : this(null, workchainId, address)
37+
public constructor(workchainId: Int, address: ByteString) : this(null, workchainId, address.toByteArray())
3638
public constructor(anycast: Anycast?, workchainId: Int, address: ByteArray) : this(
3739
anycast.toMaybe(),
3840
workchainId,
@@ -45,6 +47,12 @@ public data class AddrStd(
4547
address.toBitString()
4648
)
4749

50+
public constructor(anycast: Anycast?, workchainId: Int, address: ByteString) : this(
51+
anycast.toMaybe(),
52+
workchainId,
53+
address.toBitString()
54+
)
55+
4856
init {
4957
require(address.size == 256) { "expected address.size == 256, actual: ${address.size}" }
5058
}

block-tlb/src/StorageUsedShort.kt

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,34 @@ package org.ton.block
33
import org.ton.cell.CellBuilder
44
import org.ton.cell.CellSlice
55
import org.ton.cell.invoke
6-
import org.ton.tlb.*
6+
import org.ton.kotlin.cell.CellSize
77
import org.ton.tlb.TlbConstructor
8+
import org.ton.tlb.loadTlb
89
import org.ton.tlb.providers.TlbConstructorProvider
10+
import org.ton.tlb.storeTlb
911

12+
/**
13+
* Cell tree storage stats.
14+
*/
1015
public data class StorageUsedShort(
11-
val cells: Long,
12-
val bits: Long
13-
) : TlbObject {
14-
override fun print(printer: TlbPrettyPrinter): TlbPrettyPrinter {
15-
return printer {
16-
type("storage_used_short") {
17-
field("cells", cells)
18-
field("bits", bits)
19-
}
20-
}
16+
/**
17+
* Total number of cells in tree.
18+
*/
19+
val cellCount: Long,
20+
21+
/**
22+
* Total number of bits in tree.
23+
*/
24+
val bitCount: Long
25+
) {
26+
public constructor(cellSize: CellSize) : this(cellSize.minRefs.toLong(), cellSize.minBits.toLong()) {
27+
require(cellSize.isFixed()) { "Cell size must be fixed" }
2128
}
2229

23-
override fun toString(): String = print().toString()
30+
public fun toCellSize(): CellSize = CellSize(bitCount.toInt(), cellCount.toInt())
31+
32+
public operator fun plus(other: StorageUsedShort): StorageUsedShort =
33+
StorageUsedShort(cellCount + other.cellCount, bitCount + other.bitCount)
2434

2535
public companion object : TlbConstructorProvider<StorageUsedShort> by StorageUsedShortTlbConstructor {
2636
public val ZERO: StorageUsedShort = StorageUsedShort(0, 0)
@@ -36,8 +46,8 @@ private object StorageUsedShortTlbConstructor : TlbConstructor<StorageUsedShort>
3646
override fun storeTlb(
3747
cellBuilder: CellBuilder, value: StorageUsedShort
3848
) = cellBuilder {
39-
storeTlb(varUInteger7Codec, VarUInteger(value.cells))
40-
storeTlb(varUInteger7Codec, VarUInteger(value.bits))
49+
storeTlb(varUInteger7Codec, VarUInteger(value.cellCount))
50+
storeTlb(varUInteger7Codec, VarUInteger(value.bitCount))
4151
}
4252

4353
override fun loadTlb(

block-tlb/src/account/Account.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import org.ton.tlb.TlbCodec
1010

1111
/**
1212
* Existing account data.
13+
*
14+
* @see [ShardAccount]
1315
*/
1416
public data class Account(
1517
/**
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
@file:Suppress("PackageDirectoryMismatch")
2+
3+
package org.ton.kotlin.config
4+
5+
import org.ton.bigint.div
6+
import org.ton.bigint.times
7+
import org.ton.bigint.toBigInt
8+
import org.ton.block.AddrStd
9+
import org.ton.block.Coins
10+
import org.ton.cell.CellBuilder
11+
import org.ton.cell.CellSlice
12+
import org.ton.kotlin.cell.CellContext
13+
import org.ton.tlb.TlbCodec
14+
15+
public data class BurningConfig(
16+
val blackholeAddress: AddrStd?,
17+
val feeBurnNum: Int,
18+
val feeBurnDenom: Int
19+
) {
20+
init {
21+
require(feeBurnDenom >= 1) { "feeBurnDenom must be at least 1, actual: $feeBurnDenom" }
22+
require(feeBurnNum in 0..feeBurnDenom) { "feeBurnNum must be in 0..$feeBurnDenom, actual: $feeBurnNum" }
23+
}
24+
25+
public fun calculateBurnedFees(value: Coins): Coins {
26+
if (value == Coins.ZERO) return value
27+
return Coins(value.amount.value.times(feeBurnNum.toBigInt()).div(feeBurnDenom.toBigInt()))
28+
}
29+
30+
public companion object : TlbCodec<BurningConfig> by BurningConfigTlbCodec
31+
}
32+
33+
private object BurningConfigTlbCodec : TlbCodec<BurningConfig> {
34+
override fun loadTlb(slice: CellSlice, context: CellContext): BurningConfig {
35+
val tag = slice.loadUInt(8).toInt()
36+
require(tag == 0x01) { "Invalid BurningConfig tag: ${tag.toHexString()}" }
37+
val blackholeAddress = if (slice.loadBoolean()) AddrStd(-1, slice.loadByteArray(32)) else null
38+
val feeBurnNum = slice.loadUInt(32).toInt()
39+
val feeBurnDenom = slice.loadUInt(32).toInt()
40+
return BurningConfig(blackholeAddress, feeBurnNum, feeBurnDenom)
41+
}
42+
43+
override fun storeTlb(builder: CellBuilder, value: BurningConfig) {
44+
builder.storeUInt(0x01, 8)
45+
if (value.blackholeAddress != null) {
46+
builder.storeBoolean(true)
47+
builder.storeBitString(value.blackholeAddress.address)
48+
} else {
49+
builder.storeBoolean(false)
50+
}
51+
builder.storeUInt(value.feeBurnNum, 32)
52+
builder.storeUInt(value.feeBurnDenom, 32)
53+
}
54+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package org.ton.block.config
2+
3+
import org.ton.bigint.*
4+
import org.ton.block.Coins
5+
import org.ton.block.StorageUsedShort
6+
import org.ton.cell.CellBuilder
7+
import org.ton.cell.CellSlice
8+
import org.ton.kotlin.cell.CellContext
9+
import org.ton.tlb.TlbCodec
10+
11+
/**
12+
* Storage prices for some interval.
13+
*/
14+
public data class StoragePrices(
15+
/**
16+
* Unix timestamp in seconds since which this prices are used.
17+
*/
18+
val validSince: Long,
19+
20+
/**
21+
* Bit price in base workchain.
22+
*/
23+
val bitPrice: Long,
24+
25+
/**
26+
* Cell price in base workchain.
27+
*/
28+
val cellPrice: Long,
29+
30+
/**
31+
* Bit price in masterchain.
32+
*/
33+
val mcBitPrice: Long,
34+
35+
/**
36+
* Cell price in masterchain.
37+
*/
38+
val mcCellPrice: Long,
39+
) {
40+
/**
41+
* Computes the amount of fees for storing [stats] data for [delta] seconds.
42+
*/
43+
public fun computeStorageFee(
44+
isMasterchain: Boolean,
45+
delta: Long,
46+
stats: StorageUsedShort
47+
): Coins {
48+
var result = if (isMasterchain) {
49+
(stats.cellCount.toBigInt().times(mcCellPrice.toBigInt())).plus(
50+
(stats.bitCount.toBigInt().times(mcBitPrice.toBigInt()))
51+
)
52+
} else {
53+
(stats.cellCount.toBigInt().times(cellPrice.toBigInt())).plus(
54+
(stats.bitCount.toBigInt().times(bitPrice.toBigInt()))
55+
)
56+
}
57+
result = result.times(delta.toBigInt())
58+
val r = result.and(0xFFFF.toBigInt()).toLong() != 0L
59+
result = result.shr(16)
60+
if (r) {
61+
result = result.plus(1.toBigInt())
62+
}
63+
return Coins(result)
64+
}
65+
66+
public companion object : TlbCodec<StoragePrices> by StoragePricesTlbCodec
67+
}
68+
69+
private object StoragePricesTlbCodec : TlbCodec<StoragePrices> {
70+
override fun loadTlb(slice: CellSlice, context: CellContext): StoragePrices {
71+
val tag = slice.loadUInt(8).toInt()
72+
require(tag == 0xCC) { "Invalid StorageUsedShort tag: ${tag.toHexString()}" }
73+
val validSince = slice.loadUInt(32).toLong()
74+
val bitPrice = slice.loadULong().toLong()
75+
val cellPrice = slice.loadULong().toLong()
76+
val mcBitPrice = slice.loadULong().toLong()
77+
val mcCellPrice = slice.loadULong().toLong()
78+
return StoragePrices(validSince, bitPrice, cellPrice, mcBitPrice, mcCellPrice)
79+
}
80+
81+
override fun storeTlb(builder: CellBuilder, value: StoragePrices, context: CellContext) {
82+
builder.storeUInt(0xCC, 8)
83+
builder.storeUInt(value.validSince, 32)
84+
builder.storeULong(value.bitPrice.toULong(), 64)
85+
builder.storeULong(value.cellPrice.toULong(), 64)
86+
builder.storeULong(value.mcBitPrice.toULong(), 64)
87+
builder.storeULong(value.mcCellPrice.toULong(), 64)
88+
}
89+
}

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ plugins {
1010

1111
allprojects {
1212
group = "org.ton"
13-
version = "0.4.2"
13+
version = "0.4.3"
1414

1515
repositories {
1616
mavenCentral()

0 commit comments

Comments
 (0)