use of io.nuls.kernel.utils.VarInt in project nuls by nuls-io.
the class LocalUtxoServiceImpl method rollbackUnlockTxCoinData.
@Override
public Result<List<byte[]>> rollbackUnlockTxCoinData(Transaction tx) {
List<byte[]> addresses = new ArrayList<>();
CoinData coinData = tx.getCoinData();
if (coinData != null) {
// lock utxo - to
List<Coin> tos = coinData.getTo();
for (int i = 0, length = tos.size(); i < length; i++) {
Coin to = tos.get(i);
if (to.getLockTime() == -1L) {
try {
byte[] outKey = ArraysTool.concatenate(tx.getHash().serialize(), new VarInt(i).encode());
saveUTXO(outKey, to.serialize());
addresses.add(to.getAddress());
} catch (IOException e) {
throw new NulsRuntimeException(e);
}
break;
}
}
}
return Result.getSuccess().setData(addresses);
}
use of io.nuls.kernel.utils.VarInt 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();
}
use of io.nuls.kernel.utils.VarInt 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);
}
}
use of io.nuls.kernel.utils.VarInt in project nuls by nuls-io.
the class ConvertCoinTool method convertCoin.
public static Coin convertCoin(InputDto utxo) {
Coin coin = new Coin();
byte[] txHashBytes = Hex.decode(utxo.getFromHash());
coin.setOwner(ArraysTool.concatenate(txHashBytes, new VarInt(utxo.getFromIndex()).encode()));
coin.setLockTime(utxo.getLockTime());
coin.setNa(Na.valueOf(utxo.getValue()));
coin.setTempOwner(AddressTool.getAddress(utxo.getAddress()));
return coin;
}
use of io.nuls.kernel.utils.VarInt in project nuls by nuls-io.
the class ConsensusTool method getStopAgentCoinData.
public static CoinData getStopAgentCoinData(Agent agent, long lockTime, Long hight) throws IOException {
if (null == agent) {
return null;
}
NulsDigestData createTxHash = agent.getTxHash();
CoinData coinData = new CoinData();
List<Coin> toList = new ArrayList<>();
if (agent.getAgentAddress()[2] == NulsContext.P2SH_ADDRESS_TYPE) {
Script scriptPubkey = SignatureUtil.createOutputScript(agent.getAgentAddress());
toList.add(new Coin(scriptPubkey.getProgram(), agent.getDeposit(), lockTime));
} else {
toList.add(new Coin(agent.getAgentAddress(), agent.getDeposit(), lockTime));
}
coinData.setTo(toList);
CreateAgentTransaction transaction = (CreateAgentTransaction) ledgerService.getTx(createTxHash);
if (null == transaction) {
throw new NulsRuntimeException(TransactionErrorCode.TX_NOT_EXIST);
}
List<Coin> fromList = new ArrayList<>();
for (int index = 0; index < transaction.getCoinData().getTo().size(); index++) {
Coin coin = transaction.getCoinData().getTo().get(index);
if (coin.getNa().equals(agent.getDeposit()) && coin.getLockTime() == -1L) {
coin.setOwner(ArraysTool.concatenate(transaction.getHash().serialize(), new VarInt(index).encode()));
fromList.add(coin);
break;
}
}
if (fromList.isEmpty()) {
throw new NulsRuntimeException(KernelErrorCode.DATA_ERROR);
}
coinData.setFrom(fromList);
List<Deposit> deposits = PocConsensusContext.getChainManager().getMasterChain().getChain().getDepositList();
List<String> addressList = new ArrayList<>();
Map<String, Coin> toMap = new HashMap<>();
long blockHeight = null == hight ? -1 : hight;
for (Deposit deposit : deposits) {
if (deposit.getDelHeight() > 0 && (blockHeight <= 0 || deposit.getDelHeight() < blockHeight)) {
continue;
}
if (!deposit.getAgentHash().equals(agent.getTxHash())) {
continue;
}
DepositTransaction dtx = (DepositTransaction) ledgerService.getTx(deposit.getTxHash());
Coin fromCoin = null;
for (Coin coin : dtx.getCoinData().getTo()) {
if (!coin.getNa().equals(deposit.getDeposit()) || coin.getLockTime() != -1L) {
continue;
}
fromCoin = new Coin(ArraysTool.concatenate(dtx.getHash().serialize(), new VarInt(0).encode()), coin.getNa(), coin.getLockTime());
fromCoin.setLockTime(-1L);
fromList.add(fromCoin);
break;
}
String address = AddressTool.getStringAddressByBytes(deposit.getAddress());
Coin coin = toMap.get(address);
if (null == coin) {
if (deposit.getAddress()[2] == NulsContext.P2SH_ADDRESS_TYPE) {
Script scriptPubkey = SignatureUtil.createOutputScript(deposit.getAddress());
coin = new Coin(scriptPubkey.getProgram(), deposit.getDeposit(), 0);
} else {
coin = new Coin(deposit.getAddress(), deposit.getDeposit(), 0);
}
addressList.add(address);
toMap.put(address, coin);
} else {
coin.setNa(coin.getNa().add(fromCoin.getNa()));
}
}
for (String address : addressList) {
coinData.getTo().add(toMap.get(address));
}
return coinData;
}
Aggregations