Search in sources :

Example 6 with Entry

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

the class LevelDBManager method entryList.

public static List<Entry<byte[], byte[]>> entryList(String area) {
    if (!baseCheckArea(area)) {
        return null;
    }
    DBIterator iterator = null;
    List<Entry<byte[], byte[]>> entryList;
    try {
        DB db = AREAS.get(area);
        entryList = new ArrayList<>();
        iterator = db.iterator();
        byte[] key, bytes;
        Map.Entry<byte[], byte[]> entry;
        Comparator<byte[]> comparator = AREAS_COMPARATOR.get(area);
        for (iterator.seekToFirst(); iterator.hasNext(); iterator.next()) {
            entry = iterator.peekNext();
            key = entry.getKey();
            bytes = entry.getValue();
            entryList.add(new Entry<byte[], byte[]>(key, bytes, comparator));
        }
        // 如果自定义了比较器,则执行排序
        if (comparator != null) {
            entryList.sort(new Comparator<Entry<byte[], byte[]>>() {

                @Override
                public int compare(Entry<byte[], byte[]> o1, Entry<byte[], byte[]> o2) {
                    return o1.compareTo(o2.getKey());
                }
            });
        }
    } catch (Exception e) {
        Log.error(e);
        return null;
    } finally {
        // Make sure you close the iterator to avoid resource leaks.
        if (iterator != null) {
            try {
                iterator.close();
            } catch (Exception e) {
            // skip it
            }
        }
    }
    return entryList;
}
Also used : IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) DBIterator(org.iq80.leveldb.DBIterator) Entry(io.nuls.db.model.Entry) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) DB(org.iq80.leveldb.DB)

Example 7 with Entry

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

the class TotalCoinTask method doStatistics.

private void doStatistics() throws NulsException {
    long height = NulsContext.getInstance().getBestHeight();
    List<Entry<byte[], byte[]>> coinBytesList = getUtxoLedgerUtxoStorageService().getAllUtxoEntryBytes();
    long totalNuls = 0;
    long lockedNuls = 0;
    Coin coin = new Coin();
    for (Entry<byte[], byte[]> coinEntryBytes : coinBytesList) {
        coin.parse(coinEntryBytes.getValue(), 0);
        totalNuls += coin.getNa().getValue();
        if (coin.getLockTime() == -1 || coin.getLockTime() > System.currentTimeMillis() || (coin.getLockTime() < 1531152000000L && coin.getLockTime() > height)) {
            lockedNuls += coin.getNa().getValue();
        }
    }
    NulsContext.totalNuls = totalNuls;
    NulsContext.lockedNuls = lockedNuls;
}
Also used : Coin(io.nuls.kernel.model.Coin) Entry(io.nuls.db.model.Entry)

Example 8 with Entry

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

the class LocalUtxoServiceImpl method saveUtxoForAccount.

@Override
public Result saveUtxoForAccount(Transaction tx, List<byte[]> addressesList) {
    if (tx == null || addressesList == null || addressesList.size() == 0) {
        return Result.getFailed(KernelErrorCode.NULL_PARAMETER);
    }
    CoinData coinData = tx.getCoinData();
    if (coinData != null) {
        // delete - from
        List<Coin> froms = coinData.getFrom();
        List<byte[]> fromsList = new ArrayList<>();
        byte[] fromSource;
        byte[] utxoFromSource;
        byte[] fromIndex;
        Transaction sourceTx;
        Coin fromOfFromCoin;
        for (Coin from : froms) {
            fromSource = from.getOwner();
            fromOfFromCoin = from.getFrom();
            if (fromOfFromCoin == null) {
                utxoFromSource = new byte[tx.getHash().size()];
                fromIndex = new byte[fromSource.length - utxoFromSource.length];
                System.arraycopy(fromSource, 0, utxoFromSource, 0, tx.getHash().size());
                System.arraycopy(fromSource, tx.getHash().size(), fromIndex, 0, fromIndex.length);
                try {
                    sourceTx = ledgerService.getTx(NulsDigestData.fromDigestHex(Hex.encode(utxoFromSource)));
                } catch (Exception e) {
                    continue;
                }
                if (sourceTx == null) {
                    return Result.getFailed(TransactionErrorCode.TX_NOT_EXIST);
                }
                int index = (int) new VarInt(fromIndex, 0).value;
                fromOfFromCoin = sourceTx.getCoinData().getTo().get(index);
                from.setFrom(fromOfFromCoin);
            }
            if (fromOfFromCoin == null) {
                Log.warn("from coin not found!");
                continue;
            }
            byte[] toAddress = fromOfFromCoin.getAddress();
            boolean addressIsMatch = false;
            for (byte[] addresses : addressesList) {
                if (Arrays.equals(toAddress, addresses)) {
                    addressIsMatch = true;
                    break;
                }
            }
            if (!addressIsMatch) {
                continue;
            }
            fromsList.add(fromSource);
        }
        // save utxo - to
        List<Coin> tos = coinData.getTo();
        List<Entry<byte[], byte[]>> toList = new ArrayList<>();
        byte[] txHashBytes = null;
        try {
            txHashBytes = tx.getHash().serialize();
        } catch (IOException e) {
            throw new NulsRuntimeException(e);
        }
        byte[] outKey;
        Coin to;
        byte[] toAddress;
        for (int i = 0, length = tos.size(); i < length; i++) {
            to = tos.get(i);
            toAddress = to.getAddress();
            boolean addressIsMatch = false;
            for (byte[] addresses : addressesList) {
                if (Arrays.equals(toAddress, addresses)) {
                    addressIsMatch = true;
                    break;
                }
            }
            if (!addressIsMatch) {
                continue;
            }
            try {
                outKey = ArraysTool.concatenate(txHashBytes, new VarInt(i).encode());
                toList.add(new Entry<>(outKey, to.serialize()));
            } catch (IOException e) {
                Log.error(e);
            }
        }
        Result result = localUtxoStorageService.batchSaveAndDeleteUTXO(toList, fromsList);
        if (result.isFailed() || result.getData() == null || (int) result.getData() != toList.size() + fromsList.size()) {
            return Result.getFailed();
        }
    }
    return Result.getSuccess();
}
Also used : VarInt(io.nuls.kernel.utils.VarInt) ArrayList(java.util.ArrayList) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) IOException(java.io.IOException) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) IOException(java.io.IOException) Entry(io.nuls.db.model.Entry)

Example 9 with Entry

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

the class CheckUnConfirmTxThread method rollbackUtxo.

private void rollbackUtxo(Transaction tx) {
    if (tx == null) {
        return;
    }
    CoinData coinData = tx.getCoinData();
    if (coinData != null) {
        // save - from
        List<Coin> froms = coinData.getFrom();
        List<Entry<byte[], byte[]>> fromList = new ArrayList<>();
        byte[] fromSource;
        byte[] utxoFromSource;
        byte[] fromIndex;
        Transaction sourceTx;
        Coin fromCoin;
        for (Coin from : froms) {
            fromSource = from.getOwner();
            utxoFromSource = new byte[tx.getHash().size()];
            fromIndex = new byte[fromSource.length - utxoFromSource.length];
            System.arraycopy(fromSource, 0, utxoFromSource, 0, tx.getHash().size());
            System.arraycopy(fromSource, tx.getHash().size(), fromIndex, 0, fromIndex.length);
            try {
                sourceTx = ledgerService.getTx(NulsDigestData.fromDigestHex(Hex.encode(utxoFromSource)));
            } catch (Exception e) {
                continue;
            }
            if (sourceTx == null) {
                continue;
            }
            try {
                fromCoin = sourceTx.getCoinData().getTo().get((int) new VarInt(fromIndex, 0).value);
                // if (!AccountLegerUtils.isLocalAccount(fromCoin.getOwner()))
                if (null == AccountLegerUtils.isLocalAccount(fromCoin.getAddress())) {
                    continue;
                }
                Coin fromCoinFromLedger = ledgerService.getUtxo(fromSource);
                if (fromCoinFromLedger == null || !fromCoinFromLedger.usable()) {
                    continue;
                }
                fromList.add(new Entry<>(from.getOwner(), fromCoin.serialize()));
            } catch (IOException e) {
                throw new NulsRuntimeException(e);
            }
        }
        // delete utxo - to
        List<Coin> tos = coinData.getTo();
        List<byte[]> toList = new ArrayList<>();
        Coin toCoin;
        byte[] outKey;
        for (int i = 0, length = tos.size(); i < length; i++) {
            try {
                toCoin = tos.get(i);
                /*if (!AccountLegerUtils.isLocalAccount(toCoin.getOwner())) {
                        continue;
                    }*/
                if (null == AccountLegerUtils.isLocalAccount(toCoin.getAddress())) {
                    continue;
                }
                outKey = org.spongycastle.util.Arrays.concatenate(tx.getHash().serialize(), new VarInt(i).encode());
                toList.add(outKey);
            } catch (IOException e) {
                Log.info("delete unconfirmed output error");
                throw new NulsRuntimeException(e);
            }
        }
        localUtxoStorageService.batchSaveAndDeleteUTXO(fromList, toList);
    }
}
Also used : VarInt(io.nuls.kernel.utils.VarInt) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) IOException(java.io.IOException) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) IOException(java.io.IOException) Entry(io.nuls.db.model.Entry)

Example 10 with Entry

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

the class LocalUtxoStorageServiceImpl method batchSaveUTXO.

@Override
public Result<Integer> batchSaveUTXO(Map<byte[], byte[]> utxos) {
    BatchOperation batch = dbService.createWriteBatch(AccountLedgerStorageConstant.DB_NAME_ACCOUNT_LEDGER_COINDATA);
    Set<Map.Entry<byte[], byte[]>> utxosToSaveEntries = utxos.entrySet();
    for (Map.Entry<byte[], byte[]> entry : utxosToSaveEntries) {
        batch.put(entry.getKey(), entry.getValue());
    }
    Result batchResult = batch.executeBatch();
    if (batchResult.isFailed()) {
        return batchResult;
    }
    Result result = Result.getSuccess().setData(utxos.size());
    if (result.isSuccess() && cacheMap != null) {
        for (Map.Entry<byte[], byte[]> entry : utxosToSaveEntries) {
            cacheMap.put(new String(entry.getKey()), new Entry(entry.getKey(), entry.getValue()));
        }
    }
    return result;
}
Also used : Entry(io.nuls.db.model.Entry) BatchOperation(io.nuls.db.service.BatchOperation) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Result(io.nuls.kernel.model.Result)

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