Skip to content

Commit

Permalink
Use ScenarioRunner.submit in Daml Script (#10053)
Browse files Browse the repository at this point in the history
* Use ScenarioRunner.submit in Daml Script

changelog_begin
changelog_end

* privatize ledger variable

changelog_begin
changelog_end

* drop space

changelog_begin
changelog_end
  • Loading branch information
cocreature authored Jun 17, 2021
1 parent 3113702 commit febca5d
Show file tree
Hide file tree
Showing 13 changed files with 163 additions and 228 deletions.
6 changes: 3 additions & 3 deletions compiler/damlc/tests/src/DA/Test/ScriptService.hs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ main =
" pure ()"
]
expectScriptSuccess rs (vr "testAssertFail") $ \r ->
matchRegex r "Active contracts: #0:0, #1:0\n\nReturn value: {}\n\n$"
matchRegex r "Active contracts: #0:0, #2:0\n\nReturn value: {}\n\n$"
pure (),
testCase "contract keys" $ do
rs <-
Expand Down Expand Up @@ -667,7 +667,7 @@ main =
, " submitMulti [p0] [p1] (createCmd (T p0 p1))"
]
expectScriptSuccess rs (vr "testSucceed") $ \r ->
matchRegex r "Active contracts: #0:0, #1:0"
matchRegex r "Active contracts: #2:0, #3:0"
expectScriptFailure rs (vr "testFail") $ \r ->
matchRegex r "missing authorization from 'p1'",
testCase "submitTree" $ do
Expand Down Expand Up @@ -816,7 +816,7 @@ main =
matchRegex :: T.Text -> T.Text -> Bool
matchRegex s regex = matchTest (makeRegex regex :: Regex) s

expectScriptSuccess ::
expectScriptSuccess :: HasCallStack =>
-- | The list of script results.
[(VirtualResource, Either T.Text T.Text)] ->
-- | VR of the script
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import scalaz.std.scalaFuture._
import scalaz.syntax.traverse._

import com.daml.lf.archive.Decode.ParseError
import com.daml.lf.data.ImmArray
import com.daml.lf.data.Ref
import com.daml.lf.data.Ref.ModuleName
import com.daml.lf.language.LanguageVersion
Expand Down Expand Up @@ -176,35 +177,33 @@ class ScenarioService(implicit
}
context
.interpretScript(packageId, scenarioId.getName)
.map(_.map { case (ledger, (clientMachine, ledgerMachine), errOrValue) =>
.map(_.map { case (ledger, (clientMachine, submissionCache), errOrValue) =>
val builder = RunScenarioResponse.newBuilder
ledgerMachine.withOnLedger("runScript") { onLedger =>
errOrValue match {
case Left(err) =>
builder.setError(
new Conversions(
context.homePackageId,
ledger,
onLedger.incompleteTransaction(),
clientMachine.traceLog,
onLedger.commitLocation,
ledgerMachine.stackTrace(),
)
.convertScenarioError(err)
errOrValue match {
case Left(err) =>
builder.setError(
new Conversions(
context.homePackageId,
ledger,
submissionCache.ptx.finishIncomplete,
clientMachine.traceLog,
submissionCache.commitLocation,
ImmArray.empty,
)
case Right(value) =>
builder.setResult(
new Conversions(
context.homePackageId,
ledger,
onLedger.incompleteTransaction(),
clientMachine.traceLog,
onLedger.commitLocation,
ledgerMachine.stackTrace(),
)
.convertScenarioResult(value)
.convertScenarioError(err)
)
case Right(value) =>
builder.setResult(
new Conversions(
context.homePackageId,
ledger,
submissionCache.ptx.finishIncomplete,
clientMachine.traceLog,
submissionCache.commitLocation,
ImmArray.empty,
)
}
.convertScenarioResult(value)
)
}
builder.build
})
Expand All @@ -219,6 +218,7 @@ class ScenarioService(implicit
respObs.onNext(resp)
respObs.onCompleted()
case Failure(err) =>
System.err.println(err)
respObs.onError(err)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,9 @@ class Context(val contextId: Context.ContextId, languageVersion: LanguageVersion
ec: ExecutionContext,
esf: ExecutionSequencerFactory,
mat: Materializer,
): Future[Option[(ScenarioLedger, (Speedy.Machine, Speedy.Machine), Either[SError, SValue])]] = {
): Future[Option[
(ScenarioLedger, (Speedy.Machine, IdeLedgerClient.SubmissionCache), Either[SError, SValue])
]] = {
val defns = this.defns
val compiledPackages = PureCompiledPackages(allSignatures, defns, compilerConfig)
val expectedScriptId = DottedName.assertFromString("Daml.Script")
Expand Down Expand Up @@ -220,15 +222,15 @@ class Context(val contextId: Context.ContextId, languageVersion: LanguageVersion
clientMachine.traceLog.add(msg, optLoc)
}
Success(
Some((ledgerClient.scenarioRunner.ledger, (clientMachine, ledgerClient.machine), Left(e)))
Some((ledgerClient.ledger, (clientMachine, ledgerClient.lastSubmission), Left(e)))
)
}

resultF.transform {
case Success(v) =>
Success(
Some(
(ledgerClient.scenarioRunner.ledger, (clientMachine, ledgerClient.machine), Right(v))
(ledgerClient.ledger, (clientMachine, ledgerClient.lastSubmission), Right(v))
)
)
case Failure(e: SError) => handleFailure(e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1310,10 +1310,7 @@ private[lf] object SBuiltin {
commands = args.get(1),
location = optLocation,
mustFail = mustFail,
callback = newValue => {
machine.clearCommit
machine.returnValue = newValue
},
callback = newValue => machine.returnValue = newValue,
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ import scala.util.control.NoStackTrace

private[lf] object Speedy {

// fake participant to generate a new transactionSeed when running scenarios
private[this] val scenarioServiceParticipant =
Ref.ParticipantId.assertFromString("scenario-service")

// Would like these to have zero cost when not enabled. Better still, to be switchable at runtime.
private[this] val enableInstrumentation: Boolean = false
private[this] val enableLightweightStepTracing: Boolean = false
Expand Down Expand Up @@ -603,25 +599,6 @@ private[lf] object Speedy {
println("============================================================")
}

// reinitialize the state of the machine with a new fresh submission seed.
// Should be used only when running scenario
private[lf] def clearCommit: Unit = withOnLedger("clearCommit") { onLedger =>
val freshSeed =
crypto.Hash.deriveTransactionSeed(
onLedger.ptx.context.nextActionChildSeed,
scenarioServiceParticipant,
onLedger.ptx.submissionTime,
)
onLedger.committers = Set.empty
onLedger.commitLocation = None
onLedger.ptx = PartialTransaction.initial(
onLedger.ptx.packageToTransactionVersion,
onLedger.ptx.contractKeyUniqueness,
onLedger.ptx.submissionTime,
InitialSeeding.TransactionSeed(freshSeed),
)
}

// This translates a well-typed LF value (typically coming from the ledger)
// to speedy value and set the control of with the result.
// Note the method does not check the value is well-typed as opposed as
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,6 @@ final case class ScenarioRunner(
case Right(t) => Right(t)
}

private[this] def nextSeed(submissionSeed: crypto.Hash): crypto.Hash =
crypto.Hash.deriveTransactionSeed(
submissionSeed,
Ref.ParticipantId.assertFromString("scenario-service"),
// MinValue makes no sense here but this is what we did before so
// to avoid breaking all tests we keep it for now at least.
Time.Timestamp.MinValue,
)

private def runUnsafe(): (Double, Int, ScenarioLedger, SValue) = {
// NOTE(JM): Written with an imperative loop and exceptions for speed
// and so that we don't need to worry about stack usage.
Expand Down Expand Up @@ -92,7 +83,8 @@ final case class ScenarioRunner(
machine.compiledPackages,
ScenarioLedgerApi(ledger),
committers,
commands,
Set.empty,
SExpr.SEValue(commands),
location,
seed,
)
Expand Down Expand Up @@ -253,6 +245,7 @@ object ScenarioRunner {
def currentTime: Time.Timestamp
def commit(
committers: Set[Party],
readAs: Set[Party],
location: Option[Location],
tx: SubmittedTransaction,
): Either[SError, R]
Expand Down Expand Up @@ -368,12 +361,13 @@ object ScenarioRunner {
override def currentTime = ledger.currentTime
override def commit(
committers: Set[Party],
readAs: Set[Party],
location: Option[Location],
tx: SubmittedTransaction,
): Either[SError, ScenarioLedger.CommitResult] =
ScenarioLedger.commitTransaction(
actAs = committers,
readAs = Set.empty,
readAs = readAs,
effectiveAt = ledger.currentTime,
optLocation = location,
tx = tx,
Expand All @@ -390,15 +384,16 @@ object ScenarioRunner {
compiledPackages: CompiledPackages,
ledger: LedgerApi[R],
committers: Set[Party],
commands: SValue,
readAs: Set[Party],
commands: SExpr,
location: Option[Location],
seed: crypto.Hash,
): SubmissionResult[R] = {
val ledgerMachine = Speedy.Machine(
compiledPackages = compiledPackages,
submissionTime = Time.Timestamp.MinValue,
initialSeeding = InitialSeeding.TransactionSeed(seed),
expr = SExpr.SEApp(SExpr.SEValue(commands), Array(SExpr.SEValue(SValue.SToken))),
expr = SExpr.SEApp(commands, Array(SExpr.SEValue(SValue.SToken))),
globalCids = Set.empty,
committers = committers,
)
Expand All @@ -412,7 +407,7 @@ object ScenarioRunner {
case SResultFinalValue(resultValue) =>
onLedger.ptxInternal.finish match {
case PartialTransaction.CompleteTransaction(tx) =>
ledger.commit(committers, location, tx) match {
ledger.commit(committers, readAs, location, tx) match {
case Left(err) => SubmissionError(err, onLedger.ptxInternal, ledgerMachine.traceLog)
case Right(r) =>
Commit(r, resultValue, onLedger.ptxInternal, ledgerMachine.traceLog)
Expand All @@ -423,17 +418,17 @@ object ScenarioRunner {
case SResultError(err) =>
SubmissionError(err, onLedger.ptxInternal, ledgerMachine.traceLog)
case SResultNeedContract(coid, tid @ _, committers, _, cbPresent) =>
ledger.lookupContract(coid, committers, Set.empty, cbPresent) match {
ledger.lookupContract(coid, committers, readAs, cbPresent) match {
case Left(err) => SubmissionError(err, onLedger.ptxInternal, ledgerMachine.traceLog)
case Right(_) => go()
}
case SResultNeedKey(keyWithMaintainers, committers, cb) =>
ledger.lookupKey(keyWithMaintainers.globalKey, committers, Set.empty, cb) match {
ledger.lookupKey(keyWithMaintainers.globalKey, committers, readAs, cb) match {
case Left(err) => SubmissionError(err, onLedger.ptxInternal, ledgerMachine.traceLog)
case Right(_) => go()
}
case SResultNeedLocalKeyVisible(stakeholders, committers, cb) =>
val visible = SVisibleByKey.fromSubmitters(committers, Set.empty)(stakeholders)
val visible = SVisibleByKey.fromSubmitters(committers, readAs)(stakeholders)
cb(visible)
go()
case SResultNeedTime(callback) =>
Expand All @@ -451,4 +446,13 @@ object ScenarioRunner {
}
go()
}

private[lf] def nextSeed(submissionSeed: crypto.Hash): crypto.Hash =
crypto.Hash.deriveTransactionSeed(
submissionSeed,
Ref.ParticipantId.assertFromString("scenario-service"),
// MinValue makes no sense here but this is what we did before so
// to avoid breaking all tests we keep it for now at least.
Time.Timestamp.MinValue,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ class CollectAuthorityState {
machine.compiledPackages,
api,
committers,
commands,
Set.empty,
SExpr.SEValue(commands),
location,
crypto.Hash.hashPrivateKey(step.toString),
) match {
Expand Down Expand Up @@ -131,7 +132,8 @@ class CollectAuthorityState {
machine.compiledPackages,
api,
committers,
commands,
Set.empty,
SExpr.SEValue(commands),
location,
crypto.Hash.hashPrivateKey(step.toString),
) match {
Expand Down Expand Up @@ -203,6 +205,7 @@ class CannedLedgerApi(

override def commit(
committers: Set[Party],
readAs: Set[Party],
location: Option[Location],
tx: SubmittedTransaction,
) = Right(())
Expand Down
1 change: 0 additions & 1 deletion daml-script/runner/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ da_scala_library(
"//ledger/ledger-api-client",
"//ledger/ledger-api-common",
"//libs-scala/auth-utils",
"//libs-scala/scala-utils",
],
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ object ScriptF {
for ((msg, optLoc) <- client.tracelogIterator) {
machine.traceLog.add(msg, optLoc)
}
client.clearTracelog
}
def addPartyParticipantMapping(party: Party, participant: Participant) = {
_clients =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,5 +324,4 @@ class GrpcLedgerClient(val grpcClient: LedgerClient, val applicationId: Applicat
}

override def tracelogIterator = Iterator.empty
override def clearTracelog = ()
}
Loading

0 comments on commit febca5d

Please sign in to comment.