use of org.aion.api.server.types.FltrLg in project aion by aionnetwork.
the class ApiWeb3Aion method eth_newFilter.
public RpcMsg eth_newFilter(Object _params) {
if (!isFilterEnabled) {
return new RpcMsg(null, RpcError.NOT_ALLOWED, "Filters over rpc disabled.");
}
JSONObject _filterObj;
if (_params instanceof JSONArray) {
_filterObj = ((JSONArray) _params).getJSONObject(0);
} else if (_params instanceof JSONObject) {
_filterObj = ((JSONObject) _params).getJSONObject("filter");
} else {
return new RpcMsg(null, RpcError.INVALID_PARAMS, "Invalid parameters");
}
ArgFltr rf = ArgFltr.fromJSON(_filterObj);
if (rf == null) {
return new RpcMsg(null, RpcError.INVALID_PARAMS, "Invalid filter object provided.");
}
FltrLg filter = createFilter(rf);
if (filter.getFilterError() != null) {
return new RpcMsg(null, RpcError.INVALID_PARAMS, "Invalid filter object provided. " + filter.getFilterError());
}
// "install" the filter after populating historical data;
// rationale: until the user gets the id back, the user should not expect the filter to be
// "installed" anyway.
long id = fltrIndex.getAndIncrement();
installedFilters.put(id, filter);
return new RpcMsg(StringUtils.toJsonHex(id));
}
use of org.aion.api.server.types.FltrLg in project aion by aionnetwork.
the class ApiWeb3Aion method eth_getLogs.
public RpcMsg eth_getLogs(Object _params) {
JSONObject _filterObj;
if (_params instanceof JSONArray) {
_filterObj = ((JSONArray) _params).getJSONObject(0);
} else if (_params instanceof JSONObject) {
_filterObj = ((JSONObject) _params).getJSONObject("filter");
} else {
return new RpcMsg(null, RpcError.INVALID_PARAMS, "Invalid parameters");
}
ArgFltr rf = ArgFltr.fromJSON(_filterObj);
if (rf == null) {
return new RpcMsg(null, RpcError.INVALID_PARAMS, "Invalid filter object provided.");
}
FltrLg filter = createFilter(rf);
if (filter.getFilterError() != null) {
return new RpcMsg(null, RpcError.INVALID_PARAMS, "Invalid filter object provided. " + filter.getFilterError());
}
return new RpcMsg(buildFilterResponse(filter));
}
use of org.aion.api.server.types.FltrLg in project aion by aionnetwork.
the class ApiWeb3Aion method createFilter.
/* -------------------------------------------------------------------------
* filters
*/
/* Web3 Filters Support
*
* NOTE: newFilter behaviour is ill-defined in the JSON-rpc spec for the following scenarios:
* (an explanation of how we resolved these ambiguities follows immediately after)
*
* newFilter is used to subscribe for filter on transaction logs for transactions with provided address and topics
*
* role of fromBlock, toBlock fields within context of newFilter, newBlockFilter, newPendingTransactionFilter
* (they seem only more pertinent for getLogs)
* how we resolve it: populate historical data (best-effort) in the filter response before "installing the filter"
* onus on the user to flush the filter of the historical data, before depending on it for up-to-date values.
* apart from loading historical data, fromBlock & toBlock are ignored when loading events on filter queue
*/
private FltrLg createFilter(ArgFltr rf) {
FltrLg filter = new FltrLg();
filter.setTopics(rf.topics);
filter.setContractAddress(rf.address);
Long bnFrom = parseBnOrId(rf.fromBlock);
Long bnTo = parseBnOrId(rf.toBlock);
// The rpcServer can return the detailed error message to the client.
if (bnFrom == null || bnTo == null) {
LOG.debug("jsonrpc - eth_newFilter(): from, to block parse failed");
filter.setFilterError("jsonrpc - eth_newFilter(): from, to block parse failed");
return filter;
}
if (bnTo != BEST_PENDING_BLOCK && bnFrom > bnTo) {
LOG.debug("jsonrpc - eth_newFilter(): from block is after to block");
filter.setFilterError("jsonrpc - eth_newFilter(): from block is after to block");
return filter;
}
if (bnTo >= (bnFrom + BLOCKS_QUERY_MAX)) {
String errLog = "jsonrpc - eth_newFilter(): can't query more than " + BLOCKS_QUERY_MAX + " blocks";
LOG.debug(errLog);
filter.setFilterError(errLog);
return filter;
}
Block fromBlock = this.getBlockByBN(bnFrom);
Block toBlock = this.getBlockByBN(bnTo);
if (fromBlock != null) {
// need to add historical data
// this is our own policy: what to do in this case is not defined in the spec
//
// policy: add data from earliest to latest, until we can't fill the queue anymore
//
// caveat: filling up the events-queue with historical data will cause the following
// issue:
// the user will miss all events generated between the first poll and filter
// installation.
toBlock = toBlock == null ? getBestBlock() : toBlock;
for (long i = fromBlock.getNumber(); i <= toBlock.getNumber(); i++) {
if (filter.isFull()) {
break;
}
filter.onBlock(this.ac.getBlockchain().getBlockByNumber(i), this.ac.getAionHub().getBlockchain());
}
}
return filter;
}
Aggregations