Search in sources :

Example 16 with AionBlock

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

the class BlockPropagationHandler method processIncomingBlock.

public PropStatus processIncomingBlock(final int nodeId, final AionBlock block) {
    if (block == null)
        return PropStatus.DROPPED;
    ByteArrayWrapper hashWrapped = new ByteArrayWrapper(block.getHash());
    if (!this.blockHeaderValidator.validate(block.getHeader(), log))
        return PropStatus.DROPPED;
    // guarantees if multiple requests of same block appears, only one goes through
    synchronized (this.cacheMap) {
        if (this.cacheMap.containsKey(hashWrapped))
            return PropStatus.DROPPED;
        // regardless if block processing is successful, place into cache
        this.cacheMap.put(hashWrapped, true);
    }
    AionBlock bestBlock = this.blockchain.getBestBlock();
    // assumption is that we are on the correct chain
    if (bestBlock.getNumber() > block.getNumber())
        return PropStatus.DROPPED;
    // this implies we only propagate blocks from our own chain
    if (!bestBlock.isParentOf(block))
        return PropStatus.DROPPED;
    // send
    boolean sent = send(block, nodeId);
    // process
    ImportResult result = this.blockchain.tryToConnect(block);
    // process resulting state
    if (sent && result.isSuccessful())
        return PropStatus.PROP_CONNECTED;
    if (result.isSuccessful())
        return PropStatus.CONNECTED;
    if (sent)
        return PropStatus.PROPAGATED;
    // should never reach here, but just in case
    return PropStatus.DROPPED;
}
Also used : ImportResult(org.aion.mcf.core.ImportResult) ByteArrayWrapper(org.aion.base.util.ByteArrayWrapper) AionBlock(org.aion.zero.impl.types.AionBlock)

Example 17 with AionBlock

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

the class ApiWeb3Aion method submitBlock.

// AION Mining Pool
// TODO Test multiple threads submitting blocks
synchronized boolean submitBlock(Solution solution) {
    AionBlock block = (AionBlock) solution.getBlock();
    // set the nonce and solution
    block.getHeader().setNonce(solution.getNonce());
    block.getHeader().setSolution(solution.getSolution());
    block.getHeader().setTimestamp(solution.getTimeStamp());
    // This can be improved
    return (AionImpl.inst().addNewMinedBlock(block)).isSuccessful();
}
Also used : AionBlock(org.aion.zero.impl.types.AionBlock)

Example 18 with AionBlock

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

the class ApiWeb3Aion method eth_getTransactionByHash.

public Object eth_getTransactionByHash(String _txHash) {
    byte[] txHash = ByteUtil.hexStringToBytes(_txHash);
    if (_txHash == null || txHash == null)
        return null;
    AionTxInfo txInfo = this.ac.getAionHub().getBlockchain().getTransactionInfo(txHash);
    if (txInfo == null)
        return null;
    AionBlock b = this.ac.getBlockchain().getBlockByHash(txInfo.getBlockHash());
    if (b == null)
        return null;
    return Tx.InfoToJSON(txInfo, b);
}
Also used : AionTxInfo(org.aion.zero.impl.types.AionTxInfo) AionBlock(org.aion.zero.impl.types.AionBlock)

Example 19 with AionBlock

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

the class ApiWeb3Aion method eth_getBlockByNumber.

public Object eth_getBlockByNumber(String _bnOrId, boolean _fullTx) {
    Long bn = this.parseBnOrId(_bnOrId);
    if (bn == null || bn < 0)
        return null;
    AionBlock nb = this.ac.getBlockchain().getBlockByNumber(bn);
    if (nb == null) {
        LOG.debug("<get-block bn={} err=not-found>");
        return null;
    } else {
        BigInteger totalDiff = this.ac.getAionHub().getBlockStore().getTotalDifficultyForHash(nb.getHash());
        return Blk.AionBlockToJson(nb, totalDiff, _fullTx);
    }
}
Also used : BigInteger(java.math.BigInteger) AionBlock(org.aion.zero.impl.types.AionBlock)

Example 20 with AionBlock

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

the class ApiWeb3Aion method eth_getBlockByHash.

public Object eth_getBlockByHash(String _hashString, boolean _fullTx) {
    byte[] hash = ByteUtil.hexStringToBytes(_hashString);
    AionBlock block = this.ac.getBlockchain().getBlockByHash(hash);
    BigInteger totalDiff = this.ac.getAionHub().getBlockStore().getTotalDifficultyForHash(hash);
    if (block == null) {
        LOG.debug("<get-block bn={} err=not-found>");
        return null;
    } else {
        try {
            return Blk.AionBlockToJson(block, totalDiff, _fullTx);
        } catch (Exception ex) {
            if (LOG.isDebugEnabled())
                LOG.debug("<get-block bh={} err=exception>", _hashString);
            return null;
        }
    }
}
Also used : 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