ton-community / ton-kotlin

Kotlin/Multiplatform SDK for The Open Network
Apache License 2.0
113 stars 22 forks source link

`org.ton.block.VarUInteger` addition always throws an exception #102

Open mcnckc opened 1 year ago

mcnckc commented 1 year ago

On almost any addition (say VarUInteger(1)+VarUInteger(1)) an exception is thrown from plus in line 45

https://github.com/andreypfau/ton-kotlin/blob/b1edc4b134e89ccf252149f27c85fd530377cebe/ton-kotlin-block/src/commonMain/kotlin/org/ton/block/VarUInteger.kt#L41-L47

This happens, because VarUInteger, when constructed, holds number's length in bytes: https://github.com/andreypfau/ton-kotlin/blob/b1edc4b134e89ccf252149f27c85fd530377cebe/ton-kotlin-block/src/commonMain/kotlin/org/ton/block/VarUInteger.kt#L26-L31

Therefore, in line 45, maxLen contains number of bytes, but actualLen is clearly number of bits, and very often number of bits is larger, causing this exception. It is also related to other mathematical operators with VarUInteger Moreover, to me it seems that bit length anyway can increase when adding two numbers of the same bit length

andreypfau commented 11 months ago

arithmetic operators will be removed in the next release due separation of serialization objects and actual arithmetics, use BigInt instead.