use of io.nuls.consensus.poc.model.BlockExtendsData in project nuls by nuls-io.
the class ConsensusProcess method hasReceiveNewestBlock.
private boolean hasReceiveNewestBlock(MeetingMember self, MeetingRound round) {
BlockHeader bestBlockHeader = blockService.getBestBlockHeader().getData();
byte[] packingAddress = bestBlockHeader.getPackingAddress();
int thisIndex = self.getPackingIndexOfRound();
MeetingMember preMember;
if (thisIndex == 1) {
MeetingRound preRound = round.getPreRound();
if (preRound == null) {
Log.error("PreRound is null!");
return true;
}
preMember = preRound.getMember(preRound.getMemberCount());
} else {
preMember = round.getMember(self.getPackingIndexOfRound() - 1);
}
if (preMember == null) {
return true;
}
byte[] preBlockPackingAddress = preMember.getPackingAddress();
long thisRoundIndex = preMember.getRoundIndex();
int thisPackageIndex = preMember.getPackingIndexOfRound();
BlockExtendsData blockRoundData = new BlockExtendsData(bestBlockHeader.getExtend());
long roundIndex = blockRoundData.getRoundIndex();
int packageIndex = blockRoundData.getPackingIndexOfRound();
if (Arrays.equals(packingAddress, preBlockPackingAddress) && thisRoundIndex == roundIndex && thisPackageIndex == packageIndex) {
return true;
} else {
return false;
}
}
use of io.nuls.consensus.poc.model.BlockExtendsData in project nuls by nuls-io.
the class NulsProtocolProcess method calculateTempProtocols.
private void calculateTempProtocols(BlockExtendsData extendsData, BlockHeader header, Set<String> memberAddressSet) {
for (ProtocolTempInfoPo tempInfoPo : NulsVersionManager.getTempProtocolContainers().values()) {
// 如果容器的轮次小于当前出块节点的轮次,说明是新的一轮开始
if (tempInfoPo.getStatus() != ProtocolContainer.VALID && tempInfoPo.getRoundIndex() < extendsData.getRoundIndex()) {
// 获取上一个块,得到上一轮的共识节点数量
Result<BlockHeader> result = getBlockService().getBlockHeader(header.getPreHash());
BlockHeader preHeader = result.getData();
BlockExtendsData preExtendsData = new BlockExtendsData(preHeader.getExtend());
// 计算上一轮的覆盖率
int rate = calcRate(tempInfoPo, preExtendsData);
tempInfoPo.setCurrentPercent(rate);
if (rate < tempInfoPo.getPercent()) {
tempInfoPo.setStatus(ProtocolContainer.INVALID);
tempInfoPo.setCurrentDelay(0);
} else {
tempInfoPo.setStatus(ProtocolContainer.DELAY_LOCK);
}
// 新的轮次开始时,也许会有节点时上一轮已经退出的,因此在这一轮里要去掉
Iterator<String> iterator = tempInfoPo.getAddressSet().iterator();
while (iterator.hasNext()) {
String address = iterator.next();
if (!memberAddressSet.contains(address)) {
iterator.remove();
}
}
}
}
}
use of io.nuls.consensus.poc.model.BlockExtendsData in project nuls by nuls-io.
the class NulsProtocolProcess method calculateProtocols.
private void calculateProtocols(BlockExtendsData extendsData, BlockHeader header, Set<String> memberAddressSet) {
for (ProtocolContainer container : NulsVersionManager.getAllProtocolContainers().values()) {
// 如果容器的轮次小于当前出块节点的轮次,说明是新的一轮开始
if (container.getStatus() != ProtocolContainer.VALID && container.getRoundIndex() < extendsData.getRoundIndex()) {
// 获取上一个块,得到上一轮的共识节点数量
Result<BlockHeader> result = getBlockService().getBlockHeader(header.getPreHash());
BlockHeader preHeader = result.getData();
BlockExtendsData preExtendsData = new BlockExtendsData(preHeader.getExtend());
// 计算上一轮的覆盖率
int rate = calcRate(container, preExtendsData);
container.setCurrentPercent(rate);
if (rate < container.getPercent()) {
container.setStatus(ProtocolContainer.INVALID);
container.setCurrentDelay(0);
} else {
container.setStatus(ProtocolContainer.DELAY_LOCK);
}
// 新的轮次开始时,也许会有节点时上一轮已经退出的,因此在这一轮里要去掉
Iterator<String> iterator = container.getAddressSet().iterator();
if (iterator != null) {
while (iterator.hasNext()) {
String address = iterator.next();
if (!memberAddressSet.contains(address)) {
iterator.remove();
}
}
}
}
}
}
use of io.nuls.consensus.poc.model.BlockExtendsData in project nuls by nuls-io.
the class NulsProtocolProcess method processProtocolUpGrade.
/**
* 版本升级
* 升级规则:
* 1.通过block.extend字段获取当前出块人的版本号
* 2.如果一个节点出块的版本号为N,之后若未收到过该节点继续出块,视为该出块节点版本号为N,
* 3.记录每一轮都有哪些节点出过哪些个版本的块,若某个版本达到覆盖率后,从下一轮开始进入延迟块数的累计
* 4.进入延迟块数累计的版本,若下一轮覆盖率未达标,则延迟块数清理,待重新达标后进入延迟块的统计
*
* @param blockHeader
*/
public void processProtocolUpGrade(BlockHeader blockHeader) {
BlockExtendsData extendsData = new BlockExtendsData(blockHeader.getExtend());
String packingAddress = blockHeader.getPackingAddressStr();
MeetingRound currentRound = PocConsensusContext.getChainManager().getMasterChain().getRoundManager().getRoundByIndex(extendsData.getRoundIndex());
// 临时处理为空的情况,为空是由于第一个版本的区块不包含版本信息字段
if (extendsData.getCurrentVersion() == null) {
extendsData.setCurrentVersion(1);
}
// 处理有人发送错误的版本格式情况
if (extendsData.getCurrentVersion() < 1) {
return;
}
NulsVersionManager.getConsensusVersionMap().put(packingAddress, extendsData.getCurrentVersion());
calculateCoverageEveryRound(blockHeader, extendsData, currentRound);
containerRemoveAddress(packingAddress);
// 收到的新块版本号大于当前主网版本时
if (extendsData.getCurrentVersion() > NulsContext.MAIN_NET_VERSION) {
// 能找到本地对应的版本号配置,走正常升级流程
ProtocolContainer container = NulsVersionManager.getProtocolContainer(extendsData.getCurrentVersion());
if (container != null) {
processHigherVersion(blockHeader, extendsData, packingAddress, container);
} else {
// 不能找到配置,说明当前也许有更高版本的配置,走临时保存流程
ProtocolTempInfoPo tempInfoPo = NulsVersionManager.getTempProtocolContainer(extendsData.getProtocolKey());
if (tempInfoPo == null) {
tempInfoPo = ProtocolTransferTool.createProtocolTempInfoPo(extendsData);
NulsVersionManager.addTempProtocolContainer(tempInfoPo);
}
processTempHigherVersion(blockHeader, extendsData, packingAddress, tempInfoPo);
}
} else {
calcDelay(blockHeader, extendsData);
calcTempDelay(blockHeader, extendsData);
}
saveProtocol(blockHeader);
}
use of io.nuls.consensus.poc.model.BlockExtendsData in project nuls by nuls-io.
the class CacheLoader method loadYellowPunishList.
public List<PunishLogPo> loadYellowPunishList(List<PunishLogPo> allPunishList, int roundSize) {
List<PunishLogPo> list = new ArrayList<>();
BlockHeader blockHeader = blockService.getBestBlockHeader().getData();
if (null == blockHeader) {
return list;
}
BlockExtendsData roundData = new BlockExtendsData(blockHeader.getExtend());
long breakRoundIndex = roundData.getRoundIndex() - roundSize;
for (PunishLogPo po : allPunishList) {
if (po.getType() == PunishType.RED.getCode()) {
continue;
}
if (po.getRoundIndex() <= breakRoundIndex) {
continue;
}
list.add(po);
}
Collections.sort(list, new PunishLogComparator());
return list;
}
Aggregations