use of io.nuls.core.validate.ValidateResult 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.validate.ValidateResult in project nuls by nuls-io.
the class AccountServiceImpl method setAlias.
@Override
public Result setAlias(String address, String password, String alias) {
Account account = getAccount(address);
if (account == null) {
return new Result(false, ErrorCode.ACCOUNT_NOT_EXIST, null);
}
if (StringUtils.isNotBlank(account.getAlias())) {
return new Result(false, "Alias has been set up");
}
if (!StringUtils.validAlias(alias)) {
return new Result(false, "The alias is between 3 to 20 characters");
}
try {
TransactionEvent event = new TransactionEvent();
CoinTransferData coinData = new CoinTransferData(OperationType.TRANSFER, AccountConstant.ALIAS_NA, address, ledgerService.getTxFee(TransactionConstant.TX_TYPE_SET_ALIAS));
AliasTransaction aliasTx = new AliasTransaction(coinData, password, new Alias(address, alias));
aliasTx.setHash(NulsDigestData.calcDigestData(aliasTx.serialize()));
aliasTx.setScriptSig(createP2PKHScriptSigFromDigest(aliasTx.getHash(), account, password).serialize());
ValidateResult validate = aliasTx.verify();
if (validate.isFailed()) {
return new Result(false, validate.getMessage());
}
event.setEventBody(aliasTx);
eventBroadcaster.broadcastAndCache(event, true);
SetAliasNotice notice = new SetAliasNotice();
notice.setEventBody(aliasTx);
eventBroadcaster.publishToLocal(notice);
} catch (Exception e) {
Log.error(e);
return new Result(false, e.getMessage());
}
return new Result(true, "OK");
}
use of io.nuls.core.validate.ValidateResult in project nuls by nuls-io.
the class NewTxEventHandler method onEvent.
@Override
public void onEvent(TransactionEvent event, String fromId) {
Transaction tx = event.getEventBody();
if (null == tx) {
return;
}
boolean isMine = false;
try {
isMine = ledgerService.checkTxIsMine(tx);
} catch (NulsException e) {
Log.error(e);
}
ValidateResult result = tx.verify();
if (result.isFailed()) {
if (result.getErrorCode() == ErrorCode.ORPHAN_TX) {
orphanTxCacheManager.putTx(tx);
eventBroadcaster.broadcastHashAndCacheAysn(event, false, fromId);
return;
}
if (result.getLevel() == SeverityLevelEnum.NORMAL_FOUL) {
Log.info("-----------------------------------------------newTxHandler remove node:" + fromId);
networkService.removeNode(fromId);
} else if (result.getLevel() == SeverityLevelEnum.FLAGRANT_FOUL) {
networkService.blackNode(fromId, NodePo.BLACK);
}
return;
}
try {
if (isMine) {
ledgerService.approvalTx(tx);
}
cacheManager.putTx(tx);
eventBroadcaster.broadcastHashAndCacheAysn(event, false, fromId);
} catch (Exception e) {
Log.error(e);
}
}
use of io.nuls.core.validate.ValidateResult in project nuls by nuls-io.
the class BlockEventHandler method onEvent.
@Override
public void onEvent(BlockEvent event, String fromId) {
Block block = event.getEventBody();
if (null == block) {
Log.warn("recieved a null blockEvent form " + fromId);
return;
}
ValidateResult result = block.verify();
if (result.isFailed() && result.getErrorCode() != ErrorCode.ORPHAN_TX) {
if (result.getLevel() == SeverityLevelEnum.FLAGRANT_FOUL) {
networkService.blackNode(fromId, NodePo.YELLOW);
}
Log.warn("recieved a wrong blockEvent:{},form:{}", result.getMessage(), fromId);
return;
}
if (BlockBatchDownloadUtils.getInstance().downloadedBlock(fromId, block)) {
return;
}
blockCacheManager.addBlock(block, false, fromId);
}
use of io.nuls.core.validate.ValidateResult in project nuls by nuls-io.
the class CoinbaseValidator method validate.
@Override
public ValidateResult validate(Block block) {
if (null == block || block.getHeader() == null || null == block.getTxs() || block.getTxs().isEmpty()) {
return ValidateResult.getFailedResult(ErrorCode.DATA_FIELD_CHECK_ERROR);
}
Transaction tx = block.getTxs().get(0);
if (tx.getType() != TransactionConstant.TX_TYPE_COIN_BASE) {
return ValidateResult.getFailedResult("Coinbase transaction order wrong!");
}
for (int i = 1; i < block.getTxs().size(); i++) {
Transaction transaction = block.getTxs().get(i);
if (transaction.getType() == TransactionConstant.TX_TYPE_COIN_BASE) {
ValidateResult result = ValidateResult.getFailedResult("Coinbase transaction more than one!");
result.setLevel(SeverityLevelEnum.FLAGRANT_FOUL);
return result;
}
}
BlockRoundData blockRound = null;
try {
blockRound = new BlockRoundData(block.getHeader().getExtend());
} catch (NulsException e) {
Log.error(e);
}
if (null == blockRound) {
return ValidateResult.getFailedResult("Cann't get the round data!");
}
return ValidateResult.getSuccessResult();
}
Aggregations