use of io.nuls.kernel.exception.NulsException in project nuls by nuls-io.
the class BlockServiceImpl method rollbackBlock.
/**
* 回滚区块
* roll back the block to the store.
*
* @param block 完整区块/whole block
* @return 操作结果/operating result
* @throws NulsException 回滚区块有可能出现异常,请捕获后谨慎处理/There may be exceptions to the roll back block, please handle it carefully after capture.
*/
@Override
public Result rollbackBlock(Block block) throws NulsException {
if (null == block) {
return Result.getFailed(ProtocolErroeCode.BLOCK_IS_NULL);
}
boolean txsResult = this.rollbackTxList(block.getTxs(), block.getHeader(), true);
if (!txsResult) {
return Result.getFailed();
}
BlockHeaderPo po = new BlockHeaderPo();
po.setHash(block.getHeader().getHash());
po.setHeight(block.getHeader().getHeight());
po.setPreHash(block.getHeader().getPreHash());
Result result = this.blockHeaderStorageService.removeBlockHerader(po);
if (result.isFailed()) {
return result;
}
try {
accountLedgerService.rollbackTransactions(block.getTxs());
// 回滚合约相关交易
contractService.rollbackTransactionList(block.getTxs());
} catch (Exception e) {
Log.warn("rollbackTransaction local tx failed", e);
}
return result;
}
use of io.nuls.kernel.exception.NulsException in project nuls by nuls-io.
the class TransactionServiceImpl method rollbackTx.
/**
* 回滚交易时调用的方法,对交易相关的业务进行回退操作
* The method invoked when the transaction is rolled back and the transaction related business is returned.
*
* @param tx 操作的交易/The transaction of the operation
* @param secondaryData 辅助数据(可以为空)/Secondary data (available for null)
* @return 操作结果/operating results
*/
@Override
public Result rollbackTx(Transaction tx, Object secondaryData) {
if (null == tx) {
return Result.getSuccess();
}
List<TransactionProcessor> processorList = TransactionManager.getProcessorList(tx.getClass());
List<TransactionProcessor> rollbackedList = new ArrayList<>();
for (TransactionProcessor processor : processorList) {
Result result = processor.onRollback(tx, secondaryData);
if (result.isSuccess()) {
rollbackedList.add(processor);
} else {
for (int i = rollbackedList.size() - 1; i >= 0; i--) {
TransactionProcessor processor1 = rollbackedList.get(i);
processor1.onCommit(tx, secondaryData);
}
return result;
}
}
try {
ledgerService.rollbackTx(tx);
} catch (NulsException e) {
Log.error(e);
return Result.getFailed(e.getErrorCode());
}
return Result.getSuccess();
}
use of io.nuls.kernel.exception.NulsException in project nuls by nuls-io.
the class TransactionServiceImpl method broadcastTx.
/**
* 广播交易给连接的其他对等节点
* The broadcast transaction gives the connection to other peers.
*
* @param tx 完整交易/the whole transaction
* @return 广播结果/Broadcast the results
*/
@Override
public Result broadcastTx(Transaction tx) {
try {
ValidateResult validateResult = contractService.baseValidate(tx);
if (validateResult.isFailed()) {
return validateResult;
}
} catch (NulsException e) {
Log.error(e);
return Result.getFailed();
}
TransactionMessage message = new TransactionMessage();
message.setMsgBody(tx);
consensusService.newTx(tx);
// return Result.getSuccess();
return messageBusService.broadcast(message, null, true, 50);
}
use of io.nuls.kernel.exception.NulsException in project nuls by nuls-io.
the class BaseProtocolsModuleBootstrap method start.
@Override
public void start() {
this.waitForDependencyRunning(MessageBusConstant.MODULE_ID_MESSAGE_BUS);
this.waitForDependencyInited(ConsensusConstant.MODULE_ID_CONSENSUS, NetworkConstant.NETWORK_MODULE_ID);
BlockService blockService = NulsContext.getServiceBean(BlockService.class);
Block block0 = blockService.getGengsisBlock().getData();
Block genesisBlock = NulsContext.getInstance().getGenesisBlock();
if (null == block0) {
try {
blockService.saveBlock(genesisBlock);
} catch (NulsException e) {
Log.error(e);
throw new NulsRuntimeException(e);
}
}
Block block = blockService.getBestBlock().getData();
while (null != block && block.verify().isFailed()) {
try {
blockService.rollbackBlock(block);
} catch (NulsException e) {
Log.error(e);
}
block = blockService.getBlock(block.getHeader().getPreHash()).getData();
}
if (null != block) {
NulsContext.getInstance().setBestBlock(block);
this.initHandlers();
((DownloadServiceImpl) NulsContext.getServiceBean(DownloadService.class)).start();
} else {
start();
}
}
use of io.nuls.kernel.exception.NulsException in project nuls by nuls-io.
the class TxGroup method parse.
@Override
public void parse(NulsByteBuffer byteBuffer) throws NulsException {
requestHash = byteBuffer.readHash();
long txCount = byteBuffer.readVarInt();
this.txList = new ArrayList<>();
for (int i = 0; i < txCount; i++) {
try {
this.txList.add(byteBuffer.readTransaction());
} catch (Exception e) {
throw new NulsException(e);
}
}
initTxMap();
}
Aggregations