From 44455d2d8e6cd5553da7e7bada1a5b1088b27b41 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Wed, 7 Sep 2022 19:17:42 +0200 Subject: [PATCH 01/17] scala-js: upgrade sbt --- build.sbt | 12 +++++++++--- project/build.properties | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/build.sbt b/build.sbt index 100137a..ff2c3cf 100644 --- a/build.sbt +++ b/build.sbt @@ -5,9 +5,10 @@ description := "Common tools for scorex projects" organization := "org.scorexfoundation" -lazy val scala212 = "2.12.10" +lazy val scala213 = "2.13.8" +lazy val scala212 = "2.12.15" lazy val scala211 = "2.11.12" -lazy val scala213 = "2.13.1" + crossScalaVersions := Seq(scala212, scala211, scala213) scalaVersion := scala212 @@ -18,7 +19,7 @@ javacOptions ++= resolvers ++= Seq("Sonatype Releases" at "https://oss.sonatype.org/content/repositories/releases/", "SonaType" at "https://oss.sonatype.org/content/groups/public", - "Typesafe maven releases" at "http://repo.typesafe.com/typesafe/maven-releases/", + "Typesafe maven releases" at "https://repo.typesafe.com/typesafe/maven-releases/", "Sonatype Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/" ) @@ -54,6 +55,11 @@ pomExtra := Alexander Chepurnoy http://chepurnoy.org/ + + aslesarenko + Alexander Slesarenko + https://github.com/aslesarenko/ + // prefix version with "-SNAPSHOT" for builds without a git tag diff --git a/project/build.properties b/project/build.properties index 5a9ed92..c06db1b 100644 --- a/project/build.properties +++ b/project/build.properties @@ -1 +1 @@ -sbt.version=1.3.4 +sbt.version=1.4.5 From 602d8ad60d698472063d156a112c238ea2b5260f Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Wed, 7 Sep 2022 20:56:57 +0200 Subject: [PATCH 02/17] scala-js: removed com.typesafe.scala-logging dependence --- build.sbt | 2 -- src/main/scala/scorex/util/ScorexLogging.scala | 7 ------- 2 files changed, 9 deletions(-) delete mode 100644 src/main/scala/scorex/util/ScorexLogging.scala diff --git a/build.sbt b/build.sbt index ff2c3cf..c8ebfc5 100644 --- a/build.sbt +++ b/build.sbt @@ -1,4 +1,3 @@ -import scala.util.Try name := "scorex-util" description := "Common tools for scorex projects" @@ -25,7 +24,6 @@ resolvers ++= Seq("Sonatype Releases" at "https://oss.sonatype.org/content/repos libraryDependencies ++= Seq( "org.rudogma" %% "supertagged" % "1.5", - "com.typesafe.scala-logging" %% "scala-logging" % "3.9.2", "org.scalatest" %% "scalatest" % "3.1.1" % Test, "org.scalacheck" %% "scalacheck" % "1.14.+" % Test, // https://mvnrepository.com/artifact/org.scalatestplus/scalatestplus-scalacheck diff --git a/src/main/scala/scorex/util/ScorexLogging.scala b/src/main/scala/scorex/util/ScorexLogging.scala deleted file mode 100644 index 65ab650..0000000 --- a/src/main/scala/scorex/util/ScorexLogging.scala +++ /dev/null @@ -1,7 +0,0 @@ -package scorex.util - -import com.typesafe.scalalogging.StrictLogging - -trait ScorexLogging extends StrictLogging { - @inline protected def log = logger -} \ No newline at end of file From ebdc12867286bb9075b443022cb02b3e3b6b4c47 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Thu, 8 Sep 2022 09:55:06 +0200 Subject: [PATCH 03/17] scala-js: upgrade supertagged to 2.0-RC2 --- build.sbt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.sbt b/build.sbt index c8ebfc5..6fd960f 100644 --- a/build.sbt +++ b/build.sbt @@ -23,7 +23,7 @@ resolvers ++= Seq("Sonatype Releases" at "https://oss.sonatype.org/content/repos ) libraryDependencies ++= Seq( - "org.rudogma" %% "supertagged" % "1.5", + "org.rudogma" %% "supertagged" % "2.0-RC2", "org.scalatest" %% "scalatest" % "3.1.1" % Test, "org.scalacheck" %% "scalacheck" % "1.14.+" % Test, // https://mvnrepository.com/artifact/org.scalatestplus/scalatestplus-scalacheck From 57bd94046a6c2b6b3cd303d0c89fcf7efc8b366d Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Thu, 8 Sep 2022 10:31:22 +0200 Subject: [PATCH 04/17] scala-js: upgrade sbt and plugins --- build.sbt | 27 +++++++++++++++++++++++---- project/build.properties | 2 +- project/plugins.sbt | 2 ++ 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/build.sbt b/build.sbt index 6fd960f..877579f 100644 --- a/build.sbt +++ b/build.sbt @@ -8,7 +8,7 @@ lazy val scala213 = "2.13.8" lazy val scala212 = "2.12.15" lazy val scala211 = "2.11.12" -crossScalaVersions := Seq(scala212, scala211, scala213) +crossScalaVersions := Seq(scala211, scala212, scala213) scalaVersion := scala212 javacOptions ++= @@ -26,11 +26,30 @@ libraryDependencies ++= Seq( "org.rudogma" %% "supertagged" % "2.0-RC2", "org.scalatest" %% "scalatest" % "3.1.1" % Test, "org.scalacheck" %% "scalacheck" % "1.14.+" % Test, - // https://mvnrepository.com/artifact/org.scalatestplus/scalatestplus-scalacheck - "org.scalatestplus" %% "scalatestplus-scalacheck" % "3.1.0.0-RC2" % Test - + "org.scalatestplus" %% "scalatestplus-scalacheck" % "3.1.0.0-RC2" % Test ) +//lazy val core = crossProject(JSPlatform, JVMPlatform) +// .in(file(".")) +// .settings(moduleName := "debox") +// .settings(deboxSettings) +// .jvmSettings( +// libraryDependencies ++= Seq( +// "org.typelevel" %%% "spire-macros" % "0.17.0-M1" // Version published for Scala 2.11-2.13 +// ), +// scalaVersion := scala213, +// crossScalaVersions := Seq(scala211, scala212, scala213), +// ) +// .jsSettings( +// scalaVersion := scala213, +// crossScalaVersions := Seq(scala213), +// libraryDependencies ++= Seq( +// "org.typelevel" %%% "spire-macros" % "0.17.0" // Version supporting Scala.js 1.x 2.13, 3.x +// ), +// parallelExecution in Test := false +// ) + + publishMavenStyle in ThisBuild := true publishTo := sonatypePublishToBundle.value publishArtifact in Test := true diff --git a/project/build.properties b/project/build.properties index c06db1b..9edb75b 100644 --- a/project/build.properties +++ b/project/build.properties @@ -1 +1 @@ -sbt.version=1.4.5 +sbt.version=1.5.4 diff --git a/project/plugins.sbt b/project/plugins.sbt index 650ebe0..2586dab 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -4,3 +4,5 @@ addSbtPlugin("net.virtual-void" % "sbt-dependency-graph" % "0.9.2") addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "3.8") addSbtPlugin("com.jsuereth" % "sbt-pgp" % "2.0.0") addSbtPlugin("com.dwijnand" % "sbt-dynver" % "4.1.1") +addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject" % "1.2.0") +addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.10.1") From 52075a35af1fa9cb526bb94e442796925feea3b1 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Thu, 8 Sep 2022 14:43:42 +0200 Subject: [PATCH 05/17] scala-js: migrate to crossProject configuration --- build.sbt | 228 +++++++++++++----- .../scala/scorex/util/ByteArrayBuilder.java | 0 .../main/scala/scorex/util/Extensions.scala | 5 +- .../src}/main/scala/scorex/util/Random.scala | 0 .../scala/scorex/util/ScorexEncoding.scala | 0 .../scala/scorex/util/encode/Base16.scala | 10 +- .../scala/scorex/util/encode/Base58.scala | 0 .../scala/scorex/util/encode/Base64.scala | 0 .../scorex/util/encode/BytesEncoder.scala | 0 .../scorex/util/encode/ZigZagEncoder.scala | 2 - .../src}/main/scala/scorex/util/package.scala | 0 .../scorex/util/serialization/Reader.scala | 0 .../util/serialization/Serializer.scala | 0 .../serialization/VLQByteBufferReader.scala | 0 .../serialization/VLQByteBufferWriter.scala | 0 .../scorex/util/serialization/VLQReader.scala | 0 .../scorex/util/serialization/VLQWriter.scala | 8 +- .../scorex/util/serialization/Writer.scala | 0 .../test/scala/scorex/ModifierIdSpec.scala | 0 .../scorex/util/ExtensionsSpecification.scala | 2 +- .../test/scala/scorex/util/Generators.scala | 0 .../test/scala/scorex/util/TestHelpers.scala | 0 .../util/encode/Base16Specification.scala | 0 .../util/encode/Base58Specification.scala | 0 .../util/encode/Base64Specification.scala | 0 .../encode/BytesEncoderSpecification.scala | 0 .../util/encode/ZigZagSpecification.scala | 0 .../serialization/ByteArrayBuilderTests.scala | 0 .../VLQReaderWriterSpecification.scala | 24 +- ...LByteBufferReaderWriterSpecification.scala | 0 30 files changed, 193 insertions(+), 86 deletions(-) rename {src => shared/src}/main/scala/scorex/util/ByteArrayBuilder.java (100%) rename {src => shared/src}/main/scala/scorex/util/Extensions.scala (93%) rename {src => shared/src}/main/scala/scorex/util/Random.scala (100%) rename {src => shared/src}/main/scala/scorex/util/ScorexEncoding.scala (100%) rename {src => shared/src}/main/scala/scorex/util/encode/Base16.scala (89%) rename {src => shared/src}/main/scala/scorex/util/encode/Base58.scala (100%) rename {src => shared/src}/main/scala/scorex/util/encode/Base64.scala (100%) rename {src => shared/src}/main/scala/scorex/util/encode/BytesEncoder.scala (100%) rename {src => shared/src}/main/scala/scorex/util/encode/ZigZagEncoder.scala (97%) rename {src => shared/src}/main/scala/scorex/util/package.scala (100%) rename {src => shared/src}/main/scala/scorex/util/serialization/Reader.scala (100%) rename {src => shared/src}/main/scala/scorex/util/serialization/Serializer.scala (100%) rename {src => shared/src}/main/scala/scorex/util/serialization/VLQByteBufferReader.scala (100%) rename {src => shared/src}/main/scala/scorex/util/serialization/VLQByteBufferWriter.scala (100%) rename {src => shared/src}/main/scala/scorex/util/serialization/VLQReader.scala (100%) rename {src => shared/src}/main/scala/scorex/util/serialization/VLQWriter.scala (98%) rename {src => shared/src}/main/scala/scorex/util/serialization/Writer.scala (100%) rename {src => shared/src}/test/scala/scorex/ModifierIdSpec.scala (100%) rename {src => shared/src}/test/scala/scorex/util/ExtensionsSpecification.scala (98%) rename {src => shared/src}/test/scala/scorex/util/Generators.scala (100%) rename {src => shared/src}/test/scala/scorex/util/TestHelpers.scala (100%) rename {src => shared/src}/test/scala/scorex/util/encode/Base16Specification.scala (100%) rename {src => shared/src}/test/scala/scorex/util/encode/Base58Specification.scala (100%) rename {src => shared/src}/test/scala/scorex/util/encode/Base64Specification.scala (100%) rename {src => shared/src}/test/scala/scorex/util/encode/BytesEncoderSpecification.scala (100%) rename {src => shared/src}/test/scala/scorex/util/encode/ZigZagSpecification.scala (100%) rename {src => shared/src}/test/scala/scorex/util/serialization/ByteArrayBuilderTests.scala (100%) rename {src => shared/src}/test/scala/scorex/util/serialization/VLQReaderWriterSpecification.scala (97%) rename {src => shared/src}/test/scala/scorex/util/serialization/VQLByteBufferReaderWriterSpecification.scala (100%) diff --git a/build.sbt b/build.sbt index 877579f..40a9129 100644 --- a/build.sbt +++ b/build.sbt @@ -1,8 +1,97 @@ -name := "scorex-util" -description := "Common tools for scorex projects" +lazy val scalac: Seq[String] = Seq( + "-deprecation", // Emit warning and location for usages of deprecated APIs. + "-encoding", "utf-8", // Specify character encoding used by source files. + "-explaintypes", // Explain type errors in more detail. + "-feature", // Emit warning and location for usages of features that should be imported explicitly. + "-language:experimental.macros", // Allow macro definition (besides implementation and application) + "-language:higherKinds", // Allow higher-kinded types + "-language:implicitConversions", // Allow definition of implicit functions called views + "-unchecked", // Enable additional warnings where generated code depends on assumptions. + "-Xfatal-warnings", // Fail the compilation if there are any warnings. + // "-Ypartial-unification", // Enable partial unification in type constructor inference + "-Ywarn-dead-code", // Warn when dead code is identified. + "-Ywarn-numeric-widen" // Warn when numerics are widened. + //"-Xlog-free-terms", +) + +lazy val scalac211: Seq[String] = Seq( + "-optimize", + "-Yno-adapted-args", // Do not adapt an argument list (either by inserting () or creating a tuple) to match the receiver. + "-Ywarn-inaccessible", // Warn about inaccessible types in method signatures. + "-Xlint:unsound-match", // Pattern match may not be typesafe. + "-Ywarn-infer-any", // Warn when a type argument is inferred to be `Any`. + "-Ywarn-nullary-override", // Warn when non-nullary `def f()' overrides nullary `def f'. + "-Ywarn-nullary-unit", // Warn when nullary methods return Unit. + "-Xfuture", // Turn on future language features. +) + +lazy val scalac212: Seq[String] = Seq( + "-opt:simplify-jumps", + "-opt:compact-locals", + "-opt:copy-propagation", + "-opt:box-unbox", + "-opt:closure-invocations", + "-opt:unreachable-code", + "-Yno-adapted-args", // Do not adapt an argument list (either by inserting () or creating a tuple) to match the receiver. + "-Ywarn-inaccessible", // Warn about inaccessible types in method signatures. + "-Ywarn-unused:implicits", // Warn if an implicit parameter is unused. + "-Ywarn-unused:imports", // Warn if an import selector is not referenced. + "-Ywarn-unused:locals", // Warn if a local definition is unused. + "-Ywarn-unused:params", // Warn if a value parameter is unused. + "-Ywarn-unused:patvars", // Warn if a variable bound in a pattern is unused. + "-Ywarn-unused:privates", // Warn if a private member is unused. + "-Ywarn-extra-implicit", // Warn when more than one implicit parameter section is defined. + "-Xlint:adapted-args", // Warn if an argument list is modified to match the receiver. + "-Xlint:by-name-right-associative", // By-name parameter of right associative operator. + "-Xlint:constant", // Evaluation of a constant arithmetic expression results in an error. + "-Xlint:delayedinit-select", // Selecting member of DelayedInit. + "-Xlint:doc-detached", // A Scaladoc comment appears to be detached from its element. + "-Xlint:inaccessible", // Warn about inaccessible types in method signatures. + "-Xlint:infer-any", // Warn when a type argument is inferred to be `Any`. + "-Xlint:missing-interpolator", // A string literal appears to be missing an interpolator id. + "-Xlint:nullary-override", // Warn when non-nullary `def f()' overrides nullary `def f'. + "-Xlint:nullary-unit", // Warn when nullary methods return Unit. + "-Xlint:option-implicit", // Option.apply used implicit view. + "-Xlint:package-object-classes", // Class or object defined in package object. + "-Xlint:poly-implicit-overload", // Parameterized overloaded implicit methods are not visible as view bounds. + "-Xlint:private-shadow", // A private field (or class parameter) shadows a superclass field. + "-Xlint:stars-align", // Pattern sequence wildcard must align with sequence component. + "-Xlint:type-parameter-shadow", // A local type parameter shadows a type already in scope. + "-Xlint:unsound-match", // Pattern match may not be typesafe. + "-Ywarn-infer-any", // Warn when a type argument is inferred to be `Any`. + "-Xfuture", // Turn on future language features. +) + +lazy val scalac213: Seq[String] = Seq( + "-opt:simplify-jumps", + "-opt:compact-locals", + "-opt:copy-propagation", + "-opt:box-unbox", + "-opt:closure-invocations", + "-opt:unreachable-code", + "-Ywarn-unused:implicits", // Warn if an implicit parameter is unused. + "-Ywarn-unused:imports", // Warn if an import selector is not referenced. + "-Ywarn-unused:locals", // Warn if a local definition is unused. + "-Ywarn-unused:params", // Warn if a value parameter is unused. + "-Ywarn-unused:patvars", // Warn if a variable bound in a pattern is unused. + "-Ywarn-unused:privates", // Warn if a private member is unused. + "-Ywarn-extra-implicit", // Warn when more than one implicit parameter section is defined. + "-Xlint:adapted-args", // Warn if an argument list is modified to match the receiver. + "-Xlint:constant", // Evaluation of a constant arithmetic expression results in an error. + "-Xlint:delayedinit-select", // Selecting member of DelayedInit. + "-Xlint:doc-detached", // A Scaladoc comment appears to be detached from its element. + "-Xlint:inaccessible", // Warn about inaccessible types in method signatures. + "-Xlint:infer-any", // Warn when a type argument is inferred to be `Any`. + "-Xlint:missing-interpolator", // A string literal appears to be missing an interpolator id. + "-Xlint:option-implicit", // Option.apply used implicit view. + "-Xlint:package-object-classes", // Class or object defined in package object. + "-Xlint:poly-implicit-overload", // Parameterized overloaded implicit methods are not visible as view bounds. + "-Xlint:private-shadow", // A private field (or class parameter) shadows a superclass field. + "-Xlint:stars-align", // Pattern sequence wildcard must align with sequence component. + "-Xlint:type-parameter-shadow", // A local type parameter shadows a type already in scope. +) -organization := "org.scorexfoundation" lazy val scala213 = "2.13.8" lazy val scala212 = "2.12.15" @@ -16,74 +105,95 @@ javacOptions ++= "-target" :: "1.8" :: Nil -resolvers ++= Seq("Sonatype Releases" at "https://oss.sonatype.org/content/repositories/releases/", - "SonaType" at "https://oss.sonatype.org/content/groups/public", - "Typesafe maven releases" at "https://repo.typesafe.com/typesafe/maven-releases/", - "Sonatype Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/" +lazy val utilSettings = Seq( + organization := "org.scorexfoundation", + licenses := Seq("CC0" -> url("https://creativecommons.org/publicdomain/zero/1.0/legalcode")), + homepage := Some(url("http://github.com/ScorexFoundation/scorex-util")), + description := "Common tools for scorex projects", + + resolvers += Resolver.sonatypeRepo("public"), + libraryDependencies ++= Seq( +// scalaOrganization.value % "scala-reflect" % scalaVersion.value % "provided", + "org.rudogma" %%% "supertagged" % "2.0-RC2", + "org.scalatest" %%% "scalatest" % "3.3.0-SNAP3" % Test, + "org.scalatest" %%% "scalatest-propspec" % "3.3.0-SNAP3" % Test, + "org.scalatest" %%% "scalatest-shouldmatchers" % "3.3.0-SNAP3" % Test, + "org.scalatestplus" %%% "scalacheck-1-15" % "3.3.0.0-SNAP3" % Test, + "org.scalacheck" %%% "scalacheck" % "1.15.2" % Test + ), + + scalacOptions := { + CrossVersion.partialVersion(scalaVersion.value) match { + case Some((2, n)) if n == 13 => + scalac ++ scalac213 + case Some((2, n)) if n == 12 => + scalac ++ scalac212 + case Some((2, 11)) => + scalac ++ scalac211 + } + }, + + publishMavenStyle := true, + + publishTo := sonatypePublishToBundle.value, + pomExtra := ( + + + kushti + Alexander Chepurnoy + http://chepurnoy.org/ + + + aslesarenko + Alexander Slesarenko + https://github.com/aslesarenko/ + + + ), + scmInfo := Some( + ScmInfo( + url("https://github.com/ScorexFoundation/scorex-util"), + "scm:git@github.com:ScorexFoundation/scorex-util.git" + ) + ) ) -libraryDependencies ++= Seq( - "org.rudogma" %% "supertagged" % "2.0-RC2", - "org.scalatest" %% "scalatest" % "3.1.1" % Test, - "org.scalacheck" %% "scalacheck" % "1.14.+" % Test, - "org.scalatestplus" %% "scalatestplus-scalacheck" % "3.1.0.0-RC2" % Test -) - -//lazy val core = crossProject(JSPlatform, JVMPlatform) -// .in(file(".")) -// .settings(moduleName := "debox") -// .settings(deboxSettings) -// .jvmSettings( -// libraryDependencies ++= Seq( +lazy val core = crossProject(JSPlatform, JVMPlatform) + .in(file(".")) + .settings( + name := "scorex-util", + moduleName := "scorex-util" + ) + .settings(utilSettings) + .jvmSettings( + scalaVersion := scala213, + crossScalaVersions := Seq(scala211, scala212, scala213), + libraryDependencies ++= Seq( // "org.typelevel" %%% "spire-macros" % "0.17.0-M1" // Version published for Scala 2.11-2.13 -// ), -// scalaVersion := scala213, -// crossScalaVersions := Seq(scala211, scala212, scala213), -// ) -// .jsSettings( -// scalaVersion := scala213, -// crossScalaVersions := Seq(scala213), -// libraryDependencies ++= Seq( + ), + ) + .jsSettings( + scalaVersion := scala213, + crossScalaVersions := Seq(scala213), + libraryDependencies ++= Seq( // "org.typelevel" %%% "spire-macros" % "0.17.0" // Version supporting Scala.js 1.x 2.13, 3.x -// ), -// parallelExecution in Test := false -// ) + ), + Test / parallelExecution := false + ) +Test / publishArtifact := true +pomIncludeRepository := { _ => false } -publishMavenStyle in ThisBuild := true -publishTo := sonatypePublishToBundle.value -publishArtifact in Test := true +// prefix version with "-SNAPSHOT" for builds without a git tag +ThisBuild / dynverSonatypeSnapshots := true +// use "-" instead of default "+" +ThisBuild / dynverSeparator := "-" credentials ++= (for { username <- Option(System.getenv().get("SONATYPE_USERNAME")) password <- Option(System.getenv().get("SONATYPE_PASSWORD")) } yield Credentials("Sonatype Nexus Repository Manager", "oss.sonatype.org", username, password)).toSeq -fork in ThisBuild := true - -pomIncludeRepository in ThisBuild := { _ => false } - -licenses := Seq("CC0" -> url("https://creativecommons.org/publicdomain/zero/1.0/legalcode")) -homepage := Some(url("https://github.com/ScorexFoundation/scorex-util")) -pomExtra := - - - kushti - Alexander Chepurnoy - http://chepurnoy.org/ - - - aslesarenko - Alexander Slesarenko - https://github.com/aslesarenko/ - - - -// prefix version with "-SNAPSHOT" for builds without a git tag -dynverSonatypeSnapshots in ThisBuild := true -// use "-" instead of default "+" -dynverSeparator in ThisBuild := "-" - // PGP key for signing a release build published to sonatype // signing is done by sbt-pgp plugin diff --git a/src/main/scala/scorex/util/ByteArrayBuilder.java b/shared/src/main/scala/scorex/util/ByteArrayBuilder.java similarity index 100% rename from src/main/scala/scorex/util/ByteArrayBuilder.java rename to shared/src/main/scala/scorex/util/ByteArrayBuilder.java diff --git a/src/main/scala/scorex/util/Extensions.scala b/shared/src/main/scala/scorex/util/Extensions.scala similarity index 93% rename from src/main/scala/scorex/util/Extensions.scala rename to shared/src/main/scala/scorex/util/Extensions.scala index 746d8d6..a778e82 100644 --- a/src/main/scala/scorex/util/Extensions.scala +++ b/shared/src/main/scala/scorex/util/Extensions.scala @@ -1,6 +1,5 @@ package scorex.util -import scala.collection.generic.CanBuildFrom import scala.reflect.ClassTag object Extensions { @@ -82,13 +81,13 @@ object Extensions { } } - implicit class TraversableOps[A, Source[X] <: Traversable[X]](val xs: Source[A]) extends AnyVal { + implicit class TraversableOps[A, Source[X] <: Iterable[X]](val xs: Source[A]) extends AnyVal { /** * Safely casting each element of collection to be type of `B`. * If element can not to be cast to `B` then `AssertionError` is thrown */ - def cast[B:ClassTag](implicit cbf: CanBuildFrom[Source[A], B, Source[B]]): Source[B] = { + def cast[B:ClassTag]: Source[B] = { for (x <- xs) { require(x match { case _: B => true case _ => false}, s"Value $x doesn't conform to type ${reflect.classTag[B]}") diff --git a/src/main/scala/scorex/util/Random.scala b/shared/src/main/scala/scorex/util/Random.scala similarity index 100% rename from src/main/scala/scorex/util/Random.scala rename to shared/src/main/scala/scorex/util/Random.scala diff --git a/src/main/scala/scorex/util/ScorexEncoding.scala b/shared/src/main/scala/scorex/util/ScorexEncoding.scala similarity index 100% rename from src/main/scala/scorex/util/ScorexEncoding.scala rename to shared/src/main/scala/scorex/util/ScorexEncoding.scala diff --git a/src/main/scala/scorex/util/encode/Base16.scala b/shared/src/main/scala/scorex/util/encode/Base16.scala similarity index 89% rename from src/main/scala/scorex/util/encode/Base16.scala rename to shared/src/main/scala/scorex/util/encode/Base16.scala index 42de7c9..a1ad8d7 100644 --- a/src/main/scala/scorex/util/encode/Base16.scala +++ b/shared/src/main/scala/scorex/util/encode/Base16.scala @@ -10,12 +10,12 @@ object Base16 extends BytesEncoder { private val hexArray = "0123456789abcdef".toCharArray private val hexIndex: Array[Byte] = { - var index = Array.fill[Byte](128)(0xff.toByte) + val index = Array.fill[Byte](128)(0xff.toByte) hexArray.zipWithIndex.foreach { case (c, i) => - index(c) = i.toByte + index(c.toInt) = i.toByte } "abcdef".toCharArray.foreach{ c => - index(c.toUpper) = index(c) + index(c.toUpper.toInt) = index(c.toInt) } index } @@ -47,8 +47,8 @@ object Base16 extends BytesEncoder { val c1 = input(j) val c2 = input(j + 1) if (c1 > 0 && c1 < 127 && c2 > 0 && c2 < 127) { - val b1 = hexIndex(c1) - val b2 = hexIndex(c2) + val b1 = hexIndex(c1.toInt) + val b2 = hexIndex(c2.toInt) if ((b1 | b2) < 0) { isError = true errorMsg = "invalid characters encountered in Hex data" diff --git a/src/main/scala/scorex/util/encode/Base58.scala b/shared/src/main/scala/scorex/util/encode/Base58.scala similarity index 100% rename from src/main/scala/scorex/util/encode/Base58.scala rename to shared/src/main/scala/scorex/util/encode/Base58.scala diff --git a/src/main/scala/scorex/util/encode/Base64.scala b/shared/src/main/scala/scorex/util/encode/Base64.scala similarity index 100% rename from src/main/scala/scorex/util/encode/Base64.scala rename to shared/src/main/scala/scorex/util/encode/Base64.scala diff --git a/src/main/scala/scorex/util/encode/BytesEncoder.scala b/shared/src/main/scala/scorex/util/encode/BytesEncoder.scala similarity index 100% rename from src/main/scala/scorex/util/encode/BytesEncoder.scala rename to shared/src/main/scala/scorex/util/encode/BytesEncoder.scala diff --git a/src/main/scala/scorex/util/encode/ZigZagEncoder.scala b/shared/src/main/scala/scorex/util/encode/ZigZagEncoder.scala similarity index 97% rename from src/main/scala/scorex/util/encode/ZigZagEncoder.scala rename to shared/src/main/scala/scorex/util/encode/ZigZagEncoder.scala index 2c9215a..4394276 100644 --- a/src/main/scala/scorex/util/encode/ZigZagEncoder.scala +++ b/shared/src/main/scala/scorex/util/encode/ZigZagEncoder.scala @@ -1,7 +1,5 @@ package scorex.util.encode -import scorex.util.serialization.VLQByteBufferWriter - object ZigZagEncoder { /** diff --git a/src/main/scala/scorex/util/package.scala b/shared/src/main/scala/scorex/util/package.scala similarity index 100% rename from src/main/scala/scorex/util/package.scala rename to shared/src/main/scala/scorex/util/package.scala diff --git a/src/main/scala/scorex/util/serialization/Reader.scala b/shared/src/main/scala/scorex/util/serialization/Reader.scala similarity index 100% rename from src/main/scala/scorex/util/serialization/Reader.scala rename to shared/src/main/scala/scorex/util/serialization/Reader.scala diff --git a/src/main/scala/scorex/util/serialization/Serializer.scala b/shared/src/main/scala/scorex/util/serialization/Serializer.scala similarity index 100% rename from src/main/scala/scorex/util/serialization/Serializer.scala rename to shared/src/main/scala/scorex/util/serialization/Serializer.scala diff --git a/src/main/scala/scorex/util/serialization/VLQByteBufferReader.scala b/shared/src/main/scala/scorex/util/serialization/VLQByteBufferReader.scala similarity index 100% rename from src/main/scala/scorex/util/serialization/VLQByteBufferReader.scala rename to shared/src/main/scala/scorex/util/serialization/VLQByteBufferReader.scala diff --git a/src/main/scala/scorex/util/serialization/VLQByteBufferWriter.scala b/shared/src/main/scala/scorex/util/serialization/VLQByteBufferWriter.scala similarity index 100% rename from src/main/scala/scorex/util/serialization/VLQByteBufferWriter.scala rename to shared/src/main/scala/scorex/util/serialization/VLQByteBufferWriter.scala diff --git a/src/main/scala/scorex/util/serialization/VLQReader.scala b/shared/src/main/scala/scorex/util/serialization/VLQReader.scala similarity index 100% rename from src/main/scala/scorex/util/serialization/VLQReader.scala rename to shared/src/main/scala/scorex/util/serialization/VLQReader.scala diff --git a/src/main/scala/scorex/util/serialization/VLQWriter.scala b/shared/src/main/scala/scorex/util/serialization/VLQWriter.scala similarity index 98% rename from src/main/scala/scorex/util/serialization/VLQWriter.scala rename to shared/src/main/scala/scorex/util/serialization/VLQWriter.scala index a7f1fbf..82f26e7 100644 --- a/src/main/scala/scorex/util/serialization/VLQWriter.scala +++ b/shared/src/main/scala/scorex/util/serialization/VLQWriter.scala @@ -21,7 +21,7 @@ trait VLQWriter extends Writer { * @param x signed Short */ @inline override def putShort(x: Short): this.type = { - putULong(encodeZigZagInt(x)) + putULong(encodeZigZagInt(x.toInt).toLong) } /** @@ -35,7 +35,7 @@ trait VLQWriter extends Writer { */ @inline override def putUShort(x: Int): this.type = { require(x >= 0 && x <= 0xFFFF, s"Value $x is out of unsigned short range") - putUInt(x) + putUInt(x.toLong) } /** @@ -50,7 +50,7 @@ trait VLQWriter extends Writer { * encoding negative values than pure VLQ. * @param x signed Int */ - @inline override def putInt(x: Int): this.type = putULong(encodeZigZagInt(x)) + @inline override def putInt(x: Int): this.type = putULong(encodeZigZagInt(x).toLong) /** * Encode unsigned Int value using VLQ. @@ -156,7 +156,7 @@ trait VLQWriter extends Writer { override def putShortString(s: String): this.type = { val bytes = s.getBytes require(bytes.size < 256) - putUByte(bytes.size.toByte) + putUByte(bytes.size) putBytes(bytes) this } diff --git a/src/main/scala/scorex/util/serialization/Writer.scala b/shared/src/main/scala/scorex/util/serialization/Writer.scala similarity index 100% rename from src/main/scala/scorex/util/serialization/Writer.scala rename to shared/src/main/scala/scorex/util/serialization/Writer.scala diff --git a/src/test/scala/scorex/ModifierIdSpec.scala b/shared/src/test/scala/scorex/ModifierIdSpec.scala similarity index 100% rename from src/test/scala/scorex/ModifierIdSpec.scala rename to shared/src/test/scala/scorex/ModifierIdSpec.scala diff --git a/src/test/scala/scorex/util/ExtensionsSpecification.scala b/shared/src/test/scala/scorex/util/ExtensionsSpecification.scala similarity index 98% rename from src/test/scala/scorex/util/ExtensionsSpecification.scala rename to shared/src/test/scala/scorex/util/ExtensionsSpecification.scala index 84c5804..6309c08 100644 --- a/src/test/scala/scorex/util/ExtensionsSpecification.scala +++ b/shared/src/test/scala/scorex/util/ExtensionsSpecification.scala @@ -12,7 +12,7 @@ class ExtensionsSpecification extends AnyPropSpec property("ByteOps.toUByte") { forAll { b: Byte => - val expected = if (b >= 0) b else 255 + b + 1 + val expected = if (b >= 0) b.toInt else 255 + b + 1 b.toUByte shouldBe expected } } diff --git a/src/test/scala/scorex/util/Generators.scala b/shared/src/test/scala/scorex/util/Generators.scala similarity index 100% rename from src/test/scala/scorex/util/Generators.scala rename to shared/src/test/scala/scorex/util/Generators.scala diff --git a/src/test/scala/scorex/util/TestHelpers.scala b/shared/src/test/scala/scorex/util/TestHelpers.scala similarity index 100% rename from src/test/scala/scorex/util/TestHelpers.scala rename to shared/src/test/scala/scorex/util/TestHelpers.scala diff --git a/src/test/scala/scorex/util/encode/Base16Specification.scala b/shared/src/test/scala/scorex/util/encode/Base16Specification.scala similarity index 100% rename from src/test/scala/scorex/util/encode/Base16Specification.scala rename to shared/src/test/scala/scorex/util/encode/Base16Specification.scala diff --git a/src/test/scala/scorex/util/encode/Base58Specification.scala b/shared/src/test/scala/scorex/util/encode/Base58Specification.scala similarity index 100% rename from src/test/scala/scorex/util/encode/Base58Specification.scala rename to shared/src/test/scala/scorex/util/encode/Base58Specification.scala diff --git a/src/test/scala/scorex/util/encode/Base64Specification.scala b/shared/src/test/scala/scorex/util/encode/Base64Specification.scala similarity index 100% rename from src/test/scala/scorex/util/encode/Base64Specification.scala rename to shared/src/test/scala/scorex/util/encode/Base64Specification.scala diff --git a/src/test/scala/scorex/util/encode/BytesEncoderSpecification.scala b/shared/src/test/scala/scorex/util/encode/BytesEncoderSpecification.scala similarity index 100% rename from src/test/scala/scorex/util/encode/BytesEncoderSpecification.scala rename to shared/src/test/scala/scorex/util/encode/BytesEncoderSpecification.scala diff --git a/src/test/scala/scorex/util/encode/ZigZagSpecification.scala b/shared/src/test/scala/scorex/util/encode/ZigZagSpecification.scala similarity index 100% rename from src/test/scala/scorex/util/encode/ZigZagSpecification.scala rename to shared/src/test/scala/scorex/util/encode/ZigZagSpecification.scala diff --git a/src/test/scala/scorex/util/serialization/ByteArrayBuilderTests.scala b/shared/src/test/scala/scorex/util/serialization/ByteArrayBuilderTests.scala similarity index 100% rename from src/test/scala/scorex/util/serialization/ByteArrayBuilderTests.scala rename to shared/src/test/scala/scorex/util/serialization/ByteArrayBuilderTests.scala diff --git a/src/test/scala/scorex/util/serialization/VLQReaderWriterSpecification.scala b/shared/src/test/scala/scorex/util/serialization/VLQReaderWriterSpecification.scala similarity index 97% rename from src/test/scala/scorex/util/serialization/VLQReaderWriterSpecification.scala rename to shared/src/test/scala/scorex/util/serialization/VLQReaderWriterSpecification.scala index b033436..98caab5 100644 --- a/src/test/scala/scorex/util/serialization/VLQReaderWriterSpecification.scala +++ b/shared/src/test/scala/scorex/util/serialization/VLQReaderWriterSpecification.scala @@ -77,16 +77,16 @@ trait VLQReaderWriterSpecification extends AnyPropSpec case v: Byte => writer.put(v) case v: Short => writer.putShort(v) - if (v >= 0) writer.putUShort(v) + if (v >= 0) writer.putUShort(v.toInt) case v: Int => writer.putInt(v) - if (v >= 0) writer.putUInt(v) + if (v >= 0) writer.putUInt(v.toLong) case v: Long => // test all paths writer.putLong(v) writer.putULong(v) - case v: Array[Byte] => writer.putUShort(v.length.toShort).putBytes(v) - case v: Array[Boolean] => writer.putUShort(v.length.toShort).putBits(v) + case v: Array[Byte] => writer.putUShort(v.length).putBytes(v) + case v: Array[Boolean] => writer.putUShort(v.length).putBits(v) case _ => fail(s"writer: unsupported value type: ${any.getClass}"); } } @@ -247,11 +247,11 @@ trait VLQReaderWriterSpecification extends AnyPropSpec property("getUShort range check assertion") { def check(in: Int): Unit = - byteBufReader(byteArrayWriter().putUInt(in).toBytes).getUShort() shouldBe in + byteBufReader(byteArrayWriter().putUInt(in.toLong).toBytes).getUShort() shouldBe in def checkFail(in: Int): Unit = an[IllegalArgumentException] should be thrownBy - byteBufReader(byteArrayWriter().putUInt(in).toBytes).getUShort() + byteBufReader(byteArrayWriter().putUInt(in.toLong).toBytes).getUShort() check(0) check(0xFFFF) @@ -609,7 +609,7 @@ trait VLQReaderWriterSpecification extends AnyPropSpec private def printHolesInByteArraySpace(): Unit = { var v = Short.MinValue while (v <= Short.MaxValue) { - val bytes = BigInt(v).toByteArray + val bytes = BigInt(v.toInt).toByteArray try { val deserV = byteBufReader(bytes).getShort() val roundtripBytes = byteArrayWriter().putShort(deserV).toBytes @@ -629,17 +629,17 @@ trait VLQReaderWriterSpecification extends AnyPropSpec property("putUShort, putUInt, putULong equivalence") { forAll(Arbitrary.arbShort.arbitrary.suchThat(_ >= 0)) { v => - val expected = byteArrayWriter().putUShort(v).toBytes - byteArrayWriter().putUInt(v).toBytes shouldEqual expected - byteArrayWriter().putULong(v).toBytes shouldEqual expected + val expected = byteArrayWriter().putUShort(v.toInt).toBytes + byteArrayWriter().putUInt(v.toLong).toBytes shouldEqual expected + byteArrayWriter().putULong(v.toLong).toBytes shouldEqual expected } } property("putShort, putInt, putLong equivalence") { forAll { v: Short => val expected = byteArrayWriter().putShort(v).toBytes - byteArrayWriter().putInt(v).toBytes shouldEqual expected - byteArrayWriter().putLong(v).toBytes shouldEqual expected + byteArrayWriter().putInt(v.toInt).toBytes shouldEqual expected + byteArrayWriter().putLong(v.toLong).toBytes shouldEqual expected } } } diff --git a/src/test/scala/scorex/util/serialization/VQLByteBufferReaderWriterSpecification.scala b/shared/src/test/scala/scorex/util/serialization/VQLByteBufferReaderWriterSpecification.scala similarity index 100% rename from src/test/scala/scorex/util/serialization/VQLByteBufferReaderWriterSpecification.scala rename to shared/src/test/scala/scorex/util/serialization/VQLByteBufferReaderWriterSpecification.scala From eba76861450b66dd240ab3800ac6c8a22b37fa82 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Thu, 8 Sep 2022 15:35:42 +0200 Subject: [PATCH 06/17] scala-js: warnings are not errors --- build.sbt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.sbt b/build.sbt index 40a9129..a80cd44 100644 --- a/build.sbt +++ b/build.sbt @@ -8,7 +8,7 @@ lazy val scalac: Seq[String] = Seq( "-language:higherKinds", // Allow higher-kinded types "-language:implicitConversions", // Allow definition of implicit functions called views "-unchecked", // Enable additional warnings where generated code depends on assumptions. - "-Xfatal-warnings", // Fail the compilation if there are any warnings. +// "-Xfatal-warnings", // Fail the compilation if there are any warnings. // "-Ypartial-unification", // Enable partial unification in type constructor inference "-Ywarn-dead-code", // Warn when dead code is identified. "-Ywarn-numeric-widen" // Warn when numerics are widened. From 812842c075cfa9d24514ae9a0fdac625800f178c Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Thu, 8 Sep 2022 17:48:09 +0200 Subject: [PATCH 07/17] scala-js: migrated ByteArrayBuilder from Java --- .../scala/scorex/util/ByteArrayBuilder.java | 129 ------------------ .../scala/scorex/util/ByteArrayBuilder.scala | 115 ++++++++++++++++ .../serialization/VLQByteBufferWriter.scala | 2 +- 3 files changed, 116 insertions(+), 130 deletions(-) delete mode 100644 shared/src/main/scala/scorex/util/ByteArrayBuilder.java create mode 100644 shared/src/main/scala/scorex/util/ByteArrayBuilder.scala diff --git a/shared/src/main/scala/scorex/util/ByteArrayBuilder.java b/shared/src/main/scala/scorex/util/ByteArrayBuilder.java deleted file mode 100644 index db77b45..0000000 --- a/shared/src/main/scala/scorex/util/ByteArrayBuilder.java +++ /dev/null @@ -1,129 +0,0 @@ -package scorex.util; - -import java.nio.ByteBuffer; -import java.util.Arrays; - -/** Similar to StringBuilder but works with underlying Array[Byte]. - * Borrowed from https://github.com/odnoklassniki/one-nio/blob/master/src/one/nio/util/ByteArrayBuilder.java - * Modifications of the underlying array is performed via ByteBuffer wrapper, so that saved bytes can - * be read back via ByteBuffer API. */ -public class ByteArrayBuilder { - - protected byte[] arr; - protected ByteBuffer buf; - - public ByteArrayBuilder() { - this(256); - } - - public ByteArrayBuilder(int capacity) { - this.arr = new byte[capacity]; - this.buf = ByteBuffer.wrap(this.arr); - } - - public final byte[] array() { - return arr; - } - - public final int length() { - return buf.position(); - } - - public final void setLength(int newPosition) { - buf.position(newPosition); - } - - public final int capacity() { - return arr.length; - } - - public final byte byteAt(int index) { - return arr[index]; - } - - public final void crop(int offset) { - int count = buf.position(); - if (offset < count) { - count -= offset; - System.arraycopy(arr, offset, arr, 0, count); - } else { - count = 0; - } - } - - public final byte[] trim() { - int count = buf.position(); - if (arr.length > count) { - arr = Arrays.copyOf(arr, count); - buf = ByteBuffer.wrap(arr); - } - return arr; - } - - public final byte[] toBytes() { - int count = buf.position(); - byte[] result = new byte[count]; - System.arraycopy(arr, 0, result, 0, count); - return result; - } - - public final ByteArrayBuilder append(byte b) { - ensureCapacity(1); - buf.put(b); - return this; - } - - public final ByteArrayBuilder append(byte[] b) { - return append(b, 0, b.length); - } - - public final ByteArrayBuilder append(byte[] b, int offset, int length) { - ensureCapacity(length); - buf.put(b, offset, length); - return this; - } - - public final ByteArrayBuilder append(boolean b) { - append((byte)(b ? 0x01 : 0x00)); - return this; - } - - public final ByteArrayBuilder append(char c) { - ensureCapacity(1); - buf.putChar(c); - return this; - } - - public final ByteArrayBuilder append(short n) { - ensureCapacity(2); - buf.putShort(n); - return this; - } - - public final ByteArrayBuilder append(int n) { - ensureCapacity(4); - buf.putInt(n); - return this; - } - - public final ByteArrayBuilder append(long n) { - ensureCapacity(8); - buf.putLong(n); - return this; - } - - private void ensureCapacity(int required) { - int count = buf.position(); - if (count + required > arr.length) { - arr = Arrays.copyOf(arr, Math.max(count + required, arr.length << 1)); - newBuffer(arr); - } - } - - private void newBuffer(byte[] newArr) { - ByteBuffer newBuf = ByteBuffer.wrap(newArr); - newBuf.position(buf.position()); - buf = newBuf; - } - -} \ No newline at end of file diff --git a/shared/src/main/scala/scorex/util/ByteArrayBuilder.scala b/shared/src/main/scala/scorex/util/ByteArrayBuilder.scala new file mode 100644 index 0000000..e416138 --- /dev/null +++ b/shared/src/main/scala/scorex/util/ByteArrayBuilder.scala @@ -0,0 +1,115 @@ +package scorex.util + +import java.nio.ByteBuffer +import java.util + +/** Similar to StringBuilder but works with underlying Array[Byte]. + * Borrowed from https://github.com/odnoklassniki/one-nio/blob/master/src/one/nio/util/ByteArrayBuilder.java + * Modifications of the underlying array is performed via ByteBuffer wrapper, so that saved bytes can + * be read back via ByteBuffer API. */ +class ByteArrayBuilder(initCapacity: Int) { + protected var arr: Array[Byte] = null + protected var buf: ByteBuffer = null + + this.arr = new Array[Byte](initCapacity) + this.buf = ByteBuffer.wrap(this.arr) + + def this() = this(256) + + final def array: Array[Byte] = arr + + final def length: Int = buf.position() + + final def setLength(newPosition: Int): Unit = { + buf.position(newPosition) + } + + final def capacity(): Int = arr.length + + final def byteAt(index: Int): Byte = arr(index) + + final def crop(offset: Int): Unit = { + var count = buf.position() + if (offset < count) { + count -= offset + System.arraycopy(arr, offset, arr, 0, count) + } + else { + count = 0 + } + } + + final def trim: Array[Byte] = { + val count = buf.position() + if (arr.length > count) { + arr = util.Arrays.copyOf(arr, count) + buf = ByteBuffer.wrap(arr) + } + arr + } + + final def toBytes: Array[Byte] = { + val count = buf.position() + val result = new Array[Byte](count) + System.arraycopy(arr, 0, result, 0, count) + result + } + + final def append(b: Byte): ByteArrayBuilder = { + ensureCapacity(1) + buf.put(b) + this + } + + final def append(b: Array[Byte]): ByteArrayBuilder = + append(b, 0, b.length) + + final def append(b: Array[Byte], offset: Int, length: Int): ByteArrayBuilder = { + ensureCapacity(length) + buf.put(b, offset, length) + this + } + + final def append(b: Boolean): ByteArrayBuilder = { + append((if (b) 0x01 else 0x00).toByte) + this + } + + final def append(c: Char): ByteArrayBuilder = { + ensureCapacity(1) + buf.putChar(c) + this + } + + final def append(n: Short): ByteArrayBuilder = { + ensureCapacity(2) + buf.putShort(n) + this + } + + final def append(n: Int): ByteArrayBuilder = { + ensureCapacity(4) + buf.putInt(n) + this + } + + final def append(n: Long): ByteArrayBuilder = { + ensureCapacity(8) + buf.putLong(n) + this + } + + private def ensureCapacity(required: Int): Unit = { + val count = buf.position() + if (count + required > arr.length) { + arr = util.Arrays.copyOf(arr, Math.max(count + required, arr.length << 1)) + newBuffer(arr) + } + } + + private def newBuffer(newArr: Array[Byte]): Unit = { + val newBuf = ByteBuffer.wrap(newArr) + newBuf.position(buf.position) + buf = newBuf + } +} \ No newline at end of file diff --git a/shared/src/main/scala/scorex/util/serialization/VLQByteBufferWriter.scala b/shared/src/main/scala/scorex/util/serialization/VLQByteBufferWriter.scala index 9e9f8d3..0a72f9a 100644 --- a/shared/src/main/scala/scorex/util/serialization/VLQByteBufferWriter.scala +++ b/shared/src/main/scala/scorex/util/serialization/VLQByteBufferWriter.scala @@ -38,7 +38,7 @@ class VLQByteBufferWriter(b: ByteArrayBuilder) extends Writer with VLQWriter { this } - @inline override def length(): Int = b.length() + @inline override def length(): Int = b.length @inline override def result(): ByteArrayBuilder = b From 10a3e83d1c9bc262a5c648382f461ee7f7c2ce9e Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Thu, 8 Sep 2022 18:57:52 +0200 Subject: [PATCH 08/17] scala-js: fix ci.yml for JVM --- .github/workflows/ci.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a5ebde5..b2760b9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,7 +21,7 @@ jobs: strategy: matrix: os: [ubuntu-latest] - scala: [2.12.10, 2.11.12, 2.13.1] + scala: [2.13.8, 2.12.15, 2.11.12] java: [adopt@1.8] runs-on: ${{ matrix.os }} steps: @@ -48,16 +48,16 @@ jobs: key: ${{ runner.os }}-sbt-cache-v2-${{ hashFiles('**/*.sbt') }}-${{ hashFiles('project/build.properties') }} - name: Runs tests and collect coverage - run: sbt ++${{ matrix.scala }} coverage test coverageReport coverageAggregate + run: sbt ++${{ matrix.scala }} coverage coreJVM/test coreJVM/coverageReport coreJVM/coverageAggregate - name: Upload coverage report to Codecov uses: codecov/codecov-action@v1 with: - fail_ci_if_error: true + fail_ci_if_error: false - name: Publish a snapshot ${{ github.ref }} if: env.HAS_SECRETS == 'true' - run: sbt ++${{ matrix.scala }} publish + run: sbt ++${{ matrix.scala }} coreJVM/publish env: SONATYPE_PASSWORD: ${{ secrets.SONATYPE_PASSWORD }} SONATYPE_USERNAME: ${{ secrets.SONATYPE_USERNAME }} From 194f3858d061fa6143d2914e41f593fc6632fc34 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Thu, 8 Sep 2022 19:04:05 +0200 Subject: [PATCH 09/17] scala-js: turn-off coverage in ci.yml for JVM --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b2760b9..e19dcdf 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -48,7 +48,7 @@ jobs: key: ${{ runner.os }}-sbt-cache-v2-${{ hashFiles('**/*.sbt') }}-${{ hashFiles('project/build.properties') }} - name: Runs tests and collect coverage - run: sbt ++${{ matrix.scala }} coverage coreJVM/test coreJVM/coverageReport coreJVM/coverageAggregate + run: sbt ++${{ matrix.scala }} coreJVM/test - name: Upload coverage report to Codecov uses: codecov/codecov-action@v1 From dc4d6bfbc0e5b87c8875ea3b2d2898b3c7c020ba Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Thu, 8 Sep 2022 19:21:03 +0200 Subject: [PATCH 10/17] scala-js: add buildJs to ci --- .github/workflows/ci.yml | 50 +++++++++++++++++++++++++++++++++++----- 1 file changed, 44 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e19dcdf..8a1e28d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,7 +15,7 @@ env: jobs: build: - name: Test and publish a snapshot + name: JVM - Test and publish a snapshot env: HAS_SECRETS: ${{ secrets.SONATYPE_PASSWORD != '' }} strategy: @@ -47,17 +47,55 @@ jobs: ~/Library/Caches/Coursier/v1 key: ${{ runner.os }}-sbt-cache-v2-${{ hashFiles('**/*.sbt') }}-${{ hashFiles('project/build.properties') }} - - name: Runs tests and collect coverage + - name: Runs tests run: sbt ++${{ matrix.scala }} coreJVM/test - - name: Upload coverage report to Codecov - uses: codecov/codecov-action@v1 + - name: Publish a snapshot ${{ github.ref }} + if: env.HAS_SECRETS == 'true' + run: sbt ++${{ matrix.scala }} coreJVM/publish + env: + SONATYPE_PASSWORD: ${{ secrets.SONATYPE_PASSWORD }} + SONATYPE_USERNAME: ${{ secrets.SONATYPE_USERNAME }} + + buildJs: + name: JS - Test and publish a snapshot + env: + HAS_SECRETS: ${{ secrets.SONATYPE_PASSWORD != '' }} + strategy: + matrix: + os: [ubuntu-latest] + scala: [2.13.8] + java: [adopt@1.8] + runs-on: ${{ matrix.os }} + steps: + - name: Checkout current branch (full) + uses: actions/checkout@v2 with: - fail_ci_if_error: false + fetch-depth: 0 + + - name: Setup Java and Scala + uses: olafurpg/setup-scala@v10 + with: + java-version: ${{ matrix.java }} + + - name: Cache sbt + uses: actions/cache@v2 + with: + path: | + ~/.sbt + ~/.ivy2/cache + ~/.coursier/cache/v1 + ~/.cache/coursier/v1 + ~/AppData/Local/Coursier/Cache/v1 + ~/Library/Caches/Coursier/v1 + key: ${{ runner.os }}-sbt-cache-v2-${{ hashFiles('**/*.sbt') }}-${{ hashFiles('project/build.properties') }} + + - name: Runs tests + run: sbt ++${{ matrix.scala }} coreJS/test - name: Publish a snapshot ${{ github.ref }} if: env.HAS_SECRETS == 'true' - run: sbt ++${{ matrix.scala }} coreJVM/publish + run: sbt ++${{ matrix.scala }} coreJS/publish env: SONATYPE_PASSWORD: ${{ secrets.SONATYPE_PASSWORD }} SONATYPE_USERNAME: ${{ secrets.SONATYPE_USERNAME }} From c0dbc8a34a2363e515221e42037938eb9e693fbd Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Mon, 26 Sep 2022 14:48:12 +0200 Subject: [PATCH 11/17] scala-js: rollback ScorexLogging.scala and JVM-only dependence --- build.sbt | 1 + jvm/src/main/scala/scorex/util/ScorexLogging.scala | 7 +++++++ 2 files changed, 8 insertions(+) create mode 100644 jvm/src/main/scala/scorex/util/ScorexLogging.scala diff --git a/build.sbt b/build.sbt index a80cd44..a53397f 100644 --- a/build.sbt +++ b/build.sbt @@ -169,6 +169,7 @@ lazy val core = crossProject(JSPlatform, JVMPlatform) scalaVersion := scala213, crossScalaVersions := Seq(scala211, scala212, scala213), libraryDependencies ++= Seq( + "com.typesafe.scala-logging" %%% "scala-logging" % "3.9.2" // "org.typelevel" %%% "spire-macros" % "0.17.0-M1" // Version published for Scala 2.11-2.13 ), ) diff --git a/jvm/src/main/scala/scorex/util/ScorexLogging.scala b/jvm/src/main/scala/scorex/util/ScorexLogging.scala new file mode 100644 index 0000000..65ab650 --- /dev/null +++ b/jvm/src/main/scala/scorex/util/ScorexLogging.scala @@ -0,0 +1,7 @@ +package scorex.util + +import com.typesafe.scalalogging.StrictLogging + +trait ScorexLogging extends StrictLogging { + @inline protected def log = logger +} \ No newline at end of file From 04d759e4ab4d0174e4fef385fd5ef569b5897e0b Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Wed, 16 Nov 2022 20:15:55 +0100 Subject: [PATCH 12/17] scala-js: test and publishing JS for Scala 2.12 --- .github/workflows/ci.yml | 2 +- build.sbt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8a1e28d..c30f9e3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -64,7 +64,7 @@ jobs: strategy: matrix: os: [ubuntu-latest] - scala: [2.13.8] + scala: [2.13.8, 2.12.15] java: [adopt@1.8] runs-on: ${{ matrix.os }} steps: diff --git a/build.sbt b/build.sbt index a53397f..e1802ac 100644 --- a/build.sbt +++ b/build.sbt @@ -175,7 +175,7 @@ lazy val core = crossProject(JSPlatform, JVMPlatform) ) .jsSettings( scalaVersion := scala213, - crossScalaVersions := Seq(scala213), + crossScalaVersions := Seq(scala212, scala213), libraryDependencies ++= Seq( // "org.typelevel" %%% "spire-macros" % "0.17.0" // Version supporting Scala.js 1.x 2.13, 3.x ), From 1eea07efcbfa67cae3bf4072924432cb90fdeaa0 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Wed, 22 Feb 2023 23:00:20 +0100 Subject: [PATCH 13/17] scala-js: cleanup build.sbt --- build.sbt | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/build.sbt b/build.sbt index e1802ac..520bbb7 100644 --- a/build.sbt +++ b/build.sbt @@ -99,6 +99,7 @@ lazy val scala211 = "2.11.12" crossScalaVersions := Seq(scala211, scala212, scala213) scalaVersion := scala212 +organization := "org.scorexfoundation" javacOptions ++= "-source" :: "1.8" :: @@ -106,7 +107,6 @@ javacOptions ++= Nil lazy val utilSettings = Seq( - organization := "org.scorexfoundation", licenses := Seq("CC0" -> url("https://creativecommons.org/publicdomain/zero/1.0/legalcode")), homepage := Some(url("http://github.com/ScorexFoundation/scorex-util")), description := "Common tools for scorex projects", @@ -170,14 +170,12 @@ lazy val core = crossProject(JSPlatform, JVMPlatform) crossScalaVersions := Seq(scala211, scala212, scala213), libraryDependencies ++= Seq( "com.typesafe.scala-logging" %%% "scala-logging" % "3.9.2" -// "org.typelevel" %%% "spire-macros" % "0.17.0-M1" // Version published for Scala 2.11-2.13 ), ) .jsSettings( scalaVersion := scala213, crossScalaVersions := Seq(scala212, scala213), libraryDependencies ++= Seq( -// "org.typelevel" %%% "spire-macros" % "0.17.0" // Version supporting Scala.js 1.x 2.13, 3.x ), Test / parallelExecution := false ) From a83460b1304e1c18f1b2e41bdc52c44ef90a04b1 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Wed, 22 Feb 2023 23:38:21 +0100 Subject: [PATCH 14/17] scala-js: ScalaDocs for ByteArrayBuilder --- .../scala/scorex/util/ByteArrayBuilder.scala | 59 ++++++++++++++----- 1 file changed, 45 insertions(+), 14 deletions(-) diff --git a/shared/src/main/scala/scorex/util/ByteArrayBuilder.scala b/shared/src/main/scala/scorex/util/ByteArrayBuilder.scala index e416138..f3e5df8 100644 --- a/shared/src/main/scala/scorex/util/ByteArrayBuilder.scala +++ b/shared/src/main/scala/scorex/util/ByteArrayBuilder.scala @@ -6,39 +6,36 @@ import java.util /** Similar to StringBuilder but works with underlying Array[Byte]. * Borrowed from https://github.com/odnoklassniki/one-nio/blob/master/src/one/nio/util/ByteArrayBuilder.java * Modifications of the underlying array is performed via ByteBuffer wrapper, so that saved bytes can - * be read back via ByteBuffer API. */ + * be read back via ByteBuffer API. + * @param initCapacity initial capacity of the underlying array + */ class ByteArrayBuilder(initCapacity: Int) { - protected var arr: Array[Byte] = null - protected var buf: ByteBuffer = null + private var arr: Array[Byte] = null + private var buf: ByteBuffer = null this.arr = new Array[Byte](initCapacity) this.buf = ByteBuffer.wrap(this.arr) def this() = this(256) + /** Returns the underlying array. */ final def array: Array[Byte] = arr + /** The length of the written part of the buffer (from 0 to the current position) */ final def length: Int = buf.position() + /** Sets the new position of the buffer. */ final def setLength(newPosition: Int): Unit = { buf.position(newPosition) } + /** Returns the length of the underlying array (buffer capacity). */ final def capacity(): Int = arr.length + /** Returns a byte at the given index in the buffer. */ final def byteAt(index: Int): Byte = arr(index) - final def crop(offset: Int): Unit = { - var count = buf.position() - if (offset < count) { - count -= offset - System.arraycopy(arr, offset, arr, 0, count) - } - else { - count = 0 - } - } - + /** Trims the underlying array to the current position. */ final def trim: Array[Byte] = { val count = buf.position() if (arr.length > count) { @@ -48,6 +45,7 @@ class ByteArrayBuilder(initCapacity: Int) { arr } + /** Retruns a new array with all the written bytes. */ final def toBytes: Array[Byte] = { val count = buf.position() val result = new Array[Byte](count) @@ -55,50 +53,83 @@ class ByteArrayBuilder(initCapacity: Int) { result } + /** Appends a byte to the buffer. + * @return this builder + */ final def append(b: Byte): ByteArrayBuilder = { ensureCapacity(1) buf.put(b) this } + /** Appends a byte array to the buffer. + * @return this builder + */ final def append(b: Array[Byte]): ByteArrayBuilder = append(b, 0, b.length) + /** Appends a part of a byte array to the buffer. + * @param b the byte array to append + * @param offset the offset of the first byte to append + * @param length the number of bytes to append + * @return this builder + */ final def append(b: Array[Byte], offset: Int, length: Int): ByteArrayBuilder = { ensureCapacity(length) buf.put(b, offset, length) this } + /** Appends a boolean to the buffer (true is encoded in the buffer as 0x01, false as 0x00). + * @param b the boolean to append + * @return this builder + */ final def append(b: Boolean): ByteArrayBuilder = { append((if (b) 0x01 else 0x00).toByte) this } + /** Appends a char to the buffer. + * @param c the char to append + * @return this builder + */ final def append(c: Char): ByteArrayBuilder = { ensureCapacity(1) buf.putChar(c) this } + /** Appends a short to the buffer. + * @param n the short to append + * @return this builder + */ final def append(n: Short): ByteArrayBuilder = { ensureCapacity(2) buf.putShort(n) this } + /** Appends an Int to the buffer. + * @param n the Int to append + * @return this builder + */ final def append(n: Int): ByteArrayBuilder = { ensureCapacity(4) buf.putInt(n) this } + /** Appends a Long to the buffer. + * @param n the Long to append + * @return this builder + */ final def append(n: Long): ByteArrayBuilder = { ensureCapacity(8) buf.putLong(n) this } + /** Ensures that the underlying array has enough capacity to append the given number of bytes. */ private def ensureCapacity(required: Int): Unit = { val count = buf.position() if (count + required > arr.length) { From 1409d3b8f0f410308e8e2871d65cac4fb958dbb4 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Wed, 22 Feb 2023 23:46:27 +0100 Subject: [PATCH 15/17] scala-js: more ScalaDocs --- shared/src/main/scala/scorex/util/Extensions.scala | 1 + shared/src/main/scala/scorex/util/encode/Base16.scala | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/shared/src/main/scala/scorex/util/Extensions.scala b/shared/src/main/scala/scorex/util/Extensions.scala index a778e82..050faf6 100644 --- a/shared/src/main/scala/scorex/util/Extensions.scala +++ b/shared/src/main/scala/scorex/util/Extensions.scala @@ -81,6 +81,7 @@ object Extensions { } } + /** Extension methods for `Iterable` type */ implicit class TraversableOps[A, Source[X] <: Iterable[X]](val xs: Source[A]) extends AnyVal { /** diff --git a/shared/src/main/scala/scorex/util/encode/Base16.scala b/shared/src/main/scala/scorex/util/encode/Base16.scala index a1ad8d7..0ee6bc0 100644 --- a/shared/src/main/scala/scorex/util/encode/Base16.scala +++ b/shared/src/main/scala/scorex/util/encode/Base16.scala @@ -33,6 +33,10 @@ object Base16 extends BytesEncoder { new String(buf) } + /** Decodes a hex encoded string into a byte array. + * @param input - hex encoded string + * @return - decoded byte array + */ def decode(input: String): Try[Array[Byte]] = { var isError = false var errorMsg = "" From f55e90a916ed69d531fb9bad87e7c78fcc274e3b Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Sun, 26 Feb 2023 13:02:50 +0100 Subject: [PATCH 16/17] scala-js: fix snapshot publishing --- build.sbt | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/build.sbt b/build.sbt index 520bbb7..9d9c5a9 100644 --- a/build.sbt +++ b/build.sbt @@ -107,6 +107,7 @@ javacOptions ++= Nil lazy val utilSettings = Seq( + organization := "org.scorexfoundation", licenses := Seq("CC0" -> url("https://creativecommons.org/publicdomain/zero/1.0/legalcode")), homepage := Some(url("http://github.com/ScorexFoundation/scorex-util")), description := "Common tools for scorex projects", @@ -132,9 +133,8 @@ lazy val utilSettings = Seq( scalac ++ scalac211 } }, - + javacOptions ++= javacReleaseOption, publishMavenStyle := true, - publishTo := sonatypePublishToBundle.value, pomExtra := ( @@ -160,10 +160,7 @@ lazy val utilSettings = Seq( lazy val core = crossProject(JSPlatform, JVMPlatform) .in(file(".")) - .settings( - name := "scorex-util", - moduleName := "scorex-util" - ) + .settings(moduleName := "scorex-util") .settings(utilSettings) .jvmSettings( scalaVersion := scala213, @@ -180,14 +177,22 @@ lazy val core = crossProject(JSPlatform, JVMPlatform) Test / parallelExecution := false ) -Test / publishArtifact := true -pomIncludeRepository := { _ => false } +def javacReleaseOption = { + if (System.getProperty("java.version").startsWith("1.")) + // java <9 "--release" is not supported + Seq() + else + Seq("--release", "8") +} // prefix version with "-SNAPSHOT" for builds without a git tag ThisBuild / dynverSonatypeSnapshots := true // use "-" instead of default "+" ThisBuild / dynverSeparator := "-" +Test / publishArtifact := true +pomIncludeRepository := { _ => false } + credentials ++= (for { username <- Option(System.getenv().get("SONATYPE_USERNAME")) password <- Option(System.getenv().get("SONATYPE_PASSWORD")) From abae86ae846cd3cea00228c836b05e872fa99939 Mon Sep 17 00:00:00 2001 From: Alexander Slesarenko Date: Sun, 26 Feb 2023 13:15:10 +0100 Subject: [PATCH 17/17] scala-js: fix release publishing --- .github/workflows/release.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 584e7bf..1f5deff 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -37,7 +37,7 @@ jobs: GPG_SIGNING_KEY: ${{ secrets.GPG_SIGNING_KEY }} - name: Publish release ${{ github.ref }} - run: sbt +publishSigned sonatypeBundleRelease + run: sbt +coreJVM/publishSigned +coreJS/publishSigned sonatypeBundleRelease env: PGP_PASSPHRASE: ${{ secrets.PGP_PASSPHRASE }} SONATYPE_PASSWORD: ${{ secrets.SONATYPE_PASSWORD }}