Skip to content

Add DB versions of Splice.Amulet.CryptoHash#4662

Open
adetokunbo wants to merge 6 commits intoadetokunbo/cip-104-add-compute-reward-totalsfrom
adetokunbo/cip-104-add-compute-hashes-db-hash-function
Open

Add DB versions of Splice.Amulet.CryptoHash#4662
adetokunbo wants to merge 6 commits intoadetokunbo/cip-104-add-compute-reward-totalsfrom
adetokunbo/cip-104-add-compute-hashes-db-hash-function

Conversation

@adetokunbo
Copy link
Copy Markdown
Contributor

First part the changes that address #4382

  • split out because it's a specific change that can be reviewed on its own

Pull Request Checklist

Cluster Testing

  • If a cluster test is required, comment /cluster_test on this PR to request it, and ping someone with access to the DA-internal system to approve it.
  • If a hard-migration test is required (from the latest release), comment /hdm_test on this PR to request it, and ping someone with access to the DA-internal system to approve it.
  • If a logical synchronizer upgrade test is required (from canton-3.5), comment /lsu_test on this PR to request it, and ping someone with access to the DA-internal system to approve it.

PR Guidelines

  • Include any change that might be observable by our partners or affect their deployment in the release notes.
  • Specify fixed issues with Fixes #n, and mention issues worked on using #n
  • Include a screenshot for frontend-related PRs - see README or use your favorite screenshot tool

Merge Guidelines

  • Make the git commit message look sensible when squash-merging on GitHub (most likely: just copy your PR description).

Signed-off-by: Tim Emiola <adetokunbo@emio.la>
@adetokunbo adetokunbo added this to the Traffic-Based App Rewards milestone Mar 26, 2026
@adetokunbo adetokunbo self-assigned this Mar 26, 2026
object DbCryptoHashFunctionsTest {

/** Scala reimplementation of Daml's CryptoHash module (Splice.Amulet.CryptoHash).
* Used as the test oracle to verify plpgsql hash functions produce identical output.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please also add a corresponding test that calls the actual Daml functions.

I'd do so by adding a Splice.Amulet.TestTemplates module defining a template CryptoHashProxy that allows evaluating calls to crypto hashes.

Copy link
Copy Markdown
Contributor Author

@adetokunbo adetokunbo Mar 30, 2026

Choose a reason for hiding this comment

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

Fixed.
Needed to

  • change the build files to allow com.digitalasset.canton.util.TestEngine to be run by splice tests cdf0afe
  • inline the daml functions in the proxy class; they will be removed when this branch is merged with the actual daml changes (part of 80cc9d9)

Copy link
Copy Markdown
Contributor

@rautenrieth-da rautenrieth-da left a comment

Choose a reason for hiding this comment

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

Looks good, I'm only slightly nervous about the test strategy. We are comparing the SQL output to a re-implementation of the daml code in scala, with only two hardcoded hashes manually copied from the daml output. I think it's still fine because we don't expect changes in #3964, but I'd like to hear input on this from @meiersi-da.

Unfortunately I don't think it's possible to execute arbitrary daml functions from scala code. We have an in-memory daml engine (com.digitalasset.canton.util.TestEngine) but that takes a command and produces a transaction.

@meiersi-da
Copy link
Copy Markdown
Contributor

Looks good, I'm only slightly nervous about the test strategy. We are comparing the SQL output to a re-implementation of the daml code in scala, with only two hardcoded hashes manually copied from the daml output. I think it's still fine because we don't expect changes in #3964, but I'd like to hear input on this from @meiersi-da.

Unfortunately I don't think it's possible to execute arbitrary daml functions from scala code. We have an in-memory daml engine (com.digitalasset.canton.util.TestEngine) but that takes a command and produces a transaction.

We can and should test the equivalence to the Daml implementation.

I'd suggest to do this as follows:

  1. Add module Splice.Testing.AmuletComputationProxies to splice-amulet-test
  2. Add the following template to that module (and fix the bugs due to sketching this directly here)
template ComputeCryptoHash with
  owner : Party
  where
    nonconsuming choice ComputeCryptoHash_HashMintingBalances : Hash
      with
        input : MintingBalances
      controller owner
      do pure (hash input)

   ... add choices for the other calls you want to check
  1. Create one instance of it, and then submit commands against it to get the exercise result and thus the Daml compute value.

Signed-off-by: Tim Emiola <adetokunbo@emio.la>
@adetokunbo adetokunbo force-pushed the adetokunbo/cip-104-add-compute-hashes-db-hash-function branch from 63dcc69 to 56f1b77 Compare March 30, 2026 03:40
@adetokunbo adetokunbo force-pushed the adetokunbo/cip-104-add-compute-reward-totals branch from 77ce31d to 05e30d8 Compare March 30, 2026 03:44
Signed-off-by: Tim Emiola <adetokunbo@emio.la>
Signed-off-by: Tim Emiola <adetokunbo@emio.la>
- the new test does three-way testing between db, daml and a scala oracle

Signed-off-by: Tim Emiola <adetokunbo@emio.la>
@adetokunbo
Copy link
Copy Markdown
Contributor Author

@rautenrieth-da , @meiersi-da all comments addressed, PTAL

@adetokunbo adetokunbo changed the title [ci] Add DB versions of Splice.Amulet.CryptoHash Add DB versions of Splice.Amulet.CryptoHash Mar 30, 2026
Copy link
Copy Markdown
Contributor

@rautenrieth-da rautenrieth-da left a comment

Choose a reason for hiding this comment

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

Thanks, test coverage looks good now!

(don't forget to add the signed-off-by to the last commit)


def toSqlExpr(op: HashOp): String = op match {
case HashText(v) =>
s"daml_crypto_hash_text('${escapeSql(v)}')"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

For a test like this is good enough. For production code, you can avoid all unsafe interpolation and manual quoting using

(
  sql"select " ++ sql"daml_crypto_hash_text($v)" ++ ...
).toActionBuilder

Comment on lines +1269 to +1272
// Re-add TestEngine so Splice tests can run Daml code in-memory
Test / unmanagedSources := Seq(
(Test / sourceDirectory).value / "scala/com/digitalasset/canton/util/TestEngine.scala"
),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm wary of this dependency, as it is only available because we fork Canton.

I'd much prefer to just use the normal participant nodes we have available, and thereby be safe both from divergences in the behavior as well as be safe from having to be able to provision this dependency.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@rautenrieth-da : wdyt?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants