From 9b17889443eace153b6403b5e819dc38b67a80e3 Mon Sep 17 00:00:00 2001 From: Alexander Chepurnoy Date: Thu, 4 Apr 2024 13:45:12 +0300 Subject: [PATCH 1/2] removing hasLength which is always true --- .../difficulty/DifficultySerializer.scala | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/ergo-core/src/main/scala/org/ergoplatform/mining/difficulty/DifficultySerializer.scala b/ergo-core/src/main/scala/org/ergoplatform/mining/difficulty/DifficultySerializer.scala index a4aefe426d..9e587e9ed6 100644 --- a/ergo-core/src/main/scala/org/ergoplatform/mining/difficulty/DifficultySerializer.scala +++ b/ergo-core/src/main/scala/org/ergoplatform/mining/difficulty/DifficultySerializer.scala @@ -40,7 +40,7 @@ object DifficultySerializer extends ErgoSerializer[NBits] { if (size >= 1) bytes(4) = ((compact >> 16) & 0xFF).toByte if (size >= 2) bytes(5) = ((compact >> 8) & 0xFF).toByte if (size >= 3) bytes(6) = (compact & 0xFF).toByte - decodeMPI(bytes, hasLength = true) + decodeMPI(bytes) } /** @@ -78,19 +78,15 @@ object DifficultySerializer extends ErgoSerializer[NBits] { * MPI encoded numbers are produced by the OpenSSL BN_bn2mpi function. They consist of * a 4 byte big endian length field, followed by the stated number of bytes representing * the number in big endian format (with a sign bit). - * - * @param hasLength can be set to false if the given array is missing the 4 byte length field */ @SuppressWarnings(Array("NullAssignment")) - private def decodeMPI(mpi: Array[Byte], hasLength: Boolean): BigInteger = { + private def decodeMPI(mpi: Array[Byte]): BigInteger = { var buf: Array[Byte] = null // scalastyle:ignore - if (hasLength) { - val length: Int = readUint32BE(mpi).toInt - buf = new Array[Byte](length) - System.arraycopy(mpi, 4, buf, 0, length) - } else { - buf = mpi - } + + val length: Int = readUint32BE(mpi).toInt + buf = new Array[Byte](length) + System.arraycopy(mpi, 4, buf, 0, length) + if (buf.length == 0) { BigInteger.ZERO } else { From 461c7b3ac55f90183d56a558468ecfb9a688651a Mon Sep 17 00:00:00 2001 From: Alexander Chepurnoy Date: Thu, 4 Apr 2024 13:46:15 +0300 Subject: [PATCH 2/2] var buf = null eliminated --- .../ergoplatform/mining/difficulty/DifficultySerializer.scala | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/ergo-core/src/main/scala/org/ergoplatform/mining/difficulty/DifficultySerializer.scala b/ergo-core/src/main/scala/org/ergoplatform/mining/difficulty/DifficultySerializer.scala index 9e587e9ed6..3c42fc6303 100644 --- a/ergo-core/src/main/scala/org/ergoplatform/mining/difficulty/DifficultySerializer.scala +++ b/ergo-core/src/main/scala/org/ergoplatform/mining/difficulty/DifficultySerializer.scala @@ -79,12 +79,10 @@ object DifficultySerializer extends ErgoSerializer[NBits] { * a 4 byte big endian length field, followed by the stated number of bytes representing * the number in big endian format (with a sign bit). */ - @SuppressWarnings(Array("NullAssignment")) private def decodeMPI(mpi: Array[Byte]): BigInteger = { - var buf: Array[Byte] = null // scalastyle:ignore val length: Int = readUint32BE(mpi).toInt - buf = new Array[Byte](length) + val buf = new Array[Byte](length) System.arraycopy(mpi, 4, buf, 0, length) if (buf.length == 0) {