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);
}
}
}
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;
}
Aggregations