Search in sources :

Example 31 with Block

use of io.nuls.kernel.model.Block in project nuls by nuls-io.

the class AssemblyBlockUtil method assemblyBlock.

public static Block assemblyBlock(BlockHeader header, Map<NulsDigestData, Transaction> txMap, List<NulsDigestData> txHashList) {
    Block block = new Block();
    block.setHeader(header);
    List<Transaction> txs = new ArrayList<>();
    for (NulsDigestData txHash : txHashList) {
        Transaction tx = txMap.get(txHash);
        if (null == tx) {
            throw new NulsRuntimeException(TransactionErrorCode.TX_NOT_EXIST);
        }
        tx.setBlockHeight(header.getHeight());
        txs.add(tx);
    }
    block.setTxs(txs);
    return block;
}
Also used : Transaction(io.nuls.kernel.model.Transaction) ArrayList(java.util.ArrayList) Block(io.nuls.kernel.model.Block) NulsDigestData(io.nuls.kernel.model.NulsDigestData) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException)

Example 32 with Block

use of io.nuls.kernel.model.Block in project nuls by nuls-io.

the class DownloadThreadManager method checkFirstBlock.

private boolean checkFirstBlock() throws NulsException {
    Block localBestBlock = blockService.getBestBlock().getData();
    if (localBestBlock.getHeader().getHeight() == 0 || (newestInfos.getNetBestHeight() == localBestBlock.getHeader().getHeight() && newestInfos.getNetBestHash().equals(localBestBlock.getHeader().getHash()))) {
        return true;
    }
    if (newestInfos.getNetBestHeight() < localBestBlock.getHeader().getHeight()) {
        BlockHeader header = blockService.getBlockHeader(newestInfos.getNetBestHash()).getData();
        if (null == header && networkService.getAvailableNodes().size() >= networkService.getNetworkParam().getMaxOutCount() && DoubleUtils.div(newestInfos.getNodes().size(), networkService.getAvailableNodes().size(), 2) >= 0.5d) {
            for (long i = localBestBlock.getHeader().getHeight(); i <= newestInfos.getNetBestHeight(); i--) {
                consensusService.rollbackBlock(localBestBlock);
                localBestBlock = blockService.getBestBlock().getData();
            }
        } else if (null == header) {
            resetNetwork("The local block is higher than the network block, the number of connected nodes is not enough to allow the local rollbackTx, so reset");
            return false;
        }
    } else {
        // check need rollbackTx
        return checkRollback(localBestBlock, 0);
    }
    return true;
}
Also used : Block(io.nuls.kernel.model.Block) BlockHeader(io.nuls.kernel.model.BlockHeader)

Example 33 with Block

use of io.nuls.kernel.model.Block in project nuls by nuls-io.

the class BlockMessageHandler method onMessage.

@Override
public void onMessage(BlockMessage event, Node fromNode) {
    Block block = event.getMsgBody();
    if (null == block) {
        Log.warn("recieved a null blockEvent form " + fromNode.getId());
        return;
    }
    // Log.info("recieved:::::" + block.getHeader().getHeight() + ":::::" + fromNode.getId());
    boolean result = CollectThread.getInstance().addBlock(block);
    if (result) {
        return;
    }
    ProtocolCacheHandler.receiveBlock(block);
}
Also used : Block(io.nuls.kernel.model.Block)

Example 34 with Block

use of io.nuls.kernel.model.Block in project nuls by nuls-io.

the class CollectThread method waitBlock.

private Block waitBlock(long height) throws InterruptedException {
    Block block = null;
    long totalWait = 0;
    while (null == block && height == startHeight) {
        Thread.sleep(10L);
        totalWait += 10;
        if (totalWait > 5000) {
            boolean b = this.requestThread.retryDownload(height, getRequestSize());
            if (!b) {
                break;
            }
            // Log.info("Height:" + height + ",累计等待时间ms:::::" + totalWait+" , map size:"+map.size());
            totalWait = 0;
        }
        block = map.remove(startHeight);
    }
    // Log.info("Height:" + height + ",累计等待时间ms:::::" + totalWait+" , map size:"+map.size());
    return block;
}
Also used : Block(io.nuls.kernel.model.Block)

Aggregations

Block (io.nuls.kernel.model.Block)34 NulsDigestData (io.nuls.kernel.model.NulsDigestData)9 Test (org.junit.Test)9 BaseTest (io.nuls.consensus.poc.BaseTest)8 BlockHeader (io.nuls.kernel.model.BlockHeader)8 BlockContainer (io.nuls.consensus.poc.container.BlockContainer)4 ChainContainer (io.nuls.consensus.poc.container.ChainContainer)4 Chain (io.nuls.consensus.poc.model.Chain)4 Result (io.nuls.kernel.model.Result)4 NulsException (io.nuls.kernel.exception.NulsException)3 NulsRuntimeException (io.nuls.kernel.exception.NulsRuntimeException)3 Node (io.nuls.network.model.Node)3 DownloadService (io.nuls.protocol.service.DownloadService)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 MicroKernelBootstrap (io.nuls.kernel.MicroKernelBootstrap)2 CompleteParam (io.nuls.protocol.model.CompleteParam)2 SmallBlock (io.nuls.protocol.model.SmallBlock)2 BlockService (io.nuls.protocol.service.BlockService)2 HashSet (java.util.HashSet)2