Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Engine: Remove optionality of contract ID Seeding. #5966

Merged
merged 5 commits into from
May 13, 2020
Merged
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
Next Next commit
Engine: remove optionality of contract ID seeding
CHANGELOG_BEGIN
CHANGELOG_END
  • Loading branch information
remyhaemmerle-da committed May 13, 2020
commit 4bceaf7fc25f92f5124eabb32beb217511e9352f
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ final class Engine {
def submit(
cmds: Commands,
participantId: ParticipantId,
submissionSeed: Option[crypto.Hash],
submissionSeed: crypto.Hash,
): Result[(Tx.Transaction, Tx.Metadata)] = {
val submissionTime = cmds.ledgerEffectiveTime
preprocessor
Expand Down Expand Up @@ -108,7 +108,7 @@ final class Engine {
.getOrElse(sys.error(s"INTERNAL ERROR: Missing dependencies of package $pkgId"))
(pkgIds + pkgId) union transitiveDeps
}
tx -> meta.copy(submissionSeed = submissionSeed, usedPackages = deps)
tx -> meta.copy(submissionSeed = Some(submissionSeed), usedPackages = deps)
}
}
}
Expand Down Expand Up @@ -167,7 +167,7 @@ final class Engine {
ledgerEffectiveTime: Time.Timestamp,
participantId: Ref.ParticipantId,
submissionTime: Time.Timestamp,
submissionSeed: Option[crypto.Hash],
submissionSeed: crypto.Hash,
): Result[Unit] = {
import scalaz.std.option._
import scalaz.syntax.traverse.ToTraverseOps
Expand Down Expand Up @@ -389,15 +389,11 @@ object Engine {
def apply(): Engine = new Engine()

def initialSeeding(
submissionSeed: Option[crypto.Hash],
submissionSeed: crypto.Hash,
participant: Ref.ParticipantId,
submissionTime: Time.Timestamp,
): InitialSeeding =
submissionSeed match {
case None =>
InitialSeeding.NoSeed
case Some(seed) =>
InitialSeeding.TransactionSeed(
crypto.Hash.deriveTransactionSeed(seed, participant, submissionTime))
}
InitialSeeding.TransactionSeed(
crypto.Hash.deriveTransactionSeed(submissionSeed, participant, submissionTime))

}
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ class ContractDiscriminatorFreshnessCheckSpec
commandsReference = "test",
),
participantId = participant,
submissionSeed = Some(submissionSeed),
submissionSeed = submissionSeed,
)
.consume(pcs, pkgs, keys)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ class EngineTest extends WordSpec with Matchers with EitherValues with BazelRunf
.consume(lookupContract, lookupPackage, lookupKey)
res shouldBe 'right
val interpretResult = engine
.submit(Commands(party, ImmArray(command), let, "test"), participant, Some(submissionSeed))
.submit(Commands(party, ImmArray(command), let, "test"), participant, submissionSeed)
.consume(lookupContract, lookupPackage, lookupKey)

"be translated" in {
Expand All @@ -450,7 +450,7 @@ class EngineTest extends WordSpec with Matchers with EitherValues with BazelRunf
"be validated" in {
val Right((tx, meta)) = interpretResult
val validated = engine
.validate(tx, let, participant, meta.submissionTime, Some(submissionSeed))
.validate(tx, let, participant, meta.submissionTime, submissionSeed)
.consume(lookupContract, lookupPackage, lookupKey)
validated match {
case Left(e) =>
Expand All @@ -476,7 +476,7 @@ class EngineTest extends WordSpec with Matchers with EitherValues with BazelRunf
val templateId = Identifier(basicTestsPkgId, "BasicTests:Simple")
val hello = Identifier(basicTestsPkgId, "BasicTests:Hello")
val let = Time.Timestamp.now()
val seeding = Engine.initialSeeding(Some(submissionSeed), participant, let)
val seeding = Engine.initialSeeding(submissionSeed, participant, let)
val cid = toContractId("#BasicTests:Simple:1")
val command =
ExerciseCommand(templateId, cid, "Hello", ValueRecord(Some(hello), ImmArray.empty))
Expand Down Expand Up @@ -505,7 +505,7 @@ class EngineTest extends WordSpec with Matchers with EitherValues with BazelRunf

"be translated" in {
val Right((rtx, _)) = engine
.submit(Commands(party, ImmArray(command), let, "test"), participant, Some(submissionSeed))
.submit(Commands(party, ImmArray(command), let, "test"), participant, submissionSeed)
.consume(lookupContract, lookupPackage, lookupKey)
(tx isReplayedBy rtx) shouldBe true
}
Expand All @@ -527,7 +527,7 @@ class EngineTest extends WordSpec with Matchers with EitherValues with BazelRunf

"be validated" in {
val validated = engine
.validate(tx, let, participant, let, Some(submissionSeed))
.validate(tx, let, participant, let, submissionSeed)
.consume(lookupContract, lookupPackage, lookupKey)
validated match {
case Left(e) =>
Expand Down Expand Up @@ -567,7 +567,7 @@ class EngineTest extends WordSpec with Matchers with EitherValues with BazelRunf

"fail at submission" in {
val submitResult = engine
.submit(Commands(alice, ImmArray(command), let, "test"), participant, Some(submissionSeed))
.submit(Commands(alice, ImmArray(command), let, "test"), participant, submissionSeed)
.consume(lookupContract, lookupPackage, lookupKey)
submitResult.left.value.msg should startWith("dependency error: couldn't find key")
}
Expand All @@ -577,7 +577,7 @@ class EngineTest extends WordSpec with Matchers with EitherValues with BazelRunf
val submissionSeed = hash("exercise-by-key command with existing key")
val templateId = Identifier(basicTestsPkgId, "BasicTests:WithKey")
val let = Time.Timestamp.now()
val seeding = Engine.initialSeeding(Some(submissionSeed), participant, let)
val seeding = Engine.initialSeeding(submissionSeed, participant, let)
val command = ExerciseByKeyCommand(
templateId,
ValueRecord(None, ImmArray((None, ValueParty(alice)), (None, ValueInt64(42)))),
Expand Down Expand Up @@ -609,7 +609,7 @@ class EngineTest extends WordSpec with Matchers with EitherValues with BazelRunf

"be translated" in {
val submitResult = engine
.submit(Commands(alice, ImmArray(command), let, "test"), participant, Some(submissionSeed))
.submit(Commands(alice, ImmArray(command), let, "test"), participant, submissionSeed)
.consume(lookupContract, lookupPackage, lookupKey)
.map(_._1)
(result.map(_._1) |@| submitResult)(_ isReplayedBy _) shouldBe Right(true)
Expand All @@ -625,7 +625,7 @@ class EngineTest extends WordSpec with Matchers with EitherValues with BazelRunf

"be validated" in {
val validated = engine
.validate(tx, let, participant, let, Some(submissionSeed))
.validate(tx, let, participant, let, submissionSeed)
.consume(lookupContract, lookupPackage, lookupKey)
validated match {
case Left(e) =>
Expand Down Expand Up @@ -710,7 +710,7 @@ class EngineTest extends WordSpec with Matchers with EitherValues with BazelRunf

"be validated" in {
val validated = engine
.validate(tx, let, participant, let, Some(submissionSeed))
.validate(tx, let, participant, let, submissionSeed)
.consume(lookupContract, lookupPackage, lookupKey)
validated match {
case Left(e) =>
Expand Down Expand Up @@ -899,7 +899,7 @@ class EngineTest extends WordSpec with Matchers with EitherValues with BazelRunf
ValueRecord(None, ImmArray((Some[Name]("newReceiver"), ValueParty(clara)))))

val Right((tx, txMeta)) = engine
.submit(Commands(bob, ImmArray(command), let, "test"), participant, Some(submissionSeed))
.submit(Commands(bob, ImmArray(command), let, "test"), participant, submissionSeed)
.consume(lookupContract, lookupPackage, lookupKey)

val submissionTime = txMeta.submissionTime
Expand Down Expand Up @@ -1098,7 +1098,7 @@ class EngineTest extends WordSpec with Matchers with EitherValues with BazelRunf
}

val let = Time.Timestamp.now()
val seeding = Engine.initialSeeding(Some(submissionSeed), participant, let)
val seeding = Engine.initialSeeding(submissionSeed, participant, let)

def actFetchActors[Nid, Cid, Val](n: GenNode[Nid, Cid, Val]): Set[Party] = {
n match {
Expand Down Expand Up @@ -1280,10 +1280,7 @@ class EngineTest extends WordSpec with Matchers with EitherValues with BazelRunf
"Lookup",
ValueRecord(None, ImmArray((Some[Name]("n"), ValueInt64(42)))))
val Right((tx, txMeta)) = engine
.submit(
Commands(alice, ImmArray(exerciseCmd), now, "test"),
participant,
Some(submissionSeed))
.submit(Commands(alice, ImmArray(exerciseCmd), now, "test"), participant, submissionSeed)
.consume(lookupContractMap.get, lookupPackage, lookupKey)

val lookupNodes = tx.nodes.collect {
Expand All @@ -1301,10 +1298,7 @@ class EngineTest extends WordSpec with Matchers with EitherValues with BazelRunf
"Lookup",
ValueRecord(None, ImmArray((Some[Name]("n"), ValueInt64(42)))))
val Right((tx, txMeta)) = engine
.submit(
Commands(alice, ImmArray(exerciseCmd), now, "test"),
participant,
Some(submissionSeed))
.submit(Commands(alice, ImmArray(exerciseCmd), now, "test"), participant, submissionSeed)
.consume(lookupContractMap.get, lookupPackage, lookupKey)
val nodeSeedMap = HashMap(txMeta.nodeSeeds.toSeq: _*)

Expand Down Expand Up @@ -1332,10 +1326,7 @@ class EngineTest extends WordSpec with Matchers with EitherValues with BazelRunf
"Lookup",
ValueRecord(None, ImmArray((Some[Name]("n"), ValueInt64(57)))))
val Right((tx, txMeta @ _)) = engine
.submit(
Commands(alice, ImmArray(exerciseCmd), now, "test"),
participant,
Some(submissionSeed))
.submit(Commands(alice, ImmArray(exerciseCmd), now, "test"), participant, submissionSeed)
.consume(lookupContractMap.get, lookupPackage, lookupKey)

val nodeSeedMap = HashMap(txMeta.nodeSeeds.toSeq: _*)
Expand Down Expand Up @@ -1367,7 +1358,7 @@ class EngineTest extends WordSpec with Matchers with EitherValues with BazelRunf
.submit(
Commands(party, ImmArray(command), Time.Timestamp.now(), "test"),
participant,
Some(submissionSeed))
submissionSeed)
.consume(lookupContract, lookupPackage, lookupKey)
}

Expand All @@ -1380,6 +1371,9 @@ class EngineTest extends WordSpec with Matchers with EitherValues with BazelRunf
"fetching contracts that have keys correctly fills in the transaction structure" when {
val fetchedCid = toContractId("#1")
val now = Time.Timestamp.now()
val submissionSeed = crypto.Hash.hashPrivateKey(
"fetching contracts that have keys correctly fills in the transaction structur")
Copy link
Contributor

Choose a reason for hiding this comment

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

...transaction structure

val txSeed = crypto.Hash.deriveTransactionSeed(submissionSeed, participant, now)

"fetched via a fetch" in {

Expand All @@ -1394,7 +1388,7 @@ class EngineTest extends WordSpec with Matchers with EitherValues with BazelRunf
commands = ImmArray(cmd),
ledgerTime = now,
submissionTime = now,
seeding = InitialSeeding.NoSeed,
seeding = InitialSeeding.TransactionSeed(txSeed),
globalCids = Set(fetchedCid),
)
.consume(lookupContractMap.get, lookupPackage, lookupKey)
Expand Down Expand Up @@ -1435,7 +1429,6 @@ class EngineTest extends WordSpec with Matchers with EitherValues with BazelRunf
}

val lookupContractMap = Map(fetchedCid -> withKeyContractInst, fetcherCid -> fetcherInst)
val now = Time.Timestamp.now()

val Right((cmds, globalCids)) = preprocessor
.preprocessCommands(
Expand All @@ -1456,7 +1449,7 @@ class EngineTest extends WordSpec with Matchers with EitherValues with BazelRunf
commands = cmds,
ledgerTime = now,
submissionTime = now,
seeding = InitialSeeding.NoSeed,
seeding = InitialSeeding.TransactionSeed(txSeed),
globalCids = globalCids,
)
.consume(lookupContractMap.get, lookupPackage, lookupKey)
Expand Down Expand Up @@ -1499,7 +1492,7 @@ class EngineTest extends WordSpec with Matchers with EitherValues with BazelRunf
choiceArgument = ValueRecord(None, ImmArray((None, ValueInt64(n.toLong)))),
)
engine
.submit(Commands(party, ImmArray(command), let, "test"), participant, Some(submissionSeed))
.submit(Commands(party, ImmArray(command), let, "test"), participant, submissionSeed)
.consume(_ => None, lookupPackage, _ => None)
}

Expand All @@ -1513,7 +1506,7 @@ class EngineTest extends WordSpec with Matchers with EitherValues with BazelRunf
"be validable in whole" in {
def validate(tx: Tx.Transaction, metaData: Tx.Metadata) =
engine
.validate(tx, let, participant, metaData.submissionTime, Some(submissionSeed))
.validate(tx, let, participant, metaData.submissionTime, submissionSeed)
.consume(_ => None, lookupPackage, _ => None)

run(0).flatMap { case (tx, metaData) => validate(tx, metaData) } shouldBe Right(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import scala.language.implicitConversions
class LargeTransactionTest extends WordSpec with Matchers with BazelRunfiles {

private def hash(s: String, i: Int) =
Some(crypto.Hash.hashPrivateKey(s + ":" + i.toString))
crypto.Hash.hashPrivateKey(s + ":" + i.toString)

private val participant = Ref.ParticipantId.assertFromString("participant")

Expand Down Expand Up @@ -204,7 +204,7 @@ class LargeTransactionTest extends WordSpec with Matchers with BazelRunfiles {
submitter: Party,
cmd: Command,
cmdReference: String,
seed: Option[crypto.Hash]
seed: crypto.Hash
): Tx.Transaction = {
engine
.submit(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ object PlaySpeedy {
sexpr,
compiledPackages,
Time.Timestamp.now(),
InitialSeeding(Some(txSeed)),
InitialSeeding.TransactionSeed(txSeed),
Set.empty,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,13 +368,16 @@ object Speedy {
// reinitialize the state of the machine with a new fresh submission seed.
// Should be used only when running scenario
def clearCommit: Unit = {
val freshSeed = ptx.context.nextChildrenSeed
.map(crypto.Hash.deriveTransactionSeed(_, scenarioServiceParticipant, ptx.submissionTime))
val freshSeed =
crypto.Hash.deriveTransactionSeed(
ptx.context.nextChildrenSeed,
scenarioServiceParticipant,
ptx.submissionTime)
committers = Set.empty
commitLocation = None
ptx = PartialTransaction.initial(
submissionTime = ptx.submissionTime,
InitialSeeding(freshSeed),
InitialSeeding.TransactionSeed(freshSeed),
)
}

Expand Down Expand Up @@ -513,7 +516,7 @@ object Speedy {
def newBuilder(
compiledPackages: CompiledPackages,
submissionTime: Time.Timestamp,
submissionSeed: Option[crypto.Hash],
submissionSeed: crypto.Hash,
): Either[SError, Expr => Machine] = {
val compiler = Compiler(compiledPackages.packages)
Right(
Expand All @@ -522,7 +525,7 @@ object Speedy {
SEApp(compiler.unsafeCompile(expr), Array(SEValue.Token)),
compiledPackages,
submissionTime,
InitialSeeding(submissionSeed),
InitialSeeding.TransactionSeed(submissionSeed),
Set.empty
))
}
Expand All @@ -548,7 +551,7 @@ object Speedy {
compiledPackages: CompiledPackages,
scenario: Boolean,
submissionTime: Time.Timestamp,
transactionSeed: Option[crypto.Hash],
initialSeeding: InitialSeeding,
): Machine = {
val compiler = Compiler(compiledPackages.packages)
val sexpr =
Expand All @@ -561,7 +564,7 @@ object Speedy {
sexpr,
compiledPackages,
submissionTime,
InitialSeeding(transactionSeed),
initialSeeding,
Set.empty,
)
}
Expand Down Expand Up @@ -811,11 +814,11 @@ object Speedy {
final case class SpeedyHungry(result: SResult) extends RuntimeException with NoStackTrace

def deriveTransactionSeed(
submissionSeed: Option[crypto.Hash],
submissionSeed: crypto.Hash,
participant: Ref.ParticipantId,
submissionTime: Time.Timestamp,
): InitialSeeding =
InitialSeeding(
submissionSeed.map(crypto.Hash.deriveTransactionSeed(_, participant, submissionTime)))
InitialSeeding.TransactionSeed(
crypto.Hash.deriveTransactionSeed(submissionSeed, participant, submissionTime))

}
Loading