Search in sources :

Example 6 with Block

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

the class BlockServiceImplTest method init.

@Before
public void init() {
    MicroKernelBootstrap mk = MicroKernelBootstrap.getInstance();
    mk.init();
    mk.start();
    LevelDbModuleBootstrap bootstrap = new LevelDbModuleBootstrap();
    bootstrap.init();
    bootstrap.start();
    UtxoLedgerModuleBootstrap ledgerModuleBootstrap = new UtxoLedgerModuleBootstrap();
    ledgerModuleBootstrap.init();
    ledgerModuleBootstrap.start();
    service = NulsContext.getServiceBean(BlockService.class);
    Block block = new Block();
    BlockHeader blockHeader = new BlockHeader();
    blockHeader.setHash(NulsDigestData.calcDigestData("hashhash".getBytes()));
    blockHeader.setHeight(1286L);
    blockHeader.setExtend("extends".getBytes());
    blockHeader.setMerkleHash(NulsDigestData.calcDigestData("merkleHash".getBytes()));
    blockHeader.setPreHash(NulsDigestData.calcDigestData("prehash".getBytes()));
    try {
        blockHeader.setPackingAddress("address".getBytes());
    } catch (Exception e) {
        e.printStackTrace();
        assertTrue(false);
    }
    blockHeader.setBlockSignature(new BlockSignature());
    blockHeader.setTime(12345678901L);
    blockHeader.setTxCount(3);
    List<NulsDigestData> txHashList = new ArrayList<>();
    txHashList.add(NulsDigestData.calcDigestData("first-tx-hash".getBytes()));
    txHashList.add(NulsDigestData.calcDigestData("second-tx-hash".getBytes()));
    txHashList.add(NulsDigestData.calcDigestData("third-tx-hash".getBytes()));
// block.setTxHashList(txHashList);
// this.model = blockHeader;
}
Also used : LevelDbModuleBootstrap(io.nuls.db.module.impl.LevelDbModuleBootstrap) BlockSignature(io.nuls.kernel.script.BlockSignature) UtxoLedgerModuleBootstrap(io.nuls.ledger.module.impl.UtxoLedgerModuleBootstrap) ArrayList(java.util.ArrayList) BlockService(io.nuls.protocol.service.BlockService) Block(io.nuls.kernel.model.Block) NulsDigestData(io.nuls.kernel.model.NulsDigestData) BlockHeader(io.nuls.kernel.model.BlockHeader) MicroKernelBootstrap(io.nuls.kernel.MicroKernelBootstrap) Before(org.junit.Before)

Example 7 with Block

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

the class CollectThread method pushBlock.

private boolean pushBlock() throws InterruptedException {
    Block block = map.remove(startHeight);
    if (null == block) {
        if (startHeight - NulsContext.getInstance().getBestHeight() > 1000) {
            Thread.sleep(10L);
            return false;
        }
        block = waitBlock(startHeight);
    }
    if (null == block) {
        return false;
    }
    Result result = consensusService.addBlock(block);
    if (result.isSuccess()) {
        startHeight++;
        return true;
    }
    return false;
}
Also used : Block(io.nuls.kernel.model.Block) Result(io.nuls.kernel.model.Result)

Example 8 with Block

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

the class OrphanBlockProcess method process.

public void process(BlockContainer blockContainer) throws IOException {
    if (ConsensusStatusContext.getConsensusStatus().ordinal() < ConsensusStatus.RUNNING.ordinal()) {
        return;
    }
    Block block = blockContainer.getBlock();
    // 只处理本地误差配置的块个数以内的孤块
    long bestBlockHeight = chainManager.getBestBlockHeight();
    if (Math.abs(bestBlockHeight - block.getHeader().getHeight()) > PocConsensusConstant.MAX_ISOLATED_BLOCK_COUNT) {
        return;
    }
    // Because it is not possible to ensure that there will be repeated reception, priority is given to
    // 因为不能确保是否会有重复收到的情况,所以在此优先去重
    boolean hasExist = checkHasExist(block.getHeader().getHash());
    if (hasExist) {
        return;
    }
    ChainLog.debug("process isolated block, bestblockheight:{}, isolated {} - {}", bestBlockHeight, block.getHeader().getHeight(), block.getHeader().getHash().getDigestHex());
    // Checks if the current orphaned block is connected to an existing orphaned chain
    // 检查当前孤立块是否和已经存在的孤立链连接
    boolean success = chainManager.checkIsBeforeOrphanChainAndAdd(block);
    ChainLog.debug("checkIsBeforeIsolatedChainAndAdd: {} , block {} - {}", success, block.getHeader().getHeight(), block.getHeader().getHash().getDigestHex());
    if (success) {
        // Connect to the current isolated chain and continue to find the previous block
        // 和当前的孤立链连接,继续寻找上一区块
        foundAndProcessPreviousBlock(blockContainer);
        return;
    }
    success = chainManager.checkIsAfterOrphanChainAndAdd(block);
    ChainLog.debug("checkIsAfterIsolatedChainAndAdd: {} , block {} - {}", success, block.getHeader().getHeight(), block.getHeader().getHash().getDigestHex());
    if (success) {
        // 成功找到,并且连接上了,接下来不需要做任何操作
        return;
    }
    // Not found, then create a new isolated chain, then find the previous block
    // 没有找到,那么新建一条孤立链,接着寻找上一个区块
    chainManager.newOrphanChain(block);
    ChainLog.debug("new a isolated chain , block {} - {}", success, block.getHeader().getHeight(), block.getHeader().getHash().getDigestHex());
    foundAndProcessPreviousBlock(blockContainer);
}
Also used : Block(io.nuls.kernel.model.Block)

Example 9 with Block

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

the class BlockMonitorProcess method doProcess.

public void doProcess() {
    Block bestBlock = NulsContext.getInstance().getBestBlock();
    if (bestBlock.getHeader().getHeight() == 0) {
        return;
    }
    if (bestBlock.getHeader().getHash().equals(lastBestHash) && bestBlock.getHeader().getTime() < (TimeService.currentTimeMillis() - RESET_TIME_INTERVAL)) {
        lastBestHash = bestBlock.getHeader().getHash();
        NulsContext.getServiceBean(ConsensusPocServiceImpl.class).reset();
        return;
    }
    lastBestHash = bestBlock.getHeader().getHash();
    List<Block> blockList = chainManager.getMasterChain().getChain().getAllBlockList();
    int minCount = 3;
    if (blockList.size() < minCount) {
        return;
    }
    int count = 0;
    Set<String> addressSet = new HashSet<>();
    for (int i = blockList.size() - 1; i >= 0; i--) {
        Block block = blockList.get(i);
        addressSet.add(block.getHeader().getPackingAddressStr());
        count++;
        if (count > minCount) {
            break;
        }
    }
    DownloadService downloadService = NulsContext.getServiceBean(DownloadService.class);
    if (count > minCount && addressSet.size() == 1 && ConsensusConfig.getSeedNodeList().size() > 1) {
        // NulsContext.getServiceBean(ConsensusPocServiceImpl.class).reset();
        return;
    }
    if (downloadService.isDownloadSuccess().isSuccess() && bestBlock.getHeader().getTime() < (TimeService.currentTimeMillis() - RESET_TIME_INTERVAL)) {
    // NulsContext.getServiceBean(ConsensusPocServiceImpl.class).reset();
    }
}
Also used : ConsensusPocServiceImpl(io.nuls.consensus.poc.service.impl.ConsensusPocServiceImpl) Block(io.nuls.kernel.model.Block) DownloadService(io.nuls.protocol.service.DownloadService) HashSet(java.util.HashSet)

Example 10 with Block

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

the class CacheLoader method loadBlocks.

/**
 * 从数据存储中加载指定个数的最新块
 * Loads the latest block of the specified number from the data store.
 *
 * @param size 加载数量/load count
 * @return 区块列表/block list
 */
public List<Block> loadBlocks(int size) {
    List<Block> blockList = new ArrayList<>();
    Block block = blockService.getBestBlock().getData();
    if (null == block) {
        return blockList;
    }
    for (int i = size; i >= 0; i--) {
        if (block == null) {
            break;
        }
        blockList.add(0, block);
        if (block.getHeader().getHeight() == 0L) {
            break;
        }
        NulsDigestData preHash = block.getHeader().getPreHash();
        block = blockService.getBlock(preHash).getData();
        if (block == null || block.getHeader().getHeight() == 0L) {
            break;
        }
    }
    return blockList;
}
Also used : ArrayList(java.util.ArrayList) Block(io.nuls.kernel.model.Block) NulsDigestData(io.nuls.kernel.model.NulsDigestData)

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