Skip to content

Commit

Permalink
Remove more ficus from the ride runner
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimirlogachev committed Nov 15, 2024
1 parent 2f1a82a commit 0007744
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import com.wavesplatform.ride.runner.caches.mem.MemBlockchainDataCache
import com.wavesplatform.ride.runner.entrypoints.{Heights, WavesRideRunnerCompareService}
import com.wavesplatform.ride.runner.requests.DefaultRequestService
import com.wavesplatform.settings.*
import net.ceedubs.ficus.Ficus.*
import net.ceedubs.ficus.readers.ArbitraryTypeReader.*
import com.wavesplatform.ride.runner.input.PureconfigImplicits.*
import pureconfig.*
import pureconfig.generic.auto.*

import scala.concurrent.duration.DurationInt

Expand Down Expand Up @@ -50,5 +51,5 @@ case class RideRunnerGlobalSettings(
}

object RideRunnerGlobalSettings {
def fromRootConfig(config: Config): RideRunnerGlobalSettings = config.getConfig("waves").as[RideRunnerGlobalSettings]
def fromRootConfig(config: Config): RideRunnerGlobalSettings = ConfigSource.fromConfig(config).at("waves").loadOrThrow[RideRunnerGlobalSettings]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.wavesplatform.ride.runner.input

import com.typesafe.config.{ConfigFactory, ConfigRenderOptions}
import com.wavesplatform.account.Address
import com.wavesplatform.json.JsonManipulations
import play.api.libs.json.{JsError, JsObject, JsSuccess, JsValue, Json, Reads}
import pureconfig.ConfigReader
import pureconfig.error.CannotConvert

object PureconfigImplicits {

implicit val jsObjectConfigReader: ConfigReader[JsObject] = playJsonConfigReader

implicit val jsValueConfigReader: ConfigReader[JsValue] = playJsonConfigReader

private def playJsonConfigReader[T: Reads]: ConfigReader[T] = ConfigReader.fromCursor { cur =>
for {
configValue <- cur.asConfigValue
stubKey = "stubKey"
config = ConfigFactory.empty().withValue(stubKey, configValue)
} yield {
val jsonStr = config.root().render(ConfigRenderOptions.concise())
JsonManipulations
.pick(Json.parse(jsonStr), stubKey)
.getOrElse(fail(s"Expected a value"))
.validate[T] match {
case JsSuccess(value, _) => value
case JsError(errors) => fail(s"Can't parse: ${errors.mkString("\n")}")
}
}
}


implicit val addressConfigReader: ConfigReader[Address] =
ConfigReader.fromString(s => Address.fromString(s).left.map(_ => CannotConvert(s, "Address", "invalid address")))

private def fail(message: String, cause: Throwable = null) = throw new IllegalArgumentException(message, cause)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@ package com.wavesplatform.ride.runner.input

import cats.syntax.either.*
import cats.syntax.option.*
import com.typesafe.config.{Config, ConfigFactory, ConfigRenderOptions}
import com.typesafe.config.{Config, ConfigFactory}
import com.wavesplatform.account.*
import com.wavesplatform.account.PublicKeys.EmptyPublicKey
import com.wavesplatform.block.Block.BlockId
import com.wavesplatform.common.state.ByteStr
import com.wavesplatform.common.utils.{Base58, Base64}
import com.wavesplatform.json.JsonManipulations
import com.wavesplatform.lang.script.{Script, ScriptReader}
import com.wavesplatform.ride.ScriptUtil
import com.wavesplatform.transaction.Asset.{IssuedAsset, Waves}
import com.wavesplatform.transaction.{Asset, TxNonNegativeAmount, TxValidationError}
import pureconfig.*
import pureconfig.generic.auto.*
import pureconfig.error.CannotConvert
import play.api.libs.json.*
import com.wavesplatform.ride.runner.input.PureconfigImplicits.*

import java.nio.charset.StandardCharsets
import scala.util.Try
Expand Down Expand Up @@ -167,9 +166,6 @@ object RideRunnerInputParser {
} yield RideRunnerTransaction(amount, assetId, fee, feeAssetId, recipient, senderPublicKey, height, timestamp, proofs, version, attachment)
}

implicit val addressConfigReader: ConfigReader[Address] =
ConfigReader.fromString(s => Address.fromString(s).left.map(_ => CannotConvert(s, "Address", "invalid address")))

implicit val aliasConfigReader: ConfigReader[Alias] = ConfigReader[String].map { x =>
val chainId = AddressScheme.current.chainId

Expand Down Expand Up @@ -256,27 +252,6 @@ object RideRunnerInputParser {
else Base58.tryDecodeWithLimit(x).fold(e => fail(s"Error parsing base58: ${e.getMessage}"), identity)
}

implicit val jsObjectConfigReader: ConfigReader[JsObject] = playJsonConfigReader

implicit val jsValueConfigReader: ConfigReader[JsValue] = playJsonConfigReader

private def playJsonConfigReader[T: Reads]: ConfigReader[T] = ConfigReader.fromCursor { cur =>
for {
configValue <- cur.asConfigValue
stubKey = "stubKey"
config = ConfigFactory.empty().withValue(stubKey, configValue)
} yield {
val jsonStr = config.root().render(ConfigRenderOptions.concise())
JsonManipulations
.pick(Json.parse(jsonStr), stubKey)
.getOrElse(fail(s"Expected a value"))
.validate[T] match {
case JsSuccess(value, _) => value
case JsError(errors) => fail(s"Can't parse: ${errors.mkString("\n")}")
}
}
}

private implicit final class ValidationErrorOps[E, T](private val self: Either[E, T]) extends AnyVal {
def getOrFail: T = self.fold(e => fail(e.toString), identity)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.wavesplatform.transaction.Asset.IssuedAsset
import com.wavesplatform.transaction.TxNonNegativeAmount
import com.wavesplatform.{BaseTestSuite, HasTestAccounts}
import org.scalatest.prop.TableDrivenPropertyChecks
import com.wavesplatform.ride.runner.input.PureconfigImplicits.*
import play.api.libs.json.*
import pureconfig.*

Expand Down

0 comments on commit 0007744

Please sign in to comment.