Skip to content

[ETCM-129] prepare for Scala 2.13 replace ⇒ to => #722

New issue

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

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

Already on GitHub? # to your account

Merged
merged 1 commit into from
Oct 6, 2020
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ trait ConsensusBuilder {
* [[io.iohk.ethereum.consensus.ethash.EthashConsensus EthashConsensus]],
*/
trait StdConsensusBuilder extends ConsensusBuilder {
self: VmBuilder with BlockchainBuilder with BlockchainConfigBuilder with ConsensusConfigBuilder with Logger
self: VmBuilder with BlockchainBuilder with BlockchainConfigBuilder with ConsensusConfigBuilder with Logger =>

private lazy val mantisConfig = Config.config

Expand All @@ -38,7 +38,7 @@ trait StdConsensusBuilder extends ConsensusBuilder {

val consensus =
config.protocol match {
case Protocol.Ethash | Protocol.MockedPow buildEthashConsensus()
case Protocol.Ethash | Protocol.MockedPow => buildEthashConsensus()
}
log.info(s"Using '${protocol.name}' consensus [${consensus.getClass.getName}]")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ object ConsensusConfig extends Logger {
Protocol.Names.MockedPow
)

final val AllowedProtocolsError = (s: String) Keys.Consensus +
final val AllowedProtocolsError = (s: String) => Keys.Consensus +
" is configured as '" + s + "'" +
" but it should be one of " +
AllowedProtocols.map("'" + _ + "'").mkString(",")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package io.iohk.ethereum.consensus
import io.iohk.ethereum.nodebuilder.ShutdownHookBuilder
import io.iohk.ethereum.utils.Config

trait ConsensusConfigBuilder { self: ShutdownHookBuilder
trait ConsensusConfigBuilder { self: ShutdownHookBuilder =>
protected def buildConsensusConfig(): ConsensusConfig = ConsensusConfig(Config.config)(this)

lazy val consensusConfig: ConsensusConfig = buildConsensusConfig()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import io.iohk.ethereum.utils.Logger
* A [[io.iohk.ethereum.consensus.ConsensusBuilder ConsensusBuilder]] that builds a
* [[io.iohk.ethereum.consensus.TestConsensus TestConsensus]]
*/
trait TestConsensusBuilder { self: StdConsensusBuilder
trait TestConsensusBuilder { self: StdConsensusBuilder =>
protected def buildTestConsensus(): TestConsensus =
buildConsensus().asInstanceOf[TestConsensus] // we are in tests, so if we get an exception, so be it
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package io.iohk.ethereum
package consensus
package ethash

import com.typesafe.config.{Config TypesafeConfig}
import com.typesafe.config.{Config => TypesafeConfig}

import scala.concurrent.duration.{FiniteDuration, _}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ class EthashConsensus private(

private[this] def startMiningProcess(node: Node): Unit = {
atomicMiner.get() match {
case None
case None =>
val miner = config.generic.protocol match {
case Ethash => EthashMiner(node)
case MockedPow => MockedMiner(node)
}
atomicMiner.set(Some(miner))
sendMiner(MinerProtocol.StartMining)

case _
case _ =>
}
}

Expand Down Expand Up @@ -95,7 +95,7 @@ class EthashConsensus private(
/** Internal API, used for testing */
protected def newBlockGenerator(validators: Validators): EthashBlockGenerator = {
validators match {
case _validators: ValidatorsExecutor
case _validators: ValidatorsExecutor =>
val blockPreparator = new BlockPreparator(
vm = vm,
signedTxValidator = validators.signedTransactionValidator,
Expand All @@ -112,7 +112,7 @@ class EthashConsensus private(
blockTimestampProvider = blockGenerator.blockTimestampProvider
)

case _
case _ =>
wrongValidatorsArgument[ValidatorsExecutor](validators)
}
}
Expand All @@ -121,7 +121,7 @@ class EthashConsensus private(
/** Internal API, used for testing */
def withValidators(validators: Validators): EthashConsensus = {
validators match {
case _validators: ValidatorsExecutor
case _validators: ValidatorsExecutor =>
val blockGenerator = newBlockGenerator(validators)

new EthashConsensus(
Expand All @@ -133,7 +133,7 @@ class EthashConsensus private(
blockGenerator = blockGenerator
)

case _
case _ =>
wrongValidatorsArgument[ValidatorsExecutor](validators)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ object EthashMiner {

def apply(node: Node): ActorRef = {
node.consensus match {
case consensus: EthashConsensus
case consensus: EthashConsensus =>
val blockCreator = new EthashBlockCreator(
pendingTransactionsManager = node.pendingTransactionsManager,
getTransactionFromPoolTimeout = node.txPoolConfig.getTransactionFromPoolTimeout,
Expand All @@ -212,7 +212,7 @@ object EthashMiner {
ethService = node.ethService
)
node.system.actorOf(minerProps)
case consensus
case consensus =>
wrongConsensusArgument[EthashConsensus](consensus)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ object MockedMiner {

def apply(node: Node): ActorRef = {
node.consensus match {
case consensus: EthashConsensus
case consensus: EthashConsensus =>
val blockCreator = new EthashBlockCreator(
pendingTransactionsManager = node.pendingTransactionsManager,
getTransactionFromPoolTimeout = node.txPoolConfig.getTransactionFromPoolTimeout,
Expand All @@ -120,7 +120,7 @@ object MockedMiner {
syncEventListener = node.syncController
)
node.system.actorOf(minerProps)
case consensus
case consensus =>
wrongConsensusArgument[EthashConsensus](consensus)
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/scala/io/iohk/ethereum/consensus/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import scala.reflect.ClassTag
* Different consensus protocols are implemented in sub-packages.
*/
package object consensus {
final type GetBlockHeaderByHash = ByteString Option[BlockHeader]
final type GetNBlocksBack = (ByteString, Int) Seq[Block]
final type GetBlockHeaderByHash = ByteString => Option[BlockHeader]
final type GetNBlocksBack = (ByteString, Int) => Seq[Block]

def wrongConsensusArgument[T <: Consensus : ClassTag](consensus: Consensus): Nothing = {
val requiredClass = implicitly[ClassTag[T]].runtimeClass
Expand Down Expand Up @@ -42,10 +42,10 @@ package object consensus {
* if we run under [[io.iohk.ethereum.consensus.ethash.EthashConsensus EthashConsensus]]
* then the `_then` function is called, otherwise the `_else` value is computed.
*/
def ifEthash[A](_then: EthashConsensus A)(_else: A): A =
def ifEthash[A](_then: EthashConsensus => A)(_else: => A): A =
consensus match {
case ethash: EthashConsensus _then(ethash)
case _ _else
case ethash: EthashConsensus => _then(ethash)
case _ => _else
}
}
}
14 changes: 7 additions & 7 deletions src/main/scala/io/iohk/ethereum/healthcheck/Healthcheck.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ import scala.util.{Failure, Success}
*/
case class Healthcheck[Error, Result](
description: String,
f: () Future[Either[Error, Result]],
mapResultToError: Result Option[String] = (_: Result) None,
mapErrorToError: Error Option[String] = (error: Error) Some(String.valueOf(error)),
mapExceptionToError: Throwable Option[String] = (t: Throwable) Some(String.valueOf(t))
f: () => Future[Either[Error, Result]],
mapResultToError: Result => Option[String] = (_: Result) => None,
mapErrorToError: Error => Option[String] = (error: Error) => Some(String.valueOf(error)),
mapExceptionToError: Throwable => Option[String] = (t: Throwable) => Some(String.valueOf(t))
) {

def apply()(implicit ec: ExecutionContext): Future[HealthcheckResult] = {
f().transform {
case Success(Left(error))
case Success(Left(error)) =>
Success(HealthcheckResult(description, mapErrorToError(error)))
case Success(Right(result))
case Success(Right(result)) =>
Success(HealthcheckResult(description, mapResultToError(result)))
case Failure(t)
case Failure(t) =>
Success(HealthcheckResult(description, mapExceptionToError(t)))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ object HealthcheckResult {
def apply(description: String, error: Option[String]): HealthcheckResult =
new HealthcheckResult(
description = description,
status = error.fold(HealthcheckStatus.OK)(_ HealthcheckStatus.ERROR),
status = error.fold(HealthcheckStatus.OK)(_ => HealthcheckStatus.ERROR),
error = error
)
}
18 changes: 9 additions & 9 deletions src/main/scala/io/iohk/ethereum/jsonrpc/EthService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,9 @@ class EthService(
private[this] def fullConsensusConfig = consensus.config
private[this] def consensusConfig: ConsensusConfig = fullConsensusConfig.generic

private[this] def ifEthash[Req, Res](req: Req)(f: Req Res): ServiceResponse[Res] = {
private[this] def ifEthash[Req, Res](req: Req)(f: Req => Res): ServiceResponse[Res] = {
@inline def F[A](x: A): Future[A] = Future.successful(x)
consensus.ifEthash[ServiceResponse[Res]](_ F(Right(f(req))))(F(Left(JsonRpcErrors.ConsensusIsNotEthash)))
consensus.ifEthash[ServiceResponse[Res]](_ => F(Right(f(req))))(F(Left(JsonRpcErrors.ConsensusIsNotEthash)))
}

def protocolVersion(req: ProtocolVersionRequest): ServiceResponse[ProtocolVersionResponse] =
Expand Down Expand Up @@ -435,7 +435,7 @@ class EthService(
}

def submitHashRate(req: SubmitHashRateRequest): ServiceResponse[SubmitHashRateResponse] =
ifEthash(req) { req
ifEthash(req) { req =>
reportActive()
hashRate.updateAndGet((t: Map[ByteString, (BigInt, Date)]) => {
val now = new Date
Expand Down Expand Up @@ -464,7 +464,7 @@ class EthService(
}

def getMining(req: GetMiningRequest): ServiceResponse[GetMiningResponse] =
ifEthash(req) { _
ifEthash(req) { _ =>
val isMining = lastActive
.updateAndGet((e: Option[Date]) => {
e.filter { time =>
Expand All @@ -478,11 +478,11 @@ class EthService(

private def reportActive(): Option[Date] = {
val now = new Date()
lastActive.updateAndGet(_ Some(now))
lastActive.updateAndGet(_ => Some(now))
}

def getHashRate(req: GetHashRateRequest): ServiceResponse[GetHashRateResponse] =
ifEthash(req) { _
ifEthash(req) { _ =>
val hashRates: Map[ByteString, (BigInt, Date)] = hashRate.updateAndGet((t: Map[ByteString, (BigInt, Date)]) => {
removeObsoleteHashrates(new Date, t)
})
Expand All @@ -502,7 +502,7 @@ class EthService(
}

def getWork(req: GetWorkRequest): ServiceResponse[GetWorkResponse] =
consensus.ifEthash(ethash {
consensus.ifEthash(ethash => {
reportActive()
import io.iohk.ethereum.consensus.ethash.EthashUtils.{epoch, seed}

Expand Down Expand Up @@ -531,7 +531,7 @@ class EthService(
})(Future.successful(Left(JsonRpcErrors.ConsensusIsNotEthash)))

private def getOmmersFromPool(blockNumber: BigInt): Future[OmmersPool.Ommers] =
consensus.ifEthash(ethash {
consensus.ifEthash(ethash => {
val miningConfig = ethash.config.specific
implicit val timeout: Timeout = Timeout(miningConfig.ommerPoolQueryTimeout)

Expand Down Expand Up @@ -559,7 +559,7 @@ class EthService(
Future.successful(Right(GetCoinbaseResponse(consensusConfig.coinbase)))

def submitWork(req: SubmitWorkRequest): ServiceResponse[SubmitWorkResponse] =
consensus.ifEthash[ServiceResponse[SubmitWorkResponse]](ethash {
consensus.ifEthash[ServiceResponse[SubmitWorkResponse]](ethash => {
reportActive()
Future {
ethash.blockGenerator.getPrepared(req.powHeaderHash) match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ import io.iohk.ethereum.healthcheck.Healthcheck
object JsonRpcHealthcheck {
type T[R] = Healthcheck[JsonRpcError, R]

def apply[R](description: String, f: () ServiceResponse[R]): T[R] = Healthcheck(description, f)
def apply[R](description: String, f: () => ServiceResponse[R]): T[R] = Healthcheck(description, f)
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ class NodeJsonRpcHealthChecker(

protected def mainService: String = "node health"

final val listeningHC = JsonRpcHealthcheck("listening", () netService.listening(NetService.ListeningRequest()))
final val peerCountHC = JsonRpcHealthcheck("peerCount", () netService.peerCount(PeerCountRequest()))
final val listeningHC = JsonRpcHealthcheck("listening", () => netService.listening(NetService.ListeningRequest()))
final val peerCountHC = JsonRpcHealthcheck("peerCount", () => netService.peerCount(PeerCountRequest()))
final val earliestBlockHC = JsonRpcHealthcheck(
"earliestBlock",
() ethService.getBlockByNumber(BlockByNumberRequest(BlockParam.Earliest, true))
() => ethService.getBlockByNumber(BlockByNumberRequest(BlockParam.Earliest, true))
)
final val latestBlockHC = JsonRpcHealthcheck(
"latestBlock",
() ethService.getBlockByNumber(BlockByNumberRequest(BlockParam.Latest, true))
() => ethService.getBlockByNumber(BlockByNumberRequest(BlockParam.Latest, true))
)
final val pendingBlockHC = JsonRpcHealthcheck(
"pendingBlock",
() ethService.getBlockByNumber(BlockByNumberRequest(BlockParam.Pending, true))
() => ethService.getBlockByNumber(BlockByNumberRequest(BlockParam.Pending, true))
)

override def healthCheck(): Future[HealthcheckResponse] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ trait JsonRpcHttpServer extends Json4sSupport {
val responseF = jsonRpcHealthChecker.healthCheck()
val httpResponseF =
responseF.map {
case response if response.isOK
case response if response.isOK =>
HttpResponse(
status = StatusCodes.OK,
entity = HttpEntity(ContentTypes.`application/json`, serialization.writePretty(response))
)
case response
case response =>
HttpResponse(
status = StatusCodes.InternalServerError,
entity = HttpEntity(ContentTypes.`application/json`, serialization.writePretty(response))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class DeltaSpikeGauge(name: String, metrics: Metrics) {
}
}

private[this] final val gauge = metrics.gauge(name, () getValue)
private[this] final val gauge = metrics.gauge(name, () => getValue)

def trigger(): Unit = {
if (isTriggeredRef.compareAndSet(false, true)) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/io/iohk/ethereum/metrics/Metrics.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ case class Metrics(metricsPrefix: String, registry: MeterRegistry, serverPort: I
* Returns a [[io.micrometer.core.instrument.Gauge Gauge]].
* @param computeValue A function that computes the current gauge value.
*/
def gauge(name: String, computeValue: () Double): Gauge =
def gauge(name: String, computeValue: () => Double): Gauge =
Gauge
// Note Never use `null` as the value for the second parameter.
// If you do, you risk getting no metrics out of the gauge.
// So we just use a vanilla `this` but any other non-`null`
// value would also do.
.builder(mkName(name), this, (_: Any) computeValue())
.builder(mkName(name), this, (_: Any) => computeValue())
.register(registry)

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.iohk.ethereum.metrics

import com.typesafe.config.{Config TypesafeConfig}
import com.typesafe.config.{Config => TypesafeConfig}

final case class MetricsConfig(
enabled: Boolean,
Expand Down
8 changes: 4 additions & 4 deletions src/main/scala/io/iohk/ethereum/nodebuilder/NodeBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ trait SyncControllerBuilder {
}

trait ShutdownHookBuilder {
self: Logger
self: Logger =>
def shutdown(): Unit = {
/* No default behaviour during shutdown. */
}
Expand All @@ -511,10 +511,10 @@ trait ShutdownHookBuilder {
}
})

def shutdownOnError[A](f: A): A = {
def shutdownOnError[A](f: => A): A = {
Try(f) match {
case Success(v) v
case Failure(t)
case Success(v) => v
case Failure(t) =>
log.error(t.getMessage, t)
shutdown()
throw t
Expand Down
6 changes: 3 additions & 3 deletions src/main/scala/io/iohk/ethereum/utils/Ref.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ class Ref[T <: AnyRef] {
private[this] final val ref = new AtomicReference[Option[T]](None)

// set once (but not necessarily compute once)
final def setOnce(t: T): Boolean = ref.get().isEmpty && ref.compareAndSet(None, Some(t))
final def setOnce(t: => T): Boolean = ref.get().isEmpty && ref.compareAndSet(None, Some(t))

final def isDefined: Boolean = ref.get().isDefined
final def isEmpty: Boolean = ref.get().isEmpty

final def map[U](f: T U): Option[U] = ref.get().map(f)
final def foreach[U](f: T U): Unit = map(f)
final def map[U](f: T => U): Option[U] = ref.get().map(f)
final def foreach[U](f: T => U): Unit = map(f)
}
Loading