Search in sources :

Example 16 with Block

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

the class ChainTest method test.

@Test
public void test() {
    Chain chain = new Chain();
    assertNotNull(chain.getId());
    assertNull(chain.getBestBlock());
    Block block = new Block();
    BlockHeader blockHeader = new BlockHeader();
    blockHeader.setHeight(100l);
    block.setHeader(blockHeader);
    chain.getAllBlockList().add(block);
    Block bestBlock = chain.getBestBlock();
    assertNotNull(bestBlock);
    assertEquals(bestBlock.getHeader().getHeight(), 100l);
}
Also used : Block(io.nuls.kernel.model.Block) BlockHeader(io.nuls.kernel.model.BlockHeader) Test(org.junit.Test)

Example 17 with Block

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

the class BlockQueueProviderTest method testSizeAndClear.

@Test
public void testSizeAndClear() {
    assertNotNull(blockQueueProvider);
    assertEquals(0, blockQueueProvider.size());
    Block block = new Block();
    boolean result = blockQueueProvider.put(new BlockContainer(block, BlockContainerStatus.RECEIVED));
    assertTrue(result);
    assertEquals(1, blockQueueProvider.size());
    blockQueueProvider.clear();
    assertEquals(0, blockQueueProvider.size());
}
Also used : BlockContainer(io.nuls.consensus.poc.container.BlockContainer) Block(io.nuls.kernel.model.Block) Test(org.junit.Test) BaseTest(io.nuls.consensus.poc.BaseTest)

Example 18 with Block

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

the class Bootstrap method sysStart.

private static void sysStart() throws Exception {
    do {
        MicroKernelBootstrap mk = MicroKernelBootstrap.getInstance();
        mk.init();
        mk.start();
        WalletVersionManager.start();
        initModules();
        String ip = NulsConfig.MODULES_CONFIG.getCfgValue(RpcConstant.CFG_RPC_SECTION, RpcConstant.CFG_RPC_SERVER_IP, RpcConstant.DEFAULT_IP);
        int port = NulsConfig.MODULES_CONFIG.getCfgValue(RpcConstant.CFG_RPC_SECTION, RpcConstant.CFG_RPC_SERVER_PORT, RpcConstant.DEFAULT_PORT);
        copyWebFiles();
        if (NULSParams.BOOTSTRAP.getRpcIp() != null) {
            ip = NULSParams.BOOTSTRAP.getRpcIp();
        }
        if (NULSParams.BOOTSTRAP.getRpcPort() != null) {
            port = NULSParams.BOOTSTRAP.getRpcPort();
        }
        RpcServerManager.getInstance().startServer(ip, port);
        LanguageService languageService = NulsContext.getServiceBean(LanguageService.class);
        String languageDB = (String) languageService.getLanguage().getData();
        String language = null == languageDB ? I18nUtils.getLanguage() : languageDB;
        I18nUtils.setLanguage(language);
        if (null == languageDB) {
            languageService.saveLanguage(language);
        }
    } while (false);
    // if isDaemon flag is true, don't launch the WebView
    boolean isDaemon = NulsConfig.MODULES_CONFIG.getCfgValue(RpcConstant.CFG_RPC_SECTION, RpcConstant.CFG_RPC_DAEMON, false);
    if (!isDaemon) {
        TaskManager.asynExecuteRunnable(new WebViewBootstrap());
    }
    int i = 0;
    Map<NulsDigestData, List<Node>> map = new HashMap<>();
    NulsContext context = NulsContext.getInstance();
    DownloadService downloadService = NulsContext.getServiceBean(DownloadService.class);
    while (true) {
        if (context.getStop() > 0) {
            if (context.getStop() == 2) {
                Runtime.getRuntime().addShutdownHook(new ShutdownHook());
            }
            System.exit(0);
        }
        if (NulsContext.mastUpGrade) {
            // 如果强制升级标志开启,停止网络连接
            ModuleManager.getInstance().stopModule(NetworkConstant.NETWORK_MODULE_ID);
            Log.error(">>>>>> The new protocol version has taken effect, the network connection has been disconnected,please upgrade immediately **********");
        }
        try {
            Thread.sleep(1000L);
        } catch (InterruptedException e) {
            Log.error(e);
        }
        if (i > 10) {
            i = 0;
            if (!downloadService.isDownloadSuccess().isSuccess() && CollectThread.getInstance().getStartHeight() > 0) {
                Log.info("collect-start:{},request-start:{},BlockQueueSize:{}", CollectThread.getInstance().getStartHeight(), CollectThread.getInstance().getRequestStartHeight(), BlockQueueProvider.getInstance().size());
            }
            Block bestBlock = NulsContext.getInstance().getBestBlock();
            Collection<Node> nodes = NulsContext.getServiceBean(NetworkService.class).getAvailableNodes();
            Log.info("bestHeight:" + bestBlock.getHeader().getHeight() + " , txCount : " + bestBlock.getHeader().getTxCount() + " , tx memory pool count : " + TxMemoryPool.getInstance().size() + " - " + TxMemoryPool.getInstance().getOrphanPoolSize() + " , hash : " + bestBlock.getHeader().getHash() + ",nodeCount:" + nodes.size());
            map.clear();
            for (Node node : nodes) {
                List<Node> ips = map.get(node.getBestBlockHash());
                if (null == ips) {
                    ips = new ArrayList<>();
                    map.put(node.getBestBlockHash(), ips);
                }
                ips.add(node);
            }
            for (NulsDigestData key : map.keySet()) {
                if (key == null)
                    continue;
                List<Node> nodeList = map.get(key);
                long height = nodeList.get(0).getBestBlockHeight();
                StringBuilder ids = new StringBuilder();
                for (Node node : nodeList) {
                    ids.append("," + node.getId());
                }
                Log.info("height:" + height + ",count:" + nodeList.size() + ", hash:" + key.getDigestHex() + ids);
            }
        } else {
            i++;
        }
    }
}
Also used : ShutdownHook(io.nuls.client.rpc.resources.thread.ShutdownHook) Node(io.nuls.network.model.Node) DownloadService(io.nuls.protocol.service.DownloadService) MicroKernelBootstrap(io.nuls.kernel.MicroKernelBootstrap) WebViewBootstrap(io.nuls.client.web.view.WebViewBootstrap) LanguageService(io.nuls.client.storage.LanguageService) NulsContext(io.nuls.kernel.context.NulsContext) NulsDigestData(io.nuls.kernel.model.NulsDigestData) Block(io.nuls.kernel.model.Block) NetworkService(io.nuls.network.service.NetworkService)

Example 19 with Block

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

the class OrphanStorageServiceImpl method get.

@Override
public Block get(NulsDigestData key) {
    assert (key != null);
    byte[] content = dbService.get(DB_NAME, key.getDigestBytes());
    Block block = new Block();
    try {
        block.parse(content, 0);
    } catch (NulsException e) {
        Log.error(e);
    }
    return block;
}
Also used : NulsException(io.nuls.kernel.exception.NulsException) Block(io.nuls.kernel.model.Block)

Example 20 with Block

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

the class UtxoAccountsServiceImpl method synBlock.

/**
 * @param blockHeight
 * @return
 */
@Override
public boolean synBlock(long blockHeight) {
    Log.debug("synBlock begin===blockHeight:" + blockHeight);
    Block nodeBlock = utxoAccountsStorageService.getBlock(blockHeight).getData();
    if (nodeBlock == null) {
        Log.error("utxoAccounts getBlock faile,blockHeight:" + blockHeight);
        return false;
    }
    boolean hadRoll = false;
    try {
        // get local pre block info /从本地取上一个已同步的区块
        LocalCacheBlockBalance localLatestCacheBlock = utxoAccountsStorageService.getLocalCacheBlock(blockHeight - 1).getData();
        // rollback judge /判断回滚
        while (localLatestCacheBlock != null && !nodeBlock.getHeader().getPreHash().equals(localLatestCacheBlock.getHash())) {
            // roll back info /进行数据回滚
            rollbackBlock(localLatestCacheBlock);
            blockHeight--;
            // get pre block
            localLatestCacheBlock = utxoAccountsStorageService.getLocalCacheBlock(blockHeight - 1).getData();
            nodeBlock = utxoAccountsStorageService.getBlock(blockHeight).getData();
            hadRoll = true;
        }
        if (hadRoll) {
            return false;
        }
    } catch (NulsException e) {
        Log.error(e);
        Log.error("block syn error======blockHeight:" + blockHeight);
        return false;
    }
    // begin syn block/开始同步区块
    // analysis block/解析区块
    Map<String, UtxoAccountsBalancePo> utxoAccountsMap = new HashMap<>();
    if (!buildUtxoAccountsMap(utxoAccountsMap, nodeBlock)) {
        return false;
    }
    List<UtxoAccountsBalancePo> list = new ArrayList<>();
    LocalCacheBlockBalance localCacheBlockBalance = new LocalCacheBlockBalance();
    // LocalCacheBlockBalance preSnapshot=new LocalCacheBlockBalance();
    try {
        list = utxoAccountsMapToList(utxoAccountsMap, localCacheBlockBalance);
    } catch (NulsException e) {
        Log.info("utxoAccountsMapToList error======blockHeight:" + blockHeight);
        return false;
    }
    localCacheBlockBalance.setHash(nodeBlock.getHeader().getHash());
    localCacheBlockBalance.setPreHash(nodeBlock.getHeader().getPreHash());
    // localCacheBlockBalance.setBalanceList(list);
    localCacheBlockBalance.setBlockHeight(blockHeight);
    // save cache block info/缓存最近解析信息
    utxoAccountsStorageService.saveLocalCacheBlock(blockHeight, localCacheBlockBalance);
    // utxoAccountsStorageService.saveLocalCacheChangeSnapshot(blockHeight,localCacheBlockBalance);
    utxoAccountsStorageService.batchSaveByteUtxoAcountsInfo(list);
    // update latest block height/更新最近高度
    utxoAccountsStorageService.saveHadSynBlockHeight(blockHeight);
    // delete overdue cache data/删除过期缓存数据
    if (blockHeight > UtxoAccountsStorageConstant.MAX_CACHE_BLOCK_NUM) {
        utxoAccountsStorageService.deleteLocalCacheBlock(blockHeight - UtxoAccountsStorageConstant.MAX_CACHE_BLOCK_NUM);
    }
    Log.debug("utxoAccounts synBlock success==blockHeight:" + blockHeight);
    return true;
}
Also used : UtxoAccountsBalancePo(io.nuls.utxo.accounts.storage.po.UtxoAccountsBalancePo) NulsException(io.nuls.kernel.exception.NulsException) Block(io.nuls.kernel.model.Block) LocalCacheBlockBalance(io.nuls.utxo.accounts.storage.po.LocalCacheBlockBalance)

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