Search in sources :

Example 6 with AgentPo

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

Example 7 with AgentPo

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

the class RedPunishTxProcessor method onRollback.

@Override
public Result onRollback(RedPunishTransaction tx, Object secondaryData) {
    RedPunishData punishData = tx.getTxData();
    List<AgentPo> agentList = agentStorageService.getList();
    AgentPo agent = null;
    for (AgentPo agentPo : agentList) {
        if (agentPo.getDelHeight() <= 0) {
            continue;
        }
        if (Arrays.equals(agentPo.getAgentAddress(), punishData.getAddress())) {
            agent = agentPo;
            break;
        }
    }
    if (null == agent) {
        return Result.getFailed(PocConsensusErrorCode.AGENT_NOT_EXIST);
    }
    List<DepositPo> depositPoList = depositStorageService.getList();
    List<DepositPo> updatedList = new ArrayList<>();
    for (DepositPo po : depositPoList) {
        po.setDelHeight(-1);
        boolean success = this.depositStorageService.save(po);
        if (!success) {
            for (DepositPo po2 : depositPoList) {
                po2.setDelHeight(tx.getBlockHeight());
                this.depositStorageService.save(po2);
            }
            return Result.getFailed(PocConsensusErrorCode.UPDATE_DEPOSIT_FAILED);
        }
        updatedList.add(po);
    }
    AgentPo agentPo = agent;
    agentPo.setDelHeight(-1L);
    boolean success = agentStorageService.save(agentPo);
    if (!success) {
        for (DepositPo po2 : depositPoList) {
            po2.setDelHeight(tx.getBlockHeight());
            this.depositStorageService.save(po2);
        }
        return Result.getFailed(PocConsensusErrorCode.UPDATE_AGENT_FAILED);
    }
    byte[] key = ArraysTool.concatenate(punishData.getAddress(), new byte[] { PunishType.RED.getCode() }, SerializeUtils.uint64ToByteArray(tx.getBlockHeight()), new byte[] { 0 });
    success = storageService.delete(key);
    if (!success) {
        for (DepositPo po2 : depositPoList) {
            po2.setDelHeight(tx.getBlockHeight());
            this.depositStorageService.save(po2);
        }
        agentPo.setDelHeight(tx.getBlockHeight());
        agentStorageService.save(agentPo);
        throw new NulsRuntimeException(TransactionErrorCode.ROLLBACK_TRANSACTION_FAILED);
    }
    return Result.getSuccess();
}
Also used : DepositPo(io.nuls.consensus.poc.storage.po.DepositPo) RedPunishData(io.nuls.consensus.poc.protocol.entity.RedPunishData) ArrayList(java.util.ArrayList) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) AgentPo(io.nuls.consensus.poc.storage.po.AgentPo)

Example 8 with AgentPo

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

the class AgentCountValidator method validate.

@Override
public ValidateResult validate(CreateAgentTransaction tx) {
    ValidateResult result = ValidateResult.getSuccessResult();
    Agent agent = tx.getTxData();
    List<AgentPo> caList = agentStorageService.getList();
    if (caList != null) {
        Set<String> set = new HashSet<>();
        for (AgentPo ca : caList) {
            if (ca.getHash().equals(tx.getHash())) {
                return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TRANSACTION_REPEATED);
            }
            if (ca.getDelHeight() > 0L) {
                continue;
            }
            set.add(Hex.encode(ca.getAgentAddress()));
            set.add(Hex.encode(ca.getPackingAddress()));
        }
        boolean b = set.add(Hex.encode(agent.getAgentAddress()));
        if (!b) {
            return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.AGENT_EXIST);
        }
        b = set.add(Hex.encode(agent.getPackingAddress()));
        if (!b) {
            return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.AGENT_PACKING_EXIST);
        }
    }
    return result;
}
Also used : Agent(io.nuls.consensus.poc.protocol.entity.Agent) ValidateResult(io.nuls.kernel.validate.ValidateResult) AgentPo(io.nuls.consensus.poc.storage.po.AgentPo) HashSet(java.util.HashSet)

Example 9 with AgentPo

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

the class DepositTxValidator method validate.

@Override
public ValidateResult validate(DepositTransaction tx) throws NulsException {
    if (null == tx || null == tx.getTxData() || null == tx.getTxData().getAgentHash() || null == tx.getTxData().getDeposit() || null == tx.getTxData().getAddress()) {
        return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TX_DATA_VALIDATION_ERROR);
    }
    Deposit deposit = tx.getTxData();
    AgentPo agentPo = agentStorageService.get(deposit.getAgentHash());
    if (null == agentPo || agentPo.getDelHeight() > 0) {
        return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.AGENT_NOT_EXIST);
    }
    List<DepositPo> poList = this.getDepositListByAgent(deposit.getAgentHash());
    if (null != poList && poList.size() >= PocConsensusProtocolConstant.MAX_ACCEPT_NUM_OF_DEPOSIT) {
        return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.DEPOSIT_OVER_COUNT);
    }
    Na limit = PocConsensusProtocolConstant.ENTRUSTER_DEPOSIT_LOWER_LIMIT;
    Na max = PocConsensusProtocolConstant.SUM_OF_DEPOSIT_OF_AGENT_UPPER_LIMIT;
    Na total = Na.ZERO;
    for (DepositPo cd : poList) {
        total = total.add(cd.getDeposit());
    }
    if (limit.isGreaterThan(deposit.getDeposit())) {
        return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.DEPOSIT_NOT_ENOUGH);
    }
    if (max.isLessThan(total.add(deposit.getDeposit()))) {
        return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.DEPOSIT_TOO_MUCH);
    }
    if (!isDepositOk(deposit.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());
    }
    if (!SignatureUtil.containsAddress(tx, deposit.getAddress())) {
        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 : Deposit(io.nuls.consensus.poc.protocol.entity.Deposit) CoinData(io.nuls.kernel.model.CoinData) 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) AgentPo(io.nuls.consensus.poc.storage.po.AgentPo)

Example 10 with AgentPo

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

the class AgentStorageServiceImpl method getList.

@Override
public List<AgentPo> getList() {
    List<Entry<byte[], byte[]>> list = dbService.entryList(ConsensusStorageConstant.DB_NAME_CONSENSUS_AGENT);
    List<AgentPo> resultList = new ArrayList<>();
    if (list == null) {
        return resultList;
    }
    for (Entry<byte[], byte[]> entry : list) {
        AgentPo agentPo = new AgentPo();
        try {
            agentPo.parse(entry.getValue(), 0);
        } catch (NulsException e) {
            Log.error(e);
            throw new NulsRuntimeException(e);
        }
        NulsDigestData hash = new NulsDigestData();
        try {
            hash.parse(entry.getKey(), 0);
        } catch (NulsException e) {
            Log.error(e);
        }
        agentPo.setHash(hash);
        resultList.add(agentPo);
    }
    return resultList;
}
Also used : Entry(io.nuls.db.model.Entry) NulsException(io.nuls.kernel.exception.NulsException) ArrayList(java.util.ArrayList) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) NulsDigestData(io.nuls.kernel.model.NulsDigestData) 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