Skip to content

Commit 703a9c8

Browse files
bullet-toothdmitry-timofeev
authored andcommitted
Light Client: Fix blockchain height method (#889)
1 parent 44a8e8c commit 703a9c8

3 files changed

Lines changed: 14 additions & 3 deletions

File tree

exonum-light-client/src/main/java/com/exonum/client/ExonumHttpClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public Optional<TransactionResponse> getTransaction(HashCode id) {
125125
public long getBlockchainHeight() {
126126
BlocksResponse response = doGetBlocks(0, INCLUDE_EMPTY, null, NO_COMMIT_TIME);
127127

128-
return response.getBlocksRangeEnd();
128+
return response.getBlocksRangeEnd() - 1; // Because '$.range.end' is exclusive
129129
}
130130

131131
@Override

exonum-light-client/src/main/java/com/exonum/client/response/BlocksResponse.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,17 @@ public class BlocksResponse {
3333
long blocksRangeStart;
3434

3535
/**
36-
* The largest height of the returned blocks that match the search criteria.
36+
* The largest height of the returned blocks that match the search criteria, <em>plus one</em>.
37+
*
38+
* <p><b>WARNING: do not rely on the value, because it's unpredictable in some queries.</b>
39+
* It'll be fixed in future releases. Read below if would still like to use it.
40+
*
41+
* <p>The value is always equal to {@code blocks[0].height + 1} for responses with blocks.
42+
* For responses without blocks the value is set mostly randomly depending on request parameters
43+
* and blockchain state:
44+
* The value is equal to {@code heightMax + 1} if heightMax request parameter is passed.
45+
* If the heightMax request parameter is greater then last committed block height at the moment
46+
* then blocksRangeEnd value will be equal to {@code last_committed_block.height + 1}.
3747
* If some blocks in the range do not match the search criteria then:
3848
* {@code blocksRangeEnd - blocksRangeStart != blocks.size}.
3949
*/

exonum-light-client/src/test/java/com/exonum/client/ExonumHttpClientBlocksIntegrationTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,10 +308,11 @@ void getLastNotEmptyBlockNoBlock() throws InterruptedException {
308308
void getBlockchainHeight() throws InterruptedException {
309309
// Mock response
310310
long height = 1600;
311+
long heightExclusive = height + 1;
311312
String json = "{\n"
312313
+ " 'range': {\n"
313314
+ " 'start': 0,\n"
314-
+ " 'end': " + height + "\n"
315+
+ " 'end': " + heightExclusive + "\n"
315316
+ " },\n"
316317
+ " 'blocks': [],\n"
317318
+ " 'times': null\n"

0 commit comments

Comments
 (0)