Search in sources :

Example 21 with Block

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

the class ConsensusToolTest method testGetSmallBlock.

@Test
public void testGetSmallBlock() {
    Block block = createBlock();
    SmallBlock smallBlock = ConsensusTool.getSmallBlock(block);
    assertNotNull(smallBlock);
    assertEquals(smallBlock.getHeader(), block.getHeader());
    assertEquals(smallBlock.getSubTxList().size(), 0);
    assertEquals(smallBlock.getTxHashList().get(0), block.getTxs().get(0).getHash());
}
Also used : SmallBlock(io.nuls.protocol.model.SmallBlock) Block(io.nuls.kernel.model.Block) SmallBlock(io.nuls.protocol.model.SmallBlock) Test(org.junit.Test) BaseTest(io.nuls.consensus.poc.BaseTest)

Example 22 with Block

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

the class ConsensusConfig method initConfiguration.

public static void initConfiguration() throws Exception {
    Block genesisBlock = GenesisBlock.getInstance();
    NulsContext.getInstance().setGenesisBlock(genesisBlock);
    partakePacking = NulsConfig.MODULES_CONFIG.getCfgValue(CFG_CONSENSUS_SECTION, PROPERTY_PARTAKE_PACKING, false);
    Set<String> seedAddressSet = new HashSet<>();
    String addresses = NulsConfig.MODULES_CONFIG.getCfgValue(CFG_CONSENSUS_SECTION, PROPERTY_SEED_NODES, "");
    if (StringUtils.isBlank(addresses)) {
        return;
    }
    String[] array = addresses.split(SEED_NODES_DELIMITER);
    if (null == array) {
        return;
    }
    for (String address : array) {
        seedAddressSet.add(address);
    }
    for (String address : seedAddressSet) {
        seedNodeBytesList.add(AddressTool.getAddress(address));
        seedNodeStringList.add(address);
    }
    int minUpgradeDelay = NulsConfig.MODULES_CONFIG.getCfgValue(CFG_CONSENSUS_SECTION, MIN_PROTOCOL_UPGRADE_DELAY, 1000);
    minProtocolUpgradeDelay = minUpgradeDelay;
}
Also used : Block(io.nuls.kernel.model.Block) HashSet(java.util.HashSet)

Example 23 with Block

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

the class CacheManager method load.

public void load() throws NulsException {
    // load storage data to memory
    List<BlockHeader> blockHeaderList = cacheLoader.loadBlockHeaders(PocConsensusConstant.INIT_HEADERS_OF_ROUND_COUNT);
    List<Block> blockList = cacheLoader.loadBlocks(PocConsensusConstant.INIT_BLOCKS_COUNT);
    if (blockHeaderList == null || blockHeaderList.size() == 0 || blockList == null || blockList.size() == 0) {
        Log.error("load cache error ,not find the block info!");
        throw new NulsRuntimeException(KernelErrorCode.DATA_ERROR);
    }
    List<Agent> agentList = cacheLoader.loadAgents();
    List<Deposit> depositList = cacheLoader.loadDepositList();
    List<PunishLogPo> allPunishList = NulsContext.getServiceBean(PunishLogStorageService.class).getPunishList();
    List<PunishLogPo> yellowPunishList = cacheLoader.loadYellowPunishList(allPunishList, PocConsensusConstant.INIT_HEADERS_OF_ROUND_COUNT);
    List<PunishLogPo> redPunishList = cacheLoader.loadRedPunishList(allPunishList);
    Chain masterChain = new Chain();
    masterChain.initData(blockList.get(0).getHeader(), blockHeaderList, blockList);
    masterChain.setAgentList(agentList);
    masterChain.setDepositList(depositList);
    masterChain.setYellowPunishList(yellowPunishList);
    masterChain.setRedPunishList(redPunishList);
    ChainContainer masterChainContainer = new ChainContainer(masterChain);
    chainManager.setMasterChain(masterChainContainer);
    chainManager.getMasterChain().initRound();
}
Also used : Agent(io.nuls.consensus.poc.protocol.entity.Agent) Deposit(io.nuls.consensus.poc.protocol.entity.Deposit) Chain(io.nuls.consensus.poc.model.Chain) ChainContainer(io.nuls.consensus.poc.container.ChainContainer) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) Block(io.nuls.kernel.model.Block) PunishLogStorageService(io.nuls.consensus.poc.storage.service.PunishLogStorageService) BlockHeader(io.nuls.kernel.model.BlockHeader) PunishLogPo(io.nuls.consensus.poc.storage.po.PunishLogPo)

Example 24 with Block

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

the class OrphanBlockProcess method foundAndProcessPreviousBlock.

private void foundAndProcessPreviousBlock(BlockContainer blockContainer) {
    BlockHeader blockHeader = blockContainer.getBlock().getHeader();
    // Determine whether the previous block already exists. If it already exists, it will not be downloaded.
    // 判断上一区块是否已经存在,如果已经存在则不下载
    boolean hasExist = checkHasExist(blockHeader.getPreHash());
    if (hasExist) {
        return;
    }
    Block preBlock = downloadService.downloadBlock(blockHeader.getPreHash(), blockContainer.getNode()).getData();
    if (preBlock != null) {
        ChainLog.debug("get pre block success {} - {}", preBlock.getHeader().getHeight(), preBlock.getHeader().getHash());
        orphanBlockProvider.addBlock(new BlockContainer(preBlock, blockContainer.getNode(), BlockContainerStatus.DOWNLOADING));
    } else {
        ChainLog.debug("get pre block fail {} - {}", blockHeader.getHeight() - 1, blockHeader.getPreHash());
        // 失败情况的处理,从其它所有可用的节点去获取,如果都不成功,那么就失败,包括本次失败的节点,再次获取一次
        for (Node node : networkService.getAvailableNodes()) {
            preBlock = downloadService.downloadBlock(blockHeader.getPreHash(), node).getData();
            if (preBlock != null) {
                orphanBlockProvider.addBlock(new BlockContainer(preBlock, node, BlockContainerStatus.DOWNLOADING));
                ChainLog.debug("get pre block retry success {} - {}", preBlock.getHeader().getHeight() - 1, preBlock.getHeader().getPreHash());
                return;
            }
        }
        ChainLog.debug("get pre block complete failure {} - {}", blockHeader.getHeight() - 1, blockHeader.getPreHash());
    }
}
Also used : BlockContainer(io.nuls.consensus.poc.container.BlockContainer) Node(io.nuls.network.model.Node) Block(io.nuls.kernel.model.Block) BlockHeader(io.nuls.kernel.model.BlockHeader)

Example 25 with Block

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

the class PocRewardCacheService method initCache.

public void initCache() {
    Collection<Account> accountList = accountService.getAccountList().getData();
    if (null == accountList || accountList.isEmpty()) {
        return;
    }
    long startTime = TimeService.currentTimeMillis() - 24 * 3600000L;
    long startHeight = NulsContext.getInstance().getBestHeight() - (24 * 3600 / ProtocolConstant.BLOCK_TIME_INTERVAL_SECOND);
    if (startHeight <= 0) {
        startHeight = 1;
    }
    long index = startHeight;
    while (true) {
        Block block = blockService.getBlock(index++).getData();
        if (null == block) {
            break;
        }
        if (block.getHeader().getHeight() < 1) {
            break;
        }
        if (block.getHeader().getTime() < startTime) {
            continue;
        }
        this.addBlock(block);
    }
    // 本地账户的历史收益累计
    for (Account account : accountList) {
        List<TransactionInfo> list = accountLedgerService.getTxInfoList(account.getAddress().getAddressBytes()).getData();
        if (list == null || list.isEmpty()) {
            continue;
        }
        calcRewardHistory(account.getAddress().getBase58(), list, startHeight);
    }
    long totalValue = ledgerService.getWholeUTXO();
    this.totalRewardHeight = NulsContext.getInstance().getBestHeight();
    this.totalReward = Na.valueOf(totalValue - Na.MAX_NA_VALUE);
}
Also used : Account(io.nuls.account.model.Account) Block(io.nuls.kernel.model.Block) TransactionInfo(io.nuls.account.ledger.model.TransactionInfo)

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