Search in sources :

Example 1 with ValidateResult

use of io.nuls.core.validate.ValidateResult in project nuls by nuls-io.

the class ConsensusMeetingRunner method addOrphanTx.

private void addOrphanTx(List<Transaction> txList, long totalSize, PocMeetingMember self) {
    if ((self.getPackTime() - TimeService.currentTimeMillis()) <= 100) {
        return;
    }
    List<Transaction> orphanTxList = orphanTxCacheManager.getTxList();
    if (null == orphanTxList || orphanTxList.isEmpty()) {
        return;
    }
    txList.sort(TxTimeComparator.getInstance());
    List<NulsDigestData> outHashList = new ArrayList<>();
    for (Transaction tx : orphanTxList) {
        if ((self.getPackTime() - TimeService.currentTimeMillis()) <= 100) {
            break;
        }
        totalSize += tx.size();
        if (totalSize >= PocConsensusConstant.MAX_BLOCK_SIZE) {
            break;
        }
        ValidateResult result = tx.verify();
        if (result.isFailed()) {
            Log.error(result.getMessage());
            continue;
        }
        try {
            ledgerService.approvalTx(tx);
        } catch (Exception e) {
            Log.error(result.getMessage());
            Log.error(e);
            continue;
        }
        confirmingTxCacheManager.putTx(tx);
        txList.add(tx);
        outHashList.add(tx.getHash());
    }
    orphanTxCacheManager.removeTx(outHashList);
}
Also used : CoinBaseTransaction(io.nuls.ledger.entity.tx.CoinBaseTransaction) Transaction(io.nuls.core.chain.entity.Transaction) RedPunishTransaction(io.nuls.consensus.entity.tx.RedPunishTransaction) YellowPunishTransaction(io.nuls.consensus.entity.tx.YellowPunishTransaction) ValidateResult(io.nuls.core.validate.ValidateResult) NulsDigestData(io.nuls.core.chain.entity.NulsDigestData) NulsException(io.nuls.core.exception.NulsException) IOException(java.io.IOException) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException)

Example 2 with ValidateResult

use of io.nuls.core.validate.ValidateResult in project nuls by nuls-io.

the class SmallBlockHandler method onEvent.

@Override
public void onEvent(SmallBlockEvent event, String fromId) {
    ValidateResult result = event.getEventBody().verify();
    if (result.isFailed()) {
        return;
    }
    temporaryCacheManager.cacheSmallBlock(event.getEventBody());
    downloadDataUtils.removeSmallBlock(event.getEventBody().getBlockHash().getDigestHex());
    GetTxGroupRequest request = new GetTxGroupRequest();
    GetTxGroupParam param = new GetTxGroupParam();
    param.setBlockHash(event.getEventBody().getBlockHash());
    for (NulsDigestData hash : event.getEventBody().getTxHashList()) {
        if (!receivedTxCacheManager.txExist(hash)) {
            param.addHash(hash);
        }
    }
    request.setEventBody(param);
    this.eventBroadcaster.sendToNode(request, fromId);
}
Also used : GetTxGroupRequest(io.nuls.consensus.event.GetTxGroupRequest) ValidateResult(io.nuls.core.validate.ValidateResult) GetTxGroupParam(io.nuls.consensus.entity.GetTxGroupParam) NulsDigestData(io.nuls.core.chain.entity.NulsDigestData)

Example 3 with ValidateResult

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

Example 4 with ValidateResult

use of io.nuls.core.validate.ValidateResult in project nuls by nuls-io.

the class HeaderHashValidator method validate.

@Override
public ValidateResult validate(BlockHeader data) {
    ValidateResult result = ValidateResult.getSuccessResult();
    NulsDigestData hash = data.getHash();
    P2PKHScriptSig scriptSig = data.getScriptSig();
    NulsDigestData cfmHash = null;
    try {
        BlockHeader newHeader = new BlockHeader();
        newHeader.parse(data.serialize());
        cfmHash = newHeader.getHash();
    } catch (Exception e) {
        Log.error(e);
    } finally {
        data.setScriptSig(scriptSig);
    }
    if (!cfmHash.getDigestHex().equals(hash.getDigestHex())) {
        result = ValidateResult.getFailedResult(ERROR_MESSAGE);
    }
    return result;
}
Also used : P2PKHScriptSig(io.nuls.core.script.P2PKHScriptSig) ValidateResult(io.nuls.core.validate.ValidateResult) NulsDigestData(io.nuls.core.chain.entity.NulsDigestData) BlockHeader(io.nuls.core.chain.entity.BlockHeader) IOException(java.io.IOException)

Example 5 with ValidateResult

use of io.nuls.core.validate.ValidateResult in project nuls by nuls-io.

the class BlockFieldValidator method validate.

@Override
public ValidateResult validate(Block data) {
    ValidateResult result = ValidateResult.getSuccessResult();
    boolean failed = false;
    do {
        if (data == null) {
            failed = true;
            break;
        }
        if (data.getHeader() == null) {
            failed = true;
            break;
        }
        if (data.getTxs() == null || data.getTxs().isEmpty()) {
            failed = true;
            break;
        }
        if (data.getHeader().getTxCount() == 0 || data.getTxs().size() != data.getHeader().getTxCount()) {
            failed = true;
            break;
        }
    } while (false);
    if (failed) {
        result = ValidateResult.getFailedResult(ERROR_MESSAGE);
    }
    return result;
}
Also used : ValidateResult(io.nuls.core.validate.ValidateResult)

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