Skip to content

Commit

Permalink
[FAB-2501] cleanup java shim FSM
Browse files Browse the repository at this point in the history
- 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 <sanchezl@us.ibm.com>
  • Loading branch information
Luis Sanchez committed Feb 28, 2017
1 parent f9cc882 commit 3eaccbd
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 125 deletions.
17 changes: 3 additions & 14 deletions core/chaincode/chaincode_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,24 +347,11 @@ func (chaincodeSupport *ChaincodeSupport) getArgsAndEnv(cccid *ccprovider.CCCont
if chaincodeSupport.chaincodeLogLevel != "" {
envs = append(envs, "CORE_LOGGING_CHAINCODE="+chaincodeSupport.chaincodeLogLevel)
}

switch cLang {
case pb.ChaincodeSpec_GOLANG, pb.ChaincodeSpec_CAR:
//chaincode executable will be same as the name of the chaincode
args = []string{"chaincode", fmt.Sprintf("-peer.address=%s", chaincodeSupport.peerAddress)}
case pb.ChaincodeSpec_JAVA:
//TODO add security args
args = strings.Split(
fmt.Sprintf("java -jar chaincode.jar -a %s -i %s",
chaincodeSupport.peerAddress, cccid.Name),
" ")
if chaincodeSupport.peerTLS {
args = append(args, "-s")
if chaincodeSupport.peerTLSSvrHostOrd != "" {
args = append(args, "-o")
args = append(args, chaincodeSupport.peerTLSSvrHostOrd)
}
}
args = []string{"java", "-jar", "chaincode.jar", "--peerAddress", chaincodeSupport.peerAddress}
default:
return nil, nil, fmt.Errorf("Unknown chaincodeType: %s", cLang)
}
Expand Down Expand Up @@ -400,6 +387,8 @@ func (chaincodeSupport *ChaincodeSupport) launchAndWaitForRegister(ctxt context.
}

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

vmtype, _ := chaincodeSupport.getVMType(cds)

Expand Down
12 changes: 6 additions & 6 deletions core/chaincode/shim/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ func newChaincodeHandler(peerChatStream PeerChaincodeStream, chaincode Chaincode
"before_" + pb.ChaincodeMessage_REGISTERED.String(): func(e *fsm.Event) { v.beforeRegistered(e) },
"after_" + pb.ChaincodeMessage_RESPONSE.String(): func(e *fsm.Event) { v.afterResponse(e) },
"after_" + pb.ChaincodeMessage_ERROR.String(): func(e *fsm.Event) { v.afterError(e) },
"before_" + pb.ChaincodeMessage_INIT.String(): func(e *fsm.Event) { v.enterInitState(e) },
"before_" + pb.ChaincodeMessage_TRANSACTION.String(): func(e *fsm.Event) { v.enterTransactionState(e) },
"before_" + pb.ChaincodeMessage_INIT.String(): func(e *fsm.Event) { v.beforeInit(e) },
"before_" + pb.ChaincodeMessage_TRANSACTION.String(): func(e *fsm.Event) { v.beforeTransaction(e) },
},
)
return v
Expand Down Expand Up @@ -257,8 +257,8 @@ func (handler *Handler) handleInit(msg *pb.ChaincodeMessage) {
}()
}

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

// enterTransactionState will execute chaincode's Run if coming from a TRANSACTION event.
func (handler *Handler) enterTransactionState(e *fsm.Event) {
// beforeTransaction will execute chaincode's Run if coming from a TRANSACTION event.
func (handler *Handler) beforeTransaction(e *fsm.Event) {
msg, ok := e.Args[0].(*pb.ChaincodeMessage)
if !ok {
e.Cancel(fmt.Errorf("Received unexpected message type"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@ public class EventDesc {
/** The destination state that the FSM will be in if the transition succeeds */
String dst;

public EventDesc(String name, String dst, String... src) {
public EventDesc(String name, String[] src, String dst) {
this.name = name;
this.src = src;
this.dst = dst;
}
public EventDesc(String name, String src, String dst) {
this.name = name;
this.src = new String[] { src };
this.dst = dst;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,29 @@

package org.hyperledger.java.shim;

import com.google.protobuf.ByteString;
import io.grpc.ManagedChannel;
import io.grpc.netty.GrpcSslContexts;
import io.grpc.netty.NegotiationType;
import io.grpc.netty.NettyChannelBuilder;
import io.grpc.stub.StreamObserver;
import io.netty.handler.ssl.SslContext;
import java.io.File;

import javax.net.ssl.SSLException;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Options;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hyperledger.protos.Chaincode.ChaincodeID;
import org.hyperledger.protos.Chaincodeshim.ChaincodeMessage;
import org.hyperledger.protos.Chaincodeshim.ChaincodeMessage.Type;
import org.hyperledger.protos.ChaincodeSupportGrpc;
import org.hyperledger.protos.ChaincodeSupportGrpc.ChaincodeSupportStub;
import org.hyperledger.protos.Chaincodeshim.ChaincodeMessage;
import org.hyperledger.protos.Chaincodeshim.ChaincodeMessage.Type;

import javax.net.ssl.SSLException;
import java.io.File;
import com.google.protobuf.ByteString;

import io.grpc.ManagedChannel;
import io.grpc.netty.GrpcSslContexts;
import io.grpc.netty.NegotiationType;
import io.grpc.netty.NettyChannelBuilder;
import io.grpc.stub.StreamObserver;
import io.netty.handler.ssl.SslContext;

public abstract class ChaincodeBase {

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

private final static String CORE_CHAINCODE_ID_NAME = "CORE_CHAINCODE_ID_NAME";
private final static String CORE_PEER_ADDRESS = "CORE_PEER_ADDRESS";
private final static String CORE_PEER_TLS_ENABLED = "CORE_PEER_TLS_ENABLED";
private final static String CORE_PEER_TLS_SERVERHOSTOVERRIDE = "CORE_PEER_TLS_SERVERHOSTOVERRIDE";

// Start entry point for chaincodes bootstrap.
public void start(String[] args) {
Options options = new Options();
options.addOption("a", "peerAddress", true, "Address of peer to connect to");
options.addOption("s", "securityEnabled", false, "Present if security is enabled");
options.addOption("i", "id", true, "Identity of chaincode");
options.addOption("o", "hostNameOverride", true, "Hostname override for server certificate");
try {
CommandLine cl = new DefaultParser().parse(options, args);
if (cl.hasOption('a')) {
host = cl.getOptionValue('a');
port = new Integer(host.split(":")[1]);
host = host.split(":")[0];
}
if (cl.hasOption('s')) {
tlsEnabled = true;
logger.debug("TLS enabled");
if (cl.hasOption('o')){
hostOverrideAuthority = cl.getOptionValue('o');
logger.debug("server host override given " + hostOverrideAuthority);
}
}
if (cl.hasOption('i')) {
id = cl.getOptionValue('i');
}
} catch (Exception e) {
logger.warn("cli parsing failed with exception",e);

}

processEnvironmentOptions();
processCommandLineOptions(args);

Runnable chaincode = () -> {
logger.trace("chaincode started");
Expand All @@ -96,6 +80,51 @@ public void start(String[] args) {
};
new Thread(chaincode).start();
}

private void processCommandLineOptions(String[] args) {
Options options = new Options();
options.addOption("a", "peerAddress", true, "Address of peer to connect to");
options.addOption("s", "securityEnabled", false, "Present if security is enabled");
options.addOption("i", "id", true, "Identity of chaincode");
options.addOption("o", "hostNameOverride", true, "Hostname override for server certificate");
try {
CommandLine cl = new DefaultParser().parse(options, args);
if (cl.hasOption('a')) {
host = cl.getOptionValue('a');
port = new Integer(host.split(":")[1]);
host = host.split(":")[0];
}
if (cl.hasOption('s')) {
tlsEnabled = true;
logger.debug("TLS enabled");
if (cl.hasOption('o')){
hostOverrideAuthority = cl.getOptionValue('o');
logger.debug("server host override given " + hostOverrideAuthority);
}
}
if (cl.hasOption('i')) {
id = cl.getOptionValue('i');
}
} catch (Exception e) {
logger.warn("cli parsing failed with exception",e);

}
}

private void processEnvironmentOptions() {
if(System.getenv().containsKey(CORE_CHAINCODE_ID_NAME)) {
this.id = System.getenv(CORE_CHAINCODE_ID_NAME);
}
if(System.getenv().containsKey(CORE_PEER_ADDRESS)) {
this.host = System.getenv(CORE_PEER_ADDRESS);
}
if(System.getenv().containsKey(CORE_PEER_TLS_ENABLED)) {
this.tlsEnabled = Boolean.parseBoolean(System.getenv(CORE_PEER_TLS_ENABLED));
if(System.getenv().containsKey(CORE_PEER_TLS_SERVERHOSTOVERRIDE)) {
this.hostOverrideAuthority = System.getenv(CORE_PEER_TLS_SERVERHOSTOVERRIDE);
}
}
}

public ManagedChannel newPeerClientConnection() {
NettyChannelBuilder builder = NettyChannelBuilder.forAddress(host, port);
Expand Down Expand Up @@ -142,13 +171,13 @@ public void onNext(ChaincodeMessage message) {
e.printStackTrace();
System.exit(-1);
//TODO
// } else if (err != nil) {
// logger.Error(fmt.Sprintf("Received error from server: %s, ending chaincode stream", err))
// return
// } else if (in == nil) {
// err = fmt.Errorf("Received nil message, ending chaincode stream")
// logger.debug("Received nil message, ending chaincode stream")
// return
// } else if (err != nil) {
// logger.Error(fmt.Sprintf("Received error from server: %s, ending chaincode stream", err))
// return
// } else if (in == nil) {
// err = fmt.Errorf("Received nil message, ending chaincode stream")
// logger.debug("Received nil message, ending chaincode stream")
// return
}
}

Expand Down
Loading

0 comments on commit 3eaccbd

Please sign in to comment.