aboutsummaryrefslogtreecommitdiffstats
path: root/core/test/blocks-generator_test.go
blob: b9c0b3528881de920269b1090767210334fbf5b5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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))
}