Search in sources :

Example 1 with FltrLg

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));
}
Also used : ArgFltr(org.aion.api.server.types.ArgFltr) JSONObject(org.json.JSONObject) FltrLg(org.aion.api.server.types.FltrLg) JSONArray(org.json.JSONArray)

Example 2 with FltrLg

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));
}
Also used : ArgFltr(org.aion.api.server.types.ArgFltr) JSONObject(org.json.JSONObject) FltrLg(org.aion.api.server.types.FltrLg) JSONArray(org.json.JSONArray)

Example 3 with FltrLg

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;
}
Also used : FltrLg(org.aion.api.server.types.FltrLg) MiningBlock(org.aion.zero.impl.types.MiningBlock) Block(org.aion.zero.impl.types.Block) StakingBlock(org.aion.zero.impl.types.StakingBlock) Hex.toHexString(org.aion.util.conversions.Hex.toHexString)

Aggregations

FltrLg (org.aion.api.server.types.FltrLg)3 ArgFltr (org.aion.api.server.types.ArgFltr)2 JSONArray (org.json.JSONArray)2 JSONObject (org.json.JSONObject)2 Hex.toHexString (org.aion.util.conversions.Hex.toHexString)1 Block (org.aion.zero.impl.types.Block)1 MiningBlock (org.aion.zero.impl.types.MiningBlock)1 StakingBlock (org.aion.zero.impl.types.StakingBlock)1