Skip to content

Commit

Permalink
[Bugfix] NPE in JsonRpc2_0Rx (hyperledger-web3j#1183)
Browse files Browse the repository at this point in the history
* fix NPE in JsonRpc2_0Rx#replayBlocksFlowableSync;
refactor Collection class

* refactor JsonRpc2_0Rx#replayBlocksFlowableSync and Collection#join

* rename unit test

* run spotless on sources

* rename parameter after code review
  • Loading branch information
tau3 authored Apr 16, 2020
1 parent bdf146e commit 6deca4b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 56 deletions.
33 changes: 10 additions & 23 deletions core/src/main/java/org/web3j/protocol/rx/JsonRpc2_0Rx.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.web3j.protocol.core.DefaultBlockParameter;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.DefaultBlockParameterNumber;
import org.web3j.protocol.core.Request;
import org.web3j.protocol.core.filters.BlockFilter;
import org.web3j.protocol.core.filters.LogFilter;
import org.web3j.protocol.core.filters.PendingTransactionFilter;
Expand Down Expand Up @@ -138,35 +139,21 @@ private Flowable<EthBlock> replayBlocksFlowableSync(
private Flowable<EthBlock> replayBlocksFlowableSync(
DefaultBlockParameter startBlock,
DefaultBlockParameter endBlock,
boolean fullTransactionObjects,
boolean ascending) {

BigInteger startBlockNumber = null;
BigInteger endBlockNumber = null;
boolean containsFullTransactionObjects,
boolean isAscending) {
BigInteger startBlockNumber;
BigInteger endBlockNumber;
try {
startBlockNumber = getBlockNumber(startBlock);
endBlockNumber = getBlockNumber(endBlock);
} catch (IOException e) {
Flowable.error(e);
return Flowable.error(e);
}

if (ascending) {
return Flowables.range(startBlockNumber, endBlockNumber)
.flatMap(
i ->
web3j.ethGetBlockByNumber(
new DefaultBlockParameterNumber(i),
fullTransactionObjects)
.flowable());
} else {
return Flowables.range(startBlockNumber, endBlockNumber, false)
.flatMap(
i ->
web3j.ethGetBlockByNumber(
new DefaultBlockParameterNumber(i),
fullTransactionObjects)
.flowable());
}
return Flowables.range(startBlockNumber, endBlockNumber, isAscending)
.map(DefaultBlockParameterNumber::new)
.map(number -> web3j.ethGetBlockByNumber(number, containsFullTransactionObjects))
.flatMap(Request::flowable);
}

public Flowable<Transaction> replayTransactionsFlowable(
Expand Down
26 changes: 3 additions & 23 deletions core/src/main/java/org/web3j/utils/Collection.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/** Utility functions for working with Collections. */
public class Collection {
Expand All @@ -36,28 +38,6 @@ public static <T> T[] create(T... args) {
}

public static <T> String join(List<T> list, String separator, Function<T, String> function) {
String result = "";
for (int i = 0; i < list.size(); i++) {
result += function.apply(list.get(i)).trim();
if (i + 1 < list.size()) {
result += separator;
}
}
return result;
}

public static String join(List<String> list, String separator) {
String result = "";
for (int i = 0; i < list.size(); i++) {
result += list.get(i).trim();
if (i + 1 < list.size()) {
result += separator;
}
}
return result;
}

public interface Function<R, S> {
S apply(R r);
return list.stream().map(function).map(String::trim).collect(Collectors.joining(separator));
}
}
25 changes: 25 additions & 0 deletions core/src/test/java/org/web3j/protocol/rx/JsonRpc2_0RxTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package org.web3j.protocol.rx;

import java.io.IOException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -31,6 +32,7 @@
import org.web3j.protocol.ObjectMapperFactory;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.Web3jService;
import org.web3j.protocol.core.DefaultBlockParameter;
import org.web3j.protocol.core.DefaultBlockParameterNumber;
import org.web3j.protocol.core.Request;
import org.web3j.protocol.core.methods.response.EthBlock;
Expand All @@ -41,10 +43,13 @@
import org.web3j.utils.Numeric;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -386,4 +391,24 @@ private Transaction createTransaction(String transactionHash) {
transaction.setHash(transactionHash);
return transaction;
}

@Test
void testReplayBlocksFlowableWhenIOExceptionOnBlockResolving() throws IOException {
Web3j web3j = mock(Web3j.class, RETURNS_DEEP_STUBS);
when(web3j.ethGetBlockByNumber(any(), anyBoolean()).send())
.thenThrow(new IOException("fail"));

JsonRpc2_0Rx rpc = new JsonRpc2_0Rx(web3j, Executors.newSingleThreadScheduledExecutor());

Flowable<EthBlock> flowable =
rpc.replayBlocksFlowable(
mock(DefaultBlockParameter.class),
mock(DefaultBlockParameter.class),
false,
false);
EthBlock expected = mock(EthBlock.class);
EthBlock actual = flowable.onErrorReturnItem(expected).blockingFirst();

assertSame(expected, actual, "unexpected returned block");
}
}
10 changes: 0 additions & 10 deletions core/src/test/java/org/web3j/utils/CollectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.web3j.utils.Collection.EMPTY_STRING_ARRAY;
import static org.web3j.utils.Collection.Function;
import static org.web3j.utils.Collection.create;
import static org.web3j.utils.Collection.join;
import static org.web3j.utils.Collection.tail;
Expand All @@ -41,14 +40,6 @@ public void testCreate() {
assertArrayEquals(create("a", "b"), (new String[] {"a", "b"}));
}

@Test
public void testJoin() {
assertEquals(join(Arrays.asList("a ", "b ", " c "), ","), ("a,b,c"));
assertEquals(join(Arrays.asList("a", "b", "c", "d"), ","), ("a,b,c,d"));
assertEquals(join(Arrays.asList("a ", "b ", " c "), ", "), ("a, b, c"));
assertEquals(join(Arrays.asList("a", "b", "c", "d"), ", "), ("a, b, c, d"));
}

@Test
public void testJoinWithFunction() {
final List<FakeSpec> specs1 =
Expand All @@ -68,7 +59,6 @@ public void testJoinWithFunction() {
assertEquals(join(specs4, ", ", FakeSpec::getName), ("a, b, c"));
}

/** Fake object to test {@link Collection#join(List, String, Function)}. */
private final class FakeSpec {
private final String name;

Expand Down

0 comments on commit 6deca4b

Please sign in to comment.