Skip to content

Commit 8026601

Browse files
author
Luis Sanchez
committed
[FAB-3066] sync java/go chaincode interface
ChaincodeBase: - removed run() method - implemented new Chaincode interface - added init(stub) method - added invoke(stub) method ChaincodeStub: - added chaincode args to stub (like golang version) Handler: - ChaincodeStub initialized with args - invoke calls Chaincode.invoke() - initialize call Chaincode.init() Other: - edited chaincode docs - fixed example Java chaincodes Change-Id: I04906052317c382b78884c00a85b80a12ed1bd9d Signed-off-by: Luis Sanchez <[email protected]>
1 parent 8eb16dc commit 8026601

File tree

10 files changed

+257
-153
lines changed

10 files changed

+257
-153
lines changed

core/chaincode/shim/java/.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
protos
2+
src/main/proto
3+
.classpath

core/chaincode/shim/java/build.gradle

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright DTCC 2016 All Rights Reserved.
2+
Copyright DTCC, IBM 2016, 2017 All Rights Reserved.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -106,6 +106,24 @@ task copyProtos(type:Copy){
106106
into "${projectDir}/src/main/proto"
107107
}
108108

109+
task copyProtosFromDevEnv(type:Copy){
110+
into "peer", {
111+
from ("../../../../protos/peer"){
112+
include 'chaincode_event.proto'
113+
include 'chaincode.proto'
114+
include 'chaincode_shim.proto'
115+
include 'proposal.proto'
116+
include 'proposal_response.proto'
117+
}
118+
}
119+
into "common", {
120+
from ("../../../../protos/common"){
121+
include 'common.proto'
122+
}
123+
}
124+
into "${projectDir}/protos"
125+
}
126+
109127
tasks['build'].mustRunAfter tasks['copyProtos']
110128
build.dependsOn(copyProtos)
111129
build.finalizedBy(copyToLib)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright IBM 2017 All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
package org.hyperledger.java.shim;
15+
16+
import org.hyperledger.fabric.protos.peer.ProposalResponsePackage.Response;
17+
18+
/**
19+
* Defines methods that all chaincodes must implement.
20+
*/
21+
public interface Chaincode {
22+
/**
23+
* Called during an instantiate transaction after the container
24+
* has been established, allowing the chaincode to initialize
25+
* its internal data.
26+
*/
27+
public Response init(ChaincodeStub stub);
28+
/**
29+
* Called for every Invoke transaction. The chaincode may change
30+
* its state variables.
31+
*/
32+
public Response invoke(ChaincodeStub stub);
33+
}

core/chaincode/shim/java/src/main/java/org/hyperledger/java/shim/ChaincodeBase.java

+15-6
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,23 @@
4242
import io.grpc.stub.StreamObserver;
4343
import io.netty.handler.ssl.SslContext;
4444

45-
public abstract class ChaincodeBase {
46-
45+
public abstract class ChaincodeBase implements Chaincode {
46+
47+
/* (non-Javadoc)
48+
* @see org.hyperledger.java.shim.Chaincode#init(org.hyperledger.java.shim.ChaincodeStub)
49+
*/
50+
@Override
51+
public abstract Response init(ChaincodeStub stub);
52+
53+
/* (non-Javadoc)
54+
* @see org.hyperledger.java.shim.Chaincode#invoke(org.hyperledger.java.shim.ChaincodeStub)
55+
*/
56+
@Override
57+
public abstract Response invoke(ChaincodeStub stub);
58+
4759
private static Log logger = LogFactory.getLog(ChaincodeBase.class);
48-
49-
public abstract Response run(ChaincodeStub stub, String function, String[] args);
50-
60+
5161
public abstract String getChaincodeID();
52-
5362
public static final String DEFAULT_HOST = "127.0.0.1";
5463
public static final int DEFAULT_PORT = 7051;
5564

core/chaincode/shim/java/src/main/java/org/hyperledger/java/shim/ChaincodeStub.java

+21-6
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,43 @@
1616

1717
package org.hyperledger.java.shim;
1818

19-
import java.util.HashMap;
19+
import java.util.Collections;
2020
import java.util.List;
21-
import java.util.Map;
21+
import java.util.stream.Collectors;
2222

2323
import org.apache.commons.logging.Log;
2424
import org.apache.commons.logging.LogFactory;
25-
import org.hyperledger.fabric.protos.peer.ChaincodeShim;
2625

2726
import com.google.protobuf.ByteString;
2827

29-
//import static org.hyperledger.protos.TableProto.ColumnDefinition.Type.STRING;
30-
3128
public class ChaincodeStub {
29+
3230
private static Log logger = LogFactory.getLog(ChaincodeStub.class);
31+
3332
private final String uuid;
3433
private final Handler handler;
34+
private final List<ByteString> args;
3535

36-
public ChaincodeStub(String uuid, Handler handler) {
36+
public ChaincodeStub(String uuid, Handler handler, List<ByteString> args) {
3737
this.uuid = uuid;
3838
this.handler = handler;
39+
this.args = Collections.unmodifiableList(args);
3940
}
4041

42+
public List<byte[]> getArgs() {
43+
return args
44+
.stream()
45+
.map(x -> x.toByteArray())
46+
.collect(Collectors.toList());
47+
}
48+
49+
public List<String> getArgsAsStrings() {
50+
return args
51+
.stream()
52+
.map(x -> x.toStringUtf8())
53+
.collect(Collectors.toList());
54+
}
55+
4156
/**
4257
* Gets the UUID of this stub
4358
*

core/chaincode/shim/java/src/main/java/org/hyperledger/java/shim/Handler.java

+5-19
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import static org.hyperledger.java.shim.HandlerHelper.newCompletedEventMessage;
3333
import static org.hyperledger.java.shim.HandlerHelper.newErrorEventMessage;
3434

35-
import java.util.ArrayList;
3635
import java.util.HashMap;
3736
import java.util.List;
3837
import java.util.Map;
@@ -208,15 +207,15 @@ public void handleInit(ChaincodeMessage message) {
208207

209208
// Get the function and args from Payload
210209
final ChaincodeInput input = ChaincodeInput.parseFrom(message.getPayload());
211-
210+
212211
// Mark as a transaction (allow put/del state)
213212
markIsTransaction(message.getTxid(), true);
214213

215214
// Create the ChaincodeStub which the chaincode can use to callback
216-
final ChaincodeStub stub = new ChaincodeStub(message.getTxid(), this);
215+
final ChaincodeStub stub = new ChaincodeStub(message.getTxid(), this, input.getArgsList());
217216

218217
// Call chaincode's Run
219-
final Response result = chaincode.run(stub, getFunction(input.getArgsList()), getParameters(input.getArgsList()));
218+
final Response result = chaincode.init(stub);
220219
logger.debug(String.format(String.format("[%s]Init succeeded. Sending %s", shortID(message), COMPLETED)));
221220

222221
if(result.getStatus() == Status.SUCCESS_VALUE) {
@@ -252,19 +251,6 @@ public void handleInit(ChaincodeMessage message) {
252251
}).start();
253252
}
254253

255-
private String getFunction(List<ByteString> args) {
256-
return (args.size() > 0) ? args.get(0).toStringUtf8() : "";
257-
}
258-
259-
private String[] getParameters(List<ByteString> args) {
260-
final ArrayList<String> results = new ArrayList<>();
261-
// skip arg[0], that is the function name
262-
for(int i = 1; i < args.size(); i++) {
263-
results.add(args.get(i).toStringUtf8());
264-
}
265-
return results.toArray(new String[results.size()]);
266-
}
267-
268254
// enterInitState will initialize the chaincode if entering init from established.
269255
public void beforeInit(Event event) {
270256
logger.debug(String.format("Before %s event.", event.name));
@@ -305,12 +291,12 @@ public void handleTransaction(ChaincodeMessage message) {
305291
markIsTransaction(message.getTxid(), true);
306292

307293
// Create the ChaincodeStub which the chaincode can use to callback
308-
final ChaincodeStub stub = new ChaincodeStub(message.getTxid(), this);
294+
final ChaincodeStub stub = new ChaincodeStub(message.getTxid(), this, input.getArgsList());
309295

310296
// Call chaincode's Run
311297
Response response;
312298
try {
313-
response = chaincode.run(stub, getFunction(input.getArgsList()), getParameters(input.getArgsList()));
299+
response = chaincode.invoke(stub);
314300
} catch (Throwable throwable) {
315301
// Send ERROR message to chaincode support and change state
316302
logger.error(String.format("[%s]Error running chaincode. Transaction execution failed. Sending %s", shortID(message), ERROR));

docs/source/Setup/JAVAChaincode.rst

+14-10
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,16 @@ To get started developing Java chaincode
2323
2. Ensure you have the Java 1.8 **JDK** installed. Also ensure Java's
2424
directory is on your path with ``java -version``
2525

26-
- Additionally, you will need to have the
27-
```JAVA HOME`` <https://docs.oracle.com/cd/E19182-01/821-0917/6nluh6gq9/index.html>`__
28-
variable set to your **JDK** installation in your system path
26+
- Additionally, you will need to have the |JAVA_HOME|_ variable set to your
27+
**JDK** installation in your system path
2928

3029
3. From your command line terminal, move to the ``devenv`` subdirectory
3130
of your workspace environment. Log into a Vagrant terminal by
3231
executing the following command:
3332

33+
.. |JAVA_HOME| replace:: ``JAVA_HOME``
34+
.. _JAVA_HOME: https://docs.oracle.com/cd/E19182-01/821-0917/6nluh6gq9/index.html
35+
3436
::
3537

3638
vagrant ssh
@@ -163,13 +165,15 @@ Developing new JAVA chaincode
163165
1. Create a new Java project structure.
164166
2. Use existing ``build.grade`` from any example JAVA Chaincode project
165167
like ``examples/chaincode/java/SimpleSample``.
166-
3. Make your main class extend ChaincodeBase class and implement the
168+
3. Make your main class extend ``ChaincodeBase`` class and implement the
167169
following methods from base class.
168-
4. ``public String run(ChaincodeStub stub, String function, String[] args)``
169-
5. ``public String query(ChaincodeStub stub, String function, String[] args)``
170-
6. ``public String getChaincodeID()``
171-
7. Modify the ``mainClassName`` in ``build.gradle`` to point to your new
170+
171+
- ``public Response init(ChaincodeStub stub)``
172+
- ``public Response invoke(ChaincodeStub stub);``
173+
- ``public String getChaincodeID()``
174+
175+
4. Modify the ``mainClassName`` in ``build.gradle`` to point to your new
172176
class.
173-
8. Build this project using ``gradle -b build.gradle build``
174-
9. Run this chaincode after starting a peer in dev-mode as above using
177+
5. Build this project using ``gradle -b build.gradle build``
178+
6. Run this chaincode after starting a peer in dev-mode as above using
175179
``gradle -b build.gradle run``

0 commit comments

Comments
 (0)