Search in sources :

Example 36 with UInt256

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;
    }
}
Also used : TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) Block(neo.model.core.Block) UInt256(neo.model.bytes.UInt256)

Aggregations

UInt256 (neo.model.bytes.UInt256)36 TransactionOutput (neo.model.core.TransactionOutput)15 JSONObject (org.json.JSONObject)14 TreeMap (java.util.TreeMap)13 Fixed8 (neo.model.bytes.Fixed8)13 Transaction (neo.model.core.Transaction)13 Map (java.util.Map)11 UInt160 (neo.model.bytes.UInt160)11 CoinReference (neo.model.core.CoinReference)11 Block (neo.model.core.Block)8 JSONArray (org.json.JSONArray)7 UInt16 (neo.model.bytes.UInt16)5 BlockDb (neo.model.db.BlockDb)5 ArrayList (java.util.ArrayList)3 Header (neo.model.core.Header)3 BTreeMap (org.mapdb.BTreeMap)3 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 InvPayload (neo.model.network.InvPayload)2 Color (java.awt.Color)1