aboutsummaryrefslogtreecommitdiffstats
path: root/core/test/blocks-generator_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'core/test/blocks-generator_test.go')
-rw-r--r--core/test/blocks-generator_test.go108
1 files changed, 108 insertions, 0 deletions
diff --git a/core/test/blocks-generator_test.go b/core/test/blocks-generator_test.go
new file mode 100644
index 0000000..b9c0b35
--- /dev/null
+++ b/core/test/blocks-generator_test.go
@@ -0,0 +1,108 @@
+// Copyright 2018 The dexon-consensus-core Authors
+// This file is part of the dexon-consensus-core library.
+//
+// The dexon-consensus-core library is free software: you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public License as
+// published by the Free Software Foundation, either version 3 of the License,
+// or (at your option) any later version.
+//
+// The dexon-consensus-core library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the dexon-consensus-core library. If not, see
+// <http://www.gnu.org/licenses/>.
+
+package test
+
+import (
+ "sort"
+ "testing"
+
+ "github.com/dexon-foundation/dexon-consensus-core/blockdb"
+ "github.com/dexon-foundation/dexon-consensus-core/common"
+ "github.com/dexon-foundation/dexon-consensus-core/core/types"
+ "github.com/stretchr/testify/suite"
+)
+
+type BlocksGeneratorTestCase struct {
+ suite.Suite
+}
+
+func (s *BlocksGeneratorTestCase) TestGenerate() {
+ // This test case is to make sure the generated blocks are legimate.
+ validatorCount := 19
+ blockCount := 50
+ gen := NewBlocksGenerator(nil)
+ db, err := blockdb.NewMemBackedBlockDB()
+ s.Require().Nil(err)
+
+ err = gen.Generate(
+ validatorCount, blockCount, nil, db)
+ s.Require().Nil(err)
+
+ // Load all blocks in that database for further checking.
+ iter, err := db.GetAll()
+ s.Require().Nil(err)
+ blocksByValidator := make(map[types.ValidatorID][]*types.Block)
+ blocksByHash := make(map[common.Hash]*types.Block)
+ for {
+ block, err := iter.Next()
+ if err == blockdb.ErrIterationFinished {
+ break
+ }
+ s.Nil(err)
+
+ blocksByValidator[block.ProposerID] =
+ append(blocksByValidator[block.ProposerID], &block)
+ sort.Sort(types.ByHeight(blocksByValidator[block.ProposerID]))
+ blocksByHash[block.Hash] = &block
+ }
+
+ // Make sure these two rules are hold for these blocks:
+ // - No backward acking: the later block should only ack new blocks
+ // compared to its parent block.
+ // - Parent Ack: always ack its parent block.
+ // - No Acks in genesis bloc
+ for _, blocks := range blocksByValidator {
+ lastAckingHeights := map[types.ValidatorID]uint64{}
+ s.Require().NotEmpty(blocks)
+
+ // Check genesis block.
+ genesisBlock := blocks[0]
+ s.Equal(genesisBlock.Hash, genesisBlock.ParentHash)
+ s.Equal(genesisBlock.Height, uint64(0))
+ s.Empty(genesisBlock.Acks)
+
+ // Check normal blocks.
+ for index, block := range blocks[1:] {
+ parentAcked := false
+ for ack := range block.Acks {
+ if ack == block.ParentHash {
+ parentAcked = true
+ }
+
+ ackedBlock := blocksByHash[ack]
+ s.Require().NotNil(ackedBlock)
+ prevAckingHeight, exists :=
+ lastAckingHeights[ackedBlock.ProposerID]
+ if exists {
+ s.True(prevAckingHeight < ackedBlock.Height)
+ }
+ lastAckingHeights[ackedBlock.ProposerID] = ackedBlock.Height
+ // Block Height should always incremental by 1.
+ //
+ // Because we iterate blocks slice from 1,
+ // we need to add 1 to the index.
+ s.Equal(block.Height, uint64(index+1))
+ }
+ s.True(parentAcked)
+ }
+ }
+}
+
+func TestBlocksGenerator(t *testing.T) {
+ suite.Run(t, new(BlocksGeneratorTestCase))
+}