Skip to content

Commit d7e63ab

Browse files
mbwhiteGerrit Code Review
authored andcommitted
Merge "[FAB-15213] In-repo examples, readme update" into release-1.4
2 parents ea32146 + 7f1e772 commit d7e63ab

File tree

18 files changed

+981
-60
lines changed

18 files changed

+981
-60
lines changed

CODE_OF_CONDUCT.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
Code of Conduct Guidelines
22
==========================
33

4-
Please review the Hyperledger [Code of
5-
Conduct](https://wiki.hyperledger.org/community/hyperledger-project-code-of-conduct)
4+
Please review the Hyperledger [Code of Conduct](https://wiki.hyperledger.org/community/hyperledger-project-code-of-conduct)
65
before participating. It is important that we keep things civil.
76

87
<a rel="license" href="http://creativecommons.org/licenses/by/4.0/"><img alt="Creative Commons License" style="border-width:0" src="https://i.creativecommons.org/l/by/4.0/88x31.png" /></a><br />This work is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by/4.0/">Creative Commons Attribution 4.0 International License</a>.

CONTRACT_TUTORIAL.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
## Contract tutorials
2+
3+
This tutorial will teach you how to write Java based Hyperledger Fabric Contract; the code examples have been created using the IBM Blockchain Platform VSCode extension.
4+
5+
## Writing your own contract
6+
7+
Either gradle or maven can be used for building your code; here the example is shown with gradle.
8+
9+
### Create Gradle project
10+
You can use `fabric-contract-example/gradle` as staring point. Make sure that your project build creates a runnable jar that contains all dependencies named `chaincode.jar` as result.
11+
12+
The main class is very important for Contracts and must be set to `org.hyperledger.fabric.contract.ContractRouter`. All these are set correctly in the examples but for reference the important parts are
13+
14+
```
15+
plugins {
16+
id 'com.github.johnrengelman.shadow' version '2.0.3'
17+
}
18+
...
19+
20+
...
21+
shadowJar {
22+
baseName = 'chaincode'
23+
version = null
24+
classifier = null
25+
26+
manifest {
27+
attributes 'Main-Class': 'org.hyperledger.fabric.contract.ContractRouter'
28+
}
29+
}
30+
```
31+
32+
### Writing the contract
33+
34+
Typically a Contract will be working with one or more 'assets', so in this example we are using a `MyAssestContract` class. Within a given chaincode container,
35+
(the docker container that starts when chaincode is instantiated), you can have one or more contract classes. Each contract class has one or more
36+
'transaction functions' these can be executed from the SDK or from other contracts.
37+
38+
With the VSCode extension you can create a starter project, or you can use the same Yeoman generator from the command line.
39+
40+
```
41+
# if you don't have Yeoman already
42+
npm install -g yo
43+
44+
npm install -g generator-fabric
45+
```
46+
47+
You can then run the generator to create a sample Java Contract project that uses Gradle 4.6
48+
This is an example output of running the generator
49+
50+
```
51+
yo fabric:contract
52+
? Please specify the contract language: Java
53+
? Please specify the contract name: MyJavaContract
54+
? Please specify the contract version: 0.0.1
55+
? Please specify the contract description: My first Java contract
56+
? Please specify the contract author: ANOther
57+
? Please specify the contract license: Apache-2.0
58+
? Please specify the asset type: MyAsset
59+
```
60+
61+
### Code overview
62+
63+
As well as the gradle project files, a `org.example.MyAsset.java` and a `org.example.MyAssetContract.java` are
64+
created.
65+
66+
All contract classes like `MyAssetContract` must implement the `ContractInterface` and have a `@Contract` annotation
67+
to mark this as a Contract. The annotation allows you to specify meta information, such as version, author
68+
and description. Refer to the JavaDoc for the full specificaiton.
69+
70+
The `@Default` annotation is useful, as it marks the class as a the default contract - when referring to the
71+
transaction function from the client SDK it permits a shorthand to be used.
72+
73+
It is recommened to have a no-argument constructor in the contract.
74+
75+
Each method you wish to be a transaction function must be marked by a `@Transaction()` annotation.
76+
The first parameter to each of these must accept a `org.hyperledger.fabric.contract.Context` object. This
77+
object is the 'transactional context' and provides information about the current transaction being executed
78+
and also provides access to the APIs to check and update the ledger state.
79+
80+
Transaciton functions can have as many other parameters as they wish.
81+
82+
Standard Java primitives and strings can be passed; any other Java Object IS NOT supported, eg passing a HashMap.
83+
More complex types can be defined if they are suitably defined. Arrays are supported types are permitted.
84+
85+
The `MyAsset` class is an example of the more a complex datatype that can be passed. Such a class is
86+
marked used the `@DataType` annotation, with each property within the object marked by a `@Property` annotation.
87+
88+
Richer constraints on datatype and parameters can be applied; see the JavaDoc for details.
89+
90+
#### Building contract
91+
92+
Run build command.
93+
94+
```bash
95+
gradle clean build shadowJar
96+
```
97+
Assuming there are no build errors, you can proceed to chaincode testing.
98+

CONTRIBUTING.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,57 @@ Please visit the
77
[contributors guide](http://hyperledger-fabric.readthedocs.io/en/latest/CONTRIBUTING.html) in the
88
docs to learn how to make contributions to this exciting project.
99

10+
## Folder structure
11+
12+
The "fabric-chaincode-protos" folder contains the protobuf definition files used by
13+
Java shim to communicate with Fabric peer.
14+
15+
The "fabric-chaincode-shim" folder contains the java shim classes that define Java
16+
chaincode API and way to communicate with Fabric peer.
17+
18+
The "fabric-chaincode-docker" folder contains instructions to the build
19+
`hyperledger/fabric-javaenv` docker image.
20+
21+
The "fabric-chaincode-example-gradle" contains an example java chaincode gradle
22+
project that includes sample chaincode and basic gradle build instructions.
23+
24+
#### Install prerequisites
25+
26+
Make sure you installed all [fabric prerequisites](https://hyperledger-fabric.readthedocs.io/en/latest/prereqs.html)
27+
28+
Install java shim specific prerequisites:
29+
* Java 8
30+
* gradle 4.4
31+
32+
#### Build shim
33+
34+
Clone the fabric shim for java chaincode repo.
35+
36+
```
37+
git clone https://github.com/hyperledger/fabric-chaincode-java.git
38+
```
39+
40+
Build java shim jars (proto and shim jars) and install them to local maven repository.
41+
```
42+
cd fabric-chaincode-java
43+
gradle clean build install
44+
```
45+
46+
Build javaenv docker image, to have it locally.
47+
```
48+
gradle buildImage
49+
```
50+
51+
#### Update your chaincode dependencies
52+
53+
Make your chanincode depend on java shim master version and not on version from maven central
54+
55+
```
56+
dependencies {
57+
compile group: 'org.hyperledger.fabric-chaincode-java', name: 'fabric-chaincode-shim', version: '1.4.2-SNAPSHOT'
58+
}
59+
```
60+
1061
## Code of Conduct Guidelines <a name="conduct"></a>
1162

1263
See our [Code of Conduct Guidelines](../blob/master/CODE_OF_CONDUCT.md).

FAQ.md

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

README.md

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,19 @@
1-
# Hyperledger Fabric Shim for Java chaincode
1+
# Hyperledger Fabric - Java Contract and Chaincode
22

3-
This is a Java based implementation of Hyprledger Fabric chaincode shim APIs, which enables development of chaincodes using Java language.
3+
This is a Java implementation of Hyperledger Fabric chaincode shim APIs and contract programming model. This enables development of using Java language or other JVM based languages
44

5-
Application developers interested in developing smart contracts (what we call chaincode) for Hyperledger Fabric should
6-
read the tutorial in TUTORIAL.md file and visit
7-
`Chaincode for developers <https://hyperledger-fabric.readthedocs.io/en/latest/chaincode4ade.html>`__.
5+
## Developers
86

9-
Contributors or early adopters who need to be able to build and test recent Java chaincode shim, should reference [FAQ.md](FAQ.md) file.
7+
Application developers interested in developing smart contracts should read the [introductory tutorial](CONTRACT_TUTORIAL.md) and for a full scenario visit the
8+
[Commercial Paper](https://hyperledger-fabric.readthedocs.io/en/latest/tutorial/commercial_paper.html) tutorial.
109

11-
This project creates `fabric-chaincode-protos` and `fabric-chaincode-shim` jar
12-
files for developers consumption and the `hyperledger/fabric-javaenv` docker image
13-
to run Java chaincode.
10+
We recommend using the IBM Blockchain Platform [VSCode extension](https://marketplace.visualstudio.com/items?itemName=IBMBlockchain.ibm-blockchain-platform) to assist you in developing smart contracts. The extension is able to create sample contracts for you, an example of such a contract is in the [fabric-contract-example](./fabric-contract-example) directory; there are folders for using both gradle and maven.
1411

15-
## Folder structure
12+
In addition, this has reference to other tutorials to get you started
1613

17-
The "fabric-chaincode-protos" folder contains the protobuf definition files used by
18-
Java shim to communicate with Fabric peer.
14+
## Contributors
1915

20-
The "fabric-chaincode-shim" folder contains the java shim classes that define Java
21-
chaincode API and way to communicate with Fabric peer.
16+
Contributors or early adopters who need to be able to build and test recent builds, should reference the [contributing](CONTRIBUTING.md) guide.
2217

23-
The "fabric-chaincode-docker" folder contains instructions to the build
24-
`hyperledger/fabric-javaenv` docker image.
18+
This project creates `fabric-chaincode-protos` and `fabric-chaincode-shim` jar files for developers consumption and the `hyperledger/fabric-javaenv` docker image to run the Java chaincode and contracts.
2519

26-
The "fabric-chaincode-example-gradle" contains an example java chaincode gradle
27-
project that includes sample chaincode and basic gradle build instructions.

fabric-chaincode-example-maven/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
<repository>
2828
<id>jitpack.io</id>
2929
<url>https://jitpack.io</url>
30+
</repository>
31+
<repository>
32+
<id>nexus</id>
33+
<url>https://nexus.hyperledger.org/content/repositories/snapshots/</url>
3034
</repository>
3135
</repositories>
3236

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.gradle/
2+
build/
3+
bin/
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"generator-fabric": {
3+
"promptValues": {
4+
"subgenerator": "contract"
5+
}
6+
}
7+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
This example needs to use gradle4.6 please install this first
2+
3+
eg using sdkman
4+
5+
`sdk install gradle 4.6`
6+
7+
8+
and then add the wrapper code before building
9+
10+
`gradle wrapper`
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
plugins {
2+
id 'com.github.johnrengelman.shadow' version '2.0.3'
3+
id 'java'
4+
}
5+
6+
version '0.0.1'
7+
8+
sourceCompatibility = 1.8
9+
10+
repositories {
11+
mavenLocal()
12+
mavenCentral()
13+
maven {
14+
url 'https://jitpack.io'
15+
}
16+
maven {
17+
url "https://nexus.hyperledger.org/content/repositories/snapshots/"
18+
}
19+
20+
}
21+
22+
dependencies {
23+
compile group: 'org.hyperledger.fabric-chaincode-java', name: 'fabric-chaincode-shim', version: '1.4.2-SNAPSHOT'
24+
compile group: 'org.json', name: 'json', version: '20180813'
25+
testImplementation 'org.junit.jupiter:junit-jupiter:5.4.2'
26+
testImplementation 'org.assertj:assertj-core:3.11.1'
27+
testImplementation 'org.mockito:mockito-core:2.+'
28+
}
29+
30+
shadowJar {
31+
baseName = 'chaincode'
32+
version = null
33+
classifier = null
34+
35+
manifest {
36+
attributes 'Main-Class': 'org.hyperledger.fabric.contract.ContractRouter'
37+
}
38+
}
39+
40+
test {
41+
useJUnitPlatform()
42+
testLogging {
43+
events "passed", "skipped", "failed"
44+
}
45+
}
46+
47+
48+
tasks.withType(JavaCompile) {
49+
options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation" << "-parameters"
50+
}

0 commit comments

Comments
 (0)