diff options
Diffstat (limited to 'core/test')
-rw-r--r-- | core/test/blocks-generator.go | 18 | ||||
-rw-r--r-- | core/test/blocks-generator_test.go | 21 |
2 files changed, 25 insertions, 14 deletions
diff --git a/core/test/blocks-generator.go b/core/test/blocks-generator.go index 393e3a3..62c777e 100644 --- a/core/test/blocks-generator.go +++ b/core/test/blocks-generator.go @@ -190,26 +190,34 @@ func (ns *nodeSetStatus) proposeBlock( blockHeight = status.tip.Position.Height + 1 acks = append(acks, parentHash) } + // 10% of chance to produce empty block. + empty := ns.randGen.Float32() < 0.1 && blockHeight > 0 chainID := ns.proposerChain[proposerID] newBlock := &types.Block{ - ProposerID: proposerID, ParentHash: parentHash, Position: types.Position{ Round: ns.round, Height: blockHeight, ChainID: chainID, }, - Acks: common.NewSortedHashes(acks), Timestamp: status.getNextBlockTime(ns.timePicker), } + if empty { + newBlock.Acks = common.NewSortedHashes(common.Hashes{parentHash}) + } else { + newBlock.ProposerID = proposerID + newBlock.Acks = common.NewSortedHashes(acks) + } var err error newBlock.Hash, err = ns.hashBlock(newBlock) if err != nil { return nil, err } - newBlock.Signature, err = status.prvKey.Sign(newBlock.Hash) - if err != nil { - return nil, err + if !empty { + newBlock.Signature, err = status.prvKey.Sign(newBlock.Hash) + if err != nil { + return nil, err + } } status.blocks = append(status.blocks, newBlock) status.tip = newBlock diff --git a/core/test/blocks-generator_test.go b/core/test/blocks-generator_test.go index fafbd6c..075cc9f 100644 --- a/core/test/blocks-generator_test.go +++ b/core/test/blocks-generator_test.go @@ -51,7 +51,7 @@ func (s *BlocksGeneratorTestSuite) TestGenerate() { // Load all blocks in that database for further checking. iter, err := db.GetAll() req.NoError(err) - blocksByNode := make(map[types.NodeID][]*types.Block) + blocksByChain := make(map[uint32][]*types.Block) blocksByHash := make(map[common.Hash]*types.Block) for { block, err := iter.Next() @@ -62,11 +62,13 @@ func (s *BlocksGeneratorTestSuite) TestGenerate() { // TODO(mission): Make sure each block is correctly signed once // we have a way to access core.hashBlock. req.NotEqual(block.Hash, common.Hash{}) - req.NotEmpty(block.Signature) + if !block.IsEmpty() { + req.NotEmpty(block.Signature) + } req.Equal(block.Position.Round, uint64(1)) - blocksByNode[block.ProposerID] = - append(blocksByNode[block.ProposerID], &block) - sort.Sort(types.ByPosition(blocksByNode[block.ProposerID])) + blocksByChain[block.Position.ChainID] = + append(blocksByChain[block.Position.ChainID], &block) + sort.Sort(types.ByPosition(blocksByChain[block.Position.ChainID])) blocksByHash[block.Hash] = &block } // Make sure these two rules are hold for these blocks: @@ -77,8 +79,8 @@ func (s *BlocksGeneratorTestSuite) TestGenerate() { // previous block. // - The last block of each chain should pass endTime. // - No Acks in genesis bloc - for _, blocks := range blocksByNode { - lastAckingHeights := map[types.NodeID]uint64{} + for _, blocks := range blocksByChain { + lastAckingHeights := map[uint32]uint64{} req.NotEmpty(blocks) // Check genesis block. genesisBlock := blocks[0] @@ -95,11 +97,12 @@ func (s *BlocksGeneratorTestSuite) TestGenerate() { ackedBlock := blocksByHash[ack] req.NotNil(ackedBlock) prevAckingHeight, exists := - lastAckingHeights[ackedBlock.ProposerID] + lastAckingHeights[ackedBlock.Position.ChainID] if exists { s.True(prevAckingHeight < ackedBlock.Position.Height) } - lastAckingHeights[ackedBlock.ProposerID] = ackedBlock.Position.Height + lastAckingHeights[ackedBlock.Position.ChainID] = + ackedBlock.Position.Height // Block Height should always incremental by 1. // // Because we iterate blocks slice from 1, |