Search in sources :

Example 86 with Block

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

the class AionBlockStore method getThreeGenerationBlocksByHashWithInfo.

/**
 * Retrieves three generation blocks with unity protocol info.
 * <p>
 * Always returns a 3-element array. If the blocks cannot be retrieved the array will contain null values.
 * Block[0] is the parent block and has the given hash. Block[1] is the grandparent block.
 * Block[2] is the great grandparent block.
 *
 * @param hash the hash of the parent block
 * @return the retrieved three generation blocks with unity protocol info
 */
public final Block[] getThreeGenerationBlocksByHashWithInfo(byte[] hash) {
    Block[] blockFamily = new Block[] { null, null, null };
    if (hash == null) {
        return blockFamily;
    }
    lock.lock();
    try {
        Block block = getBlockWithInfo(hash);
        if (block != null) {
            blockFamily[0] = block;
            Block parentBlock = getBlockWithInfo(block.getParentHash());
            if (parentBlock != null) {
                blockFamily[1] = parentBlock;
                Block grandParentBlock = getBlockWithInfo(parentBlock.getParentHash());
                if (grandParentBlock != null) {
                    blockFamily[2] = grandParentBlock;
                }
            }
        }
        return blockFamily;
    } finally {
        lock.unlock();
    }
}
Also used : Block(org.aion.zero.impl.types.Block)

Example 87 with Block

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

the class AionBlockStore method dumpPastBlocks.

public String dumpPastBlocks(long numberOfBlocks, String reportsFolder) throws IOException {
    lock.lock();
    try {
        long firstBlock = getMaxNumber();
        if (firstBlock < 0) {
            return null;
        }
        long lastBlock = firstBlock - numberOfBlocks;
        File file = new File(reportsFolder, System.currentTimeMillis() + "-blocks-report.out");
        BufferedWriter writer = new BufferedWriter(new FileWriter(file));
        while (firstBlock > lastBlock && firstBlock >= 0) {
            List<BlockInfo> levelBlocks = getBlockInfoForLevel(firstBlock);
            writer.append("Blocks at level " + firstBlock + ":");
            writer.newLine();
            for (BlockInfo bi : levelBlocks) {
                writer.append("\nBlock hash from index database: " + Hex.toHexString(bi.getHash()) + "\nTotal Difficulty: " + bi.getTotalDifficulty() + "\nBlock on main chain: " + String.valueOf(bi.isMainChain()).toUpperCase());
                writer.newLine();
                Block blk = blocks.get(bi.getHash());
                if (blk != null) {
                    writer.append("\nFull block data:\n");
                    writer.append(blk.toString());
                    writer.newLine();
                } else {
                    writer.append("Retrieved block data is null.");
                }
            }
            writer.newLine();
            firstBlock--;
        }
        writer.close();
        return file.getName();
    } finally {
        lock.unlock();
    }
}
Also used : FileWriter(java.io.FileWriter) Block(org.aion.zero.impl.types.Block) File(java.io.File) BufferedWriter(java.io.BufferedWriter)

Example 88 with Block

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

the class AionBlockStore method getBlocksByNumber.

public List<Block> getBlocksByNumber(long number) {
    if (number < 0) {
        return null;
    }
    lock.lock();
    try {
        List<Block> result = new ArrayList<>();
        if (number >= index.size()) {
            return result;
        }
        List<BlockInfo> blockInfos = index.get(number);
        for (BlockInfo blockInfo : blockInfos) {
            byte[] hash = blockInfo.getHash();
            Block block = getBlockByHashWithInfo(hash);
            if (block == null) {
                throw new NullPointerException("Can find the block in the database!");
            }
            result.add(block);
        }
        return result;
    } finally {
        lock.unlock();
    }
}
Also used : ArrayList(java.util.ArrayList) Block(org.aion.zero.impl.types.Block)

Example 89 with Block

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

the class AionBlockStore method logBranchingDetails.

private void logBranchingDetails() {
    if (branchingLevel > 0 && LOG_CONS.isDebugEnabled()) {
        LOG_CONS.debug("Branching details start: level[{}]", branchingLevel);
        LOG_CONS.debug("===== Block details before branch =====");
        while (!preBranchingBlk.isEmpty()) {
            Block blk = preBranchingBlk.pop();
            LOG_CONS.debug("blk: {}", blk.toString());
        }
        LOG_CONS.debug("===== Block details after branch =====");
        while (!branchingBlk.isEmpty()) {
            Block blk = branchingBlk.pop();
            LOG_CONS.debug("blk: {}", blk.toString());
        }
        LOG_CONS.debug("Branching details end");
    }
    // reset branching block details
    branchingLevel = 0;
    branchingBlk.clear();
    preBranchingBlk.clear();
}
Also used : Block(org.aion.zero.impl.types.Block)

Example 90 with Block

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

the class AionBlockStore method getBestBlock.

/**
 *  Get current highest block data, usually use this method when the kernel need to know the
 *  block information itself.
 * @return highest block data stored at the block database
 * @see #getBestBlockWithInfo()
 */
public Block getBestBlock() {
    lock.lock();
    try {
        long maxLevel = index.size() - 1L;
        if (maxLevel < 0) {
            return null;
        }
        Block bestBlock = getChainBlockByNumber(maxLevel);
        if (bestBlock != null) {
            return bestBlock;
        }
        int depth = 0;
        while (bestBlock == null && depth < 128) {
            --maxLevel;
            bestBlock = getChainBlockByNumber(maxLevel);
            ++depth;
        }
        if (bestBlock == null) {
            LOG.error("Encountered a kernel database corruption: cannot find blockInfos at level {} in index data store. " + "Or the branch is too deep, it should not happens. " + "Please shutdown the kernel and rollback the database by executing:\t./aion.sh -n <network> -r {}", maxLevel, maxLevel - 1);
            throw new IllegalStateException("Index DB corruption or branch too deep.");
        }
        return bestBlock;
    } finally {
        lock.unlock();
    }
}
Also used : Block(org.aion.zero.impl.types.Block)

Aggregations

Block (org.aion.zero.impl.types.Block)283 MiningBlock (org.aion.zero.impl.types.MiningBlock)155 Test (org.junit.Test)148 AionTransaction (org.aion.base.AionTransaction)106 ImportResult (org.aion.zero.impl.core.ImportResult)86 ArrayList (java.util.ArrayList)63 AionAddress (org.aion.types.AionAddress)61 StakingBlock (org.aion.zero.impl.types.StakingBlock)58 AionBlockSummary (org.aion.zero.impl.types.AionBlockSummary)57 BigInteger (java.math.BigInteger)55 AionRepositoryImpl (org.aion.zero.impl.db.AionRepositoryImpl)34 ByteArrayWrapper (org.aion.util.types.ByteArrayWrapper)30 AionTxReceipt (org.aion.base.AionTxReceipt)29 Hex.toHexString (org.aion.util.conversions.Hex.toHexString)28 AccountState (org.aion.base.AccountState)26 EventBlock (org.aion.evtmgr.impl.evt.EventBlock)26 JSONArray (org.json.JSONArray)26 JSONObject (org.json.JSONObject)26 MiningBlockHeader (org.aion.zero.impl.types.MiningBlockHeader)22 AionTxExecSummary (org.aion.base.AionTxExecSummary)20