Skip to content

Commit 64cf9b1

Browse files
authored
Merge from OsamaRab3/test-java-chaincode
Replace SimpleAsset with PokeballContract in Java chaincode
2 parents 72b0f14 + a94debd commit 64cf9b1

File tree

9 files changed

+408
-119
lines changed

9 files changed

+408
-119
lines changed

samples/chaincodes/java-chaincode/Dockerfile

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,51 @@
1+
/*
2+
* SPDX-License-Identifier: Apache2.0
3+
*/
14
plugins {
5+
id 'com.github.johnrengelman.shadow' version '7.0.0'
26
id 'java'
3-
id 'application'
4-
id 'com.github.johnrengelman.shadow' version '7.1.2'
57
}
68

7-
group 'org.example'
8-
version '1.0-SNAPSHOT'
9+
version '0.0.1'
10+
11+
sourceCompatibility = 1.8
912

1013
repositories {
14+
mavenLocal()
1115
mavenCentral()
1216
maven {
13-
url "https://jitpack.io"
17+
url 'https://jitpack.io'
18+
}
19+
maven {
20+
url "https://nexus.hyperledger.org/content/repositories/snapshots/"
1421
}
1522
}
1623

1724
dependencies {
18-
implementation 'org.hyperledger.fabric-chaincode-java:fabric-chaincode-shim:2.4.1'
19-
implementation 'com.google.protobuf:protobuf-java:3.21.7'
20-
implementation 'org.json:json:20220924'
21-
implementation 'com.github.everit-org.json-schema:org.everit.json.schema:1.12.1'
22-
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.0'
23-
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.0'
25+
implementation group: 'org.hyperledger.fabric-chaincode-java', name: 'fabric-chaincode-shim', version: '2.3.0'
26+
implementation group: 'org.json', name: 'json', version: '20180813'
27+
testImplementation 'org.junit.jupiter:junit-jupiter:5.4.2'
28+
testImplementation 'org.assertj:assertj-core:3.11.1'
29+
testImplementation 'org.mockito:mockito-core:2.+ '
2430
}
2531

26-
application {
27-
mainClass = 'org.example.SimpleAsset'
32+
shadowJar {
33+
archiveBaseName.set('chaincode')
34+
archiveVersion.set('')
35+
archiveClassifier.set('all')
36+
manifest {
37+
attributes 'Main-Class': 'org.hyperledger.fabric.contract.ContractRouter'
38+
}
2839
}
2940

3041
test {
3142
useJUnitPlatform()
43+
testLogging {
44+
events "passed", "skipped", "failed"
45+
}
3246
}
3347

34-
jar {
35-
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
36-
manifest {
37-
attributes 'Main-Class': 'org.example.SimpleAsset'
38-
}
39-
from {
40-
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
41-
}
42-
exclude 'META-INF/*.RSA'
43-
exclude 'META-INF/*.SF'
44-
exclude 'META-INF/*.DSA'
48+
49+
tasks.withType(JavaCompile) {
50+
options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation" << "-parameters"
4551
}

samples/chaincodes/java-chaincode/gradlew

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/chaincodes/java-chaincode/run-dev.sh

Lines changed: 59 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,75 @@
22

33
set -eu
44

5-
echo "Checking if chaincode is installed and running..."
5+
# ========== CONFIGURATION ==========
6+
CHAINCODE_NAME="chaincode1"
7+
CHAINCODE_VERSION="0.0.1"
8+
CHANNEL_NAME="my-channel1"
9+
PEER_NAME="peer0.org1.example.com"
10+
JAR_PATH="build/libs/chaincode-all.jar"
11+
CHAINCODE_PORT=7041
612

7-
if ! docker exec peer0.org1.example.com peer chaincode list --installed 2>/dev/null | grep -q "simple-asset"; then
8-
echo "ERROR: Chaincode 'simple-asset' is not installed on the peer."
9-
echo "Please install the chaincode first"
10-
exit 1
11-
fi
13+
# ========== CHECK COMMANDS ==========
14+
for cmd in docker java grep gradle; do
15+
if ! command -v $cmd &>/dev/null; then
16+
echo "Error: '$cmd' command not found. Please install it first."
17+
exit 1
18+
fi
19+
done
1220

21+
# ========== BUILD THE JAR ==========
22+
gradle wrapper
23+
./gradlew clean shadowJar
1324

14-
if ! docker exec peer0.org1.example.com peer chaincode list --instantiated -C mychannel 2>/dev/null | grep -q "simple-asset"; then
15-
echo "ERROR: Chaincode 'simple-asset' is not committed to the channel."
16-
echo "Please commit the chaincode first."
25+
if [ ! -f "$JAR_PATH" ]; then
26+
echo "Error: JAR file was not created. Build may have failed."
1727
exit 1
1828
fi
1929

20-
21-
./gradlew clean shadowJar
22-
23-
if [ ! -f "build/libs/java-chaincode-1.0-SNAPSHOT-all.jar" ]; then
24-
echo "Error: JAR file was not created. Build may have failed."
30+
# ========== VALIDATE PEER CONTAINER ==========
31+
if ! docker ps | grep -q "$PEER_NAME"; then
32+
echo "Error: $PEER_NAME container is not running."
33+
echo "Please make sure the Fabric network is up and running."
2534
exit 1
2635
fi
2736

28-
if ! docker ps | grep -q "peer0.org1.example.com"; then
29-
echo "Error: peer0.org1.example.com container is not running."
30-
echo "Please make sure the Fabric network is up and running."
37+
# ========== GET PEER IP ==========
38+
PEER_IP=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $PEER_NAME)
39+
if [ -z "$PEER_IP" ]; then
40+
echo "Error: Could not find $PEER_NAME IP address."
3141
exit 1
3242
fi
3343

34-
# Get the IP address of peer0.org1.example.com
35-
peer_ip=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' peer0.org1.example.com)
36-
if [ -z "$peer_ip" ]; then
37-
echo "Error: Could not find peer0.org1.example.com IP address."
38-
exit 1
44+
echo "Testing connectivity to peer at $PEER_IP:$CHAINCODE_PORT..."
45+
if ! nc -z $PEER_IP $CHAINCODE_PORT 2>/dev/null; then
46+
echo "Error: Cannot connect to peer chaincode port $PEER_IP:$CHAINCODE_PORT."
47+
echo "Ensure the peer is running in dev mode and listening on port $CHAINCODE_PORT."
48+
exit 1
3949
fi
40-
# Run the chaincode in dev mode
41-
CORE_CHAINCODE_LOGLEVEL=debug \
42-
CORE_PEER_TLS_ENABLED=true \
43-
CORE_CHAINCODE_ID_NAME=simple-asset:1.0 \
44-
CORE_PEER_ADDRESS=$peer_ip:7051 \
45-
CORE_CHAINCODE_LOGGING_LEVEL=DEBUG \
46-
CORE_CHAINCODE_LOGGING_SHIM=debug \
47-
java -jar build/libs/java-chaincode-1.0-SNAPSHOT-all.jar
50+
51+
# ========== EXPORT ENVIRONMENT VARIABLES ==========
52+
export CHAINCODE_SERVER_ADDRESS=0.0.0.0:$CHAINCODE_PORT
53+
export CORE_CHAINCODE_ID_NAME="$CHAINCODE_NAME:$CHAINCODE_VERSION"
54+
export CORE_CHAINCODE_LOGGING_LEVEL="DEBUG"
55+
export CORE_CHAINCODE_LOGGING_SHIM="debug"
56+
export CORE_PEER_ADDRESS="$PEER_IP:$CHAINCODE_PORT"
57+
export CORE_PEER_LOCALMSPID="Org1MSP"
58+
export CORE_PEER_TLS_ENABLED=false
59+
export CORE_CHAINCODE_LOGLEVEL=debug
60+
export FABRIC_LOGGING_SPEC=debug
61+
62+
# ========== RUN CHAINCODE ==========
63+
echo "========================================"
64+
echo "Running Java chaincode in dev mode..."
65+
echo "Chaincode Name: $CORE_CHAINCODE_ID_NAME"
66+
echo "Peer Address: $CORE_PEER_ADDRESS"
67+
echo "========================================"
68+
69+
if java -jar "$JAR_PATH" -peer.address $PEER_IP:7041; then
70+
echo "========================================"
71+
echo "Successfully running Java in dev Mode"
72+
echo "========================================"
73+
else
74+
echo "Error: Failed to start the chaincode JAR"
75+
exit 1
76+
fi
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rootProject.name = 'java-chaincode'
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* SPDX-License-Identifier: Apache2.0
3+
*/
4+
5+
package org.example;
6+
7+
import org.hyperledger.fabric.contract.annotation.DataType;
8+
import org.hyperledger.fabric.contract.annotation.Property;
9+
import org.json.JSONObject;
10+
11+
@DataType()
12+
public class Pokeball {
13+
14+
@Property()
15+
private String value;
16+
17+
public Pokeball(){
18+
}
19+
20+
public String getValue() {
21+
return value;
22+
}
23+
24+
public void setValue(String value) {
25+
this.value = value;
26+
}
27+
28+
public String toJSONString() {
29+
return new JSONObject(this).toString();
30+
}
31+
32+
public static Pokeball fromJSONString(String json) {
33+
String value = new JSONObject(json).getString("value");
34+
Pokeball asset = new Pokeball();
35+
asset.setValue(value);
36+
return asset;
37+
}
38+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* SPDX-License-Identifier: Apache2.0
3+
*/
4+
package org.example;
5+
6+
import org.hyperledger.fabric.contract.Context;
7+
import org.hyperledger.fabric.contract.ContractInterface;
8+
import org.hyperledger.fabric.contract.annotation.Contract;
9+
import org.hyperledger.fabric.contract.annotation.Default;
10+
import org.hyperledger.fabric.contract.annotation.Transaction;
11+
import org.hyperledger.fabric.contract.annotation.Contact;
12+
import org.hyperledger.fabric.contract.annotation.Info;
13+
import org.hyperledger.fabric.contract.annotation.License;
14+
import static java.nio.charset.StandardCharsets.UTF_8;
15+
16+
@Contract(name = "PokeballContract",
17+
info = @Info(title = "Pokeball contract",
18+
description = "Pokeball implementation",
19+
version = "0.0.1",
20+
license =
21+
@License(name = "Apache2.0",
22+
url = ""),
23+
contact = @Contact(email = "PokeballContract@example.com",
24+
name = "PokeballContract",
25+
url = "http://PokeballContract.me")))
26+
@Default
27+
public class PokeballContract implements ContractInterface {
28+
public PokeballContract() {
29+
30+
}
31+
@Transaction()
32+
public boolean pokeballExists(Context ctx, String pokeballId) {
33+
byte[] buffer = ctx.getStub().getState(pokeballId);
34+
return (buffer != null && buffer.length > 0);
35+
}
36+
37+
@Transaction()
38+
public void createPokeball(Context ctx, String pokeballId, String value) {
39+
boolean exists = pokeballExists(ctx,pokeballId);
40+
if (exists) {
41+
throw new RuntimeException("The asset "+pokeballId+" already exists");
42+
}
43+
Pokeball asset = new Pokeball();
44+
asset.setValue(value);
45+
ctx.getStub().putState(pokeballId, asset.toJSONString().getBytes(UTF_8));
46+
}
47+
48+
@Transaction()
49+
public Pokeball readPokeball(Context ctx, String pokeballId) {
50+
boolean exists = pokeballExists(ctx,pokeballId);
51+
if (!exists) {
52+
throw new RuntimeException("The asset "+pokeballId+" does not exist");
53+
}
54+
55+
Pokeball newAsset = Pokeball.fromJSONString(new String(ctx.getStub().getState(pokeballId),UTF_8));
56+
return newAsset;
57+
}
58+
59+
@Transaction()
60+
public void updatePokeball(Context ctx, String pokeballId, String newValue) {
61+
boolean exists = pokeballExists(ctx,pokeballId);
62+
if (!exists) {
63+
throw new RuntimeException("The asset "+pokeballId+" does not exist");
64+
}
65+
Pokeball asset = new Pokeball();
66+
asset.setValue(newValue);
67+
68+
ctx.getStub().putState(pokeballId, asset.toJSONString().getBytes(UTF_8));
69+
}
70+
71+
@Transaction()
72+
public void deletePokeball(Context ctx, String pokeballId) {
73+
boolean exists = pokeballExists(ctx,pokeballId);
74+
if (!exists) {
75+
throw new RuntimeException("The asset "+pokeballId+" does not exist");
76+
}
77+
ctx.getStub().delState(pokeballId);
78+
}
79+
80+
}

samples/chaincodes/java-chaincode/src/main/java/org/example/SimpleAsset.java

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)