Search in sources :

Example 21 with NulsRuntimeException

use of io.nuls.kernel.exception.NulsRuntimeException 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 22 with NulsRuntimeException

use of io.nuls.kernel.exception.NulsRuntimeException in project nuls by nuls-io.

the class YellowPunishTxProcessor method onCommit.

@Override
public Result onCommit(YellowPunishTransaction tx, Object secondaryData) {
    YellowPunishData punishData = tx.getTxData();
    BlockHeader header = (BlockHeader) secondaryData;
    BlockExtendsData roundData = new BlockExtendsData(header.getExtend());
    List<PunishLogPo> savedList = new ArrayList<>();
    int index = 1;
    for (byte[] address : punishData.getAddressList()) {
        PunishLogPo po = new PunishLogPo();
        po.setAddress(address);
        po.setHeight(tx.getBlockHeight());
        po.setRoundIndex(roundData.getRoundIndex());
        po.setTime(tx.getTime());
        po.setIndex(index++);
        po.setType(PunishType.YELLOW.getCode());
        boolean result = punishLogStorageService.save(po);
        if (!result) {
            for (PunishLogPo punishLogPo : savedList) {
                punishLogStorageService.delete(getPoKey(punishLogPo.getAddress(), PunishType.YELLOW.getCode(), punishLogPo.getHeight(), punishLogPo.getIndex()));
            }
            throw new NulsRuntimeException(TransactionErrorCode.ROLLBACK_TRANSACTION_FAILED);
        } else {
            savedList.add(po);
        }
    }
    return Result.getSuccess();
}
Also used : BlockExtendsData(io.nuls.consensus.poc.model.BlockExtendsData) YellowPunishData(io.nuls.consensus.poc.protocol.entity.YellowPunishData) ArrayList(java.util.ArrayList) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) BlockHeader(io.nuls.kernel.model.BlockHeader) PunishLogPo(io.nuls.consensus.poc.storage.po.PunishLogPo)

Example 23 with NulsRuntimeException

use of io.nuls.kernel.exception.NulsRuntimeException in project nuls by nuls-io.

the class YellowPunishTxProcessor method onRollback.

@Override
public Result onRollback(YellowPunishTransaction tx, Object secondaryData) {
    YellowPunishData punishData = tx.getTxData();
    List<byte[]> deletedList = new ArrayList<>();
    int deleteIndex = 1;
    for (byte[] address : punishData.getAddressList()) {
        boolean result = punishLogStorageService.delete(this.getPoKey(address, PunishType.YELLOW.getCode(), tx.getBlockHeight(), deleteIndex++));
        if (!result) {
            BlockHeader header = (BlockHeader) secondaryData;
            BlockExtendsData roundData = new BlockExtendsData(header.getExtend());
            int index = 1;
            for (byte[] bytes : deletedList) {
                PunishLogPo po = new PunishLogPo();
                po.setAddress(bytes);
                po.setHeight(tx.getBlockHeight());
                po.setRoundIndex(roundData.getRoundIndex());
                po.setTime(tx.getTime());
                po.setIndex(index++);
                po.setType(PunishType.YELLOW.getCode());
                punishLogStorageService.save(po);
            }
            throw new NulsRuntimeException(TransactionErrorCode.ROLLBACK_TRANSACTION_FAILED);
        } else {
            deletedList.add(address);
        }
    }
    return Result.getSuccess();
}
Also used : BlockExtendsData(io.nuls.consensus.poc.model.BlockExtendsData) YellowPunishData(io.nuls.consensus.poc.protocol.entity.YellowPunishData) ArrayList(java.util.ArrayList) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) BlockHeader(io.nuls.kernel.model.BlockHeader) PunishLogPo(io.nuls.consensus.poc.storage.po.PunishLogPo)

Example 24 with NulsRuntimeException

use of io.nuls.kernel.exception.NulsRuntimeException in project nuls by nuls-io.

the class BaseNulsData method serialize.

@Override
public final byte[] serialize() throws IOException {
    ByteArrayOutputStream bos = null;
    try {
        int size = size();
        bos = new UnsafeByteArrayOutputStream(size);
        NulsOutputStreamBuffer buffer = new NulsOutputStreamBuffer(bos);
        if (size == 0) {
            bos.write(NulsConstant.PLACE_HOLDER);
        } else {
            serializeToStream(buffer);
        }
        byte[] bytes = bos.toByteArray();
        if (bytes.length != this.size()) {
            throw new NulsRuntimeException(KernelErrorCode.SERIALIZE_ERROR);
        }
        return bytes;
    } finally {
        if (bos != null) {
            try {
                bos.close();
            } catch (IOException e) {
                throw e;
            }
        }
    }
}
Also used : NulsOutputStreamBuffer(io.nuls.kernel.utils.NulsOutputStreamBuffer) UnsafeByteArrayOutputStream(io.nuls.core.tools.crypto.UnsafeByteArrayOutputStream) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) UnsafeByteArrayOutputStream(io.nuls.core.tools.crypto.UnsafeByteArrayOutputStream) IOException(java.io.IOException)

Example 25 with NulsRuntimeException

use of io.nuls.kernel.exception.NulsRuntimeException in project nuls by nuls-io.

the class ModuleService method startModules.

public void startModules(Map<String, String> bootstrapClasses) {
    moduleManager.setModulesCfg(bootstrapClasses);
    List<String> keyList = new ArrayList<>(bootstrapClasses.keySet());
    for (String key : keyList) {
        try {
            startModule(key, bootstrapClasses.get(key));
        } catch (Exception e) {
            throw new NulsRuntimeException(e);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException)

Aggregations

NulsRuntimeException (io.nuls.kernel.exception.NulsRuntimeException)68 IOException (java.io.IOException)35 NulsException (io.nuls.kernel.exception.NulsException)26 ArrayList (java.util.ArrayList)21 CoinDataResult (io.nuls.account.ledger.model.CoinDataResult)10 Result (io.nuls.kernel.model.Result)9 Account (io.nuls.account.model.Account)8 MultiSigAccount (io.nuls.account.model.MultiSigAccount)8 Entry (io.nuls.db.model.Entry)8 Agent (io.nuls.consensus.poc.protocol.entity.Agent)7 VarInt (io.nuls.kernel.utils.VarInt)7 CreateAgentTransaction (io.nuls.consensus.poc.protocol.tx.CreateAgentTransaction)6 ValidateResult (io.nuls.kernel.validate.ValidateResult)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 Deposit (io.nuls.consensus.poc.protocol.entity.Deposit)5 DepositTransaction (io.nuls.consensus.poc.protocol.tx.DepositTransaction)5 DepositPo (io.nuls.consensus.poc.storage.po.DepositPo)5 PunishLogPo (io.nuls.consensus.poc.storage.po.PunishLogPo)5 TransferTransaction (io.nuls.protocol.model.tx.TransferTransaction)5 StopAgent (io.nuls.consensus.poc.protocol.entity.StopAgent)4