Search in sources :

Example 1 with AbstractCoinTransaction

use of io.nuls.ledger.entity.tx.AbstractCoinTransaction in project nuls by nuls-io.

the class PocConsensusServiceImpl method stopConsensus.

@Override
public Transaction stopConsensus(String address, String password, Map<String, Object> paramsMap) throws NulsException, IOException {
    AbstractCoinTransaction joinTx = null;
    if (null != paramsMap && StringUtils.isNotBlank((String) paramsMap.get("txHash"))) {
        PocJoinConsensusTransaction tx = (PocJoinConsensusTransaction) ledgerService.getTx(NulsDigestData.fromDigestHex((String) paramsMap.get("txHash")));
        joinTx = tx;
    } else {
        try {
            List<Transaction> txlist = this.ledgerService.getTxList(address, TransactionConstant.TX_TYPE_REGISTER_AGENT);
            if (null != txlist || !txlist.isEmpty()) {
                joinTx = (AbstractCoinTransaction) txlist.get(0);
            }
        } catch (Exception e) {
            Log.error(e);
        }
    }
    if (null == joinTx) {
        throw new NulsRuntimeException(ErrorCode.FAILED, "The related transaction is not exist!");
    }
    Account account = this.accountService.getAccount(address);
    if (null == account) {
        throw new NulsRuntimeException(ErrorCode.ACCOUNT_NOT_EXIST, "address:" + address.toString());
    }
    if (!account.validatePassword(password)) {
        throw new NulsRuntimeException(ErrorCode.PASSWORD_IS_WRONG);
    }
    TransactionEvent event = new TransactionEvent();
    CoinTransferData coinTransferData = new CoinTransferData(OperationType.UNLOCK, this.ledgerService.getTxFee(TransactionConstant.TX_TYPE_EXIT_CONSENSUS));
    coinTransferData.setTotalNa(Na.ZERO);
    PocExitConsensusTransaction tx = new PocExitConsensusTransaction(coinTransferData, password);
    tx.setTxData(joinTx.getHash());
    try {
        tx.setHash(NulsDigestData.calcDigestData(tx.serialize()));
    } catch (IOException e) {
        Log.error(e);
        throw new NulsRuntimeException(ErrorCode.HASH_ERROR, e);
    }
    tx.setScriptSig(accountService.createP2PKHScriptSigFromDigest(tx.getHash(), account, password).serialize());
    event.setEventBody(tx);
    eventBroadcaster.broadcastHashAndCache(event, true);
    return tx;
}
Also used : Account(io.nuls.account.entity.Account) TransactionEvent(io.nuls.ledger.event.TransactionEvent) PocExitConsensusTransaction(io.nuls.consensus.entity.tx.PocExitConsensusTransaction) AbstractCoinTransaction(io.nuls.ledger.entity.tx.AbstractCoinTransaction) CoinTransferData(io.nuls.ledger.entity.params.CoinTransferData) PocExitConsensusTransaction(io.nuls.consensus.entity.tx.PocExitConsensusTransaction) Transaction(io.nuls.core.chain.entity.Transaction) RegisterAgentTransaction(io.nuls.consensus.entity.tx.RegisterAgentTransaction) PocJoinConsensusTransaction(io.nuls.consensus.entity.tx.PocJoinConsensusTransaction) AbstractCoinTransaction(io.nuls.ledger.entity.tx.AbstractCoinTransaction) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) PocJoinConsensusTransaction(io.nuls.consensus.entity.tx.PocJoinConsensusTransaction) IOException(java.io.IOException) NulsException(io.nuls.core.exception.NulsException) IOException(java.io.IOException) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException)

Example 2 with AbstractCoinTransaction

use of io.nuls.ledger.entity.tx.AbstractCoinTransaction in project nuls by nuls-io.

the class UtxoTransferTool method transferCoinData.

private static void transferCoinData(Transaction tx, List<UtxoInputPo> inputPoList, List<UtxoOutputPo> outputPoList) {
    if (tx instanceof AbstractCoinTransaction) {
        AbstractCoinTransaction coinTx = (AbstractCoinTransaction) tx;
        UtxoData utxoData = new UtxoData();
        Set<String> addressSet = new HashSet<>();
        for (UtxoInputPo inputPo : inputPoList) {
            utxoData.getInputs().add(toInput(inputPo));
            addressSet.add(inputPo.getFromOutPut().getAddress());
        }
        for (int i = 0; i < outputPoList.size(); i++) {
            UtxoOutputPo outputPo = outputPoList.get(i);
            utxoData.getOutputs().add(toOutput(outputPo));
        }
        coinTx.setCoinData(utxoData);
    }
}
Also used : AbstractCoinTransaction(io.nuls.ledger.entity.tx.AbstractCoinTransaction) UtxoData(io.nuls.ledger.entity.UtxoData) UtxoInputPo(io.nuls.db.entity.UtxoInputPo) UtxoOutputPo(io.nuls.db.entity.UtxoOutputPo) HashSet(java.util.HashSet)

Aggregations

AbstractCoinTransaction (io.nuls.ledger.entity.tx.AbstractCoinTransaction)2 Account (io.nuls.account.entity.Account)1 PocExitConsensusTransaction (io.nuls.consensus.entity.tx.PocExitConsensusTransaction)1 PocJoinConsensusTransaction (io.nuls.consensus.entity.tx.PocJoinConsensusTransaction)1 RegisterAgentTransaction (io.nuls.consensus.entity.tx.RegisterAgentTransaction)1 Transaction (io.nuls.core.chain.entity.Transaction)1 NulsException (io.nuls.core.exception.NulsException)1 NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)1 UtxoInputPo (io.nuls.db.entity.UtxoInputPo)1 UtxoOutputPo (io.nuls.db.entity.UtxoOutputPo)1 UtxoData (io.nuls.ledger.entity.UtxoData)1 CoinTransferData (io.nuls.ledger.entity.params.CoinTransferData)1 TransactionEvent (io.nuls.ledger.event.TransactionEvent)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1