use of io.nuls.protocol.model.SmallBlock in project nuls by nuls-io.
the class TemporaryCacheManagerTest method getSmallBlock.
/**
* 测试获取已存在缓存中的小区块
* <p>
* The test gets the small block in the cache.
*
* @param hash 已存入的小区快的摘要对象,A quick summary of the community that has been deposited.
* @param smallBlock 已存入缓存的小区快,The cached community is fast.
*/
private void getSmallBlock(NulsDigestData hash, SmallBlock smallBlock) {
SmallBlock sb = manager.getSmallBlockByHash(NulsDigestData.calcDigestData("abcdefg".getBytes()));
assertEquals(sb.getHeader().getHash(), smallBlock.getHeader().getHash());
}
use of io.nuls.protocol.model.SmallBlock in project nuls by nuls-io.
the class ConsensusToolTest method testGetSmallBlock.
@Test
public void testGetSmallBlock() {
Block block = createBlock();
SmallBlock smallBlock = ConsensusTool.getSmallBlock(block);
assertNotNull(smallBlock);
assertEquals(smallBlock.getHeader(), block.getHeader());
assertEquals(smallBlock.getSubTxList().size(), 0);
assertEquals(smallBlock.getTxHashList().get(0), block.getTxs().get(0).getHash());
}
use of io.nuls.protocol.model.SmallBlock in project nuls by nuls-io.
the class TemporaryCacheManagerTest method cacheSmallBlock.
/**
* 测试小区快的缓存处理流程:
* 1. 先测试放入缓存,不出现异常视为成功
* 2. 测试获取刚放入的小区快,获取的结果不能为空且和刚仿佛的是相同内容
* 3. 测试删除之前放入的小区快,删除后再获取应该得到null
* 4. 重新把小区快放回缓存中,调用清理方法,清理后缓存中应该没有任何小区块或者交易
* test the cache processing process of the smallblock:
* 1. The first test is put into the cache, and no exceptions are deemed to be successful.
* 2. Test to get the newly inserted smallblock fast, the results can not be empty and the same content as if it is just as if.
* 3. Before the test is deleted, the cells should be put into the smallblock quickly. After deleting, it should be null.
* 4. Reattach the smallblock to the cache, call the cleaning method, and there should be no blocks or transactions in the cache.
*/
@Test
public void cacheSmallBlock() {
SmallBlock smallBlock = new SmallBlock();
BlockHeader header = new BlockHeader();
NulsDigestData hash = NulsDigestData.calcDigestData("abcdefg".getBytes());
header.setHash(hash);
manager.cacheSmallBlock(smallBlock);
assertTrue(true);
this.getSmallBlock(hash, smallBlock);
this.removeSmallBlock(hash);
manager.cacheSmallBlock(smallBlock);
this.clear();
}
use of io.nuls.protocol.model.SmallBlock in project nuls by nuls-io.
the class ChainContainer method verifyRedPunish.
private boolean verifyRedPunish(RedPunishTransaction data) {
RedPunishData punishData = data.getTxData();
if (ConsensusConfig.getSeedNodeStringList().contains(AddressTool.getStringAddressByBytes(punishData.getAddress()))) {
Log.warn("The seed node can not be punished!");
return false;
}
HeaderSignValidator validator = NulsContext.getServiceBean(HeaderSignValidator.class);
LedgerService ledgerService = NulsContext.getServiceBean(LedgerService.class);
if (punishData.getReasonCode() == PunishReasonEnum.DOUBLE_SPEND.getCode()) {
if (NulsContext.MAIN_NET_VERSION <= 1) {
Log.warn("The red punish tx need higher version!");
return false;
}
SmallBlock smallBlock = new SmallBlock();
try {
smallBlock.parse(punishData.getEvidence(), 0);
} catch (NulsException e) {
Log.error(e);
return false;
}
BlockHeader header = smallBlock.getHeader();
if (header.getTime() != data.getTime()) {
return false;
}
ValidateResult result = validator.validate(header);
if (result.isFailed()) {
Log.warn(result.getMsg());
return false;
}
List<NulsDigestData> txHashList = smallBlock.getTxHashList();
if (!header.getMerkleHash().equals(NulsDigestData.calcMerkleDigestData(txHashList))) {
Log.warn(TransactionErrorCode.TX_DATA_VALIDATION_ERROR.getMsg());
return false;
}
List<Transaction> txList = smallBlock.getSubTxList();
if (null == txList || txList.size() < 2) {
Log.warn(TransactionErrorCode.TX_DATA_VALIDATION_ERROR.getMsg());
return false;
}
result = ledgerService.verifyDoubleSpend(txList);
if (result.isSuccess()) {
Log.warn(PocConsensusErrorCode.TRANSACTIONS_NEVER_DOUBLE_SPEND.getMsg());
return false;
}
} else if (punishData.getReasonCode() == PunishReasonEnum.BIFURCATION.getCode()) {
if (NulsContext.MAIN_NET_VERSION <= 1) {
Log.warn("The red punish tx need higher version!");
return false;
}
NulsByteBuffer byteBuffer = new NulsByteBuffer(punishData.getEvidence());
// 轮次
long[] roundIndex = new long[NulsContext.REDPUNISH_BIFURCATION];
for (int i = 0; i < NulsContext.REDPUNISH_BIFURCATION && !byteBuffer.isFinished(); i++) {
BlockHeader header1 = null;
BlockHeader header2 = null;
try {
header1 = byteBuffer.readNulsData(new BlockHeader());
header2 = byteBuffer.readNulsData(new BlockHeader());
} catch (NulsException e) {
Log.error(e);
}
if (null == header1 || null == header2) {
Log.warn(KernelErrorCode.DATA_NOT_FOUND.getMsg());
return false;
}
if (header1.getHeight() != header2.getHeight()) {
Log.warn(TransactionErrorCode.TX_DATA_VALIDATION_ERROR.getMsg());
return false;
}
if (i == NulsContext.REDPUNISH_BIFURCATION - 1 && (header1.getTime() + header2.getTime()) / 2 != data.getTime()) {
return false;
}
ValidateResult result = validator.validate(header1);
if (result.isFailed()) {
Log.warn(TransactionErrorCode.TX_DATA_VALIDATION_ERROR.getMsg());
return false;
}
result = validator.validate(header2);
if (result.isFailed()) {
Log.warn(TransactionErrorCode.TX_DATA_VALIDATION_ERROR.getMsg());
return false;
}
if (!Arrays.equals(header1.getBlockSignature().getPublicKey(), header2.getBlockSignature().getPublicKey())) {
Log.warn(TransactionErrorCode.SIGNATURE_ERROR.getMsg());
return false;
}
BlockExtendsData blockExtendsData = new BlockExtendsData(header1.getExtend());
roundIndex[i] = blockExtendsData.getRoundIndex();
}
// 验证轮次是否连续
boolean rs = true;
for (int i = 0; i < roundIndex.length; i++) {
if (i < roundIndex.length - 2 && roundIndex[i + 1] - roundIndex[i] != 1) {
rs = false;
break;
}
}
if (!rs) {
Log.warn(PocConsensusErrorCode.RED_CARD_VERIFICATION_FAILED.getMsg());
return false;
}
} else if (punishData.getReasonCode() != PunishReasonEnum.TOO_MUCH_YELLOW_PUNISH.getCode()) {
Log.warn(PocConsensusErrorCode.RED_CARD_VERIFICATION_FAILED.getMsg());
return false;
}
try {
return verifyCoinData(data);
} catch (IOException e) {
Log.error(e);
return false;
}
}
use of io.nuls.protocol.model.SmallBlock in project nuls by nuls-io.
the class ConsensusProcess method broadcastSmallBlock.
private void broadcastSmallBlock(Block block) {
SmallBlock smallBlock = ConsensusTool.getSmallBlock(block);
temporaryCacheManager.cacheSmallBlock(smallBlock);
SmallBlockDuplicateRemoval.needDownloadSmallBlock(smallBlock.getHeader().getHash());
blockService.broadcastBlock(smallBlock);
}
Aggregations