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

Fix web3j#152. Filters create requests based on type #154

Merged
merged 3 commits into from
Sep 6, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -1,10 +1,12 @@
package org.web3j.protocol.core.filters;

import java.io.IOException;
import java.math.BigInteger;
import java.util.List;
import java.util.concurrent.ExecutionException;

import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.Request;
import org.web3j.protocol.core.methods.response.EthFilter;
import org.web3j.protocol.core.methods.response.EthLog;

Expand Down Expand Up @@ -34,5 +36,10 @@ void process(List<EthLog.LogResult> logResults) {
}
}
}

@Override
protected Request<?, EthLog> createFilterRequest(BigInteger filterId) {
return this.web3j.ethGetFilterChanges(filterId);
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be nice if this could be a no-op, with a comment that getFilterChanges is not supported for BlockFilters.

}
}

5 changes: 4 additions & 1 deletion src/main/java/org/web3j/protocol/core/filters/Filter.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.concurrent.TimeUnit;

import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.Request;
import org.web3j.protocol.core.Response;
import org.web3j.protocol.core.methods.response.EthFilter;
import org.web3j.protocol.core.methods.response.EthLog;
Expand Down Expand Up @@ -39,7 +40,7 @@ public void run(ScheduledExecutorService scheduledExecutorService, long blockTim
}

filterId = ethFilter.getFilterId();
EthLog ethLogInit = web3j.ethGetFilterLogs(filterId).send();
EthLog ethLogInit = this.createFilterRequest(this.filterId).send();
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it makes sense for this initial request to take place asynchronously too once we have the filter id. Appreciate the current implementation is down to me, but it would be great if you could update this too. E.g.:

scheduledExecutorService.submit(() -> {
                EthLog ethLog = null;
                try {
                    ethLog = this.createFilterRequest(this.filterId).send();
                } catch (IOException e) {
                    throwException(e);
                }
                
                process(ethLog.getLogs());
            });

process(ethLogInit.getLogs());

schedule = scheduledExecutorService.scheduleAtFixedRate(() -> {
Expand Down Expand Up @@ -85,6 +86,8 @@ public void cancel() {
}
}

protected abstract Request<?, EthLog> createFilterRequest(BigInteger filterId);
Copy link
Contributor

Choose a reason for hiding this comment

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

Please can we rename this to getExistingFilterLogs or similar?


void throwException(Response.Error error) {
throw new FilterException("Invalid request: " + error.getMessage());
}
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/org/web3j/protocol/core/filters/LogFilter.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package org.web3j.protocol.core.filters;

import java.io.IOException;
import java.math.BigInteger;
import java.util.List;
import java.util.concurrent.ExecutionException;

import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.Request;
import org.web3j.protocol.core.methods.response.EthFilter;
import org.web3j.protocol.core.methods.response.EthLog;
import org.web3j.protocol.core.methods.response.Log;
Expand All @@ -23,6 +25,8 @@ public LogFilter(
this.ethFilter = ethFilter;
}



@Override
EthFilter sendRequest() throws IOException {
return web3j.ethNewFilter(ethFilter).send();
Expand All @@ -40,4 +44,9 @@ void process(List<EthLog.LogResult> logResults) {
}
}
}

@Override
protected Request<?, EthLog> createFilterRequest(BigInteger filterId) {
return this.web3j.ethGetFilterLogs(filterId);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package org.web3j.protocol.core.filters;

import java.io.IOException;
import java.math.BigInteger;
import java.util.List;
import java.util.concurrent.ExecutionException;

import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.Request;
import org.web3j.protocol.core.methods.response.EthFilter;
import org.web3j.protocol.core.methods.response.EthLog;

Expand Down Expand Up @@ -34,5 +36,10 @@ void process(List<EthLog.LogResult> logResults) {
}
}
}

@Override
protected Request<?, EthLog> createFilterRequest(BigInteger filterId) {
return this.web3j.ethGetFilterChanges(filterId);
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be nice if this could be a no-op, with a comment that getFilterChanges is not supported for PendingTransactionFilters.

}
}