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

Ledger: decouple BD transaction serialization library from archive library. #10218

Merged
merged 1 commit into from
Jul 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package com.daml.platform.db.migration.translation

import java.io.InputStream

import com.daml.lf.archive.Reader
import com.daml.lf.transaction.{TransactionCoder, TransactionOuterClass}
import com.daml.lf.value.Value.{ContractId, ContractInst, VersionedValue}
import com.daml.lf.value.ValueCoder
Expand Down Expand Up @@ -39,7 +38,7 @@ private[migration] object ContractSerializer extends ContractSerializer {
.decodeVersionedContractInstance[ContractId](
ValueCoder.CidDecoder,
TransactionOuterClass.ContractInstance.parseFrom(
Reader.damlLfCodedInputStream(stream)
ValueSerializer.lfValueCodedInputStream(stream)
),
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package com.daml.platform.db.migration.translation

import java.io.InputStream

import com.daml.lf.archive.Reader
import com.daml.lf.data.Ref.LedgerString
import com.daml.lf.transaction.{CommittedTransaction, TransactionCoder, TransactionOuterClass}
import com.daml.lf.value.ValueCoder
Expand Down Expand Up @@ -50,7 +49,7 @@ private[migration] object TransactionSerializer extends TransactionSerializer {
TransactionCoder.EventIdDecoder(trId),
ValueCoder.CidDecoder,
TransactionOuterClass.Transaction
.parseFrom(Reader.damlLfCodedInputStream(stream)),
.parseFrom(ValueSerializer.lfValueCodedInputStream(stream)),
)
)
.map(CommittedTransaction(_))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ package db.migration.translation

import java.io.InputStream

import com.daml.lf.archive.Reader
import com.daml.lf.value.Value.{ContractId, VersionedValue}
import com.daml.lf.value.{ValueCoder, ValueOuterClass}
import com.google.protobuf.CodedInputStream
import org.slf4j.LoggerFactory

private[migration] object ValueSerializer {
Expand Down Expand Up @@ -49,6 +49,14 @@ private[migration] object ValueSerializer {
errorContext: => String,
): Array[Byte] = store.serialization.ValueSerializer.serializeValue(value, errorContext)

private[this] val PROTOBUF_RECURSION_LIMIT = 1000

def lfValueCodedInputStream(stream: InputStream) = {
val cos = CodedInputStream.newInstance(stream)
cos.setRecursionLimit(PROTOBUF_RECURSION_LIMIT)
cos
}

private[this] def deserializeValueHelper(
stream: InputStream,
errorContext: => Option[String],
Expand All @@ -57,9 +65,7 @@ private[migration] object ValueSerializer {
ValueCoder
.decodeVersionedValue(
ValueCoder.CidDecoder,
ValueOuterClass.VersionedValue.parseFrom(
Reader.damlLfCodedInputStream(stream)
),
ValueOuterClass.VersionedValue.parseFrom(lfValueCodedInputStream(stream)),
)
)
.fold(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ package com.daml.platform.store.serialization

import java.io.InputStream

import com.daml.lf.archive.Reader
import com.daml.lf.value.Value.{ContractId, VersionedValue}
import com.daml.lf.value.{ValueCoder, ValueOuterClass}
import com.google.protobuf.CodedInputStream

private[platform] object ValueSerializer {

Expand All @@ -19,15 +19,22 @@ private[platform] object ValueSerializer {
.encodeVersionedValue(ValueCoder.CidEncoder, value)
.fold(error => sys.error(s"$errorContext (${error.errorMessage})"), _.toByteArray)

private[this] val PROTOBUF_RECURSION_LIMIT = 1000

private[this] def lfValueCodedInputStream(stream: InputStream) = {
val cos = CodedInputStream.newInstance(stream)
cos.setRecursionLimit(PROTOBUF_RECURSION_LIMIT)
cos
}

private def deserializeValueHelper(
stream: InputStream,
errorContext: => Option[String],
): VersionedValue[ContractId] =
ValueCoder
.decodeVersionedValue(
ValueCoder.CidDecoder,
ValueOuterClass.VersionedValue
.parseFrom(Reader.damlLfCodedInputStream(stream, Reader.PROTOBUF_RECURSION_LIMIT)),
ValueOuterClass.VersionedValue.parseFrom(lfValueCodedInputStream(stream)),
)
.fold(
error =>
Expand Down