Search in sources :

Example 16 with Entry

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

the class ContractTransferTransactionStorageImpl method loadAllContractTransferTxList.

@Override
public List<ContractTransferTransaction> loadAllContractTransferTxList() throws NulsException {
    List<ContractTransferTransaction> txList = new ArrayList<>();
    List<Entry<byte[], byte[]>> entryList = dbService.entryList(ContractStorageConstant.DB_NAME_CONTRACT_SPECIAL_TX);
    if (entryList == null || entryList.isEmpty()) {
        return txList;
    }
    ContractTransferTransaction tx;
    for (Entry<byte[], byte[]> entry : entryList) {
        tx = new ContractTransferTransaction();
        tx.parse(entry.getValue(), 0);
        txList.add(tx);
    }
    return txList;
}
Also used : Entry(io.nuls.db.model.Entry) ContractTransferTransaction(io.nuls.contract.entity.tx.ContractTransferTransaction) ArrayList(java.util.ArrayList)

Example 17 with Entry

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

the class ContractUtxoServiceImpl method saveUtxoForContractAddress.

/**
 * 从地址的维度上讲,分为两大类交易
 *   第一大类交易是普通地址转入合约地址
 *      合约交易 - 调用合约时 调用者地址向合约地址转入金额
 *      普通转账交易 - 普通地址向合约地址转入金额
 *   第二大类交易是智能合约特殊转账交易,合约地址转出到普通地址/合约地址
 *
 * @param tx
 * @return
 */
@Override
public Result saveUtxoForContractAddress(Transaction tx) {
    if (tx == null) {
        return Result.getFailed(KernelErrorCode.NULL_PARAMETER);
    }
    CoinData coinData = tx.getCoinData();
    if (coinData != null) {
        // 在合约独立账本中,只有合约转账(从合约转出)交易才能从合约地址中转出金额,所以只有这类交易才处理fromCoinData -> delete - from
        List<byte[]> fromList = new ArrayList<>();
        // 合约特殊转账交易
        List<Coin> froms = new ArrayList<>();
        List<Coin> deleteFroms = new ArrayList<>();
        if (tx.getType() == ContractConstant.TX_TYPE_CONTRACT_TRANSFER) {
            froms = coinData.getFrom();
            byte[] fromSource;
            byte[] utxoFromTxHash;
            byte[] utxoFromIndex;
            int txHashSize = tx.getHash().size();
            Coin fromOfFromCoin;
            for (Coin from : froms) {
                fromSource = from.getOwner();
                utxoFromTxHash = new byte[txHashSize];
                utxoFromIndex = new byte[fromSource.length - txHashSize];
                System.arraycopy(fromSource, 0, utxoFromTxHash, 0, txHashSize);
                System.arraycopy(fromSource, txHashSize, utxoFromIndex, 0, utxoFromIndex.length);
                fromOfFromCoin = from.getFrom();
                if (fromOfFromCoin == null) {
                    Transaction sourceTx = null;
                    try {
                        sourceTx = ledgerService.getTx(NulsDigestData.fromDigestHex(Hex.encode(utxoFromTxHash)));
                    } catch (Exception e) {
                        throw new NulsRuntimeException(e);
                    }
                    if (sourceTx == null) {
                        return Result.getFailed(TransactionErrorCode.TX_NOT_EXIST);
                    }
                    fromOfFromCoin = sourceTx.getCoinData().getTo().get((int) new VarInt(utxoFromIndex, 0).value);
                }
                // 非合约地址在合约账本中不处理
                if (!ContractUtil.isLegalContractAddress(fromOfFromCoin.getOwner())) {
                    continue;
                }
                from.setFrom(fromOfFromCoin);
                from.setTempOwner(fromOfFromCoin.getOwner());
                from.setKey(asString(fromSource));
                deleteFroms.add(from);
                fromList.add(fromSource);
            }
        }
        // save utxo - to
        List<Coin> tos = coinData.getTo();
        List<Coin> contractTos = new ArrayList<>();
        List<Entry<byte[], byte[]>> toList = new ArrayList<>();
        byte[] txHashBytes;
        try {
            txHashBytes = tx.getHash().serialize();
        } catch (IOException e) {
            throw new NulsRuntimeException(e);
        }
        Coin to;
        byte[] toAddress;
        byte[] outKey;
        for (int i = 0, length = tos.size(); i < length; i++) {
            to = tos.get(i);
            // toAddress = to.getOwner();
            // 非合约地址在合约账本中不处理
            toAddress = to.getAddress();
            if (!ContractUtil.isLegalContractAddress(toAddress)) {
                continue;
            }
            try {
                outKey = ArraysTool.concatenate(txHashBytes, new VarInt(i).encode());
                to.setTempOwner(toAddress);
                to.setKey(asString(outKey));
                contractTos.add(to);
                toList.add(new Entry<byte[], byte[]>(outKey, to.serialize()));
            } catch (IOException e) {
                throw new NulsRuntimeException(e);
            }
        }
        Result<List<Entry<byte[], byte[]>>> result = contractUtxoStorageService.batchSaveAndDeleteUTXO(toList, fromList);
        if (result.isFailed() || result.getData() == null) {
            return Result.getFailed();
        }
        // 刷新余额
        contractBalanceManager.refreshBalance(contractTos, deleteFroms);
    }
    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) ArrayList(java.util.ArrayList) List(java.util.List)

Example 18 with Entry

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

the class ContractUtxoServiceImpl method deleteUtxoOfTransaction.

@Override
public Result deleteUtxoOfTransaction(Transaction tx) {
    if (tx == null) {
        return Result.getFailed(KernelErrorCode.NULL_PARAMETER);
    }
    CoinData coinData = tx.getCoinData();
    byte[] txHashBytes;
    try {
        txHashBytes = tx.getHash().serialize();
    } catch (IOException e) {
        throw new NulsRuntimeException(e);
    }
    if (coinData != null) {
        // delete utxo - to
        List<Coin> tos = coinData.getTo();
        List<Coin> contractTos = new ArrayList<>();
        List<byte[]> toList = new ArrayList<>();
        byte[] outKey;
        Coin to;
        byte[] toAddress;
        for (int i = 0, length = tos.size(); i < length; i++) {
            to = tos.get(i);
            // toAddress = to.();
            toAddress = to.getAddress();
            if (!ContractUtil.isLegalContractAddress(toAddress)) {
                continue;
            }
            outKey = ArraysTool.concatenate(txHashBytes, new VarInt(i).encode());
            to.setTempOwner(toAddress);
            to.setKey(asString(outKey));
            contractTos.add(to);
            toList.add(outKey);
        }
        // save - from
        List<Entry<byte[], byte[]>> fromList = new ArrayList<>();
        List<Coin> froms = new ArrayList<>();
        if (tx.getType() == ContractConstant.TX_TYPE_CONTRACT_TRANSFER) {
            froms = coinData.getFrom();
            int txHashSize = tx.getHash().size();
            byte[] fromSource;
            byte[] utxoFromHash;
            byte[] utxoFromIndex;
            Transaction sourceTx;
            Coin sourceTxCoinTo;
            for (Coin from : froms) {
                fromSource = from.getOwner();
                utxoFromHash = new byte[txHashSize];
                utxoFromIndex = new byte[fromSource.length - txHashSize];
                System.arraycopy(fromSource, 0, utxoFromHash, 0, txHashSize);
                System.arraycopy(fromSource, txHashSize, utxoFromIndex, 0, utxoFromIndex.length);
                try {
                    sourceTx = ledgerService.getTx(NulsDigestData.fromDigestHex(Hex.encode(utxoFromHash)));
                } catch (Exception e) {
                    continue;
                }
                if (sourceTx == null) {
                    return Result.getFailed(TransactionErrorCode.TX_NOT_EXIST);
                }
                sourceTxCoinTo = sourceTx.getCoinData().getTo().get((int) new VarInt(utxoFromIndex, 0).value);
                if (!ContractUtil.isLegalContractAddress(sourceTxCoinTo.getAddress())) {
                    continue;
                }
                from.setFrom(sourceTxCoinTo);
                from.setTempOwner(sourceTxCoinTo.getAddress());
                from.setKey(asString(fromSource));
                try {
                    fromList.add(new Entry<byte[], byte[]>(fromSource, sourceTxCoinTo.serialize()));
                } catch (IOException e) {
                    throw new NulsRuntimeException(e);
                }
            }
        }
        // 函数将返回在数据库中存在的to
        Result<List<Entry<byte[], byte[]>>> result = contractUtxoStorageService.batchSaveAndDeleteUTXO(fromList, toList);
        if (result.isFailed() || result.getData() == null) {
            return Result.getFailed();
        }
        // 回滚余额, 找到已删除的from 加回去, 筛选出已保存的to 减掉
        contractBalanceManager.refreshBalance(froms, contractTos);
    }
    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) ArrayList(java.util.ArrayList) List(java.util.List)

Example 19 with Entry

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

the class ContractAddressStorageServiceImpl method getContractInfoList.

@Override
public Result<List<ContractAddressInfoPo>> getContractInfoList(byte[] creater) {
    List<Entry<byte[], ContractAddressInfoPo>> list = dbService.entryList(ContractStorageConstant.DB_NAME_CONTRACT_ADDRESS, ContractAddressInfoPo.class);
    if (list == null || list.size() == 0) {
        return Result.getFailed(KernelErrorCode.DATA_NOT_FOUND);
    }
    List<ContractAddressInfoPo> resultList = new ArrayList<>();
    ContractAddressInfoPo po;
    for (Entry<byte[], ContractAddressInfoPo> entry : list) {
        po = entry.getValue();
        if (Arrays.equals(creater, po.getSender())) {
            po.setContractAddress(entry.getKey());
            resultList.add(po);
        }
    }
    Result<List<ContractAddressInfoPo>> result = Result.getSuccess();
    result.setData(resultList);
    return result;
}
Also used : ContractAddressInfoPo(io.nuls.contract.storage.po.ContractAddressInfoPo) Entry(io.nuls.db.model.Entry) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList)

Example 20 with Entry

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

the class LevelDBManager method entryList.

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

                @Override
                public int compare(Entry<byte[], T> o1, Entry<byte[], T> 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)

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