Skip to content

Commit 703558c

Browse files
mbwhitejt-nti
authored andcommitted
[FABCJ-291] Startkey needs additional checks
For the open ended query, the start and empty keys are empty from the chaincode. However, in the shim, if the start key is an empty key, it is replaced with 0x01 which is nothing but a namespace for the non-composite key. Signed-off-by: Matthew B White <[email protected]>
1 parent fc4c780 commit 703558c

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

fabric-chaincode-shim/src/main/java/org/hyperledger/fabric/shim/impl/InvocationStubImpl.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@
6464

6565
class InvocationStubImpl implements ChaincodeStub {
6666

67-
private static final String UNSPECIFIED_KEY = new String(Character.toChars(0x000001));
67+
private static final String UNSPECIFIED_START_KEY = new String(Character.toChars(0x000001));
68+
private static final String UNSPECIFIED_END_KEY = "";
6869
private static final Logger LOGGER = Logger.getLogger(InvocationStubImpl.class.getName());
6970

7071
public static final String MAX_UNICODE_RUNE = "\udbff\udfff";
@@ -248,11 +249,11 @@ public QueryResultsIterator<KeyValue> getStateByRange(final String startKey, fin
248249
String start = startKey;
249250
String end = endKey;
250251

251-
if (startKey == null) {
252-
start = UNSPECIFIED_KEY;
252+
if (startKey == null || startKey.isEmpty()) {
253+
start = UNSPECIFIED_START_KEY;
253254
}
254255
if (endKey == null) {
255-
end = UNSPECIFIED_KEY;
256+
end = UNSPECIFIED_END_KEY;
256257
}
257258
CompositeKey.validateSimpleKeys(start, end);
258259

@@ -293,11 +294,11 @@ public QueryResultsIteratorWithMetadata<KeyValue> getStateByRangeWithPagination(
293294
String start = startKey;
294295
String end = endKey;
295296

296-
if (startKey == null) {
297-
start = UNSPECIFIED_KEY;
297+
if (startKey == null || startKey.isEmpty()) {
298+
start = UNSPECIFIED_START_KEY;
298299
}
299300
if (endKey == null) {
300-
end = UNSPECIFIED_KEY;
301+
end = UNSPECIFIED_END_KEY;
301302
}
302303

303304
CompositeKey.validateSimpleKeys(start, end);
@@ -350,7 +351,7 @@ public QueryResultsIterator<KeyValue> getStateByPartialCompositeKey(final Compos
350351
String cKeyAsString;
351352

352353
if (compositeKey == null) {
353-
cKeyAsString = new CompositeKey(UNSPECIFIED_KEY).toString();
354+
cKeyAsString = new CompositeKey(UNSPECIFIED_START_KEY).toString();
354355
} else {
355356
cKeyAsString = compositeKey.toString();
356357
}
@@ -365,7 +366,7 @@ public QueryResultsIteratorWithMetadata<KeyValue> getStateByPartialCompositeKeyW
365366
String cKeyAsString;
366367

367368
if (compositeKey == null) {
368-
cKeyAsString = new CompositeKey(UNSPECIFIED_KEY).toString();
369+
cKeyAsString = new CompositeKey(UNSPECIFIED_START_KEY).toString();
369370
} else {
370371
cKeyAsString = compositeKey.toString();
371372
}
@@ -520,11 +521,11 @@ public QueryResultsIterator<KeyValue> getPrivateDataByRange(final String collect
520521
String end = endKey;
521522

522523
validateCollection(collection);
523-
if (startKey == null) {
524-
start = UNSPECIFIED_KEY;
524+
if (startKey == null || startKey.isEmpty()) {
525+
start = UNSPECIFIED_START_KEY;
525526
}
526527
if (endKey == null) {
527-
end = UNSPECIFIED_KEY;
528+
end = UNSPECIFIED_END_KEY;
528529
}
529530
CompositeKey.validateSimpleKeys(start, end);
530531

@@ -553,7 +554,7 @@ public QueryResultsIterator<KeyValue> getPrivateDataByPartialCompositeKey(final
553554
final CompositeKey compositeKey) {
554555
String cKeyAsString;
555556
if (compositeKey == null) {
556-
cKeyAsString = new CompositeKey(UNSPECIFIED_KEY).toString();
557+
cKeyAsString = new CompositeKey(UNSPECIFIED_START_KEY).toString();
557558
} else {
558559
cKeyAsString = compositeKey.toString();
559560
}

fabric-chaincode-shim/src/test/java/org/hyperledger/fabric/shim/impl/InvocationStubImplTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class InvocationStubImplTest {
3030

3131
private final String channelId = "mychannel";
3232
private final String txId = "0xCAFEBABE";
33+
private final String simpleKeyStartNamespace = new String(Character.toChars(0x000001));
3334

3435
@Nested
3536
class GetStateByRangeTests {
@@ -80,9 +81,8 @@ public void nullvalues() throws InvalidProtocolBufferException {
8081

8182
final GetStateByRange range = GetStateByRange.parseFrom(msg.getPayload());
8283

83-
final String unspecifiedKey = new String(Character.toChars(0x000001));
84-
assertThat(range.getStartKey()).isEqualTo(unspecifiedKey);
85-
assertThat(range.getEndKey()).isEqualTo(unspecifiedKey);
84+
assertThat(range.getStartKey()).isEqualTo(simpleKeyStartNamespace);
85+
assertThat(range.getEndKey()).isEqualTo("");
8686
}
8787

8888
@Test
@@ -98,7 +98,7 @@ public void unbounded() throws InvalidProtocolBufferException {
9898

9999
final GetStateByRange range = GetStateByRange.parseFrom(msg.getPayload());
100100

101-
assertThat(range.getStartKey()).isEqualTo("");
101+
assertThat(range.getStartKey()).isEqualTo(simpleKeyStartNamespace);
102102
assertThat(range.getEndKey()).isEqualTo("");
103103
}
104104

0 commit comments

Comments
 (0)