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());
}
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;
}
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();
}
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());
}
}
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);
}
Aggregations