Skip to content

Commit

Permalink
[ETCM-141] scalafmtAll
Browse files Browse the repository at this point in the history
  • Loading branch information
lemastero committed Oct 16, 2020
1 parent 34c4654 commit 70c8dc5
Show file tree
Hide file tree
Showing 315 changed files with 6,054 additions and 4,162 deletions.
36 changes: 18 additions & 18 deletions src/main/scala/io/iohk/ethereum/App.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import io.iohk.ethereum.faucet.Faucet
import io.iohk.ethereum.mallet.main.Mallet
import io.iohk.ethereum.utils.{Config, Logger}


object App extends Logger {

def main(args: Array[String]): Unit = {
Expand All @@ -19,25 +18,26 @@ object App extends Logger {
val faucet = "faucet"
val ecKeyGen = "eckeygen"

args.headOption match {
case None => Mantis.main(args)
case Some(`launchMantis`) => Mantis.main(args.tail)
case Some(`launchKeytool`) => KeyTool.main(args.tail)
case Some(`downloadBootstrap`) => {
Config.Db.dataSource match {
case "rocksdb" => BootstrapDownload.main(args.tail :+ Config.Db.RocksDb.path)
}
args.headOption match {
case None => Mantis.main(args)
case Some(`launchMantis`) => Mantis.main(args.tail)
case Some(`launchKeytool`) => KeyTool.main(args.tail)
case Some(`downloadBootstrap`) => {
Config.Db.dataSource match {
case "rocksdb" => BootstrapDownload.main(args.tail :+ Config.Db.RocksDb.path)
}
case Some(`vmServer`) => VmServerApp.main(args.tail)
case Some(`mallet`) => Mallet.main(args.tail)
case Some(`faucet`) => Faucet.main(args.tail)
case Some(`ecKeyGen`) => EcKeyGen.main(args.tail)
case Some(unknown) =>
log.error(s"Unrecognised launcher option, " +
s"first parameter must be $launchKeytool, $downloadBootstrap, $launchMantis, " +
s"$mallet, $faucet, $vmServer or $ecKeyGen")
}

case Some(`vmServer`) => VmServerApp.main(args.tail)
case Some(`mallet`) => Mallet.main(args.tail)
case Some(`faucet`) => Faucet.main(args.tail)
case Some(`ecKeyGen`) => EcKeyGen.main(args.tail)
case Some(unknown) =>
log.error(
s"Unrecognised launcher option, " +
s"first parameter must be $launchKeytool, $downloadBootstrap, $launchMantis, " +
s"$mallet, $faucet, $vmServer or $ecKeyGen"
)
}

}
}
55 changes: 30 additions & 25 deletions src/main/scala/io/iohk/ethereum/BootstrapDownload.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.iohk.ethereum


import java.io.{File, FileInputStream, FileOutputStream}
import java.net.URL
import java.nio.file._
Expand All @@ -10,8 +9,6 @@ import java.util.zip.ZipInputStream
import io.iohk.ethereum.utils.Logger
import org.bouncycastle.util.encoders.Hex



/**
* A facility to
* - check the download location for a minimum amount of free space
Expand All @@ -26,14 +23,17 @@ object BootstrapDownload extends Logger {
val leveldbFolderName = "leveldb"

private def assertAndLog(cond: Boolean, msg: String): Unit = {
if(!cond) log.info(msg)
if (!cond) log.info(msg)
assert(cond, msg)
}

def cleanOutFolder(pathToDownloadTo: Path): Unit = {
val leveldbFolder = pathToDownloadTo.toFile
assertAndLog(leveldbFolder.isDirectory, s"${pathToDownloadTo} must be a folder.")
assertAndLog(leveldbFolder.getName == leveldbFolderName, s"${pathToDownloadTo} must end in a folder named $leveldbFolderName")
assertAndLog(
leveldbFolder.getName == leveldbFolderName,
s"${pathToDownloadTo} must end in a folder named $leveldbFolderName"
)
leveldbFolder.listFiles(pathname => !pathname.getName.endsWith(".zip")).foreach(_.delete())
}

Expand All @@ -43,12 +43,12 @@ object BootstrapDownload extends Logger {
val dis = new DigestInputStream(new URL(urlToDownloadFrom).openStream(), sha512)

try {
val out = new FileOutputStream(outFile)
try {
val buffer = new Array[Byte](bufferSize)
Stream.continually(dis.read(buffer)).takeWhile(_ != -1).foreach(out.write(buffer, 0, _))
} finally (out.close())
Hex.toHexString(sha512.digest)
val out = new FileOutputStream(outFile)
try {
val buffer = new Array[Byte](bufferSize)
Stream.continually(dis.read(buffer)).takeWhile(_ != -1).foreach(out.write(buffer, 0, _))
} finally (out.close())
Hex.toHexString(sha512.digest)

} finally {
dis.close()
Expand All @@ -74,34 +74,37 @@ object BootstrapDownload extends Logger {
try {
val buffer = new Array[Byte](bufferSize)
Stream.continually(zis.read(buffer)).takeWhile(_ != -1).foreach(out.write(buffer, 0, _))
} finally(out.close())
} finally (out.close())
}
}
} finally(zis.close())
} finally(in.close())
} finally (zis.close())
} finally (in.close())
}


def deleteDownloadedFile(downloadedFile: File): Unit = {
if(downloadedFile.delete()) log.info(s"Downloaded file $downloadedFile successfully deleted")
if (downloadedFile.delete()) log.info(s"Downloaded file $downloadedFile successfully deleted")
else log.info(s"Failed to delete downloaded file $downloadedFile")
}

// scalastyle:off method.length
def main(args: Array[String]): Unit = {
//download a zip file from a url.

assertAndLog(args.length == 4, "Provide the url to download from, " +
" expected hash of the downloaded file, " +
" the minimum required free disk space in giga bytes" +
" and the path to extract the file to")
assertAndLog(
args.length == 4,
"Provide the url to download from, " +
" expected hash of the downloaded file, " +
" the minimum required free disk space in giga bytes" +
" and the path to extract the file to"
)

val urlToDownloadFrom = new URL(args(0))
val expectedHash = args(1)
val minimumExpectedDiskSpace = args(2)
val pathToDownloadTo = Paths.get(args(3))

val bytesInOneGigaByte = 1024l * 1024l * 1024l
val minimumExpectedDiskSpaceInBytes = minimumExpectedDiskSpace.toLong * bytesInOneGigaByte
val bytesInOneGigaByte = 1024L * 1024L * 1024L
val minimumExpectedDiskSpaceInBytes = minimumExpectedDiskSpace.toLong * bytesInOneGigaByte

val urlToDownloadFromAsFile = new File(urlToDownloadFrom.getFile)
val pathToDownloadToAsFile = pathToDownloadTo.toFile
Expand All @@ -112,11 +115,13 @@ object BootstrapDownload extends Logger {
log.info(s"Download path is $urlToDownloadFrom")
log.info(s"Path to download to is $pathToDownloadTo")

if(!pathToDownloadToAsFile.exists()) pathToDownloadToAsFile.mkdirs()
if (!pathToDownloadToAsFile.exists()) pathToDownloadToAsFile.mkdirs()

assertAndLog(pathToDownloadToAsFile.isDirectory, s"$pathToDownloadToAsFile must be a folder.")
assertAndLog(pathToDownloadToAsFile.getUsableSpace() >= minimumExpectedDiskSpaceInBytes,
s"There is not enough free space ($minimumExpectedDiskSpace GB) to download and expand to $pathToDownloadTo ")
assertAndLog(
pathToDownloadToAsFile.getUsableSpace() >= minimumExpectedDiskSpaceInBytes,
s"There is not enough free space ($minimumExpectedDiskSpace GB) to download and expand to $pathToDownloadTo "
)

log.info(s"Free space check ok, starting download! (this could take some time)")
val hash = downloadFile(args(0), downloadedFile)
Expand Down
1 change: 0 additions & 1 deletion src/main/scala/io/iohk/ethereum/KeyTool.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.iohk.ethereum


object KeyTool {

def main(args: Array[String]): Unit = sun.security.tools.keytool.Main.main(args)
Expand Down
3 changes: 1 addition & 2 deletions src/main/scala/io/iohk/ethereum/Mantis.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ object Mantis extends Logger {
if (Config.testmode) {
log.info("Starting Mantis in test mode")
new TestNode
}
else new StdNode
} else new StdNode

log.info("Using network {}", Config.blockchains.network)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ import org.bouncycastle.util.encoders.Hex
import scala.io.Source
import scala.util.{Failure, Success, Try}

class GenesisDataLoader(
blockchain: Blockchain,
blockchainConfig: BlockchainConfig)
extends Logger{
class GenesisDataLoader(blockchain: Blockchain, blockchainConfig: BlockchainConfig) extends Logger {

private val bloomLength = 512
private val hashLength = 64
Expand Down Expand Up @@ -90,13 +87,17 @@ class GenesisDataLoader(
val storage = stateStorage.getReadOnlyStorage
val initalRootHash = MerklePatriciaTrie.EmptyRootHash

val stateMptRootHash = genesisData.alloc.zipWithIndex.foldLeft(initalRootHash) { case (rootHash, (((address, AllocAccount(balance)), idx))) =>
val mpt = MerklePatriciaTrie[Array[Byte], Account](rootHash, storage)
val paddedAddress = address.reverse.padTo(addressLength, "0").reverse.mkString
val stateRoot = mpt.put(crypto.kec256(Hex.decode(paddedAddress)),
Account(blockchainConfig.accountStartNonce, UInt256(BigInt(balance)), emptyTrieRootHash, emptyEvmHash)
).getRootHash
stateRoot
val stateMptRootHash = genesisData.alloc.zipWithIndex.foldLeft(initalRootHash) {
case (rootHash, (((address, AllocAccount(balance)), idx))) =>
val mpt = MerklePatriciaTrie[Array[Byte], Account](rootHash, storage)
val paddedAddress = address.reverse.padTo(addressLength, "0").reverse.mkString
val stateRoot = mpt
.put(
crypto.kec256(Hex.decode(paddedAddress)),
Account(blockchainConfig.accountStartNonce, UInt256(BigInt(balance)), emptyTrieRootHash, emptyEvmHash)
)
.getRootHash
stateRoot
}

val header: BlockHeader = prepareHeader(genesisData, stateMptRootHash)
Expand All @@ -108,8 +109,12 @@ class GenesisDataLoader(
log.debug("Genesis data already in the database")
Success(())
case Some(_) =>
Failure(new RuntimeException("Genesis data present in the database does not match genesis block from file." +
" Use different directory for running private blockchains."))
Failure(
new RuntimeException(
"Genesis data present in the database does not match genesis block from file." +
" Use different directory for running private blockchains."
)
)
case None =>
storage.persist()
stateStorage.forcePersist(GenesisDataLoad)
Expand Down Expand Up @@ -158,12 +163,13 @@ object GenesisDataLoader {
case other => throw new RuntimeException("Expected hex string, but got: " + other)
}

object ByteStringJsonSerializer extends CustomSerializer[ByteString](formats =>
(
{ case jv => deserializeByteString(jv) },
PartialFunction.empty
)
)
object ByteStringJsonSerializer
extends CustomSerializer[ByteString](formats =>
(
{ case jv => deserializeByteString(jv) },
PartialFunction.empty
)
)

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ case class GenesisData(
gasLimit: String,
coinbase: ByteString,
timestamp: String,
alloc: Map[String, AllocAccount])
alloc: Map[String, AllocAccount]
)
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ trait BlacklistSupport {
def isBlacklisted(blacklistId: BlackListId): Boolean =
blacklistedPeers.exists(_._1 == blacklistId)

def handleBlacklistMessages: Receive = {
case UnblacklistPeer(ref) => undoBlacklist(ref)
def handleBlacklistMessages: Receive = { case UnblacklistPeer(ref) =>
undoBlacklist(ref)
}

private def removeOldestPeer(): Unit = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ class BlockBroadcast(val etcPeerManager: ActorRef, syncConfig: SyncConfig) {
*/
def broadcastBlock(newBlock: NewBlock, handshakedPeers: Map[Peer, PeerInfo]): Unit = {
val peersWithoutBlock = handshakedPeers.collect {
case (peer, peerInfo) if shouldSendNewBlock(newBlock, peerInfo) => peer }.toSet
case (peer, peerInfo) if shouldSendNewBlock(newBlock, peerInfo) => peer
}.toSet

broadcastNewBlock(newBlock, peersWithoutBlock)

Expand All @@ -47,7 +48,6 @@ class BlockBroadcast(val etcPeerManager: ActorRef, syncConfig: SyncConfig) {
etcPeerManager ! EtcPeerManagerActor.SendMessage(newBlockHashMsg, peer.id)
}


/**
* Obtains a random subset of peers. The returned set will verify:
* subsetPeers.size == sqrt(peers.size)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,22 @@ import io.iohk.ethereum.network.EtcPeerManagerActor
* BlockchainHost actor is in charge of replying to the peer's requests for blockchain data, which includes both
* node and block data.
*/
class BlockchainHostActor(blockchain: Blockchain, peerConfiguration: PeerConfiguration,
peerEventBusActor: ActorRef, etcPeerManagerActor: ActorRef) extends Actor with ActorLogging {
class BlockchainHostActor(
blockchain: Blockchain,
peerConfiguration: PeerConfiguration,
peerEventBusActor: ActorRef,
etcPeerManagerActor: ActorRef
) extends Actor
with ActorLogging {

private val requestMsgsCodes = Set(GetNodeData.code, GetReceipts.code, GetBlockBodies.code, GetBlockHeaders.code)
peerEventBusActor ! Subscribe(MessageClassifier(requestMsgsCodes, PeerSelector.AllPeers))

override def receive: Receive = {
case MessageFromPeer(message, peerId) =>
val responseOpt = handleBlockFastDownload(message) orElse handleEvmCodeMptFastDownload(message)
responseOpt.foreach{ response =>
etcPeerManagerActor ! EtcPeerManagerActor.SendMessage(response, peerId)
}
override def receive: Receive = { case MessageFromPeer(message, peerId) =>
val responseOpt = handleBlockFastDownload(message) orElse handleEvmCodeMptFastDownload(message)
responseOpt.foreach { response =>
etcPeerManagerActor ! EtcPeerManagerActor.SendMessage(response, peerId)
}
}

/**
Expand All @@ -40,7 +44,8 @@ class BlockchainHostActor(blockchain: Blockchain, peerConfiguration: PeerConfigu
*/
private def handleEvmCodeMptFastDownload(message: Message): Option[MessageSerializable] = message match {
case GetNodeData(mptElementsHashes) =>
val hashesRequested = mptElementsHashes.take(peerConfiguration.fastSyncHostConfiguration.maxMptComponentsPerMessage)
val hashesRequested =
mptElementsHashes.take(peerConfiguration.fastSyncHostConfiguration.maxMptComponentsPerMessage)

val nodeData: Seq[ByteString] = hashesRequested.flatMap { hash =>
//Fetch mpt node by hash
Expand All @@ -63,13 +68,15 @@ class BlockchainHostActor(blockchain: Blockchain, peerConfiguration: PeerConfigu
*/
private def handleBlockFastDownload(message: Message): Option[MessageSerializable] = message match {
case request: GetReceipts =>
val receipts = request.blockHashes.take(peerConfiguration.fastSyncHostConfiguration.maxReceiptsPerMessage)
val receipts = request.blockHashes
.take(peerConfiguration.fastSyncHostConfiguration.maxReceiptsPerMessage)
.flatMap(hash => blockchain.getReceiptsByHash(hash))

Some(Receipts(receipts))

case request: GetBlockBodies =>
val blockBodies = request.hashes.take(peerConfiguration.fastSyncHostConfiguration.maxBlocksBodiesPerMessage)
val blockBodies = request.hashes
.take(peerConfiguration.fastSyncHostConfiguration.maxBlocksBodiesPerMessage)
.flatMap(hash => blockchain.getBlockBodyByHash(hash))

Some(BlockBodies(blockBodies))
Expand All @@ -79,8 +86,8 @@ class BlockchainHostActor(blockchain: Blockchain, peerConfiguration: PeerConfigu

blockNumber match {
case Some(startBlockNumber) if startBlockNumber >= 0 && request.maxHeaders >= 0 && request.skip >= 0 =>

val headersCount: BigInt = request.maxHeaders min peerConfiguration.fastSyncHostConfiguration.maxBlocksHeadersPerMessage
val headersCount: BigInt =
request.maxHeaders min peerConfiguration.fastSyncHostConfiguration.maxBlocksHeadersPerMessage

val range = if (request.reverse) {
startBlockNumber to (startBlockNumber - (request.skip + 1) * headersCount + 1) by -(request.skip + 1)
Expand All @@ -105,8 +112,12 @@ class BlockchainHostActor(blockchain: Blockchain, peerConfiguration: PeerConfigu

object BlockchainHostActor {

def props(blockchain: Blockchain, peerConfiguration: PeerConfiguration,
peerEventBusActor: ActorRef, etcPeerManagerActor: ActorRef): Props =
def props(
blockchain: Blockchain,
peerConfiguration: PeerConfiguration,
peerEventBusActor: ActorRef,
etcPeerManagerActor: ActorRef
): Props =
Props(new BlockchainHostActor(blockchain, peerConfiguration, peerEventBusActor, etcPeerManagerActor))

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ trait FastSyncReceiptsValidator {
*/
def validateReceipts(requestedHashes: Seq[ByteString], receipts: Seq[Seq[Receipt]]): ReceiptsValidationResult = {
val blockHashesWithReceipts = requestedHashes.zip(receipts)
val blockHeadersWithReceipts = blockHashesWithReceipts.map{ case (hash, blockReceipts) =>
blockchain.getBlockHeaderByHash(hash) -> blockReceipts }
val blockHeadersWithReceipts = blockHashesWithReceipts.map { case (hash, blockReceipts) =>
blockchain.getBlockHeaderByHash(hash) -> blockReceipts
}

val receiptsValidationError = blockHeadersWithReceipts.collectFirst {
case (Some(header), receipt) if validators.blockValidator.validateBlockAndReceipts(header, receipt).isLeft =>
Expand Down
Loading

0 comments on commit 70c8dc5

Please sign in to comment.