Skip to content

Commit e3644f4

Browse files
committed
[FAB-6889] add channel ID to chaincode message
Chaincodes need the channel ID in the transaction context. This CR follows the work in https://gerrit.hyperledger.org/r/#/c/14919. Change-Id: I1914138d30db27d30ac5583cef38d13d60187832 Signed-off-by: Srinivasan Muralidharan <srinivasan.muralidharan99@gmail.com>
1 parent 3cbdc87 commit e3644f4

File tree

6 files changed

+162
-130
lines changed

6 files changed

+162
-130
lines changed

shim/src/main/java/org/hyperledger/fabric/shim/ChaincodeStub.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ public interface ChaincodeStub {
7171
*/
7272
String getTxId();
7373

74+
/**
75+
* Returns the channel id
76+
*
77+
* @return the channel id
78+
*/
79+
String getChannelId();
80+
7481
/**
7582
* Invoke another chaincode using the same transaction context.
7683
*

shim/src/main/java/org/hyperledger/fabric/shim/impl/ChaincodeStubImpl.java

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
class ChaincodeStubImpl implements ChaincodeStub {
4545

4646
private static final String UNSPECIFIED_KEY = new String(Character.toChars(0x000001));
47+
private final String channelId;
4748
private final String txId;
4849
private final Handler handler;
4950
private final List<ByteString> args;
@@ -54,7 +55,8 @@ class ChaincodeStubImpl implements ChaincodeStub {
5455
private final byte[] binding;
5556
private ChaincodeEvent event;
5657

57-
ChaincodeStubImpl(String txId, Handler handler, List<ByteString> args, SignedProposal signedProposal) {
58+
ChaincodeStubImpl(String channelId, String txId, Handler handler, List<ByteString> args, SignedProposal signedProposal) {
59+
this.channelId = channelId;
5860
this.txId = txId;
5961
this.handler = handler;
6062
this.args = Collections.unmodifiableList(args);
@@ -146,26 +148,31 @@ public ChaincodeEvent getEvent() {
146148
return event;
147149
}
148150

151+
@Override
152+
public String getChannelId() {
153+
return channelId;
154+
}
155+
149156
@Override
150157
public String getTxId() {
151158
return txId;
152159
}
153160

154161
@Override
155162
public byte[] getState(String key) {
156-
return handler.getState(txId, key).toByteArray();
163+
return handler.getState(channelId, txId, key).toByteArray();
157164
}
158165

159166
@Override
160167
public void putState(String key, byte[] value) {
161168
if(key == null) throw new NullPointerException("key cannot be null");
162169
if(key.length() == 0) throw new IllegalArgumentException("key cannot not be an empty string");
163-
handler.putState(txId, key, ByteString.copyFrom(value));
170+
handler.putState(channelId, txId, key, ByteString.copyFrom(value));
164171
}
165172

166173
@Override
167174
public void delState(String key) {
168-
handler.deleteState(txId, key);
175+
handler.deleteState(channelId, txId, key);
169176
}
170177

171178
@Override
@@ -174,8 +181,8 @@ public QueryResultsIterator<KeyValue> getStateByRange(String startKey, String en
174181
if (endKey == null || endKey.isEmpty()) endKey = UNSPECIFIED_KEY;
175182
CompositeKey.validateSimpleKeys(startKey, endKey);
176183

177-
return new QueryResultsIteratorImpl<KeyValue>(this.handler, getTxId(),
178-
handler.getStateByRange(getTxId(), startKey, endKey),
184+
return new QueryResultsIteratorImpl<KeyValue>(this.handler, getChannelId(), getTxId(),
185+
handler.getStateByRange(getChannelId(), getTxId(), startKey, endKey),
179186
queryResultBytesToKv.andThen(KeyValueImpl::new)
180187
);
181188
}
@@ -210,16 +217,16 @@ public CompositeKey splitCompositeKey(String compositeKey) {
210217

211218
@Override
212219
public QueryResultsIterator<KeyValue> getQueryResult(String query) {
213-
return new QueryResultsIteratorImpl<KeyValue>(this.handler, getTxId(),
214-
handler.getQueryResult(getTxId(), query),
220+
return new QueryResultsIteratorImpl<KeyValue>(this.handler, getChannelId(), getTxId(),
221+
handler.getQueryResult(getChannelId(), getTxId(), query),
215222
queryResultBytesToKv.andThen(KeyValueImpl::new)
216223
);
217224
}
218225

219226
@Override
220227
public QueryResultsIterator<KeyModification> getHistoryForKey(String key) {
221-
return new QueryResultsIteratorImpl<KeyModification>(this.handler, getTxId(),
222-
handler.getHistoryForKey(getTxId(), key),
228+
return new QueryResultsIteratorImpl<KeyModification>(this.handler, getChannelId(), getTxId(),
229+
handler.getHistoryForKey(getChannelId(), getTxId(), key),
223230
queryResultBytesToKeyModification.andThen(KeyModificationImpl::new)
224231
);
225232
}
@@ -243,7 +250,7 @@ public Response invokeChaincode(final String chaincodeName, final List<byte[]> a
243250
} else {
244251
compositeName = chaincodeName;
245252
}
246-
return handler.invokeChaincode(this.txId, compositeName, args);
253+
return handler.invokeChaincode(this.channelId, this.txId, compositeName, args);
247254
}
248255

249256
@Override

0 commit comments

Comments
 (0)