From c1816dc4a34c98601b8e4d2c5302b4c53852ff3c Mon Sep 17 00:00:00 2001 From: Vyatcheslav Suharnikov Date: Thu, 28 Nov 2024 12:03:12 +0400 Subject: [PATCH] Ability to disable time synchronization --- .../src/main/scala/com/wavesplatform/Application.scala | 4 ++-- node/src/main/scala/com/wavesplatform/Exporter.scala | 2 +- node/src/main/scala/com/wavesplatform/Importer.scala | 8 ++++---- .../com/wavesplatform/settings/WavesSettings.scala | 4 ++-- node/src/main/scala/com/wavesplatform/utils/Time.scala | 8 +++++++- .../main/scala/com/wavesplatform/utils/UtilApp.scala | 2 +- .../wavesplatform/http/UtilsRouteEvaluateSpec.scala | 7 ++----- .../scala/com/wavesplatform/http/UtilsRouteSpec.scala | 10 +++------- 8 files changed, 22 insertions(+), 23 deletions(-) diff --git a/node/src/main/scala/com/wavesplatform/Application.scala b/node/src/main/scala/com/wavesplatform/Application.scala index b3ac210cfd..1f62398fda 100644 --- a/node/src/main/scala/com/wavesplatform/Application.scala +++ b/node/src/main/scala/com/wavesplatform/Application.scala @@ -63,7 +63,7 @@ import scala.concurrent.duration.* import scala.concurrent.{Await, Future} import scala.util.{Failure, Success, Try} -class Application(val actorSystem: ActorSystem, val settings: WavesSettings, configRoot: ConfigObject, time: NTP) extends ScorexLogging { +class Application(val actorSystem: ActorSystem, val settings: WavesSettings, configRoot: ConfigObject, time: Time) extends ScorexLogging { app => import Application.* @@ -660,7 +660,7 @@ object Application extends ScorexLogging { SystemInformationReporter.report(settings.config) } - val time = new NTP(settings.ntpServer) + val time = settings.ntpServer.fold[Time](SystemTime)(new NTP(_)) Metrics.start(settings.metrics, time) def dumpMinerConfig(): Unit = { diff --git a/node/src/main/scala/com/wavesplatform/Exporter.scala b/node/src/main/scala/com/wavesplatform/Exporter.scala index 3675fdbe43..a4be618158 100644 --- a/node/src/main/scala/com/wavesplatform/Exporter.scala +++ b/node/src/main/scala/com/wavesplatform/Exporter.scala @@ -43,7 +43,7 @@ object Exporter extends ScorexLogging { val settings = Application.loadApplicationConfig(configFile) Using.resources( - new NTP(settings.ntpServer), + settings.ntpServer.fold[Time](SystemTime)(new NTP(_)), RDB.open(settings.dbSettings) ) { (time, rdb) => val (blockchain, rdbWriter) = StorageFactory(settings, rdb, time, BlockchainUpdateTriggers.noop) diff --git a/node/src/main/scala/com/wavesplatform/Importer.scala b/node/src/main/scala/com/wavesplatform/Importer.scala index 529287f931..1ed4206211 100644 --- a/node/src/main/scala/com/wavesplatform/Importer.scala +++ b/node/src/main/scala/com/wavesplatform/Importer.scala @@ -121,7 +121,7 @@ object Importer extends ScorexLogging { appenderScheduler: Scheduler, extensionTime: Time, utxPool: UtxPool, - rdb: RDB, + rdb: RDB ): Seq[Extension] = if (wavesSettings.extensions.isEmpty) Seq.empty else { @@ -308,7 +308,7 @@ object Importer extends ScorexLogging { case _ => counter = counter + 1 } - } else if (!quit){ + } else if (!quit) { log.warn(s"Block $block is not a child of the last block ${blockchain.lastBlockId.get}") } } @@ -343,9 +343,9 @@ object Importer extends ScorexLogging { } val scheduler = Schedulers.singleThread("appender") - val time = new NTP(settings.ntpServer) + val time = settings.ntpServer.fold[Time](SystemTime)(new NTP(_)) - val rdb = RDB.open(settings.dbSettings) + val rdb = RDB.open(settings.dbSettings) val (blockchainUpdater, rdbWriter) = StorageFactory(settings, rdb, time, BlockchainUpdateTriggers.combined(triggers)) val utxPool = new UtxPoolImpl(time, blockchainUpdater, settings.utxSettings, settings.maxTxErrorLogSize, settings.minerSettings.enable) diff --git a/node/src/main/scala/com/wavesplatform/settings/WavesSettings.scala b/node/src/main/scala/com/wavesplatform/settings/WavesSettings.scala index 375adb61c6..37ce750fb8 100644 --- a/node/src/main/scala/com/wavesplatform/settings/WavesSettings.scala +++ b/node/src/main/scala/com/wavesplatform/settings/WavesSettings.scala @@ -8,7 +8,7 @@ import pureconfig.generic.auto.* case class WavesSettings( directory: String, - ntpServer: String, + ntpServer: Option[String], maxTxErrorLogSize: Int, dbSettings: DBSettings, extensions: Seq[String], @@ -33,7 +33,7 @@ object WavesSettings { val wavesConfigSource = ConfigSource.fromConfig(waves) val directory = wavesConfigSource.at("directory").loadOrThrow[String] - val ntpServer = wavesConfigSource.at("ntp-server").loadOrThrow[String] + val ntpServer = wavesConfigSource.at("ntp-server").loadOrThrow[Option[String]] val maxTxErrorLogSize = wavesConfigSource.at("max-tx-error-log-size").loadOrThrow[Int] val dbSettings = wavesConfigSource.at("db").loadOrThrow[DBSettings] val extensions = wavesConfigSource.at("extensions").loadOrThrow[Seq[String]] diff --git a/node/src/main/scala/com/wavesplatform/utils/Time.scala b/node/src/main/scala/com/wavesplatform/utils/Time.scala index dc5740b0f1..47f8385b02 100644 --- a/node/src/main/scala/com/wavesplatform/utils/Time.scala +++ b/node/src/main/scala/com/wavesplatform/utils/Time.scala @@ -9,9 +9,15 @@ import org.apache.commons.net.ntp.NTPUDPClient import java.time.Duration import scala.concurrent.duration.DurationInt -trait Time { +trait Time extends AutoCloseable { def correctedTime(): Long def getTimestamp(): Long + override def close(): Unit = {} +} + +object SystemTime extends Time { + def correctedTime(): Long = System.currentTimeMillis() + def getTimestamp(): Long = System.currentTimeMillis() } class NTP(ntpServer: String) extends Time with ScorexLogging with AutoCloseable { diff --git a/node/src/main/scala/com/wavesplatform/utils/UtilApp.scala b/node/src/main/scala/com/wavesplatform/utils/UtilApp.scala index 8e4a0802a1..9a0a1acdcf 100644 --- a/node/src/main/scala/com/wavesplatform/utils/UtilApp.scala +++ b/node/src/main/scala/com/wavesplatform/utils/UtilApp.scala @@ -233,7 +233,7 @@ object UtilApp { private[this] final class NodeState(c: Command) { lazy val settings = Application.loadApplicationConfig(c.configFile.map(new File(_))) lazy val wallet = Wallet(settings.walletSettings) - lazy val time = new NTP(settings.ntpServer) + lazy val time = settings.ntpServer.fold[Time](SystemTime)(new NTP(_)) } private[this] object Actions { diff --git a/node/tests/src/test/scala/com/wavesplatform/http/UtilsRouteEvaluateSpec.scala b/node/tests/src/test/scala/com/wavesplatform/http/UtilsRouteEvaluateSpec.scala index 8ca754e69d..a1b78493a7 100644 --- a/node/tests/src/test/scala/com/wavesplatform/http/UtilsRouteEvaluateSpec.scala +++ b/node/tests/src/test/scala/com/wavesplatform/http/UtilsRouteEvaluateSpec.scala @@ -20,7 +20,7 @@ import com.wavesplatform.test.NumericExt import com.wavesplatform.transaction.Asset.IssuedAsset import com.wavesplatform.transaction.TxHelpers.* import com.wavesplatform.transaction.{Asset, AssetIdLength, TxHelpers} -import com.wavesplatform.utils.{Schedulers, Time} +import com.wavesplatform.utils.{Schedulers, SystemTime} import io.netty.util.HashedWheelTimer import monix.execution.schedulers.SchedulerService import org.scalamock.scalatest.PathMockFactory @@ -44,10 +44,7 @@ class UtilsRouteEvaluateSpec "rest-time-limited" ) private val utilsApi: UtilsApiRoute = UtilsApiRoute( - new Time { - def correctedTime(): Long = System.currentTimeMillis() - def getTimestamp(): Long = System.currentTimeMillis() - }, + SystemTime, restAPISettings, Int.MaxValue, () => ScriptEstimatorV3.latest, diff --git a/node/tests/src/test/scala/com/wavesplatform/http/UtilsRouteSpec.scala b/node/tests/src/test/scala/com/wavesplatform/http/UtilsRouteSpec.scala index 9941601904..72a23f2bdc 100644 --- a/node/tests/src/test/scala/com/wavesplatform/http/UtilsRouteSpec.scala +++ b/node/tests/src/test/scala/com/wavesplatform/http/UtilsRouteSpec.scala @@ -26,7 +26,7 @@ import com.wavesplatform.settings.TestSettings import com.wavesplatform.state.Blockchain import com.wavesplatform.state.diffs.FeeValidation import com.wavesplatform.transaction.smart.script.ScriptCompiler -import com.wavesplatform.utils.{Schedulers, Time} +import com.wavesplatform.utils.{Schedulers, SystemTime} import io.netty.util.HashedWheelTimer import monix.execution.schedulers.SchedulerService import org.scalacheck.Gen @@ -38,7 +38,7 @@ import play.api.libs.json.* import scala.concurrent.duration.* class UtilsRouteSpec extends RouteSpec("/utils") with RestAPISettingsHelper with PropertyChecks with PathMockFactory with Inside with WithDomain { - private val estimator = ScriptEstimatorV2 + private val estimator = ScriptEstimatorV2 protected override implicit val routeTestTimeout: RouteTestTimeout = RouteTestTimeout(20.seconds) private val timeBounded: SchedulerService = Schedulers.timeBoundedFixedPool( @@ -48,11 +48,7 @@ class UtilsRouteSpec extends RouteSpec("/utils") with RestAPISettingsHelper with "rest-time-limited" ) private val utilsApi: UtilsApiRoute = UtilsApiRoute( - new Time { - def correctedTime(): Long = System.currentTimeMillis() - - def getTimestamp(): Long = System.currentTimeMillis() - }, + SystemTime, restAPISettings, Int.MaxValue, () => estimator,