Search in sources :

Example 86 with NulsException

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

the class UtxoResource method getAllUtxoByAddress.

private List<Coin> getAllUtxoByAddress(String address) {
    List<Coin> coinList = new ArrayList<>();
    byte[] addressBytes = AddressTool.getAddress(address);
    List<Entry<byte[], byte[]>> coinBytesList = utxoLedgerUtxoStorageService.getAllUtxoEntryBytes();
    Coin coin;
    for (Entry<byte[], byte[]> coinEntryBytes : coinBytesList) {
        coin = new Coin();
        try {
            coin.parse(coinEntryBytes.getValue(), 0);
        } catch (NulsException e) {
            Log.info("parse coin form db error");
            continue;
        }
        // if (Arrays.equals(coin.(), addressBytes))
        if (Arrays.equals(coin.getAddress(), addressBytes)) {
            coin.setOwner(coinEntryBytes.getKey());
            coinList.add(coin);
        }
    }
    Collections.sort(coinList, CoinComparator.getInstance());
    return coinList;
}
Also used : Coin(io.nuls.kernel.model.Coin) Entry(io.nuls.db.model.Entry) NulsException(io.nuls.kernel.exception.NulsException)

Example 87 with NulsException

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

the class UtxoLedgerServiceImpl method saveTx.

@Override
public Result saveTx(Transaction tx) throws NulsException {
    if (tx == null) {
        return Result.getFailed(LedgerErrorCode.NULL_PARAMETER);
    }
    try {
        // 保存交易
        Result result = utxoLedgerTransactionStorageService.saveTx(tx);
        if (result.isFailed()) {
            Result rollbackResult = rollbackTx(tx);
            if (rollbackResult.isFailed()) {
                throw new NulsException(rollbackResult.getErrorCode());
            }
        }
        // 保存CoinData
        result = saveCoinData(tx);
        if (result.isFailed()) {
            Result rollbackResult = rollbackCoinData(tx);
            if (rollbackResult.isFailed()) {
                throw new NulsException(rollbackResult.getErrorCode());
            }
        }
        return result;
    } catch (IOException e) {
        Log.error(e);
        Result rollbackResult = rollbackTx(tx);
        if (rollbackResult.isFailed()) {
            throw new NulsException(rollbackResult.getErrorCode());
        }
        return Result.getFailed(KernelErrorCode.IO_ERROR);
    }
}
Also used : NulsException(io.nuls.kernel.exception.NulsException) IOException(java.io.IOException) ValidateResult(io.nuls.kernel.validate.ValidateResult)

Example 88 with NulsException

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

the class UtxoLedgerServiceImpl method rollbackTx.

@Override
public Result rollbackTx(Transaction tx) throws NulsException {
    if (tx == null) {
        return Result.getFailed(LedgerErrorCode.NULL_PARAMETER);
    }
    try {
        // 回滚CoinData
        Result result = rollbackCoinData(tx);
        if (result.isFailed()) {
            Result recoveryResult = saveCoinData(tx);
            if (recoveryResult.isFailed()) {
                throw new NulsException(recoveryResult.getErrorCode());
            }
            return result;
        }
        // 回滚交易
        result = utxoLedgerTransactionStorageService.deleteTx(tx);
        if (result.isFailed()) {
            Result recoveryResult = saveTx(tx);
            if (recoveryResult.isFailed()) {
                throw new NulsException(recoveryResult.getErrorCode());
            }
        }
        return result;
    } catch (IOException e) {
        Log.error(e);
        Result rollbackResult = saveTx(tx);
        if (rollbackResult.isFailed()) {
            throw new NulsException(rollbackResult.getErrorCode());
        }
        return Result.getFailed(KernelErrorCode.IO_ERROR);
    }
}
Also used : NulsException(io.nuls.kernel.exception.NulsException) IOException(java.io.IOException) ValidateResult(io.nuls.kernel.validate.ValidateResult)

Example 89 with NulsException

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

the class UtxoLedgerUtxoStorageServiceImpl method getUtxo.

@Override
public Coin getUtxo(byte[] owner) {
    byte[] utxoBytes = getUtxoBytes(owner);
    Coin coin = null;
    try {
        if (utxoBytes != null) {
            coin = new Coin();
            coin.parse(utxoBytes, 0);
        }
    } catch (NulsException e) {
        Log.error(e);
        return null;
    }
    return coin;
}
Also used : Coin(io.nuls.kernel.model.Coin) NulsException(io.nuls.kernel.exception.NulsException)

Example 90 with NulsException

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

the class ForkChainProcess method changeChain.

private boolean changeChain(ChainContainer newMasterChain, ChainContainer originalForkChain, List<Object[]> verifyResultList) throws NulsException, IOException {
    if (newMasterChain == null || originalForkChain == null || verifyResultList == null) {
        return false;
    }
    // Now the master chain, the forked chain after the switch, needs to be put into the list of chains to be verified.
    // 现在的主链,在切换之后的分叉链,需要放入待验证链列表里面
    ChainContainer oldChain = chainManager.getMasterChain().getAfterTheForkChain(originalForkChain);
    // rollbackTransaction
    List<Block> rollbackBlockList = oldChain.getChain().getAllBlockList();
    ChainLog.debug("rollbackTransaction the master chain , need rollbackTransaction block count is {}, master chain is {} : {} - {} , service best block : {} - {}", rollbackBlockList.size(), chainManager.getMasterChain().getChain().getId(), chainManager.getBestBlock().getHeader().getHeight(), chainManager.getBestBlock().getHeader().getHash(), blockService.getBestBlock().getData().getHeader().getHeight(), blockService.getBestBlock().getData().getHeader().getHash());
    // Need descending order
    // 需要降序排列
    Collections.reverse(rollbackBlockList);
    if (rollbackBlockList != null && rollbackBlockList.size() > 0 && rollbackBlockList.get(0).getHeader().getHeight() != chainManager.getMasterChain().getBestBlock().getHeader().getHeight()) {
        Log.error("------------------------回滚的起始高度不是主链最新高度");
        Log.error("----------------------- masterChain:" + chainManager.getMasterChain().getBestBlock().getHeader().getHeight() + ",hash:" + chainManager.getMasterChain().getBestBlock().getHeader().getHash().getDigestHex());
        Log.error("----------------------- rollbackBlockList:" + rollbackBlockList.get(0).getHeader().getHeight() + ",hash:" + rollbackBlockList.get(0).getHeader().getHash().getDigestHex());
    }
    boolean rollbackResult = rollbackBlocks(rollbackBlockList);
    if (!rollbackResult) {
        return false;
    }
    boolean changeSuccess = true;
    List<Block> successList = new ArrayList<>();
    try {
        changeSuccess = doChange(successList, originalForkChain, verifyResultList);
    } catch (Exception e) {
        Log.error(e);
        changeSuccess = false;
    }
    ChainLog.debug("add new blocks complete, result {}, success count is {} , now service best block : {} - {}", changeSuccess, successList.size(), blockService.getBestBlock().getData().getHeader().getHeight(), blockService.getBestBlock().getData().getHeader().getHash());
    if (changeSuccess) {
        chainManager.setMasterChain(newMasterChain);
        newMasterChain.initRound();
        NulsContext.getInstance().setBestBlock(newMasterChain.getBestBlock());
        if (oldChain.getChain().getAllBlockList().size() > 0) {
            Collections.reverse(rollbackBlockList);
            chainManager.getChains().add(oldChain);
        }
    } else {
        // Fallback status
        // 回退状态
        Collections.reverse(successList);
        for (Block rollBlock : successList) {
            Result rs = blockService.rollbackBlock(rollBlock);
            if (rs.isSuccess()) {
                // 回滚版本更新统计数据
                nulsProtocolProcess.processProtocolRollback(rollBlock.getHeader());
                randomSeedService.rollbackBlock(rollBlock.getHeader());
            }
            RewardStatisticsProcess.rollbackBlock(rollBlock);
        }
        Collections.reverse(rollbackBlockList);
        for (Block addBlock : rollbackBlockList) {
            nulsProtocolProcess.processProtocolUpGrade(addBlock.getHeader());
            Result rs = blockService.saveBlock(addBlock);
            if (!rs.isSuccess()) {
                nulsProtocolProcess.processProtocolRollback(addBlock.getHeader());
            }
            RewardStatisticsProcess.addBlock(addBlock);
        }
    }
    return changeSuccess;
}
Also used : ChainContainer(io.nuls.consensus.poc.container.ChainContainer) IOException(java.io.IOException) NulsException(io.nuls.kernel.exception.NulsException) ContractResult(io.nuls.contract.dto.ContractResult) ValidateResult(io.nuls.kernel.validate.ValidateResult)

Aggregations

NulsException (io.nuls.kernel.exception.NulsException)109 IOException (java.io.IOException)35 Account (io.nuls.account.model.Account)25 ValidateResult (io.nuls.kernel.validate.ValidateResult)23 ECKey (io.nuls.core.tools.crypto.ECKey)20 CoinDataResult (io.nuls.account.ledger.model.CoinDataResult)18 NulsRuntimeException (io.nuls.kernel.exception.NulsRuntimeException)16 UnsupportedEncodingException (java.io.UnsupportedEncodingException)14 TransferTransaction (io.nuls.protocol.model.tx.TransferTransaction)13 Coin (io.nuls.kernel.model.Coin)12 MultiSigAccount (io.nuls.account.model.MultiSigAccount)11 ContractResult (io.nuls.contract.dto.ContractResult)9 NulsByteBuffer (io.nuls.kernel.utils.NulsByteBuffer)9 ArrayList (java.util.ArrayList)9 TransactionSignature (io.nuls.kernel.script.TransactionSignature)8 BigInteger (java.math.BigInteger)8 TransactionDataResult (io.nuls.account.ledger.model.TransactionDataResult)7 AccountPo (io.nuls.account.storage.po.AccountPo)7 AliasPo (io.nuls.account.storage.po.AliasPo)7 CryptoException (io.nuls.core.tools.crypto.Exception.CryptoException)7