use of io.nuls.kernel.exception.NulsRuntimeException in project nuls by nuls-io.
the class RedPunishTxProcessor method onRollback.
@Override
public Result onRollback(RedPunishTransaction tx, Object secondaryData) {
RedPunishData punishData = tx.getTxData();
List<AgentPo> agentList = agentStorageService.getList();
AgentPo agent = null;
for (AgentPo agentPo : agentList) {
if (agentPo.getDelHeight() <= 0) {
continue;
}
if (Arrays.equals(agentPo.getAgentAddress(), punishData.getAddress())) {
agent = agentPo;
break;
}
}
if (null == agent) {
return Result.getFailed(PocConsensusErrorCode.AGENT_NOT_EXIST);
}
List<DepositPo> depositPoList = depositStorageService.getList();
List<DepositPo> updatedList = new ArrayList<>();
for (DepositPo po : depositPoList) {
po.setDelHeight(-1);
boolean success = this.depositStorageService.save(po);
if (!success) {
for (DepositPo po2 : depositPoList) {
po2.setDelHeight(tx.getBlockHeight());
this.depositStorageService.save(po2);
}
return Result.getFailed(PocConsensusErrorCode.UPDATE_DEPOSIT_FAILED);
}
updatedList.add(po);
}
AgentPo agentPo = agent;
agentPo.setDelHeight(-1L);
boolean success = agentStorageService.save(agentPo);
if (!success) {
for (DepositPo po2 : depositPoList) {
po2.setDelHeight(tx.getBlockHeight());
this.depositStorageService.save(po2);
}
return Result.getFailed(PocConsensusErrorCode.UPDATE_AGENT_FAILED);
}
byte[] key = ArraysTool.concatenate(punishData.getAddress(), new byte[] { PunishType.RED.getCode() }, SerializeUtils.uint64ToByteArray(tx.getBlockHeight()), new byte[] { 0 });
success = storageService.delete(key);
if (!success) {
for (DepositPo po2 : depositPoList) {
po2.setDelHeight(tx.getBlockHeight());
this.depositStorageService.save(po2);
}
agentPo.setDelHeight(tx.getBlockHeight());
agentStorageService.save(agentPo);
throw new NulsRuntimeException(TransactionErrorCode.ROLLBACK_TRANSACTION_FAILED);
}
return Result.getSuccess();
}
use of io.nuls.kernel.exception.NulsRuntimeException in project nuls by nuls-io.
the class YellowPunishTxProcessor method onCommit.
@Override
public Result onCommit(YellowPunishTransaction tx, Object secondaryData) {
YellowPunishData punishData = tx.getTxData();
BlockHeader header = (BlockHeader) secondaryData;
BlockExtendsData roundData = new BlockExtendsData(header.getExtend());
List<PunishLogPo> savedList = new ArrayList<>();
int index = 1;
for (byte[] address : punishData.getAddressList()) {
PunishLogPo po = new PunishLogPo();
po.setAddress(address);
po.setHeight(tx.getBlockHeight());
po.setRoundIndex(roundData.getRoundIndex());
po.setTime(tx.getTime());
po.setIndex(index++);
po.setType(PunishType.YELLOW.getCode());
boolean result = punishLogStorageService.save(po);
if (!result) {
for (PunishLogPo punishLogPo : savedList) {
punishLogStorageService.delete(getPoKey(punishLogPo.getAddress(), PunishType.YELLOW.getCode(), punishLogPo.getHeight(), punishLogPo.getIndex()));
}
throw new NulsRuntimeException(TransactionErrorCode.ROLLBACK_TRANSACTION_FAILED);
} else {
savedList.add(po);
}
}
return Result.getSuccess();
}
use of io.nuls.kernel.exception.NulsRuntimeException in project nuls by nuls-io.
the class YellowPunishTxProcessor method onRollback.
@Override
public Result onRollback(YellowPunishTransaction tx, Object secondaryData) {
YellowPunishData punishData = tx.getTxData();
List<byte[]> deletedList = new ArrayList<>();
int deleteIndex = 1;
for (byte[] address : punishData.getAddressList()) {
boolean result = punishLogStorageService.delete(this.getPoKey(address, PunishType.YELLOW.getCode(), tx.getBlockHeight(), deleteIndex++));
if (!result) {
BlockHeader header = (BlockHeader) secondaryData;
BlockExtendsData roundData = new BlockExtendsData(header.getExtend());
int index = 1;
for (byte[] bytes : deletedList) {
PunishLogPo po = new PunishLogPo();
po.setAddress(bytes);
po.setHeight(tx.getBlockHeight());
po.setRoundIndex(roundData.getRoundIndex());
po.setTime(tx.getTime());
po.setIndex(index++);
po.setType(PunishType.YELLOW.getCode());
punishLogStorageService.save(po);
}
throw new NulsRuntimeException(TransactionErrorCode.ROLLBACK_TRANSACTION_FAILED);
} else {
deletedList.add(address);
}
}
return Result.getSuccess();
}
use of io.nuls.kernel.exception.NulsRuntimeException in project nuls by nuls-io.
the class BaseNulsData method serialize.
@Override
public final byte[] serialize() throws IOException {
ByteArrayOutputStream bos = null;
try {
int size = size();
bos = new UnsafeByteArrayOutputStream(size);
NulsOutputStreamBuffer buffer = new NulsOutputStreamBuffer(bos);
if (size == 0) {
bos.write(NulsConstant.PLACE_HOLDER);
} else {
serializeToStream(buffer);
}
byte[] bytes = bos.toByteArray();
if (bytes.length != this.size()) {
throw new NulsRuntimeException(KernelErrorCode.SERIALIZE_ERROR);
}
return bytes;
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
throw e;
}
}
}
}
use of io.nuls.kernel.exception.NulsRuntimeException in project nuls by nuls-io.
the class ModuleService method startModules.
public void startModules(Map<String, String> bootstrapClasses) {
moduleManager.setModulesCfg(bootstrapClasses);
List<String> keyList = new ArrayList<>(bootstrapClasses.keySet());
for (String key : keyList) {
try {
startModule(key, bootstrapClasses.get(key));
} catch (Exception e) {
throw new NulsRuntimeException(e);
}
}
}
Aggregations