Search in sources :

Example 6 with DepositPo

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

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

the class CancelDepositTxValidator method validate.

@Override
public ValidateResult validate(CancelDepositTransaction data) throws NulsException {
    DepositPo depositPo = depositStorageService.get(data.getTxData().getJoinTxHash());
    if (null == depositPo || depositPo.getDelHeight() > 0) {
        return ValidateResult.getFailedResult(this.getClass().getName(), KernelErrorCode.DATA_NOT_FOUND);
    }
    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, depositPo.getAddress())) {
        ValidateResult result = ValidateResult.getFailedResult(this.getClass().getName(), KernelErrorCode.SIGNATURE_ERROR);
        result.setLevel(SeverityLevelEnum.FLAGRANT_FOUL);
        return result;
    }
    return ValidateResult.getSuccessResult();
}
Also used : DepositPo(io.nuls.consensus.poc.storage.po.DepositPo) NulsException(io.nuls.kernel.exception.NulsException) ValidateResult(io.nuls.kernel.validate.ValidateResult) TransactionSignature(io.nuls.kernel.script.TransactionSignature)

Example 8 with DepositPo

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

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

the class PoConvertUtil method depositToPo.

public static DepositPo depositToPo(Deposit deposit) {
    DepositPo po = new DepositPo();
    po.setTxHash(deposit.getTxHash());
    po.setAddress(deposit.getAddress());
    po.setAgentHash(deposit.getAgentHash());
    po.setBlockHeight(deposit.getBlockHeight());
    po.setDelHeight(deposit.getDelHeight());
    po.setDeposit(deposit.getDeposit());
    po.setTime(deposit.getTime());
    return po;
}
Also used : DepositPo(io.nuls.consensus.poc.storage.po.DepositPo)

Example 10 with DepositPo

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

the class DepositStorageServiceImpl method get.

@Override
public DepositPo get(NulsDigestData hash) {
    if (hash == null) {
        return null;
    }
    byte[] body = null;
    try {
        body = dbService.get(ConsensusStorageConstant.DB_NAME_CONSENSUS_DEPOSIT, hash.serialize());
    } catch (IOException e) {
        Log.error(e);
    }
    if (body == null) {
        return null;
    }
    DepositPo depositPo = new DepositPo();
    try {
        depositPo.parse(body, 0);
    } catch (NulsException e) {
        Log.error(e);
        throw new NulsRuntimeException(e);
    }
    depositPo.setTxHash(hash);
    return depositPo;
}
Also used : DepositPo(io.nuls.consensus.poc.storage.po.DepositPo) NulsException(io.nuls.kernel.exception.NulsException) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) IOException(java.io.IOException)

Aggregations

DepositPo (io.nuls.consensus.poc.storage.po.DepositPo)17 AgentPo (io.nuls.consensus.poc.storage.po.AgentPo)7 NulsRuntimeException (io.nuls.kernel.exception.NulsRuntimeException)6 NulsException (io.nuls.kernel.exception.NulsException)5 ValidateResult (io.nuls.kernel.validate.ValidateResult)4 ArrayList (java.util.ArrayList)4 Deposit (io.nuls.consensus.poc.protocol.entity.Deposit)3 CancelDepositTransaction (io.nuls.consensus.poc.protocol.tx.CancelDepositTransaction)3 DepositTransaction (io.nuls.consensus.poc.protocol.tx.DepositTransaction)3 NulsDigestData (io.nuls.kernel.model.NulsDigestData)3 TransactionSignature (io.nuls.kernel.script.TransactionSignature)3 RedPunishData (io.nuls.consensus.poc.protocol.entity.RedPunishData)2 BlockHeader (io.nuls.kernel.model.BlockHeader)2 Coin (io.nuls.kernel.model.Coin)2 Na (io.nuls.kernel.model.Na)2 BlockExtendsData (io.nuls.consensus.poc.model.BlockExtendsData)1 RedPunishTransaction (io.nuls.consensus.poc.protocol.tx.RedPunishTransaction)1 StopAgentTransaction (io.nuls.consensus.poc.protocol.tx.StopAgentTransaction)1 DepositComparator (io.nuls.consensus.poc.protocol.util.DepositComparator)1 PunishLogPo (io.nuls.consensus.poc.storage.po.PunishLogPo)1