Skip to content

Commit 996f260

Browse files
committed
Removed ClientError
1 parent c0c6dea commit 996f260

19 files changed

+130
-145
lines changed

src/main/scala/units/BlockHash.scala

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
package units
22

33
import com.wavesplatform.common.state.ByteStr
4-
import units.util.HexBytesConverter
54
import play.api.libs.json.{Format, Reads, Writes}
6-
import supertagged.TaggedType
5+
import units.util.HexBytesConverter
76

8-
object BlockHash extends TaggedType[String] {
7+
opaque type BlockHash = String
8+
9+
object BlockHash {
910
def apply(hex: String): BlockHash = {
1011
require(hex.startsWith("0x"), "Expected hash to start with 0x")
1112
require(hex.length == 66, s"Expected hash size of 66, got: ${hex.length}. Hex: $hex") // "0x" + 32 bytes
12-
BlockHash @@ hex
13+
hex
1314
}
1415

15-
def apply(xs: ByteStr): BlockHash = BlockHash @@ HexBytesConverter.toHex(xs)
16+
def apply(xs: ByteStr): BlockHash = apply(xs.arr)
1617

1718
def apply(xs: Array[Byte]): BlockHash = {
1819
require(xs.length == 32, "Block hash size must be 32 bytes")
19-
BlockHash @@ HexBytesConverter.toHex(xs)
20+
HexBytesConverter.toHex(xs)
2021
}
2122

22-
implicit lazy val jsonFormat: Format[BlockHash] = Format(
23+
extension (bh: BlockHash) def str: String = bh
24+
25+
given Format[BlockHash] = Format(
2326
Reads.StringReads.map(apply),
2427
Writes.StringWrites.contramap(x => x)
2528
)

src/main/scala/units/ClientError.scala

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/main/scala/units/ELUpdater.scala

Lines changed: 65 additions & 84 deletions
Large diffs are not rendered by default.

src/main/scala/units/NetworkL2Block.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class NetworkL2Block private (
4949
}
5050

5151
object NetworkL2Block {
52-
private def apply(payload: JsObject, payloadBytes: Array[Byte], signature: Option[ByteStr]): Either[ClientError, NetworkL2Block] = {
52+
private def apply(payload: JsObject, payloadBytes: Array[Byte], signature: Option[ByteStr]): Either[String, NetworkL2Block] = {
5353
// See BlockToPayloadMapper for all available fields
5454
(for {
5555
hash <- (payload \ "blockHash").asOpt[BlockHash].toRight("hash not defined")
@@ -79,20 +79,20 @@ object NetworkL2Block {
7979
payloadBytes,
8080
payload,
8181
signature
82-
)).leftMap(err => ClientError(s"Error creating BlockL2 from payload ${new String(payloadBytes)}: $err at payload"))
82+
)).leftMap(err => s"Error creating BlockL2 from payload ${new String(payloadBytes)}: $err at payload")
8383
}
8484

85-
def apply(payloadBytes: Array[Byte], signature: Option[ByteStr]): Either[ClientError, NetworkL2Block] = for {
86-
payload <- Json.parse(payloadBytes).asOpt[JsObject].toRight(ClientError("Payload is not a valid JSON object"))
85+
def apply(payloadBytes: Array[Byte], signature: Option[ByteStr]): Either[String, NetworkL2Block] = for {
86+
payload <- Json.parse(payloadBytes).asOpt[JsObject].toRight("Payload is not a valid JSON object")
8787
block <- apply(payload, payloadBytes, signature)
8888
} yield block
8989

90-
def signed(payload: JsObject, signer: PrivateKey): Either[ClientError, NetworkL2Block] = {
90+
def signed(payload: JsObject, signer: PrivateKey): Either[String, NetworkL2Block] = {
9191
val payloadBytes = Json.toBytes(payload)
9292
NetworkL2Block(payload, payloadBytes, Some(crypto.sign(signer, payloadBytes)))
9393
}
9494

95-
def apply(payload: JsObject): Either[ClientError, NetworkL2Block] = apply(payload, Json.toBytes(payload), None)
95+
def apply(payload: JsObject): Either[String, NetworkL2Block] = apply(payload, Json.toBytes(payload), None)
9696

9797
def validateReferenceLength(length: Int): Boolean =
9898
length == DigestLength

src/main/scala/units/client/JsonRpcClient.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import pureconfig.*
77
import pureconfig.generic.semiauto.*
88
import sttp.client3.*
99
import sttp.client3.playJson.*
10-
import units.ClientError
1110
import units.client.JsonRpcClient.*
1211

1312
import java.util.concurrent.ThreadLocalRandom
@@ -27,8 +26,8 @@ trait JsonRpcClient {
2726
): Either[String, Option[RP]] =
2827
sendRequest(requestId, mkRequest(requestBody, timeout), config.apiRequestRetries)
2928

30-
protected def parseJson[A: Reads](jsValue: JsValue): Either[ClientError, A] =
31-
Try(jsValue.as[A]).toEither.leftMap(err => ClientError(s"Response parse error: ${err.getMessage}"))
29+
protected def parseJson[A: Reads](jsValue: JsValue): Either[String, A] =
30+
Try(jsValue.as[A]).toEither.leftMap(err => s"Response parse error: ${err.getMessage}")
3231

3332
private def mkRequest[A: Writes, B: Reads](requestBody: A, timeout: FiniteDuration): RpcRequest[B] =
3433
basicRequest

src/main/scala/units/client/contract/ChainContractClient.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ trait ChainContractClient {
433433
case None => None
434434
}
435435

436-
private def clean(hash: BlockHash): String = hash.drop(2) // Drop "0x"
436+
private def clean(hash: BlockHash): String = hash.toString.drop(2) // Drop "0x"
437437
}
438438

439439
object ChainContractClient {

src/main/scala/units/client/contract/ContractFunction.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import com.wavesplatform.lang.v1.FunctionHeader
88
import com.wavesplatform.lang.v1.compiler.Terms.{CONST_BYTESTR, CONST_LONG, CONST_STRING, EVALUATED, FUNCTION_CALL}
99
import org.web3j.utils.Numeric.cleanHexPrefix
1010
import units.util.HexBytesConverter.toHexNoPrefix
11-
import units.{BlockHash, ClientError, JobResult}
11+
import units.{BlockHash, JobResult}
1212

1313
abstract class ContractFunction(baseName: String, extraArgs: Either[CommonError, List[EVALUATED]]) {
1414
def reference: BlockHash
@@ -21,15 +21,15 @@ abstract class ContractFunction(baseName: String, extraArgs: Either[CommonError,
2121
lastC2ETransferIndex: Long,
2222
lastAssetRegistrySyncedIndex: Long
2323
): JobResult[FUNCTION_CALL] = (for {
24-
hash <- CONST_STRING(cleanHexPrefix(blockHash))
25-
ref <- CONST_STRING(cleanHexPrefix(reference))
24+
hash <- CONST_STRING(cleanHexPrefix(blockHash.toString))
25+
ref <- CONST_STRING(cleanHexPrefix(reference.toString))
2626
ntrh <- CONST_STRING(toHexNoPrefix(transfersRootHash))
2727
xtra <- extraArgs
2828
} yield FUNCTION_CALL(
2929
FunctionHeader.User(name),
3030
List(hash, ref) ++ xtra ++ List(ntrh, CONST_LONG(lastC2ETransferIndex)) ++
3131
(if (version >= 2) List(CONST_LONG(lastAssetRegistrySyncedIndex)) else Nil)
32-
)).leftMap(e => ClientError(s"Error building function call for $name: $e"))
32+
)).leftMap(e => s"Error building function call for $name: $e")
3333
}
3434

3535
object ContractFunction {

src/main/scala/units/client/engine/EngineApiClient.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import units.client.engine.model.Withdrawal.WithdrawalIndex
88
import units.el.DepositedTransaction
99
import units.eth.{EmptyL2Block, EthAddress}
1010
import units.util.BlockToPayloadMapper
11-
import units.{BlockHash, ClientError, JobResult}
11+
import units.{BlockHash, JobResult}
1212

1313
import scala.annotation.tailrec
1414

@@ -67,7 +67,7 @@ object EngineApiClient {
6767
depositedTransactions: Seq[DepositedTransaction]
6868
): JobResult[JsObject] = for {
6969
targetBlockOpt <- c.getBlockByHash(rollbackTargetBlockId)
70-
targetBlock <- targetBlockOpt.toRight(ClientError(s"Target block $rollbackTargetBlockId is not in EC"))
70+
targetBlock <- targetBlockOpt.toRight(s"Target block $rollbackTargetBlockId is not in EC")
7171
simulatedBlockJson <- c.simulate(
7272
EmptyL2Block.mkSimulateCall(targetBlock, feeRecipient, time, prevRandao, withdrawals, depositedTransactions),
7373
targetBlock.hash
@@ -78,7 +78,7 @@ object EngineApiClient {
7878
def getLastWithdrawalIndex(hash: BlockHash): JobResult[WithdrawalIndex] =
7979
c.getBlockByHash(hash) match {
8080
case Left(e) => Left(e)
81-
case Right(None) => Left(ClientError(s"Can't find $hash block on EC during withdrawal search"))
81+
case Right(None) => Left(s"Can't find $hash block on EC during withdrawal search")
8282
case Right(Some(ecBlock)) =>
8383
ecBlock.withdrawals.lastOption match {
8484
case Some(lastWithdrawal) => Right(lastWithdrawal.index)

src/main/scala/units/client/engine/HttpEngineApiClient.scala

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import units.client.engine.model.ForkchoiceUpdatedRequest.ForkChoiceAttributes
1111
import units.client.engine.model.PayloadStatus.{Syncing, Valid}
1212
import units.client.engine.model.{*, given}
1313
import units.eth.EthAddress
14-
import units.{BlockHash, ClientError, JobResult}
14+
import units.{BlockHash, JobResult}
1515

1616
import scala.concurrent.duration.{DurationInt, FiniteDuration}
1717

@@ -25,8 +25,8 @@ class HttpEngineApiClient(val config: JsonRpcClient.Config, val backend: SttpBac
2525
.flatMap {
2626
case ForkChoiceUpdatedResponse(ps @ PayloadState(Valid | Syncing, _, _), None) => Right(ps.status)
2727
case ForkChoiceUpdatedResponse(PayloadState(_, _, Some(validationError)), _) =>
28-
Left(ClientError(s"Payload validation error: $validationError"))
29-
case ForkChoiceUpdatedResponse(payloadState, _) => Left(ClientError(s"Unexpected payload status ${payloadState.status}"))
28+
Left(s"Payload validation error: $validationError")
29+
case ForkChoiceUpdatedResponse(payloadState, _) => Left(s"Unexpected payload status ${payloadState.status}")
3030
}
3131
}
3232

@@ -53,11 +53,11 @@ class HttpEngineApiClient(val config: JsonRpcClient.Config, val backend: SttpBac
5353
case ForkChoiceUpdatedResponse(PayloadState(Valid, _, _), Some(payloadId)) =>
5454
Right(payloadId)
5555
case ForkChoiceUpdatedResponse(_, None) =>
56-
Left(ClientError(s"Payload id for $lastBlockHash is not defined"))
56+
Left(s"Payload id for $lastBlockHash is not defined")
5757
case ForkChoiceUpdatedResponse(PayloadState(_, _, Some(validationError)), _) =>
58-
Left(ClientError(s"Payload validation error for $lastBlockHash: $validationError"))
58+
Left(s"Payload validation error for $lastBlockHash: $validationError")
5959
case ForkChoiceUpdatedResponse(payloadState, _) =>
60-
Left(ClientError(s"Unexpected payload status for $lastBlockHash: ${payloadState.status}"))
60+
Left(s"Unexpected payload status for $lastBlockHash: ${payloadState.status}")
6161
}
6262
}
6363

@@ -69,11 +69,11 @@ class HttpEngineApiClient(val config: JsonRpcClient.Config, val backend: SttpBac
6969

7070
def newPayload(payload: JsObject, requestId: Int): JobResult[Option[BlockHash]] = {
7171
sendEngineRequest[NewPayloadRequest, PayloadState](NewPayloadRequest(payload, requestId), BlockExecutionTimeout, requestId).flatMap {
72-
case PayloadState(_, _, Some(validationError)) => Left(ClientError(s"Payload validation error: $validationError"))
72+
case PayloadState(_, _, Some(validationError)) => Left(s"Payload validation error: $validationError")
7373
case PayloadState(Valid, Some(latestValidHash), _) => Right(Some(latestValidHash))
7474
case PayloadState(Syncing, latestValidHash, _) => Right(latestValidHash)
75-
case PayloadState(status, None, _) => Left(ClientError(s"Latest valid hash is not defined at status $status"))
76-
case PayloadState(status, _, _) => Left(ClientError(s"Unexpected payload status: $status"))
75+
case PayloadState(status, None, _) => Left(s"Latest valid hash is not defined at status $status")
76+
case PayloadState(status, _, _) => Left(s"Unexpected payload status: $status")
7777
}
7878
}
7979

@@ -94,20 +94,20 @@ class HttpEngineApiClient(val config: JsonRpcClient.Config, val backend: SttpBac
9494
GetBlockByHashRequest(hash, fullTransactionObjects = false, requestId),
9595
NonBlockExecutionTimeout,
9696
requestId
97-
).leftMap(err => ClientError(s"Error getting block by hash $hash: $err"))
97+
).leftMap(err => s"Error getting block by hash $hash: $err")
9898
}
9999

100100
def getBlockByHashJson(hash: BlockHash, fullTransactionObjects: Boolean, requestId: Int): JobResult[Option[JsObject]] = {
101101
sendRequest[GetBlockByHashRequest, JsObject](
102102
GetBlockByHashRequest(hash, fullTransactionObjects, requestId),
103103
NonBlockExecutionTimeout,
104104
requestId
105-
).leftMap(err => ClientError(s"Error getting block json by hash $hash: $err"))
105+
).leftMap(err => s"Error getting block json by hash $hash: $err")
106106
}
107107

108108
def getLastExecutionBlock(requestId: Int): JobResult[EcBlock] = for {
109109
lastEcBlockOpt <- getBlockByNumber(BlockNumber.Latest, requestId)
110-
lastEcBlock <- Either.fromOption(lastEcBlockOpt, ClientError("Impossible: EC doesn't have blocks"))
110+
lastEcBlock <- Either.fromOption(lastEcBlockOpt, "Impossible: EC doesn't have blocks")
111111
} yield lastEcBlock
112112

113113
def blockExists(hash: BlockHash, requestId: Int): JobResult[Boolean] =
@@ -116,11 +116,11 @@ class HttpEngineApiClient(val config: JsonRpcClient.Config, val backend: SttpBac
116116
override def simulate(blockStateCalls: Seq[BlockStateCall], hash: BlockHash, requestId: Int): JobResult[Seq[JsObject]] =
117117
sendRequest[SimulateRequest, Seq[JsObject]](SimulateRequest(blockStateCalls, hash, requestId), NonBlockExecutionTimeout, requestId)
118118
.flatMap(_.toRight("Simulated block was empty"))
119-
.leftMap(err => ClientError(s"Error simulating block: $err"))
119+
.leftMap(err => s"Error simulating block: $err")
120120

121121
private def getBlockByNumberJson(number: String, requestId: Int): JobResult[Option[JsObject]] = {
122122
sendRequest[GetBlockByNumberRequest, JsObject](GetBlockByNumberRequest(number, requestId), NonBlockExecutionTimeout, requestId)
123-
.leftMap(err => ClientError(s"Error getting block by number $number: $err"))
123+
.leftMap(err => s"Error getting block by number $number: $err")
124124
}
125125

126126
override def getLogs(hash: BlockHash, addresses: List[EthAddress], topics: List[String], requestId: Int): JobResult[List[GetLogsResponseEntry]] =
@@ -129,13 +129,13 @@ class HttpEngineApiClient(val config: JsonRpcClient.Config, val backend: SttpBac
129129
NonBlockExecutionTimeout,
130130
requestId
131131
)
132-
.leftMap(err => ClientError(s"Error getting block logs by hash $hash: $err"))
132+
.leftMap(err => s"Error getting block logs by hash $hash: $err")
133133
.map(_.getOrElse(List.empty))
134134

135135
private def sendEngineRequest[A: Writes, B: Reads](request: A, timeout: FiniteDuration, requestId: Int): JobResult[B] = {
136136
sendRequest(request, timeout, requestId) match {
137-
case Right(response) => response.toRight(ClientError(s"Unexpected engine API empty response"))
138-
case Left(err) => Left(ClientError(s"Engine API request error: $err"))
137+
case Right(response) => response.toRight(s"Unexpected engine API empty response")
138+
case Left(err) => Left(s"Engine API request error: $err")
139139
}
140140
}
141141
}

src/main/scala/units/client/engine/LoggedEngineApiClient.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class LoggedEngineApiClient(underlying: EngineApiClient) extends EngineApiClient
9191
logDebug(requestId, method)
9292

9393
f(requestId).tap {
94-
case Left(e) => logDebug(requestId, s"Error: ${e.message}")
94+
case Left(e) => logDebug(requestId, s"Error: $e")
9595
case Right(r) => logDebug(requestId, s"Success: ${toMsg(r)}")
9696
}
9797
}

0 commit comments

Comments
 (0)