Search in sources :

Example 91 with NulsException

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

the class BlockServiceImpl method rollbackBlock.

/**
 * 回滚区块
 * roll back the block to the store.
 *
 * @param block 完整区块/whole block
 * @return 操作结果/operating result
 * @throws NulsException 回滚区块有可能出现异常,请捕获后谨慎处理/There may be exceptions to the roll back block, please handle it carefully after capture.
 */
@Override
public Result rollbackBlock(Block block) throws NulsException {
    if (null == block) {
        return Result.getFailed(ProtocolErroeCode.BLOCK_IS_NULL);
    }
    boolean txsResult = this.rollbackTxList(block.getTxs(), block.getHeader(), true);
    if (!txsResult) {
        return Result.getFailed();
    }
    BlockHeaderPo po = new BlockHeaderPo();
    po.setHash(block.getHeader().getHash());
    po.setHeight(block.getHeader().getHeight());
    po.setPreHash(block.getHeader().getPreHash());
    Result result = this.blockHeaderStorageService.removeBlockHerader(po);
    if (result.isFailed()) {
        return result;
    }
    try {
        accountLedgerService.rollbackTransactions(block.getTxs());
        // 回滚合约相关交易
        contractService.rollbackTransactionList(block.getTxs());
    } catch (Exception e) {
        Log.warn("rollbackTransaction local tx failed", e);
    }
    return result;
}
Also used : BlockHeaderPo(io.nuls.protocol.storage.po.BlockHeaderPo) NulsException(io.nuls.kernel.exception.NulsException) ContractResult(io.nuls.contract.dto.ContractResult)

Example 92 with NulsException

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

the class TransactionServiceImpl method rollbackTx.

/**
 * 回滚交易时调用的方法,对交易相关的业务进行回退操作
 * The method invoked when the transaction is rolled back and the transaction related business is returned.
 *
 * @param tx            操作的交易/The transaction of the operation
 * @param secondaryData 辅助数据(可以为空)/Secondary data (available for null)
 * @return 操作结果/operating results
 */
@Override
public Result rollbackTx(Transaction tx, Object secondaryData) {
    if (null == tx) {
        return Result.getSuccess();
    }
    List<TransactionProcessor> processorList = TransactionManager.getProcessorList(tx.getClass());
    List<TransactionProcessor> rollbackedList = new ArrayList<>();
    for (TransactionProcessor processor : processorList) {
        Result result = processor.onRollback(tx, secondaryData);
        if (result.isSuccess()) {
            rollbackedList.add(processor);
        } else {
            for (int i = rollbackedList.size() - 1; i >= 0; i--) {
                TransactionProcessor processor1 = rollbackedList.get(i);
                processor1.onCommit(tx, secondaryData);
            }
            return result;
        }
    }
    try {
        ledgerService.rollbackTx(tx);
    } catch (NulsException e) {
        Log.error(e);
        return Result.getFailed(e.getErrorCode());
    }
    return Result.getSuccess();
}
Also used : TransactionProcessor(io.nuls.kernel.processor.TransactionProcessor) NulsException(io.nuls.kernel.exception.NulsException) ArrayList(java.util.ArrayList) ValidateResult(io.nuls.kernel.validate.ValidateResult) Result(io.nuls.kernel.model.Result)

Example 93 with NulsException

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

the class TransactionServiceImpl method broadcastTx.

/**
 * 广播交易给连接的其他对等节点
 * The broadcast transaction gives the connection to other peers.
 *
 * @param tx 完整交易/the whole transaction
 * @return 广播结果/Broadcast the results
 */
@Override
public Result broadcastTx(Transaction tx) {
    try {
        ValidateResult validateResult = contractService.baseValidate(tx);
        if (validateResult.isFailed()) {
            return validateResult;
        }
    } catch (NulsException e) {
        Log.error(e);
        return Result.getFailed();
    }
    TransactionMessage message = new TransactionMessage();
    message.setMsgBody(tx);
    consensusService.newTx(tx);
    // return Result.getSuccess();
    return messageBusService.broadcast(message, null, true, 50);
}
Also used : TransactionMessage(io.nuls.protocol.message.TransactionMessage) NulsException(io.nuls.kernel.exception.NulsException) ValidateResult(io.nuls.kernel.validate.ValidateResult)

Example 94 with NulsException

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

the class BaseProtocolsModuleBootstrap method start.

@Override
public void start() {
    this.waitForDependencyRunning(MessageBusConstant.MODULE_ID_MESSAGE_BUS);
    this.waitForDependencyInited(ConsensusConstant.MODULE_ID_CONSENSUS, NetworkConstant.NETWORK_MODULE_ID);
    BlockService blockService = NulsContext.getServiceBean(BlockService.class);
    Block block0 = blockService.getGengsisBlock().getData();
    Block genesisBlock = NulsContext.getInstance().getGenesisBlock();
    if (null == block0) {
        try {
            blockService.saveBlock(genesisBlock);
        } catch (NulsException e) {
            Log.error(e);
            throw new NulsRuntimeException(e);
        }
    }
    Block block = blockService.getBestBlock().getData();
    while (null != block && block.verify().isFailed()) {
        try {
            blockService.rollbackBlock(block);
        } catch (NulsException e) {
            Log.error(e);
        }
        block = blockService.getBlock(block.getHeader().getPreHash()).getData();
    }
    if (null != block) {
        NulsContext.getInstance().setBestBlock(block);
        this.initHandlers();
        ((DownloadServiceImpl) NulsContext.getServiceBean(DownloadService.class)).start();
    } else {
        start();
    }
}
Also used : DownloadServiceImpl(io.nuls.protocol.base.service.DownloadServiceImpl) NulsException(io.nuls.kernel.exception.NulsException) BlockService(io.nuls.protocol.service.BlockService) Block(io.nuls.kernel.model.Block) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) DownloadService(io.nuls.protocol.service.DownloadService)

Example 95 with NulsException

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

the class TxGroup method parse.

@Override
public void parse(NulsByteBuffer byteBuffer) throws NulsException {
    requestHash = byteBuffer.readHash();
    long txCount = byteBuffer.readVarInt();
    this.txList = new ArrayList<>();
    for (int i = 0; i < txCount; i++) {
        try {
            this.txList.add(byteBuffer.readTransaction());
        } catch (Exception e) {
            throw new NulsException(e);
        }
    }
    initTxMap();
}
Also used : NulsException(io.nuls.kernel.exception.NulsException) IOException(java.io.IOException) NulsException(io.nuls.kernel.exception.NulsException)

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