Skip to content

Commit 3eaccbd

Browse files
author
Luis Sanchez
committed
[FAB-2501] cleanup java shim FSM
- included are small changes to the golang shim meant to help keep both shims similar. - added some behave tests Change-Id: Ib53fc58a97844648334c8319a4b806739cd363ae Signed-off-by: Luis Sanchez <[email protected]>
1 parent f9cc882 commit 3eaccbd

File tree

8 files changed

+188
-125
lines changed

8 files changed

+188
-125
lines changed

core/chaincode/chaincode_support.go

+3-14
Original file line numberDiff line numberDiff line change
@@ -347,24 +347,11 @@ func (chaincodeSupport *ChaincodeSupport) getArgsAndEnv(cccid *ccprovider.CCCont
347347
if chaincodeSupport.chaincodeLogLevel != "" {
348348
envs = append(envs, "CORE_LOGGING_CHAINCODE="+chaincodeSupport.chaincodeLogLevel)
349349
}
350-
351350
switch cLang {
352351
case pb.ChaincodeSpec_GOLANG, pb.ChaincodeSpec_CAR:
353-
//chaincode executable will be same as the name of the chaincode
354352
args = []string{"chaincode", fmt.Sprintf("-peer.address=%s", chaincodeSupport.peerAddress)}
355353
case pb.ChaincodeSpec_JAVA:
356-
//TODO add security args
357-
args = strings.Split(
358-
fmt.Sprintf("java -jar chaincode.jar -a %s -i %s",
359-
chaincodeSupport.peerAddress, cccid.Name),
360-
" ")
361-
if chaincodeSupport.peerTLS {
362-
args = append(args, "-s")
363-
if chaincodeSupport.peerTLSSvrHostOrd != "" {
364-
args = append(args, "-o")
365-
args = append(args, chaincodeSupport.peerTLSSvrHostOrd)
366-
}
367-
}
354+
args = []string{"java", "-jar", "chaincode.jar", "--peerAddress", chaincodeSupport.peerAddress}
368355
default:
369356
return nil, nil, fmt.Errorf("Unknown chaincodeType: %s", cLang)
370357
}
@@ -400,6 +387,8 @@ func (chaincodeSupport *ChaincodeSupport) launchAndWaitForRegister(ctxt context.
400387
}
401388

402389
chaincodeLogger.Debugf("start container: %s(networkid:%s,peerid:%s)", canName, chaincodeSupport.peerNetworkID, chaincodeSupport.peerID)
390+
chaincodeLogger.Debugf("start container with args: %s", strings.Join(args, " "))
391+
chaincodeLogger.Debugf("start container with env:\n\t%s", strings.Join(env, "\n\t"))
403392

404393
vmtype, _ := chaincodeSupport.getVMType(cds)
405394

core/chaincode/shim/handler.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,8 @@ func newChaincodeHandler(peerChatStream PeerChaincodeStream, chaincode Chaincode
183183
"before_" + pb.ChaincodeMessage_REGISTERED.String(): func(e *fsm.Event) { v.beforeRegistered(e) },
184184
"after_" + pb.ChaincodeMessage_RESPONSE.String(): func(e *fsm.Event) { v.afterResponse(e) },
185185
"after_" + pb.ChaincodeMessage_ERROR.String(): func(e *fsm.Event) { v.afterError(e) },
186-
"before_" + pb.ChaincodeMessage_INIT.String(): func(e *fsm.Event) { v.enterInitState(e) },
187-
"before_" + pb.ChaincodeMessage_TRANSACTION.String(): func(e *fsm.Event) { v.enterTransactionState(e) },
186+
"before_" + pb.ChaincodeMessage_INIT.String(): func(e *fsm.Event) { v.beforeInit(e) },
187+
"before_" + pb.ChaincodeMessage_TRANSACTION.String(): func(e *fsm.Event) { v.beforeTransaction(e) },
188188
},
189189
)
190190
return v
@@ -257,8 +257,8 @@ func (handler *Handler) handleInit(msg *pb.ChaincodeMessage) {
257257
}()
258258
}
259259

260-
// enterInitState will initialize the chaincode if entering init from established.
261-
func (handler *Handler) enterInitState(e *fsm.Event) {
260+
// beforeInit will initialize the chaincode if entering init from established.
261+
func (handler *Handler) beforeInit(e *fsm.Event) {
262262
chaincodeLogger.Debugf("Entered state %s", handler.FSM.Current())
263263
msg, ok := e.Args[0].(*pb.ChaincodeMessage)
264264
if !ok {
@@ -327,8 +327,8 @@ func (handler *Handler) handleTransaction(msg *pb.ChaincodeMessage) {
327327
}()
328328
}
329329

330-
// enterTransactionState will execute chaincode's Run if coming from a TRANSACTION event.
331-
func (handler *Handler) enterTransactionState(e *fsm.Event) {
330+
// beforeTransaction will execute chaincode's Run if coming from a TRANSACTION event.
331+
func (handler *Handler) beforeTransaction(e *fsm.Event) {
332332
msg, ok := e.Args[0].(*pb.ChaincodeMessage)
333333
if !ok {
334334
e.Cancel(fmt.Errorf("Received unexpected message type"))

core/chaincode/shim/java/src/main/java/org/hyperledger/java/fsm/EventDesc.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,14 @@ public class EventDesc {
1717
/** The destination state that the FSM will be in if the transition succeeds */
1818
String dst;
1919

20-
public EventDesc(String name, String dst, String... src) {
20+
public EventDesc(String name, String[] src, String dst) {
2121
this.name = name;
2222
this.src = src;
2323
this.dst = dst;
2424
}
25+
public EventDesc(String name, String src, String dst) {
26+
this.name = name;
27+
this.src = new String[] { src };
28+
this.dst = dst;
29+
}
2530
}

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

+74-45
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,29 @@
1616

1717
package org.hyperledger.java.shim;
1818

19-
import com.google.protobuf.ByteString;
20-
import io.grpc.ManagedChannel;
21-
import io.grpc.netty.GrpcSslContexts;
22-
import io.grpc.netty.NegotiationType;
23-
import io.grpc.netty.NettyChannelBuilder;
24-
import io.grpc.stub.StreamObserver;
25-
import io.netty.handler.ssl.SslContext;
19+
import java.io.File;
20+
21+
import javax.net.ssl.SSLException;
22+
2623
import org.apache.commons.cli.CommandLine;
2724
import org.apache.commons.cli.DefaultParser;
2825
import org.apache.commons.cli.Options;
2926
import org.apache.commons.logging.Log;
3027
import org.apache.commons.logging.LogFactory;
3128
import org.hyperledger.protos.Chaincode.ChaincodeID;
32-
import org.hyperledger.protos.Chaincodeshim.ChaincodeMessage;
33-
import org.hyperledger.protos.Chaincodeshim.ChaincodeMessage.Type;
3429
import org.hyperledger.protos.ChaincodeSupportGrpc;
3530
import org.hyperledger.protos.ChaincodeSupportGrpc.ChaincodeSupportStub;
31+
import org.hyperledger.protos.Chaincodeshim.ChaincodeMessage;
32+
import org.hyperledger.protos.Chaincodeshim.ChaincodeMessage.Type;
3633

37-
import javax.net.ssl.SSLException;
38-
import java.io.File;
34+
import com.google.protobuf.ByteString;
35+
36+
import io.grpc.ManagedChannel;
37+
import io.grpc.netty.GrpcSslContexts;
38+
import io.grpc.netty.NegotiationType;
39+
import io.grpc.netty.NettyChannelBuilder;
40+
import io.grpc.stub.StreamObserver;
41+
import io.netty.handler.ssl.SslContext;
3942

4043
public abstract class ChaincodeBase {
4144

@@ -57,35 +60,16 @@ public abstract class ChaincodeBase {
5760
private Handler handler;
5861
private String id = getChaincodeID();
5962

63+
private final static String CORE_CHAINCODE_ID_NAME = "CORE_CHAINCODE_ID_NAME";
64+
private final static String CORE_PEER_ADDRESS = "CORE_PEER_ADDRESS";
65+
private final static String CORE_PEER_TLS_ENABLED = "CORE_PEER_TLS_ENABLED";
66+
private final static String CORE_PEER_TLS_SERVERHOSTOVERRIDE = "CORE_PEER_TLS_SERVERHOSTOVERRIDE";
67+
6068
// Start entry point for chaincodes bootstrap.
6169
public void start(String[] args) {
62-
Options options = new Options();
63-
options.addOption("a", "peerAddress", true, "Address of peer to connect to");
64-
options.addOption("s", "securityEnabled", false, "Present if security is enabled");
65-
options.addOption("i", "id", true, "Identity of chaincode");
66-
options.addOption("o", "hostNameOverride", true, "Hostname override for server certificate");
67-
try {
68-
CommandLine cl = new DefaultParser().parse(options, args);
69-
if (cl.hasOption('a')) {
70-
host = cl.getOptionValue('a');
71-
port = new Integer(host.split(":")[1]);
72-
host = host.split(":")[0];
73-
}
74-
if (cl.hasOption('s')) {
75-
tlsEnabled = true;
76-
logger.debug("TLS enabled");
77-
if (cl.hasOption('o')){
78-
hostOverrideAuthority = cl.getOptionValue('o');
79-
logger.debug("server host override given " + hostOverrideAuthority);
80-
}
81-
}
82-
if (cl.hasOption('i')) {
83-
id = cl.getOptionValue('i');
84-
}
85-
} catch (Exception e) {
86-
logger.warn("cli parsing failed with exception",e);
87-
88-
}
70+
71+
processEnvironmentOptions();
72+
processCommandLineOptions(args);
8973

9074
Runnable chaincode = () -> {
9175
logger.trace("chaincode started");
@@ -96,6 +80,51 @@ public void start(String[] args) {
9680
};
9781
new Thread(chaincode).start();
9882
}
83+
84+
private void processCommandLineOptions(String[] args) {
85+
Options options = new Options();
86+
options.addOption("a", "peerAddress", true, "Address of peer to connect to");
87+
options.addOption("s", "securityEnabled", false, "Present if security is enabled");
88+
options.addOption("i", "id", true, "Identity of chaincode");
89+
options.addOption("o", "hostNameOverride", true, "Hostname override for server certificate");
90+
try {
91+
CommandLine cl = new DefaultParser().parse(options, args);
92+
if (cl.hasOption('a')) {
93+
host = cl.getOptionValue('a');
94+
port = new Integer(host.split(":")[1]);
95+
host = host.split(":")[0];
96+
}
97+
if (cl.hasOption('s')) {
98+
tlsEnabled = true;
99+
logger.debug("TLS enabled");
100+
if (cl.hasOption('o')){
101+
hostOverrideAuthority = cl.getOptionValue('o');
102+
logger.debug("server host override given " + hostOverrideAuthority);
103+
}
104+
}
105+
if (cl.hasOption('i')) {
106+
id = cl.getOptionValue('i');
107+
}
108+
} catch (Exception e) {
109+
logger.warn("cli parsing failed with exception",e);
110+
111+
}
112+
}
113+
114+
private void processEnvironmentOptions() {
115+
if(System.getenv().containsKey(CORE_CHAINCODE_ID_NAME)) {
116+
this.id = System.getenv(CORE_CHAINCODE_ID_NAME);
117+
}
118+
if(System.getenv().containsKey(CORE_PEER_ADDRESS)) {
119+
this.host = System.getenv(CORE_PEER_ADDRESS);
120+
}
121+
if(System.getenv().containsKey(CORE_PEER_TLS_ENABLED)) {
122+
this.tlsEnabled = Boolean.parseBoolean(System.getenv(CORE_PEER_TLS_ENABLED));
123+
if(System.getenv().containsKey(CORE_PEER_TLS_SERVERHOSTOVERRIDE)) {
124+
this.hostOverrideAuthority = System.getenv(CORE_PEER_TLS_SERVERHOSTOVERRIDE);
125+
}
126+
}
127+
}
99128

100129
public ManagedChannel newPeerClientConnection() {
101130
NettyChannelBuilder builder = NettyChannelBuilder.forAddress(host, port);
@@ -142,13 +171,13 @@ public void onNext(ChaincodeMessage message) {
142171
e.printStackTrace();
143172
System.exit(-1);
144173
//TODO
145-
// } else if (err != nil) {
146-
// logger.Error(fmt.Sprintf("Received error from server: %s, ending chaincode stream", err))
147-
// return
148-
// } else if (in == nil) {
149-
// err = fmt.Errorf("Received nil message, ending chaincode stream")
150-
// logger.debug("Received nil message, ending chaincode stream")
151-
// return
174+
// } else if (err != nil) {
175+
// logger.Error(fmt.Sprintf("Received error from server: %s, ending chaincode stream", err))
176+
// return
177+
// } else if (in == nil) {
178+
// err = fmt.Errorf("Received nil message, ending chaincode stream")
179+
// logger.debug("Received nil message, ending chaincode stream")
180+
// return
152181
}
153182
}
154183

0 commit comments

Comments
 (0)