Skip to content
This repository was archived by the owner on Apr 13, 2022. It is now read-only.

I1132 #362

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

I1132 #362

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import scala.util.Try
name := "scorex-core"

lazy val commonSettings = Seq(
scalaVersion := "2.12.3",
scalaVersion := "2.12.10",
resolvers += Resolver.sonatypeRepo("public"),
wartremoverErrors ++= Seq(
Wart.Recursion,
Expand Down Expand Up @@ -96,7 +96,7 @@ val loggingDependencies = Seq(
"ch.qos.logback" % "logback-classic" % "1.3.0-alpha4"
)

val scorexUtil = "org.scorexfoundation" %% "scorex-util" % "0.1.6"
val scorexUtil = "org.scorexfoundation" %% "scorex-util" % "0.1.7"

val testingDependencies = Seq(
"com.typesafe.akka" %% "akka-testkit" % akkaVersion % "test",
Expand Down
18 changes: 5 additions & 13 deletions src/main/scala/scorex/core/core.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,9 @@ package object core {
//TODO implement ModifierTypeId as a trait
object ModifierTypeId extends TaggedType[Byte]

@deprecated("use `scorex.util.ModifierId`", "")
type ModifierId = scorex.util.ModifierId.Type

@deprecated("use `scorex.util.ModifierId`", "")
val ModifierId: scorex.util.ModifierId.type = scorex.util.ModifierId

object VersionTag extends TaggedType[String]

type ModifierTypeId = ModifierTypeId.Type

type VersionTag = VersionTag.Type
type VersionTag = util.ModifierId
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is confusing reuse, which complicates things.
So I suggest to declare
object VersionTag extends TaggedType[util.ModifierId]

So we don't accidentally confuse two types.

In particular, is it always 32 long hash?
Also ScalaDoc is missing, so these assumptions should be described in ScalaDoc.

Copy link
Contributor Author

@knizhnik knizhnik Jul 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that the main source of confusion is that there are many different types which are in0turn just wrappers around Array[Byte]:
ModifierId, VersionTag, EncodedTokenId, EncodedBoxId, ByteArrayWrapper
From my point of view I leave just one implementation (my choice is ByteArrayWrapper, because it most clear term) and may be left other types as aliases.
Concerning size of byte array: is it always 32 bytes or can have arbitrary length, I also do not know whether all hashes (keys) used in Ergo are 32 bytes long, but definitely it is not true for generic ByteArrayWrapper. Also my choice is to not make this assumption in ByteArrayWrapper implementation. If you think that speed of hash function is so critical, we can add just one extra if:

    override def hashCode: Int = {
      val bytes = hashBytes
      if (bytes.size == 32) {
         hashFromBytes(bytes(28), bytes(29), bytes(30), bytes(31))
      } else {
         java.util.Arrays.hashCode(bytes)
      }
    }


def idsToString(ids: Seq[(ModifierTypeId, util.ModifierId)])(implicit enc: ScorexEncoder): String = {
List(ids.headOption, ids.lastOption)
Expand All @@ -39,12 +31,12 @@ package object core {

def idToBytes: util.ModifierId => Array[Byte] = scorex.util.idToBytes

def bytesToVersion(bytes: Array[Byte]): VersionTag = VersionTag @@ Base16.encode(bytes)
def bytesToVersion(bytes: Array[Byte]): VersionTag = scorex.util.bytesToId(bytes)

def versionToBytes(id: VersionTag): Array[Byte] = Base16.decode(id).get
def versionToBytes(id: VersionTag): Array[Byte] = scorex.util.idToBytes(id)

def versionToId(version: VersionTag): util.ModifierId = util.ModifierId @@ version
def versionToId(version: VersionTag): util.ModifierId = version

def idToVersion(id: util.ModifierId): VersionTag = VersionTag @@ id
def idToVersion(id: util.ModifierId): VersionTag = id

}
4 changes: 2 additions & 2 deletions src/main/scala/scorex/core/utils/ScorexEncoder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ class ScorexEncoder extends BytesEncoder {
* with encode() and decode methods
*/
@inline
def encodeVersion(input: VersionTag): String = input
def encodeVersion(input: VersionTag): String = input.toString

/**
* This method might be useful and reimplemented, if encoding of ModifierId and VersionTag
* is different form default bytes encoding, e.g. this method should be reimplemented together
* with encode() and decode methods
*/
@inline
def encodeId(input: ModifierId): String = input
def encodeId(input: ModifierId): String = input.toString

}

Expand Down
23 changes: 12 additions & 11 deletions testkit/src/main/scala/scorex/testkit/utils/FileUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,22 @@ trait FileUtils {
createTempDirForPrefix(prefix)
}


@SuppressWarnings(Array("org.wartremover.warts.Recursion"))
def deleteRecursive(dir: java.io.File): Unit = {
for (file <- dir.listFiles) {
if (!file.getName.startsWith(".")) {
if (file.isDirectory) deleteRecursive(file)
file.delete()
}
}
}

/**
* Recursively remove all files and directories in `root`
*/
def remove(root: Path): Unit = {
Files.walkFileTree(root, new SimpleFileVisitor[Path] {
override def visitFile(file: Path, attrs: BasicFileAttributes): FileVisitResult = {
Files.delete(file)
FileVisitResult.CONTINUE
}

override def postVisitDirectory(dir: Path, exc: IOException): FileVisitResult = {
Files.delete(dir)
FileVisitResult.CONTINUE
}
})
deleteRecursive(root.toFile)
}

private def createTempDirForPrefix(prefix: String): java.io.File = {
Expand Down