From be6ec10dbe7a107bf2da8db27e341501ddbf16a2 Mon Sep 17 00:00:00 2001 From: lemastero Date: Tue, 13 Oct 2020 17:45:00 +0200 Subject: [PATCH 1/7] [ETCM-126] rename JsonRpcControllerSpec => JsonRpcControllerEthSpec --- ...onRpcControllerSpec.scala => JsonRpcControllerEthSpec.scala} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename src/test/scala/io/iohk/ethereum/jsonrpc/{JsonRpcControllerSpec.scala => JsonRpcControllerEthSpec.scala} (99%) diff --git a/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerSpec.scala b/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerEthSpec.scala similarity index 99% rename from src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerSpec.scala rename to src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerEthSpec.scala index eae51f4142..befd649052 100644 --- a/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerSpec.scala +++ b/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerEthSpec.scala @@ -48,7 +48,7 @@ import org.scalatest.matchers.should.Matchers // scalastyle:off file.size.limit // scalastyle:off magic.number -class JsonRpcControllerSpec +class JsonRpcControllerEthSpec extends AnyFlatSpec with Matchers with JRCMatchers From 53283b73265c070be4b22062a3bbad7fab577a04 Mon Sep 17 00:00:00 2001 From: lemastero Date: Tue, 13 Oct 2020 18:41:08 +0200 Subject: [PATCH 2/7] [ETCM-126] add helper functions newJsonRpcRequest newJsonRpcController and simplify tests using them --- .../jsonrpc/JsonRpcControllerEthSpec.scala | 994 +++++------------- .../io/iohk/ethereum/jsonrpc/TestSetup.scala | 119 +++ 2 files changed, 369 insertions(+), 744 deletions(-) create mode 100644 src/test/scala/io/iohk/ethereum/jsonrpc/TestSetup.scala diff --git a/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerEthSpec.scala b/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerEthSpec.scala index befd649052..709036a2cc 100644 --- a/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerEthSpec.scala +++ b/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerEthSpec.scala @@ -2,18 +2,12 @@ package io.iohk.ethereum.jsonrpc import java.time.Duration -import akka.actor.ActorSystem -import akka.testkit.TestProbe import akka.util.ByteString -import io.iohk.ethereum.blockchain.sync.EphemBlockchainTestSetup +import io.iohk.ethereum.consensus.Consensus import io.iohk.ethereum.consensus.blocks.PendingBlock -import io.iohk.ethereum.consensus.ethash.blocks.EthashBlockGenerator -import io.iohk.ethereum.consensus.ethash.validators.ValidatorsExecutor import io.iohk.ethereum.consensus.validators.SignedTransactionValidator -import io.iohk.ethereum.consensus.{Consensus, ConsensusConfigs, TestConsensus} -import io.iohk.ethereum.crypto.{ECDSASignature, kec256} -import io.iohk.ethereum.db.storage.AppStateStorage -import io.iohk.ethereum.domain.{Address, Block, BlockBody, BlockHeader, SignedTransaction} +import io.iohk.ethereum.crypto.kec256 +import io.iohk.ethereum.domain._ import io.iohk.ethereum.jsonrpc.DebugService.{ListPeersInfoRequest, ListPeersInfoResponse} import io.iohk.ethereum.jsonrpc.EthService._ import io.iohk.ethereum.jsonrpc.FilterManager.{LogFilterLogs, TxLog} @@ -23,28 +17,24 @@ import io.iohk.ethereum.jsonrpc.NetService.{ListeningResponse, PeerCountResponse import io.iohk.ethereum.jsonrpc.PersonalService._ import io.iohk.ethereum.jsonrpc.server.http.JsonRpcHttpServer import io.iohk.ethereum.jsonrpc.server.ipc.JsonRpcIpcServer -import io.iohk.ethereum.keystore.KeyStore -import io.iohk.ethereum.ledger.{BloomFilter, Ledger, StxLedger} import io.iohk.ethereum.network.EtcPeerManagerActor.PeerInfo import io.iohk.ethereum.network.p2p.messages.CommonMessages.Status import io.iohk.ethereum.network.p2p.messages.Versions import io.iohk.ethereum.ommers.OmmersPool import io.iohk.ethereum.ommers.OmmersPool.Ommers import io.iohk.ethereum.transactions.PendingTransactionsManager -import io.iohk.ethereum.utils._ import io.iohk.ethereum.{Fixtures, LongPatience, Timeouts} import org.bouncycastle.util.encoders.Hex import org.json4s.JsonAST._ import org.json4s.JsonDSL._ import org.json4s.{DefaultFormats, Extraction, Formats} -import org.scalamock.scalatest.MockFactory import org.scalatest.concurrent.{Eventually, ScalaFutures} +import org.scalatest.flatspec.AnyFlatSpec +import org.scalatest.matchers.should.Matchers import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks import scala.concurrent.Future import scala.concurrent.duration._ -import org.scalatest.flatspec.AnyFlatSpec -import org.scalatest.matchers.should.Matchers // scalastyle:off file.size.limit // scalastyle:off magic.number @@ -57,11 +47,17 @@ class JsonRpcControllerEthSpec with LongPatience with Eventually { + private def newJsonRpcRequest(method: String, params: List[JValue]) = + JsonRpcRequest("2.0", method, Some(JArray(params)), Some(JInt(1))) + + private def newJsonRpcRequest(method: String) = + JsonRpcRequest("2.0", method, None, Some(JInt(1))) + implicit val formats: Formats = DefaultFormats.preservingEmptyValues + OptionNoneToJNullSerializer + QuantitiesSerializer + UnformattedDataJsonSerializer "JsonRpcController" should "handle valid sha3 request" in new TestSetup { - val rpcRequest = JsonRpcRequest("2.0", "web3_sha3", Some(JArray(JString("0x1234") :: Nil)), Some(1)) + val rpcRequest = newJsonRpcRequest("web3_sha3", JString("0x1234") :: Nil) val response = jsonRpcController.handleRequest(rpcRequest).futureValue @@ -69,7 +65,7 @@ class JsonRpcControllerEthSpec } it should "fail when invalid request is received" in new TestSetup { - val rpcRequest = JsonRpcRequest("2.0", "web3_sha3", Some(JArray(JString("asdasd") :: Nil)), Some(1)) + val rpcRequest = newJsonRpcRequest("web3_sha3", JString("asdasd") :: Nil) val response = jsonRpcController.handleRequest(rpcRequest).futureValue @@ -77,7 +73,7 @@ class JsonRpcControllerEthSpec } it should "handle clientVersion request" in new TestSetup { - val rpcRequest = JsonRpcRequest("2.0", "web3_clientVersion", None, Some(1)) + val rpcRequest = newJsonRpcRequest("web3_clientVersion") val response = jsonRpcController.handleRequest(rpcRequest).futureValue @@ -87,7 +83,7 @@ class JsonRpcControllerEthSpec it should "Handle net_peerCount request" in new TestSetup { (netService.peerCount _).expects(*).returning(Future.successful(Right(PeerCountResponse(123)))) - val rpcRequest = JsonRpcRequest("2.0", "net_peerCount", None, Some(1)) + val rpcRequest = newJsonRpcRequest("net_peerCount") val response = jsonRpcController.handleRequest(rpcRequest).futureValue @@ -97,7 +93,7 @@ class JsonRpcControllerEthSpec it should "Handle net_listening request" in new TestSetup { (netService.listening _).expects(*).returning(Future.successful(Right(ListeningResponse(false)))) - val rpcRequest = JsonRpcRequest("2.0", "net_listening", None, Some(1)) + val rpcRequest = newJsonRpcRequest("net_listening") val response = jsonRpcController.handleRequest(rpcRequest).futureValue response should haveBooleanResult(false) @@ -108,21 +104,21 @@ class JsonRpcControllerEthSpec (netService.version _).expects(*).returning(Future.successful(Right(VersionResponse(netVersion)))) - val rpcRequest = JsonRpcRequest("2.0", "net_version", None, Some(1)) + val rpcRequest = newJsonRpcRequest("net_version") val response = jsonRpcController.handleRequest(rpcRequest).futureValue response should haveStringResult(netVersion) } it should "eth_protocolVersion" in new TestSetup { - val rpcRequest = JsonRpcRequest("2.0", "eth_protocolVersion", None, Some(1)) + val rpcRequest = newJsonRpcRequest("eth_protocolVersion") val response = jsonRpcController.handleRequest(rpcRequest).futureValue response should haveStringResult("0x3f") } it should "handle eth_chainId" in new TestSetup { - val request = JsonRpcRequest("2.0", "eth_chainId", None, Some(1)) + val request = newJsonRpcRequest("eth_chainId") val response = jsonRpcController.handleRequest(request).futureValue response should haveStringResult("0x3d") @@ -132,7 +128,7 @@ class JsonRpcControllerEthSpec val bestBlockNumber = 10 blockchain.saveBestKnownBlocks(bestBlockNumber) - val rpcRequest = JsonRpcRequest("2.0", "eth_blockNumber", None, Some(1)) + val rpcRequest = newJsonRpcRequest("eth_blockNumber") val response = jsonRpcController.handleRequest(rpcRequest).futureValue response should haveStringResult(s"0xa") @@ -144,7 +140,7 @@ class JsonRpcControllerEthSpec (appStateStorage.getEstimatedHighestBlock _).expects().returning(300) blockchain.saveBestKnownBlocks(200) - val rpcRequest = JsonRpcRequest("2.0", "eth_syncing", None, Some(1)) + val rpcRequest = newJsonRpcRequest("eth_syncing") val response = jsonRpcController.handleRequest(rpcRequest).futureValue @@ -164,12 +160,12 @@ class JsonRpcControllerEthSpec override def ipcServerConfig: JsonRpcIpcServer.JsonRpcIpcServerConfig = ??? } - val ethRpcRequest = JsonRpcRequest("2.0", "eth_protocolVersion", None, Some(1)) + val ethRpcRequest = newJsonRpcRequest("eth_protocolVersion") val ethResponse = jsonRpcController.handleRequest(ethRpcRequest).futureValue ethResponse should haveError(JsonRpcErrors.MethodNotFound) - val web3RpcRequest = JsonRpcRequest("2.0", "web3_clientVersion", None, Some(1)) + val web3RpcRequest = newJsonRpcRequest("web3_clientVersion") val web3Response = jsonRpcController.handleRequest(web3RpcRequest).futureValue web3Response should haveStringResult(version) @@ -180,11 +176,9 @@ class JsonRpcControllerEthSpec blockchain.storeBlock(blockToRequest).commit() - val rpcRequest = JsonRpcRequest( - "2.0", + val rpcRequest = newJsonRpcRequest( "eth_getBlockTransactionCountByHash", - Some(JArray(List(JString(s"0x${blockToRequest.header.hashAsHexString}")))), - Some(JInt(1)) + List(JString(s"0x${blockToRequest.header.hashAsHexString}")), ) val response = jsonRpcController.handleRequest(rpcRequest).futureValue @@ -201,11 +195,9 @@ class JsonRpcControllerEthSpec .and(blockchain.storeTotalDifficulty(blockToRequest.header.hash, blockTd)) .commit() - val request = JsonRpcRequest( - "2.0", + val request = newJsonRpcRequest( "eth_getBlockByHash", - Some(JArray(List(JString(s"0x${blockToRequest.header.hashAsHexString}"), JBool(false)))), - Some(JInt(1)) + List(JString(s"0x${blockToRequest.header.hashAsHexString}"), JBool(false)), ) val response = jsonRpcController.handleRequest(request).futureValue @@ -224,11 +216,9 @@ class JsonRpcControllerEthSpec .and(blockchain.storeTotalDifficulty(blockToRequest.header.hash, blockTd)) .commit() - val request = JsonRpcRequest( - "2.0", + val request = newJsonRpcRequest( "eth_getBlockByNumber", - Some(JArray(List(JString(s"0x${Hex.toHexString(blockToRequest.header.number.toByteArray)}"), JBool(false)))), - Some(JInt(1)) + List(JString(s"0x${Hex.toHexString(blockToRequest.header.number.toByteArray)}"), JBool(false)) ) val response = jsonRpcController.handleRequest(request).futureValue @@ -244,18 +234,12 @@ class JsonRpcControllerEthSpec blockchain.storeBlock(blockToRequest).commit() - val request: JsonRpcRequest = JsonRpcRequest( - "2.0", + val request: JsonRpcRequest = newJsonRpcRequest( "eth_getUncleByBlockHashAndIndex", - Some( - JArray( - List( - JString(s"0x${blockToRequest.header.hashAsHexString}"), - JString(s"0x${Hex.toHexString(BigInt(0).toByteArray)}") - ) - ) - ), - Some(JInt(1)) + List( + JString(s"0x${blockToRequest.header.hashAsHexString}"), + JString(s"0x${Hex.toHexString(BigInt(0).toByteArray)}") + ) ) val response = jsonRpcController.handleRequest(request).futureValue @@ -275,18 +259,12 @@ class JsonRpcControllerEthSpec blockchain.storeBlock(blockToRequest).commit() - val request: JsonRpcRequest = JsonRpcRequest( - "2.0", + val request: JsonRpcRequest = newJsonRpcRequest( "eth_getUncleByBlockNumberAndIndex", - Some( - JArray( - List( - JString(s"0x${Hex.toHexString(blockToRequest.header.number.toByteArray)}"), - JString(s"0x${Hex.toHexString(BigInt(0).toByteArray)}") - ) - ) - ), - Some(JInt(1)) + List( + JString(s"0x${Hex.toHexString(blockToRequest.header.number.toByteArray)}"), + JString(s"0x${Hex.toHexString(BigInt(0).toByteArray)}") + ) ) val response = jsonRpcController.handleRequest(request).futureValue @@ -307,18 +285,12 @@ class JsonRpcControllerEthSpec blockchain.storeBlock(blockToRequest).commit() blockchain.saveBestKnownBlocks(blockToRequest.header.number) - val request: JsonRpcRequest = JsonRpcRequest( - "2.0", + val request: JsonRpcRequest = newJsonRpcRequest( "eth_getTransactionByBlockHashAndIndex", - Some( - JArray( - List( - JString(s"0x${blockToRequest.header.hashAsHexString}"), - JString(s"0x${Hex.toHexString(BigInt(txIndexToRequest).toByteArray)}") - ) - ) - ), - Some(JInt(1)) + List( + JString(s"0x${blockToRequest.header.hashAsHexString}"), + JString(s"0x${Hex.toHexString(BigInt(txIndexToRequest).toByteArray)}") + ) ) val response = jsonRpcController.handleRequest(request).futureValue val expectedStx = blockToRequest.body.transactionList.apply(txIndexToRequest) @@ -336,18 +308,12 @@ class JsonRpcControllerEthSpec blockchain.storeBlock(blockToRequest).commit() blockchain.saveBestKnownBlocks(blockToRequest.header.number) - val request: JsonRpcRequest = JsonRpcRequest( - "2.0", + val request: JsonRpcRequest = newJsonRpcRequest( "eth_getRawTransactionByBlockHashAndIndex", - Some( - JArray( - List( - JString(s"0x${blockToRequest.header.hashAsHexString}"), - JString(s"0x${Hex.toHexString(BigInt(txIndexToRequest).toByteArray)}") - ) - ) - ), - Some(JInt(1)) + List( + JString(s"0x${blockToRequest.header.hashAsHexString}"), + JString(s"0x${Hex.toHexString(BigInt(txIndexToRequest).toByteArray)}") + ) ) val response = jsonRpcController.handleRequest(request).futureValue val expectedTxResponse = rawTrnHex(blockToRequest.body.transactionList, txIndexToRequest) @@ -357,34 +323,18 @@ class JsonRpcControllerEthSpec it should "handle eth_getRawTransactionByHash request" in new TestSetup { val mockEthService = mock[EthService] - override val jsonRpcController = - new JsonRpcController( - web3Service, - netService, - mockEthService, - personalService, - None, - debugService, - qaService, - config - ) + override val jsonRpcController = newJsonRpcController(mockEthService) val txResponse: SignedTransaction = Fixtures.Blocks.Block3125369.body.transactionList.head (mockEthService.getRawTransactionByHash _) .expects(*) .returning(Future.successful(Right(RawTransactionResponse(Some(txResponse))))) - val request: JsonRpcRequest = JsonRpcRequest( - "2.0", + val request: JsonRpcRequest = newJsonRpcRequest( "eth_getRawTransactionByHash", - Some( - JArray( - List( - JString("0xe9b2d3e8a2bc996a1c7742de825fdae2466ae783ce53484304efffe304ff232d") - ) - ) - ), - Some(JInt(1)) + List( + JString("0xe9b2d3e8a2bc996a1c7742de825fdae2466ae783ce53484304efffe304ff232d") + ) ) val response = jsonRpcController.handleRequest(request).futureValue @@ -401,8 +351,8 @@ class JsonRpcControllerEthSpec .expects(ImportRawKeyRequest(keyBytes, pass)) .returning(Future.successful(Right(ImportRawKeyResponse(addr)))) - val params = JArray(JString(key) :: JString(pass) :: Nil) - val rpcRequest = JsonRpcRequest("2.0", "personal_importRawKey", Some(params), Some(1)) + val params = JString(key) :: JString(pass) :: Nil + val rpcRequest = newJsonRpcRequest("personal_importRawKey", params) val response = jsonRpcController.handleRequest(rpcRequest).futureValue response should haveStringResult(addr.toString) @@ -416,8 +366,8 @@ class JsonRpcControllerEthSpec .expects(NewAccountRequest(pass)) .returning(Future.successful(Right(NewAccountResponse(addr)))) - val params = JArray(JString(pass) :: Nil) - val rpcRequest = JsonRpcRequest("2.0", "personal_newAccount", Some(params), Some(1)) + val params = JString(pass) :: Nil + val rpcRequest = newJsonRpcRequest("personal_newAccount", params) val response = jsonRpcController.handleRequest(rpcRequest).futureValue response should haveStringResult(addr.toString) @@ -431,7 +381,7 @@ class JsonRpcControllerEthSpec .expects(ListAccountsRequest()) .returning(Future.successful(Right(ListAccountsResponse(addresses)))) - val rpcRequest = JsonRpcRequest("2.0", "personal_listAccounts", None, Some(1)) + val rpcRequest = newJsonRpcRequest("personal_listAccounts") val response = jsonRpcController.handleRequest(rpcRequest).futureValue response should haveResult(JArray(addresses.map(a => JString(a.toString)))) @@ -440,13 +390,13 @@ class JsonRpcControllerEthSpec it should "personal_unlockAccount" in new TestSetup { val address = Address(42) val pass = "aaa" - val params = JArray(JString(address.toString) :: JString(pass) :: Nil) + val params = JString(address.toString) :: JString(pass) :: Nil (personalService.unlockAccount _) .expects(UnlockAccountRequest(address, pass, None)) .returning(Future.successful(Right(UnlockAccountResponse(true)))) - val rpcRequest = JsonRpcRequest("2.0", "personal_unlockAccount", Some(params), Some(1)) + val rpcRequest = newJsonRpcRequest("personal_unlockAccount", params) val response = jsonRpcController.handleRequest(rpcRequest).futureValue response should haveBooleanResult(true) @@ -456,13 +406,13 @@ class JsonRpcControllerEthSpec val address = Address(42) val pass = "aaa" val dur = "1" - val params = JArray(JString(address.toString) :: JString(pass) :: JString(dur) :: Nil) + val params = JString(address.toString) :: JString(pass) :: JString(dur) :: Nil (personalService.unlockAccount _) .expects(UnlockAccountRequest(address, pass, Some(Duration.ofSeconds(1)))) .returning(Future.successful(Right(UnlockAccountResponse(true)))) - val rpcRequest = JsonRpcRequest("2.0", "personal_unlockAccount", Some(params), Some(1)) + val rpcRequest = newJsonRpcRequest("personal_unlockAccount", params) val response = jsonRpcController.handleRequest(rpcRequest).futureValue response should haveBooleanResult(true) @@ -473,15 +423,15 @@ class JsonRpcControllerEthSpec val pass = "aaa" val dur = "alksjdfh" - val params = JArray(JString(address.toString) :: JString(pass) :: JString(dur) :: Nil) - val rpcRequest = JsonRpcRequest("2.0", "personal_unlockAccount", Some(params), Some(1)) + val params = JString(address.toString) :: JString(pass) :: JString(dur) :: Nil + val rpcRequest = newJsonRpcRequest("personal_unlockAccount", params) val response = jsonRpcController.handleRequest(rpcRequest).futureValue response should haveError(JsonRpcError(-32602, "Invalid method parameters", None)) val dur2 = Long.MaxValue - val params2 = JArray(JString(address.toString) :: JString(pass) :: JInt(dur2) :: Nil) - val rpcRequest2 = JsonRpcRequest("2.0", "personal_unlockAccount", Some(params2), Some(1)) + val params2 = JString(address.toString) :: JString(pass) :: JInt(dur2) :: Nil + val rpcRequest2 = newJsonRpcRequest("personal_unlockAccount", params2) val response2 = jsonRpcController.handleRequest(rpcRequest2).futureValue response2 should haveError( JsonRpcError(-32602, "Duration should be an number of seconds, less than 2^31 - 1", None) @@ -491,13 +441,13 @@ class JsonRpcControllerEthSpec it should "personal_unlockAccount should handle null passed as a duration for compatibility with Parity and web3j" in new TestSetup { val address = Address(42) val pass = "aaa" - val params = JArray(JString(address.toString) :: JString(pass) :: JNull :: Nil) + val params = JString(address.toString) :: JString(pass) :: JNull :: Nil (personalService.unlockAccount _) .expects(UnlockAccountRequest(address, pass, None)) .returning(Future.successful(Right(UnlockAccountResponse(true)))) - val rpcRequest = JsonRpcRequest("2.0", "personal_unlockAccount", Some(params), Some(1)) + val rpcRequest = newJsonRpcRequest("personal_unlockAccount", params) val response = jsonRpcController.handleRequest(rpcRequest).futureValue response should haveBooleanResult(true) @@ -505,26 +455,24 @@ class JsonRpcControllerEthSpec it should "personal_lockAccount" in new TestSetup { val address = Address(42) - val params = JArray(JString(address.toString) :: Nil) + val params = JString(address.toString) :: Nil (personalService.lockAccount _) .expects(LockAccountRequest(address)) .returning(Future.successful(Right(LockAccountResponse(true)))) - val rpcRequest = JsonRpcRequest("2.0", "personal_lockAccount", Some(params), Some(1)) + val rpcRequest = newJsonRpcRequest("personal_lockAccount", params) val response = jsonRpcController.handleRequest(rpcRequest).futureValue response should haveBooleanResult(true) } it should "personal_sendTransaction" in new TestSetup { - val params = JArray( - JObject( - "from" -> Address(42).toString, - "to" -> Address(123).toString, - "value" -> 1000 - ) :: JString("passphrase") :: Nil - ) + val params = JObject( + "from" -> Address(42).toString, + "to" -> Address(123).toString, + "value" -> 1000 + ) :: JString("passphrase") :: Nil val txHash = ByteString(1, 2, 3, 4) @@ -533,7 +481,7 @@ class JsonRpcControllerEthSpec .expects(*) .returning(Future.successful(Right(SendTransactionWithPassphraseResponse(txHash)))) - val rpcRequest = JsonRpcRequest("2.0", "personal_sendTransaction", Some(params), Some(1)) + val rpcRequest = newJsonRpcRequest("personal_sendTransaction", params) val response = jsonRpcController.handleRequest(rpcRequest).futureValue response should haveResult(JString(s"0x${Hex.toHexString(txHash.toArray)}")) @@ -560,20 +508,18 @@ class JsonRpcControllerEthSpec .expects(ListPeersInfoRequest()) .returning(Future.successful(Right(ListPeersInfoResponse(peers)))) - val rpcRequest = JsonRpcRequest("2.0", "debug_listPeersInfo", None, Some(1)) + val rpcRequest = newJsonRpcRequest("debug_listPeersInfo") val response: JsonRpcResponse = jsonRpcController.handleRequest(rpcRequest).futureValue response should haveResult(JArray(peers.map(info => JString(info.toString)))) } it should "eth_sendTransaction" in new TestSetup { - val params = JArray( - JObject( - "from" -> Address(42).toString, - "to" -> Address(123).toString, - "value" -> 1000 - ) :: Nil - ) + val params = JObject( + "from" -> Address(42).toString, + "to" -> Address(123).toString, + "value" -> 1000 + ) :: Nil val txHash = ByteString(1, 2, 3, 4) @@ -582,7 +528,7 @@ class JsonRpcControllerEthSpec .expects(*) .returning(Future.successful(Right(SendTransactionResponse(txHash)))) - val rpcRequest = JsonRpcRequest("2.0", "eth_sendTransaction", Some(params), Some(1)) + val rpcRequest = newJsonRpcRequest("eth_sendTransaction", params) val response = jsonRpcController.handleRequest(rpcRequest).futureValue response should haveResult(JString(s"0x${Hex.toHexString(txHash.toArray)}")) @@ -606,12 +552,7 @@ class JsonRpcControllerEthSpec .expects(parentBlock, *, *, *) .returns(Right(PendingBlock(Block(blockHeader, BlockBody(Nil, Nil)), Nil))) - val request: JsonRpcRequest = JsonRpcRequest( - "2.0", - "eth_getWork", - None, - Some(JInt(1)) - ) + val request: JsonRpcRequest = newJsonRpcRequest("eth_getWork") val result: Future[JsonRpcResponse] = jsonRpcController.handleRequest(request) @@ -651,12 +592,7 @@ class JsonRpcControllerEthSpec .expects(parentBlock, *, *, *) .returns(Right(PendingBlock(Block(blockHeader, BlockBody(Nil, Nil)), Nil))) - val request: JsonRpcRequest = JsonRpcRequest( - "2.0", - "eth_getWork", - None, - Some(JInt(1)) - ) + val request: JsonRpcRequest = newJsonRpcRequest("eth_getWork") val result: Future[JsonRpcResponse] = jsonRpcController.handleRequest(request) @@ -694,19 +630,13 @@ class JsonRpcControllerEthSpec .returns(Some(PendingBlock(Block(blockHeader, BlockBody(Nil, Nil)), Nil))) (appStateStorage.getBestBlockNumber _).expects().returns(1) - val request: JsonRpcRequest = JsonRpcRequest( - "2.0", + val request: JsonRpcRequest = newJsonRpcRequest( "eth_submitWork", - Some( - JArray( - List( - JString(nonce), - JString(s"0x$headerPowHash"), - JString(mixHash) - ) - ) - ), - Some(JInt(1)) + List( + JString(nonce), + JString(s"0x$headerPowHash"), + JString(mixHash) + ) ) val response = jsonRpcController.handleRequest(request).futureValue @@ -722,18 +652,12 @@ class JsonRpcControllerEthSpec (ledger.consensus _: (() => Consensus)).expects().returns(consensus).anyNumberOfTimes() - val request: JsonRpcRequest = JsonRpcRequest( - "2.0", + val request: JsonRpcRequest = newJsonRpcRequest( "eth_submitHashrate", - Some( - JArray( - List( - JString(s"0x${"0" * 61}500"), - JString(s"0x59daa26581d0acd1fce254fb7e85952f4c09d0915afd33d3886cd914bc7d283c") - ) - ) - ), - Some(JInt(1)) + List( + JString(s"0x${"0" * 61}500"), + JString(s"0x59daa26581d0acd1fce254fb7e85952f4c09d0915afd33d3886cd914bc7d283c") + ) ) val response = jsonRpcController.handleRequest(request).futureValue @@ -749,12 +673,7 @@ class JsonRpcControllerEthSpec (ledger.consensus _: (() => Consensus)).expects().returns(consensus).anyNumberOfTimes() - val request: JsonRpcRequest = JsonRpcRequest( - "2.0", - "eth_hashrate", - None, - Some(JInt(1)) - ) + val request: JsonRpcRequest = newJsonRpcRequest("eth_hashrate") val response = jsonRpcController.handleRequest(request).futureValue response should haveStringResult("0x0") @@ -766,12 +685,7 @@ class JsonRpcControllerEthSpec .commit() blockchain.saveBestKnownBlocks(42) - val request: JsonRpcRequest = JsonRpcRequest( - "2.0", - "eth_gasPrice", - None, - Some(JInt(1)) - ) + val request: JsonRpcRequest = newJsonRpcRequest("eth_gasPrice") val response = jsonRpcController.handleRequest(request).futureValue response should haveStringResult("0x4a817c800") @@ -779,34 +693,22 @@ class JsonRpcControllerEthSpec it should "eth_call" in new TestSetup { val mockEthService = mock[EthService] - override val jsonRpcController = - new JsonRpcController( - web3Service, - netService, - mockEthService, - personalService, - None, - debugService, - qaService, - config - ) + override val jsonRpcController = newJsonRpcController(mockEthService) (mockEthService.call _).expects(*).returning(Future.successful(Right(CallResponse(ByteString("asd"))))) - val json = JArray( - List( - JObject( - "from" -> "0xabbb6bebfa05aa13e908eaa492bd7a8343760477", - "to" -> "0xda714fe079751fa7a1ad80b76571ea6ec52a446c", - "gas" -> "0x12", - "gasPrice" -> "0x123", - "value" -> "0x99", - "data" -> "0xFF44" - ), - JString("latest") - ) + val json = List( + JObject( + "from" -> "0xabbb6bebfa05aa13e908eaa492bd7a8343760477", + "to" -> "0xda714fe079751fa7a1ad80b76571ea6ec52a446c", + "gas" -> "0x12", + "gasPrice" -> "0x123", + "value" -> "0x99", + "data" -> "0xFF44" + ), + JString("latest") ) - val rpcRequest = JsonRpcRequest("2.0", "eth_call", Some(json), Some(1)) + val rpcRequest = newJsonRpcRequest("eth_call", json) val response = jsonRpcController.handleRequest(rpcRequest).futureValue response should haveStringResult("0x617364") @@ -814,17 +716,7 @@ class JsonRpcControllerEthSpec it should "eth_estimateGas" in new TestSetup { val mockEthService = mock[EthService] - override val jsonRpcController = - new JsonRpcController( - web3Service, - netService, - mockEthService, - personalService, - None, - debugService, - qaService, - config - ) + override val jsonRpcController = newJsonRpcController(mockEthService) (mockEthService.estimateGas _) .expects(*) @@ -843,13 +735,13 @@ class JsonRpcControllerEthSpec val table = Table( "Requests", - JArray(List(callObj, JString("latest"))), - JArray(List(callObj)), - JArray(List(callObjWithoutData)) + List(callObj, JString("latest")), + List(callObj), + List(callObjWithoutData) ) forAll(table) { json => - val rpcRequest = JsonRpcRequest("2.0", "eth_estimateGas", Some(json), Some(1)) + val rpcRequest = newJsonRpcRequest("eth_estimateGas", json) val response = jsonRpcController.handleRequest(rpcRequest).futureValue response should haveStringResult("0x906") @@ -859,34 +751,18 @@ class JsonRpcControllerEthSpec it should "eth_getCode" in new TestSetup { val mockEthService = mock[EthService] - override val jsonRpcController = - new JsonRpcController( - web3Service, - netService, - mockEthService, - personalService, - None, - debugService, - qaService, - config - ) + override val jsonRpcController = newJsonRpcController(mockEthService) (mockEthService.getCode _) .expects(*) .returning(Future.successful(Right(GetCodeResponse(ByteString(Hex.decode("FFAA22")))))) - val request: JsonRpcRequest = JsonRpcRequest( - "2.0", + val request: JsonRpcRequest = newJsonRpcRequest( "eth_getCode", - Some( - JArray( - List( - JString(s"0x7B9Bc474667Db2fFE5b08d000F1Acc285B2Ae47D"), - JString(s"latest") - ) - ) - ), - Some(JInt(1)) + List( + JString(s"0x7B9Bc474667Db2fFE5b08d000F1Acc285B2Ae47D"), + JString(s"latest") + ) ) val response = jsonRpcController.handleRequest(request).futureValue @@ -895,33 +771,17 @@ class JsonRpcControllerEthSpec it should "eth_getUncleCountByBlockNumber" in new TestSetup { val mockEthService = mock[EthService] - override val jsonRpcController = - new JsonRpcController( - web3Service, - netService, - mockEthService, - personalService, - None, - debugService, - qaService, - config - ) + override val jsonRpcController = newJsonRpcController(mockEthService) (mockEthService.getUncleCountByBlockNumber _) .expects(*) .returning(Future.successful(Right(GetUncleCountByBlockNumberResponse(2)))) - val request: JsonRpcRequest = JsonRpcRequest( - "2.0", + val request: JsonRpcRequest = newJsonRpcRequest( "eth_getUncleCountByBlockNumber", - Some( - JArray( - List( - JString(s"0x12") - ) - ) - ), - Some(JInt(1)) + List( + JString(s"0x12") + ) ) val response = jsonRpcController.handleRequest(request).futureValue @@ -930,33 +790,17 @@ class JsonRpcControllerEthSpec it should "eth_getUncleCountByBlockHash " in new TestSetup { val mockEthService = mock[EthService] - override val jsonRpcController = - new JsonRpcController( - web3Service, - netService, - mockEthService, - personalService, - None, - debugService, - qaService, - config - ) + override val jsonRpcController = newJsonRpcController(mockEthService) (mockEthService.getUncleCountByBlockHash _) .expects(*) .returning(Future.successful(Right(GetUncleCountByBlockHashResponse(3)))) - val request: JsonRpcRequest = JsonRpcRequest( - "2.0", + val request: JsonRpcRequest = newJsonRpcRequest( "eth_getUncleCountByBlockHash", - Some( - JArray( - List( - JString(s"0x7dc64cb9d8a95763e288d71088fe3116e10dbff317c09f7a9bd5dd6974d27d20") - ) - ) - ), - Some(JInt(1)) + List( + JString(s"0x7dc64cb9d8a95763e288d71088fe3116e10dbff317c09f7a9bd5dd6974d27d20") + ) ) val response = jsonRpcController.handleRequest(request).futureValue @@ -965,33 +809,17 @@ class JsonRpcControllerEthSpec it should "eth_getBlockTransactionCountByNumber " in new TestSetup { val mockEthService = mock[EthService] - override val jsonRpcController = - new JsonRpcController( - web3Service, - netService, - mockEthService, - personalService, - None, - debugService, - qaService, - config - ) + override val jsonRpcController = newJsonRpcController(mockEthService) (mockEthService.getBlockTransactionCountByNumber _) .expects(*) .returning(Future.successful(Right(GetBlockTransactionCountByNumberResponse(17)))) - val request: JsonRpcRequest = JsonRpcRequest( - "2.0", + val request: JsonRpcRequest = newJsonRpcRequest( "eth_getBlockTransactionCountByNumber", - Some( - JArray( - List( - JString(s"0x123") - ) - ) - ), - Some(JInt(1)) + List( + JString(s"0x123") + ) ) val response = jsonRpcController.handleRequest(request).futureValue @@ -1007,12 +835,7 @@ class JsonRpcControllerEthSpec (ledger.consensus _: (() => Consensus)).expects().returns(consensus).anyNumberOfTimes() - val request: JsonRpcRequest = JsonRpcRequest( - "2.0", - "eth_coinbase", - None, - Some(JInt(1)) - ) + val request: JsonRpcRequest = newJsonRpcRequest("eth_coinbase") val response = jsonRpcController.handleRequest(request).futureValue response should haveStringResult("0x000000000000000000000000000000000000002a") @@ -1025,18 +848,12 @@ class JsonRpcControllerEthSpec blockchain.storeBlock(blockToRequest).commit() blockchain.saveBestKnownBlocks(blockToRequest.header.number) - val request: JsonRpcRequest = JsonRpcRequest( - "2.0", + val request: JsonRpcRequest = newJsonRpcRequest( "eth_getTransactionByBlockNumberAndIndex", - Some( - JArray( - List( - JString(s"latest"), - JString(s"0x${Hex.toHexString(BigInt(txIndex).toByteArray)}") - ) - ) - ), - Some(JInt(1)) + List( + JString(s"latest"), + JString(s"0x${Hex.toHexString(BigInt(txIndex).toByteArray)}") + ) ) val response = jsonRpcController.handleRequest(request).futureValue val expectedStx = blockToRequest.body.transactionList(txIndex) @@ -1054,18 +871,12 @@ class JsonRpcControllerEthSpec blockchain.storeBlock(blockToRequest).commit() - val request: JsonRpcRequest = JsonRpcRequest( - "2.0", + val request: JsonRpcRequest = newJsonRpcRequest( "eth_getTransactionByBlockNumberAndIndex", - Some( - JArray( - List( - JString(s"0xC005"), - JString(s"0x${Hex.toHexString(BigInt(txIndex).toByteArray)}") - ) - ) - ), - Some(JInt(1)) + List( + JString(s"0xC005"), + JString(s"0x${Hex.toHexString(BigInt(txIndex).toByteArray)}") + ) ) val response = jsonRpcController.handleRequest(request).futureValue val expectedStx = blockToRequest.body.transactionList(txIndex) @@ -1082,18 +893,12 @@ class JsonRpcControllerEthSpec blockchain.storeBlock(blockToRequest).commit() - val request: JsonRpcRequest = JsonRpcRequest( - "2.0", + val request: JsonRpcRequest = newJsonRpcRequest( "eth_getTransactionByBlockNumberAndIndex", - Some( - JArray( - List( - JInt(Fixtures.Blocks.Block3125369.header.number), - JString(s"0x${Hex.toHexString(BigInt(txIndex).toByteArray)}") - ) - ) - ), - Some(JInt(1)) + List( + JInt(Fixtures.Blocks.Block3125369.header.number), + JString(s"0x${Hex.toHexString(BigInt(txIndex).toByteArray)}") + ) ) val response = jsonRpcController.handleRequest(request).futureValue val expectedStx = blockToRequest.body.transactionList(txIndex) @@ -1112,18 +917,12 @@ class JsonRpcControllerEthSpec blockchain.storeBlock(blockToRequest).commit() blockchain.saveBestKnownBlocks(blockToRequest.header.number) - val request: JsonRpcRequest = JsonRpcRequest( - "2.0", + val request: JsonRpcRequest = newJsonRpcRequest( "eth_getRawTransactionByBlockNumberAndIndex", - Some( - JArray( - List( - JString(s"latest"), - JString(s"0x${Hex.toHexString(BigInt(txIndex).toByteArray)}") - ) - ) - ), - Some(JInt(1)) + List( + JString(s"latest"), + JString(s"0x${Hex.toHexString(BigInt(txIndex).toByteArray)}") + ) ) // when @@ -1143,18 +942,12 @@ class JsonRpcControllerEthSpec blockchain.storeBlock(blockToRequest).commit() - val request: JsonRpcRequest = JsonRpcRequest( - "2.0", + val request: JsonRpcRequest = newJsonRpcRequest( "eth_getRawTransactionByBlockNumberAndIndex", - Some( - JArray( - List( - JString(s"0xC005"), - JString(s"0x${Hex.toHexString(BigInt(txIndex).toByteArray)}") - ) - ) - ), - Some(JInt(1)) + List( + JString(s"0xC005"), + JString(s"0x${Hex.toHexString(BigInt(txIndex).toByteArray)}") + ) ) // when @@ -1172,18 +965,12 @@ class JsonRpcControllerEthSpec blockchain.storeBlock(blockToRequest).commit() - val request: JsonRpcRequest = JsonRpcRequest( - "2.0", + val request: JsonRpcRequest = newJsonRpcRequest( "eth_getRawTransactionByBlockNumberAndIndex", - Some( - JArray( - List( - JInt(Fixtures.Blocks.Block3125369.header.number), - JString(s"0x${Hex.toHexString(BigInt(txIndex).toByteArray)}") - ) - ) - ), - Some(JInt(1)) + List( + JInt(Fixtures.Blocks.Block3125369.header.number), + JString(s"0x${Hex.toHexString(BigInt(txIndex).toByteArray)}") + ) ) val response = jsonRpcController.handleRequest(request).futureValue val expectedTxResponse = rawTrnHex(blockToRequest.body.transactionList, txIndex) @@ -1193,34 +980,18 @@ class JsonRpcControllerEthSpec it should "eth_getBalance" in new TestSetup { val mockEthService = mock[EthService] - override val jsonRpcController = - new JsonRpcController( - web3Service, - netService, - mockEthService, - personalService, - None, - debugService, - qaService, - config - ) + override val jsonRpcController = newJsonRpcController(mockEthService) (mockEthService.getBalance _) .expects(*) .returning(Future.successful(Right(GetBalanceResponse(17)))) - val request: JsonRpcRequest = JsonRpcRequest( - "2.0", + val request: JsonRpcRequest = newJsonRpcRequest( "eth_getBalance", - Some( - JArray( - List( - JString(s"0x7B9Bc474667Db2fFE5b08d000F1Acc285B2Ae47D"), - JString(s"latest") - ) - ) - ), - Some(JInt(1)) + List( + JString(s"0x7B9Bc474667Db2fFE5b08d000F1Acc285B2Ae47D"), + JString(s"latest") + ) ) val response = jsonRpcController.handleRequest(request).futureValue @@ -1229,35 +1000,19 @@ class JsonRpcControllerEthSpec it should "eth_getStorageAt" in new TestSetup { val mockEthService = mock[EthService] - override val jsonRpcController = - new JsonRpcController( - web3Service, - netService, - mockEthService, - personalService, - None, - debugService, - qaService, - config - ) + override val jsonRpcController = newJsonRpcController(mockEthService) (mockEthService.getStorageAt _) .expects(*) .returning(Future.successful(Right(GetStorageAtResponse(ByteString("response"))))) - val request: JsonRpcRequest = JsonRpcRequest( - "2.0", + val request: JsonRpcRequest = newJsonRpcRequest( "eth_getStorageAt", - Some( - JArray( - List( - JString(s"0x7B9Bc474667Db2fFE5b08d000F1Acc285B2Ae47D"), - JString(s"0x01"), - JString(s"latest") - ) - ) - ), - Some(JInt(1)) + List( + JString(s"0x7B9Bc474667Db2fFE5b08d000F1Acc285B2Ae47D"), + JString(s"0x01"), + JString(s"latest") + ) ) val response = jsonRpcController.handleRequest(request).futureValue @@ -1266,34 +1021,18 @@ class JsonRpcControllerEthSpec it should "eth_getTransactionCount" in new TestSetup { val mockEthService = mock[EthService] - override val jsonRpcController = - new JsonRpcController( - web3Service, - netService, - mockEthService, - personalService, - None, - debugService, - qaService, - config - ) + override val jsonRpcController = newJsonRpcController(mockEthService) (mockEthService.getTransactionCount _) .expects(*) .returning(Future.successful(Right(GetTransactionCountResponse(123)))) - val request: JsonRpcRequest = JsonRpcRequest( - "2.0", + val request: JsonRpcRequest = newJsonRpcRequest( "eth_getTransactionCount", - Some( - JArray( - List( - JString(s"0x7B9Bc474667Db2fFE5b08d000F1Acc285B2Ae47D"), - JString(s"latest") - ) - ) - ), - Some(JInt(1)) + List( + JString(s"0x7B9Bc474667Db2fFE5b08d000F1Acc285B2Ae47D"), + JString(s"latest") + ) ) val response = jsonRpcController.handleRequest(request).futureValue @@ -1302,34 +1041,18 @@ class JsonRpcControllerEthSpec it should "eth_getTransactionByHash" in new TestSetup { val mockEthService = mock[EthService] - override val jsonRpcController = - new JsonRpcController( - web3Service, - netService, - mockEthService, - personalService, - None, - debugService, - qaService, - config - ) + override val jsonRpcController = newJsonRpcController(mockEthService) val txResponse = TransactionResponse(Fixtures.Blocks.Block3125369.body.transactionList.head) (mockEthService.getTransactionByHash _) .expects(*) .returning(Future.successful(Right(GetTransactionByHashResponse(Some(txResponse))))) - val request: JsonRpcRequest = JsonRpcRequest( - "2.0", + val request: JsonRpcRequest = newJsonRpcRequest( "eth_getTransactionByHash", - Some( - JArray( - List( - JString("0xe9b2d3e8a2bc996a1c7742de825fdae2466ae783ce53484304efffe304ff232d") - ) - ) - ), - Some(JInt(1)) + List( + JString("0xe9b2d3e8a2bc996a1c7742de825fdae2466ae783ce53484304efffe304ff232d") + ) ) val response = jsonRpcController.handleRequest(request).futureValue @@ -1348,18 +1071,12 @@ class JsonRpcControllerEthSpec ) .returns(Future.successful(Right(SignResponse(sig)))) - val request: JsonRpcRequest = JsonRpcRequest( - "2.0", + val request: JsonRpcRequest = newJsonRpcRequest( "eth_sign", - Some( - JArray( - List( - JString(s"0x9b2055d370f73ec7d8a03e965129118dc8f5bf83"), - JString(s"0xdeadbeaf") - ) - ) - ), - Some(JInt(1)) + List( + JString(s"0x9b2055d370f73ec7d8a03e965129118dc8f5bf83"), + JString(s"0xdeadbeaf") + ) ) val response = jsonRpcController.handleRequest(request).futureValue @@ -1380,19 +1097,13 @@ class JsonRpcControllerEthSpec ) .returns(Future.successful(Right(SignResponse(sig)))) - val request: JsonRpcRequest = JsonRpcRequest( - "2.0", + val request: JsonRpcRequest = newJsonRpcRequest( "personal_sign", - Some( - JArray( - List( - JString(s"0xdeadbeaf"), - JString(s"0x9b2055d370f73ec7d8a03e965129118dc8f5bf83"), - JString("thePassphrase") - ) - ) - ), - Some(JInt(1)) + List( + JString(s"0xdeadbeaf"), + JString(s"0x9b2055d370f73ec7d8a03e965129118dc8f5bf83"), + JString("thePassphrase") + ) ) val response = jsonRpcController.handleRequest(request).futureValue @@ -1411,20 +1122,14 @@ class JsonRpcControllerEthSpec ) ) - val request: JsonRpcRequest = JsonRpcRequest( - "2.0", + val request: JsonRpcRequest = newJsonRpcRequest( "personal_ecRecover", - Some( - JArray( - List( - JString(s"0xdeadbeaf"), - JString( - s"0xa3f20717a250c2b0b729b7e5becbff67fdaef7e0699da4de7ca5895b02a170a12d887fd3b17bfdce3481f10bea41f45ba9f709d39ce8325427b57afcfc994cee1b" - ) - ) + List( + JString(s"0xdeadbeaf"), + JString( + s"0xa3f20717a250c2b0b729b7e5becbff67fdaef7e0699da4de7ca5895b02a170a12d887fd3b17bfdce3481f10bea41f45ba9f709d39ce8325427b57afcfc994cee1b" ) - ), - Some(JInt(1)) + ) ) val response = jsonRpcController.handleRequest(request).futureValue @@ -1433,38 +1138,22 @@ class JsonRpcControllerEthSpec it should "eth_newFilter" in new TestSetup { val mockEthService = mock[EthService] - override val jsonRpcController = - new JsonRpcController( - web3Service, - netService, - mockEthService, - personalService, - None, - debugService, - qaService, - config - ) + override val jsonRpcController = newJsonRpcController(mockEthService) (mockEthService.newFilter _) .expects(*) .returning(Future.successful(Right(NewFilterResponse(123)))) - val request: JsonRpcRequest = JsonRpcRequest( - "2.0", + val request: JsonRpcRequest = newJsonRpcRequest( "eth_newFilter", - Some( - JArray( - List( - JObject( - "fromBlock" -> "0x0", - "toBlock" -> "latest", - "address" -> "0x2B5A350698C91E684EB08c10F7e462f761C0e681", - "topics" -> JArray(List(JNull, "0x00000000000000000000000000000000000000000000000000000000000001c8")) - ) - ) + List( + JObject( + "fromBlock" -> "0x0", + "toBlock" -> "latest", + "address" -> "0x2B5A350698C91E684EB08c10F7e462f761C0e681", + "topics" -> JArray(List(JNull, "0x00000000000000000000000000000000000000000000000000000000000001c8")) ) - ), - Some(JInt(1)) + ) ) val response = jsonRpcController.handleRequest(request).futureValue @@ -1473,17 +1162,7 @@ class JsonRpcControllerEthSpec it should "eth_newBlockFilter" in new TestSetup { val mockEthService = mock[EthService] - override val jsonRpcController = - new JsonRpcController( - web3Service, - netService, - mockEthService, - personalService, - None, - debugService, - qaService, - config - ) + override val jsonRpcController = newJsonRpcController(mockEthService) (mockEthService.newBlockFilter _) .expects(*) @@ -1502,27 +1181,15 @@ class JsonRpcControllerEthSpec it should "eth_newPendingTransactionFilter" in new TestSetup { val mockEthService = mock[EthService] - override val jsonRpcController = - new JsonRpcController( - web3Service, - netService, - mockEthService, - personalService, - None, - debugService, - qaService, - config - ) + override val jsonRpcController = newJsonRpcController(mockEthService) (mockEthService.newPendingTransactionFilter _) .expects(*) .returning(Future.successful(Right(NewFilterResponse(2)))) - val request: JsonRpcRequest = JsonRpcRequest( - "2.0", + val request: JsonRpcRequest = newJsonRpcRequest( "eth_newPendingTransactionFilter", - Some(JArray(List())), - Some(JInt(1)) + Nil ) val response = jsonRpcController.handleRequest(request).futureValue @@ -1531,27 +1198,15 @@ class JsonRpcControllerEthSpec it should "eth_uninstallFilter" in new TestSetup { val mockEthService = mock[EthService] - override val jsonRpcController = - new JsonRpcController( - web3Service, - netService, - mockEthService, - personalService, - None, - debugService, - qaService, - config - ) + override val jsonRpcController = newJsonRpcController(mockEthService) (mockEthService.uninstallFilter _) .expects(*) .returning(Future.successful(Right(UninstallFilterResponse(true)))) - val request: JsonRpcRequest = JsonRpcRequest( - "2.0", + val request: JsonRpcRequest = newJsonRpcRequest( "eth_uninstallFilter", - Some(JArray(List(JString("0x1")))), - Some(JInt(1)) + List(JString("0x1")) ) val response = jsonRpcController.handleRequest(request).futureValue @@ -1560,17 +1215,7 @@ class JsonRpcControllerEthSpec it should "eth_getFilterChanges" in new TestSetup { val mockEthService = mock[EthService] - override val jsonRpcController = - new JsonRpcController( - web3Service, - netService, - mockEthService, - personalService, - None, - debugService, - qaService, - config - ) + override val jsonRpcController = newJsonRpcController(mockEthService) (mockEthService.getFilterChanges _) .expects(*) @@ -1598,7 +1243,7 @@ class JsonRpcControllerEthSpec ) val request: JsonRpcRequest = - JsonRpcRequest("2.0", "eth_getFilterChanges", Some(JArray(List(JString("0x1")))), Some(JInt(1))) + newJsonRpcRequest("eth_getFilterChanges", List(JString("0x1"))) val response = jsonRpcController.handleRequest(request).futureValue response should haveResult( @@ -1621,17 +1266,7 @@ class JsonRpcControllerEthSpec it should "eth_getFilterLogs" in new TestSetup { val mockEthService = mock[EthService] - override val jsonRpcController = - new JsonRpcController( - web3Service, - netService, - mockEthService, - personalService, - None, - debugService, - qaService, - config - ) + override val jsonRpcController = newJsonRpcController(mockEthService) (mockEthService.getFilterLogs _) .expects(*) @@ -1652,7 +1287,7 @@ class JsonRpcControllerEthSpec ) val request: JsonRpcRequest = - JsonRpcRequest("2.0", "eth_getFilterLogs", Some(JArray(List(JString("0x1")))), Some(JInt(1))) + newJsonRpcRequest("eth_getFilterLogs", List(JString("0x1"))) val response = jsonRpcController.handleRequest(request).futureValue response should haveResult(JArray(List(JString("0x1234"), JString("0x4567"), JString("0x7890")))) @@ -1660,17 +1295,7 @@ class JsonRpcControllerEthSpec it should "eth_getLogs" in new TestSetup { val mockEthService = mock[EthService] - override val jsonRpcController = - new JsonRpcController( - web3Service, - netService, - mockEthService, - personalService, - None, - debugService, - qaService, - config - ) + override val jsonRpcController = newJsonRpcController(mockEthService) (mockEthService.getLogs _) .expects(*) @@ -1697,22 +1322,16 @@ class JsonRpcControllerEthSpec ) ) - val request: JsonRpcRequest = JsonRpcRequest( - "2.0", + val request: JsonRpcRequest = newJsonRpcRequest( "eth_getLogs", - Some( - JArray( - List( - JObject( - "fromBlock" -> "0x0", - "toBlock" -> "latest", - "address" -> "0x2B5A350698C91E684EB08c10F7e462f761C0e681", - "topics" -> JArray(List(JNull, "0x00000000000000000000000000000000000000000000000000000000000001c8")) - ) - ) + List( + JObject( + "fromBlock" -> "0x0", + "toBlock" -> "latest", + "address" -> "0x2B5A350698C91E684EB08c10F7e462f761C0e681", + "topics" -> JArray(List(JNull, "0x00000000000000000000000000000000000000000000000000000000000001c8")) ) - ), - Some(JInt(1)) + ) ) val response = jsonRpcController.handleRequest(request).futureValue @@ -1735,7 +1354,7 @@ class JsonRpcControllerEthSpec } it should "rpc_modules" in new TestSetup { - val request: JsonRpcRequest = JsonRpcRequest("2.0", "rpc_modules", None, Some(JInt(1))) + val request: JsonRpcRequest = newJsonRpcRequest("rpc_modules") val response = jsonRpcController.handleRequest(request).futureValue @@ -1755,17 +1374,7 @@ class JsonRpcControllerEthSpec it should "eth_getTransactionReceipt" in new TestSetup { val mockEthService = mock[EthService] - override val jsonRpcController = - new JsonRpcController( - web3Service, - netService, - mockEthService, - personalService, - None, - debugService, - qaService, - config - ) + override val jsonRpcController = newJsonRpcController(mockEthService) val arbitraryValue = 42 @@ -1803,11 +1412,9 @@ class JsonRpcControllerEthSpec (mockEthService.getTransactionReceipt _).expects(*).returning(Future.successful(mockResponse)) - val request: JsonRpcRequest = JsonRpcRequest( - "2.0", + val request: JsonRpcRequest = newJsonRpcRequest( "eth_getTransactionReceipt", - Some(JArray(List(JString(s"0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238")))), - Some(JInt(1)) + List(JString(s"0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238")) ) val response = jsonRpcController.handleRequest(request).futureValue @@ -1845,18 +1452,8 @@ class JsonRpcControllerEthSpec } it should "daedalus_getAccountTransactions" in new TestSetup { - val mockEthService = mock[EthService] - override val jsonRpcController = - new JsonRpcController( - web3Service, - netService, - mockEthService, - personalService, - None, - debugService, - qaService, - config - ) + val mockEthService: EthService = mock[EthService] + override val jsonRpcController = newJsonRpcController(mockEthService) val block = Fixtures.Blocks.Block3125369 val sentTx = block.body.transactionList.head @@ -1877,19 +1474,13 @@ class JsonRpcControllerEthSpec ) ) - val request: JsonRpcRequest = JsonRpcRequest( - "2.0", + val request: JsonRpcRequest = newJsonRpcRequest( "daedalus_getAccountTransactions", - Some( - JArray( - List( - JString(s"0x7B9Bc474667Db2fFE5b08d000F1Acc285B2Ae47D"), - JInt(100), - JInt(200) - ) - ) - ), - Some(JInt(1)) + List( + JString(s"0x7B9Bc474667Db2fFE5b08d000F1Acc285B2Ae47D"), + JInt(100), + JInt(200) + ) ) val response = jsonRpcController.handleRequest(request).futureValue @@ -1900,89 +1491,4 @@ class JsonRpcControllerEthSpec response should haveObjectResult("transactions" -> JArray(expectedTxs.toList)) } - - trait TestSetup extends MockFactory with EphemBlockchainTestSetup with JsonMethodsImplicits { - def config: JsonRpcConfig = JsonRpcConfig(Config.config) - - def rawTrnHex(xs: Seq[SignedTransaction], idx: Int): Option[JString] = - xs.lift(idx) - .map(encodeSignedTrx) - - def encodeSignedTrx(x: SignedTransaction) = - encodeAsHex(RawTransactionCodec.asRawTransaction(x)) - - val version = Config.clientVersion - val blockGenerator = mock[EthashBlockGenerator] - - override implicit lazy val system = ActorSystem("JsonRpcControllerSpec_System") - - val syncingController = TestProbe() - override lazy val ledger = mock[Ledger] - override lazy val stxLedger = mock[StxLedger] - override lazy val validators = mock[ValidatorsExecutor] - override lazy val consensus: TestConsensus = buildTestConsensus() - .withValidators(validators) - .withBlockGenerator(blockGenerator) - - val keyStore = mock[KeyStore] - - val pendingTransactionsManager = TestProbe() - val ommersPool = TestProbe() - val filterManager = TestProbe() - - val ethashConfig = ConsensusConfigs.ethashConfig - override lazy val consensusConfig = ConsensusConfigs.consensusConfig - val fullConsensusConfig = ConsensusConfigs.fullConsensusConfig - val getTransactionFromPoolTimeout: FiniteDuration = 5.seconds - - val filterConfig = new FilterConfig { - override val filterTimeout: FiniteDuration = Timeouts.normalTimeout - override val filterManagerQueryTimeout: FiniteDuration = Timeouts.normalTimeout - } - - val currentProtocolVersion = 63 - - val appStateStorage = mock[AppStateStorage] - val web3Service = new Web3Service - val netService = mock[NetService] - val personalService = mock[PersonalService] - val debugService = mock[DebugService] - val qaService = mock[QAService] - - val ethService = new EthService( - blockchain, - appStateStorage, - ledger, - stxLedger, - keyStore, - pendingTransactionsManager.ref, - syncingController.ref, - ommersPool.ref, - filterManager.ref, - filterConfig, - blockchainConfig, - currentProtocolVersion, - config, - getTransactionFromPoolTimeout - ) - - val jsonRpcController = - new JsonRpcController(web3Service, netService, ethService, personalService, None, debugService, qaService, config) - - val blockHeader = Fixtures.Blocks.ValidBlock.header.copy( - logsBloom = BloomFilter.EmptyBloomFilter, - difficulty = 10, - number = 2, - gasLimit = 0, - gasUsed = 0, - unixTimestamp = 0 - ) - - val parentBlock = Block(blockHeader.copy(number = 1), BlockBody.empty) - - val r: ByteString = ByteString(Hex.decode("a3f20717a250c2b0b729b7e5becbff67fdaef7e0699da4de7ca5895b02a170a1")) - val s: ByteString = ByteString(Hex.decode("2d887fd3b17bfdce3481f10bea41f45ba9f709d39ce8325427b57afcfc994cee")) - val v: Byte = ByteString(Hex.decode("1b")).last - val sig = ECDSASignature(r, s, v) - } } diff --git a/src/test/scala/io/iohk/ethereum/jsonrpc/TestSetup.scala b/src/test/scala/io/iohk/ethereum/jsonrpc/TestSetup.scala new file mode 100644 index 0000000000..1d8f8481f9 --- /dev/null +++ b/src/test/scala/io/iohk/ethereum/jsonrpc/TestSetup.scala @@ -0,0 +1,119 @@ +package io.iohk.ethereum.jsonrpc + +import akka.actor.ActorSystem +import akka.testkit.TestProbe +import akka.util.ByteString +import io.iohk.ethereum.{Fixtures, Timeouts} +import io.iohk.ethereum.blockchain.sync.EphemBlockchainTestSetup +import io.iohk.ethereum.consensus.{ConsensusConfigs, TestConsensus} +import io.iohk.ethereum.consensus.ethash.blocks.EthashBlockGenerator +import io.iohk.ethereum.consensus.ethash.validators.ValidatorsExecutor +import io.iohk.ethereum.crypto.ECDSASignature +import io.iohk.ethereum.db.storage.AppStateStorage +import io.iohk.ethereum.domain.{Block, BlockBody, SignedTransaction} +import io.iohk.ethereum.jsonrpc.JsonRpcController.JsonRpcConfig +import io.iohk.ethereum.keystore.KeyStore +import io.iohk.ethereum.ledger.{BloomFilter, Ledger, StxLedger} +import io.iohk.ethereum.utils.{Config, FilterConfig} +import org.bouncycastle.util.encoders.Hex +import org.json4s.JsonAST.JString +import org.scalamock.scalatest.MockFactory + +import scala.concurrent.duration._ + +trait TestSetup extends MockFactory with EphemBlockchainTestSetup with JsonMethodsImplicits { + def config: JsonRpcConfig = JsonRpcConfig(Config.config) + + def rawTrnHex(xs: Seq[SignedTransaction], idx: Int): Option[JString] = + xs.lift(idx) + .map(encodeSignedTrx) + + def encodeSignedTrx(x: SignedTransaction) = + encodeAsHex(RawTransactionCodec.asRawTransaction(x)) + + val version = Config.clientVersion + val blockGenerator = mock[EthashBlockGenerator] + + override implicit lazy val system = ActorSystem("JsonRpcControllerSpec_System") + + val syncingController = TestProbe() + override lazy val ledger = mock[Ledger] + override lazy val stxLedger = mock[StxLedger] + override lazy val validators = mock[ValidatorsExecutor] + override lazy val consensus: TestConsensus = buildTestConsensus() + .withValidators(validators) + .withBlockGenerator(blockGenerator) + + val keyStore = mock[KeyStore] + + val pendingTransactionsManager = TestProbe() + val ommersPool = TestProbe() + val filterManager = TestProbe() + + val ethashConfig = ConsensusConfigs.ethashConfig + override lazy val consensusConfig = ConsensusConfigs.consensusConfig + val fullConsensusConfig = ConsensusConfigs.fullConsensusConfig + val getTransactionFromPoolTimeout: FiniteDuration = 5.seconds + + val filterConfig = new FilterConfig { + override val filterTimeout: FiniteDuration = Timeouts.normalTimeout + override val filterManagerQueryTimeout: FiniteDuration = Timeouts.normalTimeout + } + + val currentProtocolVersion = 63 + + val appStateStorage = mock[AppStateStorage] + val web3Service = new Web3Service + val netService = mock[NetService] + val personalService = mock[PersonalService] + val debugService = mock[DebugService] + val qaService = mock[QAService] + + val ethService = new EthService( + blockchain, + appStateStorage, + ledger, + stxLedger, + keyStore, + pendingTransactionsManager.ref, + syncingController.ref, + ommersPool.ref, + filterManager.ref, + filterConfig, + blockchainConfig, + currentProtocolVersion, + config, + getTransactionFromPoolTimeout + ) + + protected def newJsonRpcController(ethService: EthService) = + new JsonRpcController( + web3Service, + netService, + ethService, + personalService, + None, + debugService, + qaService, + config + ) + + val jsonRpcController = + new JsonRpcController(web3Service, netService, ethService, personalService, None, debugService, qaService, config) + + val blockHeader = Fixtures.Blocks.ValidBlock.header.copy( + logsBloom = BloomFilter.EmptyBloomFilter, + difficulty = 10, + number = 2, + gasLimit = 0, + gasUsed = 0, + unixTimestamp = 0 + ) + + val parentBlock = Block(blockHeader.copy(number = 1), BlockBody.empty) + + val r: ByteString = ByteString(Hex.decode("a3f20717a250c2b0b729b7e5becbff67fdaef7e0699da4de7ca5895b02a170a1")) + val s: ByteString = ByteString(Hex.decode("2d887fd3b17bfdce3481f10bea41f45ba9f709d39ce8325427b57afcfc994cee")) + val v: Byte = ByteString(Hex.decode("1b")).last + val sig = ECDSASignature(r, s, v) +} From 57c404f4d6ad87126af38bb70b29b2b1a91e62ae Mon Sep 17 00:00:00 2001 From: lemastero Date: Tue, 13 Oct 2020 19:15:43 +0200 Subject: [PATCH 3/7] [ETCM-126] split JsonRpcControllerSpec into JRCFixture JRCEthSpec JRCSpec --- .../jsonrpc/JsonRpcControllerEthSpec.scala | 465 ++---------------- ...p.scala => JsonRpcControllerFixture.scala} | 10 +- .../jsonrpc/JsonRpcControllerSpec.scala | 402 +++++++++++++++ 3 files changed, 455 insertions(+), 422 deletions(-) rename src/test/scala/io/iohk/ethereum/jsonrpc/{TestSetup.scala => JsonRpcControllerFixture.scala} (90%) create mode 100644 src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerSpec.scala diff --git a/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerEthSpec.scala b/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerEthSpec.scala index 709036a2cc..388a077bab 100644 --- a/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerEthSpec.scala +++ b/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerEthSpec.scala @@ -1,25 +1,15 @@ package io.iohk.ethereum.jsonrpc -import java.time.Duration - import akka.util.ByteString import io.iohk.ethereum.consensus.Consensus import io.iohk.ethereum.consensus.blocks.PendingBlock import io.iohk.ethereum.consensus.validators.SignedTransactionValidator import io.iohk.ethereum.crypto.kec256 import io.iohk.ethereum.domain._ -import io.iohk.ethereum.jsonrpc.DebugService.{ListPeersInfoRequest, ListPeersInfoResponse} import io.iohk.ethereum.jsonrpc.EthService._ import io.iohk.ethereum.jsonrpc.FilterManager.{LogFilterLogs, TxLog} -import io.iohk.ethereum.jsonrpc.JsonRpcController.JsonRpcConfig import io.iohk.ethereum.jsonrpc.JsonSerializers.{OptionNoneToJNullSerializer, QuantitiesSerializer, UnformattedDataJsonSerializer} -import io.iohk.ethereum.jsonrpc.NetService.{ListeningResponse, PeerCountResponse, VersionResponse} import io.iohk.ethereum.jsonrpc.PersonalService._ -import io.iohk.ethereum.jsonrpc.server.http.JsonRpcHttpServer -import io.iohk.ethereum.jsonrpc.server.ipc.JsonRpcIpcServer -import io.iohk.ethereum.network.EtcPeerManagerActor.PeerInfo -import io.iohk.ethereum.network.p2p.messages.CommonMessages.Status -import io.iohk.ethereum.network.p2p.messages.Versions import io.iohk.ethereum.ommers.OmmersPool import io.iohk.ethereum.ommers.OmmersPool.Ommers import io.iohk.ethereum.transactions.PendingTransactionsManager @@ -34,7 +24,6 @@ import org.scalatest.matchers.should.Matchers import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks import scala.concurrent.Future -import scala.concurrent.duration._ // scalastyle:off file.size.limit // scalastyle:off magic.number @@ -47,84 +36,24 @@ class JsonRpcControllerEthSpec with LongPatience with Eventually { - private def newJsonRpcRequest(method: String, params: List[JValue]) = - JsonRpcRequest("2.0", method, Some(JArray(params)), Some(JInt(1))) - - private def newJsonRpcRequest(method: String) = - JsonRpcRequest("2.0", method, None, Some(JInt(1))) - implicit val formats: Formats = DefaultFormats.preservingEmptyValues + OptionNoneToJNullSerializer + QuantitiesSerializer + UnformattedDataJsonSerializer - "JsonRpcController" should "handle valid sha3 request" in new TestSetup { - val rpcRequest = newJsonRpcRequest("web3_sha3", JString("0x1234") :: Nil) - - val response = jsonRpcController.handleRequest(rpcRequest).futureValue - - response should haveStringResult("0x56570de287d73cd1cb6092bb8fdee6173974955fdef345ae579ee9f475ea7432") - } - - it should "fail when invalid request is received" in new TestSetup { - val rpcRequest = newJsonRpcRequest("web3_sha3", JString("asdasd") :: Nil) - - val response = jsonRpcController.handleRequest(rpcRequest).futureValue - - response should haveError(JsonRpcErrors.InvalidParams("Invalid method parameters")) - } - - it should "handle clientVersion request" in new TestSetup { - val rpcRequest = newJsonRpcRequest("web3_clientVersion") - - val response = jsonRpcController.handleRequest(rpcRequest).futureValue - - response should haveStringResult(version) - } - - it should "Handle net_peerCount request" in new TestSetup { - (netService.peerCount _).expects(*).returning(Future.successful(Right(PeerCountResponse(123)))) - - val rpcRequest = newJsonRpcRequest("net_peerCount") - - val response = jsonRpcController.handleRequest(rpcRequest).futureValue - - response should haveStringResult("0x7b") - } - - it should "Handle net_listening request" in new TestSetup { - (netService.listening _).expects(*).returning(Future.successful(Right(ListeningResponse(false)))) - - val rpcRequest = newJsonRpcRequest("net_listening") - val response = jsonRpcController.handleRequest(rpcRequest).futureValue - - response should haveBooleanResult(false) - } - - it should "Handle net_version request" in new TestSetup { - val netVersion = "99" - - (netService.version _).expects(*).returning(Future.successful(Right(VersionResponse(netVersion)))) - - val rpcRequest = newJsonRpcRequest("net_version") - val response = jsonRpcController.handleRequest(rpcRequest).futureValue - - response should haveStringResult(netVersion) - } - - it should "eth_protocolVersion" in new TestSetup { + it should "eth_protocolVersion" in new JsonRpcControllerFixture { val rpcRequest = newJsonRpcRequest("eth_protocolVersion") val response = jsonRpcController.handleRequest(rpcRequest).futureValue response should haveStringResult("0x3f") } - it should "handle eth_chainId" in new TestSetup { + it should "handle eth_chainId" in new JsonRpcControllerFixture { val request = newJsonRpcRequest("eth_chainId") val response = jsonRpcController.handleRequest(request).futureValue response should haveStringResult("0x3d") } - it should "handle eth_blockNumber request" in new TestSetup { + it should "handle eth_blockNumber request" in new JsonRpcControllerFixture { val bestBlockNumber = 10 blockchain.saveBestKnownBlocks(bestBlockNumber) @@ -134,7 +63,7 @@ class JsonRpcControllerEthSpec response should haveStringResult(s"0xa") } - it should "eth_syncing" in new TestSetup { + it should "eth_syncing" in new JsonRpcControllerFixture { (appStateStorage.getSyncStartingBlock _).expects().returning(100) (appStateStorage.getBestBlockNumber _).expects().returning(200) (appStateStorage.getEstimatedHighestBlock _).expects().returning(300) @@ -151,27 +80,7 @@ class JsonRpcControllerEthSpec ) } - it should "only allow to call methods of enabled apis" in new TestSetup { - override def config: JsonRpcConfig = new JsonRpcConfig { - override val apis = Seq("web3") - override val accountTransactionsMaxBlocks = 50000 - override def minerActiveTimeout: FiniteDuration = ??? - override def httpServerConfig: JsonRpcHttpServer.JsonRpcHttpServerConfig = ??? - override def ipcServerConfig: JsonRpcIpcServer.JsonRpcIpcServerConfig = ??? - } - - val ethRpcRequest = newJsonRpcRequest("eth_protocolVersion") - val ethResponse = jsonRpcController.handleRequest(ethRpcRequest).futureValue - - ethResponse should haveError(JsonRpcErrors.MethodNotFound) - - val web3RpcRequest = newJsonRpcRequest("web3_clientVersion") - val web3Response = jsonRpcController.handleRequest(web3RpcRequest).futureValue - - web3Response should haveStringResult(version) - } - - it should "handle eth_getBlockTransactionCountByHash request" in new TestSetup { + it should "handle eth_getBlockTransactionCountByHash request" in new JsonRpcControllerFixture { val blockToRequest = Block(Fixtures.Blocks.Block3125369.header, Fixtures.Blocks.Block3125369.body) blockchain.storeBlock(blockToRequest).commit() @@ -186,7 +95,7 @@ class JsonRpcControllerEthSpec response should haveResult(expectedTxCount) } - it should "handle eth_getBlockByHash request" in new TestSetup { + it should "handle eth_getBlockByHash request" in new JsonRpcControllerFixture { val blockToRequest = Block(Fixtures.Blocks.Block3125369.header, Fixtures.Blocks.Block3125369.body) val blockTd = blockToRequest.header.difficulty @@ -207,7 +116,7 @@ class JsonRpcControllerEthSpec response should haveResult(expectedBlockResponse) } - it should "handle eth_getBlockByNumber request" in new TestSetup { + it should "handle eth_getBlockByNumber request" in new JsonRpcControllerFixture { val blockToRequest = Block(Fixtures.Blocks.Block3125369.header, Fixtures.Blocks.Block3125369.body) val blockTd = blockToRequest.header.difficulty @@ -228,7 +137,7 @@ class JsonRpcControllerEthSpec response should haveResult(expectedBlockResponse) } - it should "handle eth_getUncleByBlockHashAndIndex request" in new TestSetup { + it should "handle eth_getUncleByBlockHashAndIndex request" in new JsonRpcControllerFixture { val uncle = Fixtures.Blocks.DaoForkBlock.header val blockToRequest = Block(Fixtures.Blocks.Block3125369.header, BlockBody(Nil, Seq(uncle))) @@ -253,7 +162,7 @@ class JsonRpcControllerEthSpec response should haveResult(expectedUncleBlockResponse) } - it should "handle eth_getUncleByBlockNumberAndIndex request" in new TestSetup { + it should "handle eth_getUncleByBlockNumberAndIndex request" in new JsonRpcControllerFixture { val uncle = Fixtures.Blocks.DaoForkBlock.header val blockToRequest = Block(Fixtures.Blocks.Block3125369.header, BlockBody(Nil, Seq(uncle))) @@ -278,7 +187,7 @@ class JsonRpcControllerEthSpec response should haveResult(expectedUncleBlockResponse) } - it should "handle eth_getTransactionByBlockHashAndIndex request" in new TestSetup { + it should "handle eth_getTransactionByBlockHashAndIndex request" in new JsonRpcControllerFixture { val blockToRequest = Block(Fixtures.Blocks.Block3125369.header, Fixtures.Blocks.Block3125369.body) val txIndexToRequest = blockToRequest.body.transactionList.size / 2 @@ -301,7 +210,7 @@ class JsonRpcControllerEthSpec response should haveResult(expectedTxResponse) } - it should "handle eth_getRawTransactionByBlockHashAndIndex request" in new TestSetup { + it should "handle eth_getRawTransactionByBlockHashAndIndex request" in new JsonRpcControllerFixture { val blockToRequest = Block(Fixtures.Blocks.Block3125369.header, Fixtures.Blocks.Block3125369.body) val txIndexToRequest = blockToRequest.body.transactionList.size / 2 @@ -321,7 +230,7 @@ class JsonRpcControllerEthSpec response should haveResult(expectedTxResponse) } - it should "handle eth_getRawTransactionByHash request" in new TestSetup { + it should "handle eth_getRawTransactionByHash request" in new JsonRpcControllerFixture { val mockEthService = mock[EthService] override val jsonRpcController = newJsonRpcController(mockEthService) @@ -341,180 +250,7 @@ class JsonRpcControllerEthSpec response should haveResult(encodeSignedTrx(txResponse)) } - it should "personal_importRawKey" in new TestSetup { - val key = "7a44789ed3cd85861c0bbf9693c7e1de1862dd4396c390147ecf1275099c6e6f" - val keyBytes = ByteString(Hex.decode(key)) - val addr = Address("0x00000000000000000000000000000000000000ff") - val pass = "aaa" - - (personalService.importRawKey _) - .expects(ImportRawKeyRequest(keyBytes, pass)) - .returning(Future.successful(Right(ImportRawKeyResponse(addr)))) - - val params = JString(key) :: JString(pass) :: Nil - val rpcRequest = newJsonRpcRequest("personal_importRawKey", params) - val response = jsonRpcController.handleRequest(rpcRequest).futureValue - - response should haveStringResult(addr.toString) - } - - it should "personal_newAccount" in new TestSetup { - val addr = Address("0x00000000000000000000000000000000000000ff") - val pass = "aaa" - - (personalService.newAccount _) - .expects(NewAccountRequest(pass)) - .returning(Future.successful(Right(NewAccountResponse(addr)))) - - val params = JString(pass) :: Nil - val rpcRequest = newJsonRpcRequest("personal_newAccount", params) - val response = jsonRpcController.handleRequest(rpcRequest).futureValue - - response should haveStringResult(addr.toString) - } - - it should "personal_listAccounts" in new TestSetup { - val addresses = List(34, 12391, 123).map(Address(_)) - val pass = "aaa" - - (personalService.listAccounts _) - .expects(ListAccountsRequest()) - .returning(Future.successful(Right(ListAccountsResponse(addresses)))) - - val rpcRequest = newJsonRpcRequest("personal_listAccounts") - val response = jsonRpcController.handleRequest(rpcRequest).futureValue - - response should haveResult(JArray(addresses.map(a => JString(a.toString)))) - } - - it should "personal_unlockAccount" in new TestSetup { - val address = Address(42) - val pass = "aaa" - val params = JString(address.toString) :: JString(pass) :: Nil - - (personalService.unlockAccount _) - .expects(UnlockAccountRequest(address, pass, None)) - .returning(Future.successful(Right(UnlockAccountResponse(true)))) - - val rpcRequest = newJsonRpcRequest("personal_unlockAccount", params) - val response = jsonRpcController.handleRequest(rpcRequest).futureValue - - response should haveBooleanResult(true) - } - - it should "personal_unlockAccount for specified duration" in new TestSetup { - val address = Address(42) - val pass = "aaa" - val dur = "1" - val params = JString(address.toString) :: JString(pass) :: JString(dur) :: Nil - - (personalService.unlockAccount _) - .expects(UnlockAccountRequest(address, pass, Some(Duration.ofSeconds(1)))) - .returning(Future.successful(Right(UnlockAccountResponse(true)))) - - val rpcRequest = newJsonRpcRequest("personal_unlockAccount", params) - val response = jsonRpcController.handleRequest(rpcRequest).futureValue - - response should haveBooleanResult(true) - } - - it should "personal_unlockAccount should handle possible duration errors" in new TestSetup { - val address = Address(42) - val pass = "aaa" - val dur = "alksjdfh" - - val params = JString(address.toString) :: JString(pass) :: JString(dur) :: Nil - val rpcRequest = newJsonRpcRequest("personal_unlockAccount", params) - val response = jsonRpcController.handleRequest(rpcRequest).futureValue - - response should haveError(JsonRpcError(-32602, "Invalid method parameters", None)) - - val dur2 = Long.MaxValue - val params2 = JString(address.toString) :: JString(pass) :: JInt(dur2) :: Nil - val rpcRequest2 = newJsonRpcRequest("personal_unlockAccount", params2) - val response2 = jsonRpcController.handleRequest(rpcRequest2).futureValue - response2 should haveError( - JsonRpcError(-32602, "Duration should be an number of seconds, less than 2^31 - 1", None) - ) - } - - it should "personal_unlockAccount should handle null passed as a duration for compatibility with Parity and web3j" in new TestSetup { - val address = Address(42) - val pass = "aaa" - val params = JString(address.toString) :: JString(pass) :: JNull :: Nil - - (personalService.unlockAccount _) - .expects(UnlockAccountRequest(address, pass, None)) - .returning(Future.successful(Right(UnlockAccountResponse(true)))) - - val rpcRequest = newJsonRpcRequest("personal_unlockAccount", params) - val response = jsonRpcController.handleRequest(rpcRequest).futureValue - - response should haveBooleanResult(true) - } - - it should "personal_lockAccount" in new TestSetup { - val address = Address(42) - val params = JString(address.toString) :: Nil - - (personalService.lockAccount _) - .expects(LockAccountRequest(address)) - .returning(Future.successful(Right(LockAccountResponse(true)))) - - val rpcRequest = newJsonRpcRequest("personal_lockAccount", params) - val response = jsonRpcController.handleRequest(rpcRequest).futureValue - - response should haveBooleanResult(true) - } - - it should "personal_sendTransaction" in new TestSetup { - val params = JObject( - "from" -> Address(42).toString, - "to" -> Address(123).toString, - "value" -> 1000 - ) :: JString("passphrase") :: Nil - - val txHash = ByteString(1, 2, 3, 4) - - (personalService - .sendTransaction(_: SendTransactionWithPassphraseRequest)) - .expects(*) - .returning(Future.successful(Right(SendTransactionWithPassphraseResponse(txHash)))) - - val rpcRequest = newJsonRpcRequest("personal_sendTransaction", params) - val response = jsonRpcController.handleRequest(rpcRequest).futureValue - - response should haveResult(JString(s"0x${Hex.toHexString(txHash.toArray)}")) - } - - it should "debug_listPeersInfo" in new TestSetup { - val peerStatus = Status( - protocolVersion = Versions.PV63, - networkId = 1, - totalDifficulty = BigInt("10000"), - bestHash = Fixtures.Blocks.Block3125369.header.hash, - genesisHash = Fixtures.Blocks.Genesis.header.hash - ) - val initialPeerInfo = PeerInfo( - remoteStatus = peerStatus, - totalDifficulty = peerStatus.totalDifficulty, - forkAccepted = true, - maxBlockNumber = Fixtures.Blocks.Block3125369.header.number, - bestBlockHash = peerStatus.bestHash - ) - val peers = List(initialPeerInfo) - - (debugService.listPeersInfo _) - .expects(ListPeersInfoRequest()) - .returning(Future.successful(Right(ListPeersInfoResponse(peers)))) - - val rpcRequest = newJsonRpcRequest("debug_listPeersInfo") - val response: JsonRpcResponse = jsonRpcController.handleRequest(rpcRequest).futureValue - - response should haveResult(JArray(peers.map(info => JString(info.toString)))) - } - - it should "eth_sendTransaction" in new TestSetup { + it should "eth_sendTransaction" in new JsonRpcControllerFixture { val params = JObject( "from" -> Address(42).toString, "to" -> Address(123).toString, @@ -534,7 +270,7 @@ class JsonRpcControllerEthSpec response should haveResult(JString(s"0x${Hex.toHexString(txHash.toArray)}")) } - it should "eth_getWork" in new TestSetup { + it should "eth_getWork" in new JsonRpcControllerFixture { // Just record the fact that this is going to be called, we do not care about the returned value (validators.signedTransactionValidator _: (() => SignedTransactionValidator)) .expects() @@ -574,7 +310,7 @@ class JsonRpcControllerEthSpec ) } - it should "eth_getWork when fail to get ommers and transactions" in new TestSetup { + it should "eth_getWork when fail to get ommers and transactions" in new JsonRpcControllerFixture { // Just record the fact that this is going to be called, we do not care about the returned value (validators.signedTransactionValidator _: (() => SignedTransactionValidator)) .expects() @@ -612,7 +348,7 @@ class JsonRpcControllerEthSpec ) } - it should "eth_submitWork" in new TestSetup { + it should "eth_submitWork" in new JsonRpcControllerFixture { // Just record the fact that this is going to be called, we do not care about the returned value (validators.signedTransactionValidator _: (() => SignedTransactionValidator)) .expects() @@ -643,7 +379,7 @@ class JsonRpcControllerEthSpec response should haveBooleanResult(true) } - it should "eth_submitHashrate" in new TestSetup { + it should "eth_submitHashrate" in new JsonRpcControllerFixture { // Just record the fact that this is going to be called, we do not care about the returned value (validators.signedTransactionValidator _: (() => SignedTransactionValidator)) .expects() @@ -664,7 +400,7 @@ class JsonRpcControllerEthSpec response should haveBooleanResult(true) } - it should "eth_hashrate" in new TestSetup { + it should "eth_hashrate" in new JsonRpcControllerFixture { // Just record the fact that this is going to be called, we do not care about the returned value (validators.signedTransactionValidator _: (() => SignedTransactionValidator)) .expects() @@ -679,7 +415,7 @@ class JsonRpcControllerEthSpec response should haveStringResult("0x0") } - it should "eth_gasPrice" in new TestSetup { + it should "eth_gasPrice" in new JsonRpcControllerFixture { blockchain .storeBlock(Block(Fixtures.Blocks.Block3125369.header.copy(number = 42), Fixtures.Blocks.Block3125369.body)) .commit() @@ -691,7 +427,7 @@ class JsonRpcControllerEthSpec response should haveStringResult("0x4a817c800") } - it should "eth_call" in new TestSetup { + it should "eth_call" in new JsonRpcControllerFixture { val mockEthService = mock[EthService] override val jsonRpcController = newJsonRpcController(mockEthService) @@ -714,7 +450,7 @@ class JsonRpcControllerEthSpec response should haveStringResult("0x617364") } - it should "eth_estimateGas" in new TestSetup { + it should "eth_estimateGas" in new JsonRpcControllerFixture { val mockEthService = mock[EthService] override val jsonRpcController = newJsonRpcController(mockEthService) @@ -749,7 +485,7 @@ class JsonRpcControllerEthSpec } - it should "eth_getCode" in new TestSetup { + it should "eth_getCode" in new JsonRpcControllerFixture { val mockEthService = mock[EthService] override val jsonRpcController = newJsonRpcController(mockEthService) @@ -769,7 +505,7 @@ class JsonRpcControllerEthSpec response should haveStringResult("0xffaa22") } - it should "eth_getUncleCountByBlockNumber" in new TestSetup { + it should "eth_getUncleCountByBlockNumber" in new JsonRpcControllerFixture { val mockEthService = mock[EthService] override val jsonRpcController = newJsonRpcController(mockEthService) @@ -788,7 +524,7 @@ class JsonRpcControllerEthSpec response should haveStringResult("0x2") } - it should "eth_getUncleCountByBlockHash " in new TestSetup { + it should "eth_getUncleCountByBlockHash " in new JsonRpcControllerFixture { val mockEthService = mock[EthService] override val jsonRpcController = newJsonRpcController(mockEthService) @@ -807,7 +543,7 @@ class JsonRpcControllerEthSpec response should haveStringResult("0x3") } - it should "eth_getBlockTransactionCountByNumber " in new TestSetup { + it should "eth_getBlockTransactionCountByNumber " in new JsonRpcControllerFixture { val mockEthService = mock[EthService] override val jsonRpcController = newJsonRpcController(mockEthService) @@ -826,7 +562,7 @@ class JsonRpcControllerEthSpec response should haveStringResult("0x11") } - it should "eth_coinbase " in new TestSetup { + it should "eth_coinbase " in new JsonRpcControllerFixture { // Just record the fact that this is going to be called, we do not care about the returned value (validators.signedTransactionValidator _: (() => SignedTransactionValidator)) .expects() @@ -841,7 +577,7 @@ class JsonRpcControllerEthSpec response should haveStringResult("0x000000000000000000000000000000000000002a") } - it should "eth_getTransactionByBlockNumberAndIndex by tag" in new TestSetup { + it should "eth_getTransactionByBlockNumberAndIndex by tag" in new JsonRpcControllerFixture { val blockToRequest = Block(Fixtures.Blocks.Block3125369.header, Fixtures.Blocks.Block3125369.body) val txIndex = 1 @@ -864,7 +600,7 @@ class JsonRpcControllerEthSpec response should haveResult(expectedTxResponse) } - it should "eth_getTransactionByBlockNumberAndIndex by hex number" in new TestSetup { + it should "eth_getTransactionByBlockNumberAndIndex by hex number" in new JsonRpcControllerFixture { val blockToRequest = Block(Fixtures.Blocks.Block3125369.header.copy(number = BigInt(0xc005)), Fixtures.Blocks.Block3125369.body) val txIndex = 1 @@ -887,7 +623,7 @@ class JsonRpcControllerEthSpec response should haveResult(expectedTxResponse) } - it should "eth_getTransactionByBlockNumberAndIndex by number" in new TestSetup { + it should "eth_getTransactionByBlockNumberAndIndex by number" in new JsonRpcControllerFixture { val blockToRequest = Block(Fixtures.Blocks.Block3125369.header, Fixtures.Blocks.Block3125369.body) val txIndex = 1 @@ -909,7 +645,7 @@ class JsonRpcControllerEthSpec response should haveResult(expectedTxResponse) } - it should "eth_getRawTransactionByBlockNumberAndIndex by tag" in new TestSetup { + it should "eth_getRawTransactionByBlockNumberAndIndex by tag" in new JsonRpcControllerFixture { // given val blockToRequest: Block = Block(Fixtures.Blocks.Block3125369.header, Fixtures.Blocks.Block3125369.body) val txIndex = 1 @@ -934,7 +670,7 @@ class JsonRpcControllerEthSpec response should haveResult(expectedTxResponse) } - it should "eth_getRawTransactionByBlockNumberAndIndex by hex number" in new TestSetup { + it should "eth_getRawTransactionByBlockNumberAndIndex by hex number" in new JsonRpcControllerFixture { // given val blockToRequest = Block(Fixtures.Blocks.Block3125369.header.copy(number = BigInt(0xc005)), Fixtures.Blocks.Block3125369.body) @@ -959,7 +695,7 @@ class JsonRpcControllerEthSpec response should haveResult(expectedTxResponse) } - it should "eth_getRawTransactionByBlockNumberAndIndex by number" in new TestSetup { + it should "eth_getRawTransactionByBlockNumberAndIndex by number" in new JsonRpcControllerFixture { val blockToRequest = Block(Fixtures.Blocks.Block3125369.header, Fixtures.Blocks.Block3125369.body) val txIndex = 1 @@ -978,7 +714,7 @@ class JsonRpcControllerEthSpec response should haveResult(expectedTxResponse) } - it should "eth_getBalance" in new TestSetup { + it should "eth_getBalance" in new JsonRpcControllerFixture { val mockEthService = mock[EthService] override val jsonRpcController = newJsonRpcController(mockEthService) @@ -998,7 +734,7 @@ class JsonRpcControllerEthSpec response should haveStringResult("0x11") } - it should "eth_getStorageAt" in new TestSetup { + it should "eth_getStorageAt" in new JsonRpcControllerFixture { val mockEthService = mock[EthService] override val jsonRpcController = newJsonRpcController(mockEthService) @@ -1019,7 +755,7 @@ class JsonRpcControllerEthSpec response should haveResult(JString("0x" + Hex.toHexString(ByteString("response").toArray[Byte]))) } - it should "eth_getTransactionCount" in new TestSetup { + it should "eth_getTransactionCount" in new JsonRpcControllerFixture { val mockEthService = mock[EthService] override val jsonRpcController = newJsonRpcController(mockEthService) @@ -1039,7 +775,7 @@ class JsonRpcControllerEthSpec response should haveStringResult("0x7b") } - it should "eth_getTransactionByHash" in new TestSetup { + it should "eth_getTransactionByHash" in new JsonRpcControllerFixture { val mockEthService = mock[EthService] override val jsonRpcController = newJsonRpcController(mockEthService) @@ -1059,7 +795,7 @@ class JsonRpcControllerEthSpec response should haveResult(Extraction.decompose(txResponse)) } - it should "eth_sign" in new TestSetup { + it should "eth_sign" in new JsonRpcControllerFixture { (personalService.sign _) .expects( @@ -1085,58 +821,7 @@ class JsonRpcControllerEthSpec ) } - it should "personal_sign" in new TestSetup { - - (personalService.sign _) - .expects( - SignRequest( - ByteString(Hex.decode("deadbeaf")), - Address(ByteString(Hex.decode("9b2055d370f73ec7d8a03e965129118dc8f5bf83"))), - Some("thePassphrase") - ) - ) - .returns(Future.successful(Right(SignResponse(sig)))) - - val request: JsonRpcRequest = newJsonRpcRequest( - "personal_sign", - List( - JString(s"0xdeadbeaf"), - JString(s"0x9b2055d370f73ec7d8a03e965129118dc8f5bf83"), - JString("thePassphrase") - ) - ) - - val response = jsonRpcController.handleRequest(request).futureValue - response should haveStringResult( - "0xa3f20717a250c2b0b729b7e5becbff67fdaef7e0699da4de7ca5895b02a170a12d887fd3b17bfdce3481f10bea41f45ba9f709d39ce8325427b57afcfc994cee1b" - ) - } - - it should "personal_ecRecover" in new TestSetup { - - (personalService.ecRecover _) - .expects(EcRecoverRequest(ByteString(Hex.decode("deadbeaf")), sig)) - .returns( - Future.successful( - Right(EcRecoverResponse(Address(ByteString(Hex.decode("9b2055d370f73ec7d8a03e965129118dc8f5bf83"))))) - ) - ) - - val request: JsonRpcRequest = newJsonRpcRequest( - "personal_ecRecover", - List( - JString(s"0xdeadbeaf"), - JString( - s"0xa3f20717a250c2b0b729b7e5becbff67fdaef7e0699da4de7ca5895b02a170a12d887fd3b17bfdce3481f10bea41f45ba9f709d39ce8325427b57afcfc994cee1b" - ) - ) - ) - - val response = jsonRpcController.handleRequest(request).futureValue - response should haveStringResult("0x9b2055d370f73ec7d8a03e965129118dc8f5bf83") - } - - it should "eth_newFilter" in new TestSetup { + it should "eth_newFilter" in new JsonRpcControllerFixture { val mockEthService = mock[EthService] override val jsonRpcController = newJsonRpcController(mockEthService) @@ -1160,7 +845,7 @@ class JsonRpcControllerEthSpec response should haveStringResult("0x7b") } - it should "eth_newBlockFilter" in new TestSetup { + it should "eth_newBlockFilter" in new JsonRpcControllerFixture { val mockEthService = mock[EthService] override val jsonRpcController = newJsonRpcController(mockEthService) @@ -1179,7 +864,7 @@ class JsonRpcControllerEthSpec response should haveStringResult("0x3e7") } - it should "eth_newPendingTransactionFilter" in new TestSetup { + it should "eth_newPendingTransactionFilter" in new JsonRpcControllerFixture { val mockEthService = mock[EthService] override val jsonRpcController = newJsonRpcController(mockEthService) @@ -1196,7 +881,7 @@ class JsonRpcControllerEthSpec response should haveStringResult("0x2") } - it should "eth_uninstallFilter" in new TestSetup { + it should "eth_uninstallFilter" in new JsonRpcControllerFixture { val mockEthService = mock[EthService] override val jsonRpcController = newJsonRpcController(mockEthService) @@ -1213,7 +898,7 @@ class JsonRpcControllerEthSpec response should haveBooleanResult(true) } - it should "eth_getFilterChanges" in new TestSetup { + it should "eth_getFilterChanges" in new JsonRpcControllerFixture { val mockEthService = mock[EthService] override val jsonRpcController = newJsonRpcController(mockEthService) @@ -1264,7 +949,7 @@ class JsonRpcControllerEthSpec ) } - it should "eth_getFilterLogs" in new TestSetup { + it should "eth_getFilterLogs" in new JsonRpcControllerFixture { val mockEthService = mock[EthService] override val jsonRpcController = newJsonRpcController(mockEthService) @@ -1293,7 +978,7 @@ class JsonRpcControllerEthSpec response should haveResult(JArray(List(JString("0x1234"), JString("0x4567"), JString("0x7890")))) } - it should "eth_getLogs" in new TestSetup { + it should "eth_getLogs" in new JsonRpcControllerFixture { val mockEthService = mock[EthService] override val jsonRpcController = newJsonRpcController(mockEthService) @@ -1353,26 +1038,7 @@ class JsonRpcControllerEthSpec ) } - it should "rpc_modules" in new TestSetup { - val request: JsonRpcRequest = newJsonRpcRequest("rpc_modules") - - val response = jsonRpcController.handleRequest(request).futureValue - - response should haveResult( - JObject( - "net" -> "1.0", - "rpc" -> "1.0", - "personal" -> "1.0", - "eth" -> "1.0", - "web3" -> "1.0", - "daedalus" -> "1.0", - "debug" -> "1.0", - "qa" -> "1.0" - ) - ) - } - - it should "eth_getTransactionReceipt" in new TestSetup { + it should "eth_getTransactionReceipt" in new JsonRpcControllerFixture { val mockEthService = mock[EthService] override val jsonRpcController = newJsonRpcController(mockEthService) @@ -1450,45 +1116,4 @@ class JsonRpcControllerEthSpec ) ) } - - it should "daedalus_getAccountTransactions" in new TestSetup { - val mockEthService: EthService = mock[EthService] - override val jsonRpcController = newJsonRpcController(mockEthService) - - val block = Fixtures.Blocks.Block3125369 - val sentTx = block.body.transactionList.head - val receivedTx = block.body.transactionList.last - - (mockEthService.getAccountTransactions _) - .expects(*) - .returning( - Future.successful( - Right( - GetAccountTransactionsResponse( - Seq( - TransactionResponse(sentTx, Some(block.header), isOutgoing = Some(true)), - TransactionResponse(receivedTx, Some(block.header), isOutgoing = Some(false)) - ) - ) - ) - ) - ) - - val request: JsonRpcRequest = newJsonRpcRequest( - "daedalus_getAccountTransactions", - List( - JString(s"0x7B9Bc474667Db2fFE5b08d000F1Acc285B2Ae47D"), - JInt(100), - JInt(200) - ) - ) - - val response = jsonRpcController.handleRequest(request).futureValue - val expectedTxs = Seq( - Extraction.decompose(TransactionResponse(sentTx, Some(block.header), isOutgoing = Some(true))), - Extraction.decompose(TransactionResponse(receivedTx, Some(block.header), isOutgoing = Some(false))) - ) - - response should haveObjectResult("transactions" -> JArray(expectedTxs.toList)) - } } diff --git a/src/test/scala/io/iohk/ethereum/jsonrpc/TestSetup.scala b/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerFixture.scala similarity index 90% rename from src/test/scala/io/iohk/ethereum/jsonrpc/TestSetup.scala rename to src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerFixture.scala index 1d8f8481f9..6497c38f2e 100644 --- a/src/test/scala/io/iohk/ethereum/jsonrpc/TestSetup.scala +++ b/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerFixture.scala @@ -16,12 +16,12 @@ import io.iohk.ethereum.keystore.KeyStore import io.iohk.ethereum.ledger.{BloomFilter, Ledger, StxLedger} import io.iohk.ethereum.utils.{Config, FilterConfig} import org.bouncycastle.util.encoders.Hex -import org.json4s.JsonAST.JString +import org.json4s.JsonAST.{JArray, JInt, JString, JValue} import org.scalamock.scalatest.MockFactory import scala.concurrent.duration._ -trait TestSetup extends MockFactory with EphemBlockchainTestSetup with JsonMethodsImplicits { +trait JsonRpcControllerFixture extends MockFactory with EphemBlockchainTestSetup with JsonMethodsImplicits { def config: JsonRpcConfig = JsonRpcConfig(Config.config) def rawTrnHex(xs: Seq[SignedTransaction], idx: Int): Option[JString] = @@ -116,4 +116,10 @@ trait TestSetup extends MockFactory with EphemBlockchainTestSetup with JsonMetho val s: ByteString = ByteString(Hex.decode("2d887fd3b17bfdce3481f10bea41f45ba9f709d39ce8325427b57afcfc994cee")) val v: Byte = ByteString(Hex.decode("1b")).last val sig = ECDSASignature(r, s, v) + + def newJsonRpcRequest(method: String, params: List[JValue]) = + JsonRpcRequest("2.0", method, Some(JArray(params)), Some(JInt(1))) + + def newJsonRpcRequest(method: String) = + JsonRpcRequest("2.0", method, None, Some(JInt(1))) } diff --git a/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerSpec.scala b/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerSpec.scala new file mode 100644 index 0000000000..0470d4f3a3 --- /dev/null +++ b/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerSpec.scala @@ -0,0 +1,402 @@ +package io.iohk.ethereum.jsonrpc + +import java.time.Duration + +import akka.util.ByteString +import io.iohk.ethereum.domain._ +import io.iohk.ethereum.jsonrpc.DebugService.{ListPeersInfoRequest, ListPeersInfoResponse} +import io.iohk.ethereum.jsonrpc.EthService._ +import io.iohk.ethereum.jsonrpc.JsonRpcController.JsonRpcConfig +import io.iohk.ethereum.jsonrpc.JsonSerializers.{OptionNoneToJNullSerializer, QuantitiesSerializer, UnformattedDataJsonSerializer} +import io.iohk.ethereum.jsonrpc.NetService.{ListeningResponse, PeerCountResponse, VersionResponse} +import io.iohk.ethereum.jsonrpc.PersonalService._ +import io.iohk.ethereum.jsonrpc.server.http.JsonRpcHttpServer +import io.iohk.ethereum.jsonrpc.server.ipc.JsonRpcIpcServer +import io.iohk.ethereum.network.EtcPeerManagerActor.PeerInfo +import io.iohk.ethereum.network.p2p.messages.CommonMessages.Status +import io.iohk.ethereum.network.p2p.messages.Versions +import io.iohk.ethereum.{Fixtures, LongPatience} +import org.bouncycastle.util.encoders.Hex +import org.json4s.JsonAST._ +import org.json4s.JsonDSL._ +import org.json4s.{DefaultFormats, Extraction, Formats} +import org.scalatest.concurrent.{Eventually, ScalaFutures} +import org.scalatest.flatspec.AnyFlatSpec +import org.scalatest.matchers.should.Matchers +import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks + +import scala.concurrent.Future +import scala.concurrent.duration._ + +// scalastyle:off file.size.limit +// scalastyle:off magic.number +class JsonRpcControllerSpec + extends AnyFlatSpec + with Matchers + with JRCMatchers + with ScalaCheckPropertyChecks + with ScalaFutures + with LongPatience + with Eventually { + + implicit val formats: Formats = DefaultFormats.preservingEmptyValues + OptionNoneToJNullSerializer + + QuantitiesSerializer + UnformattedDataJsonSerializer + + "JsonRpcController" should "handle valid sha3 request" in new JsonRpcControllerFixture { + val rpcRequest = newJsonRpcRequest("web3_sha3", JString("0x1234") :: Nil) + + val response = jsonRpcController.handleRequest(rpcRequest).futureValue + + response should haveStringResult("0x56570de287d73cd1cb6092bb8fdee6173974955fdef345ae579ee9f475ea7432") + } + + it should "fail when invalid request is received" in new JsonRpcControllerFixture { + val rpcRequest = newJsonRpcRequest("web3_sha3", JString("asdasd") :: Nil) + + val response = jsonRpcController.handleRequest(rpcRequest).futureValue + + response should haveError(JsonRpcErrors.InvalidParams("Invalid method parameters")) + } + + it should "handle clientVersion request" in new JsonRpcControllerFixture { + val rpcRequest = newJsonRpcRequest("web3_clientVersion") + + val response = jsonRpcController.handleRequest(rpcRequest).futureValue + + response should haveStringResult(version) + } + + it should "Handle net_peerCount request" in new JsonRpcControllerFixture { + (netService.peerCount _).expects(*).returning(Future.successful(Right(PeerCountResponse(123)))) + + val rpcRequest = newJsonRpcRequest("net_peerCount") + + val response = jsonRpcController.handleRequest(rpcRequest).futureValue + + response should haveStringResult("0x7b") + } + + it should "Handle net_listening request" in new JsonRpcControllerFixture { + (netService.listening _).expects(*).returning(Future.successful(Right(ListeningResponse(false)))) + + val rpcRequest = newJsonRpcRequest("net_listening") + val response = jsonRpcController.handleRequest(rpcRequest).futureValue + + response should haveBooleanResult(false) + } + + it should "Handle net_version request" in new JsonRpcControllerFixture { + val netVersion = "99" + + (netService.version _).expects(*).returning(Future.successful(Right(VersionResponse(netVersion)))) + + val rpcRequest = newJsonRpcRequest("net_version") + val response = jsonRpcController.handleRequest(rpcRequest).futureValue + + response should haveStringResult(netVersion) + } + + it should "only allow to call methods of enabled apis" in new JsonRpcControllerFixture { + override def config: JsonRpcConfig = new JsonRpcConfig { + override val apis = Seq("web3") + override val accountTransactionsMaxBlocks = 50000 + override def minerActiveTimeout: FiniteDuration = ??? + override def httpServerConfig: JsonRpcHttpServer.JsonRpcHttpServerConfig = ??? + override def ipcServerConfig: JsonRpcIpcServer.JsonRpcIpcServerConfig = ??? + } + + val ethRpcRequest = newJsonRpcRequest("eth_protocolVersion") + val ethResponse = jsonRpcController.handleRequest(ethRpcRequest).futureValue + + ethResponse should haveError(JsonRpcErrors.MethodNotFound) + + val web3RpcRequest = newJsonRpcRequest("web3_clientVersion") + val web3Response = jsonRpcController.handleRequest(web3RpcRequest).futureValue + + web3Response should haveStringResult(version) + } + + it should "personal_importRawKey" in new JsonRpcControllerFixture { + val key = "7a44789ed3cd85861c0bbf9693c7e1de1862dd4396c390147ecf1275099c6e6f" + val keyBytes = ByteString(Hex.decode(key)) + val addr = Address("0x00000000000000000000000000000000000000ff") + val pass = "aaa" + + (personalService.importRawKey _) + .expects(ImportRawKeyRequest(keyBytes, pass)) + .returning(Future.successful(Right(ImportRawKeyResponse(addr)))) + + val params = JString(key) :: JString(pass) :: Nil + val rpcRequest = newJsonRpcRequest("personal_importRawKey", params) + val response = jsonRpcController.handleRequest(rpcRequest).futureValue + + response should haveStringResult(addr.toString) + } + + it should "personal_newAccount" in new JsonRpcControllerFixture { + val addr = Address("0x00000000000000000000000000000000000000ff") + val pass = "aaa" + + (personalService.newAccount _) + .expects(NewAccountRequest(pass)) + .returning(Future.successful(Right(NewAccountResponse(addr)))) + + val params = JString(pass) :: Nil + val rpcRequest = newJsonRpcRequest("personal_newAccount", params) + val response = jsonRpcController.handleRequest(rpcRequest).futureValue + + response should haveStringResult(addr.toString) + } + + it should "personal_listAccounts" in new JsonRpcControllerFixture { + val addresses = List(34, 12391, 123).map(Address(_)) + val pass = "aaa" + + (personalService.listAccounts _) + .expects(ListAccountsRequest()) + .returning(Future.successful(Right(ListAccountsResponse(addresses)))) + + val rpcRequest = newJsonRpcRequest("personal_listAccounts") + val response = jsonRpcController.handleRequest(rpcRequest).futureValue + + response should haveResult(JArray(addresses.map(a => JString(a.toString)))) + } + + it should "personal_unlockAccount" in new JsonRpcControllerFixture { + val address = Address(42) + val pass = "aaa" + val params = JString(address.toString) :: JString(pass) :: Nil + + (personalService.unlockAccount _) + .expects(UnlockAccountRequest(address, pass, None)) + .returning(Future.successful(Right(UnlockAccountResponse(true)))) + + val rpcRequest = newJsonRpcRequest("personal_unlockAccount", params) + val response = jsonRpcController.handleRequest(rpcRequest).futureValue + + response should haveBooleanResult(true) + } + + it should "personal_unlockAccount for specified duration" in new JsonRpcControllerFixture { + val address = Address(42) + val pass = "aaa" + val dur = "1" + val params = JString(address.toString) :: JString(pass) :: JString(dur) :: Nil + + (personalService.unlockAccount _) + .expects(UnlockAccountRequest(address, pass, Some(Duration.ofSeconds(1)))) + .returning(Future.successful(Right(UnlockAccountResponse(true)))) + + val rpcRequest = newJsonRpcRequest("personal_unlockAccount", params) + val response = jsonRpcController.handleRequest(rpcRequest).futureValue + + response should haveBooleanResult(true) + } + + it should "personal_unlockAccount should handle possible duration errors" in new JsonRpcControllerFixture { + val address = Address(42) + val pass = "aaa" + val dur = "alksjdfh" + + val params = JString(address.toString) :: JString(pass) :: JString(dur) :: Nil + val rpcRequest = newJsonRpcRequest("personal_unlockAccount", params) + val response = jsonRpcController.handleRequest(rpcRequest).futureValue + + response should haveError(JsonRpcError(-32602, "Invalid method parameters", None)) + + val dur2 = Long.MaxValue + val params2 = JString(address.toString) :: JString(pass) :: JInt(dur2) :: Nil + val rpcRequest2 = newJsonRpcRequest("personal_unlockAccount", params2) + val response2 = jsonRpcController.handleRequest(rpcRequest2).futureValue + response2 should haveError( + JsonRpcError(-32602, "Duration should be an number of seconds, less than 2^31 - 1", None) + ) + } + + it should "personal_unlockAccount should handle null passed as a duration for compatibility with Parity and web3j" in new JsonRpcControllerFixture { + val address = Address(42) + val pass = "aaa" + val params = JString(address.toString) :: JString(pass) :: JNull :: Nil + + (personalService.unlockAccount _) + .expects(UnlockAccountRequest(address, pass, None)) + .returning(Future.successful(Right(UnlockAccountResponse(true)))) + + val rpcRequest = newJsonRpcRequest("personal_unlockAccount", params) + val response = jsonRpcController.handleRequest(rpcRequest).futureValue + + response should haveBooleanResult(true) + } + + it should "personal_lockAccount" in new JsonRpcControllerFixture { + val address = Address(42) + val params = JString(address.toString) :: Nil + + (personalService.lockAccount _) + .expects(LockAccountRequest(address)) + .returning(Future.successful(Right(LockAccountResponse(true)))) + + val rpcRequest = newJsonRpcRequest("personal_lockAccount", params) + val response = jsonRpcController.handleRequest(rpcRequest).futureValue + + response should haveBooleanResult(true) + } + + it should "personal_sendTransaction" in new JsonRpcControllerFixture { + val params = JObject( + "from" -> Address(42).toString, + "to" -> Address(123).toString, + "value" -> 1000 + ) :: JString("passphrase") :: Nil + + val txHash = ByteString(1, 2, 3, 4) + + (personalService + .sendTransaction(_: SendTransactionWithPassphraseRequest)) + .expects(*) + .returning(Future.successful(Right(SendTransactionWithPassphraseResponse(txHash)))) + + val rpcRequest = newJsonRpcRequest("personal_sendTransaction", params) + val response = jsonRpcController.handleRequest(rpcRequest).futureValue + + response should haveResult(JString(s"0x${Hex.toHexString(txHash.toArray)}")) + } + + it should "debug_listPeersInfo" in new JsonRpcControllerFixture { + val peerStatus = Status( + protocolVersion = Versions.PV63, + networkId = 1, + totalDifficulty = BigInt("10000"), + bestHash = Fixtures.Blocks.Block3125369.header.hash, + genesisHash = Fixtures.Blocks.Genesis.header.hash + ) + val initialPeerInfo = PeerInfo( + remoteStatus = peerStatus, + totalDifficulty = peerStatus.totalDifficulty, + forkAccepted = true, + maxBlockNumber = Fixtures.Blocks.Block3125369.header.number, + bestBlockHash = peerStatus.bestHash + ) + val peers = List(initialPeerInfo) + + (debugService.listPeersInfo _) + .expects(ListPeersInfoRequest()) + .returning(Future.successful(Right(ListPeersInfoResponse(peers)))) + + val rpcRequest = newJsonRpcRequest("debug_listPeersInfo") + val response: JsonRpcResponse = jsonRpcController.handleRequest(rpcRequest).futureValue + + response should haveResult(JArray(peers.map(info => JString(info.toString)))) + } + + it should "personal_sign" in new JsonRpcControllerFixture { + + (personalService.sign _) + .expects( + SignRequest( + ByteString(Hex.decode("deadbeaf")), + Address(ByteString(Hex.decode("9b2055d370f73ec7d8a03e965129118dc8f5bf83"))), + Some("thePassphrase") + ) + ) + .returns(Future.successful(Right(SignResponse(sig)))) + + val request: JsonRpcRequest = newJsonRpcRequest( + "personal_sign", + List( + JString(s"0xdeadbeaf"), + JString(s"0x9b2055d370f73ec7d8a03e965129118dc8f5bf83"), + JString("thePassphrase") + ) + ) + + val response = jsonRpcController.handleRequest(request).futureValue + response should haveStringResult( + "0xa3f20717a250c2b0b729b7e5becbff67fdaef7e0699da4de7ca5895b02a170a12d887fd3b17bfdce3481f10bea41f45ba9f709d39ce8325427b57afcfc994cee1b" + ) + } + + it should "personal_ecRecover" in new JsonRpcControllerFixture { + + (personalService.ecRecover _) + .expects(EcRecoverRequest(ByteString(Hex.decode("deadbeaf")), sig)) + .returns( + Future.successful( + Right(EcRecoverResponse(Address(ByteString(Hex.decode("9b2055d370f73ec7d8a03e965129118dc8f5bf83"))))) + ) + ) + + val request: JsonRpcRequest = newJsonRpcRequest( + "personal_ecRecover", + List( + JString(s"0xdeadbeaf"), + JString( + s"0xa3f20717a250c2b0b729b7e5becbff67fdaef7e0699da4de7ca5895b02a170a12d887fd3b17bfdce3481f10bea41f45ba9f709d39ce8325427b57afcfc994cee1b" + ) + ) + ) + + val response = jsonRpcController.handleRequest(request).futureValue + response should haveStringResult("0x9b2055d370f73ec7d8a03e965129118dc8f5bf83") + } + + it should "rpc_modules" in new JsonRpcControllerFixture { + val request: JsonRpcRequest = newJsonRpcRequest("rpc_modules") + + val response = jsonRpcController.handleRequest(request).futureValue + + response should haveResult( + JObject( + "net" -> "1.0", + "rpc" -> "1.0", + "personal" -> "1.0", + "eth" -> "1.0", + "web3" -> "1.0", + "daedalus" -> "1.0", + "debug" -> "1.0", + "qa" -> "1.0" + ) + ) + } + + it should "daedalus_getAccountTransactions" in new JsonRpcControllerFixture { + val mockEthService: EthService = mock[EthService] + override val jsonRpcController = newJsonRpcController(mockEthService) + + val block = Fixtures.Blocks.Block3125369 + val sentTx = block.body.transactionList.head + val receivedTx = block.body.transactionList.last + + (mockEthService.getAccountTransactions _) + .expects(*) + .returning( + Future.successful( + Right( + GetAccountTransactionsResponse( + Seq( + TransactionResponse(sentTx, Some(block.header), isOutgoing = Some(true)), + TransactionResponse(receivedTx, Some(block.header), isOutgoing = Some(false)) + ) + ) + ) + ) + ) + + val request: JsonRpcRequest = newJsonRpcRequest( + "daedalus_getAccountTransactions", + List( + JString(s"0x7B9Bc474667Db2fFE5b08d000F1Acc285B2Ae47D"), + JInt(100), + JInt(200) + ) + ) + + val response = jsonRpcController.handleRequest(request).futureValue + val expectedTxs = Seq( + Extraction.decompose(TransactionResponse(sentTx, Some(block.header), isOutgoing = Some(true))), + Extraction.decompose(TransactionResponse(receivedTx, Some(block.header), isOutgoing = Some(false))) + ) + + response should haveObjectResult("transactions" -> JArray(expectedTxs.toList)) + } +} From a5b95014ca709d370184b9101996ecadcefa8482 Mon Sep 17 00:00:00 2001 From: lemastero Date: Tue, 13 Oct 2020 19:24:41 +0200 Subject: [PATCH 4/7] [ETCM-126] move tests for personal_XYZ endpoints from JsonRpcControllerSpec into JsonRpcControllerPersonalSpec --- .../JsonRpcControllerPersonalSpec.scala | 229 ++++++++++++++++++ .../jsonrpc/JsonRpcControllerSpec.scala | 205 ---------------- 2 files changed, 229 insertions(+), 205 deletions(-) create mode 100644 src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerPersonalSpec.scala diff --git a/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerPersonalSpec.scala b/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerPersonalSpec.scala new file mode 100644 index 0000000000..309b334c86 --- /dev/null +++ b/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerPersonalSpec.scala @@ -0,0 +1,229 @@ +package io.iohk.ethereum.jsonrpc + +import java.time.Duration + +import akka.util.ByteString +import io.iohk.ethereum.LongPatience +import io.iohk.ethereum.domain._ +import io.iohk.ethereum.jsonrpc.JsonSerializers.{OptionNoneToJNullSerializer, QuantitiesSerializer, UnformattedDataJsonSerializer} +import io.iohk.ethereum.jsonrpc.PersonalService._ +import org.bouncycastle.util.encoders.Hex +import org.json4s.JsonAST._ +import org.json4s.JsonDSL._ +import org.json4s.{DefaultFormats, Formats} +import org.scalatest.concurrent.{Eventually, ScalaFutures} +import org.scalatest.flatspec.AnyFlatSpec +import org.scalatest.matchers.should.Matchers +import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks + +import scala.concurrent.Future + +class JsonRpcControllerPersonalSpec + extends AnyFlatSpec + with Matchers + with JRCMatchers + with ScalaCheckPropertyChecks + with ScalaFutures + with LongPatience + with Eventually { + + implicit val formats: Formats = DefaultFormats.preservingEmptyValues + OptionNoneToJNullSerializer + + QuantitiesSerializer + UnformattedDataJsonSerializer + + it should "personal_importRawKey" in new JsonRpcControllerFixture { + val key = "7a44789ed3cd85861c0bbf9693c7e1de1862dd4396c390147ecf1275099c6e6f" + val keyBytes = ByteString(Hex.decode(key)) + val addr = Address("0x00000000000000000000000000000000000000ff") + val pass = "aaa" + + (personalService.importRawKey _) + .expects(ImportRawKeyRequest(keyBytes, pass)) + .returning(Future.successful(Right(ImportRawKeyResponse(addr)))) + + val params = JString(key) :: JString(pass) :: Nil + val rpcRequest = newJsonRpcRequest("personal_importRawKey", params) + val response = jsonRpcController.handleRequest(rpcRequest).futureValue + + response should haveStringResult(addr.toString) + } + + it should "personal_newAccount" in new JsonRpcControllerFixture { + val addr = Address("0x00000000000000000000000000000000000000ff") + val pass = "aaa" + + (personalService.newAccount _) + .expects(NewAccountRequest(pass)) + .returning(Future.successful(Right(NewAccountResponse(addr)))) + + val params = JString(pass) :: Nil + val rpcRequest = newJsonRpcRequest("personal_newAccount", params) + val response = jsonRpcController.handleRequest(rpcRequest).futureValue + + response should haveStringResult(addr.toString) + } + + it should "personal_listAccounts" in new JsonRpcControllerFixture { + val addresses = List(34, 12391, 123).map(Address(_)) + val pass = "aaa" + + (personalService.listAccounts _) + .expects(ListAccountsRequest()) + .returning(Future.successful(Right(ListAccountsResponse(addresses)))) + + val rpcRequest = newJsonRpcRequest("personal_listAccounts") + val response = jsonRpcController.handleRequest(rpcRequest).futureValue + + response should haveResult(JArray(addresses.map(a => JString(a.toString)))) + } + + it should "personal_unlockAccount" in new JsonRpcControllerFixture { + val address = Address(42) + val pass = "aaa" + val params = JString(address.toString) :: JString(pass) :: Nil + + (personalService.unlockAccount _) + .expects(UnlockAccountRequest(address, pass, None)) + .returning(Future.successful(Right(UnlockAccountResponse(true)))) + + val rpcRequest = newJsonRpcRequest("personal_unlockAccount", params) + val response = jsonRpcController.handleRequest(rpcRequest).futureValue + + response should haveBooleanResult(true) + } + + it should "personal_unlockAccount for specified duration" in new JsonRpcControllerFixture { + val address = Address(42) + val pass = "aaa" + val dur = "1" + val params = JString(address.toString) :: JString(pass) :: JString(dur) :: Nil + + (personalService.unlockAccount _) + .expects(UnlockAccountRequest(address, pass, Some(Duration.ofSeconds(1)))) + .returning(Future.successful(Right(UnlockAccountResponse(true)))) + + val rpcRequest = newJsonRpcRequest("personal_unlockAccount", params) + val response = jsonRpcController.handleRequest(rpcRequest).futureValue + + response should haveBooleanResult(true) + } + + it should "personal_unlockAccount should handle possible duration errors" in new JsonRpcControllerFixture { + val address = Address(42) + val pass = "aaa" + val dur = "alksjdfh" + + val params = JString(address.toString) :: JString(pass) :: JString(dur) :: Nil + val rpcRequest = newJsonRpcRequest("personal_unlockAccount", params) + val response = jsonRpcController.handleRequest(rpcRequest).futureValue + + response should haveError(JsonRpcError(-32602, "Invalid method parameters", None)) + + val dur2 = Long.MaxValue + val params2 = JString(address.toString) :: JString(pass) :: JInt(dur2) :: Nil + val rpcRequest2 = newJsonRpcRequest("personal_unlockAccount", params2) + val response2 = jsonRpcController.handleRequest(rpcRequest2).futureValue + response2 should haveError( + JsonRpcError(-32602, "Duration should be an number of seconds, less than 2^31 - 1", None) + ) + } + + it should "personal_unlockAccount should handle null passed as a duration for compatibility with Parity and web3j" in new JsonRpcControllerFixture { + val address = Address(42) + val pass = "aaa" + val params = JString(address.toString) :: JString(pass) :: JNull :: Nil + + (personalService.unlockAccount _) + .expects(UnlockAccountRequest(address, pass, None)) + .returning(Future.successful(Right(UnlockAccountResponse(true)))) + + val rpcRequest = newJsonRpcRequest("personal_unlockAccount", params) + val response = jsonRpcController.handleRequest(rpcRequest).futureValue + + response should haveBooleanResult(true) + } + + it should "personal_lockAccount" in new JsonRpcControllerFixture { + val address = Address(42) + val params = JString(address.toString) :: Nil + + (personalService.lockAccount _) + .expects(LockAccountRequest(address)) + .returning(Future.successful(Right(LockAccountResponse(true)))) + + val rpcRequest = newJsonRpcRequest("personal_lockAccount", params) + val response = jsonRpcController.handleRequest(rpcRequest).futureValue + + response should haveBooleanResult(true) + } + + it should "personal_sendTransaction" in new JsonRpcControllerFixture { + val params = JObject( + "from" -> Address(42).toString, + "to" -> Address(123).toString, + "value" -> 1000 + ) :: JString("passphrase") :: Nil + + val txHash = ByteString(1, 2, 3, 4) + + (personalService + .sendTransaction(_: SendTransactionWithPassphraseRequest)) + .expects(*) + .returning(Future.successful(Right(SendTransactionWithPassphraseResponse(txHash)))) + + val rpcRequest = newJsonRpcRequest("personal_sendTransaction", params) + val response = jsonRpcController.handleRequest(rpcRequest).futureValue + + response should haveResult(JString(s"0x${Hex.toHexString(txHash.toArray)}")) + } + + it should "personal_sign" in new JsonRpcControllerFixture { + + (personalService.sign _) + .expects( + SignRequest( + ByteString(Hex.decode("deadbeaf")), + Address(ByteString(Hex.decode("9b2055d370f73ec7d8a03e965129118dc8f5bf83"))), + Some("thePassphrase") + ) + ) + .returns(Future.successful(Right(SignResponse(sig)))) + + val request: JsonRpcRequest = newJsonRpcRequest( + "personal_sign", + List( + JString(s"0xdeadbeaf"), + JString(s"0x9b2055d370f73ec7d8a03e965129118dc8f5bf83"), + JString("thePassphrase") + ) + ) + + val response = jsonRpcController.handleRequest(request).futureValue + response should haveStringResult( + "0xa3f20717a250c2b0b729b7e5becbff67fdaef7e0699da4de7ca5895b02a170a12d887fd3b17bfdce3481f10bea41f45ba9f709d39ce8325427b57afcfc994cee1b" + ) + } + + it should "personal_ecRecover" in new JsonRpcControllerFixture { + + (personalService.ecRecover _) + .expects(EcRecoverRequest(ByteString(Hex.decode("deadbeaf")), sig)) + .returns( + Future.successful( + Right(EcRecoverResponse(Address(ByteString(Hex.decode("9b2055d370f73ec7d8a03e965129118dc8f5bf83"))))) + ) + ) + + val request: JsonRpcRequest = newJsonRpcRequest( + "personal_ecRecover", + List( + JString(s"0xdeadbeaf"), + JString( + s"0xa3f20717a250c2b0b729b7e5becbff67fdaef7e0699da4de7ca5895b02a170a12d887fd3b17bfdce3481f10bea41f45ba9f709d39ce8325427b57afcfc994cee1b" + ) + ) + ) + + val response = jsonRpcController.handleRequest(request).futureValue + response should haveStringResult("0x9b2055d370f73ec7d8a03e965129118dc8f5bf83") + } +} diff --git a/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerSpec.scala b/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerSpec.scala index 0470d4f3a3..1a78133298 100644 --- a/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerSpec.scala +++ b/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerSpec.scala @@ -1,22 +1,16 @@ package io.iohk.ethereum.jsonrpc -import java.time.Duration - -import akka.util.ByteString -import io.iohk.ethereum.domain._ import io.iohk.ethereum.jsonrpc.DebugService.{ListPeersInfoRequest, ListPeersInfoResponse} import io.iohk.ethereum.jsonrpc.EthService._ import io.iohk.ethereum.jsonrpc.JsonRpcController.JsonRpcConfig import io.iohk.ethereum.jsonrpc.JsonSerializers.{OptionNoneToJNullSerializer, QuantitiesSerializer, UnformattedDataJsonSerializer} import io.iohk.ethereum.jsonrpc.NetService.{ListeningResponse, PeerCountResponse, VersionResponse} -import io.iohk.ethereum.jsonrpc.PersonalService._ import io.iohk.ethereum.jsonrpc.server.http.JsonRpcHttpServer import io.iohk.ethereum.jsonrpc.server.ipc.JsonRpcIpcServer import io.iohk.ethereum.network.EtcPeerManagerActor.PeerInfo import io.iohk.ethereum.network.p2p.messages.CommonMessages.Status import io.iohk.ethereum.network.p2p.messages.Versions import io.iohk.ethereum.{Fixtures, LongPatience} -import org.bouncycastle.util.encoders.Hex import org.json4s.JsonAST._ import org.json4s.JsonDSL._ import org.json4s.{DefaultFormats, Extraction, Formats} @@ -28,8 +22,6 @@ import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks import scala.concurrent.Future import scala.concurrent.duration._ -// scalastyle:off file.size.limit -// scalastyle:off magic.number class JsonRpcControllerSpec extends AnyFlatSpec with Matchers @@ -116,152 +108,6 @@ class JsonRpcControllerSpec web3Response should haveStringResult(version) } - it should "personal_importRawKey" in new JsonRpcControllerFixture { - val key = "7a44789ed3cd85861c0bbf9693c7e1de1862dd4396c390147ecf1275099c6e6f" - val keyBytes = ByteString(Hex.decode(key)) - val addr = Address("0x00000000000000000000000000000000000000ff") - val pass = "aaa" - - (personalService.importRawKey _) - .expects(ImportRawKeyRequest(keyBytes, pass)) - .returning(Future.successful(Right(ImportRawKeyResponse(addr)))) - - val params = JString(key) :: JString(pass) :: Nil - val rpcRequest = newJsonRpcRequest("personal_importRawKey", params) - val response = jsonRpcController.handleRequest(rpcRequest).futureValue - - response should haveStringResult(addr.toString) - } - - it should "personal_newAccount" in new JsonRpcControllerFixture { - val addr = Address("0x00000000000000000000000000000000000000ff") - val pass = "aaa" - - (personalService.newAccount _) - .expects(NewAccountRequest(pass)) - .returning(Future.successful(Right(NewAccountResponse(addr)))) - - val params = JString(pass) :: Nil - val rpcRequest = newJsonRpcRequest("personal_newAccount", params) - val response = jsonRpcController.handleRequest(rpcRequest).futureValue - - response should haveStringResult(addr.toString) - } - - it should "personal_listAccounts" in new JsonRpcControllerFixture { - val addresses = List(34, 12391, 123).map(Address(_)) - val pass = "aaa" - - (personalService.listAccounts _) - .expects(ListAccountsRequest()) - .returning(Future.successful(Right(ListAccountsResponse(addresses)))) - - val rpcRequest = newJsonRpcRequest("personal_listAccounts") - val response = jsonRpcController.handleRequest(rpcRequest).futureValue - - response should haveResult(JArray(addresses.map(a => JString(a.toString)))) - } - - it should "personal_unlockAccount" in new JsonRpcControllerFixture { - val address = Address(42) - val pass = "aaa" - val params = JString(address.toString) :: JString(pass) :: Nil - - (personalService.unlockAccount _) - .expects(UnlockAccountRequest(address, pass, None)) - .returning(Future.successful(Right(UnlockAccountResponse(true)))) - - val rpcRequest = newJsonRpcRequest("personal_unlockAccount", params) - val response = jsonRpcController.handleRequest(rpcRequest).futureValue - - response should haveBooleanResult(true) - } - - it should "personal_unlockAccount for specified duration" in new JsonRpcControllerFixture { - val address = Address(42) - val pass = "aaa" - val dur = "1" - val params = JString(address.toString) :: JString(pass) :: JString(dur) :: Nil - - (personalService.unlockAccount _) - .expects(UnlockAccountRequest(address, pass, Some(Duration.ofSeconds(1)))) - .returning(Future.successful(Right(UnlockAccountResponse(true)))) - - val rpcRequest = newJsonRpcRequest("personal_unlockAccount", params) - val response = jsonRpcController.handleRequest(rpcRequest).futureValue - - response should haveBooleanResult(true) - } - - it should "personal_unlockAccount should handle possible duration errors" in new JsonRpcControllerFixture { - val address = Address(42) - val pass = "aaa" - val dur = "alksjdfh" - - val params = JString(address.toString) :: JString(pass) :: JString(dur) :: Nil - val rpcRequest = newJsonRpcRequest("personal_unlockAccount", params) - val response = jsonRpcController.handleRequest(rpcRequest).futureValue - - response should haveError(JsonRpcError(-32602, "Invalid method parameters", None)) - - val dur2 = Long.MaxValue - val params2 = JString(address.toString) :: JString(pass) :: JInt(dur2) :: Nil - val rpcRequest2 = newJsonRpcRequest("personal_unlockAccount", params2) - val response2 = jsonRpcController.handleRequest(rpcRequest2).futureValue - response2 should haveError( - JsonRpcError(-32602, "Duration should be an number of seconds, less than 2^31 - 1", None) - ) - } - - it should "personal_unlockAccount should handle null passed as a duration for compatibility with Parity and web3j" in new JsonRpcControllerFixture { - val address = Address(42) - val pass = "aaa" - val params = JString(address.toString) :: JString(pass) :: JNull :: Nil - - (personalService.unlockAccount _) - .expects(UnlockAccountRequest(address, pass, None)) - .returning(Future.successful(Right(UnlockAccountResponse(true)))) - - val rpcRequest = newJsonRpcRequest("personal_unlockAccount", params) - val response = jsonRpcController.handleRequest(rpcRequest).futureValue - - response should haveBooleanResult(true) - } - - it should "personal_lockAccount" in new JsonRpcControllerFixture { - val address = Address(42) - val params = JString(address.toString) :: Nil - - (personalService.lockAccount _) - .expects(LockAccountRequest(address)) - .returning(Future.successful(Right(LockAccountResponse(true)))) - - val rpcRequest = newJsonRpcRequest("personal_lockAccount", params) - val response = jsonRpcController.handleRequest(rpcRequest).futureValue - - response should haveBooleanResult(true) - } - - it should "personal_sendTransaction" in new JsonRpcControllerFixture { - val params = JObject( - "from" -> Address(42).toString, - "to" -> Address(123).toString, - "value" -> 1000 - ) :: JString("passphrase") :: Nil - - val txHash = ByteString(1, 2, 3, 4) - - (personalService - .sendTransaction(_: SendTransactionWithPassphraseRequest)) - .expects(*) - .returning(Future.successful(Right(SendTransactionWithPassphraseResponse(txHash)))) - - val rpcRequest = newJsonRpcRequest("personal_sendTransaction", params) - val response = jsonRpcController.handleRequest(rpcRequest).futureValue - - response should haveResult(JString(s"0x${Hex.toHexString(txHash.toArray)}")) - } - it should "debug_listPeersInfo" in new JsonRpcControllerFixture { val peerStatus = Status( protocolVersion = Versions.PV63, @@ -289,57 +135,6 @@ class JsonRpcControllerSpec response should haveResult(JArray(peers.map(info => JString(info.toString)))) } - it should "personal_sign" in new JsonRpcControllerFixture { - - (personalService.sign _) - .expects( - SignRequest( - ByteString(Hex.decode("deadbeaf")), - Address(ByteString(Hex.decode("9b2055d370f73ec7d8a03e965129118dc8f5bf83"))), - Some("thePassphrase") - ) - ) - .returns(Future.successful(Right(SignResponse(sig)))) - - val request: JsonRpcRequest = newJsonRpcRequest( - "personal_sign", - List( - JString(s"0xdeadbeaf"), - JString(s"0x9b2055d370f73ec7d8a03e965129118dc8f5bf83"), - JString("thePassphrase") - ) - ) - - val response = jsonRpcController.handleRequest(request).futureValue - response should haveStringResult( - "0xa3f20717a250c2b0b729b7e5becbff67fdaef7e0699da4de7ca5895b02a170a12d887fd3b17bfdce3481f10bea41f45ba9f709d39ce8325427b57afcfc994cee1b" - ) - } - - it should "personal_ecRecover" in new JsonRpcControllerFixture { - - (personalService.ecRecover _) - .expects(EcRecoverRequest(ByteString(Hex.decode("deadbeaf")), sig)) - .returns( - Future.successful( - Right(EcRecoverResponse(Address(ByteString(Hex.decode("9b2055d370f73ec7d8a03e965129118dc8f5bf83"))))) - ) - ) - - val request: JsonRpcRequest = newJsonRpcRequest( - "personal_ecRecover", - List( - JString(s"0xdeadbeaf"), - JString( - s"0xa3f20717a250c2b0b729b7e5becbff67fdaef7e0699da4de7ca5895b02a170a12d887fd3b17bfdce3481f10bea41f45ba9f709d39ce8325427b57afcfc994cee1b" - ) - ) - ) - - val response = jsonRpcController.handleRequest(request).futureValue - response should haveStringResult("0x9b2055d370f73ec7d8a03e965129118dc8f5bf83") - } - it should "rpc_modules" in new JsonRpcControllerFixture { val request: JsonRpcRequest = newJsonRpcRequest("rpc_modules") From dd1bac98980aef9673b75433de7798625ceafbcf Mon Sep 17 00:00:00 2001 From: lemastero Date: Tue, 13 Oct 2020 19:43:34 +0200 Subject: [PATCH 5/7] [ETCM-126] move transaction related tests from JsonRpcControllerEthSpec to JsonRpcCOntrollerEthTransactionSpec --- .../jsonrpc/JsonRpcControllerEthSpec.scala | 374 ---------------- .../JsonRpcControllerEthTransactionSpec.scala | 406 ++++++++++++++++++ 2 files changed, 406 insertions(+), 374 deletions(-) create mode 100644 src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerEthTransactionSpec.scala diff --git a/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerEthSpec.scala b/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerEthSpec.scala index 388a077bab..451165d998 100644 --- a/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerEthSpec.scala +++ b/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerEthSpec.scala @@ -25,7 +25,6 @@ import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks import scala.concurrent.Future -// scalastyle:off file.size.limit // scalastyle:off magic.number class JsonRpcControllerEthSpec extends AnyFlatSpec @@ -80,21 +79,6 @@ class JsonRpcControllerEthSpec ) } - it should "handle eth_getBlockTransactionCountByHash request" in new JsonRpcControllerFixture { - val blockToRequest = Block(Fixtures.Blocks.Block3125369.header, Fixtures.Blocks.Block3125369.body) - - blockchain.storeBlock(blockToRequest).commit() - - val rpcRequest = newJsonRpcRequest( - "eth_getBlockTransactionCountByHash", - List(JString(s"0x${blockToRequest.header.hashAsHexString}")), - ) - val response = jsonRpcController.handleRequest(rpcRequest).futureValue - - val expectedTxCount = Extraction.decompose(BigInt(blockToRequest.body.transactionList.size)) - response should haveResult(expectedTxCount) - } - it should "handle eth_getBlockByHash request" in new JsonRpcControllerFixture { val blockToRequest = Block(Fixtures.Blocks.Block3125369.header, Fixtures.Blocks.Block3125369.body) val blockTd = blockToRequest.header.difficulty @@ -187,89 +171,6 @@ class JsonRpcControllerEthSpec response should haveResult(expectedUncleBlockResponse) } - it should "handle eth_getTransactionByBlockHashAndIndex request" in new JsonRpcControllerFixture { - val blockToRequest = Block(Fixtures.Blocks.Block3125369.header, Fixtures.Blocks.Block3125369.body) - val txIndexToRequest = blockToRequest.body.transactionList.size / 2 - - blockchain.storeBlock(blockToRequest).commit() - blockchain.saveBestKnownBlocks(blockToRequest.header.number) - - val request: JsonRpcRequest = newJsonRpcRequest( - "eth_getTransactionByBlockHashAndIndex", - List( - JString(s"0x${blockToRequest.header.hashAsHexString}"), - JString(s"0x${Hex.toHexString(BigInt(txIndexToRequest).toByteArray)}") - ) - ) - val response = jsonRpcController.handleRequest(request).futureValue - val expectedStx = blockToRequest.body.transactionList.apply(txIndexToRequest) - val expectedTxResponse = Extraction.decompose( - TransactionResponse(expectedStx, Some(blockToRequest.header), Some(txIndexToRequest)) - ) - - response should haveResult(expectedTxResponse) - } - - it should "handle eth_getRawTransactionByBlockHashAndIndex request" in new JsonRpcControllerFixture { - val blockToRequest = Block(Fixtures.Blocks.Block3125369.header, Fixtures.Blocks.Block3125369.body) - val txIndexToRequest = blockToRequest.body.transactionList.size / 2 - - blockchain.storeBlock(blockToRequest).commit() - blockchain.saveBestKnownBlocks(blockToRequest.header.number) - - val request: JsonRpcRequest = newJsonRpcRequest( - "eth_getRawTransactionByBlockHashAndIndex", - List( - JString(s"0x${blockToRequest.header.hashAsHexString}"), - JString(s"0x${Hex.toHexString(BigInt(txIndexToRequest).toByteArray)}") - ) - ) - val response = jsonRpcController.handleRequest(request).futureValue - val expectedTxResponse = rawTrnHex(blockToRequest.body.transactionList, txIndexToRequest) - - response should haveResult(expectedTxResponse) - } - - it should "handle eth_getRawTransactionByHash request" in new JsonRpcControllerFixture { - val mockEthService = mock[EthService] - override val jsonRpcController = newJsonRpcController(mockEthService) - - val txResponse: SignedTransaction = Fixtures.Blocks.Block3125369.body.transactionList.head - (mockEthService.getRawTransactionByHash _) - .expects(*) - .returning(Future.successful(Right(RawTransactionResponse(Some(txResponse))))) - - val request: JsonRpcRequest = newJsonRpcRequest( - "eth_getRawTransactionByHash", - List( - JString("0xe9b2d3e8a2bc996a1c7742de825fdae2466ae783ce53484304efffe304ff232d") - ) - ) - - val response = jsonRpcController.handleRequest(request).futureValue - response should haveResult(encodeSignedTrx(txResponse)) - } - - it should "eth_sendTransaction" in new JsonRpcControllerFixture { - val params = JObject( - "from" -> Address(42).toString, - "to" -> Address(123).toString, - "value" -> 1000 - ) :: Nil - - val txHash = ByteString(1, 2, 3, 4) - - (personalService - .sendTransaction(_: SendTransactionRequest)) - .expects(*) - .returning(Future.successful(Right(SendTransactionResponse(txHash)))) - - val rpcRequest = newJsonRpcRequest("eth_sendTransaction", params) - val response = jsonRpcController.handleRequest(rpcRequest).futureValue - - response should haveResult(JString(s"0x${Hex.toHexString(txHash.toArray)}")) - } - it should "eth_getWork" in new JsonRpcControllerFixture { // Just record the fact that this is going to be called, we do not care about the returned value (validators.signedTransactionValidator _: (() => SignedTransactionValidator)) @@ -543,25 +444,6 @@ class JsonRpcControllerEthSpec response should haveStringResult("0x3") } - it should "eth_getBlockTransactionCountByNumber " in new JsonRpcControllerFixture { - val mockEthService = mock[EthService] - override val jsonRpcController = newJsonRpcController(mockEthService) - - (mockEthService.getBlockTransactionCountByNumber _) - .expects(*) - .returning(Future.successful(Right(GetBlockTransactionCountByNumberResponse(17)))) - - val request: JsonRpcRequest = newJsonRpcRequest( - "eth_getBlockTransactionCountByNumber", - List( - JString(s"0x123") - ) - ) - - val response = jsonRpcController.handleRequest(request).futureValue - response should haveStringResult("0x11") - } - it should "eth_coinbase " in new JsonRpcControllerFixture { // Just record the fact that this is going to be called, we do not care about the returned value (validators.signedTransactionValidator _: (() => SignedTransactionValidator)) @@ -577,143 +459,6 @@ class JsonRpcControllerEthSpec response should haveStringResult("0x000000000000000000000000000000000000002a") } - it should "eth_getTransactionByBlockNumberAndIndex by tag" in new JsonRpcControllerFixture { - val blockToRequest = Block(Fixtures.Blocks.Block3125369.header, Fixtures.Blocks.Block3125369.body) - val txIndex = 1 - - blockchain.storeBlock(blockToRequest).commit() - blockchain.saveBestKnownBlocks(blockToRequest.header.number) - - val request: JsonRpcRequest = newJsonRpcRequest( - "eth_getTransactionByBlockNumberAndIndex", - List( - JString(s"latest"), - JString(s"0x${Hex.toHexString(BigInt(txIndex).toByteArray)}") - ) - ) - val response = jsonRpcController.handleRequest(request).futureValue - val expectedStx = blockToRequest.body.transactionList(txIndex) - val expectedTxResponse = Extraction.decompose( - TransactionResponse(expectedStx, Some(blockToRequest.header), Some(txIndex)) - ) - - response should haveResult(expectedTxResponse) - } - - it should "eth_getTransactionByBlockNumberAndIndex by hex number" in new JsonRpcControllerFixture { - val blockToRequest = - Block(Fixtures.Blocks.Block3125369.header.copy(number = BigInt(0xc005)), Fixtures.Blocks.Block3125369.body) - val txIndex = 1 - - blockchain.storeBlock(blockToRequest).commit() - - val request: JsonRpcRequest = newJsonRpcRequest( - "eth_getTransactionByBlockNumberAndIndex", - List( - JString(s"0xC005"), - JString(s"0x${Hex.toHexString(BigInt(txIndex).toByteArray)}") - ) - ) - val response = jsonRpcController.handleRequest(request).futureValue - val expectedStx = blockToRequest.body.transactionList(txIndex) - val expectedTxResponse = Extraction.decompose( - TransactionResponse(expectedStx, Some(blockToRequest.header), Some(txIndex)) - ) - - response should haveResult(expectedTxResponse) - } - - it should "eth_getTransactionByBlockNumberAndIndex by number" in new JsonRpcControllerFixture { - val blockToRequest = Block(Fixtures.Blocks.Block3125369.header, Fixtures.Blocks.Block3125369.body) - val txIndex = 1 - - blockchain.storeBlock(blockToRequest).commit() - - val request: JsonRpcRequest = newJsonRpcRequest( - "eth_getTransactionByBlockNumberAndIndex", - List( - JInt(Fixtures.Blocks.Block3125369.header.number), - JString(s"0x${Hex.toHexString(BigInt(txIndex).toByteArray)}") - ) - ) - val response = jsonRpcController.handleRequest(request).futureValue - val expectedStx = blockToRequest.body.transactionList(txIndex) - val expectedTxResponse = Extraction.decompose( - TransactionResponse(expectedStx, Some(blockToRequest.header), Some(txIndex)) - ) - - response should haveResult(expectedTxResponse) - } - - it should "eth_getRawTransactionByBlockNumberAndIndex by tag" in new JsonRpcControllerFixture { - // given - val blockToRequest: Block = Block(Fixtures.Blocks.Block3125369.header, Fixtures.Blocks.Block3125369.body) - val txIndex = 1 - - blockchain.storeBlock(blockToRequest).commit() - blockchain.saveBestKnownBlocks(blockToRequest.header.number) - - val request: JsonRpcRequest = newJsonRpcRequest( - "eth_getRawTransactionByBlockNumberAndIndex", - List( - JString(s"latest"), - JString(s"0x${Hex.toHexString(BigInt(txIndex).toByteArray)}") - ) - ) - - // when - val response = jsonRpcController.handleRequest(request).futureValue - - // then - val expectedTxResponse = rawTrnHex(blockToRequest.body.transactionList, txIndex) - - response should haveResult(expectedTxResponse) - } - - it should "eth_getRawTransactionByBlockNumberAndIndex by hex number" in new JsonRpcControllerFixture { - // given - val blockToRequest = - Block(Fixtures.Blocks.Block3125369.header.copy(number = BigInt(0xc005)), Fixtures.Blocks.Block3125369.body) - val txIndex = 1 - - blockchain.storeBlock(blockToRequest).commit() - - val request: JsonRpcRequest = newJsonRpcRequest( - "eth_getRawTransactionByBlockNumberAndIndex", - List( - JString(s"0xC005"), - JString(s"0x${Hex.toHexString(BigInt(txIndex).toByteArray)}") - ) - ) - - // when - val response = jsonRpcController.handleRequest(request).futureValue - - // then - val expectedTxResponse = rawTrnHex(blockToRequest.body.transactionList, txIndex) - - response should haveResult(expectedTxResponse) - } - - it should "eth_getRawTransactionByBlockNumberAndIndex by number" in new JsonRpcControllerFixture { - val blockToRequest = Block(Fixtures.Blocks.Block3125369.header, Fixtures.Blocks.Block3125369.body) - val txIndex = 1 - - blockchain.storeBlock(blockToRequest).commit() - - val request: JsonRpcRequest = newJsonRpcRequest( - "eth_getRawTransactionByBlockNumberAndIndex", - List( - JInt(Fixtures.Blocks.Block3125369.header.number), - JString(s"0x${Hex.toHexString(BigInt(txIndex).toByteArray)}") - ) - ) - val response = jsonRpcController.handleRequest(request).futureValue - val expectedTxResponse = rawTrnHex(blockToRequest.body.transactionList, txIndex) - - response should haveResult(expectedTxResponse) - } - it should "eth_getBalance" in new JsonRpcControllerFixture { val mockEthService = mock[EthService] override val jsonRpcController = newJsonRpcController(mockEthService) @@ -755,46 +500,6 @@ class JsonRpcControllerEthSpec response should haveResult(JString("0x" + Hex.toHexString(ByteString("response").toArray[Byte]))) } - it should "eth_getTransactionCount" in new JsonRpcControllerFixture { - val mockEthService = mock[EthService] - override val jsonRpcController = newJsonRpcController(mockEthService) - - (mockEthService.getTransactionCount _) - .expects(*) - .returning(Future.successful(Right(GetTransactionCountResponse(123)))) - - val request: JsonRpcRequest = newJsonRpcRequest( - "eth_getTransactionCount", - List( - JString(s"0x7B9Bc474667Db2fFE5b08d000F1Acc285B2Ae47D"), - JString(s"latest") - ) - ) - - val response = jsonRpcController.handleRequest(request).futureValue - response should haveStringResult("0x7b") - } - - it should "eth_getTransactionByHash" in new JsonRpcControllerFixture { - val mockEthService = mock[EthService] - override val jsonRpcController = newJsonRpcController(mockEthService) - - val txResponse = TransactionResponse(Fixtures.Blocks.Block3125369.body.transactionList.head) - (mockEthService.getTransactionByHash _) - .expects(*) - .returning(Future.successful(Right(GetTransactionByHashResponse(Some(txResponse))))) - - val request: JsonRpcRequest = newJsonRpcRequest( - "eth_getTransactionByHash", - List( - JString("0xe9b2d3e8a2bc996a1c7742de825fdae2466ae783ce53484304efffe304ff232d") - ) - ) - - val response = jsonRpcController.handleRequest(request).futureValue - response should haveResult(Extraction.decompose(txResponse)) - } - it should "eth_sign" in new JsonRpcControllerFixture { (personalService.sign _) @@ -1037,83 +742,4 @@ class JsonRpcControllerEthSpec ) ) } - - it should "eth_getTransactionReceipt" in new JsonRpcControllerFixture { - val mockEthService = mock[EthService] - override val jsonRpcController = newJsonRpcController(mockEthService) - - val arbitraryValue = 42 - - val mockRequest = GetTransactionReceiptRequest( - ByteString(Hex.decode("b903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238")) - ) - - val mockResponse = Right( - GetTransactionReceiptResponse( - Some( - TransactionReceiptResponse( - transactionHash = ByteString(Hex.decode("23" * 32)), - transactionIndex = 1, - blockNumber = Fixtures.Blocks.Block3125369.header.number, - blockHash = Fixtures.Blocks.Block3125369.header.hash, - cumulativeGasUsed = arbitraryValue * 10, - gasUsed = arbitraryValue, - contractAddress = Some(Address(arbitraryValue)), - logs = Seq( - TxLog( - logIndex = 0, - transactionIndex = 1, - transactionHash = ByteString(Hex.decode("23" * 32)), - blockHash = Fixtures.Blocks.Block3125369.header.hash, - blockNumber = Fixtures.Blocks.Block3125369.header.number, - address = Address(arbitraryValue), - data = ByteString(Hex.decode("43" * 32)), - topics = Seq(ByteString(Hex.decode("44" * 32)), ByteString(Hex.decode("45" * 32))) - ) - ) - ) - ) - ) - ) - - (mockEthService.getTransactionReceipt _).expects(*).returning(Future.successful(mockResponse)) - - val request: JsonRpcRequest = newJsonRpcRequest( - "eth_getTransactionReceipt", - List(JString(s"0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238")) - ) - - val response = jsonRpcController.handleRequest(request).futureValue - response should haveResult( - JObject( - JField("transactionHash", JString("0x" + "23" * 32)), - JField("transactionIndex", JString("0x1")), - JField("blockNumber", JString("0x2fb079")), - JField("blockHash", JString("0x" + Hex.toHexString(Fixtures.Blocks.Block3125369.header.hash.toArray[Byte]))), - JField("cumulativeGasUsed", JString("0x1a4")), - JField("gasUsed", JString("0x2a")), - JField("contractAddress", JString("0x000000000000000000000000000000000000002a")), - JField( - "logs", - JArray( - List( - JObject( - JField("logIndex", JString("0x0")), - JField("transactionIndex", JString("0x1")), - JField("transactionHash", JString("0x" + "23" * 32)), - JField( - "blockHash", - JString("0x" + Hex.toHexString(Fixtures.Blocks.Block3125369.header.hash.toArray[Byte])) - ), - JField("blockNumber", JString("0x2fb079")), - JField("address", JString("0x000000000000000000000000000000000000002a")), - JField("data", JString("0x" + "43" * 32)), - JField("topics", JArray(List(JString("0x" + "44" * 32), JString("0x" + "45" * 32)))) - ) - ) - ) - ) - ) - ) - } } diff --git a/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerEthTransactionSpec.scala b/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerEthTransactionSpec.scala new file mode 100644 index 0000000000..761ee85eb6 --- /dev/null +++ b/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerEthTransactionSpec.scala @@ -0,0 +1,406 @@ +package io.iohk.ethereum.jsonrpc + +import akka.util.ByteString +import io.iohk.ethereum.domain._ +import io.iohk.ethereum.jsonrpc.EthService._ +import io.iohk.ethereum.jsonrpc.FilterManager.TxLog +import io.iohk.ethereum.jsonrpc.JsonSerializers.{OptionNoneToJNullSerializer, QuantitiesSerializer, UnformattedDataJsonSerializer} +import io.iohk.ethereum.jsonrpc.PersonalService._ +import io.iohk.ethereum.{Fixtures, LongPatience} +import org.bouncycastle.util.encoders.Hex +import org.json4s.JsonAST._ +import org.json4s.JsonDSL._ +import org.json4s.{DefaultFormats, Extraction, Formats} +import org.scalatest.concurrent.{Eventually, ScalaFutures} +import org.scalatest.flatspec.AnyFlatSpec +import org.scalatest.matchers.should.Matchers +import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks + +import scala.concurrent.Future + +// scalastyle:off magic.number +class JsonRpcControllerEthTransactionSpec + extends AnyFlatSpec + with Matchers + with JRCMatchers + with ScalaCheckPropertyChecks + with ScalaFutures + with LongPatience + with Eventually { + + implicit val formats: Formats = DefaultFormats.preservingEmptyValues + OptionNoneToJNullSerializer + + QuantitiesSerializer + UnformattedDataJsonSerializer + + it should "handle eth_getTransactionByBlockHashAndIndex request" in new JsonRpcControllerFixture { + val blockToRequest = Block(Fixtures.Blocks.Block3125369.header, Fixtures.Blocks.Block3125369.body) + val txIndexToRequest = blockToRequest.body.transactionList.size / 2 + + blockchain.storeBlock(blockToRequest).commit() + blockchain.saveBestKnownBlocks(blockToRequest.header.number) + + val request: JsonRpcRequest = newJsonRpcRequest( + "eth_getTransactionByBlockHashAndIndex", + List( + JString(s"0x${blockToRequest.header.hashAsHexString}"), + JString(s"0x${Hex.toHexString(BigInt(txIndexToRequest).toByteArray)}") + ) + ) + val response = jsonRpcController.handleRequest(request).futureValue + val expectedStx = blockToRequest.body.transactionList.apply(txIndexToRequest) + val expectedTxResponse = Extraction.decompose( + TransactionResponse(expectedStx, Some(blockToRequest.header), Some(txIndexToRequest)) + ) + + response should haveResult(expectedTxResponse) + } + + it should "handle eth_getRawTransactionByBlockHashAndIndex request" in new JsonRpcControllerFixture { + val blockToRequest = Block(Fixtures.Blocks.Block3125369.header, Fixtures.Blocks.Block3125369.body) + val txIndexToRequest = blockToRequest.body.transactionList.size / 2 + + blockchain.storeBlock(blockToRequest).commit() + blockchain.saveBestKnownBlocks(blockToRequest.header.number) + + val request: JsonRpcRequest = newJsonRpcRequest( + "eth_getRawTransactionByBlockHashAndIndex", + List( + JString(s"0x${blockToRequest.header.hashAsHexString}"), + JString(s"0x${Hex.toHexString(BigInt(txIndexToRequest).toByteArray)}") + ) + ) + val response = jsonRpcController.handleRequest(request).futureValue + val expectedTxResponse = rawTrnHex(blockToRequest.body.transactionList, txIndexToRequest) + + response should haveResult(expectedTxResponse) + } + + it should "handle eth_getRawTransactionByHash request" in new JsonRpcControllerFixture { + val mockEthService = mock[EthService] + override val jsonRpcController = newJsonRpcController(mockEthService) + + val txResponse: SignedTransaction = Fixtures.Blocks.Block3125369.body.transactionList.head + (mockEthService.getRawTransactionByHash _) + .expects(*) + .returning(Future.successful(Right(RawTransactionResponse(Some(txResponse))))) + + val request: JsonRpcRequest = newJsonRpcRequest( + "eth_getRawTransactionByHash", + List( + JString("0xe9b2d3e8a2bc996a1c7742de825fdae2466ae783ce53484304efffe304ff232d") + ) + ) + + val response = jsonRpcController.handleRequest(request).futureValue + response should haveResult(encodeSignedTrx(txResponse)) + } + + it should "eth_sendTransaction" in new JsonRpcControllerFixture { + val params = JObject( + "from" -> Address(42).toString, + "to" -> Address(123).toString, + "value" -> 1000 + ) :: Nil + + val txHash = ByteString(1, 2, 3, 4) + + (personalService + .sendTransaction(_: SendTransactionRequest)) + .expects(*) + .returning(Future.successful(Right(SendTransactionResponse(txHash)))) + + val rpcRequest = newJsonRpcRequest("eth_sendTransaction", params) + val response = jsonRpcController.handleRequest(rpcRequest).futureValue + + response should haveResult(JString(s"0x${Hex.toHexString(txHash.toArray)}")) + } + + it should "eth_getTransactionByBlockNumberAndIndex by tag" in new JsonRpcControllerFixture { + val blockToRequest = Block(Fixtures.Blocks.Block3125369.header, Fixtures.Blocks.Block3125369.body) + val txIndex = 1 + + blockchain.storeBlock(blockToRequest).commit() + blockchain.saveBestKnownBlocks(blockToRequest.header.number) + + val request: JsonRpcRequest = newJsonRpcRequest( + "eth_getTransactionByBlockNumberAndIndex", + List( + JString(s"latest"), + JString(s"0x${Hex.toHexString(BigInt(txIndex).toByteArray)}") + ) + ) + val response = jsonRpcController.handleRequest(request).futureValue + val expectedStx = blockToRequest.body.transactionList(txIndex) + val expectedTxResponse = Extraction.decompose( + TransactionResponse(expectedStx, Some(blockToRequest.header), Some(txIndex)) + ) + + response should haveResult(expectedTxResponse) + } + + it should "eth_getTransactionByBlockNumberAndIndex by hex number" in new JsonRpcControllerFixture { + val blockToRequest = + Block(Fixtures.Blocks.Block3125369.header.copy(number = BigInt(0xc005)), Fixtures.Blocks.Block3125369.body) + val txIndex = 1 + + blockchain.storeBlock(blockToRequest).commit() + + val request: JsonRpcRequest = newJsonRpcRequest( + "eth_getTransactionByBlockNumberAndIndex", + List( + JString(s"0xC005"), + JString(s"0x${Hex.toHexString(BigInt(txIndex).toByteArray)}") + ) + ) + val response = jsonRpcController.handleRequest(request).futureValue + val expectedStx = blockToRequest.body.transactionList(txIndex) + val expectedTxResponse = Extraction.decompose( + TransactionResponse(expectedStx, Some(blockToRequest.header), Some(txIndex)) + ) + + response should haveResult(expectedTxResponse) + } + + it should "eth_getTransactionByBlockNumberAndIndex by number" in new JsonRpcControllerFixture { + val blockToRequest = Block(Fixtures.Blocks.Block3125369.header, Fixtures.Blocks.Block3125369.body) + val txIndex = 1 + + blockchain.storeBlock(blockToRequest).commit() + + val request: JsonRpcRequest = newJsonRpcRequest( + "eth_getTransactionByBlockNumberAndIndex", + List( + JInt(Fixtures.Blocks.Block3125369.header.number), + JString(s"0x${Hex.toHexString(BigInt(txIndex).toByteArray)}") + ) + ) + val response = jsonRpcController.handleRequest(request).futureValue + val expectedStx = blockToRequest.body.transactionList(txIndex) + val expectedTxResponse = Extraction.decompose( + TransactionResponse(expectedStx, Some(blockToRequest.header), Some(txIndex)) + ) + + response should haveResult(expectedTxResponse) + } + + it should "eth_getRawTransactionByBlockNumberAndIndex by tag" in new JsonRpcControllerFixture { + // given + val blockToRequest: Block = Block(Fixtures.Blocks.Block3125369.header, Fixtures.Blocks.Block3125369.body) + val txIndex = 1 + + blockchain.storeBlock(blockToRequest).commit() + blockchain.saveBestKnownBlocks(blockToRequest.header.number) + + val request: JsonRpcRequest = newJsonRpcRequest( + "eth_getRawTransactionByBlockNumberAndIndex", + List( + JString(s"latest"), + JString(s"0x${Hex.toHexString(BigInt(txIndex).toByteArray)}") + ) + ) + + // when + val response = jsonRpcController.handleRequest(request).futureValue + + // then + val expectedTxResponse = rawTrnHex(blockToRequest.body.transactionList, txIndex) + + response should haveResult(expectedTxResponse) + } + + it should "eth_getRawTransactionByBlockNumberAndIndex by hex number" in new JsonRpcControllerFixture { + // given + val blockToRequest = + Block(Fixtures.Blocks.Block3125369.header.copy(number = BigInt(0xc005)), Fixtures.Blocks.Block3125369.body) + val txIndex = 1 + + blockchain.storeBlock(blockToRequest).commit() + + val request: JsonRpcRequest = newJsonRpcRequest( + "eth_getRawTransactionByBlockNumberAndIndex", + List( + JString(s"0xC005"), + JString(s"0x${Hex.toHexString(BigInt(txIndex).toByteArray)}") + ) + ) + + // when + val response = jsonRpcController.handleRequest(request).futureValue + + // then + val expectedTxResponse = rawTrnHex(blockToRequest.body.transactionList, txIndex) + + response should haveResult(expectedTxResponse) + } + + it should "eth_getRawTransactionByBlockNumberAndIndex by number" in new JsonRpcControllerFixture { + val blockToRequest = Block(Fixtures.Blocks.Block3125369.header, Fixtures.Blocks.Block3125369.body) + val txIndex = 1 + + blockchain.storeBlock(blockToRequest).commit() + + val request: JsonRpcRequest = newJsonRpcRequest( + "eth_getRawTransactionByBlockNumberAndIndex", + List( + JInt(Fixtures.Blocks.Block3125369.header.number), + JString(s"0x${Hex.toHexString(BigInt(txIndex).toByteArray)}") + ) + ) + val response = jsonRpcController.handleRequest(request).futureValue + val expectedTxResponse = rawTrnHex(blockToRequest.body.transactionList, txIndex) + + response should haveResult(expectedTxResponse) + } + + it should "eth_getTransactionByHash" in new JsonRpcControllerFixture { + val mockEthService = mock[EthService] + override val jsonRpcController = newJsonRpcController(mockEthService) + + val txResponse = TransactionResponse(Fixtures.Blocks.Block3125369.body.transactionList.head) + (mockEthService.getTransactionByHash _) + .expects(*) + .returning(Future.successful(Right(GetTransactionByHashResponse(Some(txResponse))))) + + val request: JsonRpcRequest = newJsonRpcRequest( + "eth_getTransactionByHash", + List( + JString("0xe9b2d3e8a2bc996a1c7742de825fdae2466ae783ce53484304efffe304ff232d") + ) + ) + + val response = jsonRpcController.handleRequest(request).futureValue + response should haveResult(Extraction.decompose(txResponse)) + } + + it should "eth_getTransactionCount" in new JsonRpcControllerFixture { + val mockEthService = mock[EthService] + override val jsonRpcController = newJsonRpcController(mockEthService) + + (mockEthService.getTransactionCount _) + .expects(*) + .returning(Future.successful(Right(GetTransactionCountResponse(123)))) + + val request: JsonRpcRequest = newJsonRpcRequest( + "eth_getTransactionCount", + List( + JString(s"0x7B9Bc474667Db2fFE5b08d000F1Acc285B2Ae47D"), + JString(s"latest") + ) + ) + + val response = jsonRpcController.handleRequest(request).futureValue + response should haveStringResult("0x7b") + } + + it should "eth_getBlockTransactionCountByNumber " in new JsonRpcControllerFixture { + val mockEthService = mock[EthService] + override val jsonRpcController = newJsonRpcController(mockEthService) + + (mockEthService.getBlockTransactionCountByNumber _) + .expects(*) + .returning(Future.successful(Right(GetBlockTransactionCountByNumberResponse(17)))) + + val request: JsonRpcRequest = newJsonRpcRequest( + "eth_getBlockTransactionCountByNumber", + List( + JString(s"0x123") + ) + ) + + val response = jsonRpcController.handleRequest(request).futureValue + response should haveStringResult("0x11") + } + + it should "handle eth_getBlockTransactionCountByHash request" in new JsonRpcControllerFixture { + val blockToRequest = Block(Fixtures.Blocks.Block3125369.header, Fixtures.Blocks.Block3125369.body) + + blockchain.storeBlock(blockToRequest).commit() + + val rpcRequest = newJsonRpcRequest( + "eth_getBlockTransactionCountByHash", + List(JString(s"0x${blockToRequest.header.hashAsHexString}")), + ) + val response = jsonRpcController.handleRequest(rpcRequest).futureValue + + val expectedTxCount = Extraction.decompose(BigInt(blockToRequest.body.transactionList.size)) + response should haveResult(expectedTxCount) + } + + it should "eth_getTransactionReceipt" in new JsonRpcControllerFixture { + val mockEthService = mock[EthService] + override val jsonRpcController = newJsonRpcController(mockEthService) + + val arbitraryValue = 42 + + val mockRequest = GetTransactionReceiptRequest( + ByteString(Hex.decode("b903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238")) + ) + + val mockResponse = Right( + GetTransactionReceiptResponse( + Some( + TransactionReceiptResponse( + transactionHash = ByteString(Hex.decode("23" * 32)), + transactionIndex = 1, + blockNumber = Fixtures.Blocks.Block3125369.header.number, + blockHash = Fixtures.Blocks.Block3125369.header.hash, + cumulativeGasUsed = arbitraryValue * 10, + gasUsed = arbitraryValue, + contractAddress = Some(Address(arbitraryValue)), + logs = Seq( + TxLog( + logIndex = 0, + transactionIndex = 1, + transactionHash = ByteString(Hex.decode("23" * 32)), + blockHash = Fixtures.Blocks.Block3125369.header.hash, + blockNumber = Fixtures.Blocks.Block3125369.header.number, + address = Address(arbitraryValue), + data = ByteString(Hex.decode("43" * 32)), + topics = Seq(ByteString(Hex.decode("44" * 32)), ByteString(Hex.decode("45" * 32))) + ) + ) + ) + ) + ) + ) + + (mockEthService.getTransactionReceipt _).expects(*).returning(Future.successful(mockResponse)) + + val request: JsonRpcRequest = newJsonRpcRequest( + "eth_getTransactionReceipt", + List(JString(s"0xb903239f8543d04b5dc1ba6579132b143087c68db1b2168786408fcbce568238")) + ) + + val response = jsonRpcController.handleRequest(request).futureValue + response should haveResult( + JObject( + JField("transactionHash", JString("0x" + "23" * 32)), + JField("transactionIndex", JString("0x1")), + JField("blockNumber", JString("0x2fb079")), + JField("blockHash", JString("0x" + Hex.toHexString(Fixtures.Blocks.Block3125369.header.hash.toArray[Byte]))), + JField("cumulativeGasUsed", JString("0x1a4")), + JField("gasUsed", JString("0x2a")), + JField("contractAddress", JString("0x000000000000000000000000000000000000002a")), + JField( + "logs", + JArray( + List( + JObject( + JField("logIndex", JString("0x0")), + JField("transactionIndex", JString("0x1")), + JField("transactionHash", JString("0x" + "23" * 32)), + JField( + "blockHash", + JString("0x" + Hex.toHexString(Fixtures.Blocks.Block3125369.header.hash.toArray[Byte])) + ), + JField("blockNumber", JString("0x2fb079")), + JField("address", JString("0x000000000000000000000000000000000000002a")), + JField("data", JString("0x" + "43" * 32)), + JField("topics", JArray(List(JString("0x" + "44" * 32), JString("0x" + "45" * 32)))) + ) + ) + ) + ) + ) + ) + } +} From 68270fd1e860a2f1b4ee365a6872d7d4604a91d2 Mon Sep 17 00:00:00 2001 From: lemastero Date: Tue, 13 Oct 2020 20:03:21 +0200 Subject: [PATCH 6/7] [ETCM-126] fix broken build - unused import --- .../io/iohk/ethereum/jsonrpc/JsonRpcControllerEthSpec.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerEthSpec.scala b/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerEthSpec.scala index 451165d998..bfb803c28a 100644 --- a/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerEthSpec.scala +++ b/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerEthSpec.scala @@ -7,7 +7,7 @@ import io.iohk.ethereum.consensus.validators.SignedTransactionValidator import io.iohk.ethereum.crypto.kec256 import io.iohk.ethereum.domain._ import io.iohk.ethereum.jsonrpc.EthService._ -import io.iohk.ethereum.jsonrpc.FilterManager.{LogFilterLogs, TxLog} +import io.iohk.ethereum.jsonrpc.FilterManager.LogFilterLogs import io.iohk.ethereum.jsonrpc.JsonSerializers.{OptionNoneToJNullSerializer, QuantitiesSerializer, UnformattedDataJsonSerializer} import io.iohk.ethereum.jsonrpc.PersonalService._ import io.iohk.ethereum.ommers.OmmersPool From d814abcd163a06da329a25035a7b77f4303609f7 Mon Sep 17 00:00:00 2001 From: lemastero Date: Tue, 13 Oct 2020 20:27:36 +0200 Subject: [PATCH 7/7] [ETCM-126] fix CI failed build - redundant , --- .../io/iohk/ethereum/jsonrpc/JsonRpcControllerEthSpec.scala | 2 +- .../ethereum/jsonrpc/JsonRpcControllerEthTransactionSpec.scala | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerEthSpec.scala b/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerEthSpec.scala index bfb803c28a..f91e375c3c 100644 --- a/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerEthSpec.scala +++ b/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerEthSpec.scala @@ -90,7 +90,7 @@ class JsonRpcControllerEthSpec val request = newJsonRpcRequest( "eth_getBlockByHash", - List(JString(s"0x${blockToRequest.header.hashAsHexString}"), JBool(false)), + List(JString(s"0x${blockToRequest.header.hashAsHexString}"), JBool(false)) ) val response = jsonRpcController.handleRequest(request).futureValue diff --git a/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerEthTransactionSpec.scala b/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerEthTransactionSpec.scala index 761ee85eb6..7a00df37c4 100644 --- a/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerEthTransactionSpec.scala +++ b/src/test/scala/io/iohk/ethereum/jsonrpc/JsonRpcControllerEthTransactionSpec.scala @@ -317,7 +317,7 @@ class JsonRpcControllerEthTransactionSpec val rpcRequest = newJsonRpcRequest( "eth_getBlockTransactionCountByHash", - List(JString(s"0x${blockToRequest.header.hashAsHexString}")), + List(JString(s"0x${blockToRequest.header.hashAsHexString}")) ) val response = jsonRpcController.handleRequest(rpcRequest).futureValue