Search in sources :

Example 26 with AionBlock

use of org.aion.zero.impl.types.AionBlock in project aion by aionnetwork.

the class ApiWeb3Aion method eth_newFilter.

/* -------------------------------------------------------------------------
     * 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
     */
public String eth_newFilter(JSONObject _filterObj) {
    ArgFltr rf = ArgFltr.fromJSON(_filterObj);
    FltrLg filter = new FltrLg();
    filter.setTopics(rf.topics);
    filter.setContractAddress(rf.address);
    Long bnFrom = parseBnOrId(rf.fromBlock);
    Long bnTo = parseBnOrId(rf.toBlock);
    if (bnFrom == null || bnTo == null || bnFrom == -1 || bnTo == -1) {
        LOG.debug("jsonrpc - eth_newFilter(): from, to block parse failed");
        return null;
    }
    final AionBlock fromBlock = this.ac.getBlockchain().getBlockByNumber(bnFrom);
    AionBlock toBlock = this.ac.getBlockchain().getBlockByNumber(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());
        }
    }
    // "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 TypeConverter.toJsonHex(id);
}
Also used : AionBlock(org.aion.zero.impl.types.AionBlock)

Example 27 with AionBlock

use of org.aion.zero.impl.types.AionBlock in project aion by aionnetwork.

the class ApiWeb3Aion method eth_getBlockTransactionCountByHash.

public Object eth_getBlockTransactionCountByHash(String hashString) {
    byte[] hash = ByteUtil.hexStringToBytes(hashString);
    AionBlock b = this.ac.getBlockchain().getBlockByHash(hash);
    if (b == null)
        return null;
    long n = b.getTransactionsList().size();
    return TypeConverter.toJsonHex(n);
}
Also used : AionBlock(org.aion.zero.impl.types.AionBlock)

Example 28 with AionBlock

use of org.aion.zero.impl.types.AionBlock in project aion by aionnetwork.

the class ApiWeb3Aion method stratum_submitblock.

public Object stratum_submitblock(Object nce, Object soln, Object hdrHash, Object ts) {
    JSONObject obj = new JSONObject();
    if (nce != null && soln != null && hdrHash != null && ts != null && !nce.equals(null) && !soln.equals(null) && !hdrHash.equals(null) && !ts.equals(null)) {
        templateMapLock.writeLock().lock();
        AionBlock bestBlock = templateMap.get(hdrHash + "");
        if (bestBlock != null) {
            IEvent ev = new EventConsensus(EventConsensus.CALLBACK.ON_SOLUTION);
            ev.setFuncArgs(Collections.singletonList(new Solution(bestBlock, hexStringToBytes(nce + ""), hexStringToBytes(soln + ""), Long.parseLong(ts + "", 16))));
            evtMgr.newEvent(ev);
            LOG.info("block submitted via api <num={}, hash={}, diff={}, tx={}>", bestBlock.getNumber(), // LogUtil.toHexF8(newBlock.getHash()),
            bestBlock.getShortHash(), bestBlock.getHeader().getDifficultyBI().toString(), bestBlock.getTransactionsList().size());
            templateMap.clear();
        }
        templateMapLock.writeLock().unlock();
        // TODO: Simplified response for now, need to provide better feedback to caller in next update
        obj.put("result", true);
    } else {
        obj.put("message", "success");
        obj.put("code", -1);
    }
    return obj;
}
Also used : JSONObject(org.json.JSONObject) IEvent(org.aion.evtmgr.IEvent) EventConsensus(org.aion.evtmgr.impl.evt.EventConsensus) Solution(org.aion.equihash.Solution) AionBlock(org.aion.zero.impl.types.AionBlock)

Example 29 with AionBlock

use of org.aion.zero.impl.types.AionBlock in project aion by aionnetwork.

the class ApiWeb3Aion method eth_getTransactionByBlockNumberAndIndex.

public Object eth_getTransactionByBlockNumberAndIndex(String _bnOrId, String _index) {
    Long bn = parseBnOrId(_bnOrId);
    if (bn == null || bn < 0)
        return null;
    AionBlock b = this.ac.getBlockchain().getBlockByNumber(bn);
    if (b == null)
        return null;
    List<AionTransaction> txs = b.getTransactionsList();
    int idx = Integer.decode(_index);
    if (idx >= txs.size())
        return null;
    return Tx.AionTransactionToJSON(txs.get(idx), b, idx);
}
Also used : AionTransaction(org.aion.zero.types.AionTransaction) AionBlock(org.aion.zero.impl.types.AionBlock)

Example 30 with AionBlock

use of org.aion.zero.impl.types.AionBlock in project aion by aionnetwork.

the class ApiWeb3Aion method debug_getBlocksByNumber.

/* -------------------------------------------------------------------------
     * debug
     */
public Object debug_getBlocksByNumber(String _bnOrId, boolean _fullTransactions) {
    Long bn = parseBnOrId(_bnOrId);
    if (bn == null || bn < 0)
        return null;
    List<Map.Entry<AionBlock, Map.Entry<BigInteger, Boolean>>> blocks = ((AionBlockStore) this.ac.getAionHub().getBlockchain().getBlockStore()).getBlocksByNumber(bn);
    if (blocks == null) {
        LOG.debug("<get-block bn={} err=not-found>");
        return null;
    }
    JSONArray response = new JSONArray();
    for (Map.Entry<AionBlock, Map.Entry<BigInteger, Boolean>> block : blocks) {
        JSONObject b = (JSONObject) Blk.AionBlockToJson(block.getKey(), block.getValue().getKey(), _fullTransactions);
        b.put("mainchain", block.getValue().getValue());
        response.put(b);
    }
    return response;
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) BigInteger(java.math.BigInteger) AionBlockStore(org.aion.zero.impl.db.AionBlockStore) LRUMap(org.apache.commons.collections4.map.LRUMap) HashMap(java.util.HashMap) Map(java.util.Map) AionBlock(org.aion.zero.impl.types.AionBlock)

Aggregations

AionBlock (org.aion.zero.impl.types.AionBlock)49 Test (org.junit.Test)16 ImportResult (org.aion.mcf.core.ImportResult)15 AionTransaction (org.aion.zero.types.AionTransaction)11 BigInteger (java.math.BigInteger)9 Address (org.aion.base.type.Address)6 ArrayList (java.util.ArrayList)5 JSONObject (org.json.JSONObject)5 IByteArrayKeyValueDatabase (org.aion.base.db.IByteArrayKeyValueDatabase)3 ByteArrayWrapper (org.aion.base.util.ByteArrayWrapper)3 TrieImpl (org.aion.mcf.trie.TrieImpl)3 AionTxInfo (org.aion.zero.impl.types.AionTxInfo)3 A0BlockHeader (org.aion.zero.types.A0BlockHeader)3 Map (java.util.Map)2 ByteUtil.toHexString (org.aion.base.util.ByteUtil.toHexString)2 ECKey (org.aion.crypto.ECKey)2 IEvent (org.aion.evtmgr.IEvent)2 LRUMap (org.apache.commons.collections4.map.LRUMap)2 JSONArray (org.json.JSONArray)2 Ignore (org.junit.Ignore)2