Search in sources :

Example 31 with NulsRuntimeException

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

the class DepositStorageServiceImpl method getList.

@Override
public List<DepositPo> getList() {
    List<Entry<byte[], byte[]>> list = dbService.entryList(ConsensusStorageConstant.DB_NAME_CONSENSUS_DEPOSIT);
    List<DepositPo> resultList = new ArrayList<>();
    if (list == null) {
        return resultList;
    }
    for (Entry<byte[], byte[]> entry : list) {
        DepositPo depositPo = new DepositPo();
        try {
            depositPo.parse(entry.getValue(), 0);
        } catch (NulsException e) {
            Log.error(e);
            throw new NulsRuntimeException(e);
        }
        NulsDigestData hash = new NulsDigestData();
        try {
            hash.parse(entry.getKey(), 0);
        } catch (NulsException e) {
            Log.error(e);
        }
        depositPo.setTxHash(hash);
        resultList.add(depositPo);
    }
    return resultList;
}
Also used : Entry(io.nuls.db.model.Entry) DepositPo(io.nuls.consensus.poc.storage.po.DepositPo) NulsException(io.nuls.kernel.exception.NulsException) ArrayList(java.util.ArrayList) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) NulsDigestData(io.nuls.kernel.model.NulsDigestData)

Example 32 with NulsRuntimeException

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

the class TransactionCacheStorageServiceImpl method getTx.

@Override
public Transaction getTx(NulsDigestData hash) {
    if (hash == null) {
        return null;
    }
    byte[] hashBytes = null;
    try {
        hashBytes = hash.serialize();
    } catch (IOException e) {
        Log.error(e);
        throw new NulsRuntimeException(e);
    }
    byte[] txBytes = dbService.get(TRANSACTION_CACHE_KEY_NAME, hashBytes);
    Transaction tx = null;
    if (null != txBytes) {
        try {
            tx = TransactionManager.getInstance(new NulsByteBuffer(txBytes, 0));
        } catch (Exception e) {
            Log.error(e);
            return null;
        }
    }
    return tx;
}
Also used : Transaction(io.nuls.kernel.model.Transaction) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) IOException(java.io.IOException) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) IOException(java.io.IOException) NulsException(io.nuls.kernel.exception.NulsException) NulsByteBuffer(io.nuls.kernel.utils.NulsByteBuffer)

Example 33 with NulsRuntimeException

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

the class PocConsensusResource method createAgent.

@POST
@Path("/agent")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Create an agent for consensus! 创建共识(代理)节点 [3.6.3]", notes = "返回创建的节点成功的交易hash")
@ApiResponses(value = { @ApiResponse(code = 200, message = "success", response = String.class) })
public RpcClientResult createAgent(@ApiParam(name = "form", value = "创建节点表单数据", required = true) CreateAgentForm form) throws NulsException {
    AssertUtil.canNotEmpty(form);
    AssertUtil.canNotEmpty(form.getAgentAddress(), "agent address can not be null");
    AssertUtil.canNotEmpty(form.getCommissionRate(), "commission rate can not be null");
    AssertUtil.canNotEmpty(form.getDeposit(), "deposit can not be null");
    AssertUtil.canNotEmpty(form.getPackingAddress(), "packing address can not be null");
    if (!AddressTool.isPackingAddress(form.getPackingAddress()) || !AddressTool.validAddress(form.getAgentAddress())) {
        throw new NulsRuntimeException(AccountErrorCode.ADDRESS_ERROR);
    }
    Account account = accountService.getAccount(form.getAgentAddress()).getData();
    if (null == account) {
        return Result.getFailed(AccountErrorCode.ACCOUNT_NOT_EXIST).toRpcClientResult();
    }
    if (!account.isOk()) {
        return Result.getFailed(AccountErrorCode.IMPORTING_ACCOUNT).toRpcClientResult();
    }
    if (account.isEncrypted() && account.isLocked()) {
        AssertUtil.canNotEmpty(form.getPassword(), "password is wrong");
        if (!account.validatePassword(form.getPassword())) {
            return Result.getFailed(AccountErrorCode.PASSWORD_IS_WRONG).toRpcClientResult();
        }
    }
    CreateAgentTransaction tx = new CreateAgentTransaction();
    tx.setTime(TimeService.currentTimeMillis());
    Agent agent = new Agent();
    agent.setAgentAddress(AddressTool.getAddress(form.getAgentAddress()));
    agent.setPackingAddress(AddressTool.getAddress(form.getPackingAddress()));
    if (StringUtils.isBlank(form.getRewardAddress())) {
        agent.setRewardAddress(agent.getAgentAddress());
    } else {
        agent.setRewardAddress(AddressTool.getAddress(form.getRewardAddress()));
    }
    agent.setDeposit(Na.valueOf(form.getDeposit()));
    agent.setCommissionRate(form.getCommissionRate());
    tx.setTxData(agent);
    CoinData coinData = new CoinData();
    List<Coin> toList = new ArrayList<>();
    toList.add(new Coin(agent.getAgentAddress(), agent.getDeposit(), PocConsensusConstant.CONSENSUS_LOCK_TIME));
    coinData.setTo(toList);
    tx.setCoinData(coinData);
    CoinDataResult result = accountLedgerService.getCoinData(agent.getAgentAddress(), agent.getDeposit(), tx.size(), TransactionFeeCalculator.OTHER_PRICE_PRE_1024_BYTES);
    RpcClientResult result1 = this.txProcessing(tx, result, account, form.getPassword());
    if (!result1.isSuccess()) {
        return result1;
    }
    Map<String, String> valueMap = new HashMap<>();
    valueMap.put("value", tx.getHash().getDigestHex());
    return Result.getSuccess().setData(valueMap).toRpcClientResult();
}
Also used : Account(io.nuls.account.model.Account) MultiSigAccount(io.nuls.account.model.MultiSigAccount) StopAgent(io.nuls.consensus.poc.protocol.entity.StopAgent) Agent(io.nuls.consensus.poc.protocol.entity.Agent) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) CreateAgentTransaction(io.nuls.consensus.poc.protocol.tx.CreateAgentTransaction) CoinDataResult(io.nuls.account.ledger.model.CoinDataResult)

Example 34 with NulsRuntimeException

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

the class PocConsensusResource method stopAgent.

@POST
@Path("/agent/stop")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "注销共识节点 [3.6.5]", notes = "返回注销成功交易hash")
@ApiResponses(value = { @ApiResponse(code = 200, message = "success", response = String.class) })
public RpcClientResult stopAgent(@ApiParam(name = "form", value = "注销共识节点表单数据", required = true) StopAgentForm form) throws NulsException, IOException {
    AssertUtil.canNotEmpty(form);
    AssertUtil.canNotEmpty(form.getAddress());
    if (!AddressTool.validAddress(form.getAddress())) {
        throw new NulsRuntimeException(KernelErrorCode.PARAMETER_ERROR);
    }
    Account account = accountService.getAccount(form.getAddress()).getData();
    if (null == account) {
        return Result.getFailed(AccountErrorCode.ACCOUNT_NOT_EXIST).toRpcClientResult();
    }
    if (account.isEncrypted() && account.isLocked()) {
        AssertUtil.canNotEmpty(form.getPassword(), "password is wrong");
        if (!account.validatePassword(form.getPassword())) {
            return Result.getFailed(AccountErrorCode.PASSWORD_IS_WRONG).toRpcClientResult();
        }
    }
    StopAgentTransaction tx = new StopAgentTransaction();
    StopAgent stopAgent = new StopAgent();
    stopAgent.setAddress(AddressTool.getAddress(form.getAddress()));
    List<Agent> agentList = PocConsensusContext.getChainManager().getMasterChain().getChain().getAgentList();
    Agent agent = null;
    for (Agent a : agentList) {
        if (a.getDelHeight() > 0) {
            continue;
        }
        if (Arrays.equals(a.getAgentAddress(), account.getAddress().getAddressBytes())) {
            agent = a;
            break;
        }
    }
    if (agent == null || agent.getDelHeight() > 0) {
        return Result.getFailed(PocConsensusErrorCode.AGENT_NOT_EXIST).toRpcClientResult();
    }
    stopAgent.setCreateTxHash(agent.getTxHash());
    tx.setTxData(stopAgent);
    CoinData coinData = ConsensusTool.getStopAgentCoinData(agent, TimeService.currentTimeMillis() + PocConsensusConstant.STOP_AGENT_LOCK_TIME);
    tx.setCoinData(coinData);
    Na fee = TransactionFeeCalculator.getMaxFee(108 + tx.size());
    coinData.getTo().get(0).setNa(coinData.getTo().get(0).getNa().subtract(fee));
    RpcClientResult result1 = this.txProcessing(tx, null, account, form.getPassword());
    if (!result1.isSuccess()) {
        return result1;
    }
    Map<String, String> valueMap = new HashMap<>();
    valueMap.put("value", tx.getHash().getDigestHex());
    return Result.getSuccess().setData(valueMap).toRpcClientResult();
}
Also used : Account(io.nuls.account.model.Account) MultiSigAccount(io.nuls.account.model.MultiSigAccount) StopAgent(io.nuls.consensus.poc.protocol.entity.StopAgent) Agent(io.nuls.consensus.poc.protocol.entity.Agent) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) StopAgent(io.nuls.consensus.poc.protocol.entity.StopAgent) StopAgentTransaction(io.nuls.consensus.poc.protocol.tx.StopAgentTransaction)

Example 35 with NulsRuntimeException

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

the class PocConsensusResource method createStopMutilAgent.

@POST
@Path("/multiAccount/agent/stopMultiAgent")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "多签账户注销共识节点", notes = "返回注销成功交易hash")
@ApiResponses(value = { @ApiResponse(code = 200, message = "success", response = String.class) })
public RpcClientResult createStopMutilAgent(@ApiParam(name = "form", value = "多签账户注销共识节点表单数据", required = true) CreateStopMultiAgentForm form) throws Exception {
    if (NulsContext.MAIN_NET_VERSION <= 1) {
        return Result.getFailed(KernelErrorCode.VERSION_TOO_LOW).toRpcClientResult();
    }
    AssertUtil.canNotEmpty(form);
    AssertUtil.canNotEmpty(form.getAddress());
    AssertUtil.canNotEmpty(form.getSignAddress());
    if (!AddressTool.validAddress(form.getAddress()) || !AddressTool.validAddress(form.getSignAddress())) {
        throw new NulsRuntimeException(KernelErrorCode.PARAMETER_ERROR);
    }
    Account account = accountService.getAccount(form.getSignAddress()).getData();
    if (null == account) {
        return Result.getFailed(AccountErrorCode.ACCOUNT_NOT_EXIST).toRpcClientResult();
    }
    if (account.isEncrypted() && account.isLocked()) {
        AssertUtil.canNotEmpty(form.getPassword(), "password is wrong");
        if (!account.validatePassword(form.getPassword())) {
            return Result.getFailed(AccountErrorCode.PASSWORD_IS_WRONG).toRpcClientResult();
        }
    }
    List<Agent> agentList = PocConsensusContext.getChainManager().getMasterChain().getChain().getAgentList();
    Agent agent = null;
    for (Agent p : agentList) {
        if (p.getDelHeight() > 0) {
            continue;
        }
        if (Arrays.equals(p.getAgentAddress(), AddressTool.getAddress(form.getAddress()))) {
            agent = p;
            break;
        }
    }
    if (agent == null || agent.getDelHeight() > 0) {
        return Result.getFailed(PocConsensusErrorCode.AGENT_NOT_EXIST).toRpcClientResult();
    }
    Result<MultiSigAccount> sigAccountResult = accountService.getMultiSigAccount(form.getAddress());
    MultiSigAccount multiSigAccount = sigAccountResult.getData();
    // 验证签名账户是否属于多签账户,如果不是多签账户下的地址则提示错误
    if (!AddressTool.validSignAddress(multiSigAccount.getPubKeyList(), account.getPubKey())) {
        return Result.getFailed(AccountErrorCode.SIGN_ADDRESS_NOT_MATCH).toRpcClientResult();
    }
    Script redeemScript = accountLedgerService.getRedeemScript(multiSigAccount);
    if (redeemScript == null) {
        return Result.getFailed(AccountErrorCode.ACCOUNT_NOT_EXIST).toRpcClientResult();
    }
    // 如果为交易发起人,则需要填写组装交易需要的信息
    StopAgentTransaction tx = new StopAgentTransaction();
    TransactionSignature transactionSignature = new TransactionSignature();
    List<Script> scripts = new ArrayList<>();
    StopAgent stopAgent = new StopAgent();
    stopAgent.setAddress(agent.getAgentAddress());
    stopAgent.setCreateTxHash(agent.getTxHash());
    tx.setTime(TimeService.currentTimeMillis());
    tx.setTxData(stopAgent);
    CoinData coinData = ConsensusTool.getStopAgentCoinData(agent, TimeService.currentTimeMillis() + PocConsensusConstant.STOP_AGENT_LOCK_TIME, null);
    tx.setCoinData(coinData);
    Na fee = TransactionFeeCalculator.getMaxFee(108 + tx.size());
    coinData.getTo().get(0).setNa(coinData.getTo().get(0).getNa().subtract(fee));
    tx.setHash(NulsDigestData.calcDigestData(tx.serializeForHash()));
    // 将赎回脚本先存储在签名脚本中
    scripts.add(redeemScript);
    transactionSignature.setScripts(scripts);
    // 使用签名账户对交易进行签名
    Result resultData = accountLedgerService.txMultiProcess(tx, transactionSignature, account, form.getPassword());
    if (resultData.isSuccess()) {
        Map<String, String> valueMap = new HashMap<>();
        valueMap.put("txData", (String) resultData.getData());
        return Result.getSuccess().setData(valueMap).toRpcClientResult();
    }
    return resultData.toRpcClientResult();
}
Also used : MultiSigAccount(io.nuls.account.model.MultiSigAccount) Account(io.nuls.account.model.Account) MultiSigAccount(io.nuls.account.model.MultiSigAccount) StopAgent(io.nuls.consensus.poc.protocol.entity.StopAgent) Agent(io.nuls.consensus.poc.protocol.entity.Agent) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) StopAgent(io.nuls.consensus.poc.protocol.entity.StopAgent) CoinDataResult(io.nuls.account.ledger.model.CoinDataResult) StopAgentTransaction(io.nuls.consensus.poc.protocol.tx.StopAgentTransaction)

Aggregations

NulsRuntimeException (io.nuls.kernel.exception.NulsRuntimeException)68 IOException (java.io.IOException)35 NulsException (io.nuls.kernel.exception.NulsException)26 ArrayList (java.util.ArrayList)21 CoinDataResult (io.nuls.account.ledger.model.CoinDataResult)10 Result (io.nuls.kernel.model.Result)9 Account (io.nuls.account.model.Account)8 MultiSigAccount (io.nuls.account.model.MultiSigAccount)8 Entry (io.nuls.db.model.Entry)8 Agent (io.nuls.consensus.poc.protocol.entity.Agent)7 VarInt (io.nuls.kernel.utils.VarInt)7 CreateAgentTransaction (io.nuls.consensus.poc.protocol.tx.CreateAgentTransaction)6 ValidateResult (io.nuls.kernel.validate.ValidateResult)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 Deposit (io.nuls.consensus.poc.protocol.entity.Deposit)5 DepositTransaction (io.nuls.consensus.poc.protocol.tx.DepositTransaction)5 DepositPo (io.nuls.consensus.poc.storage.po.DepositPo)5 PunishLogPo (io.nuls.consensus.poc.storage.po.PunishLogPo)5 TransferTransaction (io.nuls.protocol.model.tx.TransferTransaction)5 StopAgent (io.nuls.consensus.poc.protocol.entity.StopAgent)4