Skip to content

Commit f9a434d

Browse files
leo-bogastryLeonor BogaJaap van der Plas
authored
[ETCM-518] refactor EthService into smaller, coherent classes
Co-authored-by: Leonor Boga <[email protected]> Co-authored-by: Jaap van der Plas <[email protected]>
1 parent 99491c7 commit f9a434d

File tree

50 files changed

+3863
-3202
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+3863
-3202
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ project/plugins/project/
66
.ensime_cache/
77
.bloop
88
out/
9+
.bsp/
910

1011
# IDE folders
1112
.idea/

bytes/src/main/scala/io/iohk/ethereum/utils/ByteStringUtils.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ object ByteStringUtils {
1313
def string2hash(hash: String): ByteString =
1414
ByteString(Hex.decode(hash))
1515

16-
1716
implicit class Padding(val bs: ByteString) extends AnyVal {
1817
def padToByteString(length: Int, b: Byte): ByteString = {
19-
if (length <= bs.length) bs else {
18+
if (length <= bs.length) bs
19+
else {
2020
val len = Math.max(bs.length, length)
2121
val result = new Array[Byte](len)
2222
bs.copyToArray(result, 0)
@@ -30,7 +30,6 @@ object ByteStringUtils {
3030
}
3131
}
3232

33-
3433
sealed trait ByteStringElement {
3534
def len: Int
3635
def asByteArray: Array[Byte]

bytes/src/main/scala/io/iohk/ethereum/utils/Hex.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ object Hex {
55
bytes.map("%02x".format(_)).mkString
66

77
def decode(hex: String): Array[Byte] =
8-
hex.toSeq.sliding(2, 2).toArray.map(s=> Integer.parseInt(s.unwrap, 16).toByte)
8+
hex.toSeq.sliding(2, 2).toArray.map(s => Integer.parseInt(s.unwrap, 16).toByte)
99
}

bytes/src/test/scala/io/iohk/ethereum/utils/ByteStringUtilsTest.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,13 @@ class ByteStringUtilsTest extends AnyWordSpec with Matchers {
4848
val bsu = string2hash("0000FFFF")
4949
val seq = ArraySeq.unsafeWrapArray(bsu.toArray)
5050
bsu.padToByteString(3, 0) shouldEqual bsu // result is ByteString
51-
bsu.padTo(3,0) shouldEqual seq // result is Seq
51+
bsu.padTo(3, 0) shouldEqual seq // result is Seq
5252

53-
val longSeq = ArraySeq[Byte](0, 0, -1, -1, 1 ,1)
53+
val longSeq = ArraySeq[Byte](0, 0, -1, -1, 1, 1)
5454
val longBsu = string2hash("0000FFFF0101")
5555
bsu.padToByteString(6, 1) shouldEqual longBsu
5656
bsu.padTo(6, 1) shouldEqual longSeq
5757
}
5858

5959
}
6060
}
61-

crypto/src/main/scala/io/iohk/ethereum/crypto/ECDSASignature.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ object ECDSASignature {
6464
case Some(id) if v == negativePointSign => (id * 2 + newNegativePointSign).toByte
6565
case Some(id) if v == positivePointSign => (id * 2 + newPositivePointSign).toByte
6666
case None => v
67-
case _ => throw new IllegalStateException(s"Unexpected pointSign. ChainId: ${chainId}, v: ${v}")
67+
case _ => throw new IllegalStateException(s"Unexpected pointSign. ChainId: ${chainId}, v: ${v}")
6868
}
6969

7070
ECDSASignature(r, s, pointSign)
@@ -177,7 +177,6 @@ case class ECDSASignature(r: BigInt, s: BigInt, v: Byte) {
177177
def bigInt2Bytes(b: BigInt) =
178178
ByteUtils.padLeft(ByteString(b.toByteArray).takeRight(RLength), RLength, 0)
179179

180-
181180
bigInt2Bytes(r) ++ bigInt2Bytes(s) ++ ByteString(v)
182181
}
183182
}

src/main/scala/io/iohk/ethereum/consensus/ethash/EthashBlockCreator.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import io.iohk.ethereum.jsonrpc.AkkaTaskOps.TaskActorOps
99
import io.iohk.ethereum.ledger.InMemoryWorldStateProxy
1010
import io.iohk.ethereum.ommers.OmmersPool
1111
import io.iohk.ethereum.transactions.PendingTransactionsManager.PendingTransactionsResponse
12+
import io.iohk.ethereum.transactions.TransactionPicker
1213
import monix.eval.Task
1314
import scala.concurrent.duration.FiniteDuration
1415

src/main/scala/io/iohk/ethereum/consensus/ethash/EthashMiner.scala

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ import io.iohk.ethereum.consensus.ethash.EthashUtils.ProofOfWork
1010
import io.iohk.ethereum.consensus.ethash.MinerProtocol.{StartMining, StopMining}
1111
import io.iohk.ethereum.crypto
1212
import io.iohk.ethereum.domain.{BlockHeader, Blockchain}
13-
import io.iohk.ethereum.jsonrpc.EthService
14-
import io.iohk.ethereum.jsonrpc.EthService.SubmitHashRateRequest
13+
import io.iohk.ethereum.jsonrpc.{EthInfoService, EthMiningService}
14+
import io.iohk.ethereum.jsonrpc.EthMiningService.SubmitHashRateRequest
1515
import io.iohk.ethereum.nodebuilder.Node
1616
import io.iohk.ethereum.utils.BigIntExtensionMethods._
1717
import io.iohk.ethereum.utils.{ByteStringUtils, ByteUtils}
1818
import monix.execution.Scheduler
1919
import org.bouncycastle.util.encoders.Hex
20+
2021
import scala.concurrent.duration._
2122
import scala.util.{Failure, Random, Success, Try}
2223

@@ -28,7 +29,7 @@ class EthashMiner(
2829
blockchain: Blockchain,
2930
blockCreator: EthashBlockCreator,
3031
syncController: ActorRef,
31-
ethService: EthService
32+
ethMiningService: EthMiningService
3233
) extends Actor
3334
with ActorLogging {
3435

@@ -70,7 +71,7 @@ class EthashMiner(
7071
val time = System.nanoTime() - startTime
7172
//FIXME: consider not reporting hash rate when time delta is zero
7273
val hashRate = if (time > 0) (mineResult.triedHashes.toLong * 1000000000) / time else Long.MaxValue
73-
ethService.submitHashRate(SubmitHashRateRequest(hashRate, ByteString("mantis-miner")))
74+
ethMiningService.submitHashRate(SubmitHashRateRequest(hashRate, ByteString("mantis-miner")))
7475
mineResult match {
7576
case MiningSuccessful(_, pow, nonce) =>
7677
log.info(
@@ -199,10 +200,11 @@ object EthashMiner {
199200
blockchain: Blockchain,
200201
blockCreator: EthashBlockCreator,
201202
syncController: ActorRef,
202-
ethService: EthService
203+
ethInfoService: EthInfoService,
204+
ethMiningService: EthMiningService
203205
): Props =
204206
Props(
205-
new EthashMiner(blockchain, blockCreator, syncController, ethService)
207+
new EthashMiner(blockchain, blockCreator, syncController, ethMiningService)
206208
).withDispatcher(BlockForgerDispatcherId)
207209

208210
def apply(node: Node): ActorRef = {
@@ -218,7 +220,8 @@ object EthashMiner {
218220
blockchain = node.blockchain,
219221
blockCreator = blockCreator,
220222
syncController = node.syncController,
221-
ethService = node.ethService
223+
ethInfoService = node.ethInfoService,
224+
ethMiningService = node.ethMiningService
222225
)
223226
node.system.actorOf(minerProps)
224227
case consensus =>
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package io.iohk.ethereum.jsonrpc
2+
3+
import io.iohk.ethereum.jsonrpc.EthBlocksService._
4+
import io.iohk.ethereum.jsonrpc.JsonRpcError.InvalidParams
5+
import io.iohk.ethereum.jsonrpc.serialization.{JsonEncoder, JsonMethodDecoder}
6+
import io.iohk.ethereum.jsonrpc.serialization.JsonMethodDecoder.NoParamsMethodDecoder
7+
import org.json4s.Extraction
8+
import org.json4s.JsonAST.{JArray, JBool, JField, JString, JValue}
9+
10+
object EthBlocksJsonMethodsImplicits extends JsonMethodsImplicits {
11+
implicit val eth_blockNumber = new NoParamsMethodDecoder(BestBlockNumberRequest())
12+
with JsonEncoder[BestBlockNumberResponse] {
13+
override def encodeJson(t: BestBlockNumberResponse): JValue = Extraction.decompose(t.bestBlockNumber)
14+
}
15+
16+
implicit val eth_getBlockTransactionCountByHash = new JsonMethodDecoder[TxCountByBlockHashRequest]
17+
with JsonEncoder[TxCountByBlockHashResponse] {
18+
override def decodeJson(params: Option[JArray]): Either[JsonRpcError, TxCountByBlockHashRequest] =
19+
params match {
20+
case Some(JArray(JString(input) :: Nil)) =>
21+
extractHash(input).map(TxCountByBlockHashRequest)
22+
case _ => Left(InvalidParams())
23+
}
24+
25+
override def encodeJson(t: TxCountByBlockHashResponse): JValue =
26+
Extraction.decompose(t.txsQuantity.map(BigInt(_)))
27+
}
28+
29+
implicit val eth_getBlockByHash = new JsonMethodDecoder[BlockByBlockHashRequest]
30+
with JsonEncoder[BlockByBlockHashResponse] {
31+
override def decodeJson(params: Option[JArray]): Either[JsonRpcError, BlockByBlockHashRequest] = {
32+
params match {
33+
case Some(JArray(JString(blockHash) :: JBool(fullTxs) :: Nil)) =>
34+
extractHash(blockHash).map(BlockByBlockHashRequest(_, fullTxs))
35+
case _ => Left(InvalidParams())
36+
}
37+
}
38+
39+
override def encodeJson(t: BlockByBlockHashResponse): JValue =
40+
Extraction.decompose(t.blockResponse)
41+
}
42+
43+
implicit val eth_getBlockByNumber = new JsonMethodDecoder[BlockByNumberRequest]
44+
with JsonEncoder[BlockByNumberResponse] {
45+
override def decodeJson(params: Option[JArray]): Either[JsonRpcError, BlockByNumberRequest] = {
46+
params match {
47+
case Some(JArray(blockStr :: JBool(fullTxs) :: Nil)) =>
48+
extractBlockParam(blockStr).map(BlockByNumberRequest(_, fullTxs))
49+
case _ => Left(InvalidParams())
50+
}
51+
}
52+
53+
override def encodeJson(t: BlockByNumberResponse): JValue =
54+
Extraction.decompose(t.blockResponse)
55+
}
56+
57+
implicit val eth_getUncleByBlockHashAndIndex = new JsonMethodDecoder[UncleByBlockHashAndIndexRequest]
58+
with JsonEncoder[UncleByBlockHashAndIndexResponse] {
59+
override def decodeJson(params: Option[JArray]): Either[JsonRpcError, UncleByBlockHashAndIndexRequest] =
60+
params match {
61+
case Some(JArray(JString(blockHash) :: uncleIndex :: Nil)) =>
62+
for {
63+
hash <- extractHash(blockHash)
64+
uncleBlockIndex <- extractQuantity(uncleIndex)
65+
} yield UncleByBlockHashAndIndexRequest(hash, uncleBlockIndex)
66+
case _ => Left(InvalidParams())
67+
}
68+
69+
override def encodeJson(t: UncleByBlockHashAndIndexResponse): JValue = {
70+
val uncleBlockResponse = Extraction.decompose(t.uncleBlockResponse)
71+
uncleBlockResponse.removeField {
72+
case JField("transactions", _) => true
73+
case _ => false
74+
}
75+
}
76+
}
77+
78+
implicit val eth_getUncleByBlockNumberAndIndex = new JsonMethodDecoder[UncleByBlockNumberAndIndexRequest]
79+
with JsonEncoder[UncleByBlockNumberAndIndexResponse] {
80+
override def decodeJson(params: Option[JArray]): Either[JsonRpcError, UncleByBlockNumberAndIndexRequest] =
81+
params match {
82+
case Some(JArray(blockStr :: uncleIndex :: Nil)) =>
83+
for {
84+
block <- extractBlockParam(blockStr)
85+
uncleBlockIndex <- extractQuantity(uncleIndex)
86+
} yield UncleByBlockNumberAndIndexRequest(block, uncleBlockIndex)
87+
case _ => Left(InvalidParams())
88+
}
89+
90+
override def encodeJson(t: UncleByBlockNumberAndIndexResponse): JValue = {
91+
val uncleBlockResponse = Extraction.decompose(t.uncleBlockResponse)
92+
uncleBlockResponse.removeField {
93+
case JField("transactions", _) => true
94+
case _ => false
95+
}
96+
}
97+
}
98+
99+
implicit val eth_getUncleCountByBlockNumber = new JsonMethodDecoder[GetUncleCountByBlockNumberRequest]
100+
with JsonEncoder[GetUncleCountByBlockNumberResponse] {
101+
def decodeJson(params: Option[JArray]): Either[JsonRpcError, GetUncleCountByBlockNumberRequest] =
102+
params match {
103+
case Some(JArray((blockValue: JValue) :: Nil)) =>
104+
for {
105+
block <- extractBlockParam(blockValue)
106+
} yield GetUncleCountByBlockNumberRequest(block)
107+
case _ => Left(InvalidParams())
108+
}
109+
110+
def encodeJson(t: GetUncleCountByBlockNumberResponse): JValue = encodeAsHex(t.result)
111+
}
112+
113+
implicit val eth_getUncleCountByBlockHash = new JsonMethodDecoder[GetUncleCountByBlockHashRequest]
114+
with JsonEncoder[GetUncleCountByBlockHashResponse] {
115+
def decodeJson(params: Option[JArray]): Either[JsonRpcError, GetUncleCountByBlockHashRequest] =
116+
params match {
117+
case Some(JArray(JString(hash) :: Nil)) =>
118+
for {
119+
blockHash <- extractHash(hash)
120+
} yield GetUncleCountByBlockHashRequest(blockHash)
121+
case _ => Left(InvalidParams())
122+
}
123+
124+
def encodeJson(t: GetUncleCountByBlockHashResponse): JValue = encodeAsHex(t.result)
125+
}
126+
127+
implicit val eth_getBlockTransactionCountByNumber = new JsonMethodDecoder[GetBlockTransactionCountByNumberRequest]
128+
with JsonEncoder[GetBlockTransactionCountByNumberResponse] {
129+
def decodeJson(params: Option[JArray]): Either[JsonRpcError, GetBlockTransactionCountByNumberRequest] =
130+
params match {
131+
case Some(JArray((blockValue: JValue) :: Nil)) =>
132+
for {
133+
block <- extractBlockParam(blockValue)
134+
} yield GetBlockTransactionCountByNumberRequest(block)
135+
case _ => Left(InvalidParams())
136+
}
137+
138+
def encodeJson(t: GetBlockTransactionCountByNumberResponse): JValue = encodeAsHex(t.result)
139+
}
140+
}

0 commit comments

Comments
 (0)