-
Notifications
You must be signed in to change notification settings - Fork 197
/
common_test.go
187 lines (153 loc) · 5.55 KB
/
common_test.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
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
package headerCheck
import (
"fmt"
"testing"
"github.com/multiversx/mx-chain-core-go/data/block"
"github.com/multiversx/mx-chain-go/process"
"github.com/multiversx/mx-chain-go/sharding/nodesCoordinator"
"github.com/multiversx/mx-chain-go/testscommon/shardingMocks"
"github.com/stretchr/testify/assert"
)
func TestComputeConsensusGroup(t *testing.T) {
t.Parallel()
t.Run("nil header should error", func(t *testing.T) {
nodesCoordinatorInstance := shardingMocks.NewNodesCoordinatorMock()
nodesCoordinatorInstance.ComputeValidatorsGroupCalled = func(randomness []byte, round uint64, shardId uint32, epoch uint32) (validatorsGroup []nodesCoordinator.Validator, err error) {
assert.Fail(t, "should have not called ComputeValidatorsGroupCalled")
return nil, nil
}
vGroup, err := ComputeConsensusGroup(nil, nodesCoordinatorInstance)
assert.Equal(t, process.ErrNilHeaderHandler, err)
assert.Nil(t, vGroup)
})
t.Run("nil nodes coordinator should error", func(t *testing.T) {
header := &block.Header{
Epoch: 1123,
Round: 37373,
Nonce: 38383,
ShardID: 2,
PrevRandSeed: []byte("prev rand seed"),
}
vGroup, err := ComputeConsensusGroup(header, nil)
assert.Equal(t, process.ErrNilNodesCoordinator, err)
assert.Nil(t, vGroup)
})
t.Run("should work for a random block", func(t *testing.T) {
header := &block.Header{
Epoch: 1123,
Round: 37373,
Nonce: 38383,
ShardID: 2,
PrevRandSeed: []byte("prev rand seed"),
}
validator1, _ := nodesCoordinator.NewValidator([]byte("pk1"), 1, 1)
validator2, _ := nodesCoordinator.NewValidator([]byte("pk2"), 1, 2)
validatorGroup := []nodesCoordinator.Validator{validator1, validator2}
nodesCoordinatorInstance := shardingMocks.NewNodesCoordinatorMock()
nodesCoordinatorInstance.ComputeValidatorsGroupCalled = func(randomness []byte, round uint64, shardId uint32, epoch uint32) (validatorsGroup []nodesCoordinator.Validator, err error) {
assert.Equal(t, header.PrevRandSeed, randomness)
assert.Equal(t, header.Round, round)
assert.Equal(t, header.ShardID, shardId)
assert.Equal(t, header.Epoch, epoch)
return validatorGroup, nil
}
vGroup, err := ComputeConsensusGroup(header, nodesCoordinatorInstance)
assert.Nil(t, err)
assert.Equal(t, validatorGroup, vGroup)
})
t.Run("should work for a start of epoch block", func(t *testing.T) {
header := &block.Header{
Epoch: 1123,
Round: 37373,
Nonce: 38383,
ShardID: 2,
PrevRandSeed: []byte("prev rand seed"),
EpochStartMetaHash: []byte("epoch start metahash"),
}
validator1, _ := nodesCoordinator.NewValidator([]byte("pk1"), 1, 1)
validator2, _ := nodesCoordinator.NewValidator([]byte("pk2"), 1, 2)
validatorGroup := []nodesCoordinator.Validator{validator1, validator2}
nodesCoordinatorInstance := shardingMocks.NewNodesCoordinatorMock()
nodesCoordinatorInstance.ComputeValidatorsGroupCalled = func(randomness []byte, round uint64, shardId uint32, epoch uint32) (validatorsGroup []nodesCoordinator.Validator, err error) {
assert.Equal(t, header.PrevRandSeed, randomness)
assert.Equal(t, header.Round, round)
assert.Equal(t, header.ShardID, shardId)
assert.Equal(t, header.Epoch-1, epoch)
return validatorGroup, nil
}
vGroup, err := ComputeConsensusGroup(header, nodesCoordinatorInstance)
assert.Nil(t, err)
assert.Equal(t, validatorGroup, vGroup)
})
}
func generatePubKeys(num int) []string {
consensusGroup := make([]string, 0, num)
for i := 0; i < num; i++ {
consensusGroup = append(consensusGroup, fmt.Sprintf("pub key %d", i))
}
return consensusGroup
}
func TestComputeSignersPublicKeys(t *testing.T) {
t.Parallel()
t.Run("should compute with 16 validators", func(t *testing.T) {
t.Parallel()
consensusGroup := generatePubKeys(16)
mask0 := byte(0b00110101)
mask1 := byte(0b01001101)
result := ComputeSignersPublicKeys(consensusGroup, []byte{mask0, mask1})
expected := []string{
"pub key 0",
"pub key 2",
"pub key 4",
"pub key 5",
"pub key 8",
"pub key 10",
"pub key 11",
"pub key 14",
}
assert.Equal(t, expected, result)
})
t.Run("should compute with 14 validators", func(t *testing.T) {
t.Parallel()
consensusGroup := generatePubKeys(14)
mask0 := byte(0b00110101)
mask1 := byte(0b00001101)
result := ComputeSignersPublicKeys(consensusGroup, []byte{mask0, mask1})
expected := []string{
"pub key 0",
"pub key 2",
"pub key 4",
"pub key 5",
"pub key 8",
"pub key 10",
"pub key 11",
}
assert.Equal(t, expected, result)
})
t.Run("should compute with 14 validators, mask is 0", func(t *testing.T) {
t.Parallel()
consensusGroup := generatePubKeys(14)
mask0 := byte(0b00000000)
mask1 := byte(0b00000000)
result := ComputeSignersPublicKeys(consensusGroup, []byte{mask0, mask1})
expected := make([]string, 0)
assert.Equal(t, expected, result)
})
t.Run("should compute with 14 validators, mask contains all bits set", func(t *testing.T) {
t.Parallel()
consensusGroup := generatePubKeys(14)
mask0 := byte(0b11111111)
mask1 := byte(0b00111111)
result := ComputeSignersPublicKeys(consensusGroup, []byte{mask0, mask1})
assert.Equal(t, consensusGroup, result)
})
t.Run("should compute with 17 validators, mask contains 2 bytes", func(t *testing.T) {
t.Parallel()
consensusGroup := generatePubKeys(17)
mask0 := byte(0b11111111)
mask1 := byte(0b11111111)
result := ComputeSignersPublicKeys(consensusGroup, []byte{mask0, mask1})
expected := generatePubKeys(16)
assert.Equal(t, expected, result)
})
}