Search in sources :

Example 11 with Entry

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

the class UnconfiredmTransactionStorageImpl method loadAllUnconfirmedList.

@Override
public Result<List<Transaction>> loadAllUnconfirmedList() {
    Result result;
    List<UnconfirmedTxPo> tmpList = new ArrayList<>();
    List<Entry<byte[], byte[]>> txs = dbService.entryList(AccountLedgerStorageConstant.DB_NAME_ACCOUNT_LEDGER_TX);
    for (Entry<byte[], byte[]> txEntry : txs) {
        try {
            UnconfirmedTxPo tmpTx = new UnconfirmedTxPo(txEntry.getValue());
            if (tmpTx != null) {
                NulsByteBuffer buffer = new NulsByteBuffer(txEntry.getKey(), 0);
                tmpTx.getTx().setHash(buffer.readHash());
                tmpList.add(tmpTx);
            }
        } catch (Exception e) {
            Log.warn("parse local tx error", e);
        }
    }
    tmpList.sort(new Comparator<UnconfirmedTxPo>() {

        @Override
        public int compare(UnconfirmedTxPo o1, UnconfirmedTxPo o2) {
            return (int) (o1.getSequence() - o2.getSequence());
        }
    });
    List<Transaction> resultList = new ArrayList<>();
    for (UnconfirmedTxPo po : tmpList) {
        resultList.add(po.getTx());
    }
    return Result.getSuccess().setData(resultList);
}
Also used : ArrayList(java.util.ArrayList) UnconfirmedTxPo(io.nuls.account.ledger.storage.po.UnconfirmedTxPo) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) NulsException(io.nuls.kernel.exception.NulsException) Result(io.nuls.kernel.model.Result) Entry(io.nuls.db.model.Entry) Transaction(io.nuls.kernel.model.Transaction) NulsByteBuffer(io.nuls.kernel.utils.NulsByteBuffer)

Example 12 with Entry

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

the class AgentStorageServiceImpl method getList.

@Override
public List<AgentPo> getList() {
    List<Entry<byte[], byte[]>> list = dbService.entryList(ConsensusStorageConstant.DB_NAME_CONSENSUS_AGENT);
    List<AgentPo> resultList = new ArrayList<>();
    if (list == null) {
        return resultList;
    }
    for (Entry<byte[], byte[]> entry : list) {
        AgentPo agentPo = new AgentPo();
        try {
            agentPo.parse(entry.getValue(), 0);
        } catch (NulsException e) {
            Log.error(e);
            throw new NulsRuntimeException(e);
        }
        NulsDigestData hash = new NulsDigestData();
        try {
            hash.parse(entry.getKey(), 0);
        } catch (NulsException e) {
            Log.error(e);
        }
        agentPo.setHash(hash);
        resultList.add(agentPo);
    }
    return resultList;
}
Also used : Entry(io.nuls.db.model.Entry) NulsException(io.nuls.kernel.exception.NulsException) ArrayList(java.util.ArrayList) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) NulsDigestData(io.nuls.kernel.model.NulsDigestData) AgentPo(io.nuls.consensus.poc.storage.po.AgentPo)

Example 13 with Entry

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

the class DepositStorageServiceImpl method getList.

@Override
public List<DepositPo> getList() {
    List<Entry<byte[], byte[]>> list = dbService.entryList(ConsensusStorageConstant.DB_NAME_CONSENSUS_DEPOSIT);
    List<DepositPo> resultList = new ArrayList<>();
    if (list == null) {
        return resultList;
    }
    for (Entry<byte[], byte[]> entry : list) {
        DepositPo depositPo = new DepositPo();
        try {
            depositPo.parse(entry.getValue(), 0);
        } catch (NulsException e) {
            Log.error(e);
            throw new NulsRuntimeException(e);
        }
        NulsDigestData hash = new NulsDigestData();
        try {
            hash.parse(entry.getKey(), 0);
        } catch (NulsException e) {
            Log.error(e);
        }
        depositPo.setTxHash(hash);
        resultList.add(depositPo);
    }
    return resultList;
}
Also used : Entry(io.nuls.db.model.Entry) DepositPo(io.nuls.consensus.poc.storage.po.DepositPo) NulsException(io.nuls.kernel.exception.NulsException) ArrayList(java.util.ArrayList) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) NulsDigestData(io.nuls.kernel.model.NulsDigestData)

Example 14 with Entry

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

the class PunishLogStorageServiceImpl method getPunishList.

@Override
public List<PunishLogPo> getPunishList() {
    List<Entry<byte[], byte[]>> list = dbService.entryList(ConsensusStorageConstant.DB_NAME_CONSENSUS_PUNISH_LOG);
    List<PunishLogPo> polist = new ArrayList<>();
    for (Entry<byte[], byte[]> entry : list) {
        PunishLogPo po = new PunishLogPo();
        try {
            po.parse(entry.getValue(), 0);
        } catch (NulsException e) {
            throw new NulsRuntimeException(e);
        }
        polist.add(po);
    }
    return polist;
}
Also used : Entry(io.nuls.db.model.Entry) NulsException(io.nuls.kernel.exception.NulsException) ArrayList(java.util.ArrayList) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) PunishLogPo(io.nuls.consensus.poc.storage.po.PunishLogPo)

Example 15 with Entry

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

the class LocalUtxoServiceImpl method deleteUtxoOfTransaction.

@Override
public Result deleteUtxoOfTransaction(Transaction tx) {
    if (tx == null) {
        return Result.getFailed(KernelErrorCode.NULL_PARAMETER);
    }
    CoinData coinData = tx.getCoinData();
    if (coinData != null) {
        // delete utxo - to
        List<Coin> tos = coinData.getTo();
        List<byte[]> toList = new ArrayList<>();
        byte[] outKey;
        for (int i = 0, length = tos.size(); i < length; i++) {
            try {
                // if(!AccountLegerUtils.isLocalAccount(tos.get(i).getOwner()))
                if (null == AccountLegerUtils.isLocalAccount(tos.get(i).getAddress())) {
                    continue;
                }
                outKey = ArraysTool.concatenate(tx.getHash().serialize(), new VarInt(i).encode());
                toList.add(outKey);
            } catch (IOException e) {
                throw new NulsRuntimeException(e);
            }
        }
        // save - from
        List<Coin> froms = coinData.getFrom();
        List<Entry<byte[], byte[]>> fromList = new ArrayList<>();
        byte[] fromSource;
        Coin fromOfFromCoin;
        byte[] utxoFromSource;
        byte[] fromIndex;
        Transaction sourceTx;
        byte[] address;
        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);
                }
                fromOfFromCoin = sourceTx.getCoinData().getTo().get((int) new VarInt(fromIndex, 0).value);
                from.setFrom(fromOfFromCoin);
            }
            if (fromOfFromCoin == null) {
                Log.warn("from coin not found!");
                continue;
            }
            address = fromOfFromCoin.getAddress();
            if (null == AccountLegerUtils.isLocalAccount(address)) {
                continue;
            }
            try {
                fromList.add(new Entry<>(fromSource, fromOfFromCoin.serialize()));
            } catch (IOException e) {
                throw new NulsRuntimeException(e);
            }
        }
        Result result = localUtxoStorageService.batchSaveAndDeleteUTXO(fromList, toList);
        if (result.isFailed() || result.getData() == null || (int) result.getData() != fromList.size() + toList.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)

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