当前位置: 首页>>代码示例>>Scala>>正文


Scala Ints类代码示例

本文整理汇总了Scala中com.google.common.primitives.Ints的典型用法代码示例。如果您正苦于以下问题:Scala Ints类的具体用法?Scala Ints怎么用?Scala Ints使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Ints类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。

示例1: validate

//设置package包名称以及导入依赖的类
package org.ergoplatform.nodeView.history.storage.modifierprocessors.popow

import com.google.common.primitives.Ints
import io.iohk.iodb.ByteArrayWrapper
import org.ergoplatform.modifiers.ErgoPersistentModifier
import org.ergoplatform.modifiers.history.{HistoryModifierSerializer, PoPoWProof}
import org.ergoplatform.nodeView.history.storage.modifierprocessors.HeadersProcessor
import org.ergoplatform.settings.Constants
import scorex.core.consensus.History.ProgressInfo

import scala.util.{Failure, Success, Try}

trait FullPoPoWProofsProcessor extends PoPoWProofsProcessor with HeadersProcessor {

  def validate(m: PoPoWProof): Try[Unit] = m.validate.map { _ =>
    //TODO what if we trying to apply better popow proof?
    //TODO validate difficulty for suffix
    if (height > 1) Failure(new Error("Trying to apply PoPoW proof to nonempty history"))
    else Success()
  }

  def process(m: PoPoWProof): ProgressInfo[ErgoPersistentModifier] = {
    val headers = m.innerchain ++ m.suffix
    val bestHeader = m.suffix.last
    val headersRows: Seq[(ByteArrayWrapper, ByteArrayWrapper)] = headers.zipWithIndex.flatMap { case (h, i) =>
      //TODO howto?
      val requiredDifficulty: BigInt = Constants.InitialDifficulty
      Seq((ByteArrayWrapper(h.id), ByteArrayWrapper(HistoryModifierSerializer.toBytes(h))),
        //TODO howto?
        (headerHeightKey(h.id), ByteArrayWrapper(Ints.toByteArray(2 + i))),
        //TODO howto?
        (headerScoreKey(h.id), ByteArrayWrapper((requiredDifficulty * (1 + i)).toByteArray)),
        (headerDiffKey(h.id), ByteArrayWrapper(requiredDifficulty.toByteArray)))
    }
    val bestHeaderRow = (BestHeaderKey, ByteArrayWrapper(bestHeader.id))
    historyStorage.insert(bestHeader.id, bestHeaderRow +: headersRows)

    ProgressInfo(None, Seq(), Seq(m.suffix.last))
  }
} 
开发者ID:ergoplatform,项目名称:ergo,代码行数:41,代码来源:FullPoPoWProofsProcessor.scala

示例2: TransactionsBlockField

//设置package包名称以及导入依赖的类
package scorex.transaction

import com.google.common.primitives.{Bytes, Ints}
import play.api.libs.json.{JsArray, JsObject, Json}
import scorex.block.{Block, BlockField}

trait TransactionsBlockField extends BlockField[Seq[Transaction]]

object TransactionsBlockField {
  def apply(version: Int, value: Seq[Transaction]): TransactionsBlockField = version match {
    case 1 | 2 => TransactionsBlockFieldVersion1or2(value)
    case 3 => TransactionsBlockFieldVersion3(value)
  }
}

case class TransactionsBlockFieldVersion1or2(override val value: Seq[Transaction]) extends TransactionsBlockField {
  override val name = "transactions"

  override lazy val json: JsObject = Json.obj(name -> JsArray(value.map(_.json)))

  override lazy val bytes: Array[Byte] = {
    val txCount = value.size.ensuring(_ <= Block.MaxTransactionsPerBlockVer1).toByte
    value.foldLeft(Array(txCount)) { case (bs, tx) =>
      val txBytes = tx.bytes
      bs ++ Bytes.ensureCapacity(Ints.toByteArray(txBytes.length), 4, 0) ++ txBytes
    }
  }
}

case class TransactionsBlockFieldVersion3(override val value: Seq[Transaction]) extends TransactionsBlockField {
  override val name = "transactions"

  override lazy val json: JsObject = Json.obj(name -> JsArray(value.map(_.json)))

  override lazy val bytes: Array[Byte] = {
    val txCount = value.size.ensuring(_ <= Block.MaxTransactionsPerBlockVer2)
    val serTxCount = Seq((txCount / 256).toByte, txCount % 256).map(_.toByte).toArray
    value.foldLeft(serTxCount) { case (bs, tx) =>
      val txBytes = tx.bytes
      bs ++ Bytes.ensureCapacity(Ints.toByteArray(txBytes.length), 4, 0) ++ txBytes
    }
  }
} 
开发者ID:wavesplatform,项目名称:Waves,代码行数:44,代码来源:TransactionsBlockField.scala

示例3: BlockCheckpoint

//设置package包名称以及导入依赖的类
package com.wavesplatform.network

import com.google.common.primitives.{Bytes, Ints}
import io.swagger.annotations.ApiModelProperty
import play.api.libs.json._
import scorex.crypto.encode.Base58

import scala.collection.immutable.Stream
import scala.util.{Failure, Success}

case class BlockCheckpoint(height: Int,
                           @ApiModelProperty(dataType = "java.lang.String") signature: Array[Byte])

case class Checkpoint(items: Seq[BlockCheckpoint],
                      @ApiModelProperty(dataType = "java.lang.String")signature: Array[Byte]) {
  def toSign: Array[Byte] = {
    val length = items.size
    val lengthBytes = Ints.toByteArray(length)

    items.foldLeft(lengthBytes) { case (bs, BlockCheckpoint(h, s)) =>
      Bytes.concat(bs, Ints.toByteArray(h), s)
    }
  }
}

object Checkpoint {
  def historyPoints(n: Int, maxRollback: Int, resultSize: Int = MaxCheckpoints): Seq[Int] =
    mult(maxRollback, 2).map(n - _).takeWhile(_ > 0).take(resultSize)

  private def mult(start: Int, step: Int): Stream[Int] =
    Stream.cons(start, mult(start * step, step))

  val MaxCheckpoints = 10

  implicit val byteArrayReads = new Reads[Array[Byte]] {
    def reads(json: JsValue) = json match {
      case JsString(s) => Base58.decode(s) match {
        case Success(bytes) if bytes.length == scorex.transaction.TransactionParser.SignatureLength => JsSuccess(bytes)
        case Success(bytes) => JsError(JsonValidationError("error.incorrect.signatureLength", bytes.length.toString))
        case Failure(t) => JsError(JsonValidationError(Seq("error.incorrect.base58", t.getLocalizedMessage), s))
      }
      case _ => JsError("error.expected.jsstring")
    }
  }

  implicit val blockCheckpointFormat: Reads[BlockCheckpoint] = Json.reads
  implicit val checkpointFormat: Reads[Checkpoint] = Json.reads
} 
开发者ID:wavesplatform,项目名称:Waves,代码行数:49,代码来源:Checkpoint.scala

示例4: PartialProof

//设置package包名称以及导入依赖的类
package scorex.perma.consensus

import com.google.common.primitives.{Ints, Longs}
import io.circe.Json
import io.circe.syntax._
import scorex.crypto.encode.Base58
import scorex.serialization.{BytesParseable, BytesSerializable, JsonSerializable}
import scorex.transaction.proof.Signature25519

import scala.util.Try

case class PartialProof(signature: Signature25519, segmentIndex: Long, segment: PermaAuthData) extends JsonSerializable
with BytesSerializable {
  lazy val json: Json = Map(
    "signature" -> Base58.encode(signature.signature).asJson,
    "segmentIndex" -> segmentIndex.asJson,
    "segment" -> segment.json
  ).asJson

  override def bytes: Array[Byte] = {
    Longs.toByteArray(segmentIndex) ++ arrayWithSize(signature.signature) ++ segment.bytes
  }
}

object PartialProof extends BytesParseable[PartialProof] {
  override def parseBytes(bytes: Array[Byte]): Try[PartialProof] = Try {
    val i = Longs.fromByteArray(bytes.slice(0, 8))
    val sigSize = Ints.fromByteArray(bytes.slice(8, 12))
    val sig = Signature25519(bytes.slice(12, 12 + sigSize))
    val segment = PermaAuthData.parseBytes(bytes.slice(12 + sigSize, bytes.length)).get
    PartialProof(sig, i, segment)
  }
} 
开发者ID:ScorexFoundation,项目名称:perma,代码行数:34,代码来源:PartialProof.scala


注:本文中的com.google.common.primitives.Ints类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。