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

Add test asserting correct handling of non-normalized keys #20684

Merged
merged 3 commits into from
Feb 5, 2025
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
15 changes: 15 additions & 0 deletions sdk/compatibility/bazel_tools/testing.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,21 @@ excluded_test_tool_tests = [
},
],
},
# New test (TXEventsByContractKeyDecimalNonNormalized) introduced
# that was asserts a previously existing bug that
# prohibited correct fetching of events by contract key
# when they contained non-normalized numeric values
{
"start": "2.10.0-snapshot.20241218.1",
"platform_ranges": [
{
"end": "2.10.0-snapshot.20241218.0",
"exclusions": [
"EventQueryServiceIT:TXEventsByContractKeyDecimalNonNormalized",
],
},
],
},
]

def in_range(version, range):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@ package com.daml.ledger.api.testtool.suites.v1_15
import com.daml.ledger.api.testtool.infrastructure.Allocation._
import com.daml.ledger.api.testtool.infrastructure.Assertions._
import com.daml.ledger.api.testtool.infrastructure.LedgerTestSuite
import com.daml.ledger.api.testtool.infrastructure.participant.ParticipantTestContext
import com.daml.ledger.api.v1.event.CreatedEvent
import com.daml.ledger.api.v1.event_query_service.{
GetEventsByContractIdRequest,
GetEventsByContractKeyRequest,
GetEventsByContractKeyResponse,
}
import com.daml.ledger.api.v1.value._
import com.daml.ledger.javaapi.data.Party
import com.daml.ledger.test.java.model.test.{Dummy, _}
import com.daml.ledger.test.java.model.test._
import com.daml.lf.value.Value.ContractId
import scalapb.GeneratedMessage

import java.util.{List => JList}
import scala.concurrent.{ExecutionContext, Future}
import scala.jdk.CollectionConverters._
import scala.concurrent.Future

class EventQueryServiceIT extends LedgerTestSuite {
import com.daml.ledger.api.testtool.suites.v1_8.CompanionImplicits._
Expand All @@ -43,6 +46,18 @@ class EventQueryServiceIT extends LedgerTestSuite {
)
}

private def makeNumericKey(party: Party, numericValue: String) =
Value(
Value.Sum.Record(
Record(fields =
Vector(
RecordField(value = Some(Value(Value.Sum.Party(party)))),
RecordField(value = Some(Value(Value.Sum.Numeric(numericValue)))),
)
)
)
)

test(
"TXEventsByContractIdBasic",
"Expose a create event by contract id",
Expand Down Expand Up @@ -330,4 +345,50 @@ class EventQueryServiceIT extends LedgerTestSuite {
}
})

test(
"TXEventsByContractKeyDecimalNonNormalized",
"Expose a visible create event by contract key with non-normalized numeric value",
allocate(TwoParties),
)(implicit ec => { case Participants(Participant(ledger, alice, bob)) =>
for {
// Correct argument scale
(eventsAlice, aliceCreateO) <- testNumericKey(ledger, alice, "300000.000001")

// Different argument scale (argument is Numeric 6)
(eventsBob, bobCreate0) <- testNumericKey(ledger, bob, "300000.0")
} yield {
val actualAlice = assertDefined(eventsAlice.createEvent, "Expected a created event")
val expectedAlice = assertDefined(aliceCreateO, "Expected a created event")
assertEquals("Looked up event should match the transaction event", actualAlice, expectedAlice)

val actualBob = assertDefined(eventsBob.createEvent, "Expected a created event")
val expectedBob = assertDefined(bobCreate0, "Expected a created event")
assertEquals("Looked up event should match the transaction event", actualBob, expectedBob)
}
})

private def testNumericKey(
ledger: ParticipantTestContext,
party: Party,
numericValue: String,
)(implicit ec: ExecutionContext): Future[(GetEventsByContractKeyResponse, Option[CreatedEvent])] =
for {
tx <- ledger.submitAndWaitForTransaction(
ledger.submitAndWaitRequest(
party,
new KeyedByDecimal(party, new java.math.BigDecimal(numericValue)).create.commands(),
)
)
createdO =
tx.transaction.flatMap(_.events.flatMap(_.event.created).headOption)

events <- ledger.getEventsByContractKey(
GetEventsByContractKeyRequest(
contractKey = Some(makeNumericKey(party, numericValue)),
templateId = Some(KeyedByDecimal.TEMPLATE_ID.toV1),
requestingParties = Seq(party),
)
)
} yield events -> createdO

}
10 changes: 10 additions & 0 deletions sdk/test-common/src/main/daml/model/Test.daml
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,16 @@ template TextKey
controller tkParty
do create this with tkDisclosedTo = tkNewDisclosedTo

type Amount = Numeric 6

template KeyedByDecimal with
party: Party
amount: Amount
where
signatory party
key (party, amount): (Party, Amount)
maintainer key._1

template TextKeyOperations
with
tkoParty: Party
Expand Down