Search in sources :

Example 11 with UtxoOutput

use of io.nuls.ledger.entity.UtxoOutput in project nuls by nuls-io.

the class LedgerCacheService method removeUtxo.

public void removeUtxo(String key) {
    UtxoOutput output = getUtxo(key);
    utxoCacheService.removeElement(LedgerConstant.UTXO, key);
    if (output != null) {
        UtxoBalance balance = (UtxoBalance) getBalance(output.getAddress());
        if (balance != null) {
            balance.getUnSpends().remove(output);
        }
    }
}
Also used : UtxoBalance(io.nuls.ledger.entity.UtxoBalance) UtxoOutput(io.nuls.ledger.entity.UtxoOutput)

Example 12 with UtxoOutput

use of io.nuls.ledger.entity.UtxoOutput 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

UtxoOutput (io.nuls.ledger.entity.UtxoOutput)12 UtxoBalance (io.nuls.ledger.entity.UtxoBalance)4 NulsDigestData (io.nuls.core.chain.entity.NulsDigestData)2 UtxoData (io.nuls.ledger.entity.UtxoData)2 UtxoInput (io.nuls.ledger.entity.UtxoInput)2 ArrayList (java.util.ArrayList)2 Na (io.nuls.core.chain.entity.Na)1 NulsException (io.nuls.core.exception.NulsException)1 P2PKHScript (io.nuls.core.script.P2PKHScript)1 P2PKHScriptSig (io.nuls.core.script.P2PKHScriptSig)1 UtxoOutputPo (io.nuls.db.entity.UtxoOutputPo)1 DbSession (io.nuls.db.transactional.annotation.DbSession)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1