Skip to content

Commit 4860368

Browse files
authored
[ETCM-522][ETCM-523] Apply scalafix scalafmt rules (#1034)
* [ETCM-522][ETCM-523] Add scalafix and add scalafmt rules * [ETCM-522][ETCM-523] Add scalafixAndFmt .buildkite step * [ETCM-522][ETCM-523] Update depsSha256 * [ETCM-522][ETCM-523] Apply scalafix and scalafmt * [ETCM-522][ETCM-523] Set Xmx for CI to 8g
1 parent cbf21d2 commit 4860368

File tree

615 files changed

+9744
-8070
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

615 files changed

+9744
-8070
lines changed

.buildkite/pipeline.nix

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@ in
2020
];
2121
};
2222

23-
scalafmt = commonAttrs // {
24-
label = "scalafmtCheck";
23+
scalafixAndFmt = commonAttrs // {
24+
label = "scalafix & scalafmt";
2525
command = ''
26-
nix-shell --run '$SBT scalafmtCheck'
26+
nix-shell --run '$SBT formatCheck'
2727
'';
2828
retry.automatic = false;
2929
};
3030

3131
compile = commonAttrs // {
3232
label = "compile everything";
33-
dependsOn = [ scalafmt ];
33+
dependsOn = [ scalafixAndFmt ];
3434
command = ''
3535
nix-shell --run '$SBT compile-all'
3636
'';

.buildkite/shell.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ with pkgs;
2121
mkShell {
2222
nativeBuildInputs = [ sbt solc lllc jdk8 protoc-wrapper retesteth netcat-gnu ];
2323
# SBT = "sbt -v -mem 2048 -J-Xmx4g -Dsbt.ivy.home=/cache/ivy2 -Dsbt.boot.directory=/cache/sbt -Dmaven.repo.local=/cache/maven -Dnix=true";
24-
SBT = "sbt -v -mem 2048 -J-Xmx6g -Dnix=true";
24+
SBT = "sbt -v -mem 8192 -Dnix=true";
2525
}

.jvmopts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
-Xms1g
22
-Xmx4g
3-
-XX:ReservedCodeCacheSize=256m
3+
-XX:ReservedCodeCacheSize=1024m
44
-XX:MaxMetaspaceSize=1g
55
-Xss4M

.scalafix.conf

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
rules = [
2+
ExplicitResultTypes
3+
NoAutoTupling
4+
NoValInForComprehension
5+
OrganizeImports
6+
ProcedureSyntax
7+
RemoveUnused
8+
]
9+
10+
OrganizeImports {
11+
groupedImports = Explode
12+
groups = [
13+
"re:javax?\\."
14+
"akka."
15+
"cats."
16+
"monix."
17+
"scala."
18+
"scala.meta."
19+
"*"
20+
"io.iohk.ethereum."
21+
]
22+
removeUnused = true
23+
}
24+
25+
RemoveUnused {
26+
imports = false // handled by OrganizeImports
27+
}

.scalafmt.conf

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
version = "2.7.1"
2-
align = none
1+
version = "2.7.5"
2+
align.preset = some
33
maxColumn = 120
4+
5+
rewrite.rules = [AvoidInfix, RedundantBraces, RedundantParens, SortModifiers]

build.sbt

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ def commonSettings(projectName: String): Seq[sbt.Def.Setting[_]] = Seq(
4545
name := projectName,
4646
organization := "io.iohk",
4747
scalaVersion := `scala-2.13`,
48+
semanticdbEnabled := true, // enable SemanticDB
49+
semanticdbVersion := scalafixSemanticdb.revision, // use Scalafix compatible version
50+
ThisBuild / scalafixScalaBinaryVersion := CrossVersion.binaryScalaVersion(scalaVersion.value),
51+
ThisBuild / scalafixDependencies ++= List(
52+
"com.github.liancheng" %% "organize-imports" % "0.5.0",
53+
"com.github.vovapolu" %% "scaluzzi" % "0.1.16"
54+
),
4855
// Scalanet snapshots are published to Sonatype after each build.
4956
resolvers += "Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots",
5057
testOptions in Test += Tests
@@ -53,7 +60,9 @@ def commonSettings(projectName: String): Seq[sbt.Def.Setting[_]] = Seq(
5360
"-unchecked",
5461
"-deprecation",
5562
"-feature",
56-
"-Xfatal-warnings",
63+
// "-Xfatal-warnings", // disabled until unused are removed
64+
"-Ywarn-unused",
65+
"-Xlint",
5766
"-encoding",
5867
"utf-8"
5968
),
@@ -86,6 +95,7 @@ lazy val bytes = {
8695
.in(file("bytes"))
8796
.configs(Integration)
8897
.settings(commonSettings("mantis-bytes"))
98+
.settings(inConfig(Integration)(scalafixConfigSettings(Integration)))
8999
.settings(publishSettings)
90100
.settings(
91101
libraryDependencies ++=
@@ -102,6 +112,7 @@ lazy val crypto = {
102112
.configs(Integration)
103113
.dependsOn(bytes)
104114
.settings(commonSettings("mantis-crypto"))
115+
.settings(inConfig(Integration)(scalafixConfigSettings(Integration)))
105116
.settings(publishSettings)
106117
.settings(
107118
libraryDependencies ++=
@@ -119,6 +130,7 @@ lazy val rlp = {
119130
.configs(Integration)
120131
.dependsOn(bytes)
121132
.settings(commonSettings("mantis-rlp"))
133+
.settings(inConfig(Integration)(scalafixConfigSettings(Integration)))
122134
.settings(publishSettings)
123135
.settings(
124136
libraryDependencies ++=
@@ -176,8 +188,6 @@ lazy val node = {
176188
(test in Evm) := (test in Evm).dependsOn(solidityCompile).value
177189
(sourceDirectory in Evm) := baseDirectory.value / "src" / "evmTest"
178190

179-
val sep = java.io.File.separator
180-
181191
val node = project
182192
.in(file("."))
183193
.configs(Integration, Benchmark, Evm, Rpc)
@@ -201,6 +211,9 @@ lazy val node = {
201211
buildInfoOptions in Compile += BuildInfoOption.ToMap
202212
)
203213
.settings(commonSettings("mantis"): _*)
214+
.settings(inConfig(Integration)(scalafixConfigSettings(Integration)))
215+
.settings(inConfig(Evm)(scalafixConfigSettings(Evm)))
216+
.settings(inConfig(Rpc)(scalafixConfigSettings(Rpc)))
204217
.settings(
205218
libraryDependencies ++= dep
206219
)
@@ -290,6 +303,36 @@ addCommandAlias(
290303
|""".stripMargin
291304
)
292305

306+
// format all modules
307+
addCommandAlias(
308+
"formatAll",
309+
""";compile-all
310+
|;bytes/scalafixAll
311+
|;bytes/scalafmtAll
312+
|;crypto/scalafixAll
313+
|;crypto/scalafmtAll
314+
|;rlp/scalafixAll
315+
|;rlp/scalafmtAll
316+
|;scalafixAll
317+
|;scalafmtAll
318+
|""".stripMargin
319+
)
320+
321+
// check modules formatting
322+
addCommandAlias(
323+
"formatCheck",
324+
""";compile-all
325+
|;bytes/scalafixAll --check
326+
|;bytes/scalafmtCheckAll
327+
|;crypto/scalafixAll --check
328+
|;crypto/scalafmtCheckAll
329+
|;rlp/scalafixAll --check
330+
|;rlp/scalafmtCheckAll
331+
|;scalafixAll --check
332+
|;scalafmtCheckAll
333+
|""".stripMargin
334+
)
335+
293336
// testAll
294337
addCommandAlias(
295338
"testAll",

bytes/src/main/scala/io/iohk/ethereum/utils/ByteStringUtils.scala

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ object ByteStringUtils {
1414
ByteString(Hex.decode(hash))
1515

1616
implicit class Padding(val bs: ByteString) extends AnyVal {
17-
def padToByteString(length: Int, b: Byte): ByteString = {
17+
def padToByteString(length: Int, b: Byte): ByteString =
1818
if (length <= bs.length) bs
1919
else {
2020
val len = Math.max(bs.length, length)
@@ -27,7 +27,6 @@ object ByteStringUtils {
2727
}
2828
ByteString.fromArray(result)
2929
}
30-
}
3130
}
3231

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

57-
implicit val byteStringOrdering: Ordering[ByteString] = {
56+
implicit val byteStringOrdering: Ordering[ByteString] =
5857
Ordering.by[ByteString, Seq[Byte]](_.toSeq)
59-
}
6058

6159
def concatByteStrings(head: ByteStringElement, tail: ByteStringElement*): ByteString = {
6260
val it = Iterator.single(head) ++ tail.iterator

bytes/src/main/scala/io/iohk/ethereum/utils/ByteUtils.scala

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
package io.iohk.ethereum.utils
22

33
import java.math.BigInteger
4-
import java.nio.{ByteBuffer, ByteOrder}
4+
import java.nio.ByteBuffer
5+
import java.nio.ByteOrder
56

67
import akka.util.ByteString
78

89
import scala.util.Random
910

1011
object ByteUtils {
1112

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

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

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

4342
def bigIntToUnsignedByteArray(i: BigInt): Array[Byte] = {
4443
val asByteArray = i.toByteArray
4544
if (asByteArray.head == 0) asByteArray.tail
4645
else asByteArray
4746
}
4847

49-
/**
50-
* Calculates xor distance between two byte arrays. Due to performance reasons needs to be as fast as possible
48+
/** Calculates xor distance between two byte arrays. Due to performance reasons needs to be as fast as possible
5149
* which means usage of while loops and var's.
5250
*
5351
* @param a - array of bytes to xor
@@ -108,9 +106,8 @@ object ByteUtils {
108106
data
109107
}
110108

111-
def compactPickledBytes(buffer: ByteBuffer): ByteString = {
109+
def compactPickledBytes(buffer: ByteBuffer): ByteString =
112110
ByteString(compactPickledBytesToArray(buffer))
113-
}
114111

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

130-
def getIntFromWord(arr: Array[Byte]): Int = {
127+
def getIntFromWord(arr: Array[Byte]): Int =
131128
ByteBuffer.wrap(arr, 0, 4).order(ByteOrder.LITTLE_ENDIAN).getInt
132-
}
133129

134-
/**
135-
* Converts array of Int to corresponding array of bytes. Due to performance reasons needs to be as fast as possible
130+
/** Converts array of Int to corresponding array of bytes. Due to performance reasons needs to be as fast as possible
136131
* which means usage of while loops and var's.
137132
*
138133
* @param arr - array of int's to convert
@@ -141,7 +136,7 @@ object ByteUtils {
141136
* @param bigEndian - param specifying which int representation should be used.
142137
* @return Unit
143138
*/
144-
def intsToBytesMut(arr: Array[Int], b: Array[Byte], bigEndian: Boolean): Unit = {
139+
def intsToBytesMut(arr: Array[Int], b: Array[Byte], bigEndian: Boolean): Unit =
145140
if (!bigEndian) {
146141
var off = 0
147142
var i = 0
@@ -175,10 +170,8 @@ object ByteUtils {
175170
i = i + 1
176171
}
177172
}
178-
}
179173

180-
/**
181-
* Converts array of bytes to corresponding array of ints. Due to performance reasons needs to be as fast as possible
174+
/** Converts array of bytes to corresponding array of ints. Due to performance reasons needs to be as fast as possible
182175
* which means usage of while loops and var's.
183176
*
184177
* @param b - array of bytes to convert
@@ -187,7 +180,7 @@ object ByteUtils {
187180
* @param bigEndian - param specifying which int representation should be used.
188181
* @return Unit
189182
*/
190-
def bytesToIntsMut(b: Array[Byte], arr: Array[Int], bigEndian: Boolean): Unit = {
183+
def bytesToIntsMut(b: Array[Byte], arr: Array[Int], bigEndian: Boolean): Unit =
191184
if (!bigEndian) {
192185
var off = 0
193186
var i = 0
@@ -222,6 +215,5 @@ object ByteUtils {
222215
i = i + 1
223216
}
224217
}
225-
}
226218

227219
}

bytes/src/test/scala/io/iohk/ethereum/utils/ByteStringUtilsTest.scala

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
package io.iohk.ethereum.utils
22

33
import akka.util.ByteString
4-
import org.scalatest.wordspec.AnyWordSpec
4+
5+
import scala.collection.immutable.ArraySeq
6+
import scala.util.Failure
7+
import scala.util.Success
8+
import scala.util.Try
9+
510
import org.scalatest.matchers.should.Matchers
11+
import org.scalatest.wordspec.AnyWordSpec
12+
613
import ByteStringUtils._
7-
import scala.collection.immutable.ArraySeq
8-
import scala.util.{Try, Success, Failure}
914

1015
class ByteStringUtilsTest extends AnyWordSpec with Matchers {
1116

@@ -39,7 +44,7 @@ class ByteStringUtilsTest extends AnyWordSpec with Matchers {
3944
val bs3: Byte = 2
4045
val bs4 = Array[Byte](3, 3)
4146
val bs5 = Array[Byte](4, 4)
42-
val summarized: ByteString = bs1 ++ bs2
47+
bs1 ++ bs2
4348
val concatenated: ByteString = ByteStringUtils.concatByteStrings(bs1, bs2, bs3, bs4, bs5)
4449
concatenated shouldEqual string2hash("0000FFFF0203030404")
4550
}
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package io.iohk.ethereum.utils
22

3-
import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks
3+
import org.scalacheck.Arbitrary
4+
import org.scalacheck.Gen
45
import org.scalatest.funsuite.AnyFunSuite
5-
import org.scalacheck.{Arbitrary, Gen}
6+
import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks
67

78
class ByteUtilsSpec extends AnyFunSuite with ScalaCheckPropertyChecks {
89
def byteArrayOfNItemsGen(n: Int): Gen[Array[Byte]] =
@@ -12,15 +13,15 @@ class ByteUtilsSpec extends AnyFunSuite with ScalaCheckPropertyChecks {
1213
forAll(byteArrayOfNItemsGen(32)) { bytes =>
1314
val toInts = ByteUtils.bytesToInts(bytes, bigEndian = false)
1415
val asBytes = ByteUtils.intsToBytes(toInts, bigEndian = false)
15-
assert(asBytes sameElements bytes)
16+
assert(asBytes.sameElements(bytes))
1617
}
1718
}
1819

1920
test("Convert Bytes to Int in big endian") {
2021
forAll(byteArrayOfNItemsGen(32)) { bytes =>
2122
val toInts = ByteUtils.bytesToInts(bytes, bigEndian = true)
2223
val asBytes = ByteUtils.intsToBytes(toInts, bigEndian = true)
23-
assert(asBytes sameElements bytes)
24+
assert(asBytes.sameElements(bytes))
2425
}
2526
}
2627
}

0 commit comments

Comments
 (0)