mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-08-09 05:32:51 +02:00
Performance improvements on the PoW miner
This commit is contained in:
@@ -37,13 +37,3 @@ fun ByteArray.indexOf(sequence: ByteArray): Int {
|
|||||||
}
|
}
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
fun ByteArray.set(
|
|
||||||
value: ByteArray,
|
|
||||||
startIndex: Int,
|
|
||||||
) {
|
|
||||||
var index = startIndex
|
|
||||||
for (byte in value) {
|
|
||||||
this[index++] = byte
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@@ -32,8 +32,9 @@ class PoWMiner(
|
|||||||
val desiredPoW: Int,
|
val desiredPoW: Int,
|
||||||
) {
|
) {
|
||||||
val hasher = Sha256Hasher()
|
val hasher = Sha256Hasher()
|
||||||
|
val emptyBytesForDesiredPoW = desiredPoW / 8
|
||||||
|
|
||||||
fun rank(byteArray: ByteArray) = PoWRankEvaluator.calculatePowRankOf(hasher.hash(byteArray))
|
fun reachedDesiredPoW(byteArray: ByteArray) = PoWRankEvaluator.atLeastPowRank(hasher.hash(byteArray), desiredPoW, emptyBytesForDesiredPoW)
|
||||||
|
|
||||||
fun run() = runDigit(buffer.nonceStarts)
|
fun run() = runDigit(buffer.nonceStarts)
|
||||||
|
|
||||||
@@ -42,15 +43,17 @@ class PoWMiner(
|
|||||||
// replaces the background base by the nonce integers
|
// replaces the background base by the nonce integers
|
||||||
buffer.bytes[index] = testByte
|
buffer.bytes[index] = testByte
|
||||||
|
|
||||||
if (rank(buffer.bytes) >= desiredPoW) return true
|
if (index + 1 < buffer.nonceEnds) {
|
||||||
|
if (runDigit(index + 1)) return true
|
||||||
if (index + 1 < buffer.nonceEnds && runDigit(index + 1)) return true
|
} else {
|
||||||
|
if (reachedDesiredPoW(buffer.bytes)) return true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private val STARTING_NONCE_SIZE = 5
|
private const val STARTING_NONCE_SIZE = 5
|
||||||
|
|
||||||
// make sure these chars are not escaped by the JSON stringifier
|
// make sure these chars are not escaped by the JSON stringifier
|
||||||
private val VALID_CHARS: List<Char> =
|
private val VALID_CHARS: List<Char> =
|
||||||
|
@@ -103,5 +103,18 @@ class PoWRankEvaluator {
|
|||||||
}
|
}
|
||||||
return rank
|
return rank
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
|
fun atLeastPowRank(
|
||||||
|
id: ByteArray,
|
||||||
|
minPoW: Int,
|
||||||
|
emptyBytes: Int,
|
||||||
|
): Boolean {
|
||||||
|
for (index in 0 until emptyBytes) {
|
||||||
|
if (id[index] != R8) return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return calculatePowRankOf(id) >= minPoW
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user