Search in sources :

Example 1 with AgentPo

use of io.nuls.consensus.poc.storage.po.AgentPo in project nuls by nuls-io.

the class CacheLoader method loadAgents.

public List<Agent> loadAgents() {
    List<Agent> agentList = new ArrayList<>();
    List<AgentPo> poList = this.agentStorageService.getList();
    for (AgentPo po : poList) {
        Agent agent = PoConvertUtil.poToAgent(po);
        agentList.add(agent);
    }
    Collections.sort(agentList, new AgentComparator());
    return agentList;
}
Also used : Agent(io.nuls.consensus.poc.protocol.entity.Agent) ArrayList(java.util.ArrayList) AgentComparator(io.nuls.consensus.poc.protocol.util.AgentComparator) AgentPo(io.nuls.consensus.poc.storage.po.AgentPo)

Example 2 with AgentPo

use of io.nuls.consensus.poc.storage.po.AgentPo 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 3 with AgentPo

use of io.nuls.consensus.poc.storage.po.AgentPo 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 4 with AgentPo

use of io.nuls.consensus.poc.storage.po.AgentPo in project nuls by nuls-io.

the class StopAgentTxProcessor method onRollback.

@Override
public Result onRollback(StopAgentTransaction tx, Object secondaryData) {
    AgentPo agentPo = agentStorageService.get(tx.getTxData().getCreateTxHash());
    if (null == agentPo || agentPo.getDelHeight() < 0) {
        throw new NulsRuntimeException(PocConsensusErrorCode.AGENT_NOT_EXIST);
    }
    agentPo.setDelHeight(-1L);
    List<DepositPo> depositPoList = depositStorageService.getList();
    for (DepositPo depositPo : depositPoList) {
        if (depositPo.getDelHeight() != tx.getBlockHeight()) {
            continue;
        }
        if (!depositPo.getAgentHash().equals(agentPo.getHash())) {
            continue;
        }
        depositPo.setDelHeight(-1L);
        depositStorageService.save(depositPo);
    }
    boolean b = agentStorageService.save(agentPo);
    if (!b) {
        return Result.getFailed(PocConsensusErrorCode.UPDATE_AGENT_FAILED);
    }
    return Result.getSuccess();
}
Also used : DepositPo(io.nuls.consensus.poc.storage.po.DepositPo) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) AgentPo(io.nuls.consensus.poc.storage.po.AgentPo)

Example 5 with AgentPo

use of io.nuls.consensus.poc.storage.po.AgentPo in project nuls by nuls-io.

the class StopAgentTxProcessor method onCommit.

@Override
public Result onCommit(StopAgentTransaction tx, Object secondaryData) {
    BlockHeader header = (BlockHeader) secondaryData;
    if (tx.getTime() < (header.getTime() - 300000L)) {
        return Result.getFailed(PocConsensusErrorCode.LOCK_TIME_NOT_REACHED);
    }
    AgentPo agentPo = agentStorageService.get(tx.getTxData().getCreateTxHash());
    if (null == agentPo || agentPo.getDelHeight() > 0) {
        throw new NulsRuntimeException(PocConsensusErrorCode.AGENT_NOT_EXIST);
    }
    List<DepositPo> depositPoList = depositStorageService.getList();
    for (DepositPo depositPo : depositPoList) {
        if (depositPo.getDelHeight() > -1L) {
            continue;
        }
        if (!depositPo.getAgentHash().equals(agentPo.getHash())) {
            continue;
        }
        depositPo.setDelHeight(tx.getBlockHeight());
        depositStorageService.save(depositPo);
    }
    agentPo.setDelHeight(tx.getBlockHeight());
    tx.getTxData().setAddress(agentPo.getAgentAddress());
    boolean b = agentStorageService.save(agentPo);
    if (!b) {
        return Result.getFailed(PocConsensusErrorCode.UPDATE_AGENT_FAILED);
    }
    return Result.getSuccess();
}
Also used : DepositPo(io.nuls.consensus.poc.storage.po.DepositPo) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) BlockHeader(io.nuls.kernel.model.BlockHeader) AgentPo(io.nuls.consensus.poc.storage.po.AgentPo)

Aggregations

AgentPo (io.nuls.consensus.poc.storage.po.AgentPo)21 NulsDigestData (io.nuls.kernel.model.NulsDigestData)8 DepositPo (io.nuls.consensus.poc.storage.po.DepositPo)7 NulsRuntimeException (io.nuls.kernel.exception.NulsRuntimeException)6 ValidateResult (io.nuls.kernel.validate.ValidateResult)6 BaseTest (io.nuls.consensus.poc.storage.BaseTest)4 NulsException (io.nuls.kernel.exception.NulsException)4 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 Agent (io.nuls.consensus.poc.protocol.entity.Agent)3 RedPunishData (io.nuls.consensus.poc.protocol.entity.RedPunishData)3 RedPunishTransaction (io.nuls.consensus.poc.protocol.tx.RedPunishTransaction)3 StopAgentTransaction (io.nuls.consensus.poc.protocol.tx.StopAgentTransaction)3 BlockHeader (io.nuls.kernel.model.BlockHeader)3 HashSet (java.util.HashSet)3 DepositTransaction (io.nuls.consensus.poc.protocol.tx.DepositTransaction)2 Coin (io.nuls.kernel.model.Coin)2 Na (io.nuls.kernel.model.Na)2 Transaction (io.nuls.kernel.model.Transaction)2 TransactionSignature (io.nuls.kernel.script.TransactionSignature)2