Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ETCM-522][ETCM-523] Apply scalafix scalafmt rules #1034

Merged
merged 5 commits into from
Jul 2, 2021
Merged
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
8 changes: 4 additions & 4 deletions .buildkite/pipeline.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ in
];
};

scalafmt = commonAttrs // {
label = "scalafmtCheck";
scalafixAndFmt = commonAttrs // {
label = "scalafix & scalafmt";
command = ''
nix-shell --run '$SBT scalafmtCheck'
nix-shell --run '$SBT formatCheck'
'';
retry.automatic = false;
};

compile = commonAttrs // {
label = "compile everything";
dependsOn = [ scalafmt ];
dependsOn = [ scalafixAndFmt ];
command = ''
nix-shell --run '$SBT compile-all'
'';
Expand Down
2 changes: 1 addition & 1 deletion .buildkite/shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ with pkgs;
mkShell {
nativeBuildInputs = [ sbt solc lllc jdk8 protoc-wrapper retesteth netcat-gnu ];
# SBT = "sbt -v -mem 2048 -J-Xmx4g -Dsbt.ivy.home=/cache/ivy2 -Dsbt.boot.directory=/cache/sbt -Dmaven.repo.local=/cache/maven -Dnix=true";
SBT = "sbt -v -mem 2048 -J-Xmx6g -Dnix=true";
SBT = "sbt -v -mem 8192 -Dnix=true";
}
2 changes: 1 addition & 1 deletion .jvmopts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-Xms1g
-Xmx4g
-XX:ReservedCodeCacheSize=256m
-XX:ReservedCodeCacheSize=1024m
-XX:MaxMetaspaceSize=1g
-Xss4M
27 changes: 27 additions & 0 deletions .scalafix.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
rules = [
ExplicitResultTypes
NoAutoTupling
NoValInForComprehension
OrganizeImports
ProcedureSyntax
RemoveUnused
]

OrganizeImports {
groupedImports = Explode
groups = [
"re:javax?\\."
"akka."
"cats."
"monix."
"scala."
"scala.meta."
"*"
"io.iohk.ethereum."
]
removeUnused = true
}

RemoveUnused {
imports = false // handled by OrganizeImports
}
6 changes: 4 additions & 2 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
version = "2.7.1"
align = none
version = "2.7.5"
align.preset = some
maxColumn = 120

rewrite.rules = [AvoidInfix, RedundantBraces, RedundantParens, SortModifiers]
49 changes: 46 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ def commonSettings(projectName: String): Seq[sbt.Def.Setting[_]] = Seq(
name := projectName,
organization := "io.iohk",
scalaVersion := `scala-2.13`,
semanticdbEnabled := true, // enable SemanticDB
semanticdbVersion := scalafixSemanticdb.revision, // use Scalafix compatible version
ThisBuild / scalafixScalaBinaryVersion := CrossVersion.binaryScalaVersion(scalaVersion.value),
ThisBuild / scalafixDependencies ++= List(
"com.github.liancheng" %% "organize-imports" % "0.5.0",
"com.github.vovapolu" %% "scaluzzi" % "0.1.16"
),
// Scalanet snapshots are published to Sonatype after each build.
resolvers += "Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots",
testOptions in Test += Tests
Expand All @@ -53,7 +60,9 @@ def commonSettings(projectName: String): Seq[sbt.Def.Setting[_]] = Seq(
"-unchecked",
"-deprecation",
"-feature",
"-Xfatal-warnings",
// "-Xfatal-warnings", // disabled until unused are removed
"-Ywarn-unused",
"-Xlint",
"-encoding",
"utf-8"
),
Expand Down Expand Up @@ -86,6 +95,7 @@ lazy val bytes = {
.in(file("bytes"))
.configs(Integration)
.settings(commonSettings("mantis-bytes"))
.settings(inConfig(Integration)(scalafixConfigSettings(Integration)))
.settings(publishSettings)
.settings(
libraryDependencies ++=
Expand All @@ -102,6 +112,7 @@ lazy val crypto = {
.configs(Integration)
.dependsOn(bytes)
.settings(commonSettings("mantis-crypto"))
.settings(inConfig(Integration)(scalafixConfigSettings(Integration)))
.settings(publishSettings)
.settings(
libraryDependencies ++=
Expand All @@ -119,6 +130,7 @@ lazy val rlp = {
.configs(Integration)
.dependsOn(bytes)
.settings(commonSettings("mantis-rlp"))
.settings(inConfig(Integration)(scalafixConfigSettings(Integration)))
.settings(publishSettings)
.settings(
libraryDependencies ++=
Expand Down Expand Up @@ -176,8 +188,6 @@ lazy val node = {
(test in Evm) := (test in Evm).dependsOn(solidityCompile).value
(sourceDirectory in Evm) := baseDirectory.value / "src" / "evmTest"

val sep = java.io.File.separator

val node = project
.in(file("."))
.configs(Integration, Benchmark, Evm, Rpc)
Expand All @@ -201,6 +211,9 @@ lazy val node = {
buildInfoOptions in Compile += BuildInfoOption.ToMap
)
.settings(commonSettings("mantis"): _*)
.settings(inConfig(Integration)(scalafixConfigSettings(Integration)))
.settings(inConfig(Evm)(scalafixConfigSettings(Evm)))
.settings(inConfig(Rpc)(scalafixConfigSettings(Rpc)))
.settings(
libraryDependencies ++= dep
)
Expand Down Expand Up @@ -290,6 +303,36 @@ addCommandAlias(
|""".stripMargin
)

// format all modules
addCommandAlias(
"formatAll",
""";compile-all
|;bytes/scalafixAll
|;bytes/scalafmtAll
|;crypto/scalafixAll
|;crypto/scalafmtAll
|;rlp/scalafixAll
|;rlp/scalafmtAll
|;scalafixAll
|;scalafmtAll
|""".stripMargin
)

// check modules formatting
addCommandAlias(
"formatCheck",
""";compile-all
|;bytes/scalafixAll --check
|;bytes/scalafmtCheckAll
|;crypto/scalafixAll --check
|;crypto/scalafmtCheckAll
|;rlp/scalafixAll --check
|;rlp/scalafmtCheckAll
|;scalafixAll --check
|;scalafmtCheckAll
|""".stripMargin
)

// testAll
addCommandAlias(
"testAll",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ object ByteStringUtils {
ByteString(Hex.decode(hash))

implicit class Padding(val bs: ByteString) extends AnyVal {
def padToByteString(length: Int, b: Byte): ByteString = {
def padToByteString(length: Int, b: Byte): ByteString =
if (length <= bs.length) bs
else {
val len = Math.max(bs.length, length)
Expand All @@ -27,7 +27,6 @@ object ByteStringUtils {
}
ByteString.fromArray(result)
}
}
}

implicit class ByteStringOps(val bytes: ByteString) extends AnyVal {
Expand All @@ -54,9 +53,8 @@ object ByteStringUtils {
def asByteArray: Array[Byte] = Array(b)
}

implicit val byteStringOrdering: Ordering[ByteString] = {
implicit val byteStringOrdering: Ordering[ByteString] =
Ordering.by[ByteString, Seq[Byte]](_.toSeq)
}

def concatByteStrings(head: ByteStringElement, tail: ByteStringElement*): ByteString = {
val it = Iterator.single(head) ++ tail.iterator
Expand Down
32 changes: 12 additions & 20 deletions bytes/src/main/scala/io/iohk/ethereum/utils/ByteUtils.scala
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package io.iohk.ethereum.utils

import java.math.BigInteger
import java.nio.{ByteBuffer, ByteOrder}
import java.nio.ByteBuffer
import java.nio.ByteOrder

import akka.util.ByteString

import scala.util.Random

object ByteUtils {

/**
* Calculates number of matching bytes from the beginning of both arrays.
/** Calculates number of matching bytes from the beginning of both arrays.
* Due to performance reasons needs to be as fast as possible which means usage of while loops and var's.
*
* @param a - first array of bytes to check
Expand All @@ -19,9 +19,8 @@ object ByteUtils {
*/
def matchingLength(a: Array[Byte], b: Array[Byte]): Int = {
var prefixLen = 0
while (prefixLen < a.length && prefixLen < b.length && a(prefixLen) == b(prefixLen)) {
while (prefixLen < a.length && prefixLen < b.length && a(prefixLen) == b(prefixLen))
prefixLen = prefixLen + 1
}
prefixLen
}

Expand All @@ -38,16 +37,15 @@ object ByteUtils {
bigIntegerToBytes(b.bigInteger, numBytes)

def toBigInt(bytes: ByteString): BigInt =
bytes.foldLeft(BigInt(0)) { (n, b) => (n << 8) + (b & 0xff) }
bytes.foldLeft(BigInt(0))((n, b) => (n << 8) + (b & 0xff))

def bigIntToUnsignedByteArray(i: BigInt): Array[Byte] = {
val asByteArray = i.toByteArray
if (asByteArray.head == 0) asByteArray.tail
else asByteArray
}

/**
* Calculates xor distance between two byte arrays. Due to performance reasons needs to be as fast as possible
/** Calculates xor distance between two byte arrays. Due to performance reasons needs to be as fast as possible
* which means usage of while loops and var's.
*
* @param a - array of bytes to xor
Expand Down Expand Up @@ -108,9 +106,8 @@ object ByteUtils {
data
}

def compactPickledBytes(buffer: ByteBuffer): ByteString = {
def compactPickledBytes(buffer: ByteBuffer): ByteString =
ByteString(compactPickledBytesToArray(buffer))
}

def byteSequenceToBuffer(bytes: IndexedSeq[Byte]): ByteBuffer =
ByteBuffer.wrap(bytes.toArray)
Expand All @@ -127,12 +124,10 @@ object ByteUtils {
ret
}

def getIntFromWord(arr: Array[Byte]): Int = {
def getIntFromWord(arr: Array[Byte]): Int =
ByteBuffer.wrap(arr, 0, 4).order(ByteOrder.LITTLE_ENDIAN).getInt
}

/**
* Converts array of Int to corresponding array of bytes. Due to performance reasons needs to be as fast as possible
/** Converts array of Int to corresponding array of bytes. Due to performance reasons needs to be as fast as possible
* which means usage of while loops and var's.
*
* @param arr - array of int's to convert
Expand All @@ -141,7 +136,7 @@ object ByteUtils {
* @param bigEndian - param specifying which int representation should be used.
* @return Unit
*/
def intsToBytesMut(arr: Array[Int], b: Array[Byte], bigEndian: Boolean): Unit = {
def intsToBytesMut(arr: Array[Int], b: Array[Byte], bigEndian: Boolean): Unit =
if (!bigEndian) {
var off = 0
var i = 0
Expand Down Expand Up @@ -175,10 +170,8 @@ object ByteUtils {
i = i + 1
}
}
}

/**
* Converts array of bytes to corresponding array of ints. Due to performance reasons needs to be as fast as possible
/** Converts array of bytes to corresponding array of ints. Due to performance reasons needs to be as fast as possible
* which means usage of while loops and var's.
*
* @param b - array of bytes to convert
Expand All @@ -187,7 +180,7 @@ object ByteUtils {
* @param bigEndian - param specifying which int representation should be used.
* @return Unit
*/
def bytesToIntsMut(b: Array[Byte], arr: Array[Int], bigEndian: Boolean): Unit = {
def bytesToIntsMut(b: Array[Byte], arr: Array[Int], bigEndian: Boolean): Unit =
if (!bigEndian) {
var off = 0
var i = 0
Expand Down Expand Up @@ -222,6 +215,5 @@ object ByteUtils {
i = i + 1
}
}
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package io.iohk.ethereum.utils

import akka.util.ByteString
import org.scalatest.wordspec.AnyWordSpec

import scala.collection.immutable.ArraySeq
import scala.util.Failure
import scala.util.Success
import scala.util.Try

import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec

import ByteStringUtils._
import scala.collection.immutable.ArraySeq
import scala.util.{Try, Success, Failure}

class ByteStringUtilsTest extends AnyWordSpec with Matchers {

Expand Down Expand Up @@ -39,7 +44,7 @@ class ByteStringUtilsTest extends AnyWordSpec with Matchers {
val bs3: Byte = 2
val bs4 = Array[Byte](3, 3)
val bs5 = Array[Byte](4, 4)
val summarized: ByteString = bs1 ++ bs2
bs1 ++ bs2
val concatenated: ByteString = ByteStringUtils.concatByteStrings(bs1, bs2, bs3, bs4, bs5)
concatenated shouldEqual string2hash("0000FFFF0203030404")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package io.iohk.ethereum.utils

import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks
import org.scalacheck.Arbitrary
import org.scalacheck.Gen
import org.scalatest.funsuite.AnyFunSuite
import org.scalacheck.{Arbitrary, Gen}
import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks

class ByteUtilsSpec extends AnyFunSuite with ScalaCheckPropertyChecks {
def byteArrayOfNItemsGen(n: Int): Gen[Array[Byte]] =
Expand All @@ -12,15 +13,15 @@ class ByteUtilsSpec extends AnyFunSuite with ScalaCheckPropertyChecks {
forAll(byteArrayOfNItemsGen(32)) { bytes =>
val toInts = ByteUtils.bytesToInts(bytes, bigEndian = false)
val asBytes = ByteUtils.intsToBytes(toInts, bigEndian = false)
assert(asBytes sameElements bytes)
assert(asBytes.sameElements(bytes))
}
}

test("Convert Bytes to Int in big endian") {
forAll(byteArrayOfNItemsGen(32)) { bytes =>
val toInts = ByteUtils.bytesToInts(bytes, bigEndian = true)
val asBytes = ByteUtils.intsToBytes(toInts, bigEndian = true)
assert(asBytes sameElements bytes)
assert(asBytes.sameElements(bytes))
}
}
}
Loading