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;
}
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();
}
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();
}
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;
}
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;
}
Aggregations