Search in sources :

Example 1 with YellowPunishData

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();
}
Also used : BlockExtendsData(io.nuls.consensus.poc.model.BlockExtendsData) YellowPunishData(io.nuls.consensus.poc.protocol.entity.YellowPunishData) ArrayList(java.util.ArrayList) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) BlockHeader(io.nuls.kernel.model.BlockHeader) PunishLogPo(io.nuls.consensus.poc.storage.po.PunishLogPo)

Example 2 with YellowPunishData

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();
}
Also used : BlockExtendsData(io.nuls.consensus.poc.model.BlockExtendsData) YellowPunishData(io.nuls.consensus.poc.protocol.entity.YellowPunishData) ArrayList(java.util.ArrayList) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) BlockHeader(io.nuls.kernel.model.BlockHeader) PunishLogPo(io.nuls.consensus.poc.storage.po.PunishLogPo)

Example 3 with YellowPunishData

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;
}
Also used : BlockExtendsData(io.nuls.consensus.poc.model.BlockExtendsData) YellowPunishTransaction(io.nuls.consensus.poc.protocol.tx.YellowPunishTransaction) YellowPunishData(io.nuls.consensus.poc.protocol.entity.YellowPunishData) MeetingMember(io.nuls.consensus.poc.model.MeetingMember) MeetingRound(io.nuls.consensus.poc.model.MeetingRound)

Aggregations

BlockExtendsData (io.nuls.consensus.poc.model.BlockExtendsData)3 YellowPunishData (io.nuls.consensus.poc.protocol.entity.YellowPunishData)3 PunishLogPo (io.nuls.consensus.poc.storage.po.PunishLogPo)2 NulsRuntimeException (io.nuls.kernel.exception.NulsRuntimeException)2 BlockHeader (io.nuls.kernel.model.BlockHeader)2 ArrayList (java.util.ArrayList)2 MeetingMember (io.nuls.consensus.poc.model.MeetingMember)1 MeetingRound (io.nuls.consensus.poc.model.MeetingRound)1 YellowPunishTransaction (io.nuls.consensus.poc.protocol.tx.YellowPunishTransaction)1