use of io.nuls.core.exception.NulsException in project nuls by nuls-io.
the class VersionManager method start.
public static void start() throws NulsException {
URL libsUrl = VersionUtils.class.getClassLoader().getResource("libs");
if (null == libsUrl) {
// devlopment
return;
}
List<NulsVersion> versionList = NulsContext.getServiceBeanList(NulsVersion.class);
String localVersion = null;
for (NulsVersion version : versionList) {
if (null == localVersion) {
localVersion = version.getVersion();
continue;
}
if (VersionUtils.higherThan(version.getVersion(), localVersion)) {
localVersion = version.getVersion();
continue;
}
}
NulsContext.VERSION = localVersion;
String jsonStr = null;
try {
jsonStr = new String(HttpDownloadUtils.download(HIGHEST_VERDION_FILE_URL), NulsContext.DEFAULT_ENCODING);
} catch (IOException e) {
Log.error(e);
return;
}
Map<String, Object> map = null;
try {
map = JSONUtils.json2map(jsonStr);
} catch (Exception e) {
Log.error(e);
throw new NulsException(ErrorCode.FAILED, "Parse version json faild!");
}
String version = (String) map.get("version");
String sign = (String) map.get("sign");
// todo 验证签名
NulsContext.NEWEST_VERSION = version;
// todo 自动更新开关
boolean autoUpdate = true;
if (VersionUtils.equalsWith(version, localVersion)) {
return;
} else if (autoUpdate) {
try {
updateFile(localVersion, version, versionList, libsUrl);
} catch (IOException e) {
throw new NulsException(e);
}
}
}
use of io.nuls.core.exception.NulsException in project nuls by nuls-io.
the class BaseEvent method parse.
@Override
protected void parse(NulsByteBuffer byteBuffer) throws NulsException {
this.header = byteBuffer.readNulsData(new EventHeader());
this.eventBody = parseEventBody(byteBuffer);
try {
this.hash = NulsDigestData.calcDigestData(this.serialize());
} catch (IOException e) {
Log.error(e);
throw new NulsException(ErrorCode.DATA_PARSE_ERROR);
}
}
use of io.nuls.core.exception.NulsException in project nuls by nuls-io.
the class BlockManager method rollbackTxList.
private void rollbackTxList(List<Transaction> txList, int start, int end) {
List<NulsDigestData> txHashList = new ArrayList<>();
for (int i = start; i <= end && i < txList.size(); i++) {
Transaction tx = txList.get(i);
if (tx.getStatus() == TxStatusEnum.AGREED) {
try {
ledgerService.rollbackTx(tx);
} catch (NulsException e) {
Log.error(e);
}
txHashList.add(tx.getHash());
}
}
confirmingTxCacheManager.removeTxList(txHashList);
}
use of io.nuls.core.exception.NulsException in project nuls by nuls-io.
the class BlockManager method appravalBlock.
private void appravalBlock(Block block) {
for (int i = 0; i < block.getHeader().getTxCount(); i++) {
Transaction tx = block.getTxs().get(i);
tx.setBlockHeight(block.getHeader().getHeight());
tx.setIndex(i);
tx.setIndex(i);
if (tx.getStatus() == null || tx.getStatus() == TxStatusEnum.CACHED) {
try {
this.ledgerService.approvalTx(tx);
confirmingTxCacheManager.putTx(tx);
} catch (NulsException e) {
rollbackTxList(block.getTxs(), 0, i);
Log.error(e);
throw new NulsRuntimeException(e);
}
}
}
txCacheManager.removeTx(block.getTxHashList());
}
use of io.nuls.core.exception.NulsException in project nuls by nuls-io.
the class HeaderContinuityValidator method validate.
@Override
public ValidateResult validate(BlockHeader header) {
ValidateResult result = ValidateResult.getSuccessResult();
boolean failed = false;
do {
if (header.getHeight() == 0) {
failed = !header.getPreHash().equals(NulsDigestData.EMPTY_HASH);
break;
}
BlockHeader preHeader = null;
try {
preHeader = NulsContext.getServiceBean(BlockService.class).getBlockHeader(header.getHeight() - 1);
} catch (NulsException e) {
// todo
e.printStackTrace();
}
if (null == preHeader) {
break;
}
failed = !preHeader.getHash().equals(header.getPreHash());
if (failed) {
break;
}
BlockRoundData roundData = new BlockRoundData();
try {
roundData.parse(header.getExtend());
} catch (NulsException e) {
Log.error(e);
}
long shouldTime = roundData.getRoundStartTime() + roundData.getPackingIndexOfRound() * PocConsensusConstant.BLOCK_TIME_INTERVAL_SECOND * 1000;
// todo 3 seconds error
long difference = header.getTime() - shouldTime;
failed = difference > 3000 || difference < -3000;
if (failed) {
break;
}
} while (false);
if (failed) {
result = ValidateResult.getFailedResult(ERROR_MESSAGE);
}
return result;
}
Aggregations