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
|
// 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 core
import (
"testing"
"github.com/dexon-foundation/dexon-consensus/core/types"
"github.com/stretchr/testify/suite"
)
type BlockPoolTestSuite struct {
suite.Suite
}
func (s *BlockPoolTestSuite) TestBasicUsage() {
// This test case try this flow:
// - add some blocks into pool.
// - get tips, check if expected.
// - get tips, should be identical to previous call.
// - remove tips, and get tips again, check if expected.
// - purge one chain, check if expected.
var (
req = s.Require()
pool = newBlockPool(3)
)
addBlockWithPosition := func(chainID uint32, height uint64) {
pool.addBlock(&types.Block{
Position: types.Position{
ChainID: chainID,
Height: height,
}})
}
chkPos := func(b *types.Block, chainID uint32, height uint64) {
req.Equal(b.Position.ChainID, chainID)
req.Equal(b.Position.Height, height)
}
addBlockWithPosition(0, 0)
addBlockWithPosition(0, 1)
addBlockWithPosition(0, 2)
addBlockWithPosition(0, 3)
addBlockWithPosition(2, 0)
addBlockWithPosition(2, 1)
addBlockWithPosition(2, 2)
// Check each tip.
chkPos(pool.tip(0), 0, 0)
chkPos(pool.tip(2), 2, 0)
req.Nil(pool.tip(1))
// Remove tips of chain#0, #1.
pool.removeTip(0)
pool.removeTip(1)
// Get tips of chain#0, #2 back to check.
chkPos(pool.tip(0), 0, 1)
chkPos(pool.tip(2), 2, 0) // Chain#2 is untouched.
// Purge with height lower than lowest height.
pool.purgeBlocks(0, 0)
chkPos(pool.tip(0), 0, 1) // Chain#0 is not affected.
// Purge with height in range.
pool.purgeBlocks(0, 2)
chkPos(pool.tip(0), 0, 3) // Height = 1, 2 are purged.
// Purge with height higher than highest height.
pool.purgeBlocks(0, 4)
req.Nil(pool.tip(0)) // Whole chain is purged.
}
func TestBlockPool(t *testing.T) {
suite.Run(t, new(BlockPoolTestSuite))
}
|