Search in sources :

Example 6 with StopAgentTransaction

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

Example 7 with StopAgentTransaction

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

the class CancelDepositTxProcessor method conflictDetect.

@Override
public ValidateResult conflictDetect(List<Transaction> txList) {
    if (txList == null || txList.size() <= 1) {
        return ValidateResult.getSuccessResult();
    }
    Set<NulsDigestData> hashSet = new HashSet<>();
    Set<NulsDigestData> agentHashSet = 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()));
        } else if (tx.getType() == ConsensusConstant.TX_TYPE_STOP_AGENT) {
            StopAgentTransaction transaction = (StopAgentTransaction) tx;
            agentHashSet.add(transaction.getTxData().getCreateTxHash());
        }
    }
    for (Transaction tx : txList) {
        if (tx.getType() == ConsensusConstant.TX_TYPE_CANCEL_DEPOSIT) {
            CancelDepositTransaction transaction = (CancelDepositTransaction) tx;
            if (!hashSet.add(transaction.getTxData().getJoinTxHash())) {
                return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TRANSACTION_REPEATED);
            }
            if (agentHashSet.contains(transaction.getTxData().getJoinTxHash())) {
                return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.AGENT_STOPPED);
            }
            DepositPo depositPo = depositStorageService.get(transaction.getTxData().getJoinTxHash());
            if (depositPo.getDelHeight() > 0) {
                return ValidateResult.getFailedResult(this.getClass().getName(), TransactionErrorCode.TRANSACTION_REPEATED);
            }
            AgentPo agentPo = agentStorageService.get(depositPo.getAgentHash());
            if (null == agentPo || agentPo.getDelHeight() > 0) {
                return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.AGENT_NOT_EXIST);
            }
            if (addressSet.contains(AddressTool.getStringAddressByBytes(agentPo.getAgentAddress()))) {
                return ValidateResult.getFailedResult(this.getClass().getName(), PocConsensusErrorCode.AGENT_PUNISHED);
            }
        }
    }
    return ValidateResult.getSuccessResult();
}
Also used : DepositPo(io.nuls.consensus.poc.storage.po.DepositPo) StopAgentTransaction(io.nuls.consensus.poc.protocol.tx.StopAgentTransaction) DepositTransaction(io.nuls.consensus.poc.protocol.tx.DepositTransaction) RedPunishTransaction(io.nuls.consensus.poc.protocol.tx.RedPunishTransaction) Transaction(io.nuls.kernel.model.Transaction) CancelDepositTransaction(io.nuls.consensus.poc.protocol.tx.CancelDepositTransaction) RedPunishTransaction(io.nuls.consensus.poc.protocol.tx.RedPunishTransaction) CancelDepositTransaction(io.nuls.consensus.poc.protocol.tx.CancelDepositTransaction) NulsDigestData(io.nuls.kernel.model.NulsDigestData) StopAgentTransaction(io.nuls.consensus.poc.protocol.tx.StopAgentTransaction) HashSet(java.util.HashSet) AgentPo(io.nuls.consensus.poc.storage.po.AgentPo)

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