Improve Version to cap parts at 16-bits rather than masking them

This commit is contained in:
bemusementpark 2024-08-03 21:52:01 +09:30
parent 6e1ed8cc11
commit 5417660996
2 changed files with 5 additions and 6 deletions

View File

@ -29,11 +29,10 @@ class SnodeVersionTest(
// Snode.Version only considers the first 4 integers, so these are equal
arrayOf("1.0.0.0", "1.0.0.0.1", true, false),
arrayOf("1.0.0.1", "1.0.0.1", true, false),
arrayOf("12345.12345.12345.12345", "12345.12345.12345.12345", true, false),
arrayOf("11111.11111.11111.11111", "11111.11111.11111.99999", false, true),
arrayOf("11111.11111.11111.11111", "11111.11111.99999.99999", false, true),
arrayOf("11111.11111.11111.11111", "11111.99999.99999.99999", false, true),
arrayOf("11111.11111.11111.11111", "99999.99999.99999.99999", false, true),
// parts can be up to 16 bits, around 65,535
arrayOf("65535.65535.65535.65535", "65535.65535.65535.65535", true, false),
// values higher than this are coerced to 65535 (:
arrayOf("65535.65535.65535.65535", "65535.65535.65535.99999", true, false),
)
}

View File

@ -64,7 +64,7 @@ class Snode(val address: String, val port: Int, val publicKeySet: KeySet?, val v
private const val MASK = 0xFFFFUL
private fun Sequence<ULong>.foldToVersionAsULong() = take(4).foldIndexed(0UL) { i, acc, it ->
it and MASK shl (3 - i) * MASK_BITS or acc
it.coerceAtMost(MASK) shl (3 - i) * MASK_BITS or acc
}
}