<commit_before>package scorex.consensus.qora

import java.util

import com.google.common.primitives.{Bytes, Longs}
import play.api.libs.json.Json
import scorex.block.{Block, QoraGenesisBlockGenerationData}
import scorex.consensus.BlockGenerationData
import scorex.crypto.{Base58, Crypto}
import scorex.database.blockchain.PrunableBlockchainStorage


class QoraBlockGenerationData(val generatingBalance: Long, val generatorSignature: Array[Byte])
  extends BlockGenerationData {

  import QoraBlockGenerationDataParser._

  require(generatingBalance > 0)

  override def toBytes: Array[Byte] = Bytes.concat(
    Longs.toByteArray(generatingBalance),
    generatorSignature
  ).ensuring(_.length == GENERATION_DATA_LENGTH)

  override def toJson = Json.obj(
    "generatingBalance" -> generatingBalance,
    "generatorSignature" -> Base58.encode(generatorSignature)
  )

  override def signature() = generatorSignature

  override def isGenesis = QoraGenesisBlockGenerationData.generatorSignature.sameElements(generatorSignature)

  override def isValid(block: Block): Boolean = {
    if (generatingBalance != QoraBlockGenerationFunctions.getNextBlockGeneratingBalance(block.parent().get)) {
      //CHECK IF GENERATING BALANCE IS CORRECT
      false
    } else {
      //CREATE TARGET
      val targetBytes = Array.fill(32)(Byte.MaxValue)

      //DIVIDE TARGET BY BASE TARGET
      val baseTarget = BigInt(QoraBlockGenerationFunctions.getBaseTarget(generatingBalance))
      val genBalance = PrunableBlockchainStorage.generationBalance(block.generator.address).toBigInt()
      val target0 = BigInt(1, targetBytes) / baseTarget * genBalance

      //MULTIPLE TARGET BY GUESSES
      val guesses = (block.timestamp - block.parent().get.timestamp) / 1000
      val lowerTarget = target0 * BigInt(guesses - 1)
      val target = target0 * BigInt(guesses)

      //CONVERT HIT TO BIGINT
      val hit = BigInt(1, Crypto.sha256(generatorSignature))

      hit > lowerTarget && hit <= target
    }
  }

  override def isSignatureValid(block: Block): Boolean = {<commit_msg>better comments, target bounds fix<commit_after>package scorex.consensus.qora

import java.util

import com.google.common.primitives.{Bytes, Longs}
import play.api.libs.json.Json
import scorex.block.{Block, QoraGenesisBlockGenerationData}
import scorex.consensus.BlockGenerationData
import scorex.crypto.{Base58, Crypto}
import scorex.database.blockchain.PrunableBlockchainStorage


class QoraBlockGenerationData(val generatingBalance: Long, val generatorSignature: Array[Byte])
  extends BlockGenerationData {

  import QoraBlockGenerationDataParser._

  require(generatingBalance > 0)

  override def toBytes: Array[Byte] = Bytes.concat(
    Longs.toByteArray(generatingBalance),
    generatorSignature
  ).ensuring(_.length == GENERATION_DATA_LENGTH)

  override def toJson = Json.obj(
    "generatingBalance" -> generatingBalance,
    "generatorSignature" -> Base58.encode(generatorSignature)
  )

  override def signature() = generatorSignature

  override def isGenesis = QoraGenesisBlockGenerationData.generatorSignature.sameElements(generatorSignature)

  override def isValid(block: Block): Boolean = {
    if (generatingBalance != QoraBlockGenerationFunctions.getNextBlockGeneratingBalance(block.parent().get)) {
      //CHECK IF GENERATING BALANCE IS CORRECT
      false
    } else {
      //target base
      val targetBytes = Array.fill(32)(Byte.MaxValue)
      val baseTarget = BigInt(QoraBlockGenerationFunctions.getBaseTarget(generatingBalance))
      val genBalance = PrunableBlockchainStorage.generationBalance(block.generator.address).toBigInt()
      val target0 = BigInt(1, targetBytes) / baseTarget * genBalance

      //target bounds
      val guesses = (block.timestamp - block.parent().get.timestamp) / 1000
      val lowerTarget = target0 * BigInt(guesses - 1)
      val target = target0 * BigInt(guesses)

      val hit = BigInt(1, Crypto.sha256(generatorSignature))

      //generation check
      hit >= lowerTarget && hit < target
    }
  }

  override def isSignatureValid(block: Block): Boolean = {
{'fraction_non_alphanumeric': 0.08908957773980962, 'fraction_numerical': 0.00976324139614352, 'mean_word_length': 4.413474240422722, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1373106', 'n_tokens_mistral': 1204, 'n_tokens_neox': 1154, 'n_words': 255}