Search in sources :

Example 31 with NulsRuntimeException

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

the class PocConsensusServiceImpl method registerAgent.

private Transaction registerAgent(Agent agent, Account account, String password) throws IOException, NulsException {
    TransactionEvent event = new TransactionEvent();
    CoinTransferData data = new CoinTransferData(OperationType.LOCK, this.ledgerService.getTxFee(TransactionConstant.TX_TYPE_REGISTER_AGENT));
    data.setTotalNa(agent.getDeposit());
    data.addFrom(account.getAddress().toString());
    Coin coin = new Coin();
    coin.setUnlockHeight(0);
    coin.setUnlockTime(0);
    coin.setNa(agent.getDeposit());
    data.addTo(account.getAddress().toString(), coin);
    RegisterAgentTransaction tx = null;
    try {
        tx = new RegisterAgentTransaction(data, password);
    } catch (NulsException e) {
        Log.error(e);
        throw new NulsRuntimeException(e);
    }
    Consensus<Agent> con = new ConsensusAgentImpl();
    con.setAddress(account.getAddress().toString());
    agent.setStartTime(TimeService.currentTimeMillis());
    con.setExtend(agent);
    tx.setTxData(con);
    tx.setHash(NulsDigestData.calcDigestData(tx.serialize()));
    tx.setScriptSig(accountService.createP2PKHScriptSigFromDigest(tx.getHash(), account, password).serialize());
    tx.verifyWithException();
    event.setEventBody(tx);
    List<String> nodeList = eventBroadcaster.broadcastHashAndCache(event, true);
    if (null == nodeList || nodeList.isEmpty()) {
        throw new NulsRuntimeException(ErrorCode.FAILED, "broadcast transaction failed!");
    }
    return tx;
}
Also used : Coin(io.nuls.ledger.entity.params.Coin) Agent(io.nuls.consensus.entity.member.Agent) TransactionEvent(io.nuls.ledger.event.TransactionEvent) RegisterAgentTransaction(io.nuls.consensus.entity.tx.RegisterAgentTransaction) CoinTransferData(io.nuls.ledger.entity.params.CoinTransferData) ConsensusAgentImpl(io.nuls.consensus.entity.ConsensusAgentImpl) NulsException(io.nuls.core.exception.NulsException) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException)

Example 32 with NulsRuntimeException

use of io.nuls.core.exception.NulsRuntimeException 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 33 with NulsRuntimeException

use of io.nuls.core.exception.NulsRuntimeException 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 34 with NulsRuntimeException

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

the class JoinConsensusTxService method onCommit.

@Override
public void onCommit(PocJoinConsensusTransaction tx) throws NulsException {
    manager.changeDepositStatus(tx.getTxData().getHexHash(), ConsensusStatusEnum.WAITING);
    Consensus<Deposit> cd = tx.getTxData();
    cd.getExtend().setTxHash(tx.getHash().getDigestHex());
    cd.getExtend().setStatus(ConsensusStatusEnum.WAITING.getCode());
    DepositPo po = ConsensusTool.depositToPojo(cd, tx.getHash().getDigestHex());
    po.setBlockHeight(tx.getBlockHeight());
    po.setTime(tx.getTime());
    depositDataService.save(po);
    Map<String, Object> paramsMap = new HashMap<>();
    paramsMap.put("agentHash", cd.getExtend().getAgentHash());
    List<DepositPo> poList = depositDataService.getList(paramsMap);
    long sum = 0L;
    for (DepositPo depositPo : poList) {
        sum += depositPo.getDeposit();
    }
    if (sum >= PocConsensusConstant.SUM_OF_DEPOSIT_OF_AGENT_LOWER_LIMIT.getValue()) {
        manager.changeAgentStatusByHash(tx.getTxData().getExtend().getAgentHash(), ConsensusStatusEnum.IN);
        manager.changeDepositStatusByAgentHash(tx.getTxData().getExtend().getAgentHash(), ConsensusStatusEnum.IN);
        AgentPo daPo = this.accountDataService.get(cd.getExtend().getAgentHash());
        if (null == daPo) {
            throw new NulsRuntimeException(ErrorCode.DATA_ERROR, "the agent cannot find,agent hash:" + cd.getExtend().getAgentHash());
        }
        daPo.setStatus(ConsensusStatusEnum.IN.getCode());
        this.accountDataService.updateSelective(daPo);
    }
    EntrustConsensusNotice notice = new EntrustConsensusNotice();
    notice.setEventBody(tx);
    NulsContext.getServiceBean(EventBroadcaster.class).publishToLocal(notice);
}
Also used : EventBroadcaster(io.nuls.event.bus.service.intf.EventBroadcaster) Deposit(io.nuls.consensus.entity.member.Deposit) HashMap(java.util.HashMap) EntrustConsensusNotice(io.nuls.consensus.event.notice.EntrustConsensusNotice) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) DepositPo(io.nuls.db.entity.DepositPo) AgentPo(io.nuls.db.entity.AgentPo)

Example 35 with NulsRuntimeException

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

the class AESEncrypt method encrypt.

/**
 * 加密
 *
 * @param plainBytes
 * @param iv
 * @param aesKey
 * @return EncryptedData
 */
public static EncryptedData encrypt(byte[] plainBytes, byte[] iv, KeyParameter aesKey) throws NulsRuntimeException {
    Utils.checkNotNull(plainBytes);
    Utils.checkNotNull(aesKey);
    try {
        if (iv == null) {
            iv = new byte[16];
            SECURE_RANDOM.nextBytes(iv);
        }
        ParametersWithIV keyWithIv = new ParametersWithIV(aesKey, iv);
        // Encrypt using AES.
        BufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESFastEngine()));
        cipher.init(true, keyWithIv);
        byte[] encryptedBytes = new byte[cipher.getOutputSize(plainBytes.length)];
        final int length1 = cipher.processBytes(plainBytes, 0, plainBytes.length, encryptedBytes, 0);
        final int length2 = cipher.doFinal(encryptedBytes, length1);
        return new EncryptedData(iv, Arrays.copyOf(encryptedBytes, length1 + length2));
    } catch (Exception e) {
        throw new NulsRuntimeException(e);
    }
}
Also used : ParametersWithIV(org.spongycastle.crypto.params.ParametersWithIV) PaddedBufferedBlockCipher(org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher) BufferedBlockCipher(org.spongycastle.crypto.BufferedBlockCipher) PaddedBufferedBlockCipher(org.spongycastle.crypto.paddings.PaddedBufferedBlockCipher) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) CBCBlockCipher(org.spongycastle.crypto.modes.CBCBlockCipher) AESFastEngine(org.spongycastle.crypto.engines.AESFastEngine) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException)

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