From 6acde52a633cd0dda5a5ee1ca5622abdefed8149 Mon Sep 17 00:00:00 2001 From: Bogdan Suierica Date: Mon, 26 Apr 2021 15:18:16 +0200 Subject: [PATCH 1/6] fix bug seed calculation --- .../iohk/ethereum/consensus/pow/EthashDAGManager.scala | 4 ++-- .../io/iohk/ethereum/consensus/pow/EthashMiner.scala | 3 ++- .../io/iohk/ethereum/consensus/pow/EthashUtils.scala | 10 ++++++++-- .../pow/validators/EthashBlockHeaderValidator.scala | 2 +- .../io/iohk/ethereum/jsonrpc/EthMiningService.scala | 4 +++- .../io/iohk/ethereum/nodebuilder/NodeBuilder.scala | 2 ++ .../iohk/ethereum/consensus/pow/EthashUtilsSpec.scala | 6 +++--- .../iohk/ethereum/jsonrpc/EthMiningServiceSpec.scala | 1 + .../ethereum/jsonrpc/JsonRpcControllerFixture.scala | 1 + 9 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/main/scala/io/iohk/ethereum/consensus/pow/EthashDAGManager.scala b/src/main/scala/io/iohk/ethereum/consensus/pow/EthashDAGManager.scala index b989e69827..77f8a9e917 100644 --- a/src/main/scala/io/iohk/ethereum/consensus/pow/EthashDAGManager.scala +++ b/src/main/scala/io/iohk/ethereum/consensus/pow/EthashDAGManager.scala @@ -13,11 +13,11 @@ class EthashDAGManager(blockCreator: EthashBlockCreator) extends Logger { var currentEpochDagSize: Option[Long] = None var currentEpochDag: Option[Array[Array[Int]]] = None - def calculateDagSize(blockNumber: Long, epoch: Long): (Array[Array[Int]], Long) = { + def calculateDagSize(blockNumber: Long, epoch: Long, ecip1099ActivationBlock: Long): (Array[Array[Int]], Long) = { (currentEpoch, currentEpochDag, currentEpochDagSize) match { case (Some(`epoch`), Some(dag), Some(dagSize)) => (dag, dagSize) case _ => - val seed = EthashUtils.seed(blockNumber) + val seed = EthashUtils.seed(blockNumber, ecip1099ActivationBlock) val dagSize = EthashUtils.dagSize(epoch) val dagNumHashes = (dagSize / EthashUtils.HASH_BYTES).toInt val dag = diff --git a/src/main/scala/io/iohk/ethereum/consensus/pow/EthashMiner.scala b/src/main/scala/io/iohk/ethereum/consensus/pow/EthashMiner.scala index 29fea3c582..d0cfaf45e1 100644 --- a/src/main/scala/io/iohk/ethereum/consensus/pow/EthashMiner.scala +++ b/src/main/scala/io/iohk/ethereum/consensus/pow/EthashMiner.scala @@ -100,7 +100,8 @@ class EthashMiner( private def doEthashMining(blockNumber: Long, block: Block): (Long, MiningResult) = { val epoch = EthashUtils.epoch(blockNumber, blockCreator.blockchainConfig.ecip1099BlockNumber.toLong) - val (dag, dagSize) = dagManager.calculateDagSize(blockNumber, epoch) + val (dag, dagSize) = + dagManager.calculateDagSize(blockNumber, epoch, blockCreator.blockchainConfig.ecip1099BlockNumber.toLong) val headerHash = crypto.kec256(BlockHeader.getEncodedWithoutNonce(block.header)) val startTime = System.nanoTime() val mineResult = diff --git a/src/main/scala/io/iohk/ethereum/consensus/pow/EthashUtils.scala b/src/main/scala/io/iohk/ethereum/consensus/pow/EthashUtils.scala index b49a7edc90..8a0df25b31 100644 --- a/src/main/scala/io/iohk/ethereum/consensus/pow/EthashUtils.scala +++ b/src/main/scala/io/iohk/ethereum/consensus/pow/EthashUtils.scala @@ -63,12 +63,18 @@ object EthashUtils { private def epochBeforeEcip1099(blockNumber: Long): Long = blockNumber / EPOCH_LENGTH_BEFORE_ECIP_1099 - def seed(blockNumber: Long): ByteString = { - val epoch = epochBeforeEcip1099(blockNumber) + def seed(blockNumber: Long, ecip1099ActivationBlock: Long): ByteString = { + val epoch: Long = epochBeforeEcip1099(startBlock(blockNumber, ecip1099ActivationBlock)) (BigInt(0) until epoch) .foldLeft(ByteString(Hex.decode("00" * 32))) { case (b, _) => kec256(b) } } + def startBlock(blockNumber: Long, ecip1099ActivationBlock: Long): Long = + (blockNumber / calcEpochLength(blockNumber, ecip1099ActivationBlock)) * calcEpochLength( + blockNumber, + ecip1099ActivationBlock + ) + 1 + private def calcEpochLength(blockNumber: Long, ecip1099ActivationBlock: Long): Long = if (blockNumber < ecip1099ActivationBlock) EPOCH_LENGTH_BEFORE_ECIP_1099 else EPOCH_LENGTH_AFTER_ECIP_1099 diff --git a/src/main/scala/io/iohk/ethereum/consensus/pow/validators/EthashBlockHeaderValidator.scala b/src/main/scala/io/iohk/ethereum/consensus/pow/validators/EthashBlockHeaderValidator.scala index 7c38135673..b37a6f8f1e 100644 --- a/src/main/scala/io/iohk/ethereum/consensus/pow/validators/EthashBlockHeaderValidator.scala +++ b/src/main/scala/io/iohk/ethereum/consensus/pow/validators/EthashBlockHeaderValidator.scala @@ -42,7 +42,7 @@ class EthashBlockHeaderValidator(blockchainConfig: BlockchainConfig) { } val epoch = EthashUtils.epoch(blockHeader.number.toLong, blockchainConfig.ecip1099BlockNumber.toLong) - val seed = EthashUtils.seed(blockHeader.number.toLong) + val seed = EthashUtils.seed(blockHeader.number.toLong, blockchainConfig.ecip1099BlockNumber.toLong) val powCacheData = getPowCacheData(epoch, seed) val proofOfWork = hashimotoLight( diff --git a/src/main/scala/io/iohk/ethereum/jsonrpc/EthMiningService.scala b/src/main/scala/io/iohk/ethereum/jsonrpc/EthMiningService.scala index 8d0907ddb5..145cc0d3ea 100644 --- a/src/main/scala/io/iohk/ethereum/jsonrpc/EthMiningService.scala +++ b/src/main/scala/io/iohk/ethereum/jsonrpc/EthMiningService.scala @@ -21,6 +21,7 @@ import scala.collection.concurrent.{TrieMap, Map => ConcurrentMap} import scala.concurrent.duration.FiniteDuration import scala.language.existentials import io.iohk.ethereum.transactions.TransactionPicker +import io.iohk.ethereum.utils.BlockchainConfig object EthMiningService { @@ -45,6 +46,7 @@ object EthMiningService { class EthMiningService( blockchain: Blockchain, + blockchainConfig: BlockchainConfig, ledger: Ledger, jsonRpcConfig: JsonRpcConfig, ommersPool: ActorRef, @@ -92,7 +94,7 @@ class EthMiningService( Right( GetWorkResponse( powHeaderHash = ByteString(kec256(BlockHeader.getEncodedWithoutNonce(pb.block.header))), - dagSeed = EthashUtils.seed(pb.block.header.number.toLong), + dagSeed = EthashUtils.seed(pb.block.header.number.toLong, blockchainConfig.ecip1099BlockNumber.toLong), target = ByteString((BigInt(2).pow(256) / pb.block.header.difficulty).toByteArray) ) ) diff --git a/src/main/scala/io/iohk/ethereum/nodebuilder/NodeBuilder.scala b/src/main/scala/io/iohk/ethereum/nodebuilder/NodeBuilder.scala index be4a3920cd..dd2cada695 100644 --- a/src/main/scala/io/iohk/ethereum/nodebuilder/NodeBuilder.scala +++ b/src/main/scala/io/iohk/ethereum/nodebuilder/NodeBuilder.scala @@ -400,6 +400,7 @@ trait EthInfoServiceBuilder { trait EthMiningServiceBuilder { self: BlockchainBuilder + with BlockchainConfigBuilder with LedgerBuilder with JSONRpcConfigBuilder with OmmersPoolBuilder @@ -409,6 +410,7 @@ trait EthMiningServiceBuilder { lazy val ethMiningService = new EthMiningService( blockchain, + blockchainConfig, ledger, jsonRpcConfig, ommersPool, diff --git a/src/test/scala/io/iohk/ethereum/consensus/pow/EthashUtilsSpec.scala b/src/test/scala/io/iohk/ethereum/consensus/pow/EthashUtilsSpec.scala index 2c1a5a0a68..0e8e6c777d 100644 --- a/src/test/scala/io/iohk/ethereum/consensus/pow/EthashUtilsSpec.scala +++ b/src/test/scala/io/iohk/ethereum/consensus/pow/EthashUtilsSpec.scala @@ -19,7 +19,7 @@ class EthashUtilsSpec extends AnyFlatSpec with Matchers with ScalaCheckPropertyC "Ethash" should "generate correct hash" in { forAll(Gen.choose[Long](0, 15000000L)) { blockNumber => - seed(blockNumber) shouldBe seedForBlockReference(blockNumber) + seed(blockNumber, ecip1099forkBlockNumber) shouldBe seedForBlockReference(blockNumber) } } @@ -55,7 +55,7 @@ class EthashUtilsSpec extends AnyFlatSpec with Matchers with ScalaCheckPropertyC val blockNumber = 486382 val _epoch = epoch(blockNumber, ecip1099forkBlockNumber) - val _seed = seed(blockNumber) + val _seed = seed(blockNumber, ecip1099forkBlockNumber) val cache = makeCache(_epoch, _seed) val proofOfWork = hashimotoLight(hash, nonce, dagSize(_epoch), cache) @@ -129,7 +129,7 @@ class EthashUtilsSpec extends AnyFlatSpec with Matchers with ScalaCheckPropertyC forAll(table) { (blockNumber, hashWithoutNonce, nonce, mixHash) => val _epoch = epoch(blockNumber, ecip1099forkBlockNumber) - val _seed = seed(blockNumber) + val _seed = seed(blockNumber, ecip1099forkBlockNumber) val cache = makeCache(_epoch, _seed) val proofOfWork = hashimotoLight(Hex.decode(hashWithoutNonce), Hex.decode(nonce), dagSize(_epoch), cache) diff --git a/src/test/scala/io/iohk/ethereum/jsonrpc/EthMiningServiceSpec.scala b/src/test/scala/io/iohk/ethereum/jsonrpc/EthMiningServiceSpec.scala index dd98168e12..7f293233ff 100644 --- a/src/test/scala/io/iohk/ethereum/jsonrpc/EthMiningServiceSpec.scala +++ b/src/test/scala/io/iohk/ethereum/jsonrpc/EthMiningServiceSpec.scala @@ -249,6 +249,7 @@ class EthMiningServiceSpec lazy val ethMiningService = new EthMiningService( blockchain, + blockchainConfig, ledger, jsonRpcConfig, ommersPool.ref, diff --git a/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerFixture.scala b/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerFixture.scala index d7f6be4f33..873ca8eb7f 100644 --- a/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerFixture.scala +++ b/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerFixture.scala @@ -85,6 +85,7 @@ class JsonRpcControllerFixture(implicit system: ActorSystem) val ethMiningService = new EthMiningService( blockchain, + blockchainConfig, ledger, config, ommersPool.ref, From d91a12900579bb2ac2e65f561e768f41cc1d0966 Mon Sep 17 00:00:00 2001 From: Jaap van der Plas Date: Mon, 26 Apr 2021 15:48:35 +0200 Subject: [PATCH 2/6] [ETCM-797] add testcase for ecip 1099 seed bug --- .../consensus/pow/EthashUtilsSpec.scala | 32 ++++++++++--------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/test/scala/io/iohk/ethereum/consensus/pow/EthashUtilsSpec.scala b/src/test/scala/io/iohk/ethereum/consensus/pow/EthashUtilsSpec.scala index 0e8e6c777d..61dfeafe45 100644 --- a/src/test/scala/io/iohk/ethereum/consensus/pow/EthashUtilsSpec.scala +++ b/src/test/scala/io/iohk/ethereum/consensus/pow/EthashUtilsSpec.scala @@ -10,6 +10,8 @@ import org.scalatest.matchers.should.Matchers import scala.annotation.tailrec import io.iohk.ethereum.SuperSlow +import io.iohk.ethereum.utils.ByteStringUtils +import org.scalatest.prop.TableFor2 class EthashUtilsSpec extends AnyFlatSpec with Matchers with ScalaCheckPropertyChecks with SuperSlow { @@ -18,8 +20,21 @@ class EthashUtilsSpec extends AnyFlatSpec with Matchers with ScalaCheckPropertyC val ecip1099forkBlockNumber: Long = 11460000 "Ethash" should "generate correct hash" in { - forAll(Gen.choose[Long](0, 15000000L)) { blockNumber => - seed(blockNumber, ecip1099forkBlockNumber) shouldBe seedForBlockReference(blockNumber) + val seedEpoch0 = ByteStringUtils.string2hash("0000000000000000000000000000000000000000000000000000000000000000") + val seedEpoch1 = ByteStringUtils.string2hash("290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563") + val seedEpoch382 = ByteStringUtils.string2hash("d3d0aa11197dcdcfcb3ad3c73d415af47299bddb47fda6081d31d9dd06462f6a") + val seedEpoch383 = ByteStringUtils.string2hash("bf532874eb434842e7a3e4acd113fe454541651872760d9b95d11d7f90ca25dc") + val table: TableFor2[Long, ByteString] = Table( + ("blockNumber", "referenceSeed"), + (0, seedEpoch0), + (1, seedEpoch0), + (30_000, seedEpoch1), + (ecip1099forkBlockNumber, seedEpoch382), + (ecip1099forkBlockNumber + 30_000, seedEpoch382), + (ecip1099forkBlockNumber + 60_000, seedEpoch383) + ) + forAll(table) { (blockNumber, referenceSeed) => + seed(blockNumber, ecip1099forkBlockNumber) shouldBe referenceSeed } } @@ -137,17 +152,4 @@ class EthashUtilsSpec extends AnyFlatSpec with Matchers with ScalaCheckPropertyC } } } - - def seedForBlockReference(blockNumber: BigInt): ByteString = { - @tailrec - def go(current: BigInt, currentHash: ByteString): ByteString = { - if (current < EPOCH_LENGTH_BEFORE_ECIP_1099) { - currentHash - } else { - go(current - EPOCH_LENGTH_BEFORE_ECIP_1099, kec256(currentHash)) - } - } - - go(blockNumber, ByteString(Hex.decode("00" * 32))) - } } From 48b39b57ed135da493997b705ac9430df16aab50 Mon Sep 17 00:00:00 2001 From: Jaap van der Plas Date: Mon, 26 Apr 2021 16:05:51 +0200 Subject: [PATCH 3/6] [ETCM-797] add debug endpoint to request a specific block (by number) from connected peers --- .../jsonrpc/DebugJsonMethodsImplicits.scala | 26 +++++++++++++++++-- .../iohk/ethereum/jsonrpc/DebugService.scala | 26 ++++++++++++++++++- .../ethereum/jsonrpc/JsonRpcController.scala | 4 ++- 3 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/main/scala/io/iohk/ethereum/jsonrpc/DebugJsonMethodsImplicits.scala b/src/main/scala/io/iohk/ethereum/jsonrpc/DebugJsonMethodsImplicits.scala index ca4714c008..00d1a056d0 100644 --- a/src/main/scala/io/iohk/ethereum/jsonrpc/DebugJsonMethodsImplicits.scala +++ b/src/main/scala/io/iohk/ethereum/jsonrpc/DebugJsonMethodsImplicits.scala @@ -1,9 +1,16 @@ package io.iohk.ethereum.jsonrpc -import io.iohk.ethereum.jsonrpc.DebugService.{ListPeersInfoRequest, ListPeersInfoResponse} +import io.circe.Json.JNumber +import io.iohk.ethereum.jsonrpc.DebugService.{ + GetSpecificBlockRequest, + GetSpecificBlockResponse, + ListPeersInfoRequest, + ListPeersInfoResponse +} +import io.iohk.ethereum.jsonrpc.JsonRpcError.{InvalidParams, ParseError} import io.iohk.ethereum.jsonrpc.serialization.JsonMethodDecoder.NoParamsMethodDecoder import io.iohk.ethereum.jsonrpc.serialization.{JsonEncoder, JsonMethodCodec} -import org.json4s.JsonAST.{JArray, JString, JValue} +import org.json4s.JsonAST._ object DebugJsonMethodsImplicits extends JsonMethodsImplicits { @@ -12,4 +19,19 @@ object DebugJsonMethodsImplicits extends JsonMethodsImplicits { def encodeJson(t: ListPeersInfoResponse): JValue = JArray(t.peers.map(a => JString(a.toString))) } + + implicit val debug_getSpecificBlockRequest: JsonMethodCodec[GetSpecificBlockRequest, GetSpecificBlockResponse] = + new JsonMethodCodec[GetSpecificBlockRequest, GetSpecificBlockResponse] { + override def decodeJson( + params: Option[JArray] + ): Either[JsonRpcError, GetSpecificBlockRequest] = + params match { + case Some(JArray(JInt(blockNumber) :: Nil)) => + Right(GetSpecificBlockRequest(blockNumber.bigInteger)) + case _ => + Left(InvalidParams()) + } + + override def encodeJson(t: GetSpecificBlockResponse): JValue = JBool(true) + } } diff --git a/src/main/scala/io/iohk/ethereum/jsonrpc/DebugService.scala b/src/main/scala/io/iohk/ethereum/jsonrpc/DebugService.scala index 171545fd7a..12e9b74f80 100644 --- a/src/main/scala/io/iohk/ethereum/jsonrpc/DebugService.scala +++ b/src/main/scala/io/iohk/ethereum/jsonrpc/DebugService.scala @@ -3,10 +3,10 @@ package io.iohk.ethereum.jsonrpc import akka.actor.ActorRef import akka.util.Timeout import io.iohk.ethereum.jsonrpc.AkkaTaskOps._ -import io.iohk.ethereum.jsonrpc.DebugService.{ListPeersInfoRequest, ListPeersInfoResponse} import io.iohk.ethereum.network.EtcPeerManagerActor.{PeerInfo, PeerInfoResponse} import io.iohk.ethereum.network.PeerManagerActor.Peers import io.iohk.ethereum.network.{EtcPeerManagerActor, Peer, PeerActor, PeerId, PeerManagerActor} +import io.iohk.ethereum.network.p2p.messages.PV62.GetBlockHeaders import monix.eval.Task import scala.concurrent.duration._ @@ -14,9 +14,13 @@ import scala.concurrent.duration._ object DebugService { case class ListPeersInfoRequest() case class ListPeersInfoResponse(peers: List[PeerInfo]) + + case class GetSpecificBlockRequest(number: BigInt) + case class GetSpecificBlockResponse() } class DebugService(peerManager: ActorRef, etcPeerManager: ActorRef) { + import DebugService._ def listPeersInfo(getPeersInfoRequest: ListPeersInfoRequest): ServiceResponse[ListPeersInfoResponse] = { for { @@ -41,4 +45,24 @@ class DebugService(peerManager: ActorRef, etcPeerManager: ActorRef) { .askFor[PeerInfoResponse](EtcPeerManagerActor.PeerInfoRequest(peer)) .map(resp => resp.peerInfo) } + + def getSpecificBlock(req: GetSpecificBlockRequest): ServiceResponse[GetSpecificBlockResponse] = + for { + ids <- getPeerIds + peers <- Task.traverse(ids)(id => getPeerInfo(id).map(id -> _)) + eligiblePeers = peers.collect { + case (id, Some(peerInfo)) if peerInfo.maxBlockNumber >= req.number => id + } + _ <- Task.now { + eligiblePeers.foreach { peerId => + peerManager.tell( + PeerManagerActor.SendMessage( + GetBlockHeaders(Left(req.number - 1), 2, 0, false), + peerId + ), + peerManager + ) + } + } + } yield Right(GetSpecificBlockResponse()) } diff --git a/src/main/scala/io/iohk/ethereum/jsonrpc/JsonRpcController.scala b/src/main/scala/io/iohk/ethereum/jsonrpc/JsonRpcController.scala index 8af330d94c..9b1bada542 100644 --- a/src/main/scala/io/iohk/ethereum/jsonrpc/JsonRpcController.scala +++ b/src/main/scala/io/iohk/ethereum/jsonrpc/JsonRpcController.scala @@ -1,7 +1,7 @@ package io.iohk.ethereum.jsonrpc import io.iohk.ethereum.jsonrpc.CheckpointingService._ -import io.iohk.ethereum.jsonrpc.DebugService.{ListPeersInfoRequest, ListPeersInfoResponse} +import io.iohk.ethereum.jsonrpc.DebugService._ import io.iohk.ethereum.jsonrpc.EthBlocksService._ import io.iohk.ethereum.jsonrpc.EthInfoService._ import io.iohk.ethereum.jsonrpc.EthTxService._ @@ -227,6 +227,8 @@ case class JsonRpcController( private def handleDebugRequest: PartialFunction[JsonRpcRequest, Task[JsonRpcResponse]] = { case req @ JsonRpcRequest(_, "debug_listPeersInfo", _, _) => handle[ListPeersInfoRequest, ListPeersInfoResponse](debugService.listPeersInfo, req) + case req @ JsonRpcRequest(_, "debug_getSpecificBlock", _, _) => + handle[GetSpecificBlockRequest, GetSpecificBlockResponse](debugService.getSpecificBlock, req) } private def handleTestRequest: PartialFunction[JsonRpcRequest, Task[JsonRpcResponse]] = { From aef19cc271cf506ffb248bac67a089c266ef1213 Mon Sep 17 00:00:00 2001 From: Jaap van der Plas Date: Mon, 26 Apr 2021 17:58:12 +0200 Subject: [PATCH 4/6] refactor EthashUtils.seed and add a comment --- .../ethereum/consensus/pow/EthashUtils.scala | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/main/scala/io/iohk/ethereum/consensus/pow/EthashUtils.scala b/src/main/scala/io/iohk/ethereum/consensus/pow/EthashUtils.scala index 8a0df25b31..9aa8c3ed08 100644 --- a/src/main/scala/io/iohk/ethereum/consensus/pow/EthashUtils.scala +++ b/src/main/scala/io/iohk/ethereum/consensus/pow/EthashUtils.scala @@ -61,20 +61,19 @@ object EthashUtils { // scalastyle:on magic.number - private def epochBeforeEcip1099(blockNumber: Long): Long = blockNumber / EPOCH_LENGTH_BEFORE_ECIP_1099 - + // computes seed for epoch of given blockNumber + // this also involves the non-ECIP1099 epoch of the first blocks of the + // ECIP1099 epoch, to make sure every block in the latter results in the same + // seed being calculated, would there be a cache miss. def seed(blockNumber: Long, ecip1099ActivationBlock: Long): ByteString = { - val epoch: Long = epochBeforeEcip1099(startBlock(blockNumber, ecip1099ActivationBlock)) + val epochLength = calcEpochLength(blockNumber, ecip1099ActivationBlock) + val startBlock = (blockNumber / epochLength) * epochLength + 1 + val epoch = startBlock / EPOCH_LENGTH_BEFORE_ECIP_1099 + (BigInt(0) until epoch) .foldLeft(ByteString(Hex.decode("00" * 32))) { case (b, _) => kec256(b) } } - def startBlock(blockNumber: Long, ecip1099ActivationBlock: Long): Long = - (blockNumber / calcEpochLength(blockNumber, ecip1099ActivationBlock)) * calcEpochLength( - blockNumber, - ecip1099ActivationBlock - ) + 1 - private def calcEpochLength(blockNumber: Long, ecip1099ActivationBlock: Long): Long = if (blockNumber < ecip1099ActivationBlock) EPOCH_LENGTH_BEFORE_ECIP_1099 else EPOCH_LENGTH_AFTER_ECIP_1099 From 26d03a0e58e4fb0799b6d2960db7bc5a8b9f662a Mon Sep 17 00:00:00 2001 From: Jaap van der Plas Date: Mon, 26 Apr 2021 18:04:37 +0200 Subject: [PATCH 5/6] Revert "[ETCM-797] add debug endpoint to request a specific block (by number) from connected peers" This reverts commit 48b39b57ed135da493997b705ac9430df16aab50. --- .../jsonrpc/DebugJsonMethodsImplicits.scala | 26 ++----------------- .../iohk/ethereum/jsonrpc/DebugService.scala | 26 +------------------ .../ethereum/jsonrpc/JsonRpcController.scala | 4 +-- 3 files changed, 4 insertions(+), 52 deletions(-) diff --git a/src/main/scala/io/iohk/ethereum/jsonrpc/DebugJsonMethodsImplicits.scala b/src/main/scala/io/iohk/ethereum/jsonrpc/DebugJsonMethodsImplicits.scala index 00d1a056d0..ca4714c008 100644 --- a/src/main/scala/io/iohk/ethereum/jsonrpc/DebugJsonMethodsImplicits.scala +++ b/src/main/scala/io/iohk/ethereum/jsonrpc/DebugJsonMethodsImplicits.scala @@ -1,16 +1,9 @@ package io.iohk.ethereum.jsonrpc -import io.circe.Json.JNumber -import io.iohk.ethereum.jsonrpc.DebugService.{ - GetSpecificBlockRequest, - GetSpecificBlockResponse, - ListPeersInfoRequest, - ListPeersInfoResponse -} -import io.iohk.ethereum.jsonrpc.JsonRpcError.{InvalidParams, ParseError} +import io.iohk.ethereum.jsonrpc.DebugService.{ListPeersInfoRequest, ListPeersInfoResponse} import io.iohk.ethereum.jsonrpc.serialization.JsonMethodDecoder.NoParamsMethodDecoder import io.iohk.ethereum.jsonrpc.serialization.{JsonEncoder, JsonMethodCodec} -import org.json4s.JsonAST._ +import org.json4s.JsonAST.{JArray, JString, JValue} object DebugJsonMethodsImplicits extends JsonMethodsImplicits { @@ -19,19 +12,4 @@ object DebugJsonMethodsImplicits extends JsonMethodsImplicits { def encodeJson(t: ListPeersInfoResponse): JValue = JArray(t.peers.map(a => JString(a.toString))) } - - implicit val debug_getSpecificBlockRequest: JsonMethodCodec[GetSpecificBlockRequest, GetSpecificBlockResponse] = - new JsonMethodCodec[GetSpecificBlockRequest, GetSpecificBlockResponse] { - override def decodeJson( - params: Option[JArray] - ): Either[JsonRpcError, GetSpecificBlockRequest] = - params match { - case Some(JArray(JInt(blockNumber) :: Nil)) => - Right(GetSpecificBlockRequest(blockNumber.bigInteger)) - case _ => - Left(InvalidParams()) - } - - override def encodeJson(t: GetSpecificBlockResponse): JValue = JBool(true) - } } diff --git a/src/main/scala/io/iohk/ethereum/jsonrpc/DebugService.scala b/src/main/scala/io/iohk/ethereum/jsonrpc/DebugService.scala index 12e9b74f80..171545fd7a 100644 --- a/src/main/scala/io/iohk/ethereum/jsonrpc/DebugService.scala +++ b/src/main/scala/io/iohk/ethereum/jsonrpc/DebugService.scala @@ -3,10 +3,10 @@ package io.iohk.ethereum.jsonrpc import akka.actor.ActorRef import akka.util.Timeout import io.iohk.ethereum.jsonrpc.AkkaTaskOps._ +import io.iohk.ethereum.jsonrpc.DebugService.{ListPeersInfoRequest, ListPeersInfoResponse} import io.iohk.ethereum.network.EtcPeerManagerActor.{PeerInfo, PeerInfoResponse} import io.iohk.ethereum.network.PeerManagerActor.Peers import io.iohk.ethereum.network.{EtcPeerManagerActor, Peer, PeerActor, PeerId, PeerManagerActor} -import io.iohk.ethereum.network.p2p.messages.PV62.GetBlockHeaders import monix.eval.Task import scala.concurrent.duration._ @@ -14,13 +14,9 @@ import scala.concurrent.duration._ object DebugService { case class ListPeersInfoRequest() case class ListPeersInfoResponse(peers: List[PeerInfo]) - - case class GetSpecificBlockRequest(number: BigInt) - case class GetSpecificBlockResponse() } class DebugService(peerManager: ActorRef, etcPeerManager: ActorRef) { - import DebugService._ def listPeersInfo(getPeersInfoRequest: ListPeersInfoRequest): ServiceResponse[ListPeersInfoResponse] = { for { @@ -45,24 +41,4 @@ class DebugService(peerManager: ActorRef, etcPeerManager: ActorRef) { .askFor[PeerInfoResponse](EtcPeerManagerActor.PeerInfoRequest(peer)) .map(resp => resp.peerInfo) } - - def getSpecificBlock(req: GetSpecificBlockRequest): ServiceResponse[GetSpecificBlockResponse] = - for { - ids <- getPeerIds - peers <- Task.traverse(ids)(id => getPeerInfo(id).map(id -> _)) - eligiblePeers = peers.collect { - case (id, Some(peerInfo)) if peerInfo.maxBlockNumber >= req.number => id - } - _ <- Task.now { - eligiblePeers.foreach { peerId => - peerManager.tell( - PeerManagerActor.SendMessage( - GetBlockHeaders(Left(req.number - 1), 2, 0, false), - peerId - ), - peerManager - ) - } - } - } yield Right(GetSpecificBlockResponse()) } diff --git a/src/main/scala/io/iohk/ethereum/jsonrpc/JsonRpcController.scala b/src/main/scala/io/iohk/ethereum/jsonrpc/JsonRpcController.scala index 9b1bada542..8af330d94c 100644 --- a/src/main/scala/io/iohk/ethereum/jsonrpc/JsonRpcController.scala +++ b/src/main/scala/io/iohk/ethereum/jsonrpc/JsonRpcController.scala @@ -1,7 +1,7 @@ package io.iohk.ethereum.jsonrpc import io.iohk.ethereum.jsonrpc.CheckpointingService._ -import io.iohk.ethereum.jsonrpc.DebugService._ +import io.iohk.ethereum.jsonrpc.DebugService.{ListPeersInfoRequest, ListPeersInfoResponse} import io.iohk.ethereum.jsonrpc.EthBlocksService._ import io.iohk.ethereum.jsonrpc.EthInfoService._ import io.iohk.ethereum.jsonrpc.EthTxService._ @@ -227,8 +227,6 @@ case class JsonRpcController( private def handleDebugRequest: PartialFunction[JsonRpcRequest, Task[JsonRpcResponse]] = { case req @ JsonRpcRequest(_, "debug_listPeersInfo", _, _) => handle[ListPeersInfoRequest, ListPeersInfoResponse](debugService.listPeersInfo, req) - case req @ JsonRpcRequest(_, "debug_getSpecificBlock", _, _) => - handle[GetSpecificBlockRequest, GetSpecificBlockResponse](debugService.getSpecificBlock, req) } private def handleTestRequest: PartialFunction[JsonRpcRequest, Task[JsonRpcResponse]] = { From 1cc751bac475e1ac4dc46fad93ed0665edd14b12 Mon Sep 17 00:00:00 2001 From: Jaap van der Plas Date: Mon, 26 Apr 2021 18:04:49 +0200 Subject: [PATCH 6/6] remove ecip1099ActivationBlock parameter from EthashDAGManager --- .../io/iohk/ethereum/consensus/pow/EthashDAGManager.scala | 4 ++-- .../scala/io/iohk/ethereum/consensus/pow/EthashMiner.scala | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/scala/io/iohk/ethereum/consensus/pow/EthashDAGManager.scala b/src/main/scala/io/iohk/ethereum/consensus/pow/EthashDAGManager.scala index 77f8a9e917..7072ed033b 100644 --- a/src/main/scala/io/iohk/ethereum/consensus/pow/EthashDAGManager.scala +++ b/src/main/scala/io/iohk/ethereum/consensus/pow/EthashDAGManager.scala @@ -13,11 +13,11 @@ class EthashDAGManager(blockCreator: EthashBlockCreator) extends Logger { var currentEpochDagSize: Option[Long] = None var currentEpochDag: Option[Array[Array[Int]]] = None - def calculateDagSize(blockNumber: Long, epoch: Long, ecip1099ActivationBlock: Long): (Array[Array[Int]], Long) = { + def calculateDagSize(blockNumber: Long, epoch: Long): (Array[Array[Int]], Long) = { (currentEpoch, currentEpochDag, currentEpochDagSize) match { case (Some(`epoch`), Some(dag), Some(dagSize)) => (dag, dagSize) case _ => - val seed = EthashUtils.seed(blockNumber, ecip1099ActivationBlock) + val seed = EthashUtils.seed(blockNumber, blockCreator.blockchainConfig.ecip1099BlockNumber.toLong) val dagSize = EthashUtils.dagSize(epoch) val dagNumHashes = (dagSize / EthashUtils.HASH_BYTES).toInt val dag = diff --git a/src/main/scala/io/iohk/ethereum/consensus/pow/EthashMiner.scala b/src/main/scala/io/iohk/ethereum/consensus/pow/EthashMiner.scala index d0cfaf45e1..29fea3c582 100644 --- a/src/main/scala/io/iohk/ethereum/consensus/pow/EthashMiner.scala +++ b/src/main/scala/io/iohk/ethereum/consensus/pow/EthashMiner.scala @@ -100,8 +100,7 @@ class EthashMiner( private def doEthashMining(blockNumber: Long, block: Block): (Long, MiningResult) = { val epoch = EthashUtils.epoch(blockNumber, blockCreator.blockchainConfig.ecip1099BlockNumber.toLong) - val (dag, dagSize) = - dagManager.calculateDagSize(blockNumber, epoch, blockCreator.blockchainConfig.ecip1099BlockNumber.toLong) + val (dag, dagSize) = dagManager.calculateDagSize(blockNumber, epoch) val headerHash = crypto.kec256(BlockHeader.getEncodedWithoutNonce(block.header)) val startTime = System.nanoTime() val mineResult =