Search in sources :

Example 6 with ValidateResult

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

the class AgentDepositValidator method validate.

@Override
public ValidateResult validate(RegisterAgentTransaction tx) {
    ValidateResult result = ValidateResult.getSuccessResult();
    Agent agent = tx.getTxData().getExtend();
    if (null == agent) {
        return ValidateResult.getFailedResult(ErrorCode.NULL_PARAMETER);
    }
    if (PocConsensusConstant.AGENT_DEPOSIT_LOWER_LIMIT.isGreaterThan(agent.getDeposit())) {
        return ValidateResult.getFailedResult(ErrorCode.DEPOSIT_NOT_ENOUGH);
    }
    if (!agent.getDeposit().equals(tx.getCoinData().getTotalNa())) {
        return ValidateResult.getFailedResult(SeverityLevelEnum.FLAGRANT_FOUL, ErrorCode.DEPOSIT_ERROR);
    }
    return result;
}
Also used : Agent(io.nuls.consensus.entity.member.Agent) ValidateResult(io.nuls.core.validate.ValidateResult)

Example 7 with ValidateResult

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

the class TransactionResource method forwardTransaction.

@POST
@Produces(MediaType.APPLICATION_JSON)
public RpcResult forwardTransaction(TxForm form) {
    Transaction tx = null;
    try {
        tx = form.getTx();
    } catch (Exception e) {
        Log.error(e);
    }
    if (tx == null) {
        throw new NulsRuntimeException(ErrorCode.NULL_PARAMETER);
    }
    ValidateResult result = tx.verify();
    if (result.isFailed() && ErrorCode.ORPHAN_TX != result.getErrorCode()) {
        return RpcResult.getFailed(ErrorCode.DATA_ERROR);
    }
    TransactionEvent event = new TransactionEvent();
    event.setEventBody(tx);
    List<String> list = eventBroadcaster.broadcastAndCache(event, true);
    return RpcResult.getSuccess().setData(list);
}
Also used : TransactionEvent(io.nuls.ledger.event.TransactionEvent) Transaction(io.nuls.core.chain.entity.Transaction) ValidateResult(io.nuls.core.validate.ValidateResult) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException)

Example 8 with ValidateResult

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

the class BlockMaintenanceThread method checkGenesisBlock.

public void checkGenesisBlock() throws Exception {
    Block genesisBlock = NulsContext.getInstance().getGenesisBlock();
    ValidateResult result = genesisBlock.verify();
    if (result.isFailed()) {
        throw new NulsRuntimeException(ErrorCode.DATA_ERROR, result.getMessage());
    }
    Block localGenesisBlock = this.blockService.getGengsisBlock();
    if (null == localGenesisBlock) {
        for (Transaction tx : genesisBlock.getTxs()) {
            ledgerService.approvalTx(tx);
        }
        this.blockService.saveBlock(genesisBlock);
        return;
    }
    localGenesisBlock.verify();
    String logicHash = genesisBlock.getHeader().getHash().getDigestHex();
    String localHash = localGenesisBlock.getHeader().getHash().getDigestHex();
    if (!logicHash.equals(localHash)) {
        throw new NulsRuntimeException(ErrorCode.DATA_ERROR);
    }
}
Also used : Transaction(io.nuls.core.chain.entity.Transaction) ValidateResult(io.nuls.core.validate.ValidateResult) Block(io.nuls.core.chain.entity.Block) BestCorrectBlock(io.nuls.consensus.entity.block.BestCorrectBlock) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException)

Example 9 with ValidateResult

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

the class ConsensusMeetingRunner method coinBaseTx.

private void coinBaseTx(List<Transaction> txList, PocMeetingMember self) throws NulsException, IOException {
    CoinTransferData data = new CoinTransferData(OperationType.COIN_BASE, this.ledgerService.getTxFee(TransactionConstant.TX_TYPE_COIN_BASE));
    data.setFee(Na.ZERO);
    List<ConsensusReward> rewardList = calcReward(txList, self);
    Na total = Na.ZERO;
    for (ConsensusReward reward : rewardList) {
        Coin coin = new Coin();
        coin.setNa(reward.getReward());
        data.addTo(reward.getAddress(), coin);
        total = total.add(reward.getReward());
    }
    data.setTotalNa(total);
    CoinBaseTransaction tx;
    try {
        tx = new CoinBaseTransaction(data, null);
    } catch (NulsException e) {
        Log.error(e);
        throw new NulsRuntimeException(e);
    }
    tx.setFee(Na.ZERO);
    tx.setHash(NulsDigestData.calcDigestData(tx));
    tx.setScriptSig(accountService.createP2PKHScriptSigFromDigest(tx.getHash(), consensusManager.getConsensusStatusInfo().getAccount(), NulsContext.CACHED_PASSWORD_OF_WALLET).serialize());
    ValidateResult validateResult = tx.verify();
    confirmingTxCacheManager.putTx(tx);
    if (null == validateResult || validateResult.isFailed()) {
        throw new NulsRuntimeException(ErrorCode.CONSENSUS_EXCEPTION);
    }
    try {
        ledgerService.approvalTx(tx);
    } catch (NulsException e) {
        throw new NulsRuntimeException(e);
    }
    txList.add(0, tx);
}
Also used : Coin(io.nuls.ledger.entity.params.Coin) CoinTransferData(io.nuls.ledger.entity.params.CoinTransferData) Na(io.nuls.core.chain.entity.Na) ConsensusReward(io.nuls.consensus.entity.meeting.ConsensusReward) CoinBaseTransaction(io.nuls.ledger.entity.tx.CoinBaseTransaction) NulsException(io.nuls.core.exception.NulsException) ValidateResult(io.nuls.core.validate.ValidateResult) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException)

Example 10 with ValidateResult

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

the class ConsensusMeetingRunner method packing.

private void packing(PocMeetingMember self) throws NulsException, IOException {
    Block bestBlock = context.getBestBlock();
    List<Transaction> txList = txCacheManager.getTxList();
    txList.sort(TxTimeComparator.getInstance());
    BlockData bd = new BlockData();
    bd.setHeight(bestBlock.getHeader().getHeight() + 1);
    bd.setPreHash(bestBlock.getHeader().getHash());
    BlockRoundData roundData = new BlockRoundData();
    roundData.setRoundIndex(consensusManager.getCurrentRound().getIndex());
    roundData.setConsensusMemberCount(consensusManager.getCurrentRound().getMemberCount());
    roundData.setPackingIndexOfRound(self.getIndexOfRound());
    roundData.setRoundStartTime(consensusManager.getCurrentRound().getStartTime());
    bd.setRoundData(roundData);
    List<Integer> outTxList = new ArrayList<>();
    List<NulsDigestData> outHashList = new ArrayList<>();
    List<NulsDigestData> hashList = new ArrayList<>();
    long totalSize = 0L;
    for (int i = 0; i < txList.size(); i++) {
        if ((self.getPackTime() - TimeService.currentTimeMillis()) <= 100) {
            break;
        }
        Transaction tx = txList.get(i);
        totalSize += tx.size();
        if (totalSize >= PocConsensusConstant.MAX_BLOCK_SIZE) {
            break;
        }
        outHashList.add(tx.getHash());
        ValidateResult result = tx.verify();
        if (result.isFailed()) {
            Log.error(result.getMessage());
            outTxList.add(i);
            continue;
        }
        try {
            ledgerService.approvalTx(tx);
        } catch (Exception e) {
            Log.error(e);
            outTxList.add(i);
            continue;
        }
        confirmingTxCacheManager.putTx(tx);
    }
    txCacheManager.removeTx(hashList);
    for (int i = outTxList.size() - 1; i >= 0; i--) {
        txList.remove(i);
    }
    txCacheManager.removeTx(outHashList);
    if (totalSize < PocConsensusConstant.MAX_BLOCK_SIZE) {
        addOrphanTx(txList, totalSize, self);
    }
    addConsensusTx(bestBlock, txList, self);
    bd.setTxList(txList);
    Log.info("txCount:" + txList.size());
    Block newBlock = ConsensusTool.createBlock(bd, consensusManager.getConsensusStatusInfo().getAccount());
    ValidateResult result = newBlock.verify();
    if (result.isFailed()) {
        Log.warn("packing block error:" + result.getMessage());
        for (Transaction tx : newBlock.getTxs()) {
            if (tx.getType() == TransactionConstant.TX_TYPE_COIN_BASE) {
                continue;
            }
            ledgerService.rollbackTx(tx);
        }
        return;
    }
    confirmingTxCacheManager.putTx(newBlock.getTxs().get(0));
    blockManager.addBlock(newBlock, false, null);
    BlockHeaderEvent event = new BlockHeaderEvent();
    event.setEventBody(newBlock.getHeader());
    eventBroadcaster.broadcastAndCache(event, false);
    PackedBlockNotice notice = new PackedBlockNotice();
    notice.setEventBody(newBlock.getHeader());
    eventBroadcaster.publishToLocal(notice);
}
Also used : ValidateResult(io.nuls.core.validate.ValidateResult) NulsException(io.nuls.core.exception.NulsException) IOException(java.io.IOException) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) BlockHeaderEvent(io.nuls.consensus.event.BlockHeaderEvent) 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) BlockRoundData(io.nuls.consensus.entity.block.BlockRoundData) Block(io.nuls.core.chain.entity.Block) NulsDigestData(io.nuls.core.chain.entity.NulsDigestData) PackedBlockNotice(io.nuls.consensus.event.notice.PackedBlockNotice) BlockData(io.nuls.consensus.entity.block.BlockData)

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