Skip to content

Commit

Permalink
Handle visibility outside of speedy (#10056)
Browse files Browse the repository at this point in the history
It makes no sense to pass NotVisible to Speedy especially since that
is not how visibility is handled for the Engine. Also it lets us
delete code and I like deleted code.

changelog_begin
changelog_end
  • Loading branch information
cocreature authored Jun 21, 2021
1 parent b796695 commit fb757d8
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ class Engine(val config: EngineConfig = new EngineConfig(LanguageVersion.StableV
return ResultNeedKey(
gk,
result =>
if (cb(SKeyLookupResult(result)))
if (cb(result))
interpretLoop(machine, time)
else
ResultError(Error.Interpretation.ContractKeyNotFound(gk.globalKey)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1188,12 +1188,13 @@ private[lf] object SBuiltin {
private[speedy] sealed abstract class SBUKeyBuiltin(operation: KeyOperation)
extends OnLedgerBuiltin(1) {

private def cacheGlobalLookup(onLedger: OnLedger, gkey: GlobalKey, result: SKeyLookupResult) = {
import PartialTransaction.{KeyActive, KeyInactive}
val keyMapping = result match {
case SKeyLookupResult.Found(cid) => KeyActive(cid)
case SKeyLookupResult.NotFound | SKeyLookupResult.NotVisible => KeyInactive
}
private def cacheGlobalLookup(
onLedger: OnLedger,
gkey: GlobalKey,
result: Option[V.ContractId],
) = {
import PartialTransaction.{KeyActive, KeyInactive, KeyMapping}
val keyMapping: KeyMapping = result.fold[KeyMapping](KeyInactive)(KeyActive(_))
onLedger.ptx = onLedger.ptx.copy(
globalKeyInputs = onLedger.ptx.globalKeyInputs.updated(gkey, keyMapping)
)
Expand Down Expand Up @@ -1247,14 +1248,13 @@ private[lf] object SBuiltin {
// modify keys if the archive was for a key
// already brought into scope.
val activeResult = result match {
case SKeyLookupResult.Found(cid) if onLedger.ptx.consumedBy.contains(cid) =>
SKeyLookupResult.NotFound
case SKeyLookupResult.Found(_) | SKeyLookupResult.NotFound |
SKeyLookupResult.NotVisible =>
case Some(cid) if onLedger.ptx.consumedBy.contains(cid) =>
None
case _ =>
result
}
activeResult match {
case SKeyLookupResult.Found(cid) =>
case Some(cid) =>
onLedger.ptx = onLedger.ptx.copy(
keys = onLedger.ptx.keys.updated(gkey, KeyActive(cid))
)
Expand All @@ -1263,12 +1263,11 @@ private[lf] object SBuiltin {
// We delegate to CtrlImportValue the task to check cid.
operation.handleInputKeyFound(machine, cid)
true
case SKeyLookupResult.NotFound =>
case None =>
onLedger.ptx = onLedger.ptx.copy(
keys = onLedger.ptx.keys.updated(gkey, KeyInactive)
)
operation.handleInputKeyNotFound(machine)
case SKeyLookupResult.NotVisible => false
}
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ object SResult {
committers: Set[Party],
// Callback.
// returns true if machine can continue with the given result.
cb: SKeyLookupResult => Boolean,
cb: Option[ContractId] => Boolean,
) extends SResult

final case class SResultNeedLocalKeyVisible(
Expand Down Expand Up @@ -105,15 +105,4 @@ object SResult {
}
}
}

sealed abstract class SKeyLookupResult
object SKeyLookupResult {
final case class Found(coid: ContractId) extends SKeyLookupResult
final case object NotFound extends SKeyLookupResult
final case object NotVisible extends SKeyLookupResult

def apply(coid: Option[ContractId]): SKeyLookupResult =
coid.fold[SKeyLookupResult](NotFound)(Found)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ object ScenarioRunner {
gk: GlobalKey,
actAs: Set[Party],
readAs: Set[Party],
canContinue: SKeyLookupResult => Boolean,
canContinue: Option[ContractId] => Boolean,
): Either[SError, Unit]
def currentTime: Time.Timestamp
def commit(
Expand Down Expand Up @@ -300,25 +300,21 @@ object ScenarioRunner {
gk: GlobalKey,
actAs: Set[Party],
readAs: Set[Party],
canContinue: SKeyLookupResult => Boolean,
canContinue: Option[ContractId] => Boolean,
): Either[SError, Unit] =
handleUnsafe(lookupKeyUnsafe(gk, actAs, readAs, canContinue))

private def lookupKeyUnsafe(
gk: GlobalKey,
actAs: Set[Party],
readAs: Set[Party],
canContinue: SKeyLookupResult => Boolean,
canContinue: Option[ContractId] => Boolean,
): Unit = {
val effectiveAt = ledger.currentTime
val readers = actAs union readAs

def missingWith(err: SError) =
if (!canContinue(SKeyLookupResult.NotFound))
throw SRunnerException(err)

def notVisibleWith(err: SError) =
if (!canContinue(SKeyLookupResult.NotVisible))
if (!canContinue(None))
throw SRunnerException(err)

ledger.ledgerData.activeKeys.get(gk) match {
Expand All @@ -332,11 +328,11 @@ object ScenarioRunner {
) match {
case ScenarioLedger.LookupOk(_, _, stakeholders) =>
if (!readers.intersect(stakeholders).isEmpty)
// We should always be able to continue with a SKeyLookupResult.Found.
// We should always be able to continue with a Some(_).
// Run to get side effects and assert result.
assert(canContinue(SKeyLookupResult.Found(acoid)))
assert(canContinue(Some(acoid)))
else
notVisibleWith(
throw SRunnerException(
ScenarioErrorContractKeyNotVisible(acoid, gk, actAs, readAs, stakeholders)
)
case ScenarioLedger.LookupContractNotFound(coid) =>
Expand All @@ -351,7 +347,7 @@ object ScenarioRunner {
observers @ _,
stakeholders,
) =>
notVisibleWith(
throw SRunnerException(
ScenarioErrorContractKeyNotVisible(coid, gk, actAs, readAs, stakeholders)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ class CannedLedgerApi(
gk: GlobalKey,
actAs: Set[Party],
readAs: Set[Party],
canContinue: SKeyLookupResult => Boolean,
canContinue: Option[ContractId] => Boolean,
) =
throw new RuntimeException("Keys are not supported in the benchmark")
override def currentTime = throw new RuntimeException("getTime is not supported in the benchmark")
Expand Down

0 comments on commit fb757d8

Please sign in to comment.