Search in sources :

Example 6 with Na

use of io.nuls.core.chain.entity.Na in project nuls by nuls-io.

the class UtxoData method getTotalNa.

@Override
public Na getTotalNa() {
    if (null == this.totalNa) {
        Set<String> addressSet = new HashSet<>();
        if (null != this.getInputs()) {
            for (UtxoInput input : this.getInputs()) {
                addressSet.add(input.getFrom().getAddress());
            }
        }
        Na totalNa = Na.ZERO;
        if (null != this.getOutputs()) {
            for (int i = 0; i < this.getOutputs().size(); i++) {
                UtxoOutput output = this.getOutputs().get(i);
                if (addressSet.contains(output.getAddress())) {
                    if (i == 0 && output.isLocked()) {
                        totalNa = totalNa.add(Na.valueOf(output.getValue()));
                        break;
                    }
                } else {
                    totalNa = totalNa.add(Na.valueOf(output.getValue()));
                }
            }
        }
        this.setTotalNa(totalNa);
    }
    return this.totalNa;
}
Also used : Na(io.nuls.core.chain.entity.Na) HashSet(java.util.HashSet)

Example 7 with Na

use of io.nuls.core.chain.entity.Na in project nuls by nuls-io.

the class UtxoCoinManager method getAccountsUnSpend.

public List<UtxoOutput> getAccountsUnSpend(List<String> addressList, Na value) {
    lock.lock();
    List<UtxoOutput> unSpends = new ArrayList<>();
    try {
        // check use-able is enough , find unSpend utxo
        Na amount = Na.ZERO;
        boolean enough = false;
        for (String address : addressList) {
            UtxoBalance balance = (UtxoBalance) ledgerCacheService.getBalance(address);
            if (balance == null || balance.getUnSpends().isEmpty()) {
                continue;
            }
            for (int i = 0; i < balance.getUnSpends().size(); i++) {
                UtxoOutput output = balance.getUnSpends().get(i);
                if (!output.isUsable()) {
                    continue;
                }
                unSpends.add(output);
                amount = amount.add(Na.valueOf(output.getValue()));
                if (amount.isGreaterThan(value)) {
                    enough = true;
                    break;
                }
            }
            if (enough) {
                break;
            }
        }
        if (!enough) {
            unSpends = new ArrayList<>();
        }
    } catch (Exception e) {
        Log.error(e);
        unSpends = new ArrayList<>();
    } finally {
        lock.unlock();
    }
    return unSpends;
}
Also used : Na(io.nuls.core.chain.entity.Na) ArrayList(java.util.ArrayList) UtxoBalance(io.nuls.ledger.entity.UtxoBalance) UtxoOutput(io.nuls.ledger.entity.UtxoOutput)

Aggregations

Na (io.nuls.core.chain.entity.Na)7 Deposit (io.nuls.consensus.entity.member.Deposit)3 Consensus (io.nuls.consensus.entity.Consensus)2 NulsException (io.nuls.core.exception.NulsException)2 Coin (io.nuls.ledger.entity.params.Coin)2 Account (io.nuls.account.entity.Account)1 Address (io.nuls.account.entity.Address)1 ConsensusReward (io.nuls.consensus.entity.meeting.ConsensusReward)1 PocMeetingMember (io.nuls.consensus.entity.meeting.PocMeetingMember)1 PocMeetingRound (io.nuls.consensus.entity.meeting.PocMeetingRound)1 Agent (io.nuls.consensus.entity.member.Agent)1 NulsDigestData (io.nuls.core.chain.entity.NulsDigestData)1 NulsRuntimeException (io.nuls.core.exception.NulsRuntimeException)1 P2PKHScript (io.nuls.core.script.P2PKHScript)1 ValidateResult (io.nuls.core.validate.ValidateResult)1 UtxoBalance (io.nuls.ledger.entity.UtxoBalance)1 UtxoOutput (io.nuls.ledger.entity.UtxoOutput)1 CoinTransferData (io.nuls.ledger.entity.params.CoinTransferData)1 CoinBaseTransaction (io.nuls.ledger.entity.tx.CoinBaseTransaction)1 LockNulsTransaction (io.nuls.ledger.entity.tx.LockNulsTransaction)1