Search in sources :

Example 6 with ValidateResult

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

the class CreateAgentTxProcessor method conflictDetect.

@Override
public ValidateResult conflictDetect(List<Transaction> txList) {
    // 冲突检测,检测委托人和打包人是否重复
    if (null == txList || txList.isEmpty()) {
        return ValidateResult.getSuccessResult();
    }
    Set<String> addressSet = new HashSet<>();
    List<Agent> agentList = PocConsensusContext.getChainManager().getMasterChain().getChain().getAgentList();
    for (Agent agent : agentList) {
        if (agent.getDelHeight() > 0) {
            continue;
        }
        if (agent.getBlockHeight() == txList.get(0).getBlockHeight()) {
            continue;
        }
        addressSet.add(agent.getAgentAddressStr());
        addressSet.add(agent.getPackingAddressStr());
    }
    for (Transaction transaction : txList) {
        if (transaction.getType() == ConsensusConstant.TX_TYPE_REGISTER_AGENT) {
            CreateAgentTransaction createAgentTransaction = (CreateAgentTransaction) transaction;
            Agent agent = createAgentTransaction.getTxData();
            if (!addressSet.add(agent.getPackingAddressStr()) || !addressSet.add(agent.getAgentAddressStr())) {
                return (ValidateResult) ValidateResult.getFailedResult(getClass().getName(), PocConsensusErrorCode.AGENT_EXIST).setData(transaction);
            }
        } else if (transaction.getType() == ConsensusConstant.TX_TYPE_RED_PUNISH) {
            RedPunishTransaction redPunishTransaction = (RedPunishTransaction) transaction;
            RedPunishData redPunishData = redPunishTransaction.getTxData();
            String address = AddressTool.getStringAddressByBytes(redPunishData.getAddress());
            if (!addressSet.add(address)) {
                return (ValidateResult) ValidateResult.getFailedResult(getClass().getName(), PocConsensusErrorCode.LACK_OF_CREDIT).setData(transaction);
            }
        }
    }
    return ValidateResult.getSuccessResult();
}
Also used : Agent(io.nuls.consensus.poc.protocol.entity.Agent) CreateAgentTransaction(io.nuls.consensus.poc.protocol.tx.CreateAgentTransaction) RedPunishTransaction(io.nuls.consensus.poc.protocol.tx.RedPunishTransaction) Transaction(io.nuls.kernel.model.Transaction) RedPunishTransaction(io.nuls.consensus.poc.protocol.tx.RedPunishTransaction) RedPunishData(io.nuls.consensus.poc.protocol.entity.RedPunishData) ValidateResult(io.nuls.kernel.validate.ValidateResult) CreateAgentTransaction(io.nuls.consensus.poc.protocol.tx.CreateAgentTransaction) HashSet(java.util.HashSet)

Example 7 with ValidateResult

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

the class CreateAgentTxValidator method validate.

@Override
public ValidateResult validate(CreateAgentTransaction tx) {
    Agent agent = tx.getTxData();
    if (null == agent) {
        return ValidateResult.getFailedResult(getClass().getName(), PocConsensusErrorCode.AGENT_NOT_EXIST);
    }
    if (!AddressTool.validNormalAddress(agent.getPackingAddress())) {
        return ValidateResult.getFailedResult(getClass().getName(), AccountErrorCode.ADDRESS_ERROR);
    }
    if (Arrays.equals(agent.getAgentAddress(), agent.getPackingAddress())) {
        return ValidateResult.getFailedResult(getClass().getName(), PocConsensusErrorCode.AGENTADDR_AND_PACKING_SAME);
    }
    if (Arrays.equals(agent.getRewardAddress(), agent.getPackingAddress())) {
        return ValidateResult.getFailedResult(getClass().getName(), PocConsensusErrorCode.REWARDADDR_PACKING_SAME);
    }
    if (tx.getTime() <= 0) {
        return ValidateResult.getFailedResult(getClass().getName(), KernelErrorCode.DATA_ERROR);
    }
    double commissionRate = agent.getCommissionRate();
    if (commissionRate < PocConsensusProtocolConstant.MIN_COMMISSION_RATE || commissionRate > PocConsensusProtocolConstant.MAX_COMMISSION_RATE) {
        return ValidateResult.getFailedResult(this.getClass().getSimpleName(), PocConsensusErrorCode.COMMISSION_RATE_OUT_OF_RANGE);
    }
    if (PocConsensusProtocolConstant.AGENT_DEPOSIT_LOWER_LIMIT.isGreaterThan(agent.getDeposit())) {
        return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.DEPOSIT_NOT_ENOUGH);
    }
    if (PocConsensusProtocolConstant.AGENT_DEPOSIT_UPPER_LIMIT.isLessThan(agent.getDeposit())) {
        return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.DEPOSIT_TOO_MUCH);
    }
    if (!isDepositOk(agent.getDeposit(), tx.getCoinData())) {
        return ValidateResult.getFailedResult(this.getClass().getName(), SeverityLevelEnum.FLAGRANT_FOUL, PocConsensusErrorCode.DEPOSIT_ERROR);
    }
    TransactionSignature sig = new TransactionSignature();
    try {
        sig.parse(tx.getTransactionSignature(), 0);
    } catch (NulsException e) {
        Log.error(e);
        return ValidateResult.getFailedResult(this.getClass().getName(), e.getErrorCode());
    }
    try {
        if (!SignatureUtil.containsAddress(tx, agent.getAgentAddress())) {
            ValidateResult result = ValidateResult.getFailedResult(this.getClass().getName(), KernelErrorCode.SIGNATURE_ERROR);
            result.setLevel(SeverityLevelEnum.FLAGRANT_FOUL);
            return result;
        }
    } catch (NulsException e) {
        ValidateResult result = ValidateResult.getFailedResult(this.getClass().getName(), KernelErrorCode.SIGNATURE_ERROR);
        result.setLevel(SeverityLevelEnum.FLAGRANT_FOUL);
        return result;
    }
    CoinData coinData = tx.getCoinData();
    Set<String> addressSet = new HashSet<>();
    int lockCount = 0;
    for (Coin coin : coinData.getTo()) {
        if (coin.getLockTime() == PocConsensusConstant.CONSENSUS_LOCK_TIME) {
            lockCount++;
        }
        // addressSet.add(AddressTool.getStringAddressByBytes(coin.()));
        addressSet.add(AddressTool.getStringAddressByBytes(coin.getAddress()));
    }
    if (lockCount > 1) {
        return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
    }
    if (addressSet.size() > 1) {
        return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
    }
    return ValidateResult.getSuccessResult();
}
Also used : Agent(io.nuls.consensus.poc.protocol.entity.Agent) Coin(io.nuls.kernel.model.Coin) NulsException(io.nuls.kernel.exception.NulsException) CoinData(io.nuls.kernel.model.CoinData) ValidateResult(io.nuls.kernel.validate.ValidateResult) TransactionSignature(io.nuls.kernel.script.TransactionSignature) HashSet(java.util.HashSet)

Example 8 with ValidateResult

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

the class StopAgentTxValidator method validate.

@Override
public ValidateResult validate(StopAgentTransaction data) throws NulsException {
    AgentPo agentPo = agentStorageService.get(data.getTxData().getCreateTxHash());
    if (null == agentPo || agentPo.getDelHeight() > 0) {
        return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.AGENT_NOT_EXIST);
    }
    TransactionSignature sig = new TransactionSignature();
    try {
        sig.parse(data.getTransactionSignature(), 0);
    } catch (NulsException e) {
        Log.error(e);
        return ValidateResult.getFailedResult(this.getClass().getName(), e.getErrorCode());
    }
    if (!SignatureUtil.containsAddress(data, agentPo.getAgentAddress())) {
        ValidateResult result = ValidateResult.getFailedResult(this.getClass().getName(), KernelErrorCode.SIGNATURE_ERROR);
        result.setLevel(SeverityLevelEnum.FLAGRANT_FOUL);
        return result;
    }
    if (data.getCoinData().getTo() == null || data.getCoinData().getTo().isEmpty()) {
        return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
    }
    List<DepositPo> allDepositList = depositStorageService.getList();
    Map<NulsDigestData, DepositPo> depositMap = new HashMap<>();
    Na totalNa = agentPo.getDeposit();
    DepositPo ownDeposit = new DepositPo();
    ownDeposit.setDeposit(agentPo.getDeposit());
    ownDeposit.setAddress(agentPo.getAgentAddress());
    depositMap.put(data.getTxData().getCreateTxHash(), ownDeposit);
    for (DepositPo deposit : allDepositList) {
        if (deposit.getDelHeight() > -1L && (data.getBlockHeight() == -1L || deposit.getDelHeight() < data.getBlockHeight())) {
            continue;
        }
        if (!deposit.getAgentHash().equals(agentPo.getHash())) {
            continue;
        }
        depositMap.put(deposit.getTxHash(), deposit);
        totalNa = totalNa.add(deposit.getDeposit());
    }
    Na fromTotal = Na.ZERO;
    Map<String, Na> verifyToMap = new HashMap<>();
    for (Coin coin : data.getCoinData().getFrom()) {
        if (coin.getLockTime() != -1L) {
            return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
        }
        NulsDigestData txHash = new NulsDigestData();
        txHash.parse(coin.getOwner(), 0);
        DepositPo deposit = depositMap.remove(txHash);
        if (deposit == null) {
            return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
        }
        if (deposit.getAgentHash() == null && !coin.getNa().equals(agentPo.getDeposit())) {
            return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
        } else if (!deposit.getDeposit().equals(coin.getNa())) {
            return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
        }
        fromTotal = fromTotal.add(coin.getNa());
        if (deposit.getAgentHash() == null) {
            continue;
        }
        String address = AddressTool.getStringAddressByBytes(deposit.getAddress());
        Na na = verifyToMap.get(address);
        if (null == na) {
            na = deposit.getDeposit();
        } else {
            na = na.add(deposit.getDeposit());
        }
        verifyToMap.put(address, na);
    }
    if (!depositMap.isEmpty()) {
        return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
    }
    if (!totalNa.equals(fromTotal)) {
        return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
    }
    Na ownToCoin = ownDeposit.getDeposit().subtract(data.getFee());
    long ownLockTime = 0L;
    for (Coin coin : data.getCoinData().getTo()) {
        // String address = AddressTool.getStringAddressByBytes(coin.());
        String address = AddressTool.getStringAddressByBytes(coin.getAddress());
        Na na = verifyToMap.get(address);
        if (null != na && na.equals(coin.getNa())) {
            verifyToMap.remove(address);
            continue;
        }
        if (ownToCoin != null && Arrays.equals(coin.getAddress(), ownDeposit.getAddress()) && coin.getNa().equals(ownToCoin)) {
            ownToCoin = null;
            ownLockTime = coin.getLockTime();
            continue;
        } else {
            return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
        }
    }
    if (ownLockTime < (data.getTime() + PocConsensusConstant.STOP_AGENT_LOCK_TIME)) {
        return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.LOCK_TIME_NOT_REACHED);
    } else if (data.getBlockHeight() <= 0 && ownLockTime < (TimeService.currentTimeMillis() + PocConsensusConstant.STOP_AGENT_LOCK_TIME - 300000L)) {
        return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.LOCK_TIME_NOT_REACHED);
    }
    if (!verifyToMap.isEmpty()) {
        return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
    }
    return ValidateResult.getSuccessResult();
}
Also used : ValidateResult(io.nuls.kernel.validate.ValidateResult) TransactionSignature(io.nuls.kernel.script.TransactionSignature) Coin(io.nuls.kernel.model.Coin) DepositPo(io.nuls.consensus.poc.storage.po.DepositPo) Na(io.nuls.kernel.model.Na) NulsException(io.nuls.kernel.exception.NulsException) NulsDigestData(io.nuls.kernel.model.NulsDigestData) AgentPo(io.nuls.consensus.poc.storage.po.AgentPo)

Example 9 with ValidateResult

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

the class DepositTxProcessor method conflictDetect.

/**
 * 冲突检测,检测如果传入的交易列表中有相冲突的交易,则返回失败,写明失败原因及所有的应该舍弃的交易列表
 * 本方法不检查双花冲突,双花由账本接口实现
 * <p>
 * Conflict detection, which detects conflicting transactions in the incoming transaction list, returns failure,
 * indicating the cause of failure and all the list of trades that should be discarded.
 * This method does not check the double flower conflict, the double flower is realized by the accounting interface.
 *
 * @param txList 需要检查的交易列表/A list of transactions to be checked.
 * @return 操作结果:成功则返回successResult,失败时,data中返回丢弃列表,msg中返回冲突原因
 * Operation result: success returns successResult. When failure, data returns the discard list, and MSG returns the cause of conflict.
 */
@Override
public ValidateResult conflictDetect(List<Transaction> txList) {
    if (null == txList || txList.isEmpty()) {
        return ValidateResult.getSuccessResult();
    }
    Set<NulsDigestData> outAgentHash = new HashSet<>();
    Map<NulsDigestData, Na> naMap = new HashMap<>();
    List<DepositTransaction> dTxList = new ArrayList<>();
    for (Transaction transaction : txList) {
        switch(transaction.getType()) {
            case ConsensusConstant.TX_TYPE_STOP_AGENT:
                StopAgentTransaction stopAgentTransaction = (StopAgentTransaction) transaction;
                outAgentHash.add(stopAgentTransaction.getTxData().getCreateTxHash());
                break;
            case ConsensusConstant.TX_TYPE_JOIN_CONSENSUS:
                DepositTransaction depositTransaction = (DepositTransaction) transaction;
                Na na = naMap.get(depositTransaction.getTxData().getAgentHash());
                if (null == na) {
                    na = getAgentTotalDeposit(depositTransaction.getTxData().getAgentHash());
                }
                if (na == null) {
                    na = depositTransaction.getTxData().getDeposit();
                } else {
                    na = na.add(depositTransaction.getTxData().getDeposit());
                }
                if (na.isGreaterThan(PocConsensusProtocolConstant.SUM_OF_DEPOSIT_OF_AGENT_UPPER_LIMIT)) {
                    ValidateResult validateResult = ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.DEPOSIT_TOO_MUCH);
                    validateResult.setData(transaction);
                    return validateResult;
                } else {
                    naMap.put(depositTransaction.getTxData().getAgentHash(), na);
                }
                dTxList.add(depositTransaction);
                break;
            case ConsensusConstant.TX_TYPE_RED_PUNISH:
                RedPunishTransaction redPunishTransaction = (RedPunishTransaction) transaction;
                RedPunishData redPunishData = redPunishTransaction.getTxData();
                AgentPo agent = this.getAgentByAddress(redPunishData.getAddress());
                if (null != agent) {
                    outAgentHash.add(agent.getHash());
                }
                break;
            default:
                continue;
        }
    }
    if (dTxList.isEmpty() || outAgentHash.isEmpty()) {
        return ValidateResult.getSuccessResult();
    }
    for (DepositTransaction depositTransaction : dTxList) {
        if (outAgentHash.contains(depositTransaction.getTxData().getAgentHash())) {
            ValidateResult validateResult = ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.AGENT_STOPPED);
            validateResult.setData(depositTransaction);
            return validateResult;
        }
    }
    return ValidateResult.getSuccessResult();
}
Also used : DepositTransaction(io.nuls.consensus.poc.protocol.tx.DepositTransaction) RedPunishTransaction(io.nuls.consensus.poc.protocol.tx.RedPunishTransaction) ValidateResult(io.nuls.kernel.validate.ValidateResult) StopAgentTransaction(io.nuls.consensus.poc.protocol.tx.StopAgentTransaction) DepositTransaction(io.nuls.consensus.poc.protocol.tx.DepositTransaction) RedPunishTransaction(io.nuls.consensus.poc.protocol.tx.RedPunishTransaction) RedPunishData(io.nuls.consensus.poc.protocol.entity.RedPunishData) StopAgentTransaction(io.nuls.consensus.poc.protocol.tx.StopAgentTransaction) AgentPo(io.nuls.consensus.poc.storage.po.AgentPo)

Example 10 with ValidateResult

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

the class StopAgentTxProcessor method conflictDetect.

@Override
public ValidateResult conflictDetect(List<Transaction> txList) {
    if (txList == null || txList.isEmpty()) {
        return ValidateResult.getSuccessResult();
    }
    Set<NulsDigestData> hashSet = new HashSet<>();
    Set<String> addressSet = new HashSet<>();
    for (Transaction tx : txList) {
        if (tx.getType() == ConsensusConstant.TX_TYPE_RED_PUNISH) {
            RedPunishTransaction transaction = (RedPunishTransaction) tx;
            addressSet.add(AddressTool.getStringAddressByBytes(transaction.getTxData().getAddress()));
        }
    }
    for (Transaction tx : txList) {
        if (tx.getType() == ConsensusConstant.TX_TYPE_STOP_AGENT) {
            StopAgentTransaction transaction = (StopAgentTransaction) tx;
            if (!hashSet.add(transaction.getTxData().getCreateTxHash())) {
                ValidateResult result = ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TRANSACTION_REPEATED);
                result.setData(transaction);
                return result;
            }
            if (transaction.getTxData().getAddress() == null) {
                CreateAgentTransaction agentTransaction = (CreateAgentTransaction) ledgerService.getTx(transaction.getTxData().getCreateTxHash());
                if (null == agentTransaction) {
                    ValidateResult result = ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.AGENT_NOT_EXIST);
                    result.setData(transaction);
                    return result;
                }
                transaction.getTxData().setAddress(agentTransaction.getTxData().getAgentAddress());
            }
            AgentPo po = agentStorageService.get(transaction.getTxData().getCreateTxHash());
            if (null == po || po.getDelHeight() > 0) {
                ValidateResult result = ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.AGENT_STOPPED);
                result.setData(transaction);
                return result;
            }
            if (addressSet.contains(AddressTool.getStringAddressByBytes(transaction.getTxData().getAddress()))) {
                ValidateResult result = ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.AGENT_STOPPED);
                result.setData(transaction);
                return result;
            }
        }
    }
    return ValidateResult.getSuccessResult();
}
Also used : StopAgentTransaction(io.nuls.consensus.poc.protocol.tx.StopAgentTransaction) CreateAgentTransaction(io.nuls.consensus.poc.protocol.tx.CreateAgentTransaction) RedPunishTransaction(io.nuls.consensus.poc.protocol.tx.RedPunishTransaction) Transaction(io.nuls.kernel.model.Transaction) RedPunishTransaction(io.nuls.consensus.poc.protocol.tx.RedPunishTransaction) ValidateResult(io.nuls.kernel.validate.ValidateResult) NulsDigestData(io.nuls.kernel.model.NulsDigestData) CreateAgentTransaction(io.nuls.consensus.poc.protocol.tx.CreateAgentTransaction) StopAgentTransaction(io.nuls.consensus.poc.protocol.tx.StopAgentTransaction) HashSet(java.util.HashSet) AgentPo(io.nuls.consensus.poc.storage.po.AgentPo)

Aggregations

ValidateResult (io.nuls.kernel.validate.ValidateResult)31 NulsException (io.nuls.kernel.exception.NulsException)12 IOException (java.io.IOException)8 Agent (io.nuls.consensus.poc.protocol.entity.Agent)6 RedPunishTransaction (io.nuls.consensus.poc.protocol.tx.RedPunishTransaction)6 Coin (io.nuls.kernel.model.Coin)6 Transaction (io.nuls.kernel.model.Transaction)6 RedPunishData (io.nuls.consensus.poc.protocol.entity.RedPunishData)5 AgentPo (io.nuls.consensus.poc.storage.po.AgentPo)5 TransactionSignature (io.nuls.kernel.script.TransactionSignature)5 HashSet (java.util.HashSet)5 CreateAgentTransaction (io.nuls.consensus.poc.protocol.tx.CreateAgentTransaction)4 CoinData (io.nuls.kernel.model.CoinData)4 SmallBlock (io.nuls.protocol.model.SmallBlock)4 ArrayList (java.util.ArrayList)4 BaseTest (io.nuls.consensus.poc.BaseTest)3 DepositPo (io.nuls.consensus.poc.storage.po.DepositPo)3 ContractResult (io.nuls.contract.dto.ContractResult)3 NulsRuntimeException (io.nuls.kernel.exception.NulsRuntimeException)3 CoinBaseTransaction (io.nuls.protocol.model.tx.CoinBaseTransaction)3