Skip to content

Commit c380e5e

Browse files
committed
ETCM-165: Separate bytes project.
1 parent e5376be commit c380e5e

File tree

7 files changed

+63
-37
lines changed

7 files changed

+63
-37
lines changed

.buildkite/pipeline.nix

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,19 @@ in
6262
'';
6363
};
6464

65+
test-bytes = commonAttrs // {
66+
dependsOn = [ compile ];
67+
label = "bytes tests";
68+
command = ''
69+
nix-shell --run '$SBT coverage bytes/test'
70+
'';
71+
artifactPaths = [
72+
"bytes/target/test-reports/**/*"
73+
"bytes/target/scala-2.12/scoverage-report/**/*"
74+
"bytes/target/scala-2.12/coverage-report/**/*"
75+
];
76+
};
77+
6578
test-rlp = commonAttrs // {
6679
dependsOn = [ compile ];
6780
label = "RLP tests";

build.sbt

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ def commonSettings(projectName: String): Seq[sbt.Def.Setting[_]] = Seq(
1818
resolvers += "Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots",
1919
testOptions in Test += Tests
2020
.Argument(TestFrameworks.ScalaTest, "-l", "EthashMinerSpec"), // miner tests disabled by default,
21-
2221
scalacOptions := Seq(
2322
"-unchecked",
2423
"-deprecation",
@@ -31,38 +30,50 @@ def commonSettings(projectName: String): Seq[sbt.Def.Setting[_]] = Seq(
3130
"-encoding",
3231
"utf-8"
3332
),
34-
3533
scalacOptions in (Compile, console) ~= (_.filterNot(
3634
Set(
3735
"-Ywarn-unused-import",
3836
"-Xfatal-warnings"
3937
)
4038
)),
41-
4239
scalacOptions ~= (options => if (mantisDev) options.filterNot(_ == "-Xfatal-warnings") else options),
43-
4440
Test / parallelExecution := true,
45-
4641
testOptions in Test += Tests.Argument("-oDG"),
47-
4842
(scalastyleConfig in Test) := file("scalastyle-test-config.xml")
4943
)
5044

51-
lazy val rlp = {
45+
lazy val bytes = {
5246
// Adding an "it" config because in `Dependencies.scala` some are declared with `% "it,test"`
5347
// which would fail if the project didn't have configuration to add to.
5448
val Integration = config("it") extend Test
5549

50+
val bytes = project
51+
.in(file("bytes"))
52+
.configs(Integration)
53+
.settings(commonSettings("mantis-bytes"))
54+
.settings(
55+
libraryDependencies ++=
56+
Dependencies.akkaUtil ++
57+
Dependencies.testing
58+
)
59+
60+
bytes
61+
}
62+
63+
lazy val rlp = {
64+
val Integration = config("it") extend Test
65+
5666
val rlp = project
57-
.in(file("rlp"))
58-
.configs(Integration)
59-
.settings(commonSettings("mantis-rlp"))
60-
.settings(
61-
libraryDependencies ++=
62-
Dependencies.akkaUtil ++
63-
Dependencies.shapeless ++
64-
Dependencies.testing
65-
)
67+
.in(file("rlp"))
68+
.configs(Integration)
69+
.dependsOn(bytes)
70+
.settings(commonSettings("mantis-rlp"))
71+
.settings(
72+
libraryDependencies ++=
73+
Dependencies.akkaUtil ++
74+
Dependencies.shapeless ++
75+
Dependencies.testing
76+
)
6677

6778
rlp
6879
}
@@ -124,7 +135,7 @@ lazy val node = {
124135
.in(file("."))
125136
.configs(Integration, Benchmark, Evm, Ets, Snappy, Rpc)
126137
.enablePlugins(BuildInfoPlugin)
127-
.dependsOn(rlp)
138+
.dependsOn(bytes, rlp)
128139
.settings(
129140
buildInfoKeys := Seq[BuildInfoKey](name, version, git.gitHeadCommit),
130141
buildInfoPackage := "io.iohk.ethereum.utils"
@@ -134,7 +145,7 @@ lazy val node = {
134145
libraryDependencies ++= dep
135146
)
136147
.settings(
137-
executableScriptName := name.value,
148+
executableScriptName := name.value
138149
)
139150
.settings(
140151
inConfig(Integration)(
@@ -153,23 +164,19 @@ lazy val node = {
153164
PB.targets in Compile := Seq(
154165
scalapb.gen() -> (sourceManaged in Compile).value / "protobuf"
155166
),
156-
157167
// have the protobuf API version file as a resource
158168
unmanagedResourceDirectories in Compile += baseDirectory.value / "src" / "main" / "protobuf",
159-
160169
// Packaging
161170
mainClass in Compile := Some("io.iohk.ethereum.App"),
162171
discoveredMainClasses in Compile := Seq(),
163172
// Requires the 'ant-javafx.jar' that comes with Oracle JDK
164173
// Enables creating an executable with the configuration files, has to be run on the OS corresponding to the desired version
165174
ThisBuild / jdkPackagerType := "image",
166-
167175
mappings in Universal += (resourceDirectory in Compile).value / "logback.xml" -> "conf/logback.xml",
168176
mappings in Universal += (resourceDirectory in Compile).value / "application.conf" -> "conf/base.conf",
169177
mappings in Universal ++= directory((resourceDirectory in Compile).value / "chains").map { case (f, name) =>
170178
f -> s"conf/$name"
171179
},
172-
173180
jdkPackagerJVMArgs := Seq(
174181
"-Dconfig.file=." + sep + "conf" + sep + "app.conf",
175182
"-Dlogback.configurationFile=." + sep + "conf" + sep + "logback.xml",
@@ -187,7 +194,9 @@ coverageExcludedPackages := "io\\.iohk\\.ethereum\\.extvm\\.msg.*"
187194

188195
addCommandAlias(
189196
"compile-all",
190-
""";rlp/compile
197+
""";bytes/compile
198+
|;bytes/test:compile
199+
|;rlp/compile
191200
|;rlp/test:compile
192201
|;compile
193202
|;test:compile
@@ -204,6 +213,9 @@ addCommandAlias(
204213
addCommandAlias(
205214
"pp",
206215
""";compile-all
216+
|;bytes/scalafmtAll
217+
|;bytes/scalastyle
218+
|;bytes/test:scalastyle
207219
|;rlp/scalafmtAll
208220
|;rlp/scalastyle
209221
|;rlp/test:scalastyle

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ object ByteUtils {
4040
def toBigInt(bytes: ByteString): BigInt =
4141
bytes.foldLeft(BigInt(0)) { (n, b) => (n << 8) + (b & 0xff) }
4242

43+
def bigIntToUnsignedByteArray(i: BigInt): Array[Byte] = {
44+
val asByteArray = i.toByteArray
45+
if (asByteArray.head == 0) asByteArray.tail
46+
else asByteArray
47+
}
48+
4349
/**
4450
* Calculates xor distance between two byte arrays. Due to performance reasons needs to be as fast as possible
4551
* which means usage of while loops and var's.

src/test/scala/io/iohk/ethereum/utils/ByteUtilsSpec.scala renamed to bytes/src/test/scala/io/iohk/ethereum/utils/ByteUtilsSpec.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package io.iohk.ethereum.utils
22

3-
import io.iohk.ethereum.ObjectGenerators
43
import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks
54
import org.scalatest.funsuite.AnyFunSuite
5+
import org.scalacheck.{Arbitrary, Gen}
6+
7+
class ByteUtilsSpec extends AnyFunSuite with ScalaCheckPropertyChecks {
8+
def byteArrayOfNItemsGen(n: Int): Gen[Array[Byte]] =
9+
Gen.listOfN(n, Arbitrary.arbitrary[Byte]).map(_.toArray)
610

7-
class ByteUtilsSpec extends AnyFunSuite with ScalaCheckPropertyChecks with ObjectGenerators {
811
test("Convert Bytes to Int in little endian") {
912
forAll(byteArrayOfNItemsGen(32)) { bytes =>
1013
val toInts = ByteUtils.bytesToInts(bytes, bigEndian = false)

rlp/src/main/scala/io/iohk/ethereum/rlp/RLP.scala

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,6 @@ private[rlp] object RLP {
135135
else Array[Byte](singleByte)
136136
}
137137

138-
private[rlp] def bigIntToUnsignedByteArray(i: BigInt): Array[Byte] = {
139-
val asByteArray = i.toByteArray
140-
if (asByteArray.head == 0) asByteArray.tail
141-
else asByteArray
142-
}
143-
144138
/**
145139
* This function converts a short value to a big endian byte array of minimal length
146140
*

rlp/src/main/scala/io/iohk/ethereum/rlp/RLPImplicits.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package io.iohk.ethereum.rlp
22

33
import akka.util.ByteString
44
import io.iohk.ethereum.rlp.RLP._
5+
import io.iohk.ethereum.utils.ByteUtils
56
import RLPCodec.Ops
67

78
object RLPImplicits {
@@ -50,7 +51,7 @@ object RLPImplicits {
5051
implicit val bigIntEncDec = new RLPEncoder[BigInt] with RLPDecoder[BigInt] {
5152

5253
override def encode(obj: BigInt): RLPValue = RLPValue(
53-
if (obj.equals(BigInt(0))) byteToByteArray(0: Byte) else bigIntToUnsignedByteArray(obj)
54+
if (obj.equals(BigInt(0))) byteToByteArray(0: Byte) else ByteUtils.bigIntToUnsignedByteArray(obj)
5455
)
5556

5657
override def decode(rlp: RLPEncodeable): BigInt = rlp match {

src/main/scala/io/iohk/ethereum/utils/BigIntExtensionMethods.scala

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@ import io.iohk.ethereum.domain.UInt256
44

55
object BigIntExtensionMethods {
66
implicit class BigIntAsUnsigned(val srcBigInteger: BigInt) extends AnyVal {
7-
def toUnsignedByteArray: Array[Byte] = {
8-
val asByteArray = srcBigInteger.toByteArray
9-
if (asByteArray.head == 0) asByteArray.tail
10-
else asByteArray
11-
}
7+
def toUnsignedByteArray: Array[Byte] =
8+
ByteUtils.bigIntToUnsignedByteArray(srcBigInteger)
129

1310
def u256: UInt256 = UInt256(srcBigInteger)
1411
}

0 commit comments

Comments
 (0)