Search in sources :

Example 1 with UtxoBalance

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

the class LedgerCacheService method putUtxo.

public void putUtxo(String key, UtxoOutput output) {
    utxoCacheService.putElement(LedgerConstant.UTXO, key, output);
    String address = output.getAddress();
    UtxoBalance balance = (UtxoBalance) getBalance(address);
    if (balance == null) {
        balance = new UtxoBalance();
        List<UtxoOutput> outputs = new CopyOnWriteArrayList<>();
        outputs.add(output);
        balance.setUnSpends(outputs);
        putBalance(address, balance);
    } else {
        balance.getUnSpends().add(output);
        if (balance.getUnSpends().size() > 1) {
            Collections.sort(balance.getUnSpends());
        }
    }
}
Also used : UtxoBalance(io.nuls.ledger.entity.UtxoBalance) UtxoOutput(io.nuls.ledger.entity.UtxoOutput) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 2 with UtxoBalance

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

the class AccountResource method getUtxo.

@GET
@Path("/utxo/")
@Produces(MediaType.APPLICATION_JSON)
public RpcResult getUtxo(@QueryParam("address") String address, @QueryParam("amount") long amount) {
    if (!Address.validAddress(address) || amount <= 0 || amount > Na.MAX_NA_VALUE) {
        return RpcResult.getFailed(ErrorCode.PARAMETER_ERROR);
    }
    UtxoBalance balance = (UtxoBalance) ledgerService.getBalance(address);
    if (balance == null || balance.getUnSpends() == null) {
        return RpcResult.getFailed("balance not enough");
    }
    amount += this.ledgerService.getTxFee(Integer.MAX_VALUE).getValue();
    long usable = 0;
    boolean enough = false;
    List<OutputDto> dtoList = new ArrayList<>();
    for (int i = 0; i < balance.getUnSpends().size(); i++) {
        UtxoOutput output = balance.getUnSpends().get(i);
        if (output.isUsable()) {
            usable += output.getValue();
            dtoList.add(new OutputDto(output));
        }
        if (usable > amount) {
            enough = true;
            break;
        }
    }
    if (!enough) {
        return RpcResult.getFailed("balance not enough");
    }
    return RpcResult.getSuccess().setData(dtoList);
}
Also used : UtxoBalance(io.nuls.ledger.entity.UtxoBalance) ArrayList(java.util.ArrayList) UtxoOutput(io.nuls.ledger.entity.UtxoOutput)

Example 3 with UtxoBalance

use of io.nuls.ledger.entity.UtxoBalance 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 4 with UtxoBalance

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

UtxoBalance (io.nuls.ledger.entity.UtxoBalance)4 UtxoOutput (io.nuls.ledger.entity.UtxoOutput)4 ArrayList (java.util.ArrayList)2 Na (io.nuls.core.chain.entity.Na)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1