Skip to content

Commit 6447ce5

Browse files
Switch to relying on package names instead of module names (#1976)
--------- Signed-off-by: Oriol Muñoz <[email protected]>
1 parent 77e8546 commit 6447ce5

File tree

22 files changed

+798
-556
lines changed

22 files changed

+798
-556
lines changed

apps/app/src/main/scala/org/lfdecentralizedtrust/splice/console/LedgerApiExtensions.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ trait LedgerApiExtensions extends AppendedClues with Matchers {
404404
predicate: TC => Boolean = (_: TC) => true,
405405
): Seq[TC] = {
406406
val filterIdentifier =
407-
PackageQualifiedName.getFromResources(templateCompanion.getTemplateIdWithPackageId)
407+
PackageQualifiedName.fromJavaCodegenCompanion(templateCompanion)
408408
val templateId = TemplateId(
409409
s"#${filterIdentifier.packageName}",
410410
filterIdentifier.qualifiedName.moduleName,
@@ -443,8 +443,7 @@ trait LedgerApiExtensions extends AppendedClues with Matchers {
443443
](templateCompanion: javaapi.data.codegen.ContractCompanion[TC, TCid, T])(
444444
partyId: PartyId
445445
): Seq[CreatedEvent] = {
446-
val filterIdentifier =
447-
PackageQualifiedName.getFromResources(templateCompanion.getTemplateIdWithPackageId)
446+
val filterIdentifier = PackageQualifiedName.fromJavaCodegenCompanion(templateCompanion)
448447
val templateId = TemplateId(
449448
s"#${filterIdentifier.packageName}",
450449
filterIdentifier.qualifiedName.moduleName,

apps/app/src/test/scala/org/lfdecentralizedtrust/splice/integration/tests/DecentralizedSynchronizerMigrationIntegrationTest.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,11 +1025,11 @@ class DecentralizedSynchronizerMigrationIntegrationTest
10251025
pageSize = 1000,
10261026
templates = Some(
10271027
Vector(
1028-
DsoRules.TEMPLATE_ID_WITH_PACKAGE_ID,
1029-
AmuletRules.TEMPLATE_ID_WITH_PACKAGE_ID,
1030-
AnsRules.TEMPLATE_ID_WITH_PACKAGE_ID,
1028+
DsoRules.COMPANION,
1029+
AmuletRules.COMPANION,
1030+
AnsRules.COMPANION,
10311031
).map(
1032-
PackageQualifiedName.getFromResources(_)
1032+
PackageQualifiedName.fromJavaCodegenCompanion
10331033
)
10341034
),
10351035
)

apps/app/src/test/scala/org/lfdecentralizedtrust/splice/integration/tests/ScanTimeBasedIntegrationTest.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -510,8 +510,8 @@ class ScanTimeBasedIntegrationTest
510510
migrationId,
511511
templates = Some(
512512
Vector(
513-
PackageQualifiedName.getFromResources(Amulet.TEMPLATE_ID_WITH_PACKAGE_ID),
514-
PackageQualifiedName.getFromResources(AnsEntry.TEMPLATE_ID_WITH_PACKAGE_ID),
513+
PackageQualifiedName.fromJavaCodegenCompanion(Amulet.COMPANION),
514+
PackageQualifiedName.fromJavaCodegenCompanion(AnsEntry.COMPANION),
515515
)
516516
),
517517
partyIds = Some(Vector(aliceUserParty)),

apps/common/src/main/resources/db/migration/canton-network/postgres/stable/V047__package_name_acs_stores.sql

Lines changed: 363 additions & 0 deletions
Large diffs are not rendered by default.

apps/common/src/main/scala/org/lfdecentralizedtrust/splice/store/DbVotesStoreQueryBuilder.scala

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import org.lfdecentralizedtrust.splice.codegen.java.splice.dsorules.VoteRequest
1111
import org.lfdecentralizedtrust.splice.store.db.AcsQueries.AcsStoreId
1212
import org.lfdecentralizedtrust.splice.store.db.TxLogQueries.TxLogStoreId
1313
import org.lfdecentralizedtrust.splice.store.db.{AcsQueries, TxLogQueries}
14-
import org.lfdecentralizedtrust.splice.util.QualifiedName
1514
import slick.dbio.{Effect, NoStream}
1615
import slick.jdbc.canton.ActionBasedSQLInterpolation.Implicits.actionBasedSQLInterpolationCanton
1716
import slick.sql.SqlStreamingAction
@@ -110,10 +109,8 @@ trait DbVotesAcsStoreQueryBuilder extends AcsQueries with LimitHelpers with Name
110109
acsTableName,
111110
acsStoreId,
112111
domainMigrationId,
113-
where = (sql""" template_id_qualified_name = ${QualifiedName(
114-
VoteRequest.TEMPLATE_ID_WITH_PACKAGE_ID
115-
)}
116-
and #$trackingCidColumnName in """ ++ voteRequestTrackingCidsSql).toActionBuilder,
112+
VoteRequest.COMPANION,
113+
where = (sql""" #$trackingCidColumnName in """ ++ voteRequestTrackingCidsSql).toActionBuilder,
117114
orderLimit = sql"""limit ${sqlLimit(limit)}""",
118115
)
119116
}
@@ -133,10 +130,8 @@ trait DbVotesAcsStoreQueryBuilder extends AcsQueries with LimitHelpers with Name
133130
acsTableName,
134131
acsStoreId,
135132
domainMigrationId,
136-
where = (sql""" template_id_qualified_name = ${QualifiedName(
137-
VoteRequest.TEMPLATE_ID_WITH_PACKAGE_ID
138-
)}
139-
and #$trackingCidColumnName = $voteRequestCid """).toActionBuilder,
133+
VoteRequest.COMPANION,
134+
where = (sql""" #$trackingCidColumnName = $voteRequestCid """).toActionBuilder,
140135
).headOption
141136
}
142137

apps/common/src/main/scala/org/lfdecentralizedtrust/splice/store/MultiDomainAcsStore.scala

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ package org.lfdecentralizedtrust.splice.store
66
import org.apache.pekko.NotUsed
77
import org.apache.pekko.stream.scaladsl.Source
88
import com.daml.ledger.api.v2.transaction_filter.{EventFormat, CumulativeFilter}
9-
import org.lfdecentralizedtrust.splice.util.Contract.Companion.Template as TemplateCompanion
9+
import org.lfdecentralizedtrust.splice.util.Contract.Companion.{
10+
Interface,
11+
Template as TemplateCompanion,
12+
}
1013
import com.daml.ledger.javaapi.data.{CreatedEvent, ExercisedEvent, Identifier, Template}
1114
import com.daml.ledger.javaapi.data.codegen.{ContractId, DamlRecord}
1215
import com.daml.metrics.api.MetricsContext
@@ -26,6 +29,7 @@ import org.lfdecentralizedtrust.splice.util.{
2629
Contract,
2730
ContractWithState,
2831
PackageQualifiedName,
32+
QualifiedName,
2933
TemplateJsonDecoder,
3034
}
3135
import org.lfdecentralizedtrust.splice.util.PrettyInstances.*
@@ -171,22 +175,6 @@ trait MultiDomainAcsStore extends HasIngestionSink with AutoCloseable with Named
171175
traceContext: TraceContext,
172176
): Future[Seq[Contract[TCid, T]]]
173177

174-
/** At most 1000 (`notOnDomainsTotalLimit`) contracts sorted by a hash of
175-
* contract ID and participant ID.
176-
*
177-
* The idea is that different apps making the same migration on different
178-
* participants will split the work better, while preserving determinism of a
179-
* specific running app for fault-tolerance. For the former to happen, the
180-
* position of a contract on one list must have no correlation with that on
181-
* another list; that is why the contract ID by itself cannot be used by
182-
* itself as the source of the sort key.
183-
*/
184-
def listAssignedContractsNotOnDomainN(
185-
excludedDomain: SynchronizerId,
186-
companions: Seq[ConstrainedTemplate],
187-
limit: notOnDomainsTotalLimit.type = notOnDomainsTotalLimit,
188-
)(implicit tc: TraceContext): Future[Seq[AssignedContract[?, ?]]]
189-
190178
private[splice] def listExpiredFromPayloadExpiry[C, TCid <: ContractId[T], T <: Template](
191179
companion: C
192180
)(implicit
@@ -470,7 +458,7 @@ object MultiDomainAcsStore extends StoreErrors {
470458
TemplateFilter[TCid, T, R],
471459
) =
472460
(
473-
PackageQualifiedName.getFromResources(templateCompanion.getTemplateIdWithPackageId),
461+
PackageQualifiedName.fromJavaCodegenCompanion(templateCompanion),
474462
TemplateFilter(
475463
ev => {
476464
val c = Contract.fromCreatedEvent(templateCompanion)(ev)
@@ -597,6 +585,8 @@ object MultiDomainAcsStore extends StoreErrors {
597585

598586
def typeId(companion: C): Identifier
599587

588+
def packageQualifiedName(companion: C): PackageQualifiedName
589+
600590
def toContractId(companion: C, contractId: String): TCid
601591

602592
protected def fromJson(
@@ -619,6 +609,13 @@ object MultiDomainAcsStore extends StoreErrors {
619609
override def typeId(companion: Contract.Companion.Template[TCid, T]): Identifier =
620610
companion.getTemplateIdWithPackageId
621611

612+
override def packageQualifiedName(
613+
companion: TemplateCompanion[TCid, T]
614+
): PackageQualifiedName = PackageQualifiedName(
615+
companion.PACKAGE_NAME,
616+
QualifiedName(companion.getTemplateIdWithPackageId),
617+
)
618+
622619
override def toContractId(companion: Companion.Template[TCid, T], contractId: String): TCid =
623620
companion.toContractId(new ContractId[T](contractId))
624621

@@ -652,6 +649,13 @@ object MultiDomainAcsStore extends StoreErrors {
652649
override def typeId(companion: Contract.Companion.Interface[ICid, Marker, View]): Identifier =
653650
companion.getTemplateIdWithPackageId
654651

652+
override def packageQualifiedName(
653+
companion: Interface[ICid, Marker, View]
654+
): PackageQualifiedName = PackageQualifiedName(
655+
companion.PACKAGE_NAME,
656+
QualifiedName(companion.getTemplateIdWithPackageId),
657+
)
658+
655659
override def toContractId(
656660
companion: Companion.Interface[ICid, Marker, View],
657661
contractId: String,

apps/common/src/main/scala/org/lfdecentralizedtrust/splice/store/db/AcsJdbcTypes.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,11 @@ trait AcsJdbcTypes {
218218
protected implicit val qualifiedNameGetResult: GetResult[QualifiedName] =
219219
GetResult.GetString.andThen { s => QualifiedName.assertFromString(s) }
220220

221+
protected implicit val packageQualifiedNameGetResult: GetResult[PackageQualifiedName] =
222+
implicitly[GetResult[(QualifiedName, String)]].andThen { case (qualifiedName, packageName) =>
223+
PackageQualifiedName(packageName, qualifiedName)
224+
}
225+
221226
protected implicit lazy val qualifiedNameJdbcType: JdbcType[QualifiedName] =
222227
MappedColumnType.base[QualifiedName, String](
223228
{ _.toString }, {

apps/common/src/main/scala/org/lfdecentralizedtrust/splice/store/db/AcsQueries.scala

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,33 @@ import scalaz.{@@, Tag}
2626
import slick.jdbc.canton.ActionBasedSQLInterpolation.Implicits.actionBasedSQLInterpolationCanton
2727
import slick.jdbc.canton.SQLActionBuilder
2828
import slick.jdbc.{GetResult, PositionedResult, SetParameter}
29+
import slick.dbio.Effect
30+
import slick.sql.SqlStreamingAction
2931

3032
trait AcsQueries extends AcsJdbcTypes {
3133

3234
/** @param tableName Must be SQL-safe, as it needs to be interpolated unsafely.
3335
* This is fine, as all calls to this method should use static string constants.
3436
*/
35-
protected def selectFromAcsTable(
37+
protected def selectFromAcsTable[C, TCid <: ContractId[_], T](
3638
tableName: String,
3739
storeId: AcsStoreId,
3840
migrationId: Long,
39-
where: SQLActionBuilder,
41+
companion: C,
42+
where: SQLActionBuilder = sql"true",
4043
orderLimit: SQLActionBuilder = sql"",
41-
) =
44+
)(implicit companionClass: ContractCompanion[C, TCid, T]) = {
45+
val packageQualifiedName = companionClass.packageQualifiedName(companion)
4246
(sql"""
4347
select #${SelectFromAcsTableResult.sqlColumnsCommaSeparated()}
4448
from #$tableName acs
45-
where acs.store_id = $storeId and acs.migration_id = $migrationId and """ ++ where ++ sql"""
49+
where acs.store_id = $storeId
50+
and acs.migration_id = $migrationId
51+
and acs.package_name = ${packageQualifiedName.packageName}
52+
and acs.template_id_qualified_name = ${packageQualifiedName.qualifiedName}
53+
and """ ++ where ++ sql"""
4654
""" ++ orderLimit).toActionBuilder.as[AcsQueries.SelectFromAcsTableResult]
55+
}
4756

4857
implicit val GetResultSelectFromAcsTable: GetResult[AcsQueries.SelectFromAcsTableResult] =
4958
GetResult { prs =>
@@ -55,7 +64,7 @@ trait AcsQueries extends AcsJdbcTypes {
5564
<<[Long],
5665
<<[ContractId[Any]],
5766
<<[String],
58-
<<[QualifiedName],
67+
<<[PackageQualifiedName],
5968
<<[Json],
6069
<<[Array[Byte]],
6170
<<[Timestamp],
@@ -65,18 +74,27 @@ trait AcsQueries extends AcsJdbcTypes {
6574
}
6675

6776
/** Similar to [[selectFromAcsTable]], but also returns the contract state (i.e., the domain to which a contract is currently assigned) */
68-
protected def selectFromAcsTableWithState(
77+
protected def selectFromAcsTableWithState[C, TCid <: ContractId[_], T](
6978
tableName: String,
7079
storeId: AcsStoreId,
7180
migrationId: Long,
72-
where: SQLActionBuilder,
81+
companion: C,
82+
additionalWhere: SQLActionBuilder = sql"",
7383
orderLimit: SQLActionBuilder = sql"",
74-
) =
84+
)(implicit companionClass: ContractCompanion[C, TCid, T]): SqlStreamingAction[Vector[
85+
SelectFromAcsTableWithStateResult
86+
], SelectFromAcsTableWithStateResult, Effect.Read] = {
87+
val packageQualifiedName = companionClass.packageQualifiedName(companion)
7588
(sql"""
7689
select #${SelectFromAcsTableWithStateResult.sqlColumnsCommaSeparated()}
7790
from #$tableName acs
78-
where acs.store_id = $storeId and acs.migration_id = $migrationId and """ ++ where ++ sql"""
91+
where acs.store_id = $storeId
92+
and acs.migration_id = $migrationId
93+
and acs.package_name = ${packageQualifiedName.packageName}
94+
and acs.template_id_qualified_name = ${packageQualifiedName.qualifiedName}
95+
""" ++ additionalWhere ++ sql"""
7996
""" ++ orderLimit).toActionBuilder.as[AcsQueries.SelectFromAcsTableWithStateResult]
97+
}
8098

8199
implicit val GetResultSelectFromContractStateResult
82100
: GetResult[AcsQueries.SelectFromContractStateResult] =
@@ -104,13 +122,15 @@ trait AcsQueries extends AcsJdbcTypes {
104122
* This guarantees that the fetched contracts exist in the given offset,
105123
* whereas two separate queries (one to fetch the contract and one to fetch the offset) don't guarantee that.
106124
*/
107-
protected def selectFromAcsTableWithOffset(
125+
protected def selectFromAcsTableWithOffset[C, TCid <: ContractId[_], T](
108126
tableName: String,
109127
storeId: AcsStoreId,
110128
migrationId: Long,
129+
companion: C,
111130
where: SQLActionBuilder,
112131
orderLimit: SQLActionBuilder = sql"",
113-
) =
132+
)(implicit companionClass: ContractCompanion[C, TCid, T]) = {
133+
val packageQualifiedName = companionClass.packageQualifiedName(companion)
114134
(sql"""
115135
select
116136
acs.store_id,
@@ -120,6 +140,7 @@ trait AcsQueries extends AcsJdbcTypes {
120140
contract_id,
121141
template_id_package_id,
122142
template_id_qualified_name,
143+
package_name,
123144
create_arguments,
124145
created_event_blob,
125146
created_at,
@@ -130,10 +151,13 @@ trait AcsQueries extends AcsJdbcTypes {
130151
left join #$tableName acs
131152
on o.store_id = acs.store_id
132153
and o.migration_id = acs.migration_id
154+
and acs.package_name = ${packageQualifiedName.packageName}
155+
and acs.template_id_qualified_name = ${packageQualifiedName.qualifiedName}
133156
and """ ++ where ++ sql"""
134157
where sd.id = $storeId and o.migration_id = $migrationId
135158
""" ++ orderLimit).toActionBuilder
136159
.as[AcsQueries.SelectFromAcsTableResultWithOffset]
160+
}
137161

138162
implicit val GetResultSelectFromAcsTableResultWithOffset
139163
: GetResult[AcsQueries.SelectFromAcsTableResultWithOffset] = { (pp: PositionedResult) =>
@@ -161,13 +185,15 @@ trait AcsQueries extends AcsJdbcTypes {
161185

162186
/** Same as [[selectFromAcsTableWithOffset]], but also includes the contract state.
163187
*/
164-
protected def selectFromAcsTableWithStateAndOffset(
188+
protected def selectFromAcsTableWithStateAndOffset[C, TCid <: ContractId[_], T](
165189
tableName: String,
166190
storeId: AcsStoreId,
167191
migrationId: Long,
192+
companion: C,
168193
where: SQLActionBuilder = sql"true",
169194
orderLimit: SQLActionBuilder = sql"",
170-
) =
195+
)(implicit companionClass: ContractCompanion[C, TCid, T]) = {
196+
val packageQualifiedName = companionClass.packageQualifiedName(companion)
171197
(sql"""
172198
select
173199
acs.store_id,
@@ -177,6 +203,7 @@ trait AcsQueries extends AcsJdbcTypes {
177203
acs.contract_id,
178204
acs.template_id_package_id,
179205
acs.template_id_qualified_name,
206+
acs.package_name,
180207
acs.create_arguments,
181208
acs.created_event_blob,
182209
acs.created_at,
@@ -194,10 +221,13 @@ trait AcsQueries extends AcsJdbcTypes {
194221
left join #$tableName acs
195222
on o.store_id = acs.store_id
196223
and o.migration_id = acs.migration_id
224+
and acs.package_name = ${packageQualifiedName.packageName}
225+
and acs.template_id_qualified_name = ${packageQualifiedName.qualifiedName}
197226
and """ ++ where ++ sql"""
198227
where sd.id = $storeId and o.migration_id = $migrationId
199228
""" ++ orderLimit).toActionBuilder
200229
.as[AcsQueries.SelectFromAcsTableResultWithStateAndOffset]
230+
}
201231

202232
implicit val GetResultSelectFromAcsTableResultWithStateOffset
203233
: GetResult[AcsQueries.SelectFromAcsTableResultWithStateAndOffset] = {
@@ -311,7 +341,7 @@ object AcsQueries {
311341
eventNumber: Long,
312342
contractId: ContractId[Any],
313343
templateIdPackageId: String,
314-
templateIdQualifiedName: QualifiedName,
344+
packageQualifiedName: PackageQualifiedName,
315345
createArguments: Json,
316346
createdEventBlob: Array[Byte],
317347
createdAt: Timestamp,
@@ -321,12 +351,22 @@ object AcsQueries {
321351
companionClass: ContractCompanion[C, TCId, T],
322352
decoder: TemplateJsonDecoder,
323353
): Contract[TCId, T] = {
354+
// safety check: if the PackageQualifiedNames don't match,
355+
// it means that we would be returning a contract of a different template
356+
// note that the packageId not matching is expected due to upgrades, but the name will be stable
357+
val expectedPackageQualifiedName = companionClass.packageQualifiedName(companion)
358+
if (expectedPackageQualifiedName != packageQualifiedName) {
359+
throw new IllegalStateException(
360+
s"Contract $contractId has a different package qualified name than expected. Expected: $expectedPackageQualifiedName - Got: $packageQualifiedName"
361+
)
362+
}
363+
324364
companionClass
325365
.fromJson(companion)(
326366
new Identifier(
327367
templateIdPackageId,
328-
templateIdQualifiedName.moduleName,
329-
templateIdQualifiedName.entityName,
368+
packageQualifiedName.qualifiedName.moduleName,
369+
packageQualifiedName.qualifiedName.entityName,
330370
),
331371
contractId.contractId,
332372
createArguments,
@@ -351,6 +391,7 @@ object AcsQueries {
351391
${qualifier}contract_id,
352392
${qualifier}template_id_package_id,
353393
${qualifier}template_id_qualified_name,
394+
${qualifier}package_name,
354395
${qualifier}create_arguments,
355396
${qualifier}created_event_blob,
356397
${qualifier}created_at,

apps/common/src/main/scala/org/lfdecentralizedtrust/splice/store/db/DbAppStore.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ abstract class DbTxLogAppStore[TXE](
5858
acsContractFilter,
5959
txLogConfig,
6060
domainMigrationInfo,
61-
participantId,
6261
retryProvider,
6362
handleIngestionSummary,
6463
)
@@ -98,7 +97,6 @@ abstract class DbAppStore(
9897
acsContractFilter,
9998
TxLogStore.Config.empty,
10099
domainMigrationInfo,
101-
participantId,
102100
retryProvider,
103101
handleIngestionSummary,
104102
)

0 commit comments

Comments
 (0)