Skip to content

Commit de3d2d1

Browse files
author
Luis Sanchez
committed
[FAB-3222] extract java ChaincodeStub interface
- preparation for further cleanup of API - also changed getUuid() to getTxId() in this CR Change-Id: I83f01d7cc0e0b1dcc350e326f1ce06684f3b81b4 Signed-off-by: Luis Sanchez <[email protected]>
1 parent 99b6817 commit de3d2d1

File tree

3 files changed

+218
-168
lines changed

3 files changed

+218
-168
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright DTCC, IBM 2016, 2017 All Rights Reserved.
2+
Copyright IBM 2017 All Rights Reserved.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -13,80 +13,54 @@
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
16-
1716
package org.hyperledger.fabric.shim;
1817

1918
import java.nio.charset.StandardCharsets;
2019
import java.util.Arrays;
21-
import java.util.Collections;
2220
import java.util.List;
2321
import java.util.stream.Collectors;
2422

25-
import org.apache.commons.logging.Log;
26-
import org.apache.commons.logging.LogFactory;
2723
import org.hyperledger.fabric.protos.peer.ChaincodeEventPackage.ChaincodeEvent;
2824
import org.hyperledger.fabric.protos.peer.ProposalResponsePackage.Response;
2925

3026
import com.google.protobuf.ByteString;
3127

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 {
5429

5530
/**
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)}.
5834
*
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
6336
*/
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();
7738

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();
8147

8248
/**
83-
* Gets the UUID of this stub
49+
* Returns the transaction id
8450
*
85-
* @return the id used to identify this communication channel
51+
* @return the transaction id
8652
*/
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);
9064

9165
/**
9266
* Get the state of the provided key from the ledger, and returns is as a
@@ -96,9 +70,7 @@ public String getUuid() {
9670
* the key of the desired state
9771
* @return the String value of the requested state
9872
*/
99-
public String getState(String key) {
100-
return handler.handleGetState(key, uuid).toStringUtf8();
101-
}
73+
String getState(String key);
10274

10375
/**
10476
* Puts the given state into a ledger, automatically wrapping it in a
@@ -109,76 +81,15 @@ public String getState(String key) {
10981
* @param value
11082
* value to be put
11183
*/
112-
public void putState(String key, String value) {
113-
handler.handlePutState(key, ByteString.copyFromUtf8(value), uuid);
114-
}
84+
void putState(String key, String value);
11585

11686
/**
11787
* Deletes the state of the given key from the ledger
11888
*
11989
* @param key
12090
* key of the state to be deleted
12191
*/
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);
18293

18394
/**
18495
* Given a set of attributes, this method combines these attributes to
@@ -188,33 +99,18 @@ public Map<String, String> getStateByPartialCompositeKey(String objectType, Stri
18899
* @param attributes
189100
* @return
190101
*/
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);
199103

200104
/**
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.
202107
*
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.
207112
*/
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);
218114

219115
/**
220116
* Invoke another chaincode using the same transaction context.
@@ -223,10 +119,10 @@ public Response invokeChaincode(final String chaincodeName, final List<byte[]> a
223119
* @param args Arguments to pass on to the called chaincode.
224120
* @return
225121
*/
226-
public Response invokeChaincode(final String chaincodeName, final List<byte[]> args) {
122+
default Response invokeChaincode(String chaincodeName, List<byte[]> args) {
227123
return invokeChaincode(chaincodeName, args, null);
228124
}
229-
125+
230126
/**
231127
* Invoke another chaincode using the same transaction context.
232128
*
@@ -238,10 +134,10 @@ public Response invokeChaincode(final String chaincodeName, final List<byte[]> a
238134
* @param channel If not specified, the caller's channel is assumed.
239135
* @return
240136
*/
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) {
242138
return invokeChaincode(chaincodeName, args.stream().map(x->x.getBytes(StandardCharsets.UTF_8)).collect(Collectors.toList()), channel);
243139
}
244-
140+
245141
/**
246142
* Invoke another chaincode using the same transaction context.
247143
*
@@ -253,7 +149,7 @@ public Response invokeChaincodeWithStringArgs(final String chaincodeName, final
253149
* @param args Arguments to pass on to the called chaincode.
254150
* @return
255151
*/
256-
public Response invokeChaincodeWithStringArgs(final String chaincodeName, final List<String> args) {
152+
default Response invokeChaincodeWithStringArgs(String chaincodeName, List<String> args) {
257153
return invokeChaincodeWithStringArgs(chaincodeName, args, null);
258154
}
259155

@@ -268,37 +164,27 @@ public Response invokeChaincodeWithStringArgs(final String chaincodeName, final
268164
* @param args Arguments to pass on to the called chaincode.
269165
* @return
270166
*/
271-
public Response invokeChaincodeWithStringArgs(final String chaincodeName, final String... args) {
167+
default Response invokeChaincodeWithStringArgs(final String chaincodeName, final String... args) {
272168
return invokeChaincodeWithStringArgs(chaincodeName, Arrays.asList(args), null);
273169
}
274170

275-
// ------RAW CALLS------
276-
277171
/**
278172
* @param key
279173
* @return
280174
*/
281-
public ByteString getRawState(String key) {
282-
return handler.handleGetState(key, uuid);
283-
}
175+
ByteString getRawState(String key);
284176

285177
/**
286178
* @param key
287179
* @param value
288180
*/
289-
public void putRawState(String key, ByteString value) {
290-
handler.handlePutState(key, value, uuid);
291-
}
181+
void putRawState(String key, ByteString value);
292182

293183
/**
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
299187
*/
300-
// public GetStateByRangeResponse getStateByRangeRaw(String startKey, String endKey, int limit) {
301-
// return handler.handleGetStateByRange(startKey, endKey, limit, uuid);
302-
// }
188+
ChaincodeEvent getEvent();
303189

304-
}
190+
}

0 commit comments

Comments
 (0)