diff --git a/src/main/scala/io/iohk/ethereum/jsonrpc/DebugJsonMethodsImplicits.scala b/src/main/scala/io/iohk/ethereum/jsonrpc/DebugJsonMethodsImplicits.scala index ca4714c008..fa5f1544b3 100644 --- a/src/main/scala/io/iohk/ethereum/jsonrpc/DebugJsonMethodsImplicits.scala +++ b/src/main/scala/io/iohk/ethereum/jsonrpc/DebugJsonMethodsImplicits.scala @@ -1,6 +1,13 @@ 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} @@ -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[JsonAST.JArray] + ): Either[JsonRpcError, GetSpecificBlockRequest] = + params match { + case Some(JArray(JInt(blockNumber) :: Nil)) => + Right(GetSpecificBlockRequest(blockNumber.bigInteger)) + case _ => + Left(InvalidParams()) + } + + override def encodeJson(t: GetSpecificBlockResponse): json4s.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..26162f29cb 100644 --- a/src/main/scala/io/iohk/ethereum/jsonrpc/DebugService.scala +++ b/src/main/scala/io/iohk/ethereum/jsonrpc/DebugService.scala @@ -14,6 +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) { @@ -41,4 +44,27 @@ 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()) + +// Task(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..4f24370a6a 100644 --- a/src/main/scala/io/iohk/ethereum/jsonrpc/JsonRpcController.scala +++ b/src/main/scala/io/iohk/ethereum/jsonrpc/JsonRpcController.scala @@ -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.getSpecificBlockRequest, req) } private def handleTestRequest: PartialFunction[JsonRpcRequest, Task[JsonRpcResponse]] = {