use of io.nuls.kernel.exception.NulsRuntimeException 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.exception.NulsRuntimeException 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;
}
use of io.nuls.kernel.exception.NulsRuntimeException in project nuls by nuls-io.
the class BaseTest method createBlock.
protected Block createBlock() {
// new a block header
BlockHeader blockHeader = new BlockHeader();
blockHeader.setHeight(0);
blockHeader.setPreHash(NulsDigestData.calcDigestData("00000000000".getBytes()));
blockHeader.setTime(1L);
blockHeader.setTxCount(1);
blockHeader.setMerkleHash(NulsDigestData.calcDigestData(new byte[20]));
// add a round data
BlockRoundData roundData = new BlockRoundData();
roundData.setConsensusMemberCount(1);
roundData.setPackingIndexOfRound(1);
roundData.setRoundIndex(1);
roundData.setRoundStartTime(1L);
try {
blockHeader.setExtend(roundData.serialize());
} catch (IOException e) {
throw new NulsRuntimeException(e);
}
// new a block of height 0
Block block = new Block();
block.setHeader(blockHeader);
List<Transaction> txs = new ArrayList<>();
block.setTxs(txs);
Transaction tx = new TestTransaction();
txs.add(tx);
List<NulsDigestData> txHashList = block.getTxHashList();
blockHeader.setMerkleHash(NulsDigestData.calcMerkleDigestData(txHashList));
NulsSignData signData = signDigest(blockHeader.getHash().getDigestBytes(), ecKey);
BlockSignature sig = new BlockSignature();
sig.setSignData(signData);
sig.setPublicKey(ecKey.getPubKey());
blockHeader.setBlockSignature(sig);
return block;
}
use of io.nuls.kernel.exception.NulsRuntimeException in project nuls by nuls-io.
the class StopAgentTxProcessor method onRollback.
@Override
public Result onRollback(StopAgentTransaction tx, Object secondaryData) {
AgentPo agentPo = agentStorageService.get(tx.getTxData().getCreateTxHash());
if (null == agentPo || agentPo.getDelHeight() < 0) {
throw new NulsRuntimeException(PocConsensusErrorCode.AGENT_NOT_EXIST);
}
agentPo.setDelHeight(-1L);
List<DepositPo> depositPoList = depositStorageService.getList();
for (DepositPo depositPo : depositPoList) {
if (depositPo.getDelHeight() != tx.getBlockHeight()) {
continue;
}
if (!depositPo.getAgentHash().equals(agentPo.getHash())) {
continue;
}
depositPo.setDelHeight(-1L);
depositStorageService.save(depositPo);
}
boolean b = agentStorageService.save(agentPo);
if (!b) {
return Result.getFailed(PocConsensusErrorCode.UPDATE_AGENT_FAILED);
}
return Result.getSuccess();
}
use of io.nuls.kernel.exception.NulsRuntimeException in project nuls by nuls-io.
the class StopAgentTxProcessor method onCommit.
@Override
public Result onCommit(StopAgentTransaction tx, Object secondaryData) {
BlockHeader header = (BlockHeader) secondaryData;
if (tx.getTime() < (header.getTime() - 300000L)) {
return Result.getFailed(PocConsensusErrorCode.LOCK_TIME_NOT_REACHED);
}
AgentPo agentPo = agentStorageService.get(tx.getTxData().getCreateTxHash());
if (null == agentPo || agentPo.getDelHeight() > 0) {
throw new NulsRuntimeException(PocConsensusErrorCode.AGENT_NOT_EXIST);
}
List<DepositPo> depositPoList = depositStorageService.getList();
for (DepositPo depositPo : depositPoList) {
if (depositPo.getDelHeight() > -1L) {
continue;
}
if (!depositPo.getAgentHash().equals(agentPo.getHash())) {
continue;
}
depositPo.setDelHeight(tx.getBlockHeight());
depositStorageService.save(depositPo);
}
agentPo.setDelHeight(tx.getBlockHeight());
tx.getTxData().setAddress(agentPo.getAgentAddress());
boolean b = agentStorageService.save(agentPo);
if (!b) {
return Result.getFailed(PocConsensusErrorCode.UPDATE_AGENT_FAILED);
}
return Result.getSuccess();
}
Aggregations