Search in sources :

Example 16 with Account

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);
    }
}
Also used : Account(io.nuls.account.entity.Account) Agent(io.nuls.consensus.entity.member.Agent) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) IOException(java.io.IOException) JoinConsensusParam(io.nuls.consensus.entity.params.JoinConsensusParam)

Example 17 with Account

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;
}
Also used : Account(io.nuls.account.entity.Account) TransactionEvent(io.nuls.ledger.event.TransactionEvent) PocExitConsensusTransaction(io.nuls.consensus.entity.tx.PocExitConsensusTransaction) AbstractCoinTransaction(io.nuls.ledger.entity.tx.AbstractCoinTransaction) CoinTransferData(io.nuls.ledger.entity.params.CoinTransferData) PocExitConsensusTransaction(io.nuls.consensus.entity.tx.PocExitConsensusTransaction) Transaction(io.nuls.core.chain.entity.Transaction) RegisterAgentTransaction(io.nuls.consensus.entity.tx.RegisterAgentTransaction) PocJoinConsensusTransaction(io.nuls.consensus.entity.tx.PocJoinConsensusTransaction) AbstractCoinTransaction(io.nuls.ledger.entity.tx.AbstractCoinTransaction) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) PocJoinConsensusTransaction(io.nuls.consensus.entity.tx.PocJoinConsensusTransaction) IOException(java.io.IOException) NulsException(io.nuls.core.exception.NulsException) IOException(java.io.IOException) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException)

Example 18 with Account

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;
}
Also used : Account(io.nuls.account.entity.Account)

Example 19 with 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);
}
Also used : Account(io.nuls.account.entity.Account) Transaction(io.nuls.core.chain.entity.Transaction) AliasTransaction(io.nuls.account.entity.tx.AliasTransaction) AccountPo(io.nuls.db.entity.AccountPo) TransactionLocalPo(io.nuls.db.entity.TransactionLocalPo)

Example 20 with Account

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;
}
Also used : Account(io.nuls.account.entity.Account) AccountPo(io.nuls.db.entity.AccountPo)

Aggregations

Account (io.nuls.account.entity.Account)28 NulsException (io.nuls.core.exception.NulsException)14 NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)11 Result (io.nuls.core.chain.entity.Result)9 ValidateResult (io.nuls.core.validate.ValidateResult)9 AccountPo (io.nuls.db.entity.AccountPo)7 IOException (java.io.IOException)7 DbSession (io.nuls.db.transactional.annotation.DbSession)4 AccountService (io.nuls.account.service.intf.AccountService)3 Agent (io.nuls.consensus.entity.member.Agent)3 CoinTransferData (io.nuls.ledger.entity.params.CoinTransferData)3 Address (io.nuls.account.entity.Address)2 Alias (io.nuls.account.entity.Alias)2 AliasTransaction (io.nuls.account.entity.tx.AliasTransaction)2 Consensus (io.nuls.consensus.entity.Consensus)2 Deposit (io.nuls.consensus.entity.member.Deposit)2 Transaction (io.nuls.core.chain.entity.Transaction)2 AliasPo (io.nuls.db.entity.AliasPo)2 Coin (io.nuls.ledger.entity.params.Coin)2 LockNulsTransaction (io.nuls.ledger.entity.tx.LockNulsTransaction)2