Search in sources :

Example 1 with StopAgent

use of io.nuls.consensus.poc.protocol.entity.StopAgent in project nuls by nuls-io.

the class PocConsensusResource method getStopAgentFee.

@GET
@Path("/agent/stop/fee")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "get the fee of stop agent! 获取停止节点的手续费", notes = "返回停止节点交易手续费")
@ApiResponses(value = { @ApiResponse(code = 200, message = "success", response = String.class) })
public RpcClientResult getStopAgentFee(@ApiParam(name = "address", value = "创建节点的账户地址", required = true) @QueryParam("address") String address) throws NulsException, IOException {
    AssertUtil.canNotEmpty(address, "address can not be null");
    if (!AddressTool.validAddress(address)) {
        return Result.getFailed(KernelErrorCode.PARAMETER_ERROR).toRpcClientResult();
    }
    Account account = accountService.getAccount(address).getData();
    if (null == account) {
        return Result.getFailed(AccountErrorCode.ACCOUNT_NOT_EXIST).toRpcClientResult();
    }
    if (!account.isOk()) {
        return Result.getFailed(AccountErrorCode.IMPORTING_ACCOUNT).toRpcClientResult();
    }
    StopAgentTransaction tx = new StopAgentTransaction();
    StopAgent stopAgent = new StopAgent();
    stopAgent.setAddress(AddressTool.getAddress(address));
    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(), stopAgent.getAddress())) {
            agent = a;
            break;
        }
    }
    if (agent == null || agent.getDelHeight() > 0) {
        return Result.getFailed(PocConsensusErrorCode.AGENT_NOT_EXIST).toRpcClientResult();
    }
    NulsDigestData createTxHash = agent.getTxHash();
    stopAgent.setCreateTxHash(createTxHash);
    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));
    Na resultFee = TransactionFeeCalculator.getMaxFee(108 + tx.size());
    Map<String, Long> map = new HashMap<>();
    map.put("fee", fee.getValue());
    map.put("maxAmount", getMaxAmount(resultFee, address, tx));
    return Result.getSuccess().setData(map).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) StopAgent(io.nuls.consensus.poc.protocol.entity.StopAgent) StopAgentTransaction(io.nuls.consensus.poc.protocol.tx.StopAgentTransaction)

Example 2 with StopAgent

use of io.nuls.consensus.poc.protocol.entity.StopAgent 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 3 with StopAgent

use of io.nuls.consensus.poc.protocol.entity.StopAgent in project nuls by nuls-io.

the class PocConsensusResource method getStopMultiAgentFee.

@GET
@Path("/multiAccount/agent/stop/fee")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "get the fee of stop agent! 获取停止节点的手续费", notes = "返回停止节点交易手续费")
@ApiResponses(value = { @ApiResponse(code = 200, message = "success", response = String.class) })
public RpcClientResult getStopMultiAgentFee(@ApiParam(name = "address", value = "创建节点的账户地址", required = true) @QueryParam("address") String address) throws NulsException, IOException {
    AssertUtil.canNotEmpty(address, "address can not be null");
    if (!AddressTool.validAddress(address)) {
        return Result.getFailed(KernelErrorCode.PARAMETER_ERROR).toRpcClientResult();
    }
    StopAgentTransaction tx = new StopAgentTransaction();
    StopAgent stopAgent = new StopAgent();
    stopAgent.setAddress(AddressTool.getAddress(address));
    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(), stopAgent.getAddress())) {
            agent = a;
            break;
        }
    }
    if (agent == null || agent.getDelHeight() > 0) {
        return Result.getFailed(PocConsensusErrorCode.AGENT_NOT_EXIST).toRpcClientResult();
    }
    NulsDigestData createTxHash = agent.getTxHash();
    stopAgent.setCreateTxHash(createTxHash);
    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));
    Na resultFee = TransactionFeeCalculator.getMaxFee(108 + tx.size());
    Result rs = accountLedgerService.getMultiMaxAmountOfOnce(AddressTool.getAddress(address), tx, TransactionFeeCalculator.OTHER_PRICE_PRE_1024_BYTES, 0);
    Map<String, Long> map = new HashMap<>();
    Long maxAmount = null;
    if (rs.isSuccess()) {
        maxAmount = ((Na) rs.getData()).getValue();
    }
    map.put("fee", resultFee.getValue());
    map.put("maxAmount", maxAmount);
    rs.setData(map);
    return Result.getSuccess().setData(rs).toRpcClientResult();
}
Also used : StopAgent(io.nuls.consensus.poc.protocol.entity.StopAgent) Agent(io.nuls.consensus.poc.protocol.entity.Agent) StopAgent(io.nuls.consensus.poc.protocol.entity.StopAgent) StopAgentTransaction(io.nuls.consensus.poc.protocol.tx.StopAgentTransaction) CoinDataResult(io.nuls.account.ledger.model.CoinDataResult)

Example 4 with StopAgent

use of io.nuls.consensus.poc.protocol.entity.StopAgent 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

Agent (io.nuls.consensus.poc.protocol.entity.Agent)4 StopAgent (io.nuls.consensus.poc.protocol.entity.StopAgent)4 StopAgentTransaction (io.nuls.consensus.poc.protocol.tx.StopAgentTransaction)4 Account (io.nuls.account.model.Account)3 MultiSigAccount (io.nuls.account.model.MultiSigAccount)3 CoinDataResult (io.nuls.account.ledger.model.CoinDataResult)2 NulsRuntimeException (io.nuls.kernel.exception.NulsRuntimeException)2