Search in sources :

Example 21 with Entry

use of io.nuls.db.model.Entry in project nuls by nuls-io.

the class UtxoResource method getAllUtxoByAddress.

private List<Coin> getAllUtxoByAddress(String address) {
    List<Coin> coinList = new ArrayList<>();
    byte[] addressBytes = AddressTool.getAddress(address);
    List<Entry<byte[], byte[]>> coinBytesList = utxoLedgerUtxoStorageService.getAllUtxoEntryBytes();
    Coin coin;
    for (Entry<byte[], byte[]> coinEntryBytes : coinBytesList) {
        coin = new Coin();
        try {
            coin.parse(coinEntryBytes.getValue(), 0);
        } catch (NulsException e) {
            Log.info("parse coin form db error");
            continue;
        }
        // if (Arrays.equals(coin.(), addressBytes))
        if (Arrays.equals(coin.getAddress(), addressBytes)) {
            coin.setOwner(coinEntryBytes.getKey());
            coinList.add(coin);
        }
    }
    Collections.sort(coinList, CoinComparator.getInstance());
    return coinList;
}
Also used : Coin(io.nuls.kernel.model.Coin) Entry(io.nuls.db.model.Entry) NulsException(io.nuls.kernel.exception.NulsException)

Example 22 with Entry

use of io.nuls.db.model.Entry in project nuls by nuls-io.

the class UtxoResource method getInfo.

@GET
@Path("/info")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "查询代币情况")
@ApiResponses(value = { @ApiResponse(code = 200, message = "success", response = TokenInfoDto.class) })
public RpcClientResult getInfo() throws NulsException {
    long height = NulsContext.getInstance().getBestHeight();
    List<Entry<byte[], byte[]>> coinBytesList = utxoLedgerUtxoStorageService.getAllUtxoEntryBytes();
    double totalNuls = 0d;
    double lockedNuls = 0d;
    Map<String, Holder> map = new HashMap<>();
    Coin coin = new Coin();
    int index = 0;
    for (Entry<byte[], byte[]> coinEntryBytes : coinBytesList) {
        coin.parse(coinEntryBytes.getValue(), 0);
        double value = coin.getNa().toDouble();
        String address = AddressTool.getStringAddressByBytes(coin.getOwner());
        Holder holder = map.get(address);
        if (null == holder) {
            holder = new Holder();
            holder.setAddress(address);
            map.put(address, holder);
        }
        holder.addTotal(value);
        totalNuls = DoubleUtils.sum(totalNuls, value);
        if (coin.getLockTime() == -1 || coin.getLockTime() > System.currentTimeMillis() || (coin.getLockTime() < 1531152000000L && coin.getLockTime() > height)) {
            holder.addLocked(value);
            lockedNuls = DoubleUtils.sum(lockedNuls, value);
        }
        System.out.println(index++);
    }
    Result<TokenInfoDto> result = Result.getSuccess();
    TokenInfoDto info = new TokenInfoDto();
    info.setTotalNuls(DoubleUtils.getRoundStr(totalNuls, 8, true));
    info.setLockedNuls(DoubleUtils.getRoundStr(lockedNuls, 8, true));
    List<Holder> holderList = new ArrayList<>(map.values());
    Collections.sort(holderList);
    List<HolderDto> dtoList = new ArrayList<>();
    for (Holder holder : holderList) {
        HolderDto dto = new HolderDto(holder);
        dtoList.add(dto);
    }
    info.setAddressList(dtoList);
    result.setData(info);
    return result.toRpcClientResult();
}
Also used : Coin(io.nuls.kernel.model.Coin) Entry(io.nuls.db.model.Entry) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 23 with Entry

use of io.nuls.db.model.Entry in project nuls by nuls-io.

the class ContractBalanceManager method getAllTokensByAccount.

public Result<List<ContractTokenInfo>> getAllTokensByAccount(String account) {
    Map<String, ContractTokenInfo> tokensMap = contractTokenOfLocalAccount.get(account);
    if (tokensMap == null || tokensMap.size() == 0) {
        return Result.getSuccess().setData(new ArrayList<>());
    }
    List<ContractTokenInfo> resultList = new ArrayList<>();
    Set<Map.Entry<String, ContractTokenInfo>> entries = tokensMap.entrySet();
    String contractAddress;
    ContractTokenInfo info;
    for (Map.Entry<String, ContractTokenInfo> entry : entries) {
        contractAddress = entry.getKey();
        info = entry.getValue();
        info.setContractAddress(contractAddress);
        resultList.add(info);
    }
    return Result.getSuccess().setData(resultList);
}
Also used : Entry(io.nuls.db.model.Entry) LedgerUtil.asString(io.nuls.ledger.util.LedgerUtil.asString) ContractTokenInfo(io.nuls.contract.dto.ContractTokenInfo) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 24 with Entry

use of io.nuls.db.model.Entry in project nuls by nuls-io.

the class ContractBalanceManager method initContractBalance.

/**
 * 初始化缓存本地所有合约账户的余额信息
 */
public void initContractBalance() {
    balanceMap = new ConcurrentHashMap<>();
    List<Entry<byte[], byte[]>> rawList = contractUtxoStorageService.loadAllCoinList();
    Coin coin;
    String strAddress;
    ContractBalance balance;
    for (Entry<byte[], byte[]> coinEntry : rawList) {
        coin = new Coin();
        try {
            coin.parse(coinEntry.getValue(), 0);
            // strAddress = asString(coin.getOwner());
            coin.setKey(asString(coinEntry.getKey()));
            strAddress = asString(coin.getAddress());
        } catch (NulsException e) {
            Log.error("parse contract coin error form db", e);
            continue;
        }
        balance = balanceMap.get(strAddress);
        if (balance == null) {
            balance = new ContractBalance();
            balanceMap.put(strAddress, balance);
        }
        // 共识奖励的utxo
        if (coin.getLockTime() != 0) {
            balance.getConsensusRewardCoins().put(coin.getKey(), coin);
        } else {
            balance.addUsable(coin.getNa());
        }
    }
}
Also used : Coin(io.nuls.kernel.model.Coin) Entry(io.nuls.db.model.Entry) ContractBalance(io.nuls.contract.ledger.module.ContractBalance) NulsException(io.nuls.kernel.exception.NulsException) LedgerUtil.asString(io.nuls.ledger.util.LedgerUtil.asString)

Aggregations

Entry (io.nuls.db.model.Entry)24 ArrayList (java.util.ArrayList)12 NulsRuntimeException (io.nuls.kernel.exception.NulsRuntimeException)9 NulsException (io.nuls.kernel.exception.NulsException)8 IOException (java.io.IOException)8 Coin (io.nuls.kernel.model.Coin)6 VarInt (io.nuls.kernel.utils.VarInt)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 List (java.util.List)4 Result (io.nuls.kernel.model.Result)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 DB (org.iq80.leveldb.DB)3 DBIterator (org.iq80.leveldb.DBIterator)3 ContractAddressInfoPo (io.nuls.contract.storage.po.ContractAddressInfoPo)2 BatchOperation (io.nuls.db.service.BatchOperation)2 NulsDigestData (io.nuls.kernel.model.NulsDigestData)2 LedgerUtil.asString (io.nuls.ledger.util.LedgerUtil.asString)2 LinkedList (java.util.LinkedList)2 UnconfirmedTxPo (io.nuls.account.ledger.storage.po.UnconfirmedTxPo)1 AgentPo (io.nuls.consensus.poc.storage.po.AgentPo)1