Search in sources :

Example 1 with StopAgentTransaction

use of io.nuls.consensus.poc.protocol.tx.StopAgentTransaction in project nuls by nuls-io.

the class DepositTxProcessor method conflictDetect.

/**
 * 冲突检测,检测如果传入的交易列表中有相冲突的交易,则返回失败,写明失败原因及所有的应该舍弃的交易列表
 * 本方法不检查双花冲突,双花由账本接口实现
 * <p>
 * Conflict detection, which detects conflicting transactions in the incoming transaction list, returns failure,
 * indicating the cause of failure and all the list of trades that should be discarded.
 * This method does not check the double flower conflict, the double flower is realized by the accounting interface.
 *
 * @param txList 需要检查的交易列表/A list of transactions to be checked.
 * @return 操作结果:成功则返回successResult,失败时,data中返回丢弃列表,msg中返回冲突原因
 * Operation result: success returns successResult. When failure, data returns the discard list, and MSG returns the cause of conflict.
 */
@Override
public ValidateResult conflictDetect(List<Transaction> txList) {
    if (null == txList || txList.isEmpty()) {
        return ValidateResult.getSuccessResult();
    }
    Set<NulsDigestData> outAgentHash = new HashSet<>();
    Map<NulsDigestData, Na> naMap = new HashMap<>();
    List<DepositTransaction> dTxList = new ArrayList<>();
    for (Transaction transaction : txList) {
        switch(transaction.getType()) {
            case ConsensusConstant.TX_TYPE_STOP_AGENT:
                StopAgentTransaction stopAgentTransaction = (StopAgentTransaction) transaction;
                outAgentHash.add(stopAgentTransaction.getTxData().getCreateTxHash());
                break;
            case ConsensusConstant.TX_TYPE_JOIN_CONSENSUS:
                DepositTransaction depositTransaction = (DepositTransaction) transaction;
                Na na = naMap.get(depositTransaction.getTxData().getAgentHash());
                if (null == na) {
                    na = getAgentTotalDeposit(depositTransaction.getTxData().getAgentHash());
                }
                if (na == null) {
                    na = depositTransaction.getTxData().getDeposit();
                } else {
                    na = na.add(depositTransaction.getTxData().getDeposit());
                }
                if (na.isGreaterThan(PocConsensusProtocolConstant.SUM_OF_DEPOSIT_OF_AGENT_UPPER_LIMIT)) {
                    ValidateResult validateResult = ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.DEPOSIT_TOO_MUCH);
                    validateResult.setData(transaction);
                    return validateResult;
                } else {
                    naMap.put(depositTransaction.getTxData().getAgentHash(), na);
                }
                dTxList.add(depositTransaction);
                break;
            case ConsensusConstant.TX_TYPE_RED_PUNISH:
                RedPunishTransaction redPunishTransaction = (RedPunishTransaction) transaction;
                RedPunishData redPunishData = redPunishTransaction.getTxData();
                AgentPo agent = this.getAgentByAddress(redPunishData.getAddress());
                if (null != agent) {
                    outAgentHash.add(agent.getHash());
                }
                break;
            default:
                continue;
        }
    }
    if (dTxList.isEmpty() || outAgentHash.isEmpty()) {
        return ValidateResult.getSuccessResult();
    }
    for (DepositTransaction depositTransaction : dTxList) {
        if (outAgentHash.contains(depositTransaction.getTxData().getAgentHash())) {
            ValidateResult validateResult = ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.AGENT_STOPPED);
            validateResult.setData(depositTransaction);
            return validateResult;
        }
    }
    return ValidateResult.getSuccessResult();
}
Also used : DepositTransaction(io.nuls.consensus.poc.protocol.tx.DepositTransaction) RedPunishTransaction(io.nuls.consensus.poc.protocol.tx.RedPunishTransaction) ValidateResult(io.nuls.kernel.validate.ValidateResult) StopAgentTransaction(io.nuls.consensus.poc.protocol.tx.StopAgentTransaction) DepositTransaction(io.nuls.consensus.poc.protocol.tx.DepositTransaction) RedPunishTransaction(io.nuls.consensus.poc.protocol.tx.RedPunishTransaction) RedPunishData(io.nuls.consensus.poc.protocol.entity.RedPunishData) StopAgentTransaction(io.nuls.consensus.poc.protocol.tx.StopAgentTransaction) AgentPo(io.nuls.consensus.poc.storage.po.AgentPo)

Example 2 with StopAgentTransaction

use of io.nuls.consensus.poc.protocol.tx.StopAgentTransaction in project nuls by nuls-io.

the class StopAgentTxProcessor method conflictDetect.

@Override
public ValidateResult conflictDetect(List<Transaction> txList) {
    if (txList == null || txList.isEmpty()) {
        return ValidateResult.getSuccessResult();
    }
    Set<NulsDigestData> hashSet = new HashSet<>();
    Set<String> addressSet = new HashSet<>();
    for (Transaction tx : txList) {
        if (tx.getType() == ConsensusConstant.TX_TYPE_RED_PUNISH) {
            RedPunishTransaction transaction = (RedPunishTransaction) tx;
            addressSet.add(AddressTool.getStringAddressByBytes(transaction.getTxData().getAddress()));
        }
    }
    for (Transaction tx : txList) {
        if (tx.getType() == ConsensusConstant.TX_TYPE_STOP_AGENT) {
            StopAgentTransaction transaction = (StopAgentTransaction) tx;
            if (!hashSet.add(transaction.getTxData().getCreateTxHash())) {
                ValidateResult result = ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TRANSACTION_REPEATED);
                result.setData(transaction);
                return result;
            }
            if (transaction.getTxData().getAddress() == null) {
                CreateAgentTransaction agentTransaction = (CreateAgentTransaction) ledgerService.getTx(transaction.getTxData().getCreateTxHash());
                if (null == agentTransaction) {
                    ValidateResult result = ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.AGENT_NOT_EXIST);
                    result.setData(transaction);
                    return result;
                }
                transaction.getTxData().setAddress(agentTransaction.getTxData().getAgentAddress());
            }
            AgentPo po = agentStorageService.get(transaction.getTxData().getCreateTxHash());
            if (null == po || po.getDelHeight() > 0) {
                ValidateResult result = ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.AGENT_STOPPED);
                result.setData(transaction);
                return result;
            }
            if (addressSet.contains(AddressTool.getStringAddressByBytes(transaction.getTxData().getAddress()))) {
                ValidateResult result = ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.AGENT_STOPPED);
                result.setData(transaction);
                return result;
            }
        }
    }
    return ValidateResult.getSuccessResult();
}
Also used : StopAgentTransaction(io.nuls.consensus.poc.protocol.tx.StopAgentTransaction) CreateAgentTransaction(io.nuls.consensus.poc.protocol.tx.CreateAgentTransaction) RedPunishTransaction(io.nuls.consensus.poc.protocol.tx.RedPunishTransaction) Transaction(io.nuls.kernel.model.Transaction) RedPunishTransaction(io.nuls.consensus.poc.protocol.tx.RedPunishTransaction) ValidateResult(io.nuls.kernel.validate.ValidateResult) NulsDigestData(io.nuls.kernel.model.NulsDigestData) CreateAgentTransaction(io.nuls.consensus.poc.protocol.tx.CreateAgentTransaction) StopAgentTransaction(io.nuls.consensus.poc.protocol.tx.StopAgentTransaction) HashSet(java.util.HashSet) AgentPo(io.nuls.consensus.poc.storage.po.AgentPo)

Example 3 with StopAgentTransaction

use of io.nuls.consensus.poc.protocol.tx.StopAgentTransaction 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 4 with StopAgentTransaction

use of io.nuls.consensus.poc.protocol.tx.StopAgentTransaction 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 5 with StopAgentTransaction

use of io.nuls.consensus.poc.protocol.tx.StopAgentTransaction 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)

Aggregations

StopAgentTransaction (io.nuls.consensus.poc.protocol.tx.StopAgentTransaction)7 Agent (io.nuls.consensus.poc.protocol.entity.Agent)4 StopAgent (io.nuls.consensus.poc.protocol.entity.StopAgent)4 Account (io.nuls.account.model.Account)3 MultiSigAccount (io.nuls.account.model.MultiSigAccount)3 RedPunishTransaction (io.nuls.consensus.poc.protocol.tx.RedPunishTransaction)3 AgentPo (io.nuls.consensus.poc.storage.po.AgentPo)3 CoinDataResult (io.nuls.account.ledger.model.CoinDataResult)2 DepositTransaction (io.nuls.consensus.poc.protocol.tx.DepositTransaction)2 NulsRuntimeException (io.nuls.kernel.exception.NulsRuntimeException)2 NulsDigestData (io.nuls.kernel.model.NulsDigestData)2 Transaction (io.nuls.kernel.model.Transaction)2 ValidateResult (io.nuls.kernel.validate.ValidateResult)2 HashSet (java.util.HashSet)2 RedPunishData (io.nuls.consensus.poc.protocol.entity.RedPunishData)1 CancelDepositTransaction (io.nuls.consensus.poc.protocol.tx.CancelDepositTransaction)1 CreateAgentTransaction (io.nuls.consensus.poc.protocol.tx.CreateAgentTransaction)1 DepositPo (io.nuls.consensus.poc.storage.po.DepositPo)1