Skip to content

Commit 922b205

Browse files
Igor Grahovacdzajkowski
authored andcommitted
ETCM-697: Added initial import raw block test method
Signed-off-by: Igor Grahovac <[email protected]>
1 parent d276b87 commit 922b205

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

src/main/scala/io/iohk/ethereum/jsonrpc/JsonRpcController.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,8 @@ case class JsonRpcController(
245245
handle[ModifyTimestampRequest, ModifyTimestampResponse](testService.modifyTimestamp, req)
246246
case req @ JsonRpcRequest(_, "test_rewindToBlock", _, _) =>
247247
handle[RewindToBlockRequest, RewindToBlockResponse](testService.rewindToBlock, req)
248+
case req @ JsonRpcRequest(_, "test_importRawBlock", _, _) =>
249+
handle[ImportRawBlockRequest, ImportRawBlockResponse](testService.importRawBlock, req)
248250
case req @ JsonRpcRequest(_, "miner_setEtherbase", _, _) =>
249251
handle[SetEtherbaseRequest, SetEtherbaseResponse](testService.setEtherbase, req)
250252
}

src/main/scala/io/iohk/ethereum/jsonrpc/TestJsonMethodsImplicits.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,18 @@ object TestJsonMethodsImplicits extends JsonMethodsImplicits {
131131
override def encodeJson(t: RewindToBlockResponse): JValue = true
132132
}
133133

134+
implicit val test_importRawBlock = new JsonMethodDecoder[ImportRawBlockRequest]
135+
with JsonEncoder[ImportRawBlockResponse] {
136+
def decodeJson(params: Option[JArray]): Either[JsonRpcError, ImportRawBlockRequest] =
137+
params match {
138+
case Some(JArray(JString(blockRlp) :: Nil)) =>
139+
Right(ImportRawBlockRequest(blockRlp))
140+
case _ => Left(InvalidParams())
141+
}
142+
143+
override def encodeJson(t: ImportRawBlockResponse): JValue = t.blockHash
144+
}
145+
134146
implicit val miner_setEtherbase = new JsonMethodDecoder[SetEtherbaseRequest] with JsonEncoder[SetEtherbaseResponse] {
135147
def decodeJson(params: Option[JArray]): Either[JsonRpcError, SetEtherbaseRequest] =
136148
params match {

src/main/scala/io/iohk/ethereum/jsonrpc/TestService.scala

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,23 @@ package io.iohk.ethereum.jsonrpc
22

33
import akka.actor.ActorRef
44
import akka.util.{ByteString, Timeout}
5-
import cats.syntax.functor._
65
import io.iohk.ethereum.blockchain.data.{GenesisAccount, GenesisData, GenesisDataLoader}
76
import io.iohk.ethereum.consensus.ConsensusConfig
87
import io.iohk.ethereum.consensus.blocks._
8+
import io.iohk.ethereum.domain.Block._
99
import io.iohk.ethereum.domain.{Address, Block, BlockchainImpl, UInt256}
10+
import io.iohk.ethereum.ledger._
1011
import io.iohk.ethereum.testmode.{TestLedgerWrapper, TestmodeConsensus}
1112
import io.iohk.ethereum.transactions.PendingTransactionsManager
1213
import io.iohk.ethereum.transactions.PendingTransactionsManager.PendingTransactionsResponse
13-
import io.iohk.ethereum.utils.Logger
14+
import io.iohk.ethereum.utils.{ByteStringUtils, Logger}
1415
import monix.eval.Task
1516
import monix.execution.Scheduler
1617
import org.bouncycastle.util.encoders.Hex
18+
import io.iohk.ethereum.jsonrpc.JsonMethodsImplicits._
19+
1720
import scala.concurrent.duration._
18-
import scala.util.Try
21+
import scala.util.{Failure, Success, Try}
1922

2023
object TestService {
2124
case class GenesisParams(
@@ -56,6 +59,9 @@ object TestService {
5659

5760
case class SetEtherbaseRequest(etherbase: Address)
5861
case class SetEtherbaseResponse()
62+
63+
case class ImportRawBlockRequest(blockRlp: String)
64+
case class ImportRawBlockResponse(blockHash: String)
5965
}
6066

6167
class TestService(
@@ -139,6 +145,27 @@ class TestService(
139145
Task.now(Right(RewindToBlockResponse()))
140146
}
141147

148+
def importRawBlock(request: ImportRawBlockRequest): ServiceResponse[ImportRawBlockResponse] = {
149+
Try(decode(request.blockRlp).toBlock) match {
150+
case Failure(_) => Task.now(Left(JsonRpcError(-1, "block validation failed!", None)))
151+
case Success(value) => {
152+
testLedgerWrapper.ledger
153+
.importBlock(value)
154+
.flatMap(handleResult)
155+
}
156+
}
157+
}
158+
159+
private def handleResult(blockImportResult: BlockImportResult): ServiceResponse[ImportRawBlockResponse] = {
160+
blockImportResult match {
161+
case BlockImportedToTop(blockImportData) => {
162+
consensus.blockTimestamp += 1
163+
Task.now(Right(ImportRawBlockResponse(ByteStringUtils.hash2string(blockImportData.head.block.hash))))
164+
}
165+
case _ => Task.now(Left(JsonRpcError(-1, "block validation failed!", None)))
166+
}
167+
}
168+
142169
def setEtherbase(req: SetEtherbaseRequest): ServiceResponse[SetEtherbaseResponse] = {
143170
etherbase = req.etherbase
144171
Task.now(Right(SetEtherbaseResponse()))

0 commit comments

Comments
 (0)