Search in sources :

Example 6 with SmallBlock

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());
}
Also used : SmallBlock(io.nuls.protocol.model.SmallBlock)

Example 7 with SmallBlock

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());
}
Also used : SmallBlock(io.nuls.protocol.model.SmallBlock) Block(io.nuls.kernel.model.Block) SmallBlock(io.nuls.protocol.model.SmallBlock) Test(org.junit.Test) BaseTest(io.nuls.consensus.poc.BaseTest)

Example 8 with SmallBlock

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();
}
Also used : SmallBlock(io.nuls.protocol.model.SmallBlock) NulsDigestData(io.nuls.kernel.model.NulsDigestData) BlockHeader(io.nuls.kernel.model.BlockHeader) Test(org.junit.Test)

Example 9 with SmallBlock

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;
    }
}
Also used : SmallBlock(io.nuls.protocol.model.SmallBlock) ValidateResult(io.nuls.kernel.validate.ValidateResult) IOException(java.io.IOException) LedgerService(io.nuls.ledger.service.LedgerService) HeaderSignValidator(io.nuls.protocol.model.validator.HeaderSignValidator) CoinBaseTransaction(io.nuls.protocol.model.tx.CoinBaseTransaction) RedPunishData(io.nuls.consensus.poc.protocol.entity.RedPunishData) NulsException(io.nuls.kernel.exception.NulsException) NulsByteBuffer(io.nuls.kernel.utils.NulsByteBuffer)

Example 10 with SmallBlock

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);
}
Also used : SmallBlock(io.nuls.protocol.model.SmallBlock)

Aggregations

SmallBlock (io.nuls.protocol.model.SmallBlock)12 ValidateResult (io.nuls.kernel.validate.ValidateResult)4 IOException (java.io.IOException)3 RedPunishData (io.nuls.consensus.poc.protocol.entity.RedPunishData)2 ContractResult (io.nuls.contract.dto.ContractResult)2 NulsDigestData (io.nuls.kernel.model.NulsDigestData)2 CoinBaseTransaction (io.nuls.protocol.model.tx.CoinBaseTransaction)2 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 BaseTest (io.nuls.consensus.poc.BaseTest)1 MeetingMember (io.nuls.consensus.poc.model.MeetingMember)1 MeetingRound (io.nuls.consensus.poc.model.MeetingRound)1 Agent (io.nuls.consensus.poc.protocol.entity.Agent)1 CreateAgentTransaction (io.nuls.consensus.poc.protocol.tx.CreateAgentTransaction)1 DepositTransaction (io.nuls.consensus.poc.protocol.tx.DepositTransaction)1 RedPunishTransaction (io.nuls.consensus.poc.protocol.tx.RedPunishTransaction)1 YellowPunishTransaction (io.nuls.consensus.poc.protocol.tx.YellowPunishTransaction)1 ContractTransaction (io.nuls.contract.entity.tx.ContractTransaction)1 NulsException (io.nuls.kernel.exception.NulsException)1 Block (io.nuls.kernel.model.Block)1