use of io.nuls.consensus.poc.protocol.entity.YellowPunishData 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.consensus.poc.protocol.entity.YellowPunishData 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.consensus.poc.protocol.entity.YellowPunishData in project nuls by nuls-io.
the class ConsensusTool method createYellowPunishTx.
public static YellowPunishTransaction createYellowPunishTx(Block preBlock, MeetingMember self, MeetingRound round) throws NulsException, IOException {
BlockExtendsData preBlockRoundData = new BlockExtendsData(preBlock.getHeader().getExtend());
if (self.getRoundIndex() - preBlockRoundData.getRoundIndex() > 1) {
return null;
}
int yellowCount = 0;
if (self.getRoundIndex() == preBlockRoundData.getRoundIndex() && self.getPackingIndexOfRound() != preBlockRoundData.getPackingIndexOfRound() + 1) {
yellowCount = self.getPackingIndexOfRound() - preBlockRoundData.getPackingIndexOfRound() - 1;
}
if (self.getRoundIndex() != preBlockRoundData.getRoundIndex() && (self.getPackingIndexOfRound() != 1 || preBlockRoundData.getPackingIndexOfRound() != preBlockRoundData.getConsensusMemberCount())) {
yellowCount = self.getPackingIndexOfRound() + preBlockRoundData.getConsensusMemberCount() - preBlockRoundData.getPackingIndexOfRound() - 1;
}
if (yellowCount == 0) {
return null;
}
List<byte[]> addressList = new ArrayList<>();
for (int i = 1; i <= yellowCount; i++) {
int index = self.getPackingIndexOfRound() - i;
if (index > 0) {
MeetingMember member = round.getMember(index);
if (member.getAgent() == null) {
continue;
} else if (member.getAgent().getDelHeight() > 0) {
continue;
}
addressList.add(member.getAgentAddress());
} else {
MeetingRound preRound = round.getPreRound();
MeetingMember member = preRound.getMember(index + preRound.getMemberCount());
if (member.getAgent() == null || member.getAgent().getDelHeight() > 0) {
continue;
}
addressList.add(member.getAgentAddress());
}
}
if (addressList.isEmpty()) {
return null;
}
YellowPunishTransaction punishTx = new YellowPunishTransaction();
YellowPunishData data = new YellowPunishData();
data.setAddressList(addressList);
punishTx.setTxData(data);
punishTx.setTime(self.getPackEndTime());
punishTx.setHash(NulsDigestData.calcDigestData(punishTx.serializeForHash()));
return punishTx;
}
Aggregations