use of io.nuls.account.entity.Account in project nuls by nuls-io.
the class PocConsensusServiceImpl method startConsensus.
@Override
public Transaction startConsensus(String address, String password, Map<String, Object> paramsMap) throws NulsException {
Account account = this.accountService.getAccount(address);
if (null == account) {
throw new NulsRuntimeException(ErrorCode.FAILED, "The account is not exist,address:" + address);
}
if (paramsMap == null || paramsMap.size() < 2) {
throw new NulsRuntimeException(ErrorCode.NULL_PARAMETER);
}
if (!account.validatePassword(password)) {
throw new NulsRuntimeException(ErrorCode.PASSWORD_IS_WRONG);
}
JoinConsensusParam params = new JoinConsensusParam(paramsMap);
if (StringUtils.isNotBlank(params.getIntroduction())) {
Agent agent = new Agent();
agent.setPackingAddress(params.getPackingAddress());
agent.setDeposit(Na.valueOf(params.getDeposit()));
agent.setIntroduction(params.getIntroduction());
agent.setSeed(params.isSeed());
agent.setCommissionRate(params.getCommissionRate());
agent.setAgentName(params.getAgentName());
try {
return this.registerAgent(agent, account, password);
} catch (IOException e) {
throw new NulsRuntimeException(e);
}
}
try {
return this.joinTheConsensus(account, password, params.getDeposit(), params.getAgentHash());
} catch (IOException e) {
throw new NulsRuntimeException(e);
}
}
use of io.nuls.account.entity.Account in project nuls by nuls-io.
the class PocConsensusServiceImpl method stopConsensus.
@Override
public Transaction stopConsensus(String address, String password, Map<String, Object> paramsMap) throws NulsException, IOException {
AbstractCoinTransaction joinTx = null;
if (null != paramsMap && StringUtils.isNotBlank((String) paramsMap.get("txHash"))) {
PocJoinConsensusTransaction tx = (PocJoinConsensusTransaction) ledgerService.getTx(NulsDigestData.fromDigestHex((String) paramsMap.get("txHash")));
joinTx = tx;
} else {
try {
List<Transaction> txlist = this.ledgerService.getTxList(address, TransactionConstant.TX_TYPE_REGISTER_AGENT);
if (null != txlist || !txlist.isEmpty()) {
joinTx = (AbstractCoinTransaction) txlist.get(0);
}
} catch (Exception e) {
Log.error(e);
}
}
if (null == joinTx) {
throw new NulsRuntimeException(ErrorCode.FAILED, "The related transaction is not exist!");
}
Account account = this.accountService.getAccount(address);
if (null == account) {
throw new NulsRuntimeException(ErrorCode.ACCOUNT_NOT_EXIST, "address:" + address.toString());
}
if (!account.validatePassword(password)) {
throw new NulsRuntimeException(ErrorCode.PASSWORD_IS_WRONG);
}
TransactionEvent event = new TransactionEvent();
CoinTransferData coinTransferData = new CoinTransferData(OperationType.UNLOCK, this.ledgerService.getTxFee(TransactionConstant.TX_TYPE_EXIT_CONSENSUS));
coinTransferData.setTotalNa(Na.ZERO);
PocExitConsensusTransaction tx = new PocExitConsensusTransaction(coinTransferData, password);
tx.setTxData(joinTx.getHash());
try {
tx.setHash(NulsDigestData.calcDigestData(tx.serialize()));
} catch (IOException e) {
Log.error(e);
throw new NulsRuntimeException(ErrorCode.HASH_ERROR, e);
}
tx.setScriptSig(accountService.createP2PKHScriptSigFromDigest(tx.getHash(), account, password).serialize());
event.setEventBody(tx);
eventBroadcaster.broadcastHashAndCache(event, true);
return tx;
}
use of io.nuls.account.entity.Account in project nuls by nuls-io.
the class AccountServiceImpl method getDefaultAccount.
@Override
public Account getDefaultAccount() {
Account account = null;
if (NulsContext.DEFAULT_ACCOUNT_ID != null) {
account = getAccount(NulsContext.DEFAULT_ACCOUNT_ID);
}
if (account == null) {
List<Account> accounts = getAccountList();
if (accounts != null && !accounts.isEmpty()) {
account = accounts.get(0);
NulsContext.DEFAULT_ACCOUNT_ID = account.getAddress().getBase58();
}
}
return account;
}
use of io.nuls.account.entity.Account in project nuls by nuls-io.
the class AccountServiceImpl method importSave.
private void importSave(List<Account> accounts) throws Exception {
List<AccountPo> accountPoList = new ArrayList<>();
for (Account account : accounts) {
AccountPo accountPo = new AccountPo();
AccountTool.toPojo(account, accountPo);
List<TransactionLocalPo> transactionPos = new ArrayList<>();
for (Transaction tx : account.getMyTxs()) {
TransactionLocalPo po = UtxoTransferTool.toLocalTransactionPojo(tx);
transactionPos.add(po);
}
accountPo.setMyTxs(transactionPos);
accountPoList.add(accountPo);
}
accountAliasDBService.importAccount(accountPoList);
}
use of io.nuls.account.entity.Account in project nuls by nuls-io.
the class AccountServiceImpl method getAccountList.
@Override
public List<Account> getAccountList() {
List<Account> list = this.accountCacheService.getAccountList();
if (null != list && !list.isEmpty()) {
return list;
}
list = new ArrayList<>();
List<AccountPo> poList = this.accountDao.getList();
Set<String> addressList = new HashSet<>();
if (null == poList || poList.isEmpty()) {
return list;
}
for (AccountPo po : poList) {
Account account = new Account();
AccountTool.toBean(po, account);
list.add(account);
addressList.add(account.getAddress().getBase58());
}
this.accountCacheService.putAccountList(list);
NulsContext.LOCAL_ADDRESS_LIST = addressList;
return list;
}
Aggregations