From c2c22f8dea7d34632e318f54ca9af382ee16183d Mon Sep 17 00:00:00 2001 From: Hubert Slojewski Date: Tue, 30 Nov 2021 15:25:18 +0100 Subject: [PATCH] kvutils: Protos no longer depend on the Daml-LF transaction proto [KVL-1166] (#11909) CHANGELOG_BEGIN - [Integration Kit] kvutils protos no longer depend on the Daml-LF transaction proto CHANGELOG_END --- ledger/participant-state/kvutils/BUILD.bazel | 1 - .../kvutils/store/events/transaction.proto | 8 +-- .../state/kvutils/store/state.proto | 5 +- .../state/kvutils/Conversions.scala | 55 +++++++++++-------- .../state/kvutils/KeyValueCommitting.scala | 6 +- .../state/kvutils/KeyValueConsumption.scala | 6 +- .../state/kvutils/KeyValueSubmission.scala | 2 +- .../participant/state/kvutils/Raw.scala | 7 +++ .../DamlTransactionEntrySummary.scala | 4 +- .../transaction/TransactionCommitter.scala | 17 +++--- .../ModelConformanceValidator.scala | 10 ++-- .../participant/state/kvutils/KVTest.scala | 4 +- .../state/kvutils/TestHelpers.scala | 2 +- .../state/kvutils/ConversionsSpec.scala | 45 ++++++--------- .../TransactionCommitterSpec.scala | 52 +++++++----------- .../ModelConformanceValidatorSpec.scala | 6 +- .../batch/ConflictDetectionSpec.scala | 3 +- .../kvutils/tools/BUILD.bazel | 1 - .../kvutils/tools/codec-benchmark/README.md | 2 +- .../benchmark/BenchmarkWithLedgerExport.scala | 5 +- .../scala/com/daml/lf/benchmark/package.scala | 8 +-- .../kvutils/tools/engine/replay/Replay.scala | 21 ++----- 22 files changed, 131 insertions(+), 139 deletions(-) diff --git a/ledger/participant-state/kvutils/BUILD.bazel b/ledger/participant-state/kvutils/BUILD.bazel index fdd54387384b..f3398c1381d9 100644 --- a/ledger/participant-state/kvutils/BUILD.bazel +++ b/ledger/participant-state/kvutils/BUILD.bazel @@ -117,7 +117,6 @@ da_scala_library( "//daml-lf/engine", "//daml-lf/language", "//daml-lf/transaction", - "//daml-lf/transaction:transaction_proto_java", "//daml-lf/transaction:value_proto_java", "//daml-lf/transaction-test-lib", "//ledger-api/grpc-definitions:ledger_api_proto_scala", diff --git a/ledger/participant-state/kvutils/src/main/protobuf/com/daml/ledger/participant/state/kvutils/store/events/transaction.proto b/ledger/participant-state/kvutils/src/main/protobuf/com/daml/ledger/participant/state/kvutils/store/events/transaction.proto index 4e3efef1dc9f..41fad4d92dca 100644 --- a/ledger/participant-state/kvutils/src/main/protobuf/com/daml/ledger/participant/state/kvutils/store/events/transaction.proto +++ b/ledger/participant-state/kvutils/src/main/protobuf/com/daml/ledger/participant/state/kvutils/store/events/transaction.proto @@ -19,7 +19,6 @@ option csharp_namespace = "Com.Daml.Ledger.Participant.State.KVUtils.Store.Event import "google/protobuf/duration.proto"; import "google/protobuf/timestamp.proto"; -import "com/daml/lf/transaction.proto"; import "com/daml/ledger/participant/state/kvutils/store/events/rejection_reason.proto"; message DamlSubmitterInfo { @@ -40,8 +39,8 @@ message DamlSubmitterInfo { // with submitter information and metadata (see Ledger API `commands.proto` for // their source). message DamlTransactionEntry { - // The original submitted transaction, with relative identifiers. - com.daml.lf.transaction.Transaction transaction = 1; + // The originally submitted transaction (see com.daml.lf.transaction.Transaction). + bytes raw_transaction = 1; // The submitting party and metadata about the submitted command. // The submitting party is authorized against the submitting participant @@ -87,7 +86,8 @@ message DamlTransactionBlindingInfo { message DivulgenceEntry { string contract_id = 1; repeated string divulged_to_local_parties = 2; - com.daml.lf.transaction.ContractInstance contract_instance = 3; + // The contract instance stored as bytes (see com.daml.lf.transaction.ContractInstance). + bytes raw_contract_instance = 3; } // Disclosure, specified in terms of local transaction node IDs. diff --git a/ledger/participant-state/kvutils/src/main/protobuf/com/daml/ledger/participant/state/kvutils/store/state.proto b/ledger/participant-state/kvutils/src/main/protobuf/com/daml/ledger/participant/state/kvutils/store/state.proto index 87493e565297..3754a10e14a3 100644 --- a/ledger/participant-state/kvutils/src/main/protobuf/com/daml/ledger/participant/state/kvutils/store/state.proto +++ b/ledger/participant-state/kvutils/src/main/protobuf/com/daml/ledger/participant/state/kvutils/store/state.proto @@ -12,7 +12,6 @@ import "google/protobuf/empty.proto"; import "google/protobuf/timestamp.proto"; import "com/daml/daml_lf_dev/daml_lf.proto"; import "com/daml/ledger/participant/state/kvutils/store/events/configuration.proto"; -import "com/daml/lf/transaction.proto"; import "com/daml/lf/value.proto"; @@ -117,8 +116,8 @@ message DamlContractState { // The contract key set by the contract. Optional. DamlContractKey contract_key = 6; - // The contract instance. - com.daml.lf.transaction.ContractInstance contract_instance = 7; + // The contract instance stored as bytes (see com.daml.lf.transaction.ContractInstance). + bytes raw_contract_instance = 7; } message DamlContractKey { diff --git a/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/participant/state/kvutils/Conversions.scala b/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/participant/state/kvutils/Conversions.scala index e3ae6a126089..e14c06f2e92c 100644 --- a/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/participant/state/kvutils/Conversions.scala +++ b/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/participant/state/kvutils/Conversions.scala @@ -257,13 +257,16 @@ private[state] object Conversions { private def assertEncode[X](context: => String, x: Either[ValueCoder.EncodeError, X]): X = x.fold(err => throw Err.EncodeError(context, err.errorMessage), identity) - def encodeTransaction(tx: VersionedTransaction): TransactionOuterClass.Transaction = - assertEncode( + def encodeTransaction(tx: VersionedTransaction): Raw.Transaction = { + val transaction = assertEncode( "Transaction", TransactionCoder.encodeTransaction(TransactionCoder.NidEncoder, ValueCoder.CidEncoder, tx), ) + Raw.Transaction(transaction.toByteString) + } - def decodeTransaction(tx: TransactionOuterClass.Transaction): VersionedTransaction = + def decodeTransaction(rawTx: Raw.Transaction): VersionedTransaction = { + val tx = TransactionOuterClass.Transaction.parseFrom(rawTx.bytes) assertDecode( "Transaction", TransactionCoder @@ -273,6 +276,7 @@ private[state] object Conversions { tx, ), ) + } def decodeVersionedValue(protoValue: ValueOuterClass.VersionedValue): VersionedValue = assertDecode( @@ -281,21 +285,26 @@ private[state] object Conversions { ) def decodeContractInstance( - coinst: TransactionOuterClass.ContractInstance - ): Value.VersionedContractInstance = + rawContractInstance: Raw.ContractInstance + ): Value.VersionedContractInstance = { + val contractInstance = + TransactionOuterClass.ContractInstance.parseFrom(rawContractInstance.bytes) assertDecode( "ContractInstance", TransactionCoder - .decodeVersionedContractInstance(ValueCoder.CidDecoder, coinst), + .decodeVersionedContractInstance(ValueCoder.CidDecoder, contractInstance), ) + } def encodeContractInstance( coinst: Value.VersionedContractInstance - ): TransactionOuterClass.ContractInstance = - assertEncode( + ): Raw.ContractInstance = { + val contractInstance = assertEncode( "ContractInstance", TransactionCoder.encodeContractInstance(ValueCoder.CidEncoder, coinst), ) + Raw.ContractInstance(contractInstance.toByteString) + } def contractIdStructOrStringToStateKey[A]( coidStruct: ValueOuterClass.ContractId @@ -320,7 +329,7 @@ private[state] object Conversions { */ def encodeBlindingInfo( blindingInfo: BlindingInfo, - divulgedContracts: Map[ContractId, TransactionOuterClass.ContractInstance], + divulgedContracts: Map[ContractId, Raw.ContractInstance], ): DamlTransactionBlindingInfo = DamlTransactionBlindingInfo.newBuilder .addAllDisclosures(encodeDisclosure(blindingInfo.disclosure).asJava) @@ -354,32 +363,32 @@ private[state] object Conversions { def extractDivulgedContracts( damlTransactionBlindingInfo: DamlTransactionBlindingInfo - ): Either[Seq[String], Map[ContractId, Value.VersionedContractInstance]] = { + ): Either[Seq[String], Map[ContractId, Raw.ContractInstance]] = { val divulgences = damlTransactionBlindingInfo.getDivulgencesList.asScala.toVector if (divulgences.isEmpty) { Right(Map.empty) } else { val resultAccumulator: Either[Seq[String], mutable.Builder[ - (ContractId, Value.VersionedContractInstance), - Map[ContractId, Value.VersionedContractInstance], + (ContractId, Raw.ContractInstance), + Map[ContractId, Raw.ContractInstance], ]] = Right(Map.newBuilder) divulgences .foldLeft(resultAccumulator) { case (Right(contractInstanceIndex), divulgenceEntry) => - if (divulgenceEntry.hasContractInstance) { - val contractId = decodeContractId(divulgenceEntry.getContractId) - val contractInstance = decodeContractInstance(divulgenceEntry.getContractInstance) - Right(contractInstanceIndex += (contractId -> contractInstance)) - } else { + if (divulgenceEntry.getRawContractInstance.isEmpty) { Left(Vector(divulgenceEntry.getContractId)) + } else { + val contractId = decodeContractId(divulgenceEntry.getContractId) + val rawContractInstance = Raw.ContractInstance(divulgenceEntry.getRawContractInstance) + Right(contractInstanceIndex += (contractId -> rawContractInstance)) } case (Left(missingContracts), divulgenceEntry) => // If populated by an older version of the KV WriteService, the contract instances will be missing. // Hence, we assume that, if one is missing, all are and return the list of missing ids. - if (divulgenceEntry.hasContractInstance) { - Left(missingContracts) - } else { + if (divulgenceEntry.getRawContractInstance.isEmpty) { Left(missingContracts :+ divulgenceEntry.getContractId) + } else { + Left(missingContracts) } } .map(_.result()) @@ -554,17 +563,17 @@ private[state] object Conversions { private def encodeDivulgenceEntry( contractId: ContractId, divulgedTo: Set[Ref.Party], - contractInstance: TransactionOuterClass.ContractInstance, + rawContractInstance: Raw.ContractInstance, ): DivulgenceEntry = DivulgenceEntry.newBuilder .setContractId(contractIdToString(contractId)) .addAllDivulgedToLocalParties(encodeParties(divulgedTo).asJava) - .setContractInstance(contractInstance) + .setRawContractInstance(rawContractInstance.bytes) .build private def encodeDivulgence( divulgence: Relation[ContractId, Ref.Party], - divulgedContractsIndex: Map[ContractId, TransactionOuterClass.ContractInstance], + divulgedContractsIndex: Map[ContractId, Raw.ContractInstance], ): List[DivulgenceEntry] = divulgence.toList .sortBy(_._1.coid) diff --git a/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/participant/state/kvutils/KeyValueCommitting.scala b/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/participant/state/kvutils/KeyValueCommitting.scala index 171606a004c8..aa7aa96187e0 100644 --- a/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/participant/state/kvutils/KeyValueCommitting.scala +++ b/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/participant/state/kvutils/KeyValueCommitting.scala @@ -214,13 +214,15 @@ object KeyValueCommitting { transactionEntry: DamlTransactionEntry ): Set[DamlStateKey] = { val outputs = Set.newBuilder[DamlStateKey] + val transaction = + TransactionOuterClass.Transaction.parseFrom(transactionEntry.getRawTransaction) val txVersion = - TransactionCoder.decodeVersion(transactionEntry.getTransaction.getVersion) match { + TransactionCoder.decodeVersion(transaction.getVersion) match { case Right(value) => value case Left(err) => throw Err.DecodeError("Transaction", err.errorMessage) } - transactionEntry.getTransaction.getNodesList.asScala.foreach { node => + transaction.getNodesList.asScala.foreach { node => val nodeVersion = TransactionCoder.decodeNodeVersion(txVersion, node) match { case Right(value) => value case Left(err) => throw Err.DecodeError("Node", err.errorMessage) diff --git a/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/participant/state/kvutils/KeyValueConsumption.scala b/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/participant/state/kvutils/KeyValueConsumption.scala index c0dec772358c..0d4c43871d50 100644 --- a/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/participant/state/kvutils/KeyValueConsumption.scala +++ b/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/participant/state/kvutils/KeyValueConsumption.scala @@ -265,7 +265,8 @@ object KeyValueConsumption { txEntry: DamlTransactionEntry, recordTime: Timestamp, )(implicit loggingContext: LoggingContext): Update.TransactionAccepted = { - val transaction = Conversions.decodeTransaction(txEntry.getTransaction) + val rawTransaction = Raw.Transaction(txEntry.getRawTransaction) + val transaction = Conversions.decodeTransaction(rawTransaction) val hexTxId = parseLedgerString("TransactionId")( BaseEncoding.base16.encode(entryId.toByteArray) ) @@ -319,7 +320,8 @@ object KeyValueConsumption { if (!damlTransactionBlindingInfo.getDivulgencesList.isEmpty) { Conversions.extractDivulgedContracts(damlTransactionBlindingInfo) match { case Right(divulgedContractsIndex) => - divulgedContractsIndex.view.map { case (contractId, contractInstance) => + divulgedContractsIndex.view.map { case (contractId, rawContractInstance) => + val contractInstance = Conversions.decodeContractInstance(rawContractInstance) DivulgedContract(contractId, contractInstance) }.toList case Left(missingContractIds) => diff --git a/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/participant/state/kvutils/KeyValueSubmission.scala b/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/participant/state/kvutils/KeyValueSubmission.scala index 89b30ab68afc..d61c7a6d4a10 100644 --- a/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/participant/state/kvutils/KeyValueSubmission.scala +++ b/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/participant/state/kvutils/KeyValueSubmission.scala @@ -78,7 +78,7 @@ class KeyValueSubmission(metrics: Metrics) { .addAllInputDamlState(contractKeyStates.asJava) .setTransactionEntry( DamlTransactionEntry.newBuilder - .setTransaction(Conversions.encodeTransaction(tx)) + .setRawTransaction(Conversions.encodeTransaction(tx).bytes) .setSubmitterInfo(encodedSubInfo) .setLedgerEffectiveTime(buildTimestamp(meta.ledgerEffectiveTime)) .setWorkflowId(meta.workflowId.getOrElse("")) diff --git a/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/participant/state/kvutils/Raw.scala b/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/participant/state/kvutils/Raw.scala index a4d21875d096..df26134acb5c 100644 --- a/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/participant/state/kvutils/Raw.scala +++ b/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/participant/state/kvutils/Raw.scala @@ -66,4 +66,11 @@ object Raw { type StateEntry = (StateKey, Envelope) + final case class Transaction(override val bytes: ByteString) extends Bytes + + object Transaction extends Companion[Transaction] + + final case class ContractInstance(override val bytes: ByteString) extends Bytes + + object ContractInstance extends Companion[ContractInstance] } diff --git a/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/participant/state/kvutils/committer/transaction/DamlTransactionEntrySummary.scala b/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/participant/state/kvutils/committer/transaction/DamlTransactionEntrySummary.scala index 1d2d0248fcff..1a4bbb9f3802 100644 --- a/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/participant/state/kvutils/committer/transaction/DamlTransactionEntrySummary.scala +++ b/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/participant/state/kvutils/committer/transaction/DamlTransactionEntrySummary.scala @@ -3,7 +3,7 @@ package com.daml.ledger.participant.state.kvutils.committer.transaction -import com.daml.ledger.participant.state.kvutils.Conversions +import com.daml.ledger.participant.state.kvutils.{Conversions, Raw} import com.daml.ledger.participant.state.kvutils.Conversions.parseTimestamp import com.daml.ledger.participant.state.kvutils.store.events.{ DamlSubmitterInfo, @@ -44,6 +44,6 @@ private[transaction] object DamlTransactionEntrySummary { ): DamlTransactionEntrySummary = new DamlTransactionEntrySummary( submission, - Conversions.decodeTransaction(submission.getTransaction), + Conversions.decodeTransaction(Raw.Transaction(submission.getRawTransaction)), ) } diff --git a/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/participant/state/kvutils/committer/transaction/TransactionCommitter.scala b/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/participant/state/kvutils/committer/transaction/TransactionCommitter.scala index c0e3847a23b9..7494768a4663 100644 --- a/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/participant/state/kvutils/committer/transaction/TransactionCommitter.scala +++ b/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/participant/state/kvutils/committer/transaction/TransactionCommitter.scala @@ -24,7 +24,7 @@ import com.daml.ledger.participant.state.kvutils.store.{ DamlStateValue, } import com.daml.ledger.participant.state.kvutils.wire.DamlSubmission -import com.daml.ledger.participant.state.kvutils.{Conversions, Err} +import com.daml.ledger.participant.state.kvutils.{Conversions, Err, Raw} import com.daml.lf.data.Ref.Party import com.daml.lf.engine.{Blinding, Engine} import com.daml.lf.transaction.{BlindingInfo, TransactionOuterClass} @@ -160,7 +160,8 @@ private[kvutils] class TransactionCommitter( commitContext: CommitContext, transactionEntry: DamlTransactionEntrySummary, )(implicit loggingContext: LoggingContext): StepResult[DamlTransactionEntrySummary] = { - val transaction = transactionEntry.submission.getTransaction + val transaction = + TransactionOuterClass.Transaction.parseFrom(transactionEntry.submission.getRawTransaction) val nodes = transaction.getNodesList.asScala val nodeMap: Map[String, TransactionOuterClass.Node] = nodes.view.map(n => n.getNodeId -> n).toMap @@ -217,7 +218,7 @@ private[kvutils] class TransactionCommitter( .build() val newTransactionEntry = transactionEntry.submission.toBuilder - .setTransaction(newTransaction) + .setRawTransaction(newTransaction.toByteString) .build() StepContinue(DamlTransactionEntrySummary(newTransactionEntry)) @@ -260,7 +261,7 @@ private[kvutils] class TransactionCommitter( commitContext: CommitContext, )(implicit loggingContext: LoggingContext - ): Map[ContractId, TransactionOuterClass.ContractInstance] = { + ): Map[ContractId, Raw.ContractInstance] = { val localContracts = transactionEntry.transaction.localContracts val consumedContracts = transactionEntry.transaction.consumedContracts val contractKeys = transactionEntry.transaction.updatedContractKeys @@ -270,8 +271,8 @@ private[kvutils] class TransactionCommitter( cs.setActiveAt(buildTimestamp(transactionEntry.ledgerEffectiveTime)) val localDisclosure = blindingInfo.disclosure(nid) cs.addAllLocallyDisclosedTo((localDisclosure: Iterable[String]).asJava) - cs.setContractInstance( - Conversions.encodeContractInstance(createNode.versionedCoinst) + cs.setRawContractInstance( + Conversions.encodeContractInstance(createNode.versionedCoinst).bytes ) createNode.key.foreach { keyWithMaintainers => cs.setContractKey( @@ -299,7 +300,7 @@ private[kvutils] class TransactionCommitter( } // Update contract state of divulged contracts. val divulgedContractsBuilder = { - val builder = Map.newBuilder[ContractId, TransactionOuterClass.ContractInstance] + val builder = Map.newBuilder[ContractId, Raw.ContractInstance] builder.sizeHint(blindingInfo.divulgence.size) builder } @@ -307,7 +308,7 @@ private[kvutils] class TransactionCommitter( for ((coid, parties) <- blindingInfo.divulgence) { val key = contractIdToStateKey(coid) val cs = getContractState(commitContext, key) - divulgedContractsBuilder += (coid -> cs.getContractInstance) + divulgedContractsBuilder += (coid -> Raw.ContractInstance(cs.getRawContractInstance)) val divulged: Set[String] = cs.getDivulgedToList.asScala.toSet val newDivulgences: Set[String] = parties.toSet[String] -- divulged if (newDivulgences.nonEmpty) { diff --git a/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/participant/state/kvutils/committer/transaction/validation/ModelConformanceValidator.scala b/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/participant/state/kvutils/committer/transaction/validation/ModelConformanceValidator.scala index d02bea82e43a..3d8d23ca69dd 100644 --- a/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/participant/state/kvutils/committer/transaction/validation/ModelConformanceValidator.scala +++ b/ledger/participant-state/kvutils/src/main/scala/com/daml/ledger/participant/state/kvutils/committer/transaction/validation/ModelConformanceValidator.scala @@ -16,7 +16,7 @@ import com.daml.ledger.participant.state.kvutils.committer.transaction.{ } import com.daml.ledger.participant.state.kvutils.committer.{CommitContext, StepContinue, StepResult} import com.daml.ledger.participant.state.kvutils.store.{DamlContractState, DamlStateValue} -import com.daml.ledger.participant.state.kvutils.{Conversions, Err} +import com.daml.ledger.participant.state.kvutils.{Conversions, Err, Raw} import com.daml.lf.archive import com.daml.lf.data.Ref.PackageId import com.daml.lf.data.Time.Timestamp @@ -146,9 +146,11 @@ private[transaction] class ModelConformanceValidator(engine: Engine, metrics: Me ): Option[Value.VersionedContractInstance] = commitContext .read(contractIdToStateKey(contractId)) - .map(_.getContractState) - .map(_.getContractInstance) - .map(Conversions.decodeContractInstance) + .map { stateValue => + val rawContractInstance = + Raw.ContractInstance(stateValue.getContractState.getRawContractInstance) + Conversions.decodeContractInstance(rawContractInstance) + } // Helper to lookup package from the state. The package contents are stored in the [[DamlLogEntry]], // which we find by looking up the Daml state entry at `DamlStateKey(packageId = pkgId)`. diff --git a/ledger/participant-state/kvutils/src/test/lib/scala/com/daml/ledger/participant/state/kvutils/KVTest.scala b/ledger/participant-state/kvutils/src/test/lib/scala/com/daml/ledger/participant/state/kvutils/KVTest.scala index 0ea6639f4cd5..8a289410810c 100644 --- a/ledger/participant-state/kvutils/src/test/lib/scala/com/daml/ledger/participant/state/kvutils/KVTest.scala +++ b/ledger/participant-state/kvutils/src/test/lib/scala/com/daml/ledger/participant/state/kvutils/KVTest.scala @@ -210,7 +210,9 @@ object KVTest { state.damlState .get(Conversions.contractIdToStateKey(contractId)) .map { v => - Conversions.decodeContractInstance(v.getContractState.getContractInstance) + Conversions.decodeContractInstance( + Raw.ContractInstance(v.getContractState.getRawContractInstance) + ) }, packages = state.uploadedPackages.get, keys = globalKey => diff --git a/ledger/participant-state/kvutils/src/test/lib/scala/com/daml/ledger/participant/state/kvutils/TestHelpers.scala b/ledger/participant-state/kvutils/src/test/lib/scala/com/daml/ledger/participant/state/kvutils/TestHelpers.scala index 842676866837..8d4882228a74 100644 --- a/ledger/participant-state/kvutils/src/test/lib/scala/com/daml/ledger/participant/state/kvutils/TestHelpers.scala +++ b/ledger/participant-state/kvutils/src/test/lib/scala/com/daml/ledger/participant/state/kvutils/TestHelpers.scala @@ -68,7 +68,7 @@ object TestHelpers { tx: SubmittedTransaction, ): DamlTransactionEntry = DamlTransactionEntry.newBuilder - .setTransaction(Conversions.encodeTransaction(tx)) + .setRawTransaction(Conversions.encodeTransaction(tx).bytes) .setSubmitterInfo( DamlSubmitterInfo.newBuilder .setCommandId("commandId") diff --git a/ledger/participant-state/kvutils/src/test/suite/scala/com/daml/ledger/participant/state/kvutils/ConversionsSpec.scala b/ledger/participant-state/kvutils/src/test/suite/scala/com/daml/ledger/participant/state/kvutils/ConversionsSpec.scala index 89498ba1c4b8..c4f759278c63 100644 --- a/ledger/participant-state/kvutils/src/test/suite/scala/com/daml/ledger/participant/state/kvutils/ConversionsSpec.scala +++ b/ledger/participant-state/kvutils/src/test/suite/scala/com/daml/ledger/participant/state/kvutils/ConversionsSpec.scala @@ -33,9 +33,8 @@ import com.daml.lf.data.Ref.Party import com.daml.lf.data.Relation.Relation import com.daml.lf.data.Time.{Timestamp => LfTimestamp} import com.daml.lf.engine.Error -import com.daml.lf.transaction.test.TransactionBuilder import com.daml.lf.transaction.{BlindingInfo, NodeId, TransactionOuterClass, TransactionVersion} -import com.daml.lf.value.Value.{ContractId, ContractInstance, ValueText} +import com.daml.lf.value.Value.ContractId import com.daml.lf.value.ValueOuterClass import com.daml.logging.{ContextualizedLogger, LoggingContext} import com.fasterxml.jackson.databind.ObjectMapper @@ -65,8 +64,8 @@ class ConversionsSpec extends AnyWordSpec with Matchers with OptionValues { encodeBlindingInfo( wronglySortedBlindingInfo, Map( - contractId0 -> apiContractInstance0, - contractId1 -> apiContractInstance1, + contractId0 -> rawApiContractInstance0, + contractId1 -> rawApiContractInstance1, ), ) shouldBe correctlySortedEncodedBlindingInfo } @@ -83,8 +82,8 @@ class ConversionsSpec extends AnyWordSpec with Matchers with OptionValues { maybeDivulgedContracts shouldBe Right( Map( - contractId0 -> lfContractInstance0, - contractId1 -> lfContractInstance1, + contractId0 -> rawApiContractInstance0, + contractId1 -> rawApiContractInstance1, ) ) } @@ -593,12 +592,12 @@ class ConversionsSpec extends AnyWordSpec with Matchers with OptionValues { private def newDivulgenceEntry( contractId: String, parties: List[String], - contractInstance: TransactionOuterClass.ContractInstance, + rawContractInstance: Raw.ContractInstance, ) = DivulgenceEntry.newBuilder .setContractId(contractId) .addAllDivulgedToLocalParties(parties.asJava) - .setContractInstance(contractInstance) + .setRawContractInstance(rawContractInstance.bytes) .build private lazy val party0: Party = Party.assertFromString("party0") @@ -607,6 +606,9 @@ class ConversionsSpec extends AnyWordSpec with Matchers with OptionValues { private lazy val contractId1: ContractId = ContractId.V1(wronglySortedHashes.head) private lazy val node0: NodeId = NodeId(0) private lazy val node1: NodeId = NodeId(1) + private lazy val rawApiContractInstance0 = rawApiContractInstance("contract 0") + private lazy val rawApiContractInstance1 = rawApiContractInstance("contract 1") + private lazy val wronglySortedPartySet = ListSet(party1, party0) private lazy val wronglySortedHashes: List[Hash] = List(crypto.Hash.hashPrivateKey("hash0"), crypto.Hash.hashPrivateKey("hash1")).sorted.reverse @@ -619,14 +621,6 @@ class ConversionsSpec extends AnyWordSpec with Matchers with OptionValues { divulgence = wronglySortedDivulgence, ) - private lazy val Seq( - (apiContractInstance0, lfContractInstance0), - (apiContractInstance1, lfContractInstance1), - ) = - Seq("contract 0", "contract 1").map(discriminator => - apiContractInstance(discriminator) -> lfContractInstance(discriminator) - ) - private lazy val correctlySortedParties = List(party0, party1) private lazy val correctlySortedPartiesAsStrings = correctlySortedParties.asInstanceOf[List[String]] @@ -643,12 +637,12 @@ class ConversionsSpec extends AnyWordSpec with Matchers with OptionValues { newDivulgenceEntry( contractId0.coid, correctlySortedPartiesAsStrings, - apiContractInstance0, + rawApiContractInstance0, ), newDivulgenceEntry( contractId1.coid, correctlySortedPartiesAsStrings, - apiContractInstance1, + rawApiContractInstance1, ), ).asJava ) @@ -670,8 +664,8 @@ class ConversionsSpec extends AnyWordSpec with Matchers with OptionValues { deduplicationKey.toByteArray } - private def apiContractInstance(discriminator: String) = - TransactionOuterClass.ContractInstance + private def rawApiContractInstance(discriminator: String) = { + val contractInstance = TransactionOuterClass.ContractInstance .newBuilder() .setTemplateId( ValueOuterClass.Identifier @@ -689,15 +683,8 @@ class ConversionsSpec extends AnyWordSpec with Matchers with OptionValues { ) ) .build() - - private def lfContractInstance(discriminator: String) = - new TransactionBuilder(_ => txVersion).versionContract( - ContractInstance( - Ref.Identifier.assertFromString("some:template:name"), - ValueText(discriminator), - "", - ) - ) + Raw.ContractInstance(contractInstance.toByteString) + } private def finalReasonDetails( finalReason: CommandRejected.FinalReason diff --git a/ledger/participant-state/kvutils/src/test/suite/scala/com/daml/ledger/participant/state/kvutils/committer/transaction/TransactionCommitterSpec.scala b/ledger/participant-state/kvutils/src/test/suite/scala/com/daml/ledger/participant/state/kvutils/committer/transaction/TransactionCommitterSpec.scala index 41a4a990bb4c..67074149fcb2 100644 --- a/ledger/participant-state/kvutils/src/test/suite/scala/com/daml/ledger/participant/state/kvutils/committer/transaction/TransactionCommitterSpec.scala +++ b/ledger/participant-state/kvutils/src/test/suite/scala/com/daml/ledger/participant/state/kvutils/committer/transaction/TransactionCommitterSpec.scala @@ -25,7 +25,7 @@ import com.daml.lf.engine.Engine import com.daml.lf.transaction._ import com.daml.lf.transaction.test.TransactionBuilder import com.daml.lf.value.Value.{ContractId, ValueRecord, ValueText} -import com.daml.lf.value.{Value, ValueOuterClass} +import com.daml.lf.value.Value import com.daml.logging.LoggingContext import com.daml.metrics.Metrics import com.google.protobuf.Duration @@ -139,7 +139,8 @@ class TransactionCommitterSpec actual match { case StepContinue(logEntry) => - val transaction = logEntry.submission.getTransaction + val transaction = + TransactionOuterClass.Transaction.parseFrom(logEntry.submission.getRawTransaction) transaction.getRootsList.asScala should contain theSameElementsInOrderAs Seq( "Exercise-1", "Create-1", @@ -221,7 +222,7 @@ class TransactionCommitterSpec val builder = TransactionBuilder() val cid = builder.newCid - val (expectedContractInstance, txEntry) = txEntryWithDivulgedContract(builder, cid) + val (expectedRawContractInstance, txEntry) = txEntryWithDivulgedContract(builder, cid) val txEntryBuilder = txEntry.toBuilder // deduplication duration is mandatory as we set the context dedup entry during blinding txEntryBuilder.getSubmitterInfoBuilder.setDeduplicationDuration( @@ -243,12 +244,12 @@ class TransactionCommitterSpec ( entry.getContractId, entry.getDivulgedToLocalPartiesList.asScala.toSet, - entry.getContractInstance, + entry.getRawContractInstance, ) ) actualDivulgencesList should contain theSameElementsAs { - Vector((cid.coid, Set("ChoiceObserver"), expectedContractInstance)) + Vector((cid.coid, Set("ChoiceObserver"), expectedRawContractInstance)) } val actualDisclosureList = @@ -363,7 +364,7 @@ object TransactionCommitterSpec { .addAllRoots(roots.asJava) .addAllNodes(nodes.asJava) .build() - val outTx = aDamlTransactionEntry.toBuilder.setTransaction(tx).build() + val outTx = aDamlTransactionEntry.toBuilder.setRawTransaction(tx.toByteString).build() DamlTransactionEntrySummary(outTx) } @@ -371,15 +372,13 @@ object TransactionCommitterSpec { builder: TransactionBuilder, divulgedContractId: Value.ContractId, ) = { - val moduleName = "DummyModule" - val templateName = "DummyTemplate" - - val argValue = "DummyText" + val templateId = "DummyModule:DummyTemplate" + val textArgument = ValueText("DummyText") val createNode = builder.create( id = divulgedContractId, - templateId = s"$moduleName:$templateName", - argument = ValueText(argValue), + templateId = templateId, + argument = textArgument, signatories = Set("Alice"), observers = Set.empty, key = None, @@ -396,27 +395,18 @@ object TransactionCommitterSpec { builder.add(createNode) builder.add(exerciseNode) - val expectedContractInstance = - TransactionOuterClass.ContractInstance - .newBuilder() - .setTemplateId( - ValueOuterClass.Identifier - .newBuilder() - .setPackageId(defaultPackageId) - .addModuleName(moduleName) - .addName(templateName) - ) - .setArgVersioned( - ValueOuterClass.VersionedValue - .newBuilder() - .setVersion(TransactionVersion.StableVersions.max.protoValue) - .setValue( - ValueOuterClass.Value.newBuilder().setText(argValue).build().toByteString - ) + val expectedRawContractInstance = Conversions + .encodeContractInstance( + Value.VersionedContractInstance( + version = TransactionVersion.StableVersions.max, + template = templateId, + arg = textArgument, + agreementText = "", ) - .build() + ) + .bytes - expectedContractInstance -> createTransactionEntry( + expectedRawContractInstance -> createTransactionEntry( List("aSubmitter"), SubmittedTransaction(builder.build()), ) diff --git a/ledger/participant-state/kvutils/src/test/suite/scala/com/daml/ledger/participant/state/kvutils/committer/transaction/validation/ModelConformanceValidatorSpec.scala b/ledger/participant-state/kvutils/src/test/suite/scala/com/daml/ledger/participant/state/kvutils/committer/transaction/validation/ModelConformanceValidatorSpec.scala index 44eb1dff3722..78d78a16a040 100644 --- a/ledger/participant-state/kvutils/src/test/suite/scala/com/daml/ledger/participant/state/kvutils/committer/transaction/validation/ModelConformanceValidatorSpec.scala +++ b/ledger/participant-state/kvutils/src/test/suite/scala/com/daml/ledger/participant/state/kvutils/committer/transaction/validation/ModelConformanceValidatorSpec.scala @@ -97,7 +97,7 @@ class ModelConformanceValidatorSpec DamlTransactionEntry.newBuilder .setSubmissionSeed(aSubmissionSeed) .setLedgerEffectiveTime(Conversions.buildTimestamp(ledgerEffectiveTime)) - .setTransaction(Conversions.encodeTransaction(aTransaction._1)) + .setRawTransaction(Conversions.encodeTransaction(aTransaction._1).bytes) .build ) @@ -422,7 +422,7 @@ object ModelConformanceValidatorSpec { .setContractState( DamlContractState .newBuilder() - .setContractInstance( + .setRawContractInstance( ContractInstance .newBuilder() .setTemplateId( @@ -440,6 +440,8 @@ object ModelConformanceValidatorSpec { ValueOuterClass.Value.newBuilder().setText("dummyValue").build().toByteString ) ) + .build() + .toByteString ) .build() ) diff --git a/ledger/participant-state/kvutils/src/test/suite/scala/com/daml/ledger/validator/batch/ConflictDetectionSpec.scala b/ledger/participant-state/kvutils/src/test/suite/scala/com/daml/ledger/validator/batch/ConflictDetectionSpec.scala index c60ec79f529f..a2cafa3d3ad7 100644 --- a/ledger/participant-state/kvutils/src/test/suite/scala/com/daml/ledger/validator/batch/ConflictDetectionSpec.scala +++ b/ledger/participant-state/kvutils/src/test/suite/scala/com/daml/ledger/validator/batch/ConflictDetectionSpec.scala @@ -266,8 +266,7 @@ class ConflictDetectionSpec extends AsyncWordSpec with Matchers with Inside with .setRecordTime(com.google.protobuf.Timestamp.getDefaultInstance) builder.getTransactionEntryBuilder .setLedgerEffectiveTime(com.google.protobuf.Timestamp.getDefaultInstance) - .getTransactionBuilder - .addRoots("foo") + .setRawTransaction(ByteString.copyFromUtf8("invalid transaction")) builder.build } diff --git a/ledger/participant-state/kvutils/tools/BUILD.bazel b/ledger/participant-state/kvutils/tools/BUILD.bazel index b76e688d3e8b..fec9fb3e048f 100644 --- a/ledger/participant-state/kvutils/tools/BUILD.bazel +++ b/ledger/participant-state/kvutils/tools/BUILD.bazel @@ -168,7 +168,6 @@ da_scala_library( "//daml-lf/engine", "//daml-lf/language", "//daml-lf/transaction", - "//daml-lf/transaction:transaction_proto_java", "//daml-lf/transaction-test-lib", "//ledger/participant-state/kvutils", "//ledger/participant-state/kvutils:daml_kvutils_proto_java", diff --git a/ledger/participant-state/kvutils/tools/codec-benchmark/README.md b/ledger/participant-state/kvutils/tools/codec-benchmark/README.md index 87874b674372..89c4ab1af891 100644 --- a/ledger/participant-state/kvutils/tools/codec-benchmark/README.md +++ b/ledger/participant-state/kvutils/tools/codec-benchmark/README.md @@ -61,7 +61,7 @@ you run the benchmarks that are relevant to you with the proper settings. #### Terminology -* _serialize_: translate from a Protobuf object to its serialized from +* _serialize_: translate from a Protobuf object to its serialized form * _deserialize_: translate a serialized Protobuf message into its object representation * _encode_: translate a Protobuf object to a Daml-LF object * _decode_: translate a Daml-LF object to a Protobuf object diff --git a/ledger/participant-state/kvutils/tools/codec-benchmark/src/benchmark/scala/com/daml/lf/benchmark/BenchmarkWithLedgerExport.scala b/ledger/participant-state/kvutils/tools/codec-benchmark/src/benchmark/scala/com/daml/lf/benchmark/BenchmarkWithLedgerExport.scala index 58ab21ac0d34..a7454549bd90 100644 --- a/ledger/participant-state/kvutils/tools/codec-benchmark/src/benchmark/scala/com/daml/lf/benchmark/BenchmarkWithLedgerExport.scala +++ b/ledger/participant-state/kvutils/tools/codec-benchmark/src/benchmark/scala/com/daml/lf/benchmark/BenchmarkWithLedgerExport.scala @@ -10,6 +10,7 @@ import com.daml.ledger.participant.state.kvutils.export.ProtobufBasedLedgerDataI import com.daml.ledger.participant.state.kvutils.wire.DamlSubmission import com.daml.ledger.participant.state.kvutils.{Envelope, Raw} import com.daml.lf.archive.Decode +import com.daml.lf.transaction.TransactionOuterClass import org.openjdk.jmh.annotations.{Param, Scope, Setup, State} import scala.jdk.CollectionConverters._ @@ -46,7 +47,9 @@ abstract class BenchmarkWithLedgerExport { } case Envelope.SubmissionMessage(submission) if submission.getPayloadCase == DamlSubmission.PayloadCase.TRANSACTION_ENTRY => - builder += submission.getTransactionEntry.getTransaction + builder += TransactionOuterClass.Transaction.parseFrom( + submission.getTransactionEntry.getRawTransaction + ) case Envelope.SubmissionBatchMessage(batch) => for (submission <- batch.getSubmissionsList.asScala) { decodeEnvelope(Raw.Envelope(submission.getSubmission)) diff --git a/ledger/participant-state/kvutils/tools/codec-benchmark/src/benchmark/scala/com/daml/lf/benchmark/package.scala b/ledger/participant-state/kvutils/tools/codec-benchmark/src/benchmark/scala/com/daml/lf/benchmark/package.scala index cb97ed638062..1dadac69dcce 100644 --- a/ledger/participant-state/kvutils/tools/codec-benchmark/src/benchmark/scala/com/daml/lf/benchmark/package.scala +++ b/ledger/participant-state/kvutils/tools/codec-benchmark/src/benchmark/scala/com/daml/lf/benchmark/package.scala @@ -9,8 +9,7 @@ import com.daml.lf.transaction.TransactionCoder.{ decodeTransaction, encodeTransaction, } -import com.daml.lf.transaction.TransactionOuterClass.Transaction -import com.daml.lf.transaction.VersionedTransaction +import com.daml.lf.transaction.{TransactionOuterClass, VersionedTransaction} import com.daml.lf.value.ValueCoder._ import com.daml.lf.value.ValueOuterClass import com.google.protobuf.ByteString @@ -22,9 +21,10 @@ package object benchmark { * It's the in-memory representation of the Protobuf message that * describes a transaction, not its serialized form. */ - private[lf] type EncodedTransaction = Transaction + private[lf] type EncodedTransaction = TransactionOuterClass.Transaction private[lf] object EncodedTransaction { - def deserialize(bytes: ByteString): EncodedTransaction = Transaction.parseFrom(bytes) + def deserialize(bytes: ByteString): EncodedTransaction = + TransactionOuterClass.Transaction.parseFrom(bytes) } /** This is the output of a successful call to diff --git a/ledger/participant-state/kvutils/tools/engine-replay/src/replay/scala/ledger/participant/state/kvutils/tools/engine/replay/Replay.scala b/ledger/participant-state/kvutils/tools/engine-replay/src/replay/scala/ledger/participant/state/kvutils/tools/engine/replay/Replay.scala index 53c51f881ed5..fc57530bf172 100644 --- a/ledger/participant-state/kvutils/tools/engine-replay/src/replay/scala/ledger/participant/state/kvutils/tools/engine/replay/Replay.scala +++ b/ledger/participant-state/kvutils/tools/engine-replay/src/replay/scala/ledger/participant/state/kvutils/tools/engine/replay/Replay.scala @@ -12,21 +12,15 @@ import com.daml.ledger.participant.state.kvutils.export.{ SubmissionInfo, } import com.daml.ledger.participant.state.kvutils.wire.DamlSubmission -import com.daml.ledger.participant.state.kvutils.{Envelope, Raw} +import com.daml.ledger.participant.state.kvutils.{Conversions, Envelope, Raw} import com.daml.lf.archive.UniversalArchiveDecoder import com.daml.lf.crypto import com.daml.lf.data._ import com.daml.lf.engine.{Engine, EngineConfig, Error} import com.daml.lf.language.{Ast, LanguageVersion, Util => AstUtil} -import com.daml.lf.transaction.{ - GlobalKey, - GlobalKeyWithMaintainers, - Node, - SubmittedTransaction, - TransactionCoder => TxCoder, -} +import com.daml.lf.transaction.{GlobalKey, GlobalKeyWithMaintainers, Node, SubmittedTransaction} import com.daml.lf.value.Value.ContractId -import com.daml.lf.value.{Value, ValueCoder => ValCoder} +import com.daml.lf.value.Value import scala.collection.compat._ import scala.collection.compat.immutable.LazyList @@ -127,13 +121,8 @@ private[replay] object Replay { submission.getPayloadCase match { case DamlSubmission.PayloadCase.TRANSACTION_ENTRY => val entry = submission.getTransactionEntry - val tx = TxCoder - .decodeTransaction( - TxCoder.NidDecoder, - ValCoder.CidDecoder, - submission.getTransactionEntry.getTransaction, - ) - .fold(err => sys.error(err.toString), SubmittedTransaction(_)) + val rawTransaction = Raw.Transaction(submission.getTransactionEntry.getRawTransaction) + val tx = SubmittedTransaction(Conversions.decodeTransaction(rawTransaction)) LazyList( TxEntry( tx = tx,