use of io.nuls.kernel.exception.NulsException in project nuls by nuls-io.
the class UtxoResource method getAllUtxoByAddress.
private List<Coin> getAllUtxoByAddress(String address) {
List<Coin> coinList = new ArrayList<>();
byte[] addressBytes = AddressTool.getAddress(address);
List<Entry<byte[], byte[]>> coinBytesList = utxoLedgerUtxoStorageService.getAllUtxoEntryBytes();
Coin coin;
for (Entry<byte[], byte[]> coinEntryBytes : coinBytesList) {
coin = new Coin();
try {
coin.parse(coinEntryBytes.getValue(), 0);
} catch (NulsException e) {
Log.info("parse coin form db error");
continue;
}
// if (Arrays.equals(coin.(), addressBytes))
if (Arrays.equals(coin.getAddress(), addressBytes)) {
coin.setOwner(coinEntryBytes.getKey());
coinList.add(coin);
}
}
Collections.sort(coinList, CoinComparator.getInstance());
return coinList;
}
use of io.nuls.kernel.exception.NulsException in project nuls by nuls-io.
the class UtxoLedgerServiceImpl method saveTx.
@Override
public Result saveTx(Transaction tx) throws NulsException {
if (tx == null) {
return Result.getFailed(LedgerErrorCode.NULL_PARAMETER);
}
try {
// 保存交易
Result result = utxoLedgerTransactionStorageService.saveTx(tx);
if (result.isFailed()) {
Result rollbackResult = rollbackTx(tx);
if (rollbackResult.isFailed()) {
throw new NulsException(rollbackResult.getErrorCode());
}
}
// 保存CoinData
result = saveCoinData(tx);
if (result.isFailed()) {
Result rollbackResult = rollbackCoinData(tx);
if (rollbackResult.isFailed()) {
throw new NulsException(rollbackResult.getErrorCode());
}
}
return result;
} catch (IOException e) {
Log.error(e);
Result rollbackResult = rollbackTx(tx);
if (rollbackResult.isFailed()) {
throw new NulsException(rollbackResult.getErrorCode());
}
return Result.getFailed(KernelErrorCode.IO_ERROR);
}
}
use of io.nuls.kernel.exception.NulsException in project nuls by nuls-io.
the class UtxoLedgerServiceImpl method rollbackTx.
@Override
public Result rollbackTx(Transaction tx) throws NulsException {
if (tx == null) {
return Result.getFailed(LedgerErrorCode.NULL_PARAMETER);
}
try {
// 回滚CoinData
Result result = rollbackCoinData(tx);
if (result.isFailed()) {
Result recoveryResult = saveCoinData(tx);
if (recoveryResult.isFailed()) {
throw new NulsException(recoveryResult.getErrorCode());
}
return result;
}
// 回滚交易
result = utxoLedgerTransactionStorageService.deleteTx(tx);
if (result.isFailed()) {
Result recoveryResult = saveTx(tx);
if (recoveryResult.isFailed()) {
throw new NulsException(recoveryResult.getErrorCode());
}
}
return result;
} catch (IOException e) {
Log.error(e);
Result rollbackResult = saveTx(tx);
if (rollbackResult.isFailed()) {
throw new NulsException(rollbackResult.getErrorCode());
}
return Result.getFailed(KernelErrorCode.IO_ERROR);
}
}
use of io.nuls.kernel.exception.NulsException in project nuls by nuls-io.
the class UtxoLedgerUtxoStorageServiceImpl method getUtxo.
@Override
public Coin getUtxo(byte[] owner) {
byte[] utxoBytes = getUtxoBytes(owner);
Coin coin = null;
try {
if (utxoBytes != null) {
coin = new Coin();
coin.parse(utxoBytes, 0);
}
} catch (NulsException e) {
Log.error(e);
return null;
}
return coin;
}
use of io.nuls.kernel.exception.NulsException in project nuls by nuls-io.
the class ForkChainProcess method changeChain.
private boolean changeChain(ChainContainer newMasterChain, ChainContainer originalForkChain, List<Object[]> verifyResultList) throws NulsException, IOException {
if (newMasterChain == null || originalForkChain == null || verifyResultList == null) {
return false;
}
// Now the master chain, the forked chain after the switch, needs to be put into the list of chains to be verified.
// 现在的主链,在切换之后的分叉链,需要放入待验证链列表里面
ChainContainer oldChain = chainManager.getMasterChain().getAfterTheForkChain(originalForkChain);
// rollbackTransaction
List<Block> rollbackBlockList = oldChain.getChain().getAllBlockList();
ChainLog.debug("rollbackTransaction the master chain , need rollbackTransaction block count is {}, master chain is {} : {} - {} , service best block : {} - {}", rollbackBlockList.size(), chainManager.getMasterChain().getChain().getId(), chainManager.getBestBlock().getHeader().getHeight(), chainManager.getBestBlock().getHeader().getHash(), blockService.getBestBlock().getData().getHeader().getHeight(), blockService.getBestBlock().getData().getHeader().getHash());
// Need descending order
// 需要降序排列
Collections.reverse(rollbackBlockList);
if (rollbackBlockList != null && rollbackBlockList.size() > 0 && rollbackBlockList.get(0).getHeader().getHeight() != chainManager.getMasterChain().getBestBlock().getHeader().getHeight()) {
Log.error("------------------------回滚的起始高度不是主链最新高度");
Log.error("----------------------- masterChain:" + chainManager.getMasterChain().getBestBlock().getHeader().getHeight() + ",hash:" + chainManager.getMasterChain().getBestBlock().getHeader().getHash().getDigestHex());
Log.error("----------------------- rollbackBlockList:" + rollbackBlockList.get(0).getHeader().getHeight() + ",hash:" + rollbackBlockList.get(0).getHeader().getHash().getDigestHex());
}
boolean rollbackResult = rollbackBlocks(rollbackBlockList);
if (!rollbackResult) {
return false;
}
boolean changeSuccess = true;
List<Block> successList = new ArrayList<>();
try {
changeSuccess = doChange(successList, originalForkChain, verifyResultList);
} catch (Exception e) {
Log.error(e);
changeSuccess = false;
}
ChainLog.debug("add new blocks complete, result {}, success count is {} , now service best block : {} - {}", changeSuccess, successList.size(), blockService.getBestBlock().getData().getHeader().getHeight(), blockService.getBestBlock().getData().getHeader().getHash());
if (changeSuccess) {
chainManager.setMasterChain(newMasterChain);
newMasterChain.initRound();
NulsContext.getInstance().setBestBlock(newMasterChain.getBestBlock());
if (oldChain.getChain().getAllBlockList().size() > 0) {
Collections.reverse(rollbackBlockList);
chainManager.getChains().add(oldChain);
}
} else {
// Fallback status
// 回退状态
Collections.reverse(successList);
for (Block rollBlock : successList) {
Result rs = blockService.rollbackBlock(rollBlock);
if (rs.isSuccess()) {
// 回滚版本更新统计数据
nulsProtocolProcess.processProtocolRollback(rollBlock.getHeader());
randomSeedService.rollbackBlock(rollBlock.getHeader());
}
RewardStatisticsProcess.rollbackBlock(rollBlock);
}
Collections.reverse(rollbackBlockList);
for (Block addBlock : rollbackBlockList) {
nulsProtocolProcess.processProtocolUpGrade(addBlock.getHeader());
Result rs = blockService.saveBlock(addBlock);
if (!rs.isSuccess()) {
nulsProtocolProcess.processProtocolRollback(addBlock.getHeader());
}
RewardStatisticsProcess.addBlock(addBlock);
}
}
return changeSuccess;
}
Aggregations