Skip to content
Open
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2020, Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
# Copyright (c) 2024, Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

lib/
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright 2020 Digital Asset (Switzerland) GmbH and/or its affiliates
Copyright 2024 Digital Asset (Switzerland) GmbH and/or its affiliates

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
4 changes: 2 additions & 2 deletions PingPong/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Java Bindings Ping-Pong Example

::

Copyright (c) 2023 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
Copyright (c) 2024 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
SPDX-License-Identifier: Apache-2.0.0


Expand Down Expand Up @@ -31,7 +31,7 @@ To set a project up:

#. If you do not have it already, install the DAML SDK by running::

curl https://get.daml.com | sh -s 2.8.0
curl https://get.daml.com | sh -s 2.10.0

#. Use the start script for starting a ledger & the java application:

Expand Down
10 changes: 6 additions & 4 deletions PingPong/daml.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
# Copyright (c) 2023 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
# Copyright (c) 2024 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

sdk-version: 2.7.0
sdk-version: 2.10.0-snapshot.20241106.0
name: ex-java-bindings
source: daml/PingPong.daml
init-script: PingPong:setup
version: 0.0.2
version: 1.0.0
dependencies:
- daml-prim
- daml-stdlib
- daml-script
codegen:
java:
package-prefix: examples.pingpong.codegen
output-directory: src/main/java/
output-directory: src/main/java/
build-options:
- --target=1.17
2 changes: 1 addition & 1 deletion PingPong/daml/PingPong.daml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- Copyright (c) 2023 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
-- Copyright (c) 2024 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
-- SPDX-License-Identifier: Apache-2.0

module PingPong where
Expand Down
136 changes: 136 additions & 0 deletions PingPong/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#!/usr/bin/env bash
# Copyright (c) 2024 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

# Use absolute paths to allow this script to be called from any location
root_dir=$(cd "$(dirname $0)"; pwd -P)

clean () {
if [[ -d ${root_dir}/.dars ]]; then
rm -r ${root_dir}/.dars
fi
}

build () {
daml build

# Copy all package dars into a dedicated folder
mkdir -p ${root_dir}/.dars
cp ${root_dir}/.daml/dist/* ${root_dir}/.dars/
}

start () {

mkdir -p ${root_dir}/temp
output_file=$(mktemp -p ${root_dir}/temp)

pushd ${root_dir} > /dev/null || exit
daml sandbox > ${output_file} &
sandbox_pid=$!
popd > /dev/null || exit

echo "Sandbox running with pid: ${sandbox_pid}"
tail -n0 -f ${output_file} | sed '/Canton sandbox is ready/ q'

rm ${root_dir}/temp/*

echo "${sandbox_pid}" > ${root_dir}/temp/pid
}

dars() {
find ${root_dir}/.daml/dist -name "*${1}.dar" -exec daml ledger upload-dar --host localhost --port 6865 {} \;
}

setup () {
pushd ${root_dir} > /dev/null || exit
script_name=$(yq e '.init-script' daml.yaml)
script_options=$(yq e '.script-options[]' daml.yaml | tr '\n' ' ')
dar_name="${root_dir}/.daml/dist/ex-java-bindings-${1}.dar"
if [ ! -f ${dar_name} ]; then
echo "Setup dar not found!"
fi
echo "Running the script: ${script_name}"
daml script --dar ${dar_name} --script-name ${script_name} --ledger-host localhost --ledger-port 6865 ${script_options}
popd > /dev/null || exit
}

codegen() {
pushd ${root_dir} > /dev/null || exit
echo "Generating java code"
daml codegen java
echo "Compiling code"
mvn compile
popd > /dev/null || exit
}

grpc() {
pushd ${root_dir} > /dev/null || exit
mvn exec:java -Dexec.mainClass=examples.pingpong.grpc.PingPongGrpcMain -Dpackage.id=$packageId -Dexec.args="localhost 6865"
popd > /dev/null || exit
}

rx() {
pushd ${root_dir} > /dev/null || exit
mvn exec:java -Dexec.mainClass=examples.pingpong.reactive.PingPongReactiveMain -Dpackage.id=$packageId -Dexec.args="localhost 6865"
popd > /dev/null || exit
}

cdg() {
pushd ${root_dir} > /dev/null || exit
mvn exec:java -Dexec.mainClass=examples.pingpong.codegen.PingPongCodegenMain -Dpackage.id=$packageId -Dexec.args="localhost 6865"
popd > /dev/null || exit
}

stop () {
sandbox_pid=$(cat "${root_dir}/temp/pid")
echo "Stopping sandbox with pid: ${sandbox_pid}"
kill ${sandbox_pid}
}

operation=${1}
extension=${2:-"1.0.0"}
packageId=$(daml damlc inspect-dar --json ${root_dir}/.daml/dist/ex-java-bindings-${extension}.dar | jq '.main_package_id' -r)

case $operation in
start)
start
;;

stop)
stop
;;

build)
build
;;

dars)
dars ${extension}
;;

setup)
setup ${extension}
;;

codegen)
codegen
;;

grpc)
grpc
;;

rx)
rx
;;

cdg)
cdg
;;

up)
start
dars ${extension}
setup ${extension}

esac
4 changes: 2 additions & 2 deletions PingPong/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
<groupId>com.daml.ledger.examples</groupId>
<artifactId>example-ping-pong-java</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<version>1.0.0-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<sdk-version>2.7.0</sdk-version>
<sdk-version>2.10.0-snapshot.20241106.13056.0.v910c3af2</sdk-version>
</properties>

<dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2023 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// Copyright (c) 2024 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package examples.pingpong.codegen;
Expand All @@ -21,6 +21,7 @@
import io.grpc.ManagedChannelBuilder;

import java.util.List;
import java.util.Optional;
import java.util.UUID;

public class PingPongCodegenMain {
Expand All @@ -47,24 +48,21 @@ public static void main(String[] args) {
// Initialize a plaintext gRPC channel
ManagedChannel channel = ManagedChannelBuilder.forAddress(host, port).usePlaintext().build();

// fetch the ledger ID, which is used in subsequent requests sent to the ledger
String ledgerId = fetchLedgerId(channel);

// fetch the party IDs that got created in the Daml init script
String aliceParty = fetchPartyId(channel, ALICE_USER);
String bobParty = fetchPartyId(channel, BOB_USER);

// initialize the ping pong processors for Alice and Bob
PingPongProcessor aliceProcessor = new PingPongProcessor(aliceParty, ledgerId, channel);
PingPongProcessor bobProcessor = new PingPongProcessor(bobParty, ledgerId, channel);
PingPongProcessor aliceProcessor = new PingPongProcessor(aliceParty, channel);
PingPongProcessor bobProcessor = new PingPongProcessor(bobParty, channel);

// start the processors asynchronously
aliceProcessor.runIndefinitely();
bobProcessor.runIndefinitely();

// send the initial commands for both parties
createInitialContracts(channel, ledgerId, aliceParty, bobParty, numInitialContracts);
createInitialContracts(channel, ledgerId, bobParty, aliceParty, numInitialContracts);
createInitialContracts(channel, aliceParty, bobParty, numInitialContracts);
createInitialContracts(channel, bobParty, aliceParty, numInitialContracts);


try {
Expand All @@ -80,12 +78,11 @@ public static void main(String[] args) {
* Creates numContracts number of Ping contracts. The sender is used as the submitting party.
*
* @param channel the gRPC channel to use for services
* @param ledgerId the previously fetched ledger id
* @param sender the party that sends the initial Ping contract
* @param receiver the party that receives the initial Ping contract
* @param numContracts the number of initial contracts to create
*/
private static void createInitialContracts(ManagedChannel channel, String ledgerId, String sender, String receiver, int numContracts) {
private static void createInitialContracts(ManagedChannel channel, String sender, String receiver, int numContracts) {
CommandSubmissionServiceFutureStub submissionService = CommandSubmissionServiceGrpc.newFutureStub(channel);

for (int i = 0; i < numContracts; i++) {
Expand All @@ -102,24 +99,12 @@ private static void createInitialContracts(ManagedChannel channel, String ledger
.withWorkflowId(String.format("Ping-%s-%d", sender, i));

// convert the command submission to a proto data structure
final var request = SubmitRequest.toProto(ledgerId, commandsSubmission);
final var request = SubmitRequest.toProto("", commandsSubmission);
// asynchronously send the request
submissionService.submit(request);
}
}

/**
* Fetches the ledger id via the Ledger Identity Service.
*
* @param channel the gRPC channel to use for services
* @return the ledger id as provided by the ledger
*/
private static String fetchLedgerId(ManagedChannel channel) {
LedgerIdentityServiceBlockingStub ledgerIdService = LedgerIdentityServiceGrpc.newBlockingStub(channel);
GetLedgerIdentityResponse identityResponse = ledgerIdService.getLedgerIdentity(GetLedgerIdentityRequest.getDefaultInstance());
return identityResponse.getLedgerId();
}

private static String fetchPartyId(ManagedChannel channel, String userId) {
UserManagementServiceBlockingStub userManagementService = UserManagementServiceGrpc.newBlockingStub(channel);
GetUserResponse getUserResponse = userManagementService.getUser(GetUserRequest.newBuilder().setUserId(userId).build());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2023 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// Copyright (c) 2024 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package examples.pingpong.codegen;
Expand Down Expand Up @@ -33,17 +33,15 @@
public class PingPongProcessor {

private final String party;
private final String ledgerId;

private final TransactionServiceStub transactionService;
private final CommandSubmissionServiceBlockingStub submissionService;

private final Identifier pingIdentifier;
private final Identifier pongIdentifier;

public PingPongProcessor(String party, String ledgerId, ManagedChannel channel) {
public PingPongProcessor(String party, ManagedChannel channel) {
this.party = party;
this.ledgerId = ledgerId;
this.transactionService = TransactionServiceGrpc.newStub(channel);
this.submissionService = CommandSubmissionServiceGrpc.newBlockingStub(channel);
this.pingIdentifier = Ping.TEMPLATE_ID;
Expand All @@ -58,8 +56,8 @@ public void runIndefinitely() {
final var filtersByParty = new FiltersByParty(Map.of(party, inclusiveFilter));
// assemble the request for the transaction stream
final var getTransactionsRequest = new GetTransactionsRequest(
ledgerId,
LedgerOffset.LedgerBegin.getInstance(),
"",
LedgerOffset.LedgerEnd.getInstance(),
filtersByParty,
true
);
Expand Down Expand Up @@ -105,7 +103,7 @@ private void processTransaction(Transaction tx) {
.withActAs(List.of(party))
.withReadAs(List.of(party))
.withWorkflowId(tx.getWorkflowId());
submissionService.submit(SubmitRequest.toProto(ledgerId, commandsSubmission));
submissionService.submit(SubmitRequest.toProto("", commandsSubmission));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package examples.pingpong.grpc;

import com.daml.ledger.javaapi.data.Identifier;

public class IdentifierCreator {
private String packageId;
private static String packageName = "#ex-java-bindings";
public IdentifierCreator(String packageId) {
this.packageId = packageId;
}
public Identifier pingIdentifier() {
return Identifier.fromProto(
com.daml.ledger.api.v1.ValueOuterClass.Identifier.newBuilder()
.setPackageId(packageName)
.setModuleName("PingPong")
.setEntityName("Ping")
.build()
);
}
public Identifier pinnedPingIdentifier() {
return Identifier.fromProto(
com.daml.ledger.api.v1.ValueOuterClass.Identifier.newBuilder()
.setPackageId(packageId)
.setModuleName("PingPong")
.setEntityName("Ping")
.build()
);
}
public Identifier pongIdentifier() {
return Identifier.fromProto(
com.daml.ledger.api.v1.ValueOuterClass.Identifier.newBuilder()
.setPackageId(packageName)
.setModuleName("PingPong")
.setEntityName("Pong")
.build()
);
}
public Identifier pinnedPongIdentifier() {
return Identifier.fromProto(
com.daml.ledger.api.v1.ValueOuterClass.Identifier.newBuilder()
.setPackageId(packageId)
.setModuleName("PingPong")
.setEntityName("Pong")
.build()
);
}
}
Loading