Skip to content

Commit

Permalink
Add blockHash filter to EthFilter due to EIP-234 (#1292)
Browse files Browse the repository at this point in the history
* fix test bug that actual and expected value were switched

* add blockHash filter to EthFilter
  • Loading branch information
JhChoy authored Nov 13, 2020
1 parent 7ed7ae0 commit 266abcb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
package org.web3j.protocol.core.methods.request;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.web3j.protocol.core.DefaultBlockParameter;
Expand All @@ -24,6 +24,7 @@
public class EthFilter extends Filter<EthFilter> {
private DefaultBlockParameter fromBlock; // optional, params - defaults to latest for both
private DefaultBlockParameter toBlock;
private String blockHash; // optional, cannot be used together with fromBlock/toBlock
private List<String> address; // spec. implies this can be single address as string or list

public EthFilter() {
Expand All @@ -40,7 +41,17 @@ public EthFilter(

public EthFilter(
DefaultBlockParameter fromBlock, DefaultBlockParameter toBlock, String address) {
this(fromBlock, toBlock, Arrays.asList(address));
this(fromBlock, toBlock, Collections.singletonList(address));
}

public EthFilter(String blockHash) {
super();
this.blockHash = blockHash;
}

public EthFilter(String blockHash, String address) {
this(null, null, Collections.singletonList(address));
this.blockHash = blockHash;
}

public DefaultBlockParameter getFromBlock() {
Expand All @@ -51,6 +62,10 @@ public DefaultBlockParameter getToBlock() {
return toBlock;
}

public String getBlockHash() {
return blockHash;
}

public List<String> getAddress() {
return address;
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/test/java/org/web3j/protocol/RequestTester.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected void verifyResult(String expected) throws Exception {

Buffer buffer = new Buffer();
requestBody.writeTo(buffer);
assertEquals(replaceRequestId(buffer.readUtf8()), (replaceRequestId(expected)));
assertEquals((replaceRequestId(expected)), replaceRequestId(buffer.readUtf8()));
}

private String replaceRequestId(String json) {
Expand Down
15 changes: 15 additions & 0 deletions core/src/test/java/org/web3j/protocol/core/RequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,21 @@ public void testEthGetLogsWithNumericBlockRange() throws Exception {
+ "\"toBlock\":\"latest\",\"address\":[\"\"]}],\"id\":1}");
}

@Test
public void testEthGetLogsWithBlockHash() throws Exception {
web3j.ethGetLogs(
new EthFilter(
"0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331",
""))
.send();

verifyResult(
"{\"jsonrpc\":\"2.0\",\"method\":\"eth_getLogs\","
+ "\"params\":[{\"topics\":[],"
+ "\"blockHash\":\"0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331\","
+ "\"address\":[\"\"]}],\"id\":<generatedValue>}");
}

@Test
public void testEthGetWork() throws Exception {
web3j.ethGetWork().send();
Expand Down

0 comments on commit 266abcb

Please sign in to comment.