Search in sources :

Example 1 with NulsRuntimeException

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

the class AccountServiceImpl method createAccount.

@Override
@DbSession
public Result<List<String>> createAccount(int count, String password) {
    if (count <= 0 || count > AccountTool.CREATE_MAX_SIZE) {
        return new Result<>(false, "between 0 and 100 can be created at once");
    }
    // todo need to recover the status of the wallet.
    if (!StringUtils.validPassword(password)) {
        return new Result(false, "Length between 8 and 20, the combination of characters and numbers");
    }
    Account defaultAccount = getDefaultAccount();
    if (defaultAccount != null && defaultAccount.isEncrypted()) {
        try {
            if (!defaultAccount.unlock(password)) {
                return Result.getFailed(ErrorCode.PASSWORD_IS_WRONG);
            }
            defaultAccount.lock();
        } catch (NulsException e) {
            return Result.getFailed(ErrorCode.PASSWORD_IS_WRONG);
        }
    }
    locker.lock();
    try {
        List<Account> accounts = new ArrayList<>();
        List<AccountPo> accountPos = new ArrayList<>();
        List<String> resultList = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            Account account = AccountTool.createAccount();
            signAccount(account);
            account.encrypt(password);
            AccountPo po = new AccountPo();
            AccountTool.toPojo(account, po);
            accounts.add(account);
            accountPos.add(po);
            resultList.add(account.getAddress().getBase58());
        }
        accountDao.save(accountPos);
        accountCacheService.putAccountList(accounts);
        NulsContext.LOCAL_ADDRESS_LIST.addAll(resultList);
        for (Account account : accounts) {
            AccountCreateNotice notice = new AccountCreateNotice();
            notice.setEventBody(account);
            eventBroadcaster.publishToLocal(notice);
        }
        return new Result<>(true, "OK", resultList);
    } catch (Exception e) {
        Log.error(e);
        // todo remove newaccounts
        throw new NulsRuntimeException(ErrorCode.FAILED, "create account failed!");
    } finally {
        locker.unlock();
    }
}
Also used : Account(io.nuls.account.entity.Account) AccountPo(io.nuls.db.entity.AccountPo) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) NulsException(io.nuls.core.exception.NulsException) IOException(java.io.IOException) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) ValidateResult(io.nuls.core.validate.ValidateResult) Result(io.nuls.core.chain.entity.Result) NulsException(io.nuls.core.exception.NulsException) DbSession(io.nuls.db.transactional.annotation.DbSession)

Example 2 with NulsRuntimeException

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

the class AccountServiceImpl method setDefaultAccount.

@Override
public void setDefaultAccount(String id) {
    if (id == null) {
        return;
    }
    Account account = accountCacheService.getAccountById(id);
    if (null != account) {
        NulsContext.DEFAULT_ACCOUNT_ID = id;
    } else {
        throw new NulsRuntimeException(ErrorCode.FAILED, "The account not exist,id:" + id);
    }
    DefaultAccountChangeNotice notice = new DefaultAccountChangeNotice();
    notice.setEventBody(account);
    eventBroadcaster.publishToLocal(notice);
}
Also used : Account(io.nuls.account.entity.Account) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException)

Example 3 with NulsRuntimeException

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

the class UtxoCoinDataProvider method save.

/**
 * 1. change spending output status  (cache and database)
 * 2. save new input
 * 3. save new unSpend output (cache and database)
 * 4. finally, calc balance
 */
@Override
@DbSession
public void save(CoinData coinData, Transaction tx) throws NulsException {
    UtxoData utxoData = (UtxoData) coinData;
    List<UtxoInputPo> inputPoList = new ArrayList<>();
    List<UtxoOutput> spends = new ArrayList<>();
    List<UtxoOutputPo> spendPoList = new ArrayList<>();
    List<TxAccountRelationPo> txRelations = new ArrayList<>();
    Set<String> addressSet = new HashSet<>();
    try {
        processDataInput(utxoData, inputPoList, spends, spendPoList, addressSet);
        List<UtxoOutputPo> outputPoList = new ArrayList<>();
        for (int i = 0; i < utxoData.getOutputs().size(); i++) {
            UtxoOutput output = utxoData.getOutputs().get(i);
            output = ledgerCacheService.getUtxo(output.getKey());
            if (output == null) {
                throw new NulsRuntimeException(ErrorCode.DATA_NOT_FOUND);
            }
            if (output.isConfirm() || OutPutStatusEnum.UTXO_SPENT == output.getStatus()) {
                Log.error("-----------------------------------save() output status is" + output.getStatus().name());
                throw new NulsRuntimeException(ErrorCode.DATA_ERROR, "use a not legal utxo");
            }
            if (OutPutStatusEnum.UTXO_UNCONFIRM_CONSENSUS_LOCK == output.getStatus()) {
                output.setStatus(OutPutStatusEnum.UTXO_CONFIRM_CONSENSUS_LOCK);
            } else if (OutPutStatusEnum.UTXO_UNCONFIRM_TIME_LOCK == output.getStatus()) {
                output.setStatus(OutPutStatusEnum.UTXO_CONFIRM_TIME_LOCK);
            } else if (OutPutStatusEnum.UTXO_UNCONFIRM_UNSPEND == output.getStatus()) {
                output.setStatus(OutPutStatusEnum.UTXO_CONFIRM_UNSPEND);
            } else if (OutPutStatusEnum.UTXO_UNCONFIRM_SPEND == output.getStatus()) {
                output.setStatus(OutPutStatusEnum.UTXO_CONFIRM_SPEND);
            }
            UtxoOutputPo outputPo = UtxoTransferTool.toOutputPojo(output);
            outputPoList.add(outputPo);
            addressSet.add(Address.fromHashs(output.getAddress()).getBase58());
        }
        for (String address : addressSet) {
            TxAccountRelationPo relationPo = new TxAccountRelationPo(tx.getHash().getDigestHex(), address);
            txRelations.add(relationPo);
        }
        outputDataService.updateStatus(spendPoList);
        inputDataService.save(inputPoList);
        outputDataService.save(outputPoList);
        relationDataService.save(txRelations);
        afterSaveDatabase(spends, utxoData, tx);
        for (String address : addressSet) {
            UtxoTransactionTool.getInstance().calcBalance(address, true);
        }
    } catch (Exception e) {
        // }
        throw e;
    }
}
Also used : TxAccountRelationPo(io.nuls.db.entity.TxAccountRelationPo) UtxoOutputPo(io.nuls.db.entity.UtxoOutputPo) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) NulsException(io.nuls.core.exception.NulsException) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) UtxoInputPo(io.nuls.db.entity.UtxoInputPo) DbSession(io.nuls.db.transactional.annotation.DbSession)

Example 4 with NulsRuntimeException

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

the class UtxoLedgerServiceImpl method saveTxList.

@Override
@DbSession
public boolean saveTxList(List<Transaction> txList) throws IOException {
    lock.lock();
    try {
        List<TransactionPo> poList = new ArrayList<>();
        List<TransactionLocalPo> localPoList = new ArrayList<>();
        for (int i = 0; i < txList.size(); i++) {
            Transaction tx = txList.get(i);
            boolean isMine = false;
            try {
                isMine = this.checkTxIsMine(tx);
            } catch (NulsException e) {
                throw new NulsRuntimeException(e);
            }
            TransactionPo po = UtxoTransferTool.toTransactionPojo(tx);
            poList.add(po);
            if (isMine) {
                TransactionLocalPo localPo = UtxoTransferTool.toLocalTransactionPojo(tx);
                localPoList.add(localPo);
            }
        }
        txDao.saveTxList(poList);
        if (localPoList.size() > 0) {
            txDao.saveLocalList(localPoList);
        }
    } finally {
        lock.unlock();
    }
    return false;
}
Also used : TransferTransaction(io.nuls.ledger.entity.tx.TransferTransaction) AbstractCoinTransaction(io.nuls.ledger.entity.tx.AbstractCoinTransaction) LockNulsTransaction(io.nuls.ledger.entity.tx.LockNulsTransaction) NulsException(io.nuls.core.exception.NulsException) TransactionLocalPo(io.nuls.db.entity.TransactionLocalPo) ArrayList(java.util.ArrayList) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) TransactionPo(io.nuls.db.entity.TransactionPo) DbSession(io.nuls.db.transactional.annotation.DbSession)

Example 5 with NulsRuntimeException

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

the class DisruptorUtil method createDisruptor.

/**
 * create a disruptor
 *
 * @param name           The title of the disruptor
 * @param ringBufferSize The size of ringBuffer
 */
public void createDisruptor(String name, int ringBufferSize) {
    if (DISRUPTOR_MAP.keySet().contains(name)) {
        throw new NulsRuntimeException(ErrorCode.FAILED, "create disruptor faild,the name is repetitive!");
    }
    Disruptor<DisruptorEvent> disruptor = new Disruptor<DisruptorEvent>(EVENT_FACTORY, ringBufferSize, new NulsThreadFactory(ModuleService.getInstance().getModuleId(EventBusModuleBootstrap.class), name), ProducerType.SINGLE, new BlockingWaitStrategy());
    // SleepingWaitStrategy
    // disruptor.handleEventsWith(new EventHandler<DisruptorEvent>() {
    // @Override
    // public void onEvent(DisruptorEvent disruptorEvent, long l, boolean b) throws Exception {
    // Log.debug(disruptorEvent.getData() + "");
    // }
    // });
    DISRUPTOR_MAP.put(name, disruptor);
}
Also used : NulsThreadFactory(io.nuls.core.thread.manager.NulsThreadFactory) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) Disruptor(com.lmax.disruptor.dsl.Disruptor)

Aggregations

NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)64 NulsException (io.nuls.core.exception.NulsException)26 IOException (java.io.IOException)11 Transaction (io.nuls.core.chain.entity.Transaction)10 ArrayList (java.util.ArrayList)9 Account (io.nuls.account.entity.Account)7 AbstractNulsQueue (io.nuls.core.utils.queue.intf.AbstractNulsQueue)7 DbSession (io.nuls.db.transactional.annotation.DbSession)6 CoinTransferData (io.nuls.ledger.entity.params.CoinTransferData)5 ValidateResult (io.nuls.core.validate.ValidateResult)4 Coin (io.nuls.ledger.entity.params.Coin)4 TransactionEvent (io.nuls.ledger.event.TransactionEvent)4 AccountService (io.nuls.account.service.intf.AccountService)3 Block (io.nuls.core.chain.entity.Block)3 AccountPo (io.nuls.db.entity.AccountPo)3 AbstractCoinTransaction (io.nuls.ledger.entity.tx.AbstractCoinTransaction)3 BlockHashResponse (io.nuls.consensus.entity.BlockHashResponse)2 RedPunishData (io.nuls.consensus.entity.RedPunishData)2 BestCorrectBlock (io.nuls.consensus.entity.block.BestCorrectBlock)2 Agent (io.nuls.consensus.entity.member.Agent)2