Search in sources :

Example 6 with BlockExtendsData

use of io.nuls.consensus.poc.model.BlockExtendsData in project nuls by nuls-io.

the class CacheLoader method loadBlockHeaders.

/**
 * @param size
 * @return
 */
public List<BlockHeader> loadBlockHeaders(int size) {
    List<BlockHeader> blockHeaderList = new ArrayList<>();
    BlockHeader blockHeader = blockService.getBestBlockHeader().getData();
    if (null == blockHeader) {
        return blockHeaderList;
    }
    BlockExtendsData roundData = new BlockExtendsData(blockHeader.getExtend());
    long breakRoundIndex = roundData.getRoundIndex() - size;
    while (true) {
        if (blockHeader == null) {
            break;
        }
        blockHeaderList.add(0, blockHeader);
        if (blockHeader.getHeight() == 0L) {
            break;
        }
        NulsDigestData preHash = blockHeader.getPreHash();
        blockHeader = blockService.getBlockHeader(preHash).getData();
        BlockExtendsData blockRoundData = new BlockExtendsData(blockHeader.getExtend());
        if (blockRoundData.getRoundIndex() <= breakRoundIndex) {
            break;
        }
    }
    return blockHeaderList;
}
Also used : BlockExtendsData(io.nuls.consensus.poc.model.BlockExtendsData) ArrayList(java.util.ArrayList) NulsDigestData(io.nuls.kernel.model.NulsDigestData) BlockHeader(io.nuls.kernel.model.BlockHeader)

Example 7 with BlockExtendsData

use of io.nuls.consensus.poc.model.BlockExtendsData 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 8 with BlockExtendsData

use of io.nuls.consensus.poc.model.BlockExtendsData 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 9 with BlockExtendsData

use of io.nuls.consensus.poc.model.BlockExtendsData in project nuls by nuls-io.

the class BifurcationUtil method recordEvidence.

/**
 * 统计并验证分叉出块地址的证据,如果是连续的分叉则保存到证据集合中,不是连续的就清空
 *
 * @param agent            分叉的出块地址所有者节点
 * @param header           新收到的区块头
 * @param otherBlockHeader 本地当前以保存的最新区块头
 */
private void recordEvidence(Agent agent, BlockHeader header, BlockHeader otherBlockHeader) {
    // 验证出块地址PackingAddress,记录分叉的连续次数,如达到连续3轮则红牌惩罚
    String packingAddress = AddressTool.getStringAddressByBytes(agent.getPackingAddress());
    BlockExtendsData extendsData = new BlockExtendsData(header.getExtend());
    Evidence evidence = new Evidence(extendsData.getRoundIndex(), header, otherBlockHeader);
    if (!bifurcationEvidenceMap.containsKey(packingAddress)) {
        List<Evidence> list = new ArrayList<>();
        list.add(evidence);
        bifurcationEvidenceMap.put(packingAddress, list);
    } else {
        List<Evidence> evidenceList = bifurcationEvidenceMap.get(packingAddress);
        if (evidenceList.size() >= NulsContext.REDPUNISH_BIFURCATION) {
            return;
        }
        ListIterator<Evidence> iterator = evidenceList.listIterator();
        boolean isSerialRoundIndex = false;
        while (iterator.hasNext()) {
            Evidence e = iterator.next();
            // 如果与其中一个记录的轮次是连续的,则加入记录
            if (e.getRoundIndex() + 1 == extendsData.getRoundIndex()) {
                iterator.add(evidence);
                isSerialRoundIndex = true;
            }
        }
        if (!isSerialRoundIndex) {
            // 分叉不是连续的轮次,则清空记录(重置).
            bifurcationEvidenceMap.remove(packingAddress);
        }
    }
    bifurcationEvidenceStorageService.save(Evidence.bifurcationEvidenceMapToPoMap(bifurcationEvidenceMap));
}
Also used : BlockExtendsData(io.nuls.consensus.poc.model.BlockExtendsData) Evidence(io.nuls.consensus.poc.model.Evidence)

Example 10 with BlockExtendsData

use of io.nuls.consensus.poc.model.BlockExtendsData in project nuls by nuls-io.

the class GenesisBlock method fillHeader.

private void fillHeader(Map<String, Object> jsonMap) throws NulsException {
    Integer height = (Integer) jsonMap.get(CONFIG_FILED_HEIGHT);
    AssertUtil.canNotEmpty(height, KernelErrorCode.CONFIG_ERROR.getMsg());
    BlockHeader header = new BlockHeader();
    this.setHeader(header);
    header.setHeight(height);
    header.setTime(blockTime);
    header.setPreHash(NulsDigestData.calcDigestData(new byte[35]));
    header.setTxCount(this.getTxs().size());
    List<NulsDigestData> txHashList = new ArrayList<>();
    for (Transaction tx : this.getTxs()) {
        txHashList.add(tx.getHash());
    }
    header.setMerkleHash(NulsDigestData.calcMerkleDigestData(txHashList));
    BlockExtendsData data = new BlockExtendsData();
    data.setRoundIndex(1);
    data.setRoundStartTime(header.getTime() - ProtocolConstant.BLOCK_TIME_INTERVAL_SECOND * 1000);
    data.setConsensusMemberCount(1);
    data.setPackingIndexOfRound(1);
    try {
        header.setExtend(data.serialize());
    } catch (IOException e) {
        throw new NulsRuntimeException(e);
    }
    header.setHash(NulsDigestData.calcDigestData(header));
    BlockSignature p2PKHScriptSig = new BlockSignature();
    NulsSignData signData = this.signature(header.getHash().getDigestBytes());
    p2PKHScriptSig.setSignData(signData);
    p2PKHScriptSig.setPublicKey(getGenesisPubkey());
    header.setBlockSignature(p2PKHScriptSig);
}
Also used : BigInteger(java.math.BigInteger) CoinBaseTransaction(io.nuls.protocol.model.tx.CoinBaseTransaction) BlockExtendsData(io.nuls.consensus.poc.model.BlockExtendsData) BlockSignature(io.nuls.kernel.script.BlockSignature) ArrayList(java.util.ArrayList) NulsRuntimeException(io.nuls.kernel.exception.NulsRuntimeException) IOException(java.io.IOException)

Aggregations

BlockExtendsData (io.nuls.consensus.poc.model.BlockExtendsData)16 BlockHeader (io.nuls.kernel.model.BlockHeader)7 ArrayList (java.util.ArrayList)6 PunishLogPo (io.nuls.consensus.poc.storage.po.PunishLogPo)5 NulsRuntimeException (io.nuls.kernel.exception.NulsRuntimeException)5 MeetingRound (io.nuls.consensus.poc.model.MeetingRound)3 YellowPunishData (io.nuls.consensus.poc.protocol.entity.YellowPunishData)3 MeetingMember (io.nuls.consensus.poc.model.MeetingMember)2 YellowPunishTransaction (io.nuls.consensus.poc.protocol.tx.YellowPunishTransaction)2 RandomSeedStatusPo (io.nuls.consensus.poc.storage.po.RandomSeedStatusPo)2 ProtocolContainer (io.nuls.protocol.base.version.ProtocolContainer)2 CoinBaseTransaction (io.nuls.protocol.model.tx.CoinBaseTransaction)2 ProtocolTempInfoPo (io.nuls.protocol.storage.po.ProtocolTempInfoPo)2 IOException (java.io.IOException)2 BlockData (io.nuls.consensus.poc.model.BlockData)1 Chain (io.nuls.consensus.poc.model.Chain)1 Evidence (io.nuls.consensus.poc.model.Evidence)1 Agent (io.nuls.consensus.poc.protocol.entity.Agent)1 Deposit (io.nuls.consensus.poc.protocol.entity.Deposit)1 RedPunishData (io.nuls.consensus.poc.protocol.entity.RedPunishData)1