Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add blockHash filter to EthFilter due to EIP-234 #1292

Merged
merged 2 commits into from
Nov 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this super(); call is redundant.

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