mirror of
https://github.com/fluencelabs/aqua.git
synced 2025-04-24 22:42:13 +00:00
Addr aliases and info fixes (#470)
This commit is contained in:
parent
9f59d376ca
commit
277bb5c8b8
@ -1,13 +1,13 @@
|
||||
package aqua.config
|
||||
|
||||
import aqua.js.FluenceEnvironment
|
||||
import aqua.js.{FluenceEnvironment, FluenceNode}
|
||||
import cats.Applicative
|
||||
import cats.data.Validated
|
||||
import cats.data.Validated.{invalidNel, validNel}
|
||||
import cats.effect.ExitCode
|
||||
import cats.effect.kernel.Async
|
||||
import com.monovore.decline.{Command, Opts}
|
||||
import Validated.{invalidNel, validNel}
|
||||
import cats.syntax.applicative.*
|
||||
import com.monovore.decline.{Command, Opts}
|
||||
|
||||
import scala.scalajs.js
|
||||
|
||||
@ -24,7 +24,7 @@ object ConfigOpts {
|
||||
val Stage = "stage"
|
||||
val TestNet = "testnet"
|
||||
|
||||
def envArg: Opts[js.Array[js.Dynamic]] =
|
||||
def envArg: Opts[js.Array[FluenceNode]] =
|
||||
Opts
|
||||
.argument[String](s"$Krasnodar | $Stage | $TestNet")
|
||||
.withDefault(Krasnodar)
|
||||
@ -47,7 +47,7 @@ object ConfigOpts {
|
||||
header = "List addresses of default peers in Fluence network"
|
||||
) {
|
||||
envArg.map { env =>
|
||||
println(env.toList.map(n => n.selectDynamic("multiaddr")).mkString("\n"))
|
||||
println(env.toList.map(n => n.multiaddr).mkString("\n"))
|
||||
ExitCode.Success.pure[F]
|
||||
}
|
||||
}
|
||||
|
@ -1,19 +1,26 @@
|
||||
package aqua.js
|
||||
|
||||
import scala.scalajs.js
|
||||
import scala.scalajs.js.annotation.JSImport
|
||||
import scala.scalajs.js.annotation.{JSExportAll, JSImport}
|
||||
|
||||
@js.native
|
||||
@JSImport("@fluencelabs/fluence-network-environment/dist/index.js", "Node")
|
||||
class FluenceNode extends js.Object {
|
||||
val multiaddr: String = js.native
|
||||
def peerId: String = js.native
|
||||
}
|
||||
|
||||
object FluenceEnvironment {
|
||||
|
||||
@js.native
|
||||
@JSImport("@fluencelabs/fluence-network-environment/dist/index.js", "stage")
|
||||
val stage: js.Array[js.Dynamic] = js.native
|
||||
val stage: js.Array[FluenceNode] = js.native
|
||||
|
||||
@js.native
|
||||
@JSImport("@fluencelabs/fluence-network-environment/dist/index.js", "krasnodar")
|
||||
val krasnodar: js.Array[js.Dynamic] = js.native
|
||||
val krasnodar: js.Array[FluenceNode] = js.native
|
||||
|
||||
@js.native
|
||||
@JSImport("@fluencelabs/fluence-network-environment/dist/index.js", "testNet")
|
||||
val testnet: js.Array[js.Dynamic] = js.native
|
||||
val testnet: js.Array[FluenceNode] = js.native
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package aqua.js
|
||||
import aqua.*
|
||||
import aqua.backend.*
|
||||
|
||||
import java.util.Base64
|
||||
import scala.concurrent.Promise
|
||||
import scala.scalajs.js
|
||||
import scala.scalajs.js.JSConverters.*
|
||||
@ -325,6 +326,18 @@ class KeyPair extends js.Object {
|
||||
def toEd25519PrivateKey(): js.typedarray.Uint8Array = js.native
|
||||
}
|
||||
|
||||
object KeyPairOp {
|
||||
|
||||
def toDynamicJSON(kp: KeyPair) = {
|
||||
val encoder = Base64.getEncoder()
|
||||
js.Dynamic.literal(
|
||||
peerId = kp.Libp2pPeerId.toB58String(),
|
||||
secretKey = encoder.encodeToString(kp.toEd25519PrivateKey().toArray.map(s => s.toByte)),
|
||||
publicKey = encoder.encodeToString(kp.Libp2pPeerId.pubKey.bytes.toArray.map(s => s.toByte))
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@js.native
|
||||
@JSImport("@fluencelabs/fluence", "KeyPair")
|
||||
object KeyPair extends js.Object {
|
||||
|
@ -1,6 +1,6 @@
|
||||
package aqua.keypair
|
||||
|
||||
import aqua.js.KeyPair
|
||||
import aqua.js.{KeyPair, KeyPairOp}
|
||||
import cats.Show
|
||||
|
||||
import java.util.Base64
|
||||
@ -8,15 +8,9 @@ import scala.scalajs.js
|
||||
import scala.scalajs.js.JSON
|
||||
|
||||
object KeyPairShow {
|
||||
def stringify(keypair: KeyPair): String = {
|
||||
val encoder = Base64.getEncoder()
|
||||
val kp = js.Dynamic.literal(
|
||||
peerId = keypair.Libp2pPeerId.toB58String(),
|
||||
secretKey = encoder.encodeToString(keypair.toEd25519PrivateKey().toArray.map(s => s.toByte)),
|
||||
publicKey = encoder.encodeToString(keypair.Libp2pPeerId.pubKey.bytes.toArray.map(s => s.toByte)),
|
||||
)
|
||||
|
||||
JSON.stringify(kp, space = 4)
|
||||
def stringify(keypair: KeyPair): String = {
|
||||
JSON.stringify(KeyPairOp.toDynamicJSON(keypair), space = 4)
|
||||
}
|
||||
|
||||
implicit val show: Show[KeyPair] = Show.show(KeyPairShow.stringify)
|
||||
|
@ -4,7 +4,7 @@ import aqua.LogLevelTransformer
|
||||
import aqua.backend.FunctionDef
|
||||
import aqua.builder.{Finisher, ResultPrinter, Service}
|
||||
import aqua.io.OutputPrinter
|
||||
import aqua.js.{CallJsFunction, Fluence, FluenceUtils, PeerConfig}
|
||||
import aqua.js.{CallJsFunction, Fluence, FluenceUtils, KeyPair, KeyPairOp, PeerConfig}
|
||||
import aqua.keypair.KeyPairShow.show
|
||||
import aqua.run.RunCommand.createKeyPair
|
||||
import cats.data.Validated.{invalidNec, validNec}
|
||||
@ -55,8 +55,13 @@ object FuncCaller {
|
||||
)
|
||||
.toFuture
|
||||
_ =
|
||||
if (config.common.showSK) OutputPrinter.print(keyPair.show)
|
||||
else OutputPrinter.print("Your peerId: " + peer.getStatus().peerId)
|
||||
if (config.common.showConfig) {
|
||||
val configJson = KeyPairOp.toDynamicJSON(keyPair)
|
||||
configJson.updateDynamic("relay")(config.common.multiaddr)
|
||||
config.common.timeout.foreach(t => configJson.updateDynamic("timeout")(t))
|
||||
configJson.updateDynamic("log-level")(config.common.logLevel.name)
|
||||
OutputPrinter.print(JSON.stringify(configJson, null, 4))
|
||||
}
|
||||
|
||||
// register all services
|
||||
_ = (services ++ config.argumentGetters.values :+ finisherService).map(_.register(peer))
|
||||
|
@ -1,22 +1,21 @@
|
||||
package aqua.run
|
||||
|
||||
import aqua.FluenceOpts.{
|
||||
logLevelOpt,
|
||||
multiaddrOpt,
|
||||
onOpt,
|
||||
printAir,
|
||||
secretKeyOpt,
|
||||
showSKOpt,
|
||||
timeoutOpt
|
||||
}
|
||||
import aqua.FluenceOpts.{logLevelOpt, onOpt, printAir, secretKeyOpt, showConfigOpt, timeoutOpt}
|
||||
import aqua.builder.{ArgumentGetter, Service}
|
||||
import aqua.AppOpts
|
||||
import aqua.config.ConfigOpts.{Krasnodar, Stage, TestNet}
|
||||
import aqua.js.FluenceEnvironment
|
||||
import com.monovore.decline.Opts
|
||||
import scribe.Level
|
||||
import cats.syntax.flatMap.*
|
||||
import cats.syntax.functor.*
|
||||
import cats.syntax.applicative.*
|
||||
import cats.syntax.apply.*
|
||||
import cats.data.Validated
|
||||
import cats.data.Validated.{invalidNel, validNel}
|
||||
import cats.data.NonEmptyList
|
||||
|
||||
import scala.util.Try
|
||||
|
||||
case class GeneralRunOptions(
|
||||
timeout: Option[Int],
|
||||
@ -25,11 +24,42 @@ case class GeneralRunOptions(
|
||||
on: Option[String],
|
||||
printAir: Boolean,
|
||||
secretKey: Option[Array[Byte]],
|
||||
showSK: Boolean
|
||||
showConfig: Boolean
|
||||
)
|
||||
|
||||
object GeneralRunOptions {
|
||||
|
||||
val multiaddrOpt: Opts[String] =
|
||||
Opts
|
||||
.option[String]("addr", "Relay multiaddress", "a")
|
||||
.mapValidated { s =>
|
||||
if ((s.startsWith("/dns4/") || s.startsWith("/ip4/")) && s.contains("/p2p/12D3")) {
|
||||
validNel(s)
|
||||
} else {
|
||||
Validated.catchNonFatal {
|
||||
val splitted = s.split("-")
|
||||
val index = splitted(1).toInt
|
||||
splitted.head.toLowerCase match {
|
||||
case Krasnodar =>
|
||||
validNel(FluenceEnvironment.krasnodar(index).multiaddr)
|
||||
case TestNet =>
|
||||
validNel(FluenceEnvironment.testnet(index).multiaddr)
|
||||
case Stage =>
|
||||
validNel(FluenceEnvironment.stage(index).multiaddr)
|
||||
case _ =>
|
||||
invalidNel(
|
||||
"Invalid multiaddr format. Run 'aqua config default_peers' for valid multiaddress."
|
||||
)
|
||||
}
|
||||
}.andThen(identity)
|
||||
.leftMap(_ =>
|
||||
NonEmptyList.one(
|
||||
"Invalid multiaddr format. Run 'aqua config default_peers' for valid multiaddress."
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
val commonOpt: Opts[GeneralRunOptions] =
|
||||
(
|
||||
AppOpts.wrapWithOption(timeoutOpt),
|
||||
@ -38,7 +68,7 @@ object GeneralRunOptions {
|
||||
onOpt,
|
||||
printAir,
|
||||
AppOpts.wrapWithOption(secretKeyOpt),
|
||||
showSKOpt
|
||||
showConfigOpt
|
||||
)
|
||||
.mapN(GeneralRunOptions.apply)
|
||||
|
||||
@ -50,7 +80,7 @@ object GeneralRunOptions {
|
||||
onOpt,
|
||||
printAir,
|
||||
secretKeyOpt.map(Some.apply),
|
||||
showSKOpt
|
||||
showConfigOpt
|
||||
)
|
||||
.mapN(GeneralRunOptions.apply)
|
||||
}
|
||||
|
@ -12,19 +12,15 @@ object FluenceOpts {
|
||||
Opts
|
||||
.option[Int]("timeout", "Request timeout in milliseconds", "t")
|
||||
|
||||
val multiaddrOpt: Opts[String] =
|
||||
Opts
|
||||
.option[String]("addr", "Relay multiaddress", "a")
|
||||
|
||||
val onOpt: Opts[Option[String]] =
|
||||
AppOpts.wrapWithOption(
|
||||
Opts
|
||||
.option[String]("on", "Where function will be run. Default: host_peer_id", "o")
|
||||
)
|
||||
|
||||
val showSKOpt: Opts[Boolean] =
|
||||
val showConfigOpt: Opts[Boolean] =
|
||||
Opts
|
||||
.flag("show-sk", "Show your secret key")
|
||||
.flag("show-config", "Print current configuration on start")
|
||||
.map(_ => true)
|
||||
.withDefault(false)
|
||||
|
||||
|
@ -81,7 +81,8 @@ object CompareTypes {
|
||||
else {
|
||||
val tailCmp = compareProducts(ltail, rtail)
|
||||
// If one is >, and another eq, it's >, and vice versa
|
||||
if (headCmp >= 0 && tailCmp >= 0) 1.0
|
||||
if (headCmp == 0 && tailCmp == 0) 0
|
||||
else if (headCmp >= 0 && tailCmp >= 0) 1.0
|
||||
else if (headCmp <= 0 && tailCmp <= 0) -1.0
|
||||
else NaN
|
||||
}
|
||||
@ -129,7 +130,8 @@ object CompareTypes {
|
||||
val cmpDom = apply(ldom, rdom)
|
||||
val cmpCodom = apply(lcodom, rcodom)
|
||||
|
||||
if (cmpDom >= 0 && cmpCodom <= 0) -1.0
|
||||
if (cmpDom == 0 && cmpCodom == 0) 0
|
||||
else if (cmpDom >= 0 && cmpCodom <= 0) -1.0
|
||||
else if (cmpDom <= 0 && cmpCodom >= 0) 1.0
|
||||
else NaN
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user