Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions src/main/scala/encry/Starter.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package encry

import java.io.File
import java.net.InetSocketAddress
import java.nio.file.Files
import akka.actor.{ Actor, ActorRef }
import akka.http.scaladsl.Http
import cats.Functor
Expand All @@ -12,7 +14,7 @@ import cats.syntax.either._
import cats.syntax.option._
import cats.syntax.validated._
import encry.Starter.InitNodeResult
import encry.api.http.DataHolderForApi
import encry.api.http.{ DataHolderForApi, SettingsStorage }
import encry.api.http.DataHolderForApi.PassForStorage
import encry.cli.ConsoleListener
import encry.cli.ConsoleListener.{ prompt, StartListening }
Expand Down Expand Up @@ -65,7 +67,9 @@ class Starter(settings: EncryAppSettings,
self ! res
}

def startNonEmptyNode: Either[Throwable, InitNodeResult] =
def startNonEmptyNode: Either[Throwable, InitNodeResult] = {
val storage = SettingsStorage.init(settings)
val newSettings: EncryAppSettings = storage.getSettings.getOrElse(settings)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

incorrect behavior

for {
walletPassword <- {
println("Please, enter wallet password:")
Expand All @@ -75,16 +79,17 @@ class Starter(settings: EncryAppSettings,
InitNodeResult(
"",
walletPassword,
settings.node.offlineGeneration,
newSettings.node.offlineGeneration,
fastSync = false,
settings.snapshotSettings.enableSnapshotCreation,
settings.network.knownPeers,
settings.network.connectOnlyWithKnownPeers.getOrElse(false),
newSettings.snapshotSettings.enableSnapshotCreation,
newSettings.network.knownPeers,
newSettings.network.connectOnlyWithKnownPeers.getOrElse(false),
"",
settings.network.nodeName.getOrElse(""),
settings.network.declaredAddress,
settings.network.bindAddress
newSettings.network.nodeName.getOrElse(""),
newSettings.network.declaredAddress,
newSettings.network.bindAddress
)
}

def startEmptyNode: Either[Throwable, InitNodeResult] =
for {
Expand Down Expand Up @@ -400,6 +405,11 @@ class Starter(settings: EncryAppSettings,
network = networkSettings,
snapshotSettings = snapshotSettings
)
if (!Files.exists(new File(s"${settings.directory}/state").toPath)) {
val storage: SettingsStorage = SettingsStorage.init(newSettings)
storage.putSettings(newSettings)
storage.close()
}
val influxRef: Option[ActorRef] = newSettings.influxDB.map { influxSettings =>
context.system
.actorOf(StatsSender.props(influxSettings, newSettings.network, newSettings.constants), "statsSender")
Expand Down
76 changes: 76 additions & 0 deletions src/main/scala/encry/api/http/SettingsStorage.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package encry.api.http

import java.io.{ ByteArrayInputStream, ByteArrayOutputStream, File, ObjectInputStream, ObjectOutputStream }
import cats.syntax.either._
import com.typesafe.scalalogging.StrictLogging
import encry.settings.EncryAppSettings
import org.encryfoundation.common.utils.Algos
import org.iq80.leveldb.{ DB, Options }
import scorex.crypto.hash.Digest32
import encry.storage.levelDb.versionalLevelDB.LevelDbFactory

trait SettingsStorage extends StrictLogging with AutoCloseable {

val storage: DB

def getSettings: Option[EncryAppSettings] = SettingsStorage.deserialise(storage.get(SettingsStorage.SettingsKey))

def putSettings(settings: EncryAppSettings): Either[Throwable, Unit] = {
println(s"settings = ${settings.network.nodeName}")
val batch = storage.createWriteBatch()
val serialisedSettings: Array[Byte] = SettingsStorage.serialise(settings)
try {
batch.put(SettingsStorage.SettingsKey, serialisedSettings)
storage.write(batch).asRight[Throwable]
} catch {
case err: Throwable => err.asLeft[Unit]
} finally {
batch.close()
}
}

override def close(): Unit = storage.close()

}

object SettingsStorage extends StrictLogging {

val SettingsKey: Digest32 = Algos.hash(s"Settings_Key")

def serialise(value: EncryAppSettings): Array[Byte] = {
val stream: ByteArrayOutputStream = new ByteArrayOutputStream()
val oos = new ObjectOutputStream(stream)
try {
oos.writeObject(value)
stream.toByteArray
} catch {
case err: Throwable => throw new Exception(s"Error during serialisation cause of $err")
} finally {
oos.close()
}
}

def deserialise(bytes: Array[Byte]): Option[EncryAppSettings] = {
val ois = new ObjectInputStream(new ByteArrayInputStream(bytes))
try {
val value = ois.readObject
val deserialised: Option[EncryAppSettings] = value match {
case set: EncryAppSettings =>
logger.info(s"Deserialisation ended successfully")
Some(set)
}
deserialised
} catch {
case err: Throwable => throw new Exception(s"Error during serialisation cause of $err")
} finally {
ois.close()
}
}

def getDirStorage(settings: EncryAppSettings): File = new File(s"${settings.directory}/userSettings")

def init(settings: EncryAppSettings): SettingsStorage = new SettingsStorage {
override val storage: DB = LevelDbFactory.factory.open(getDirStorage(settings), new Options)
}

}