-
Notifications
You must be signed in to change notification settings - Fork 33
/
module_raintree_test.go
306 lines (270 loc) · 19.2 KB
/
module_raintree_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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
//go:build test
package p2p
import (
"log"
"os"
"path/filepath"
"regexp"
"sort"
"strconv"
"sync"
"testing"
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/anypb"
"github.com/pokt-network/pocket/internal/testutil"
)
// TODO(#314): Add the tooling and instructions on how to generate unit tests in this file.
func TestMain(m *testing.M) {
exitCode := m.Run()
files, err := filepath.Glob("*.json")
if err != nil {
log.Fatalf("Error finding json file: %v", err)
}
for _, f := range files {
os.Remove(f)
}
os.Exit(exitCode)
}
// ### RainTree Unit Tests ###
func TestRainTreeNetworkCompleteOneNodes(t *testing.T) {
// val_1
originatorNode := validatorId(1)
expectedCalls := TestNetworkSimulationConfig{
originatorNode: {0, 0}, // val_1, the originator, does 0 network reads or writes
}
testRainTreeCalls(t, originatorNode, expectedCalls)
}
func TestRainTreeNetworkCompleteTwoNodes(t *testing.T) {
// val_1
// └───────┐
// val_2
originatorNode := validatorId(1)
// Per the diagram above, in the case of a 2 node network, the originator node (val_1) does a
// single write to another node (val_2), also the
// originator node and never performs any reads or writes during a RainTree broadcast.
expectedCalls := TestNetworkSimulationConfig{
// Attempt: I think Validator 1 is sending a message in a 2 (including self) node network
originatorNode: {0, 1}, // val_1 does a single network write (to val_2)
validatorId(2): {1, 0}, // val_2 does a single network read (from val_1)
}
testRainTreeCalls(t, originatorNode, expectedCalls)
}
func TestRainTreeNetworkCompleteThreeNodes(t *testing.T) {
// val_1
// ┌───────┴────┬─────────┐
// val_2 val_1 val_3
originatorNode := validatorId(1)
expectedCalls := TestNetworkSimulationConfig{
originatorNode: {0, 2}, // val_1 does two network writes (to val_2 and val_3)
validatorId(2): {1, 0}, // val_2 does a single network read (from val_1)
validatorId(3): {1, 0}, // val_2 does a single network read (from val_3)
}
testRainTreeCalls(t, originatorNode, expectedCalls)
}
func TestRainTreeNetworkCompleteFourNodes(t *testing.T) {
// Test configurations (visualization retrieved from simulator)
// val_1
// ┌───────────────┴────┬─────────────────┐
// val_2 val_1 val_3
// └───────┐ └───────┐ └───────┐
// val_3 val_2 val_4
originatorNode := validatorId(1)
expectedCalls := TestNetworkSimulationConfig{
originatorNode: {0, 3}, // val_1 does 3 network writes (two to val_2 and 1 to val_3)
validatorId(2): {2, 1}, // val_2 does 2 network reads (both from val_1) and 1 network write (to val_3)
validatorId(3): {2, 1}, // val_2 does 2 network reads (from val_1 and val_2) and 1 network write (to val_4)
validatorId(4): {1, 0}, // val_2 does 1 network read (from val_3)
}
testRainTreeCalls(t, originatorNode, expectedCalls)
}
func TestRainTreeNetworkCompleteNineNodes(t *testing.T) {
// val_1
// ┌──────────────────────┴────────────┬────────────────────────────────┐
// val_4 val_1 val_7
// ┌───────┴────┬─────────┐ ┌───────┴────┬─────────┐ ┌───────┴────┬─────────┐
// val_6 val_4 val_8 val_3 val_1 val_5 val_9 val_7 val_2
originatorNode := validatorId(1)
expectedCalls := TestNetworkSimulationConfig{
originatorNode: {0, 4},
validatorId(2): {1, 0},
validatorId(3): {1, 0},
validatorId(4): {1, 2},
validatorId(5): {1, 0},
validatorId(6): {1, 0},
validatorId(7): {1, 2},
validatorId(8): {1, 0},
validatorId(9): {1, 0},
}
testRainTreeCalls(t, originatorNode, expectedCalls)
}
// val_1
// ┌────────────────────────────────────────────────────────────────────────┴───────────────────────────────────┬─────────────────────────────────────────────────────────────────────────────────────────────────────────┐
// val_5 val_1 val_9
// ┌────────────────────────┴────────────┬──────────────────────────────────┐ ┌──────────────────────┴────────────┬────────────────────────────────┐ ┌────────────────────────┴──────────────┬──────────────────────────────────┐
// val_7 val_5 val_10 val_3 val_1 val_6 val_11 val_9 val_2
// ┌───────┴────┬──────────┐ ┌───────┴────┬─────────┐ ┌────────┴─────┬──────────┐ ┌───────┴────┬─────────┐ ┌───────┴────┬─────────┐ ┌───────┴────┬─────────┐ ┌────────┴─────┬──────────┐ ┌────────┴────┬──────────┐ ┌───────┴────┬─────────┐
// val_8 val_7 val_10 val_6 val_5 val_8 val_11 val_10 val_5 val_4 val_3 val_6 val_2 val_1 val_4 val_7 val_6 val_1 val_12 val_11 val_2 val_10 val_9 val_12 val_3 val_2 val_9
func TestRainTreeCompleteTwelveNodes(t *testing.T) {
originatorNode := validatorId(1)
expectedCalls := TestNetworkSimulationConfig{
originatorNode: {1, 6},
validatorId(2): {3, 2},
validatorId(3): {2, 2},
validatorId(4): {2, 0},
validatorId(5): {2, 4},
validatorId(6): {3, 2},
validatorId(7): {2, 2},
validatorId(8): {2, 0},
validatorId(9): {2, 4},
validatorId(10): {3, 2},
validatorId(11): {2, 2},
validatorId(12): {2, 0},
}
testRainTreeCalls(t, originatorNode, expectedCalls)
}
func TestRainTreeNetworkCompleteEighteenNodes(t *testing.T) {
// val_1
// ┌──────────────────────────────────────────────────────────────────────────┴─────────────────────────────────────┬─────────────────────────────────────────────────────────────────────────────────────────────────────────┐
// val_7 val_1 val_13
// ┌──────────────────────────┴────────────┬────────────────────────────────────┐ ┌────────────────────────┴────────────┬──────────────────────────────────┐ ┌────────────────────────┴──────────────┬────────────────────────────────────┐
// val_11 val_7 val_15 val_5 val_1 val_9 val_17 val_13 val_3
// ┌────────┴─────┬───────────┐ ┌───────┴────┬──────────┐ ┌────────┴─────┬──────────┐ ┌───────┴────┬──────────┐ ┌───────┴────┬─────────┐ ┌────────┴────┬─────────┐ ┌───────┴─────┬──────────┐ ┌────────┴─────┬───────────┐ ┌───────┴────┬──────────┐
// val_13 val_11 val_16 val_9 val_7 val_12 val_17 val_15 val_8 val_7 val_5 val_10 val_3 val_1 val_6 val_11 val_9 val_2 val_1 val_17 val_4 val_15 val_13 val_18 val_5 val_3 val_14
originatorNode := validatorId(1)
expectedCalls := TestNetworkSimulationConfig{
originatorNode: {1, 6},
validatorId(2): {1, 0},
validatorId(3): {2, 2},
validatorId(4): {1, 0},
validatorId(5): {2, 2},
validatorId(6): {1, 0},
validatorId(7): {2, 4},
validatorId(8): {1, 0},
validatorId(9): {2, 2},
validatorId(10): {1, 0},
validatorId(11): {2, 2},
validatorId(12): {1, 0},
validatorId(13): {2, 4},
validatorId(14): {1, 0},
validatorId(15): {2, 2},
validatorId(16): {1, 0},
validatorId(17): {2, 2},
validatorId(18): {1, 0},
}
testRainTreeCalls(t, originatorNode, expectedCalls)
}
func TestRainTreeNetworkCompleteTwentySevenNodes(t *testing.T) {
// val_1
// ┌────────────────────────────────────────────────────────────────────────────────┴───────────────────────────────────────┬───────────────────────────────────────────────────────────────────────────────────────────────────────────┐
// val_10 val_1 val_19
// ┌──────────────────────────┴──────────────┬──────────────────────────────────────┐ ┌────────────────────────┴────────────┬──────────────────────────────────┐ ┌────────────────────────┴──────────────┬────────────────────────────────────┐
// val_16 val_10 val_22 val_7 val_1 val_13 val_25 val_19 val_4
// ┌────────┴─────┬───────────┐ ┌────────┴─────┬───────────┐ ┌────────┴─────┬───────────┐ ┌────────┴────┬──────────┐ ┌───────┴────┬─────────┐ ┌────────┴─────┬──────────┐ ┌───────┴─────┬──────────┐ ┌────────┴─────┬───────────┐ ┌───────┴────┬──────────┐
// val_20 val_16 val_24 val_14 val_10 val_18 val_26 val_22 val_12 val_11 val_7 val_15 val_5 val_1 val_9 val_17 val_13 val_3 val_2 val_25 val_6 val_23 val_19 val_27 val_8 val_4 val_21
originatorNode := validatorId(1)
expectedCalls := TestNetworkSimulationConfig{
originatorNode: {0, 6},
validatorId(2): {1, 0},
validatorId(3): {1, 0},
validatorId(4): {1, 2},
validatorId(5): {1, 0},
validatorId(6): {1, 0},
validatorId(7): {1, 2},
validatorId(8): {1, 0},
validatorId(9): {1, 0},
validatorId(10): {1, 4},
validatorId(11): {1, 0},
validatorId(12): {1, 0},
validatorId(13): {1, 2},
validatorId(14): {1, 0},
validatorId(15): {1, 0},
validatorId(16): {1, 2},
validatorId(17): {1, 0},
validatorId(18): {1, 0},
validatorId(19): {1, 4},
validatorId(20): {1, 0},
validatorId(21): {1, 0},
validatorId(22): {1, 2},
validatorId(23): {1, 0},
validatorId(24): {1, 0},
validatorId(25): {1, 2},
validatorId(26): {1, 0},
validatorId(27): {1, 0},
}
testRainTreeCalls(t, originatorNode, expectedCalls)
}
// ### RainTree Unit Helpers - To remove redundancy of code in the unit tests ###
// Helper function that can be used for end-to-end P2P module tests that creates a "real" P2P module.
// 1. It creates and configures a "real" P2P module where all the other components of the node are mocked.
// 2. It then triggers a single message and waits for all of the expected messages transmission to complete before announcing failure.
func testRainTreeCalls(t *testing.T, origNode string, networkSimulationConfig TestNetworkSimulationConfig) {
var readWriteWaitGroup sync.WaitGroup
// Configure & prepare test module
numValidators := len(networkSimulationConfig)
runtimeConfigs := createMockRuntimeMgrs(t, numValidators)
genesisMock := runtimeConfigs[0].GetGenesis()
busMocks := createMockBuses(t, runtimeConfigs, &readWriteWaitGroup)
valIds := make([]string, 0, numValidators)
for valId := range networkSimulationConfig {
valIds = append(valIds, valId)
}
sort.Slice(valIds, func(i, j int) bool {
iId := extractNumericId(valIds[i])
jId := extractNumericId(valIds[j])
return iId < jId
})
testutil.PrepareDNSMockFromServiceURLs(t, valIds)
// Create connection and bus mocks along with a shared WaitGroup to track the number of expected
// reads and writes throughout the mocked local network
for i, valId := range valIds {
expectedCall := networkSimulationConfig[valId]
expectedReads := expectedCall.numNetworkReads
expectedWrites := expectedCall.numNetworkWrites
log.Printf("[valId: %s] expected reads: %d\n", valId, expectedReads)
log.Printf("[valId: %s] expected writes: %d\n", valId, expectedWrites)
readWriteWaitGroup.Add(expectedReads)
readWriteWaitGroup.Add(expectedWrites)
persistenceMock := preparePersistenceMock(t, busMocks[i], genesisMock)
consensusMock := prepareConsensusMock(t, busMocks[i])
currentHeightProviderMock := prepareCurrentHeightProviderMock(t, busMocks[i])
busMocks[i].RegisterModule(currentHeightProviderMock)
busMocks[i].EXPECT().
GetCurrentHeightProvider().
Return(currentHeightProviderMock).
AnyTimes()
telemetryMock := prepareTelemetryMock(t, busMocks[i], valId, &readWriteWaitGroup, expectedWrites)
prepareBusMock(busMocks[i], persistenceMock, consensusMock, telemetryMock)
}
libp2pMockNet := mocknet.New()
// Inject the connection and bus mocks into the P2P modules
p2pModules := createP2PModules(t, busMocks, libp2pMockNet)
for _, p2pMod := range p2pModules {
err := p2pMod.Start()
require.NoError(t, err)
}
// Wait for completion
defer waitForNetworkSimulationCompletion(t, &readWriteWaitGroup)
t.Cleanup(func() {
// Stop all p2p modules
for _, p2pMod := range p2pModules {
err := p2pMod.Stop()
require.NoError(t, err)
}
err := libp2pMockNet.Close()
require.NoError(t, err)
})
// Send the first message (by the originator) to trigger a RainTree broadcast
p := &anypb.Any{TypeUrl: "test"}
p2pMod := p2pModules[origNode]
require.NoError(t, p2pMod.Broadcast(p))
}
func extractNumericId(valId string) int64 {
re := regexp.MustCompile(`\d+`)
numStr := re.FindString(valId)
num, err := strconv.ParseInt(numStr, 10, 64)
if err != nil {
return -1
}
return num
}