@@ -2,20 +2,23 @@ package io.iohk.ethereum.jsonrpc
2
2
3
3
import akka .actor .ActorRef
4
4
import akka .util .{ByteString , Timeout }
5
- import cats .syntax .functor ._
6
5
import io .iohk .ethereum .blockchain .data .{GenesisAccount , GenesisData , GenesisDataLoader }
7
6
import io .iohk .ethereum .consensus .ConsensusConfig
8
7
import io .iohk .ethereum .consensus .blocks ._
8
+ import io .iohk .ethereum .domain .Block ._
9
9
import io .iohk .ethereum .domain .{Address , Block , BlockchainImpl , UInt256 }
10
+ import io .iohk .ethereum .ledger ._
10
11
import io .iohk .ethereum .testmode .{TestLedgerWrapper , TestmodeConsensus }
11
12
import io .iohk .ethereum .transactions .PendingTransactionsManager
12
13
import io .iohk .ethereum .transactions .PendingTransactionsManager .PendingTransactionsResponse
13
- import io .iohk .ethereum .utils .Logger
14
+ import io .iohk .ethereum .utils .{ ByteStringUtils , Logger }
14
15
import monix .eval .Task
15
16
import monix .execution .Scheduler
16
17
import org .bouncycastle .util .encoders .Hex
18
+ import io .iohk .ethereum .jsonrpc .JsonMethodsImplicits ._
19
+
17
20
import scala .concurrent .duration ._
18
- import scala .util .Try
21
+ import scala .util .{ Failure , Success , Try }
19
22
20
23
object TestService {
21
24
case class GenesisParams (
@@ -56,6 +59,9 @@ object TestService {
56
59
57
60
case class SetEtherbaseRequest (etherbase : Address )
58
61
case class SetEtherbaseResponse ()
62
+
63
+ case class ImportRawBlockRequest (blockRlp : String )
64
+ case class ImportRawBlockResponse (blockHash : String )
59
65
}
60
66
61
67
class TestService (
@@ -139,6 +145,27 @@ class TestService(
139
145
Task .now(Right (RewindToBlockResponse ()))
140
146
}
141
147
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
+
142
169
def setEtherbase (req : SetEtherbaseRequest ): ServiceResponse [SetEtherbaseResponse ] = {
143
170
etherbase = req.etherbase
144
171
Task .now(Right (SetEtherbaseResponse ()))
0 commit comments