Created
October 17, 2023 13:39
-
-
Save kushti/f7ff643f4aeb010393caca4e620fb137 to your computer and use it in GitHub Desktop.
NBitsTester.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.common.primitives.{Ints, Longs} | |
import org.ergoplatform.mining.difficulty.DifficultySerializer | |
object NBitsTester extends App { | |
val nb = 118012349L | |
val diff = DifficultySerializer.decodeCompactBits(nb) | |
println(nb.toBinaryString) | |
println(diff) | |
val nb2 = DifficultySerializer.encodeCompactBits(diff / 96) | |
println(nb2) | |
println(nb2.toBinaryString) | |
val diff2 = BigInt.apply(Array[Byte](2, 0, 0, 0)) | |
println("diff2") | |
println(diff2) | |
println(DifficultySerializer.encodeCompactBits(diff2).toBinaryString) | |
println(DifficultySerializer.encodeCompactBits(diff2 / 128).toBinaryString) | |
// 100 00001000 00000000 00000000 | |
// 11 00010000 00000000 00000000 | |
val diffb = DifficultySerializer.decodeCompactBits(386197775L) | |
println(386197775L.toBinaryString) | |
println(DifficultySerializer.encodeCompactBits(diffb / 4).toBinaryString) | |
def compare128(nb1: Long, nb2: Long): Boolean = { | |
val nBytes1 = Longs.toByteArray(nb1).takeRight(4) | |
val nBytes2 = Longs.toByteArray(nb2).takeRight(4) | |
println(nBytes1.mkString(",")) | |
val exp1 = nBytes1.head | |
val exp2 = nBytes2.head | |
val m1 = Ints.fromByteArray(Array[Byte](0) ++ nBytes1.tail) | |
val m2 = Ints.fromByteArray(Array[Byte](0) ++ nBytes2.tail) | |
println(exp1) | |
println(exp2) | |
println(m1) | |
println(m2) | |
(exp1 == exp2 && m1 < 128 * m2) || ((exp1 - 1 == exp2) && (2 * m1 < m2)) | |
} | |
def nbRecalc(nb: Long, factor: Int) = { | |
val diff = DifficultySerializer.decodeCompactBits(nb) | |
val diff2 = diff / factor | |
DifficultySerializer.encodeCompactBits(diff2) | |
} | |
println(compare128(nb, nbRecalc(nb, 1))) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment