Search in sources :

Example 1 with CoinTransferData

use of io.nuls.ledger.entity.params.CoinTransferData in project nuls by nuls-io.

the class UtxoLedgerServiceImpl method lock.

@Override
public Result lock(String address, String password, Na amount, long unlockTime, String remark) {
    LockNulsTransaction tx = null;
    try {
        CoinTransferData coinData = new CoinTransferData(OperationType.LOCK, amount, address, getTxFee(TransactionConstant.TX_TYPE_LOCK));
        coinData.addTo(address, new Coin(amount, unlockTime));
        tx = UtxoTransactionTool.getInstance().createLockNulsTx(coinData, password, remark);
        tx.verify();
        TransactionEvent event = new TransactionEvent();
        event.setEventBody(tx);
        eventBroadcaster.broadcastAndCacheAysn(event, true);
    } catch (Exception e) {
        Log.error(e);
        try {
            rollbackTx(tx);
        } catch (NulsException e1) {
            Log.error(e1);
        }
        return new Result(false, e.getMessage());
    }
    return new Result(true, "OK", tx.getHash().getDigestHex());
}
Also used : LockNulsTransaction(io.nuls.ledger.entity.tx.LockNulsTransaction) Coin(io.nuls.ledger.entity.params.Coin) TransactionEvent(io.nuls.ledger.event.TransactionEvent) CoinTransferData(io.nuls.ledger.entity.params.CoinTransferData) NulsException(io.nuls.core.exception.NulsException) NulsException(io.nuls.core.exception.NulsException) IOException(java.io.IOException) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) ValidateResult(io.nuls.core.validate.ValidateResult)

Example 2 with CoinTransferData

use of io.nuls.ledger.entity.params.CoinTransferData in project nuls by nuls-io.

the class PocConsensusServiceImpl method joinTheConsensus.

private Transaction joinTheConsensus(Account account, String password, long amount, String agentHash) throws IOException, NulsException {
    AssertUtil.canNotEmpty(account);
    AssertUtil.canNotEmpty(password);
    if (amount < PocConsensusConstant.ENTRUSTER_DEPOSIT_LOWER_LIMIT.getValue()) {
        throw new NulsRuntimeException(ErrorCode.NULL_PARAMETER);
    }
    AssertUtil.canNotEmpty(agentHash);
    TransactionEvent event = new TransactionEvent();
    Consensus<Deposit> ca = new ConsensusDepositImpl();
    ca.setAddress(account.getAddress().toString());
    Deposit deposit = new Deposit();
    deposit.setAgentHash(agentHash);
    deposit.setDeposit(Na.valueOf(amount));
    deposit.setStartTime(TimeService.currentTimeMillis());
    ca.setExtend(deposit);
    CoinTransferData data = new CoinTransferData(OperationType.LOCK, this.ledgerService.getTxFee(TransactionConstant.TX_TYPE_JOIN_CONSENSUS));
    data.setTotalNa(deposit.getDeposit());
    data.addFrom(account.getAddress().toString());
    Coin coin = new Coin();
    coin.setUnlockHeight(0);
    coin.setUnlockTime(0);
    coin.setNa(deposit.getDeposit());
    data.addTo(account.getAddress().toString(), coin);
    PocJoinConsensusTransaction tx = null;
    try {
        tx = new PocJoinConsensusTransaction(data, password);
    } catch (NulsException e) {
        throw new NulsRuntimeException(e);
    }
    tx.setTime(TimeService.currentTimeMillis());
    tx.setTxData(ca);
    tx.setHash(NulsDigestData.calcDigestData(tx.serialize()));
    tx.setScriptSig(accountService.createP2PKHScriptSigFromDigest(tx.getHash(), account, password).serialize());
    tx.verifyWithException();
    event.setEventBody(tx);
    List<String> nodeList = eventBroadcaster.broadcastAndCache(event, true);
    if (null == nodeList || nodeList.isEmpty()) {
        throw new NulsRuntimeException(ErrorCode.FAILED, "broadcast transaction failed!");
    }
    return tx;
}
Also used : Deposit(io.nuls.consensus.entity.member.Deposit) Coin(io.nuls.ledger.entity.params.Coin) TransactionEvent(io.nuls.ledger.event.TransactionEvent) CoinTransferData(io.nuls.ledger.entity.params.CoinTransferData) NulsException(io.nuls.core.exception.NulsException) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException) PocJoinConsensusTransaction(io.nuls.consensus.entity.tx.PocJoinConsensusTransaction) ConsensusDepositImpl(io.nuls.consensus.entity.ConsensusDepositImpl)

Example 3 with CoinTransferData

use of io.nuls.ledger.entity.params.CoinTransferData in project nuls by nuls-io.

the class ConsensusMeetingRunner method coinBaseTx.

private void coinBaseTx(List<Transaction> txList, PocMeetingMember self) throws NulsException, IOException {
    CoinTransferData data = new CoinTransferData(OperationType.COIN_BASE, this.ledgerService.getTxFee(TransactionConstant.TX_TYPE_COIN_BASE));
    data.setFee(Na.ZERO);
    List<ConsensusReward> rewardList = calcReward(txList, self);
    Na total = Na.ZERO;
    for (ConsensusReward reward : rewardList) {
        Coin coin = new Coin();
        coin.setNa(reward.getReward());
        data.addTo(reward.getAddress(), coin);
        total = total.add(reward.getReward());
    }
    data.setTotalNa(total);
    CoinBaseTransaction tx;
    try {
        tx = new CoinBaseTransaction(data, null);
    } catch (NulsException e) {
        Log.error(e);
        throw new NulsRuntimeException(e);
    }
    tx.setFee(Na.ZERO);
    tx.setHash(NulsDigestData.calcDigestData(tx));
    tx.setScriptSig(accountService.createP2PKHScriptSigFromDigest(tx.getHash(), consensusManager.getConsensusStatusInfo().getAccount(), NulsContext.CACHED_PASSWORD_OF_WALLET).serialize());
    ValidateResult validateResult = tx.verify();
    confirmingTxCacheManager.putTx(tx);
    if (null == validateResult || validateResult.isFailed()) {
        throw new NulsRuntimeException(ErrorCode.CONSENSUS_EXCEPTION);
    }
    try {
        ledgerService.approvalTx(tx);
    } catch (NulsException e) {
        throw new NulsRuntimeException(e);
    }
    txList.add(0, tx);
}
Also used : Coin(io.nuls.ledger.entity.params.Coin) CoinTransferData(io.nuls.ledger.entity.params.CoinTransferData) Na(io.nuls.core.chain.entity.Na) ConsensusReward(io.nuls.consensus.entity.meeting.ConsensusReward) CoinBaseTransaction(io.nuls.ledger.entity.tx.CoinBaseTransaction) NulsException(io.nuls.core.exception.NulsException) ValidateResult(io.nuls.core.validate.ValidateResult) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException)

Example 4 with CoinTransferData

use of io.nuls.ledger.entity.params.CoinTransferData in project nuls by nuls-io.

the class PocConsensusServiceImpl method registerAgent.

private Transaction registerAgent(Agent agent, Account account, String password) throws IOException, NulsException {
    TransactionEvent event = new TransactionEvent();
    CoinTransferData data = new CoinTransferData(OperationType.LOCK, this.ledgerService.getTxFee(TransactionConstant.TX_TYPE_REGISTER_AGENT));
    data.setTotalNa(agent.getDeposit());
    data.addFrom(account.getAddress().toString());
    Coin coin = new Coin();
    coin.setUnlockHeight(0);
    coin.setUnlockTime(0);
    coin.setNa(agent.getDeposit());
    data.addTo(account.getAddress().toString(), coin);
    RegisterAgentTransaction tx = null;
    try {
        tx = new RegisterAgentTransaction(data, password);
    } catch (NulsException e) {
        Log.error(e);
        throw new NulsRuntimeException(e);
    }
    Consensus<Agent> con = new ConsensusAgentImpl();
    con.setAddress(account.getAddress().toString());
    agent.setStartTime(TimeService.currentTimeMillis());
    con.setExtend(agent);
    tx.setTxData(con);
    tx.setHash(NulsDigestData.calcDigestData(tx.serialize()));
    tx.setScriptSig(accountService.createP2PKHScriptSigFromDigest(tx.getHash(), account, password).serialize());
    tx.verifyWithException();
    event.setEventBody(tx);
    List<String> nodeList = eventBroadcaster.broadcastHashAndCache(event, true);
    if (null == nodeList || nodeList.isEmpty()) {
        throw new NulsRuntimeException(ErrorCode.FAILED, "broadcast transaction failed!");
    }
    return tx;
}
Also used : Coin(io.nuls.ledger.entity.params.Coin) Agent(io.nuls.consensus.entity.member.Agent) TransactionEvent(io.nuls.ledger.event.TransactionEvent) RegisterAgentTransaction(io.nuls.consensus.entity.tx.RegisterAgentTransaction) CoinTransferData(io.nuls.ledger.entity.params.CoinTransferData) ConsensusAgentImpl(io.nuls.consensus.entity.ConsensusAgentImpl) NulsException(io.nuls.core.exception.NulsException) NulsRuntimeException(io.nuls.core.exception.NulsRuntimeException)

Example 5 with CoinTransferData

use of io.nuls.ledger.entity.params.CoinTransferData 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)

Aggregations

NulsException (io.nuls.core.exception.NulsException)7 NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)7 CoinTransferData (io.nuls.ledger.entity.params.CoinTransferData)7 Coin (io.nuls.ledger.entity.params.Coin)5 TransactionEvent (io.nuls.ledger.event.TransactionEvent)5 IOException (java.io.IOException)4 Account (io.nuls.account.entity.Account)3 ValidateResult (io.nuls.core.validate.ValidateResult)3 PocJoinConsensusTransaction (io.nuls.consensus.entity.tx.PocJoinConsensusTransaction)2 RegisterAgentTransaction (io.nuls.consensus.entity.tx.RegisterAgentTransaction)2 CoinBaseTransaction (io.nuls.ledger.entity.tx.CoinBaseTransaction)2 Alias (io.nuls.account.entity.Alias)1 AliasTransaction (io.nuls.account.entity.tx.AliasTransaction)1 AccountService (io.nuls.account.service.intf.AccountService)1 ConsensusAgentImpl (io.nuls.consensus.entity.ConsensusAgentImpl)1 ConsensusDepositImpl (io.nuls.consensus.entity.ConsensusDepositImpl)1 ConsensusReward (io.nuls.consensus.entity.meeting.ConsensusReward)1 Agent (io.nuls.consensus.entity.member.Agent)1 Deposit (io.nuls.consensus.entity.member.Deposit)1 PocExitConsensusTransaction (io.nuls.consensus.entity.tx.PocExitConsensusTransaction)1