use of io.nuls.account.model.Balance in project nuls by nuls-io.
the class BalanceManager method getBalance.
/**
* 获取账户余额
*/
public Result<Balance> getBalance(byte[] address) {
lock.lock();
try {
if (address == null || address.length != Address.ADDRESS_LENGTH) {
return Result.getFailed(AccountLedgerErrorCode.PARAMETER_ERROR);
}
String addressKey = AddressTool.getStringAddressByBytes(address);
BalanceCacheEntity entity = balanceMap.get(addressKey);
Balance balance = null;
if (entity == null || (entity.getEarlistLockTime() > 0L && entity.getEarlistLockTime() <= TimeService.currentTimeMillis())) {
try {
balance = calBalanceByAddress(address);
} catch (NulsException e) {
Log.info("getbalance of address[" + AddressTool.getStringAddressByBytes(address) + "] error");
}
} else {
balance = entity.getBalance();
}
return Result.getSuccess().setData(balance);
} finally {
lock.unlock();
}
}
use of io.nuls.account.model.Balance in project nuls by nuls-io.
the class AccountLedgerServiceImpl method getBalance.
@Override
public Result<Balance> getBalance(byte[] address) {
if (address == null || address.length != Address.ADDRESS_LENGTH) {
return Result.getFailed(AccountErrorCode.ADDRESS_ERROR);
}
Account account = AccountLegerUtils.isLocalAccount(address);
if (null == account) {
return Result.getFailed(AccountErrorCode.ACCOUNT_NOT_EXIST);
}
Balance balance;
if (account.isOk()) {
balance = balanceManager.getBalance(address).getData();
} else {
balance = new Balance();
Result<UtxoAccountsBalance> result = this.utxoAccountsBalanceService.getUtxoAccountsBalance(address);
if (!result.isFailed()) {
UtxoAccountsBalance uab = result.getData();
balance.setBalance(uab.getBalance());
balance.setLocked(uab.getHadLocked());
balance.setUsable(balance.getBalance().subtract(balance.getLocked()));
}
}
if (balance == null) {
return Result.getFailed(AccountErrorCode.ACCOUNT_NOT_EXIST);
}
return Result.getSuccess().setData(balance);
}
use of io.nuls.account.model.Balance in project nuls by nuls-io.
the class BalanceManager method calBalanceByAddress.
/**
* 计算账户的余额,这个方法应该和获取余额方法互斥,避免并发导致数据不准确
*/
public Balance calBalanceByAddress(byte[] address) throws NulsException {
lock.lock();
try {
if (accountService.getAccount(address).isFailed()) {
return null;
}
List<Coin> coinList = getCoinListByAddress(address);
Collections.sort(coinList, CoinComparator.getInstance());
BalanceCacheEntity balanceCacheEntity = new BalanceCacheEntity();
Na usable = Na.ZERO;
Na locked = Na.ZERO;
for (Coin coin : coinList) {
if (coin.usable()) {
usable = usable.add(coin.getNa());
} else {
locked = locked.add(coin.getNa());
long lockTime = coin.getLockTime();
// the consensus lock type
if (lockTime <= 0L) {
continue;
}
// the height lock type
if (balanceCacheEntity.getLowestLockHeigh() == 0L || (lockTime < NulsConstant.BlOCKHEIGHT_TIME_DIVIDE && lockTime < balanceCacheEntity.getLowestLockHeigh())) {
balanceCacheEntity.setLowestLockHeigh(lockTime);
continue;
}
// the time lock type
if (balanceCacheEntity.getEarlistLockTime() == 0L || (lockTime > NulsConstant.BlOCKHEIGHT_TIME_DIVIDE && lockTime < balanceCacheEntity.getEarlistLockTime())) {
balanceCacheEntity.setEarlistLockTime(lockTime);
continue;
}
}
}
Balance balance = new Balance();
balance.setUsable(usable);
balance.setLocked(locked);
balance.setBalance(usable.add(locked));
balanceCacheEntity.setBalance(balance);
balanceMap.put(AddressTool.getStringAddressByBytes(address), balanceCacheEntity);
return balance;
} finally {
lock.unlock();
}
}
Aggregations