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();
}
}
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);
}
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;
}
}
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;
}
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);
}
Aggregations