Skip to content

Commit

Permalink
[Speedy] create Scenario Machine
Browse files Browse the repository at this point in the history
  • Loading branch information
remyhaemmerle-da committed Dec 15, 2022
1 parent 5f01588 commit d14160b
Show file tree
Hide file tree
Showing 24 changed files with 311 additions and 286 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class Context(val contextId: Context.ContextId, languageVersion: LanguageVersion
private val txSeeding =
crypto.Hash.hashPrivateKey(s"scenario-service")

private[this] def buildMachine(defn: SDefinition): Speedy.OffLedgerMachine =
private[this] def buildMachine(defn: SDefinition): Speedy.ScenarioMachine =
Speedy.Machine.fromScenarioSExpr(
PureCompiledPackages(allSignatures, defns, compilerConfig),
defn.body,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import com.daml.lf.data.Ref.{Identifier, PackageId, ParticipantId, Party}
import com.daml.lf.language.Ast._
import com.daml.lf.speedy.{InitialSeeding, Pretty, SError, SValue}
import com.daml.lf.speedy.SExpr.{SEApp, SExpr}
import com.daml.lf.speedy.Speedy.{Machine, OffLedgerMachine, OnLedgerMachine}
import com.daml.lf.speedy.Speedy.{Machine, PureMachine, UpdateMachine}
import com.daml.lf.speedy.SResult._
import com.daml.lf.transaction.{
Node,
Expand Down Expand Up @@ -318,7 +318,7 @@ class Engine(val config: EngineConfig = Engine.StableConfig) {
submissionTime: Time.Timestamp,
seeding: speedy.InitialSeeding,
)(implicit loggingContext: LoggingContext): Result[(SubmittedTransaction, Tx.Metadata)] = {
val machine = OnLedgerMachine(
val machine = UpdateMachine(
compiledPackages = compiledPackages,
submissionTime = submissionTime,
initialSeeding = seeding,
Expand Down Expand Up @@ -366,7 +366,7 @@ class Engine(val config: EngineConfig = Engine.StableConfig) {
// TODO SC remove 'return', notwithstanding a love of unhandled exceptions
@SuppressWarnings(Array("org.wartremover.warts.Return"))
private[engine] def interpretLoop(
machine: OnLedgerMachine,
machine: UpdateMachine,
time: Time.Timestamp,
): Result[(SubmittedTransaction, Tx.Metadata)] = {
def detailMsg = Some(
Expand Down Expand Up @@ -418,7 +418,7 @@ class Engine(val config: EngineConfig = Engine.StableConfig) {
)

case err @ (_: SResultScenarioSubmit | _: SResultScenarioPassTime |
_: SResultScenarioGetParty) =>
_: SResultScenarioGetParty | _: SResultScenarioGetTime) =>
return ResultError(
Error.Interpretation.Internal(
NameOf.qualifiedNameOfCurrentFunc,
Expand All @@ -430,7 +430,7 @@ class Engine(val config: EngineConfig = Engine.StableConfig) {
}

machine.finish match {
case Right(OnLedgerMachine.Result(tx, _, nodeSeeds, globalKeyMapping, disclosedContracts)) =>
case Right(UpdateMachine.Result(tx, _, nodeSeeds, globalKeyMapping, disclosedContracts)) =>
deps(tx).flatMap { deps =>
val meta = Tx.Metadata(
submissionSeed = None,
Expand Down Expand Up @@ -522,13 +522,13 @@ class Engine(val config: EngineConfig = Engine.StableConfig) {
argument: Value,
interfaceId: Identifier,
)(implicit loggingContext: LoggingContext): Result[Versioned[Value]] = {
def interpret(machine: OffLedgerMachine): Result[SValue] = {
def interpret(machine: PureMachine): Result[SValue] = {
machine.run() match {
case SResultFinal(v) => ResultDone(v)
case SResultError(err) => handleError(err, None)
case err @ (_: SResultNeedPackage | _: SResultNeedContract | _: SResultNeedKey |
_: SResultNeedTime | _: SResultScenarioGetParty | _: SResultScenarioPassTime |
_: SResultScenarioSubmit) =>
_: SResultScenarioSubmit | _: SResultScenarioGetTime) =>
ResultError(
Error.Interpretation.Internal(
NameOf.qualifiedNameOfCurrentFunc,
Expand Down
1 change: 0 additions & 1 deletion daml-lf/interpreter/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ da_scala_library(
"//daml-lf/transaction",
"//daml-lf/validation",
"//libs-scala/contextualized-logging",
"//libs-scala/nameof",
"//libs-scala/scala-utils",
],
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package explore
import com.daml.lf.language.PackageInterface
import com.daml.lf.speedy.SExpr0._
import com.daml.lf.speedy.SValue._
import com.daml.lf.speedy.SResult._
import com.daml.lf.speedy.SBuiltin._
import com.daml.lf.speedy.Speedy._
import com.daml.logging.LoggingContext
Expand Down Expand Up @@ -68,23 +67,17 @@ object PlaySpeedy {
Config(names)
}

def runMachine(name: String, machine: Machine, expected: Int): Unit = {
def runMachine(name: String, machine: PureMachine, expected: Int): Unit = {

println(s"example name: $name")

machine.run() match {
case SResultFinal(value) =>
println(s"final-value: $value")
value match {
case SInt64(got) =>
if (got != expected) {
throw new MachineProblem(s"Expected final integer to be $expected, but got $got")
}
case _ =>
throw new MachineProblem(s"Expected final-value to be an integer")
machine.runPure().toTry.get match {
case SInt64(got) =>
if (got != expected) {
throw MachineProblem(s"Expected final integer to be $expected, but got $got")
}
case res =>
throw new MachineProblem(s"Unexpected result from machine $res")
case _ =>
throw MachineProblem(s"Expected final-value to be an integer")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import com.daml.bazeltools.BazelRunfiles.{rlocation}
import com.daml.lf.archive.UniversalArchiveDecoder
import com.daml.lf.data.Ref.{DefinitionRef, Identifier, QualifiedName}
import com.daml.lf.speedy.SExpr._
import com.daml.lf.speedy.SResult._
import com.daml.lf.speedy.SValue._
import com.daml.lf.speedy.Speedy._
import com.daml.logging.LoggingContext
Expand Down Expand Up @@ -85,7 +84,7 @@ object PlaySpeedy {
throw new MachineProblem(s"Unexpecteded result when compiling $x")
}

val machine: Machine = {
val machine: PureMachine = {
println(s"Setup machine for: ${config.funcName}(${config.argValue})")
val expr = {
val ref: DefinitionRef =
Expand All @@ -100,14 +99,8 @@ object PlaySpeedy {
Machine.fromPureSExpr(compiledPackages, expr)
}

val result: SValue = {
println("Run...")
machine.run() match {
case SResultFinal(value) => value
case res => throw new MachineProblem(s"Unexpected result from machine $res")
}
}

println("Run...")
val result = machine.runPure().toTry.get
println(s"Final-value: $result")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ private[speedy] object PhaseOne {
stacktracing: StackTraceMode,
)

private val SEGetTime = SEBuiltin(SBGetTime)
private val SUGetTime = SEBuiltin(SBUGetTime)

private val SSGetTime = SEBuiltin(SBSGetTime)

private val SBEToTextNumeric = SEAbs(1, SEBuiltin(SBToText))

Expand Down Expand Up @@ -757,7 +759,7 @@ private[lf] final class PhaseOne(
}
}
case UpdateGetTime =>
Return(SEGetTime)
Return(SUGetTime)
case UpdateLookupByKey(RetrieveByKey(templateId, key)) =>
compileExp(env, key) { key =>
Return(t.LookupByKeyDefRef(templateId)(key))
Expand Down Expand Up @@ -845,7 +847,7 @@ private[lf] final class PhaseOne(
case ScenarioMustFailAt(partyE, updateE, _retType @ _) =>
compileCommit(env, partyE, updateE, optLoc, mustFail = true)
case ScenarioGetTime =>
Return(SEGetTime)
Return(SSGetTime)
case ScenarioGetParty(e) =>
compileGetParty(env, e)
case ScenarioPass(relTime) =>
Expand Down Expand Up @@ -911,7 +913,7 @@ private[lf] final class PhaseOne(
compileExp(env, body) { body =>
let(env, body) { (bodyPos, env) =>
unaryFunction(env) { (tokenPos, env) =>
Return(SBSPure(env.toSEVar(bodyPos), env.toSEVar(tokenPos)))
Return(SBPure(env.toSEVar(bodyPos), env.toSEVar(tokenPos)))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ private[lf] object Pretty {
)
case SBUCreate => text("$create")
case SBFetchAny => text("$fetchAny")
case SBGetTime => text("$getTime")
case SBUGetTime | SBSGetTime => text("$getTime")
case _ => str(x)
}
case SEAppOnlyFunIsAtomic(fun, args) =>
Expand Down
Loading

0 comments on commit d14160b

Please sign in to comment.