Skip to content

Commit

Permalink
[ETCM-797] add debug endpoint to request a specific block (by number)…
Browse files Browse the repository at this point in the history
… from connected peers
  • Loading branch information
Jaap van der Plas committed Apr 26, 2021
1 parent d91a129 commit 649c810
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -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)
}
}
29 changes: 28 additions & 1 deletion src/main/scala/io/iohk/ethereum/jsonrpc/DebugService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@ 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._

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 {
Expand All @@ -41,4 +45,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()))
}
}
Original file line number Diff line number Diff line change
@@ -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._
Expand Down Expand Up @@ -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]] = {
Expand Down

0 comments on commit 649c810

Please sign in to comment.