Search in sources :

Example 11 with ValidateResult

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();
}
Also used : NodeDownloadingStatus(io.nuls.consensus.entity.NodeDownloadingStatus) ValidateResult(io.nuls.core.validate.ValidateResult) Block(io.nuls.core.chain.entity.Block) ValidateResult(io.nuls.core.validate.ValidateResult) Result(io.nuls.core.chain.entity.Result)

Example 12 with ValidateResult

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");
}
Also used : Account(io.nuls.account.entity.Account) AliasTransaction(io.nuls.account.entity.tx.AliasTransaction) TransactionEvent(io.nuls.ledger.event.TransactionEvent) CoinTransferData(io.nuls.ledger.entity.params.CoinTransferData) Alias(io.nuls.account.entity.Alias) ValidateResult(io.nuls.core.validate.ValidateResult) NulsException(io.nuls.core.exception.NulsException) IOException(java.io.IOException) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) ValidateResult(io.nuls.core.validate.ValidateResult) Result(io.nuls.core.chain.entity.Result)

Example 13 with ValidateResult

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);
    }
}
Also used : Transaction(io.nuls.core.chain.entity.Transaction) NulsException(io.nuls.core.exception.NulsException) ValidateResult(io.nuls.core.validate.ValidateResult) NulsException(io.nuls.core.exception.NulsException)

Example 14 with ValidateResult

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);
}
Also used : ValidateResult(io.nuls.core.validate.ValidateResult) Block(io.nuls.core.chain.entity.Block)

Example 15 with ValidateResult

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();
}
Also used : Transaction(io.nuls.core.chain.entity.Transaction) NulsException(io.nuls.core.exception.NulsException) ValidateResult(io.nuls.core.validate.ValidateResult) BlockRoundData(io.nuls.consensus.entity.block.BlockRoundData)

Aggregations

ValidateResult (io.nuls.core.validate.ValidateResult)18 NulsException (io.nuls.core.exception.NulsException)8 NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)7 Transaction (io.nuls.core.chain.entity.Transaction)6 IOException (java.io.IOException)5 Block (io.nuls.core.chain.entity.Block)4 NulsDigestData (io.nuls.core.chain.entity.NulsDigestData)4 BlockRoundData (io.nuls.consensus.entity.block.BlockRoundData)3 CoinBaseTransaction (io.nuls.ledger.entity.tx.CoinBaseTransaction)3 TransactionEvent (io.nuls.ledger.event.TransactionEvent)3 Agent (io.nuls.consensus.entity.member.Agent)2 RedPunishTransaction (io.nuls.consensus.entity.tx.RedPunishTransaction)2 YellowPunishTransaction (io.nuls.consensus.entity.tx.YellowPunishTransaction)2 BlockHeader (io.nuls.core.chain.entity.BlockHeader)2 Result (io.nuls.core.chain.entity.Result)2 CoinTransferData (io.nuls.ledger.entity.params.CoinTransferData)2 Account (io.nuls.account.entity.Account)1 Alias (io.nuls.account.entity.Alias)1 AliasTransaction (io.nuls.account.entity.tx.AliasTransaction)1 Consensus (io.nuls.consensus.entity.Consensus)1