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();
}
}
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();
}
}
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();
}
}
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();
}
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();
}
}
Aggregations