1
1
/*
2
- Copyright DTCC, IBM 2016, 2017 All Rights Reserved.
2
+ Copyright IBM 2017 All Rights Reserved.
3
3
4
4
Licensed under the Apache License, Version 2.0 (the "License");
5
5
you may not use this file except in compliance with the License.
13
13
See the License for the specific language governing permissions and
14
14
limitations under the License.
15
15
*/
16
-
17
16
package org .hyperledger .fabric .shim ;
18
17
19
18
import java .nio .charset .StandardCharsets ;
20
19
import java .util .Arrays ;
21
- import java .util .Collections ;
22
20
import java .util .List ;
23
21
import java .util .stream .Collectors ;
24
22
25
- import org .apache .commons .logging .Log ;
26
- import org .apache .commons .logging .LogFactory ;
27
23
import org .hyperledger .fabric .protos .peer .ChaincodeEventPackage .ChaincodeEvent ;
28
24
import org .hyperledger .fabric .protos .peer .ProposalResponsePackage .Response ;
29
25
30
26
import com .google .protobuf .ByteString ;
31
27
32
- public class ChaincodeStub {
33
-
34
- private static Log logger = LogFactory .getLog (ChaincodeStub .class );
35
-
36
- private final String uuid ;
37
- private final Handler handler ;
38
- private final List <ByteString > args ;
39
- private ChaincodeEvent event ;
40
-
41
- public ChaincodeStub (String uuid , Handler handler , List <ByteString > args ) {
42
- this .uuid = uuid ;
43
- this .handler = handler ;
44
- this .args = Collections .unmodifiableList (args );
45
- }
46
-
47
- public List <byte []> getArgs () {
48
- return args .stream ().map (x -> x .toByteArray ()).collect (Collectors .toList ());
49
- }
50
-
51
- public List <String > getArgsAsStrings () {
52
- return args .stream ().map (x -> x .toStringUtf8 ()).collect (Collectors .toList ());
53
- }
28
+ public interface ChaincodeStub {
54
29
55
30
/**
56
- * Defines the CHAINCODE type event that will be posted to interested
57
- * clients when the chaincode's result is committed to the ledger.
31
+ * Returns the arguments corresponding to the call to
32
+ * {@link Chaincode#init(ChaincodeStub)} or
33
+ * {@link Chaincode#invoke(ChaincodeStub)}.
58
34
*
59
- * @param name
60
- * Name of event. Cannot be null or empty string.
61
- * @param payload
62
- * Optional event payload.
35
+ * @return a list of arguments
63
36
*/
64
- public void setEvent (String name , byte [] payload ) {
65
- if (name == null || name .trim ().length () == 0 ) throw new IllegalArgumentException ("Event name cannot be null or empty string." );
66
- if (payload != null ) {
67
- this .event = ChaincodeEvent .newBuilder ()
68
- .setEventName (name )
69
- .setPayload (ByteString .copyFrom (payload ))
70
- .build ();
71
- } else {
72
- this .event = ChaincodeEvent .newBuilder ()
73
- .setEventName (name )
74
- .build ();
75
- }
76
- }
37
+ List <byte []> getArgs ();
77
38
78
- ChaincodeEvent getEvent () {
79
- return event ;
80
- }
39
+ /**
40
+ * Returns the arguments corresponding to the call to
41
+ * {@link Chaincode#init(ChaincodeStub)} or
42
+ * {@link Chaincode#invoke(ChaincodeStub)}.
43
+ *
44
+ * @return a list of arguments cast to a UTF-8 string
45
+ */
46
+ List <String > getArgsAsStrings ();
81
47
82
48
/**
83
- * Gets the UUID of this stub
49
+ * Returns the transaction id
84
50
*
85
- * @return the id used to identify this communication channel
51
+ * @return the transaction id
86
52
*/
87
- public String getUuid () {
88
- return uuid ;
89
- }
53
+ String getTxId ();
54
+
55
+ /**
56
+ * Invoke another chaincode using the same transaction context.
57
+ *
58
+ * @param chaincodeName Name of chaincode to be invoked.
59
+ * @param args Arguments to pass on to the called chaincode.
60
+ * @param channel If not specified, the caller's channel is assumed.
61
+ * @return
62
+ */
63
+ Response invokeChaincode (String chaincodeName , List <byte []> args , String channel );
90
64
91
65
/**
92
66
* Get the state of the provided key from the ledger, and returns is as a
@@ -96,9 +70,7 @@ public String getUuid() {
96
70
* the key of the desired state
97
71
* @return the String value of the requested state
98
72
*/
99
- public String getState (String key ) {
100
- return handler .handleGetState (key , uuid ).toStringUtf8 ();
101
- }
73
+ String getState (String key );
102
74
103
75
/**
104
76
* Puts the given state into a ledger, automatically wrapping it in a
@@ -109,76 +81,15 @@ public String getState(String key) {
109
81
* @param value
110
82
* value to be put
111
83
*/
112
- public void putState (String key , String value ) {
113
- handler .handlePutState (key , ByteString .copyFromUtf8 (value ), uuid );
114
- }
84
+ void putState (String key , String value );
115
85
116
86
/**
117
87
* Deletes the state of the given key from the ledger
118
88
*
119
89
* @param key
120
90
* key of the state to be deleted
121
91
*/
122
- public void delState (String key ) {
123
- handler .handleDeleteState (key , uuid );
124
- }
125
- /**
126
- * Given a start key and end key, this method returns a map of items with
127
- * value converted to UTF-8 string.
128
- *
129
- * @param startKey
130
- * @param endKey
131
- * @return
132
- */
133
- // TODO: Uncomment and fix range query with new proto type
134
- /*
135
- public Map<String, String> getStateByRange(String startKey, String endKey) {
136
- Map<String, String> retMap = new HashMap<>();
137
- for (Map.Entry<String, ByteString> item : getStateByRangeRaw(startKey, endKey).entrySet()) {
138
- retMap.put(item.getKey(), item.getValue().toStringUtf8());
139
- }
140
- return retMap;
141
- }
142
- */
143
- /**
144
- * This method is same as getStateByRange, except it returns value in
145
- * ByteString, useful in cases where serialized object can be retrieved.
146
- *
147
- * @param startKey
148
- * @param endKey
149
- * @return
150
- */
151
- // TODO: Uncomment and fix range query with new proto type
152
- /*
153
- public Map<String, ByteString> getStateByRangeRaw(String startKey, String endKey) {
154
- Map<String, ByteString> map = new HashMap<>();
155
- for (ChaincodeShim.QueryStateKeyValue mapping : handler.handleGetStateByRange(startKey, endKey, uuid).getKeysAndValuesList()) {
156
- map.put(mapping.getKey(), mapping.getValue());
157
- }
158
- return map;
159
- }
160
- */
161
-
162
- /**
163
- * Given a partial composite key, this method returns a map of items (whose
164
- * key's prefix matches the given partial composite key) with value
165
- * converted to UTF-8 string and this methid should be used only for a
166
- * partial composite key; For a full composite key, an iter with empty
167
- * response would be returned.
168
- *
169
- * @param startKey
170
- * @param endKey
171
- * @return
172
- */
173
-
174
- // TODO: Uncomment and fix range query with new proto type
175
- /*
176
- public Map<String, String> getStateByPartialCompositeKey(String objectType, String[] attributes) {
177
- String partialCompositeKey = new String();
178
- partialCompositeKey = createCompositeKey(objectType, attributes);
179
- return getStateByRange(partialCompositeKey + "1", partialCompositeKey + ":");
180
- }
181
- */
92
+ void delState (String key );
182
93
183
94
/**
184
95
* Given a set of attributes, this method combines these attributes to
@@ -188,33 +99,18 @@ public Map<String, String> getStateByPartialCompositeKey(String objectType, Stri
188
99
* @param attributes
189
100
* @return
190
101
*/
191
- public String createCompositeKey (String objectType , String [] attributes ) {
192
- String compositeKey = new String ();
193
- compositeKey = compositeKey + objectType ;
194
- for (String attribute : attributes ) {
195
- compositeKey = compositeKey + attribute .length () + attribute ;
196
- }
197
- return compositeKey ;
198
- }
102
+ String createCompositeKey (String objectType , String [] attributes );
199
103
200
104
/**
201
- * Invoke another chaincode using the same transaction context.
105
+ * Defines the CHAINCODE type event that will be posted to interested
106
+ * clients when the chaincode's result is committed to the ledger.
202
107
*
203
- * @param chaincodeName Name of chaincode to be invoked.
204
- * @param args Arguments to pass on to the called chaincode .
205
- * @param channel If not specified, the caller's channel is assumed.
206
- * @return
108
+ * @param name
109
+ * Name of event. Cannot be null or empty string .
110
+ * @param payload
111
+ * Optional event payload.
207
112
*/
208
- public Response invokeChaincode (final String chaincodeName , final List <byte []> args , final String channel ) {
209
- // internally we handle chaincode name as a composite name
210
- final String compositeName ;
211
- if (channel != null && channel .trim ().length () > 0 ) {
212
- compositeName = chaincodeName + "/" + channel ;
213
- } else {
214
- compositeName = chaincodeName ;
215
- }
216
- return handler .handleInvokeChaincode (compositeName , args , this .uuid );
217
- }
113
+ void setEvent (String name , byte [] payload );
218
114
219
115
/**
220
116
* Invoke another chaincode using the same transaction context.
@@ -223,10 +119,10 @@ public Response invokeChaincode(final String chaincodeName, final List<byte[]> a
223
119
* @param args Arguments to pass on to the called chaincode.
224
120
* @return
225
121
*/
226
- public Response invokeChaincode (final String chaincodeName , final List <byte []> args ) {
122
+ default Response invokeChaincode (String chaincodeName , List <byte []> args ) {
227
123
return invokeChaincode (chaincodeName , args , null );
228
124
}
229
-
125
+
230
126
/**
231
127
* Invoke another chaincode using the same transaction context.
232
128
*
@@ -238,10 +134,10 @@ public Response invokeChaincode(final String chaincodeName, final List<byte[]> a
238
134
* @param channel If not specified, the caller's channel is assumed.
239
135
* @return
240
136
*/
241
- public Response invokeChaincodeWithStringArgs (final String chaincodeName , final List <String > args , final String channel ) {
137
+ default Response invokeChaincodeWithStringArgs (String chaincodeName , List <String > args , String channel ) {
242
138
return invokeChaincode (chaincodeName , args .stream ().map (x ->x .getBytes (StandardCharsets .UTF_8 )).collect (Collectors .toList ()), channel );
243
139
}
244
-
140
+
245
141
/**
246
142
* Invoke another chaincode using the same transaction context.
247
143
*
@@ -253,7 +149,7 @@ public Response invokeChaincodeWithStringArgs(final String chaincodeName, final
253
149
* @param args Arguments to pass on to the called chaincode.
254
150
* @return
255
151
*/
256
- public Response invokeChaincodeWithStringArgs (final String chaincodeName , final List <String > args ) {
152
+ default Response invokeChaincodeWithStringArgs (String chaincodeName , List <String > args ) {
257
153
return invokeChaincodeWithStringArgs (chaincodeName , args , null );
258
154
}
259
155
@@ -268,37 +164,27 @@ public Response invokeChaincodeWithStringArgs(final String chaincodeName, final
268
164
* @param args Arguments to pass on to the called chaincode.
269
165
* @return
270
166
*/
271
- public Response invokeChaincodeWithStringArgs (final String chaincodeName , final String ... args ) {
167
+ default Response invokeChaincodeWithStringArgs (final String chaincodeName , final String ... args ) {
272
168
return invokeChaincodeWithStringArgs (chaincodeName , Arrays .asList (args ), null );
273
169
}
274
170
275
- // ------RAW CALLS------
276
-
277
171
/**
278
172
* @param key
279
173
* @return
280
174
*/
281
- public ByteString getRawState (String key ) {
282
- return handler .handleGetState (key , uuid );
283
- }
175
+ ByteString getRawState (String key );
284
176
285
177
/**
286
178
* @param key
287
179
* @param value
288
180
*/
289
- public void putRawState (String key , ByteString value ) {
290
- handler .handlePutState (key , value , uuid );
291
- }
181
+ void putRawState (String key , ByteString value );
292
182
293
183
/**
294
- *
295
- * @param startKey
296
- * @param endKey
297
- * @param limit
298
- * @return
184
+ * Returns the CHAINCODE type event that will be posted to interested
185
+ * clients when the chaincode's result is committed to the ledger.
186
+ * @return the chaincode event or null
299
187
*/
300
- // public GetStateByRangeResponse getStateByRangeRaw(String startKey, String endKey, int limit) {
301
- // return handler.handleGetStateByRange(startKey, endKey, limit, uuid);
302
- // }
188
+ ChaincodeEvent getEvent ();
303
189
304
- }
190
+ }
0 commit comments