Search in sources :

Example 1 with CancelDepositTransaction

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

the class PocConsensusResource method getWithdrawFee.

@GET
@Path("/withdraw/fee")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "get the fee of cancel deposit! 获取撤销委托的手续费", notes = "返回撤销委托交易手续费")
@ApiResponses(value = { @ApiResponse(code = 200, message = "success", response = String.class) })
public RpcClientResult getWithdrawFee(@ApiParam(name = "address", value = "委托账户地址", required = true) @QueryParam("address") String address, @ApiParam(name = "depositTxHash", value = "委托交易摘要", required = true) @QueryParam("depositTxHash") String depositTxHash) throws NulsException, IOException {
    AssertUtil.canNotEmpty(depositTxHash);
    if (!NulsDigestData.validHash(depositTxHash)) {
        return Result.getFailed(KernelErrorCode.PARAMETER_ERROR).toRpcClientResult();
    }
    AssertUtil.canNotEmpty(address);
    if (!AddressTool.validAddress(address)) {
        return Result.getFailed(AccountErrorCode.ADDRESS_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();
    }
    CancelDepositTransaction tx = new CancelDepositTransaction();
    CancelDeposit cancelDeposit = new CancelDeposit();
    NulsDigestData hash = NulsDigestData.fromDigestHex(depositTxHash);
    DepositTransaction depositTransaction = (DepositTransaction) ledgerService.getTx(hash);
    if (null == depositTransaction) {
        return Result.getFailed(TransactionErrorCode.TX_NOT_EXIST).toRpcClientResult();
    }
    cancelDeposit.setAddress(account.getAddress().getAddressBytes());
    cancelDeposit.setJoinTxHash(hash);
    tx.setTxData(cancelDeposit);
    CoinData coinData = new CoinData();
    List<Coin> toList = new ArrayList<>();
    toList.add(new Coin(cancelDeposit.getAddress(), depositTransaction.getTxData().getDeposit(), 0));
    coinData.setTo(toList);
    List<Coin> fromList = new ArrayList<>();
    for (int index = 0; index < depositTransaction.getCoinData().getTo().size(); index++) {
        Coin coin = depositTransaction.getCoinData().getTo().get(index);
        if (coin.getLockTime() == -1L && coin.getNa().equals(depositTransaction.getTxData().getDeposit())) {
            coin.setOwner(ArraysTool.concatenate(hash.serialize(), new VarInt(index).encode()));
            fromList.add(coin);
            break;
        }
    }
    if (fromList.isEmpty()) {
        return Result.getFailed(KernelErrorCode.DATA_ERROR).toRpcClientResult();
    }
    coinData.setFrom(fromList);
    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, account.getAddress().getBase58(), tx));
    return Result.getSuccess().setData(map).toRpcClientResult();
}
Also used : CancelDeposit(io.nuls.consensus.poc.protocol.entity.CancelDeposit) Account(io.nuls.account.model.Account) MultiSigAccount(io.nuls.account.model.MultiSigAccount) CancelDepositTransaction(io.nuls.consensus.poc.protocol.tx.CancelDepositTransaction) DepositTransaction(io.nuls.consensus.poc.protocol.tx.DepositTransaction) VarInt(io.nuls.kernel.utils.VarInt) CancelDepositTransaction(io.nuls.consensus.poc.protocol.tx.CancelDepositTransaction)

Example 2 with CancelDepositTransaction

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

the class PocConsensusResource method withdraw.

@POST
@Path("/withdraw")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "退出共识 [3.6.11]", notes = "返回退出成功的交易hash")
@ApiResponses(value = { @ApiResponse(code = 200, message = "success", response = String.class) })
public RpcClientResult withdraw(@ApiParam(name = "form", value = "退出共识表单数据", required = true) WithdrawForm form) throws NulsException, IOException {
    AssertUtil.canNotEmpty(form);
    AssertUtil.canNotEmpty(form.getTxHash());
    if (!NulsDigestData.validHash(form.getTxHash())) {
        return Result.getFailed(KernelErrorCode.PARAMETER_ERROR).toRpcClientResult();
    }
    AssertUtil.canNotEmpty(form.getAddress());
    if (!AddressTool.validAddress(form.getAddress())) {
        return Result.getFailed(AccountErrorCode.ADDRESS_ERROR).toRpcClientResult();
    }
    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();
        }
    }
    CancelDepositTransaction tx = new CancelDepositTransaction();
    CancelDeposit cancelDeposit = new CancelDeposit();
    NulsDigestData hash = NulsDigestData.fromDigestHex(form.getTxHash());
    DepositTransaction depositTransaction = null;
    try {
        depositTransaction = (DepositTransaction) ledgerService.getTx(hash);
    } catch (Exception e) {
        return Result.getFailed(KernelErrorCode.PARAMETER_ERROR).toRpcClientResult();
    }
    if (null == depositTransaction) {
        return Result.getFailed(TransactionErrorCode.TX_NOT_EXIST).toRpcClientResult();
    }
    cancelDeposit.setAddress(AddressTool.getAddress(form.getAddress()));
    cancelDeposit.setJoinTxHash(hash);
    tx.setTxData(cancelDeposit);
    CoinData coinData = new CoinData();
    List<Coin> toList = new ArrayList<>();
    toList.add(new Coin(cancelDeposit.getAddress(), depositTransaction.getTxData().getDeposit(), 0));
    coinData.setTo(toList);
    List<Coin> fromList = new ArrayList<>();
    for (int index = 0; index < depositTransaction.getCoinData().getTo().size(); index++) {
        Coin coin = depositTransaction.getCoinData().getTo().get(index);
        if (coin.getLockTime() == -1L && coin.getNa().equals(depositTransaction.getTxData().getDeposit())) {
            coin.setOwner(ArraysTool.concatenate(hash.serialize(), new VarInt(index).encode()));
            fromList.add(coin);
            break;
        }
    }
    if (fromList.isEmpty()) {
        return Result.getFailed(KernelErrorCode.DATA_ERROR).toRpcClientResult();
    }
    coinData.setFrom(fromList);
    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 : CancelDeposit(io.nuls.consensus.poc.protocol.entity.CancelDeposit) Account(io.nuls.account.model.Account) MultiSigAccount(io.nuls.account.model.MultiSigAccount) CancelDepositTransaction(io.nuls.consensus.poc.protocol.tx.CancelDepositTransaction) DepositTransaction(io.nuls.consensus.poc.protocol.tx.DepositTransaction) VarInt(io.nuls.kernel.utils.VarInt) NulsException(io.nuls.kernel.exception.NulsException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) IOException(java.io.IOException) CancelDepositTransaction(io.nuls.consensus.poc.protocol.tx.CancelDepositTransaction)

Example 3 with CancelDepositTransaction

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

the class PocConsensusResource method createWithdrawMutil.

@POST
@Path("/multiAccount/mutilWithdraw")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "多签账户退出共识", notes = "返回退出成功的交易hash")
@ApiResponses(value = { @ApiResponse(code = 200, message = "success", response = String.class) })
public RpcClientResult createWithdrawMutil(@ApiParam(name = "form", value = "多签退出共识表单数据", required = true) CreateMultiWithdrawForm form) throws Exception {
    AssertUtil.canNotEmpty(form);
    AssertUtil.canNotEmpty(form.getTxHash());
    AssertUtil.canNotEmpty(form.getAddress());
    AssertUtil.canNotEmpty(form.getSignAddress());
    if (!NulsDigestData.validHash(form.getTxHash())) {
        return Result.getFailed(KernelErrorCode.PARAMETER_ERROR).toRpcClientResult();
    }
    if (!AddressTool.validAddress(form.getAddress()) || !AddressTool.validAddress(form.getSignAddress())) {
        return Result.getFailed(AccountErrorCode.ADDRESS_ERROR).toRpcClientResult();
    }
    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.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();
    }
    CancelDepositTransaction tx = new CancelDepositTransaction();
    TransactionSignature transactionSignature = new TransactionSignature();
    List<Script> scripts = new ArrayList<>();
    CancelDeposit cancelDeposit = new CancelDeposit();
    NulsDigestData hash = NulsDigestData.fromDigestHex(form.getTxHash());
    DepositTransaction depositTransaction = null;
    try {
        depositTransaction = (DepositTransaction) ledgerService.getTx(hash);
    } catch (Exception e) {
        return Result.getFailed(KernelErrorCode.PARAMETER_ERROR).toRpcClientResult();
    }
    if (null == depositTransaction) {
        return Result.getFailed(TransactionErrorCode.TX_NOT_EXIST).toRpcClientResult();
    }
    // Create unlock script
    cancelDeposit.setAddress(AddressTool.getAddress(form.getAddress()));
    cancelDeposit.setJoinTxHash(hash);
    tx.setTxData(cancelDeposit);
    CoinData coinData = new CoinData();
    List<Coin> toList = new ArrayList<>();
    if (cancelDeposit.getAddress()[2] == NulsContext.P2SH_ADDRESS_TYPE) {
        Script scriptPubkey = SignatureUtil.createOutputScript(cancelDeposit.getAddress());
        toList.add(new Coin(scriptPubkey.getProgram(), depositTransaction.getTxData().getDeposit(), PocConsensusConstant.CONSENSUS_LOCK_TIME));
    } else {
        toList.add(new Coin(cancelDeposit.getAddress(), depositTransaction.getTxData().getDeposit(), 0));
    }
    coinData.setTo(toList);
    List<Coin> fromList = new ArrayList<>();
    for (int index = 0; index < depositTransaction.getCoinData().getTo().size(); index++) {
        Coin coin = depositTransaction.getCoinData().getTo().get(index);
        if (coin.getLockTime() == -1L && coin.getNa().equals(depositTransaction.getTxData().getDeposit())) {
            coin.setOwner(ArraysTool.concatenate(hash.serialize(), new VarInt(index).encode()));
            fromList.add(coin);
            break;
        }
    }
    if (fromList.isEmpty()) {
        return Result.getFailed(KernelErrorCode.DATA_ERROR).toRpcClientResult();
    }
    coinData.setFrom(fromList);
    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) CancelDeposit(io.nuls.consensus.poc.protocol.entity.CancelDeposit) Account(io.nuls.account.model.Account) MultiSigAccount(io.nuls.account.model.MultiSigAccount) CancelDepositTransaction(io.nuls.consensus.poc.protocol.tx.CancelDepositTransaction) DepositTransaction(io.nuls.consensus.poc.protocol.tx.DepositTransaction) VarInt(io.nuls.kernel.utils.VarInt) NulsException(io.nuls.kernel.exception.NulsException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) IOException(java.io.IOException) CoinDataResult(io.nuls.account.ledger.model.CoinDataResult) CancelDepositTransaction(io.nuls.consensus.poc.protocol.tx.CancelDepositTransaction)

Example 4 with CancelDepositTransaction

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

the class CancelDepositTxProcessor method onRollback.

@Override
public Result onRollback(CancelDepositTransaction tx, Object secondaryData) {
    DepositTransaction transaction = (DepositTransaction) ledgerService.getTx(tx.getTxData().getJoinTxHash());
    if (null == transaction) {
        return Result.getFailed(TransactionErrorCode.TX_NOT_EXIST);
    }
    DepositPo po = depositStorageService.get(tx.getTxData().getJoinTxHash());
    if (null == po) {
        return Result.getFailed(KernelErrorCode.DATA_NOT_FOUND);
    }
    if (po.getDelHeight() != tx.getBlockHeight()) {
        return Result.getFailed(PocConsensusErrorCode.DEPOSIT_NEVER_CANCELED);
    }
    po.setDelHeight(-1L);
    boolean b = depositStorageService.save(po);
    if (b) {
        return Result.getSuccess();
    }
    return Result.getFailed(TransactionErrorCode.ROLLBACK_TRANSACTION_FAILED);
}
Also used : DepositTransaction(io.nuls.consensus.poc.protocol.tx.DepositTransaction) CancelDepositTransaction(io.nuls.consensus.poc.protocol.tx.CancelDepositTransaction) DepositPo(io.nuls.consensus.poc.storage.po.DepositPo)

Example 5 with CancelDepositTransaction

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

the class CancelDepositTxProcessor method onCommit.

@Override
public Result onCommit(CancelDepositTransaction tx, Object secondaryData) {
    DepositTransaction transaction = (DepositTransaction) ledgerService.getTx(tx.getTxData().getJoinTxHash());
    if (null == transaction) {
        return Result.getFailed(TransactionErrorCode.TX_NOT_EXIST);
    }
    DepositPo po = depositStorageService.get(tx.getTxData().getJoinTxHash());
    if (null == po) {
        return Result.getFailed(KernelErrorCode.DATA_NOT_FOUND);
    }
    tx.getTxData().setAddress(po.getAddress());
    if (po.getDelHeight() > 0L) {
        return Result.getFailed(PocConsensusErrorCode.DEPOSIT_WAS_CANCELED);
    }
    po.setDelHeight(tx.getBlockHeight());
    boolean b = depositStorageService.save(po);
    if (b) {
        return Result.getSuccess();
    }
    return Result.getFailed(TransactionErrorCode.SAVE_TX_ERROR);
}
Also used : DepositTransaction(io.nuls.consensus.poc.protocol.tx.DepositTransaction) CancelDepositTransaction(io.nuls.consensus.poc.protocol.tx.CancelDepositTransaction) DepositPo(io.nuls.consensus.poc.storage.po.DepositPo)

Aggregations

CancelDepositTransaction (io.nuls.consensus.poc.protocol.tx.CancelDepositTransaction)6 DepositTransaction (io.nuls.consensus.poc.protocol.tx.DepositTransaction)6 Account (io.nuls.account.model.Account)3 MultiSigAccount (io.nuls.account.model.MultiSigAccount)3 CancelDeposit (io.nuls.consensus.poc.protocol.entity.CancelDeposit)3 DepositPo (io.nuls.consensus.poc.storage.po.DepositPo)3 VarInt (io.nuls.kernel.utils.VarInt)3 NulsException (io.nuls.kernel.exception.NulsException)2 NulsRuntimeException (io.nuls.kernel.exception.NulsRuntimeException)2 IOException (java.io.IOException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 CoinDataResult (io.nuls.account.ledger.model.CoinDataResult)1 RedPunishTransaction (io.nuls.consensus.poc.protocol.tx.RedPunishTransaction)1 StopAgentTransaction (io.nuls.consensus.poc.protocol.tx.StopAgentTransaction)1 AgentPo (io.nuls.consensus.poc.storage.po.AgentPo)1 NulsDigestData (io.nuls.kernel.model.NulsDigestData)1 Transaction (io.nuls.kernel.model.Transaction)1 HashSet (java.util.HashSet)1