Skip to content

Commit aa62891

Browse files
committed
✨ 🍱 🎉 Init add endorsement chaincode examples
1 parent c181c10 commit aa62891

File tree

1 file changed

+180
-0
lines changed

1 file changed

+180
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
package io.github.hooj0.chaincode;
2+
3+
import java.lang.reflect.Method;
4+
import java.util.List;
5+
6+
import org.apache.commons.logging.Log;
7+
import org.apache.commons.logging.LogFactory;
8+
import org.hyperledger.fabric.shim.ChaincodeBase;
9+
import org.hyperledger.fabric.shim.ChaincodeStub;
10+
import org.hyperledger.fabric.shim.ext.sbe.StateBasedEndorsement;
11+
import org.hyperledger.fabric.shim.ext.sbe.StateBasedEndorsement.RoleType;
12+
import org.hyperledger.fabric.shim.ext.sbe.impl.StateBasedEndorsementFactory;
13+
14+
import com.google.gson.JsonArray;
15+
16+
/**
17+
* endorsement chaincode examples
18+
* @author hoojo
19+
* @createDate 2018年12月4日 上午11:14:32
20+
* @file EndorsementChaincode.java
21+
* @package io.github.hooj0.chaincode
22+
* @project fabric-chaincode-endorsement-maven
23+
* @blog http://hoojo.cnblogs.com
24+
25+
* @version 1.0
26+
*/
27+
public class EndorsementChaincode extends ChaincodeBase {
28+
29+
private static final Log log = LogFactory.getLog(EndorsementChaincode.class);
30+
31+
private static final String STATE_NAME = "endorsed_state";
32+
33+
@Override
34+
public Response init(ChaincodeStub stub) {
35+
log.info(">>> init endorsement chaincode");
36+
37+
try {
38+
stub.putStringState(STATE_NAME, "foo");
39+
40+
return newSuccessResponse();
41+
} catch (Exception e) {
42+
log.error(e.getMessage(), e);
43+
return newErrorResponse(e);
44+
}
45+
}
46+
47+
@Override
48+
public Response invoke(ChaincodeStub stub) {
49+
log.info(">>> invoke endorsement chaincode");
50+
51+
try {
52+
53+
String func = stub.getFunction();
54+
55+
Method method = EndorsementChaincode.class.getMethod(func, ChaincodeStub.class);
56+
Response response = (Response) method.invoke(this, stub);
57+
58+
return response;
59+
} catch (Exception e) {
60+
log.error(e.getMessage(), e);
61+
return newErrorResponse(e);
62+
}
63+
}
64+
65+
public Response addOrg(ChaincodeStub stub) {
66+
log.info(">>> endorsement chaincode invoking addOrg method.");
67+
68+
try {
69+
List<String> params = stub.getParameters();
70+
if (params.isEmpty()) {
71+
return newErrorResponse("add orgs params is empty!");
72+
}
73+
74+
byte[] bytes = this.getEndorsement(stub);
75+
76+
StateBasedEndorsement endorsement = StateBasedEndorsementFactory.getInstance().newStateBasedEndorsement(bytes);
77+
endorsement.addOrgs(RoleType.RoleTypePeer, params.toArray(new String[] {}));
78+
79+
this.setEndorsement(stub, endorsement.policy());
80+
81+
return newSuccessResponse();
82+
} catch (Exception e) {
83+
log.error(e.getMessage(), e);
84+
return newErrorResponse(e);
85+
}
86+
}
87+
88+
public Response removeOrg(ChaincodeStub stub) {
89+
log.info(">>> endorsement chaincode invoking remove Org method.");
90+
91+
try {
92+
List<String> params = stub.getParameters();
93+
if (params.isEmpty()) {
94+
return newErrorResponse("remove orgs params is empty!");
95+
}
96+
97+
byte[] bytes = this.getEndorsement(stub);
98+
99+
StateBasedEndorsement endorsement = StateBasedEndorsementFactory.getInstance().newStateBasedEndorsement(bytes);
100+
endorsement.delOrgs(params.toArray(new String[] {}));
101+
102+
this.setEndorsement(stub, endorsement.policy());
103+
104+
return newSuccessResponse();
105+
} catch (Exception e) {
106+
log.error(e.getMessage(), e);
107+
return newErrorResponse(e);
108+
}
109+
}
110+
111+
public Response listOrg(ChaincodeStub stub) {
112+
log.info(">>> endorsement chaincode invoking listOrg method.");
113+
114+
try {
115+
byte[] bytes = this.getEndorsement(stub);
116+
117+
StateBasedEndorsement endorsement = StateBasedEndorsementFactory.getInstance().newStateBasedEndorsement(bytes);
118+
List<String> orgs = endorsement.listOrgs();
119+
120+
JsonArray array = new JsonArray();
121+
orgs.forEach(e -> orgs.add(e));
122+
123+
return newSuccessResponse(array.toString().getBytes());
124+
} catch (Exception e) {
125+
log.error(e.getMessage(), e);
126+
return newErrorResponse(e);
127+
}
128+
}
129+
130+
public Response clean(ChaincodeStub stub) {
131+
log.info(">>> endorsement chaincode invoking clean method.");
132+
133+
try {
134+
this.setEndorsement(stub, null);
135+
136+
return newSuccessResponse();
137+
} catch (Exception e) {
138+
log.error(e.getMessage(), e);
139+
return newErrorResponse(e);
140+
}
141+
}
142+
143+
public Response get(ChaincodeStub stub) {
144+
log.info(">>> endorsement chaincode invoking get method.");
145+
146+
try {
147+
return newSuccessResponse(stub.getState(STATE_NAME));
148+
} catch (Exception e) {
149+
log.error(e.getMessage(), e);
150+
return newErrorResponse(e);
151+
}
152+
}
153+
154+
public Response set(ChaincodeStub stub) {
155+
log.info(">>> endorsement chaincode invoking set method.");
156+
157+
try {
158+
List<String> params = stub.getParameters();
159+
if (params.size() != 1) {
160+
return newErrorResponse("set params size Extra long!");
161+
}
162+
163+
stub.putStringState(STATE_NAME, params.get(0));
164+
165+
return newSuccessResponse();
166+
} catch (Exception e) {
167+
log.error(e.getMessage(), e);
168+
return newErrorResponse(e);
169+
}
170+
}
171+
172+
private byte[] getEndorsement(ChaincodeStub stub) {
173+
//return stub.getStateValidationParameter(STATE_NAME);
174+
return new byte[0];
175+
}
176+
177+
private void setEndorsement(ChaincodeStub stub, byte[] bytes) {
178+
//stub.setStateValidationParameter(STATE_NAME, bytes);
179+
}
180+
}

0 commit comments

Comments
 (0)