Search in sources :

Example 6 with CreateAgentTransaction

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

the class PocConsensusResource method createMutilAgent.

@POST
@Path("/multiAccount/createMultiAgent")
@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 createMutilAgent(@ApiParam(name = "form", value = "多签地址创建节点表单数据", required = true) CreateMultiAgentForm form) throws Exception {
    if (NulsContext.MAIN_NET_VERSION <= 1) {
        return Result.getFailed(KernelErrorCode.VERSION_TOO_LOW).toRpcClientResult();
    }
    AssertUtil.canNotEmpty(form);
    AssertUtil.canNotEmpty(form.getAgentAddress(), "agent address can not be null");
    AssertUtil.canNotEmpty(form.getSignAddress(), "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()) || !AddressTool.validAddress(form.getSignAddress())) {
        throw new NulsRuntimeException(AccountErrorCode.ADDRESS_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();
        }
    }
    Result<MultiSigAccount> sigAccountResult = accountService.getMultiSigAccount(form.getAgentAddress());
    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();
    }
    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()));
    }
    TransactionSignature transactionSignature = new TransactionSignature();
    List<Script> scripts = new ArrayList<>();
    agent.setDeposit(Na.valueOf(form.getDeposit()));
    agent.setCommissionRate(form.getCommissionRate());
    tx.setTxData(agent);
    CoinData coinData = new CoinData();
    List<Coin> toList = new ArrayList<>();
    if (agent.getAgentAddress()[2] == NulsContext.P2SH_ADDRESS_TYPE) {
        Script scriptPubkey = SignatureUtil.createOutputScript(agent.getAgentAddress());
        toList.add(new Coin(scriptPubkey.getProgram(), agent.getDeposit(), PocConsensusConstant.CONSENSUS_LOCK_TIME));
    } else {
        toList.add(new Coin(agent.getAgentAddress(), agent.getDeposit(), PocConsensusConstant.CONSENSUS_LOCK_TIME));
    }
    coinData.setTo(toList);
    tx.setCoinData(coinData);
    // 交易签名的长度为m*单个签名长度+赎回脚本长度
    int scriptSignLenth = redeemScript.getProgram().length + ((int) multiSigAccount.getM()) * 72;
    CoinDataResult result = accountLedgerService.getMutilCoinData(agent.getAgentAddress(), agent.getDeposit(), tx.size() + scriptSignLenth, TransactionFeeCalculator.OTHER_PRICE_PRE_1024_BYTES);
    if (null != result) {
        if (result.isEnough()) {
            tx.getCoinData().setFrom(result.getCoinList());
            if (null != result.getChange()) {
                tx.getCoinData().getTo().add(result.getChange());
            }
        } else {
            return Result.getFailed(TransactionErrorCode.INSUFFICIENT_BALANCE).toRpcClientResult();
        }
    }
    tx.setHash(NulsDigestData.calcDigestData(tx.serializeForHash()));
    // 将赎回脚本先存储在签名脚本中
    scripts.add(redeemScript);
    transactionSignature.setScripts(scripts);
    Result finalResult = accountLedgerService.txMultiProcess(tx, transactionSignature, account, form.getPassword());
    if (finalResult.isSuccess()) {
        Map<String, String> valueMap = new HashMap<>();
        valueMap.put("txData", (String) finalResult.getData());
        return Result.getSuccess().setData(valueMap).toRpcClientResult();
    }
    return finalResult.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) CoinDataResult(io.nuls.account.ledger.model.CoinDataResult) CreateAgentTransaction(io.nuls.consensus.poc.protocol.tx.CreateAgentTransaction) CoinDataResult(io.nuls.account.ledger.model.CoinDataResult)

Example 7 with CreateAgentTransaction

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

the class PocConsensusResource method getCreateAgentFee.

@GET
@Path("/agent/fee")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "get the fee of create agent! 获取创建共识(代理)节点的手续费", notes = "返回创建的节点成功的交易手续费")
@ApiResponses(value = { @ApiResponse(code = 200, message = "success", response = String.class) })
public RpcClientResult getCreateAgentFee(@BeanParam() GetCreateAgentFeeForm 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 (StringUtils.isBlank(form.getRewardAddress())) {
        form.setRewardAddress(form.getAgentAddress());
    }
    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();
    }
    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<>();
    if (agent.getAgentAddress()[2] == 3) {
        Script scriptPubkey = SignatureUtil.createOutputScript(agent.getAgentAddress());
        toList.add(new Coin(scriptPubkey.getProgram(), agent.getDeposit(), -1));
    } else {
        toList.add(new Coin(agent.getAgentAddress(), agent.getDeposit(), -1));
    }
    coinData.setTo(toList);
    tx.setCoinData(coinData);
    CoinDataResult result = accountLedgerService.getCoinData(agent.getAgentAddress(), agent.getDeposit(), tx.size() - P2PHKSignature.SERIALIZE_LENGTH, TransactionFeeCalculator.OTHER_PRICE_PRE_1024_BYTES);
    tx.getCoinData().setFrom(result.getCoinList());
    if (null != result.getChange()) {
        tx.getCoinData().getTo().add(result.getChange());
    }
    Na fee = TransactionFeeCalculator.getMaxFee(108 + tx.size());
    Map<String, Long> map = new HashMap<>();
    map.put("fee", fee.getValue());
    map.put("maxAmount", getMaxAmount(fee, form.getAgentAddress(), 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) CreateAgentTransaction(io.nuls.consensus.poc.protocol.tx.CreateAgentTransaction) CoinDataResult(io.nuls.account.ledger.model.CoinDataResult)

Example 8 with CreateAgentTransaction

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

the class PocConsensusResource method getCreateMultiAgentFee.

@GET
@Path("/multiAccount/Agent/fee")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "get the fee of create agent! 获取创建共识(代理)节点的手续费", notes = "返回创建的节点成功的交易手续费")
@ApiResponses(value = { @ApiResponse(code = 200, message = "success", response = String.class) })
public RpcClientResult getCreateMultiAgentFee(@BeanParam() GetCreateAgentFeeForm form) throws Exception {
    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 (StringUtils.isBlank(form.getRewardAddress())) {
        form.setRewardAddress(form.getAgentAddress());
    }
    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);
    Result<MultiSigAccount> sigAccountResult = accountService.getMultiSigAccount(form.getAgentAddress());
    MultiSigAccount multiSigAccount = sigAccountResult.getData();
    Script redeemScript = accountLedgerService.getRedeemScript(multiSigAccount);
    if (redeemScript == null) {
        return Result.getFailed(AccountErrorCode.ACCOUNT_NOT_EXIST).toRpcClientResult();
    }
    CoinData coinData = new CoinData();
    List<Coin> toList = new ArrayList<>();
    if (agent.getAgentAddress()[2] == 3) {
        Script scriptPubkey = SignatureUtil.createOutputScript(agent.getAgentAddress());
        toList.add(new Coin(scriptPubkey.getProgram(), agent.getDeposit(), -1));
    } else {
        toList.add(new Coin(agent.getAgentAddress(), agent.getDeposit(), -1));
    }
    coinData.setTo(toList);
    tx.setCoinData(coinData);
    CoinDataResult result = accountLedgerService.getMutilCoinData(agent.getAgentAddress(), agent.getDeposit(), tx.size(), TransactionFeeCalculator.OTHER_PRICE_PRE_1024_BYTES);
    tx.getCoinData().setFrom(result.getCoinList());
    if (null != result.getChange()) {
        tx.getCoinData().getTo().add(result.getChange());
    }
    Na fee = TransactionFeeCalculator.getMaxFee(108 + tx.size());
    // 交易签名的长度为m*单个签名长度+赎回脚本长度
    int scriptSignLenth = redeemScript.getProgram().length + ((int) multiSigAccount.getM()) * 72;
    Result rs = accountLedgerService.getMultiMaxAmountOfOnce(AddressTool.getAddress(form.getAgentAddress()), tx, TransactionFeeCalculator.OTHER_PRICE_PRE_1024_BYTES, scriptSignLenth);
    Map<String, Long> map = new HashMap<>();
    Long maxAmount = null;
    if (rs.isSuccess()) {
        maxAmount = ((Na) rs.getData()).getValue();
    }
    map.put("fee", fee.getValue());
    map.put("maxAmount", maxAmount);
    rs.setData(map);
    return Result.getSuccess().setData(rs).toRpcClientResult();
}
Also used : MultiSigAccount(io.nuls.account.model.MultiSigAccount) StopAgent(io.nuls.consensus.poc.protocol.entity.StopAgent) Agent(io.nuls.consensus.poc.protocol.entity.Agent) CoinDataResult(io.nuls.account.ledger.model.CoinDataResult) CreateAgentTransaction(io.nuls.consensus.poc.protocol.tx.CreateAgentTransaction) CoinDataResult(io.nuls.account.ledger.model.CoinDataResult)

Example 9 with CreateAgentTransaction

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

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

the class RegisterAgentProcessTest method newTx.

private CreateAgentTransaction newTx() {
    CreateAgentTransaction tx = new CreateAgentTransaction();
    Agent agent = new Agent();
    tx.setTxData(agent);
    byte[] address = AddressTool.getAddress(ecKey.getPubKey());
    byte[] address1 = AddressTool.getAddress(new ECKey().getPubKey());
    byte[] address2 = AddressTool.getAddress(new ECKey().getPubKey());
    agent.setRewardAddress(address);
    agent.setPackingAddress(address1);
    agent.setAgentAddress(address2);
    agent.setDeposit(PocConsensusProtocolConstant.AGENT_DEPOSIT_LOWER_LIMIT);
    tx.setTime(System.currentTimeMillis());
    return tx;
}
Also used : Agent(io.nuls.consensus.poc.protocol.entity.Agent) CreateAgentTransaction(io.nuls.consensus.poc.protocol.tx.CreateAgentTransaction) ECKey(io.nuls.core.tools.crypto.ECKey)

Aggregations

CreateAgentTransaction (io.nuls.consensus.poc.protocol.tx.CreateAgentTransaction)11 Agent (io.nuls.consensus.poc.protocol.entity.Agent)9 CoinDataResult (io.nuls.account.ledger.model.CoinDataResult)4 MultiSigAccount (io.nuls.account.model.MultiSigAccount)4 StopAgent (io.nuls.consensus.poc.protocol.entity.StopAgent)4 NulsRuntimeException (io.nuls.kernel.exception.NulsRuntimeException)4 Account (io.nuls.account.model.Account)3 Deposit (io.nuls.consensus.poc.protocol.entity.Deposit)3 DepositTransaction (io.nuls.consensus.poc.protocol.tx.DepositTransaction)3 ECKey (io.nuls.core.tools.crypto.ECKey)3 ValidateResult (io.nuls.kernel.validate.ValidateResult)3 RedPunishTransaction (io.nuls.consensus.poc.protocol.tx.RedPunishTransaction)2 Transaction (io.nuls.kernel.model.Transaction)2 HashSet (java.util.HashSet)2 BaseTest (io.nuls.consensus.poc.BaseTest)1 ChainContainer (io.nuls.consensus.poc.container.ChainContainer)1 RedPunishData (io.nuls.consensus.poc.protocol.entity.RedPunishData)1 StopAgentTransaction (io.nuls.consensus.poc.protocol.tx.StopAgentTransaction)1 AgentPo (io.nuls.consensus.poc.storage.po.AgentPo)1 CreateAgentTxValidator (io.nuls.consensus.poc.tx.validator.CreateAgentTxValidator)1