use of neo.model.bytes.UInt256 in project neo-java by coranos.
the class LocalNodeDataSynchronizedUtil method verifyUnverifiedBlocks.
/**
* verify any unverified blocks, by checking for their prevHash in the
* blockchain.
*
* @param localNodeData
* the local node data to use.
* @return true if any block was added to the chain.
*/
public static boolean verifyUnverifiedBlocks(final LocalNodeData localNodeData) {
synchronized (localNodeData) {
boolean anyBlockChanged = false;
final List<Block> putBlockList = new ArrayList<>();
final Set<UInt256> putBlockHashs = new TreeSet<>();
for (final Block block : localNodeData.getUnverifiedBlockPoolSet()) {
if (localNodeData.getBlockDb().containsBlockWithHash(block.prevHash) || block.hash.equals(GenesisBlockUtil.GENESIS_HASH) || putBlockHashs.contains(block.prevHash)) {
putBlockList.add(block);
putBlockHashs.add(block.hash);
anyBlockChanged = true;
localNodeData.updateHighestBlockTime();
final long blockIndex = block.getIndexAsLong();
localNodeData.getVerifiedHeaderPoolMap().remove(blockIndex);
}
}
final Block highestBlock = localNodeData.getBlockDb().getHeaderOfBlockWithMaxIndex();
if (!putBlockList.isEmpty()) {
final boolean forceSynch;
final long localBlockHeight;
if (highestBlock != null) {
localBlockHeight = highestBlock.getIndexAsLong();
} else {
localBlockHeight = 0;
}
final long blockchainBlockHeight = localNodeData.getBlockchainBlockHeight();
final long localBlockHeightLowerBy = blockchainBlockHeight - localBlockHeight;
if (localBlockHeightLowerBy < 10) {
forceSynch = true;
} else {
forceSynch = false;
}
if (LOG.isDebugEnabled()) {
final String msg = "INTERIM put forceSynch={};localBlockHeight:{};blockchainBlockHeight:{};localBlockHeightLowerBy:{};";
LOG.debug(msg, forceSynch, localBlockHeight, blockchainBlockHeight, localBlockHeightLowerBy);
}
localNodeData.getBlockDb().put(forceSynch, putBlockList.toArray(new Block[0]));
}
if (highestBlock != null) {
final long highestIndex = highestBlock.getIndexAsLong();
final Iterator<Block> blockIt = localNodeData.getUnverifiedBlockPoolSet().iterator();
while (blockIt.hasNext()) {
final Block block = blockIt.next();
if (block.getIndexAsLong() <= highestIndex) {
blockIt.remove();
anyBlockChanged = true;
}
}
}
return anyBlockChanged;
}
}
Aggregations