Skip to content

Commit 80e6ff5

Browse files
author
Dmitry Voronov
committed
Completed and tested 2.13 migration
1 parent e3d1594 commit 80e6ff5

33 files changed

+155
-121
lines changed

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

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

33
import akka.util.ByteString
44

5+
import scala.collection.mutable
6+
import scala.math.Ordering.Implicits._
7+
58
object ByteStringUtils {
69
def hash2string(hash: ByteString): String =
710
Hex.toHexString(hash.toArray[Byte])
@@ -48,19 +51,19 @@ object ByteStringUtils {
4851
def asByteArray: Array[Byte] = Array(b)
4952
}
5053

51-
def concatByteStrings(bse: ByteStringElement*): ByteString = {
52-
concatByteStrings(bse.toIterable)
54+
implicit val byteStringOrdering: Ordering[ByteString] = {
55+
Ordering.by[ByteString, Seq[Byte]](_.toSeq)
56+
}
57+
58+
def concatByteStrings(head: ByteStringElement, tail: ByteStringElement*): ByteString = {
59+
val it = Iterator.single(head) ++ tail.iterator
60+
concatByteStrings(it)
5361
}
5462

55-
def concatByteStrings(bse: Iterable[ByteStringElement]): ByteString = {
56-
val totalLength = bse.map(_.len).sum
57-
val result = new Array[Byte](totalLength)
58-
bse.foldLeft(0)( (i, el) => {
59-
val arr = el.asByteArray
60-
System.arraycopy(arr, 0, result, i, el.len)
61-
i + el.len
62-
})
63-
ByteString.fromArray(result)
63+
def concatByteStrings(elements: Iterator[ByteStringElement]): ByteString = {
64+
val builder = new mutable.ArrayBuilder.ofByte
65+
elements.foreach(el => builder.addAll(el.asByteArray))
66+
ByteString(builder.result())
6467
}
6568

6669
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.iohk.ethereum.utils
2+
3+
import akka.util.ByteString
4+
import org.scalatest.wordspec.AnyWordSpec
5+
6+
class ByteStringUtilsTest extends AnyWordSpec {
7+
8+
"ByteStringUtilsTest" should {
9+
10+
"concatByteStrings for simple bytestrings" in {
11+
val bs1 = ByteString("000")
12+
val bs2 = ByteString("111")
13+
val summarized: ByteString = bs1 ++ bs2
14+
15+
val concatenated: ByteString = ByteStringUtils.concatByteStrings(bs1, bs2)
16+
println(s"Bs1: ${bs1} Bs2: ${bs2} Sum: ${summarized} Concat: ${concatenated}")
17+
assert(summarized == concatenated)
18+
}
19+
20+
}
21+
}

project/Dependencies.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ object Dependencies {
6060
val testing: Seq[ModuleID] = Seq(
6161
"org.scalatest" %% "scalatest" % "3.2.2" % "it,test",
6262
"org.scalamock" %% "scalamock" % "5.0.0" % "test",
63-
"org.scalatestplus" %% "scalacheck-1-14" % "3.2.2.0" % "test",
64-
"org.scalacheck" %% "scalacheck" % "1.14.3" % "it,test",
63+
"org.scalatestplus" %% "scalacheck-1-15" % "3.2.3.0" % "test",
64+
"org.scalacheck" %% "scalacheck" % "1.15.1" % "it,test",
6565
"com.softwaremill.diffx" %% "diffx-core" % "0.3.30" % "test",
6666
"com.softwaremill.diffx" %% "diffx-scalatest" % "0.3.30" % "test"
6767
)

src/benchmark/scala/io/iohk/ethereum/rlp/RLPSpeedSuite.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class RLPSpeedSuite
5757
val ITERATIONS: Int = 10000000
5858
log.info("Starting " + ITERATIONS + " decoding iterations...")
5959
val start1: Long = System.currentTimeMillis
60-
(1 to ITERATIONS).foreach { _ => RLP.rawDecode(payload); Unit }
60+
(1 to ITERATIONS).foreach { _ => RLP.rawDecode(payload); () }
6161
val end1: Long = System.currentTimeMillis
6262
log.info("Result decode()\t: " + (end1 - start1) + "ms")
6363
}

src/it/scala/io/iohk/ethereum/db/DataSourceIntegrationTestBehavior.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import io.iohk.ethereum.db.dataSource.{DataSource, DataSourceUpdate}
99
import io.iohk.ethereum.db.dataSource.DataSource.{Key, Namespace, Value}
1010
import org.scalatest.flatspec.AnyFlatSpec
1111
import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks
12+
import io.iohk.ethereum.utils.ByteStringUtils._
1213

1314
trait DataSourceIntegrationTestBehavior extends ScalaCheckPropertyChecks with ObjectGenerators {
1415

@@ -91,7 +92,7 @@ trait DataSourceIntegrationTestBehavior extends ScalaCheckPropertyChecks with Ob
9192
val db = createDataSource(path)
9293
db.update(prepareUpdate(toUpsert = keyList.zip(keyList)))
9394

94-
val keyListWithExtraByte = keyList.map(1.toByte +: _)
95+
val keyListWithExtraByte = keyList.map(key => concatByteStrings(1.toByte, key))
9596
updateInSeparateCalls(db, keyList.zip(keyListWithExtraByte))
9697

9798
keyList.zip(keyListWithExtraByte).foreach { case (key, value) =>

src/it/scala/io/iohk/ethereum/db/RockDbIteratorSpec.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class RockDbIteratorSpec extends FlatSpecBase with ResourceFixtures with Matcher
4545
_ <- writeNValuesToDb(largeNum, db, Namespaces.NodeNamespace)
4646
fib <- db
4747
.iterate(Namespaces.NodeNamespace)
48-
.map(_.right.get)
48+
.map(_.toOption.get)
4949
.consumeWith(Consumer.foreachEval[Task, (Array[Byte], Array[Byte])] { _ =>
5050
for {
5151
cur <- counter.updateAndGet(i => i + 1)
@@ -71,7 +71,7 @@ class RockDbIteratorSpec extends FlatSpecBase with ResourceFixtures with Matcher
7171
_ <- writeNValuesToDb(largeNum, db, Namespaces.NodeNamespace)
7272
_ <- db
7373
.iterate(Namespaces.NodeNamespace)
74-
.map(_.right.get)
74+
.map(_.toOption.get)
7575
.consumeWith(Consumer.foreachEval[Task, (Array[Byte], Array[Byte])] { _ =>
7676
counter.update(current => current + 1)
7777
})
@@ -92,8 +92,8 @@ class RockDbIteratorSpec extends FlatSpecBase with ResourceFixtures with Matcher
9292
_ <- Task(codeStorage.update(Seq(), codeKeyValues).commit())
9393
_ <- Task(nodeStorage.update(Seq(), nodeKeyValues))
9494
result <- Task.parZip2(
95-
codeStorage.storageContent.map(_.right.get).map(_._1).toListL,
96-
nodeStorage.storageContent.map(_.right.get).map(_._1).toListL
95+
codeStorage.storageContent.map(_.toOption.get).map(_._1).toListL,
96+
nodeStorage.storageContent.map(_.toOption.get).map(_._1).toListL
9797
)
9898
(codeResult, nodeResult) = result
9999
} yield {
@@ -112,7 +112,7 @@ class RockDbIteratorSpec extends FlatSpecBase with ResourceFixtures with Matcher
112112
)
113113
)
114114
)
115-
elems <- db.iterate(Namespaces.NodeNamespace).map(_.right.get).toListL
115+
elems <- db.iterate(Namespaces.NodeNamespace).map(_.toOption.get).toListL
116116
} yield {
117117
val deserialized = elems.map { case (bytes, bytes1) => (ByteString(bytes), ByteString(bytes1)) }
118118
assert(elems.size == keyValues.size)

src/main/scala/io/iohk/ethereum/db/dataSource/DataSourceBatchUpdate.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.iohk.ethereum.db.dataSource
22

3-
import scala.collection.compat.immutable.ArraySeq
3+
import scala.collection.immutable.ArraySeq
44

55
case class DataSourceBatchUpdate(dataSource: DataSource, updates: Array[DataUpdate] = Array.empty) {
66

src/main/scala/io/iohk/ethereum/db/dataSource/RocksDbDataSource.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import monix.eval.Task
1111
import monix.reactive.Observable
1212
import org.rocksdb._
1313

14-
import scala.collection.compat.immutable.ArraySeq
14+
import scala.collection.immutable.ArraySeq
1515
import scala.collection.mutable
1616
import scala.util.control.NonFatal
1717

src/main/scala/io/iohk/ethereum/db/storage/AppStateStorage.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import java.math.BigInteger
55
import io.iohk.ethereum.db.dataSource.{DataSource, DataSourceBatchUpdate}
66
import io.iohk.ethereum.db.storage.AppStateStorage._
77

8-
import scala.collection.compat.immutable.ArraySeq
8+
import scala.collection.immutable.ArraySeq
99

1010
/**
1111
* This class is used to store app state variables

src/main/scala/io/iohk/ethereum/db/storage/FastSyncStateStorage.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import io.iohk.ethereum.blockchain.sync.fast.FastSync._
99
import io.iohk.ethereum.db.dataSource.DataSource
1010
import io.iohk.ethereum.utils.ByteUtils.compactPickledBytes
1111

12-
import scala.collection.compat.immutable.ArraySeq
12+
import scala.collection.immutable.ArraySeq
1313

1414
object FastSyncStateStorage {
1515

src/main/scala/io/iohk/ethereum/jsonrpc/server/controllers/JsonRpcBaseController.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import monix.eval.Task
1414
import org.json4s.JsonDSL._
1515
import org.json4s.{DefaultFormats, native}
1616

17-
import scala.collection.compat.immutable.ArraySeq
17+
import scala.collection.immutable.ArraySeq
1818
import scala.concurrent.ExecutionContext
1919
import scala.concurrent.duration.FiniteDuration
2020

src/main/scala/io/iohk/ethereum/network/rlpx/FrameCodec.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ class FrameCodec(private val secrets: Secrets) {
172172
elements.append(bytes)
173173
i += bytes.length
174174
}
175-
concatByteStrings(elements)
175+
concatByteStrings(elements.iterator)
176176
}
177177

178178
private def processFramePadding(totalSize: Int): ByteString = {

src/test/scala/io/iohk/ethereum/ObjectGenerators.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ import org.scalacheck.{Arbitrary, Gen, Shrink}
1919
// scalastyle:off number.of.methods
2020
trait ObjectGenerators {
2121

22-
def noShrink[T]: Shrink[T] = Shrink[T](_ => Stream.empty)
23-
2422
def byteGen: Gen[Byte] = Gen.choose(Byte.MinValue, Byte.MaxValue)
2523

2624
def shortGen: Gen[Short] = Gen.choose(Short.MinValue, Short.MaxValue)

src/test/scala/io/iohk/ethereum/blockchain/sync/SyncControllerSpec.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ class SyncControllerSpec extends AnyFlatSpec with Matchers with BeforeAndAfter w
536536
sender ! MessageFromPeer(BlockHeaders(Seq(pivotHeader)), peer)
537537
} else {
538538
if (!onlyPivot) {
539-
val start = msg.underlyingMsg.block.left.get
539+
val start = msg.underlyingMsg.block.swap.toOption.get
540540
val stop = start + msg.underlyingMsg.maxHeaders
541541
val headers = (start until stop).flatMap(i => blockchainData.headers.get(i))
542542
sender ! MessageFromPeer(BlockHeaders(headers), peer)

src/test/scala/io/iohk/ethereum/blockchain/sync/regular/RegularSyncSpec.scala

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -358,13 +358,11 @@ class RegularSyncSpec
358358
})
359359

360360
"save fetched node" in sync(new Fixture(testSystem) {
361-
implicit val ec: Scheduler = Scheduler(system.dispatcher)
362361
override lazy val blockchain: BlockchainImpl = stub[BlockchainImpl]
363362
override lazy val ledger: TestLedgerImpl = stub[TestLedgerImpl]
364363
val failingBlock: Block = testBlocksChunked.head.head
365364
peersClient.setAutoPilot(new PeersClientAutoPilot)
366-
367-
(ledger.resolveBranch _).when(*).returns(NewBetterBranch(Nil))
365+
(ledger.resolveBranch _).when(*).returns(NewBetterBranch(Nil)).repeat(10)
368366
(ledger
369367
.importBlock(_: Block)(_: Scheduler))
370368
.when(*, *)

src/test/scala/io/iohk/ethereum/cli/CliCommandsSpec.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class CliCommandsSpec extends AnyFlatSpec with Matchers with EitherValues {
8282
it should "generate one key pair when passed no args" in {
8383
val result = api.parse(Seq(generateKeyPairsCommand))
8484
result shouldBe a[Right[_, _]]
85-
val stringSplit = result.right.get.split("\\n\\n")
85+
val stringSplit = result.toOption.get.split("\\n\\n")
8686
stringSplit.length shouldEqual 1
8787
}
8888

@@ -91,7 +91,7 @@ class CliCommandsSpec extends AnyFlatSpec with Matchers with EitherValues {
9191
val numOfKeysAsInt = numOfKeys.toInt
9292
val result = api.parse(Seq(generateKeyPairsCommand, numOfKeys))
9393
result shouldBe a[Right[_, _]]
94-
val stringSplit = result.right.get.split("\\n\\n")
94+
val stringSplit = result.toOption.get.split("\\n\\n")
9595
stringSplit.length shouldEqual numOfKeysAsInt
9696
}
9797

src/test/scala/io/iohk/ethereum/consensus/validators/BlockWithCheckpointHeaderValidatorSpec.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import org.scalatest.Assertion
1919
import org.scalatest.flatspec.AnyFlatSpec
2020
import org.scalatest.matchers.should.Matchers
2121
import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks
22+
import ByteStringUtils.byteStringOrdering
2223

2324
class BlockWithCheckpointHeaderValidatorSpec
2425
extends AnyFlatSpec
@@ -224,9 +225,7 @@ class BlockWithCheckpointHeaderValidatorSpec
224225
import Ordering.Implicits._
225226
val actualSigners = invalidCheckpoint.signatures.flatMap(_.publicKey(validBlockParent.hash)).sortBy(_.toSeq)
226227
val duplicatedSigner = ByteString(crypto.pubKeyFromKeyPair(keys.head))
227-
val expectedSigners =
228-
(keys.map(kp => ByteString(crypto.pubKeyFromKeyPair(kp))) :+ duplicatedSigner)
229-
.sortBy(_.toSeq)
228+
val expectedSigners = (keys.map(kp => ByteString(crypto.pubKeyFromKeyPair(kp))) :+ duplicatedSigner).sorted
230229
actualSigners shouldEqual expectedSigners
231230

232231
val headerWithInvalidCheckpoint = checkpointBlockGenerator

src/test/scala/io/iohk/ethereum/domain/ArbitraryIntegerMptSpec.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import org.scalatest.matchers.should.Matchers
1111

1212
class ArbitraryIntegerMptSpec extends AnyFlatSpec with Matchers with ScalaCheckPropertyChecks {
1313

14-
implicit val bigIntListNoShrink = noShrink[List[BigInt]]
15-
1614
def keyGen: Gen[BigInt] = byteArrayOfNItemsGen(128).map(BigInt.apply)
1715
def valueGen: Gen[BigInt] = byteArrayOfNItemsGen(128).map(BigInt.apply)
1816

src/test/scala/io/iohk/ethereum/extvm/MessageHandlerSpec.scala

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,19 @@ import akka.stream.OverflowStrategy
55
import akka.stream.scaladsl.{Keep, Sink, SinkQueueWithCancel, Source, SourceQueueWithComplete}
66
import akka.testkit.TestProbe
77
import akka.util.ByteString
8-
import scalapb.GeneratedMessage
8+
import scalapb.{GeneratedMessage, GeneratedMessageCompanion}
99
import io.iohk.ethereum.vm.Generators
1010
import java.math.BigInteger
11+
12+
import com.google.protobuf.CodedOutputStream
1113
import org.bouncycastle.util.BigIntegers
1214
import org.scalamock.scalatest.MockFactory
1315
import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks
16+
1417
import scala.concurrent.ExecutionContext.Implicits.global
1518
import org.scalatest.flatspec.AnyFlatSpec
1619
import org.scalatest.matchers.should.Matchers
20+
import scalapb.descriptors.{FieldDescriptor, PValue}
1721

1822
class MessageHandlerSpec extends AnyFlatSpec with Matchers with MockFactory with ScalaCheckPropertyChecks {
1923

@@ -33,9 +37,16 @@ class MessageHandlerSpec extends AnyFlatSpec with Matchers with MockFactory with
3337
val (out, fut) = Source.queue[ByteString](1024, OverflowStrategy.dropTail).toMat(Sink.seq)(Keep.both).run()
3438
fut.pipeTo(probe.ref)
3539

36-
val gm = mock[GeneratedMessage]
37-
(() => gm.toByteArray).expects().returning(bytes.toArray[Byte])
38-
40+
// mock of final fields is no longer possible
41+
// had to create a GeneratedMessage stub
42+
val gm = new GeneratedMessage {
43+
override def writeTo(output: CodedOutputStream): Unit = output.writeRawBytes(bytes)
44+
override def getFieldByNumber(fieldNumber: Int): Any = ???
45+
override def getField(field: FieldDescriptor): PValue = ???
46+
override def companion: GeneratedMessageCompanion[_] = ???
47+
override def serializedSize: Int = bytes.size
48+
override def toProtoString: String = ???
49+
}
3950
val messageHandler = new MessageHandler(in, out)
4051
messageHandler.sendMessage(gm)
4152
messageHandler.close()

0 commit comments

Comments
 (0)