Skip to content

Commit d726175

Browse files
committed
[FAB-13803] Parameter Marshalling
- 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]>
1 parent 72dc716 commit d726175

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+2197
-896
lines changed

fabric-chaincode-example-gradle/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ repositories {
1515
}
1616

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

fabric-chaincode-protos/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ buildscript {
4545
}
4646

4747
dependencies {
48-
compile 'com.google.protobuf:protobuf-java:3.5.1'
48+
compile 'com.google.protobuf:protobuf-java:3.7.1'
4949
compile 'com.google.protobuf:protobuf-java-util:3.5.1'
5050
compile 'io.grpc:grpc-netty-shaded:1.9.0'
5151
compile 'io.grpc:grpc-protobuf:1.9.0'

fabric-chaincode-shim/build.gradle

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ jacocoTestCoverageVerification {
7878
'org.hyperledger.fabric.contract.routing.impl.ContractDefinitionImpl',
7979
'org.hyperledger.fabric.contract.routing.RoutingRegistry',
8080
'org.hyperledger.fabric.contract.execution.impl.ContractInvocationRequest',
81-
'org.hyperledger.fabric.contract.routing.TransactionType']
81+
'org.hyperledger.fabric.contract.routing.TransactionType',
82+
'org.hyperledger.fabric.contract.metadata.MetadataBuilder']
8283
limit {
8384
minimum = 0.86
8485
}
@@ -91,7 +92,8 @@ jacocoTestCoverageVerification {
9192
'org.hyperledger.fabric.contract.execution.impl.ContractInvocationRequest',
9293
'org.hyperledger.fabric.contract.routing.impl.ContractDefinitionImpl',
9394
'org.hyperledger.fabric.contract.routing.RoutingRegistry',
94-
'org.hyperledger.fabric.shim.impl.Handler']
95+
'org.hyperledger.fabric.shim.impl.Handler',
96+
'org.hyperledger.fabric.contract.metadata.MetadataBuilder']
9597
limit {
9698
minimum = 0.71
9799
}

fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/Logger.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,19 @@
99
import java.io.StringWriter;
1010
import java.util.function.Supplier;
1111
import java.util.logging.Level;
12+
import java.util.logging.LogManager;
1213

13-
public class Logger extends java.util.logging.Logger{
14+
/**
15+
* Logger class to use throughout the Contract Implementation
16+
*
17+
*/
18+
public class Logger extends java.util.logging.Logger {
1419

1520
protected Logger(String name) {
1621
super(name, null);
22+
23+
// ensure that the parent logger is set
24+
this.setParent(java.util.logging.Logger.getLogger("org.hyperledger.fabric"));
1725
}
1826

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

3139
public static Logger getLogger(Class<?> class1) {
32-
return Logger.getLogger(class1.getName());
40+
// important to add the logger to the log manager
41+
Logger l = Logger.getLogger(class1.getName());
42+
LogManager.getLogManager().addLogger(l);
43+
return l;
3344
}
3445

3546
public void error(String message) {
36-
log(Level.SEVERE,message);
47+
log(Level.SEVERE, message);
3748
}
3849

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

4354
public String formatError(Throwable throwable) {
44-
if (throwable == null) return null;
55+
if (throwable == null)
56+
return null;
4557
final StringWriter buffer = new StringWriter();
4658
buffer.append(throwable.getMessage());
4759
throwable.printStackTrace(new PrintWriter(buffer));

fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/Context.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,14 @@
99
import org.hyperledger.fabric.shim.ChaincodeStub;
1010

1111
/**
12-
* Context provides {@link ChaincodeStub} API for handling world state
12+
*
13+
* This context is available to all 'transaction functions' and provides the
14+
* transaction context.
15+
*
16+
* It also provides access to the APIs for the world state. {@see ChaincodeStub}
17+
*
18+
* Applications can implement their own versions if they wish to add
19+
* functionality.
1320
*/
1421
public interface Context extends ChaincodeStub {
1522
}

fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/ContextFactory.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
/*
2-
Copyright IBM Corp., DTCC All Rights Reserved.
2+
Copyright IBM Corp. All Rights Reserved.
33
44
SPDX-License-Identifier: Apache-2.0
55
*/
66

77
package org.hyperledger.fabric.contract;
88

9-
import org.hyperledger.fabric.shim.ChaincodeStub;
10-
119
import java.lang.reflect.InvocationHandler;
1210
import java.lang.reflect.Method;
1311
import java.lang.reflect.Proxy;
1412

13+
import org.hyperledger.fabric.shim.ChaincodeStub;
14+
1515
/**
16-
* Factory to create {@link Context} from {@link ChaincodeStub}
17-
* by wrapping stub with dynamic proxy.
16+
* Factory to create {@link Context} from {@link ChaincodeStub} by wrapping stub
17+
* with dynamic proxy.
1818
*/
1919
public class ContextFactory {
2020
private static ContextFactory cf;
@@ -27,11 +27,8 @@ static synchronized public ContextFactory getInstance() {
2727
}
2828

2929
public synchronized Context createContext(final ChaincodeStub stub) {
30-
Context newContext = (Context) Proxy.newProxyInstance(
31-
this.getClass().getClassLoader(),
32-
new Class[]{Context.class},
33-
new ContextInvocationHandler(stub)
34-
);
30+
Context newContext = (Context) Proxy.newProxyInstance(this.getClass().getClassLoader(),
31+
new Class[] { Context.class }, new ContextInvocationHandler(stub));
3532
return newContext;
3633
}
3734

fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/ContractInterface.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
package org.hyperledger.fabric.contract;
88

99
import org.hyperledger.fabric.shim.ChaincodeStub;
10-
import org.hyperledger.fabric.shim.ResponseUtils;
1110

1211
/**
1312
* Interface all contracts should implement

fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/contract/ContractRouter.java

Lines changed: 104 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import org.hyperledger.fabric.contract.routing.ContractDefinition;
1515
import org.hyperledger.fabric.contract.routing.RoutingRegistry;
1616
import org.hyperledger.fabric.contract.routing.TxFunction;
17-
import org.hyperledger.fabric.contract.routing.TxFunction.Routing;
1817
import org.hyperledger.fabric.contract.routing.TypeRegistry;
1918
import org.hyperledger.fabric.contract.routing.impl.RoutingRegistryImpl;
2019
import org.hyperledger.fabric.contract.routing.impl.TypeRegistryImpl;
@@ -27,114 +26,118 @@
2726
* {@link org.hyperledger.fabric.shim.Chaincode} interface.
2827
*/
2928
public class ContractRouter extends ChaincodeBase {
30-
private static Logger logger = Logger.getLogger(ContractRouter.class.getName());
31-
32-
private RoutingRegistry registry;
33-
private TypeRegistry typeRegistry;
34-
private ExecutionService executor;
35-
36-
/**
37-
* Take the arguments from the cli, and initiate processing of cli options and
38-
* environment variables.
39-
*
40-
* Create the Contract scanner, and the Execution service
41-
*
42-
* @param args
43-
*/
44-
public ContractRouter(String[] args) {
45-
super.initializeLogging();
46-
super.processEnvironmentOptions();
47-
super.processCommandLineOptions(args);
48-
49-
super.validateOptions();
50-
registry = new RoutingRegistryImpl();
51-
typeRegistry = new TypeRegistryImpl();
52-
executor = ExecutionFactory.getInstance().createExecutionService();
53-
}
54-
55-
/**
56-
* Locate all the contracts that are available on the classpath
57-
*/
58-
void findAllContracts() {
59-
registry.findAndSetContracts(this.typeRegistry);
60-
}
61-
62-
/**
63-
* Start the chaincode container off and running, this will send the initial
64-
* flow back to the peer
65-
*
66-
* @throws Exception
67-
*/
68-
void startRouting() {
69-
try {
70-
super.connectToPeer();
71-
} catch (Exception e) {
72-
ContractRuntimeException cre = new ContractRuntimeException("Unable to start routing");
73-
logger.error(()->logger.formatError(cre));
74-
throw cre;
75-
}
76-
}
77-
78-
@Override
79-
public Response init(ChaincodeStub stub) {
80-
InvocationRequest request = ExecutionFactory.getInstance().createRequest(stub);
81-
Routing routing = getRouting(request);
82-
83-
logger.debug(() -> "Got routing:" + routing);
84-
return executor.executeRequest(routing, request, stub);
85-
}
86-
87-
@Override
88-
public Response invoke(ChaincodeStub stub) {
89-
logger.debug(() -> "Got the invocations:" + stub.getFunction() + " " + stub.getParameters());
90-
InvocationRequest request = ExecutionFactory.getInstance().createRequest(stub);
91-
Routing routing = getRouting(request);
92-
93-
logger.debug(() -> "Got routing:" + routing);
94-
return executor.executeRequest(routing, request, stub);
95-
}
96-
97-
/**
98-
* Given the Invocation Request, return the routing object for this call
99-
*
100-
* @param request
101-
* @return
102-
*/
103-
TxFunction.Routing getRouting(InvocationRequest request) {
104-
//request name is the fully qualified 'name:txname'
29+
private static Logger logger = Logger.getLogger(ContractRouter.class.getName());
30+
31+
private RoutingRegistry registry;
32+
private TypeRegistry typeRegistry;
33+
private ExecutionService executor;
34+
35+
/**
36+
* Take the arguments from the cli, and initiate processing of cli options and
37+
* environment variables.
38+
*
39+
* Create the Contract scanner, and the Execution service
40+
*
41+
* @param args
42+
*/
43+
public ContractRouter(String[] args) {
44+
super.initializeLogging();
45+
super.processEnvironmentOptions();
46+
super.processCommandLineOptions(args);
47+
48+
super.validateOptions();
49+
logger.debug("ContractRouter<init>");
50+
registry = new RoutingRegistryImpl();
51+
typeRegistry = new TypeRegistryImpl();
52+
executor = ExecutionFactory.getInstance().createExecutionService(typeRegistry);
53+
}
54+
55+
/**
56+
* Locate all the contracts that are available on the classpath
57+
*/
58+
protected void findAllContracts() {
59+
registry.findAndSetContracts(this.typeRegistry);
60+
}
61+
62+
/**
63+
* Start the chaincode container off and running, this will send the initial
64+
* flow back to the peer
65+
*
66+
* @throws Exception
67+
*/
68+
void startRouting() {
69+
try {
70+
super.connectToPeer();
71+
} catch (Exception e) {
72+
ContractRuntimeException cre = new ContractRuntimeException("Unable to start routing");
73+
logger.error(() -> logger.formatError(cre));
74+
throw cre;
75+
}
76+
}
77+
78+
@Override
79+
public Response invoke(ChaincodeStub stub) {
80+
logger.info(() -> "Got invoke routing request");
81+
if (stub.getStringArgs().size() > 0) {
82+
logger.info(() -> "Got the invoke request for:" + stub.getFunction() + " " + stub.getParameters());
83+
InvocationRequest request = ExecutionFactory.getInstance().createRequest(stub);
84+
TxFunction txFn = getRouting(request);
85+
86+
logger.info(() -> "Got routing:" + txFn.getRouting());
87+
return executor.executeRequest(txFn, request, stub);
88+
} else {
89+
return ResponseUtils.newSuccessResponse();
90+
}
91+
92+
}
93+
94+
@Override
95+
public Response init(ChaincodeStub stub) {
96+
return invoke(stub);
97+
}
98+
99+
/**
100+
* Given the Invocation Request, return the routing object for this call
101+
*
102+
* @param request
103+
* @return
104+
*/
105+
TxFunction getRouting(InvocationRequest request) {
106+
// request name is the fully qualified 'name:txname'
105107
if (registry.containsRoute(request)) {
106-
return registry.getRoute(request);
108+
return registry.getTxFn(request);
107109
} else {
108-
ContractDefinition contract = registry.getContract(request.getNamespace());
109-
return contract.getUnkownRoute();
110+
logger.debug(() -> "Namespace is " + request);
111+
ContractDefinition contract = registry.getContract(request.getNamespace());
112+
return contract.getUnkownRoute();
110113
}
111-
}
114+
}
112115

113-
/**
114-
* Main method to start the contract based chaincode
115-
*
116-
*/
117-
public static void main(String[] args) {
116+
/**
117+
* Main method to start the contract based chaincode
118+
*
119+
*/
120+
public static void main(String[] args) {
118121

119-
ContractRouter cfc = new ContractRouter(args);
120-
cfc.findAllContracts();
122+
ContractRouter cfc = new ContractRouter(args);
123+
cfc.findAllContracts();
121124

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

127-
// commence routing, once this has returned the chaincode and contract api is
128-
// 'open for business'
129-
cfc.startRouting();
130+
// commence routing, once this has returned the chaincode and contract api is
131+
// 'open for chaining'
132+
cfc.startRouting();
130133

131-
}
134+
}
132135

133-
private TypeRegistry getTypeRegistry() {
134-
return this.typeRegistry;
135-
}
136+
protected TypeRegistry getTypeRegistry() {
137+
return this.typeRegistry;
138+
}
136139

137-
private RoutingRegistry getRoutingRegistry() {
138-
return this.registry;
139-
}
140+
protected RoutingRegistry getRoutingRegistry() {
141+
return this.registry;
142+
}
140143
}

0 commit comments

Comments
 (0)