forked from thetatoken/theta-protocol-ledger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testutil.go
51 lines (45 loc) · 1.23 KB
/
testutil.go
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
package consensus
import (
"math/big"
"github.com/stretchr/testify/assert"
"github.com/thetatoken/theta/core"
)
// GetFinalizedBlocks drains the FinalizedBlocks channel and return a slice of block hashes.
func GetFinalizedBlocks(ch chan *core.Block) []string {
res := []string{}
loop:
for {
select {
case block := <-ch:
res = append(res, block.Hash().String())
default:
break loop
}
}
return res
}
// AssertFinalizedBlocks asserts finalized blocks are as expected.
func AssertFinalizedBlocks(assert *assert.Assertions, expected []string, ch chan *core.Block) {
assert.Equal(expected, GetFinalizedBlocks(ch))
}
// AssertFinalizedBlocksNotConflicting asserts two chains are not conflicting.
func AssertFinalizedBlocksNotConflicting(assert *assert.Assertions, c1 []string, c2 []string, msg string) {
length := len(c2)
if len(c1) < len(c2) {
length = len(c1)
}
for i := 0; i < length; i++ {
if c1[i] != c2[i] {
assert.Failf(msg, "Conflicts found: %v, %v", c1, c2)
}
}
}
func NewTestValidatorSet(addressStrs []string) *core.ValidatorSet {
s := core.NewValidatorSet()
for _, addressStr := range addressStrs {
stake := new(big.Int).SetUint64(1)
v := core.NewValidator(addressStr, stake)
s.AddValidator(v)
}
return s
}