Skip to content

Commit

Permalink
[FAB-13803] Parameter Marshalling
Browse files Browse the repository at this point in the history
- Implements the same marhsalling as the Node.js
- Some of the changes have been made as the result of Eclipse
  automatically updating imports and other
  warning related updates
- json schema passed value validation

Change-Id: I8e0f48d75c4ec2d5a01c2516f0ebdb9f98e08a01
Signed-off-by: Matthew B. White <[email protected]>
  • Loading branch information
mbwhite committed Jun 10, 2019
1 parent 72dc716 commit d726175
Show file tree
Hide file tree
Showing 53 changed files with 2,197 additions and 896 deletions.
2 changes: 1 addition & 1 deletion fabric-chaincode-example-gradle/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ repositories {
}

dependencies {
compile group: 'org.hyperledger.fabric-chaincode-java', name: 'fabric-chaincode-shim', version: '2.0.0-SNAPSHOT'
implementation group: 'org.hyperledger.fabric-chaincode-java', name: 'fabric-chaincode-shim', version: '2.0.0-SNAPSHOT'
testCompile group: 'junit', name: 'junit', version: '4.12'
}

Expand Down
2 changes: 1 addition & 1 deletion fabric-chaincode-protos/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ buildscript {
}

dependencies {
compile 'com.google.protobuf:protobuf-java:3.5.1'
compile 'com.google.protobuf:protobuf-java:3.7.1'
compile 'com.google.protobuf:protobuf-java-util:3.5.1'
compile 'io.grpc:grpc-netty-shaded:1.9.0'
compile 'io.grpc:grpc-protobuf:1.9.0'
Expand Down
6 changes: 4 additions & 2 deletions fabric-chaincode-shim/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ jacocoTestCoverageVerification {
'org.hyperledger.fabric.contract.routing.impl.ContractDefinitionImpl',
'org.hyperledger.fabric.contract.routing.RoutingRegistry',
'org.hyperledger.fabric.contract.execution.impl.ContractInvocationRequest',
'org.hyperledger.fabric.contract.routing.TransactionType']
'org.hyperledger.fabric.contract.routing.TransactionType',
'org.hyperledger.fabric.contract.metadata.MetadataBuilder']
limit {
minimum = 0.86
}
Expand All @@ -91,7 +92,8 @@ jacocoTestCoverageVerification {
'org.hyperledger.fabric.contract.execution.impl.ContractInvocationRequest',
'org.hyperledger.fabric.contract.routing.impl.ContractDefinitionImpl',
'org.hyperledger.fabric.contract.routing.RoutingRegistry',
'org.hyperledger.fabric.shim.impl.Handler']
'org.hyperledger.fabric.shim.impl.Handler',
'org.hyperledger.fabric.contract.metadata.MetadataBuilder']
limit {
minimum = 0.71
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,19 @@
import java.io.StringWriter;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.LogManager;

public class Logger extends java.util.logging.Logger{
/**
* Logger class to use throughout the Contract Implementation
*
*/
public class Logger extends java.util.logging.Logger {

protected Logger(String name) {
super(name, null);

// ensure that the parent logger is set
this.setParent(java.util.logging.Logger.getLogger("org.hyperledger.fabric"));
}

public static Logger getLogger(String name) {
Expand All @@ -29,19 +37,23 @@ public void debug(String msg) {
}

public static Logger getLogger(Class<?> class1) {
return Logger.getLogger(class1.getName());
// important to add the logger to the log manager
Logger l = Logger.getLogger(class1.getName());
LogManager.getLogManager().addLogger(l);
return l;
}

public void error(String message) {
log(Level.SEVERE,message);
log(Level.SEVERE, message);
}

public void error(Supplier<String> msgSupplier) {
log(Level.SEVERE, msgSupplier);
}

public String formatError(Throwable throwable) {
if (throwable == null) return null;
if (throwable == null)
return null;
final StringWriter buffer = new StringWriter();
buffer.append(throwable.getMessage());
throwable.printStackTrace(new PrintWriter(buffer));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@
import org.hyperledger.fabric.shim.ChaincodeStub;

/**
* Context provides {@link ChaincodeStub} API for handling world state
*
* This context is available to all 'transaction functions' and provides the
* transaction context.
*
* It also provides access to the APIs for the world state. {@see ChaincodeStub}
*
* Applications can implement their own versions if they wish to add
* functionality.
*/
public interface Context extends ChaincodeStub {
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
/*
Copyright IBM Corp., DTCC All Rights Reserved.
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package org.hyperledger.fabric.contract;

import org.hyperledger.fabric.shim.ChaincodeStub;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import org.hyperledger.fabric.shim.ChaincodeStub;

/**
* Factory to create {@link Context} from {@link ChaincodeStub}
* by wrapping stub with dynamic proxy.
* Factory to create {@link Context} from {@link ChaincodeStub} by wrapping stub
* with dynamic proxy.
*/
public class ContextFactory {
private static ContextFactory cf;
Expand All @@ -27,11 +27,8 @@ static synchronized public ContextFactory getInstance() {
}

public synchronized Context createContext(final ChaincodeStub stub) {
Context newContext = (Context) Proxy.newProxyInstance(
this.getClass().getClassLoader(),
new Class[]{Context.class},
new ContextInvocationHandler(stub)
);
Context newContext = (Context) Proxy.newProxyInstance(this.getClass().getClassLoader(),
new Class[] { Context.class }, new ContextInvocationHandler(stub));
return newContext;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package org.hyperledger.fabric.contract;

import org.hyperledger.fabric.shim.ChaincodeStub;
import org.hyperledger.fabric.shim.ResponseUtils;

/**
* Interface all contracts should implement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.hyperledger.fabric.contract.routing.ContractDefinition;
import org.hyperledger.fabric.contract.routing.RoutingRegistry;
import org.hyperledger.fabric.contract.routing.TxFunction;
import org.hyperledger.fabric.contract.routing.TxFunction.Routing;
import org.hyperledger.fabric.contract.routing.TypeRegistry;
import org.hyperledger.fabric.contract.routing.impl.RoutingRegistryImpl;
import org.hyperledger.fabric.contract.routing.impl.TypeRegistryImpl;
Expand All @@ -27,114 +26,118 @@
* {@link org.hyperledger.fabric.shim.Chaincode} interface.
*/
public class ContractRouter extends ChaincodeBase {
private static Logger logger = Logger.getLogger(ContractRouter.class.getName());

private RoutingRegistry registry;
private TypeRegistry typeRegistry;
private ExecutionService executor;

/**
* Take the arguments from the cli, and initiate processing of cli options and
* environment variables.
*
* Create the Contract scanner, and the Execution service
*
* @param args
*/
public ContractRouter(String[] args) {
super.initializeLogging();
super.processEnvironmentOptions();
super.processCommandLineOptions(args);

super.validateOptions();
registry = new RoutingRegistryImpl();
typeRegistry = new TypeRegistryImpl();
executor = ExecutionFactory.getInstance().createExecutionService();
}

/**
* Locate all the contracts that are available on the classpath
*/
void findAllContracts() {
registry.findAndSetContracts(this.typeRegistry);
}

/**
* Start the chaincode container off and running, this will send the initial
* flow back to the peer
*
* @throws Exception
*/
void startRouting() {
try {
super.connectToPeer();
} catch (Exception e) {
ContractRuntimeException cre = new ContractRuntimeException("Unable to start routing");
logger.error(()->logger.formatError(cre));
throw cre;
}
}

@Override
public Response init(ChaincodeStub stub) {
InvocationRequest request = ExecutionFactory.getInstance().createRequest(stub);
Routing routing = getRouting(request);

logger.debug(() -> "Got routing:" + routing);
return executor.executeRequest(routing, request, stub);
}

@Override
public Response invoke(ChaincodeStub stub) {
logger.debug(() -> "Got the invocations:" + stub.getFunction() + " " + stub.getParameters());
InvocationRequest request = ExecutionFactory.getInstance().createRequest(stub);
Routing routing = getRouting(request);

logger.debug(() -> "Got routing:" + routing);
return executor.executeRequest(routing, request, stub);
}

/**
* Given the Invocation Request, return the routing object for this call
*
* @param request
* @return
*/
TxFunction.Routing getRouting(InvocationRequest request) {
//request name is the fully qualified 'name:txname'
private static Logger logger = Logger.getLogger(ContractRouter.class.getName());

private RoutingRegistry registry;
private TypeRegistry typeRegistry;
private ExecutionService executor;

/**
* Take the arguments from the cli, and initiate processing of cli options and
* environment variables.
*
* Create the Contract scanner, and the Execution service
*
* @param args
*/
public ContractRouter(String[] args) {
super.initializeLogging();
super.processEnvironmentOptions();
super.processCommandLineOptions(args);

super.validateOptions();
logger.debug("ContractRouter<init>");
registry = new RoutingRegistryImpl();
typeRegistry = new TypeRegistryImpl();
executor = ExecutionFactory.getInstance().createExecutionService(typeRegistry);
}

/**
* Locate all the contracts that are available on the classpath
*/
protected void findAllContracts() {
registry.findAndSetContracts(this.typeRegistry);
}

/**
* Start the chaincode container off and running, this will send the initial
* flow back to the peer
*
* @throws Exception
*/
void startRouting() {
try {
super.connectToPeer();
} catch (Exception e) {
ContractRuntimeException cre = new ContractRuntimeException("Unable to start routing");
logger.error(() -> logger.formatError(cre));
throw cre;
}
}

@Override
public Response invoke(ChaincodeStub stub) {
logger.info(() -> "Got invoke routing request");
if (stub.getStringArgs().size() > 0) {
logger.info(() -> "Got the invoke request for:" + stub.getFunction() + " " + stub.getParameters());
InvocationRequest request = ExecutionFactory.getInstance().createRequest(stub);
TxFunction txFn = getRouting(request);

logger.info(() -> "Got routing:" + txFn.getRouting());
return executor.executeRequest(txFn, request, stub);
} else {
return ResponseUtils.newSuccessResponse();
}

}

@Override
public Response init(ChaincodeStub stub) {
return invoke(stub);
}

/**
* Given the Invocation Request, return the routing object for this call
*
* @param request
* @return
*/
TxFunction getRouting(InvocationRequest request) {
// request name is the fully qualified 'name:txname'
if (registry.containsRoute(request)) {
return registry.getRoute(request);
return registry.getTxFn(request);
} else {
ContractDefinition contract = registry.getContract(request.getNamespace());
return contract.getUnkownRoute();
logger.debug(() -> "Namespace is " + request);
ContractDefinition contract = registry.getContract(request.getNamespace());
return contract.getUnkownRoute();
}
}
}

/**
* Main method to start the contract based chaincode
*
*/
public static void main(String[] args) {
/**
* Main method to start the contract based chaincode
*
*/
public static void main(String[] args) {

ContractRouter cfc = new ContractRouter(args);
cfc.findAllContracts();
ContractRouter cfc = new ContractRouter(args);
cfc.findAllContracts();

// Create the Metadata ahead of time rather than have to produce every
// time
MetadataBuilder.initialize(cfc.getRoutingRegistry(),cfc.getTypeRegistry());
logger.info(() -> "Metadata follows:" + MetadataBuilder.debugString());
// Create the Metadata ahead of time rather than have to produce every
// time
MetadataBuilder.initialize(cfc.getRoutingRegistry(), cfc.getTypeRegistry());
logger.info(() -> "Metadata follows:" + MetadataBuilder.debugString());

// commence routing, once this has returned the chaincode and contract api is
// 'open for business'
cfc.startRouting();
// commence routing, once this has returned the chaincode and contract api is
// 'open for chaining'
cfc.startRouting();

}
}

private TypeRegistry getTypeRegistry() {
return this.typeRegistry;
}
protected TypeRegistry getTypeRegistry() {
return this.typeRegistry;
}

private RoutingRegistry getRoutingRegistry() {
return this.registry;
}
protected RoutingRegistry getRoutingRegistry() {
return this.registry;
}
}
Loading

0 comments on commit d726175

Please sign in to comment.