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;
}
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();
}
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();
}
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));
}
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);
}
Aggregations