use of io.nuls.core.chain.entity.Block in project nuls by nuls-io.
the class BlockBatchDownloadUtils method checkHash.
private Result checkHash() throws InterruptedException {
for (long i = currentRound.getStart(); i <= currentRound.getEnd(); i++) {
Block block = blockMap.get(i);
if (null == block) {
return Result.getFailed("data error");
}
if ((i - currentRound.getStart()) % DOWNLOAD_BLOCKS_PER_TIME == 0) {
NulsDigestData mustHash = blocksHash.getHash(block.getHeader().getHeight());
NulsDigestData hash = block.getHeader().getHash();
if (null == hash || null == mustHash || !mustHash.getDigestHex().equals(hash.getDigestHex())) {
failedExecute(block.getHeader().getHeight());
return Result.getFailed("hash wrong!");
}
}
String preHash = block.getHeader().getPreHash().getDigestHex();
Block preBlock = blockMap.get(block.getHeader().getHeight() - 1);
if (preBlock == null) {
preBlock = blockService.getBlock(preHash);
}
if (null == preBlock || preBlock.getHeader().getHeight() != (block.getHeader().getHeight() - 1)) {
failedExecute(block.getHeader().getHeight());
return Result.getFailed("prehash wrong!");
}
}
return Result.getSuccess();
}
use of io.nuls.core.chain.entity.Block in project nuls by nuls-io.
the class BlockBatchDownloadUtils method verify.
private synchronized void verify() {
boolean done = true;
if (nodeStatusMap.isEmpty()) {
working = false;
return;
}
for (NodeDownloadingStatus status : nodeStatusMap.values()) {
if (!done) {
break;
}
done = status.finished();
if (!done && status.getUpdateTime() < (TimeService.currentTimeMillis() - DOWNLOAD_IDLE_TIME_OUT)) {
nodeStatusMap.remove(status.getNodeId());
try {
failedExecute(status);
} catch (InterruptedException e) {
Log.error(e);
}
}
}
if (!done) {
return;
}
Result result;
try {
result = checkHash();
} catch (InterruptedException e) {
Log.error(e);
return;
}
if (null == result || result.isFailed()) {
return;
}
for (long i = currentRound.getStart(); i <= currentRound.getEnd(); i++) {
Block block = blockMap.get(i);
if (null == block) {
// todo
Log.error("cache block is null");
break;
}
ValidateResult result1 = block.verify();
if (result1.isFailed() && result1.getErrorCode() != ErrorCode.ORPHAN_TX) {
if (null != result1.getMessage()) {
Log.info(result1.getMessage());
}
try {
failedExecute(block.getHeader().getHeight());
} catch (InterruptedException e) {
Log.error(e);
}
return;
}
blockManager.addBlock(block, false, null);
receivedTxCacheManager.removeTx(block.getTxHashList());
confirmingTxCacheManager.putTxList(block.getTxs());
}
finished();
}
use of io.nuls.core.chain.entity.Block in project nuls by nuls-io.
the class BlockServiceImpl method rollbackBlock.
@Override
@DbSession
public void rollbackBlock(long height) {
Block block = this.getBlock(height);
if (null == block) {
return;
}
blockManager.rollback(block);
this.rollback(block.getTxs(), block.getTxs().size() - 1);
this.ledgerService.deleteTx(block.getHeader().getHeight());
blockStorageService.delete(block.getHeader().getHash().getDigestHex());
}
use of io.nuls.core.chain.entity.Block in project nuls by nuls-io.
the class BlockStorageService method getBlockHeaderList.
public List<BlockHeader> getBlockHeaderList(long startHeight, long endHeight, long split) {
List<BlockHeaderPo> strList = this.headerDao.getHashList(startHeight, endHeight, split);
Map<Long, BlockHeader> headerMap = new HashMap<>();
for (BlockHeaderPo po : strList) {
BlockHeader header = new BlockHeader();
header.setHash(NulsDigestData.fromDigestHex(po.getHash()));
header.setHeight(po.getHeight());
headerMap.put(po.getHeight(), header);
}
if ((endHeight - startHeight + 1) == headerMap.size()) {
return new ArrayList<>(headerMap.values());
}
List<BlockHeader> headerList = new ArrayList<>();
for (long i = startHeight; i <= endHeight; i++) {
if (headerMap.containsKey(i)) {
headerList.add(headerMap.get(i));
continue;
}
BlockHeader header = blockCacheManager.getBlockHeader(i);
if (null == header) {
Block block = blockCacheManager.getBlock(i);
if (null != block) {
header = block.getHeader();
}
}
if (null != header) {
headerList.add(header);
}
}
return headerList;
}
use of io.nuls.core.chain.entity.Block in project nuls by nuls-io.
the class BlockStorageService method getBlock.
public Block getBlock(long height) throws Exception {
Block block = blockCacheManager.getBlock(height);
if (null != block && block.getTxs().size() == block.getHeader().getTxCount()) {
return block;
}
BlockHeader header = getBlockHeader(height);
if (null == header) {
return null;
}
List<Transaction> txList = null;
try {
txList = ledgerService.getTxList(height);
} catch (Exception e) {
Log.error(e);
}
if (header.getTxCount() != txList.size()) {
Log.warn("block has wrong tx size!");
}
return fillBlock(header, txList);
}
Aggregations