Search in sources :

Example 21 with AionBlock

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

the class ApiWeb3Aion method getRepoByJsonBlockId.

// --------------------------------------------------------------------
// Helper Functions
// --------------------------------------------------------------------
private IRepository getRepoByJsonBlockId(String _bnOrId) {
    Long bn = parseBnOrId(_bnOrId);
    // if you passed in an invalid bnOrId, pending or it's an error
    if (bn == null || bn < 0)
        return null;
    AionBlock b = this.ac.getBlockchain().getBlockByNumber(bn);
    if (b == null)
        return null;
    return ac.getRepository().getSnapshotTo(b.getStateRoot());
}
Also used : AionBlock(org.aion.zero.impl.types.AionBlock)

Example 22 with AionBlock

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

the class ApiWeb3Aion method stratum_getmininginfo.

public Object stratum_getmininginfo() {
    AionBlock bestBlock = getBestBlock();
    JSONObject obj = new JSONObject();
    obj.put("blocks", bestBlock.getNumber());
    obj.put("currentblocksize", 0);
    obj.put("currentblocktx", bestBlock.getTransactionsList().size());
    obj.put("difficulty", 3368767.14053294);
    obj.put("errors", "");
    obj.put("genproclimit", -1);
    obj.put("hashespersec", 0);
    obj.put("pooledtx", 0);
    obj.put("testnet", false);
    return obj;
}
Also used : JSONObject(org.json.JSONObject) AionBlock(org.aion.zero.impl.types.AionBlock)

Example 23 with AionBlock

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

the class ApiWeb3Aion method stratum_getHeaderByBlockNumber.

public Object stratum_getHeaderByBlockNumber(Object _blockNum) {
    JSONObject obj = new JSONObject();
    if (_blockNum != null && !_blockNum.equals(null)) {
        String bnStr = _blockNum + "";
        try {
            int bnInt = Integer.decode(bnStr);
            AionBlock block = getBlockRaw(bnInt);
            if (block != null) {
                A0BlockHeader header = block.getHeader();
                // 0 = success
                obj.put("code", 0);
                obj.put("nonce", toHexString(header.getNonce()));
                obj.put("solution", toHexString(header.getSolution()));
                obj.put("headerHash", toHexString(HashUtil.h256(header.getHeaderBytes(false))));
                obj.putOpt("blockHeader", header.toJSON());
            } else {
                obj.put("message", "Fail - Unable to find block" + bnStr);
                obj.put("code", -2);
            }
        } catch (Exception e) {
            obj.put("message", bnStr + " must be an integer value");
            obj.put("code", -3);
        }
    } else {
        obj.put("message", "Missing block number");
        obj.put("code", -1);
    }
    return obj;
}
Also used : A0BlockHeader(org.aion.zero.types.A0BlockHeader) JSONObject(org.json.JSONObject) ByteUtil.toHexString(org.aion.base.util.ByteUtil.toHexString) AionBlock(org.aion.zero.impl.types.AionBlock)

Example 24 with AionBlock

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

the class ApiWeb3Aion method getTransactionsByBlockId.

private List<AionTransaction> getTransactionsByBlockId(String id) {
    Long bn = parseBnOrId(id);
    if (bn == null || bn < 0)
        return null;
    AionBlock b = this.ac.getBlockchain().getBlockByNumber(bn);
    if (b == null)
        return null;
    return b.getTransactionsList();
}
Also used : AionBlock(org.aion.zero.impl.types.AionBlock)

Example 25 with AionBlock

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

the class ApiWeb3Aion method stratum_getblocktemplate.

public Object stratum_getblocktemplate() {
    // TODO: Change this to a synchronized map implementation mapping
    // block hashes to the block. Allow multiple block templates at same height.
    templateMapLock.writeLock().lock();
    AionBlock bestBlock = getBlockTemplate();
    // have switch branches, abandon current work to start on new branch
    if (!templateMap.keySet().isEmpty()) {
        if (templateMap.get(templateMap.keySet().iterator().next()).getNumber() < bestBlock.getNumber()) {
            // Found a higher block, clear any remaining cached entries and start on new height
            templateMap.clear();
        }
    }
    templateMap.put(toHexString(bestBlock.getHeader().getStaticHash()), bestBlock);
    JSONObject coinbaseaux = new JSONObject();
    coinbaseaux.put("flags", "062f503253482f");
    JSONObject obj = new JSONObject();
    obj.put("previousblockhash", toHexString(bestBlock.getParentHash()));
    obj.put("height", bestBlock.getNumber());
    obj.put("target", toHexString(BigInteger.valueOf(2).pow(256).divide(new BigInteger(bestBlock.getHeader().getDifficulty())).toByteArray()));
    obj.put("transactions", new JSONArray());
    obj.putOpt("blockHeader", bestBlock.getHeader().toJSON());
    obj.put("coinbaseaux", coinbaseaux);
    obj.put("headerHash", toHexString(bestBlock.getHeader().getStaticHash()));
    templateMapLock.writeLock().unlock();
    return obj;
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) BigInteger(java.math.BigInteger) 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