aboutsummaryrefslogtreecommitdiffstats
path: root/core/consensus-timestamp_test.go
blob: 615142cd5472a1a9bfa4ffc275fb3587d60d6dc6 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// 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 core

import (
    "math"
    "math/rand"
    "testing"
    "time"

    "github.com/stretchr/testify/suite"

    "github.com/dexon-foundation/dexon-consensus-core/common"
    "github.com/dexon-foundation/dexon-consensus-core/core/types"
)

type ConsensusTimestampTest struct {
    suite.Suite
}

func (s *ConsensusTimestampTest) generateBlocksWithTimestamp(
    blockNum, chainNum int,
    step, sigma time.Duration) []*types.Block {
    blocks := make([]*types.Block, blockNum)
    chainIDs := make([]uint32, len(blocks))
    for i := range chainIDs {
        chainIDs[i] = uint32(i % chainNum)
    }
    rand.Shuffle(len(chainIDs), func(i, j int) {
        chainIDs[i], chainIDs[j] = chainIDs[j], chainIDs[i]
    })
    chainTimestamps := make(map[uint32]time.Time)
    for idx := range blocks {
        blocks[idx] = &types.Block{}
        block := blocks[idx]
        if idx < chainNum {
            // Genesis blocks.
            block.Position.ChainID = uint32(idx)
            block.ParentHash = common.Hash{}
            block.Position.Height = 0
            s.Require().True(block.IsGenesis())
            chainTimestamps[uint32(idx)] = time.Now().UTC()
        } else {
            block.Position.ChainID = chainIDs[idx]
            // Assign 1 to height to make this block non-genesis.
            block.Position.Height = 1
            s.Require().False(block.IsGenesis())
        }
        block.Timestamp = chainTimestamps[block.Position.ChainID]
        // Update timestamp for next block.
        diffSeconds := rand.NormFloat64() * sigma.Seconds()
        diffSeconds = math.Min(diffSeconds, step.Seconds()/2.1)
        diffSeconds = math.Max(diffSeconds, -step.Seconds()/2.1)
        diffDuration := time.Duration(diffSeconds*1000) * time.Millisecond
        chainTimestamps[block.Position.ChainID] =
            chainTimestamps[block.Position.ChainID].Add(step).Add(diffDuration)
        s.Require().True(block.Timestamp.Before(
            chainTimestamps[block.Position.ChainID]))
    }
    return blocks
}

func (s *ConsensusTimestampTest) extractTimestamps(
    blocks []*types.Block) []time.Time {
    timestamps := make([]time.Time, 0, len(blocks))
    for _, block := range blocks {
        if block.IsGenesis() {
            continue
        }
        timestamps = append(timestamps, block.ConsensusTimestamp)
    }
    return timestamps
}

// TestTimestampPartition verifies that processing segments of compatction chain
// should have the same result as processing the whole chain at once.
func (s *ConsensusTimestampTest) TestTimestampPartition() {
    var round uint64
    blockNums := []int{50, 100, 30}
    chainNum := 19
    sigma := 100 * time.Millisecond
    totalTimestamps := make([]time.Time, 0)
    ct := newConsensusTimestamp(round, uint32(chainNum))
    totalBlockNum := 0
    for _, blockNum := range blockNums {
        totalBlockNum += blockNum
    }
    totalChain := s.generateBlocksWithTimestamp(
        totalBlockNum, chainNum, time.Second, sigma)
    for _, blockNum := range blockNums {
        var chain []*types.Block
        chain, totalChain = totalChain[:blockNum], totalChain[blockNum:]
        err := ct.processBlocks(chain)
        s.Require().NoError(err)
        timestamps := s.extractTimestamps(chain)
        totalChain = append(totalChain, chain...)
        totalTimestamps = append(totalTimestamps, timestamps...)
    }
    ct2 := newConsensusTimestamp(round, uint32(chainNum))
    err := ct2.processBlocks(totalChain)
    s.Require().NoError(err)
    timestamps2 := s.extractTimestamps(totalChain)
    s.Equal(totalTimestamps, timestamps2)
}

func (s *ConsensusTimestampTest) TestTimestampIncrease() {
    var round uint64
    chainNum := 19
    sigma := 100 * time.Millisecond
    ct := newConsensusTimestamp(round, uint32(chainNum))
    chain := s.generateBlocksWithTimestamp(1000, chainNum, time.Second, sigma)
    err := ct.processBlocks(chain)
    s.Require().NoError(err)
    timestamps := s.extractTimestamps(chain)
    for i := 1; i < len(timestamps); i++ {
        s.False(timestamps[i].Before(timestamps[i-1]))
    }
    // Test if the processBlocks is stable.
    ct2 := newConsensusTimestamp(round, uint32(chainNum))
    ct2.processBlocks(chain)
    s.Require().NoError(err)
    timestamps2 := s.extractTimestamps(chain)
    s.Equal(timestamps, timestamps2)
}

func TestConsensusTimestamp(t *testing.T) {
    suite.Run(t, new(ConsensusTimestampTest))
}