aboutsummaryrefslogtreecommitdiffstats
path: root/dex/cache_test.go
blob: 3b43e77aaa44c735c6b887740b6f007ba7a60d07 (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright 2018 The dexon-consensus Authors
// This file is part of the dexon-consensus library.
//
// The dexon-consensus 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 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 library. If not, see
// <http://www.gnu.org/licenses/>.

package dex

import (
    "sort"
    "strings"
    "testing"

    coreCommon "github.com/dexon-foundation/dexon-consensus/common"
    coreDb "github.com/dexon-foundation/dexon-consensus/core/db"
    coreTypes "github.com/dexon-foundation/dexon-consensus/core/types"
)

type byHash []*coreTypes.Vote

func (v byHash) Len() int {
    return len(v)
}

func (v byHash) Less(i int, j int) bool {
    return strings.Compare(v[i].BlockHash.String(), v[j].BlockHash.String()) < 0
}

func (v byHash) Swap(i int, j int) {
    v[i], v[j] = v[j], v[i]
}

func TestCacheVote(t *testing.T) {
    db, err := coreDb.NewMemBackedDB()
    if err != nil {
        panic(err)
    }
    cache := newCache(3, db)
    pos0 := coreTypes.Position{
        Height: uint64(0),
    }
    pos1 := coreTypes.Position{
        Height: uint64(1),
    }
    vote1 := &coreTypes.Vote{
        VoteHeader: coreTypes.VoteHeader{
            BlockHash: coreCommon.NewRandomHash(),
            Position:  pos0,
        },
    }
    vote2 := &coreTypes.Vote{
        VoteHeader: coreTypes.VoteHeader{
            BlockHash: coreCommon.NewRandomHash(),
            Position:  pos0,
        },
    }
    vote3 := &coreTypes.Vote{
        VoteHeader: coreTypes.VoteHeader{
            BlockHash: coreCommon.NewRandomHash(),
            Position:  pos1,
        },
    }
    vote4 := &coreTypes.Vote{
        VoteHeader: coreTypes.VoteHeader{
            BlockHash: coreCommon.NewRandomHash(),
            Position:  pos1,
        },
    }
    cache.addVote(vote1)
    cache.addVote(vote2)
    cache.addVote(vote3)

    votes := cache.votes(pos0)
    sort.Sort(byHash(votes))

    resultVotes := []*coreTypes.Vote{vote1, vote2}
    sort.Sort(byHash(resultVotes))

    if len(votes) != 2 {
        t.Errorf("fail to get votes: have %d, want 2", len(votes))
    }
    if !votes[0].BlockHash.Equal(resultVotes[0].BlockHash) {
        t.Errorf("get wrong vote: have %s, want %s", votes[0], resultVotes[0])
    }
    if !votes[1].BlockHash.Equal(resultVotes[1].BlockHash) {
        t.Errorf("get wrong vote: have %s, want %s", votes[1], resultVotes[1])
    }
    votes = cache.votes(pos1)
    sort.Sort(byHash(votes))
    if len(votes) != 1 {
        t.Errorf("fail to get votes: have %d, want 1", len(votes))
    }
    if !votes[0].BlockHash.Equal(vote3.BlockHash) {
        t.Errorf("get wrong vote: have %s, want %s", votes[0], vote3)
    }

    cache.addVote(vote4)

    votes = cache.votes(pos0)
    sort.Sort(byHash(votes))

    if len(votes) != 0 {
        t.Errorf("fail to get votes: have %d, want 0", len(votes))
    }
    votes = cache.votes(pos1)
    sort.Sort(byHash(votes))

    resultVotes = []*coreTypes.Vote{vote3, vote4}
    sort.Sort(byHash(resultVotes))

    if len(votes) != 2 {
        t.Errorf("fail to get votes: have %d, want 1", len(votes))
    }
    if !votes[0].BlockHash.Equal(resultVotes[0].BlockHash) {
        t.Errorf("get wrong vote: have %s, want %s", votes[0], resultVotes[0])
    }
    if !votes[1].BlockHash.Equal(resultVotes[1].BlockHash) {
        t.Errorf("get wrong vote: have %s, want %s", votes[1], resultVotes[1])
    }
}

func TestCacheBlock(t *testing.T) {
    db, err := coreDb.NewMemBackedDB()
    if err != nil {
        panic(err)
    }
    cache := newCache(3, db)
    block1 := &coreTypes.Block{
        Hash: coreCommon.NewRandomHash(),
    }
    block2 := &coreTypes.Block{
        Hash: coreCommon.NewRandomHash(),
    }
    block3 := &coreTypes.Block{
        Hash: coreCommon.NewRandomHash(),
    }
    block4 := &coreTypes.Block{
        Hash: coreCommon.NewRandomHash(),
    }
    cache.addBlock(block1)
    cache.addBlock(block2)
    cache.addBlock(block3)

    hashes := coreCommon.Hashes{block1.Hash, block2.Hash, block3.Hash, block4.Hash}
    hashMap := map[coreCommon.Hash]struct{}{
        block1.Hash: {},
        block2.Hash: {},
        block3.Hash: {},
    }
    blocks := cache.blocks(hashes)
    if len(blocks) != 3 {
        t.Errorf("fail to get blocks: have %d, want 3", len(blocks))
    }
    for _, block := range blocks {
        if _, exist := hashMap[block.Hash]; !exist {
            t.Errorf("get wrong block: have %s, want %v", block, hashMap)
        }
    }

    cache.addBlock(block4)

    blocks = cache.blocks(hashes)
    hashMap[block4.Hash] = struct{}{}
    if len(blocks) != 3 {
        t.Errorf("fail to get blocks: have %d, want 3", len(blocks))
    }
    hasNewBlock := false
    for _, block := range blocks {
        if _, exist := hashMap[block.Hash]; !exist {
            t.Errorf("get wrong block: have %s, want %v", block, hashMap)
        }
        if block.Hash.Equal(block4.Hash) {
            hasNewBlock = true
        }
    }
    if !hasNewBlock {
        t.Errorf("expect block %s in cache, have %v", block4, blocks)
    }

    block5 := &coreTypes.Block{
        Hash: coreCommon.NewRandomHash(),
    }
    if err := db.PutBlock(*block5); err != nil {
        panic(err)
    }
    blocks = cache.blocks(coreCommon.Hashes{block5.Hash})
    if len(blocks) != 1 {
        t.Errorf("fail to get blocks: have %d, want 1", len(blocks))
    } else {
        if !blocks[0].Hash.Equal(block5.Hash) {
            t.Errorf("get wrong block: have %s, want %s", blocks[0], block5)
        }
    }
}