-
Notifications
You must be signed in to change notification settings - Fork 34
/
main_test.go
761 lines (643 loc) · 22.3 KB
/
main_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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"math"
"math/rand"
"net/http"
"net/url"
"os"
"testing"
"github.com/go-playground/validator/v10"
"github.com/rumsystem/quorum/internal/pkg/logging"
"github.com/rumsystem/quorum/internal/pkg/utils"
"github.com/rumsystem/quorum/pkg/chainapi/api"
"github.com/rumsystem/quorum/pkg/pb"
"github.com/rumsystem/quorum/testnode"
"google.golang.org/protobuf/encoding/protojson"
"time"
)
/*
To Verify :
Node create a group and leave it, should be able to rejoin and group
*/
type RespError struct {
Error string `json:"error"`
}
// temp solution
const (
BOOTSTRAP = "bootstrap"
OWNER_NODE = "fullnode_0"
USER_NODE = "fullnode_1"
PRODUCER_NODE1 = "producernode_0"
PRODUCER_NODE2 = "producernode_1"
)
var (
pidlist []int
bootstrapNode *testnode.NodeInfo
nodes map[string]*testnode.NodeInfo
fullnodes, producernodes, groups, posts, randRangeMin, randRangeMax int
logger = logging.Logger("main_test")
)
func TestMain(m *testing.M) {
logging.SetLogLevel("main_test", "debug")
producerNodesPtr := flag.Int("bpnodes", 2, "mock producernodes")
fullnodesPtr := flag.Int("fullnodes", 2, "mock fullnodes")
groupsPtr := flag.Int("groups", 1, "groups on owner node")
postsPtr := flag.Int("posts", 100, "posts to group")
rextestmode := flag.Bool("rextest", false, "RumExchange Test Mode")
randMin := flag.Int("rmin", 10, "post rand min value")
randMax := flag.Int("rmax", 200, "post rand max value")
flag.Parse()
fullnodes = *fullnodesPtr
producernodes = *producerNodesPtr
groups = *groupsPtr
posts = *postsPtr
randRangeMin = *randMin
randRangeMax = *randMax
if randRangeMax-randRangeMin < 100 {
logger.Error("different between randMin / randMax should larger than 100(ms)")
return
}
logger.Debugf("Setup test env")
logger.Debugf(">>> full nodes: <%d>", fullnodes)
logger.Debugf(">>> producer nodes: <%d>", producernodes)
logger.Debugf(">>> group <%d>", groups)
pidch := make(chan int)
go func() {
for {
select {
case pid := <-pidch:
logger.Debug("receive pid", pid)
pidlist = append(pidlist, pid)
if len(pidlist) == len(nodes) {
logger.Debugf("All done...")
return
}
}
}
}()
cliargs := testnode.Nodecliargs{Rextest: *rextestmode}
var tempdatadir string
//bootstrapapi, peerapilist, tempdatadir, _ = testnode.RunNodesWithBootstrap(context.Background(), cliargs, pidch, fullnodes, bpnodes)
nodelist, tempdatadir, _ := testnode.RunNodesWithBootstrap(context.Background(), cliargs, pidch, fullnodes, producernodes)
nodes = make(map[string]*testnode.NodeInfo)
//transfer list to map
for _, node := range nodelist {
if node.NodeName == BOOTSTRAP {
bootstrapNode = node
} else {
nodes[node.NodeName] = node
}
}
exitVal := m.Run()
logger.Debug("all tests finished, clean up:", tempdatadir)
testnode.Cleanup(tempdatadir, nodelist)
os.Exit(exitVal)
}
func TestJoinGroup(t *testing.T) {
logger.Debugf("_____________TestJoinGroup_RUNNING_____________")
//get owner node
ownerNode := nodes[OWNER_NODE]
//create 3 group on fullnode, join the group then leave, repeat 3 times and verify the group exist and in "IDLE" status
groupToCreate := 3
count := 0
for count < 3 {
for i := 0; i < groupToCreate; i++ {
logger.Debugf("_____________CREATE_GROUP_____________")
var groupseed string
var groupId string
groupName := fmt.Sprintf("testgroup_%d", i+1)
status, resp, err := testnode.RequestAPI(ownerNode.APIBaseUrl, "/api/v1/group", "POST", fmt.Sprintf(`{"group_name":"%s","app_key":"default", "consensus_type":"poa","encryption_type":"public"}`, groupName))
if err == nil || status != 200 {
var objmap map[string]interface{}
if err := json.Unmarshal(resp, &objmap); err != nil {
t.Errorf("Data Unmarshal error %s", err)
} else {
groupseed = string(resp)
seedurl := objmap["seed"]
groupId = testnode.SeedUrlToGroupId(seedurl.(string))
logger.Debugf("group {Name <%s>, GroupId<%s>} created on node <%s>", groupName, groupId, ownerNode.NodeName)
}
} else {
t.Errorf("create group on peer%d error %s", 1, err)
}
time.Sleep(1 * time.Second)
// try join the same group just created
logger.Debugf("_____________TEST_JOIN_EXIST_GROUP_____________")
status, _, err = testnode.RequestAPI(ownerNode.APIBaseUrl, "/api/v2/group/join", "POST", groupseed)
//check if failed
if status != 400 {
t.Errorf("Join existed group test failed with err %s", err.Error())
}
time.Sleep(1 * time.Second)
logger.Debugf("_____________TEST_LEAVE_GROUP_____________")
status, _, _ = testnode.RequestAPI(ownerNode.APIBaseUrl, "/api/v1/group/leave", "POST", fmt.Sprintf(`{"group_id":"%s"}`, groupId))
if status != 200 {
t.Errorf("Leave group test failed with response code %d", status)
}
time.Sleep(1 * time.Second)
logger.Debugf("_____________TEST_JOIN_LEAVED_GROUP_____________")
status, _, _ = testnode.RequestAPI(ownerNode.APIBaseUrl, "/api/v2/group/join", "POST", groupseed)
if status != 200 {
t.Errorf("join leaved group test failed with response code %d", status)
}
// verify group status
_, resp, err = testnode.RequestAPI(ownerNode.APIBaseUrl, "/api/v1/groups", "GET", "")
if err != nil {
t.Errorf("get peer group error %s", err)
}
groupslist := &api.GroupInfoList{}
if err := json.Unmarshal(resp, &groupslist); err != nil {
t.Errorf("parse peer group error %s", err)
}
//check group number,
if len(groupslist.GroupInfos) != 1 {
t.Errorf("Group number check failed, have %d groups, except 1", len(groupslist.GroupInfos))
}
//ready := "IDLE"
for _, groupinfo := range groupslist.GroupInfos {
logger.Debugf("Group %s status %s", groupinfo.GroupId, groupinfo.RexSyncerStatus)
if groupinfo.GroupId != groupId {
t.Errorf("Check group status failed %s, groupId mismatch", err)
}
//No need to check IDLE status in this test case
//if groupinfo.GroupStatus != ready {
// t.Errorf("Check group status failed %s, group not IDLE", err)
//}
}
logger.Debugf("_____________TEST_LEAVE_GROUP_____________")
//leave group
status, resp, err = testnode.RequestAPI(ownerNode.APIBaseUrl, "/api/v1/group/leave", "POST", fmt.Sprintf(`{"group_id":"%s"}`, groupId))
if status != 200 {
if err != nil {
t.Errorf("Leave group test failed with response code %d, resp <%s>, err <%s>", status, string(resp), err.Error())
} else {
t.Errorf("leave group test failed with response code %d, resp <%s>", status, string(resp))
}
}
logger.Debugf("_____________TEST_CLEAR_GROUP_____________")
//clear group data
status, resp, err = testnode.RequestAPI(ownerNode.APIBaseUrl, "/api/v1/group/clear", "POST", fmt.Sprintf(`{"group_id":"%s"}`, groupId))
if status != 200 {
if err != nil {
t.Errorf("clean group test failed with response code %d, resp <%s>, err <%s>", status, string(resp), err.Error())
} else {
t.Errorf("clean group test failed with response code %d, resp <%s>", status, string(resp))
}
}
}
time.Sleep(1 * time.Second)
count++
}
}
/*
How to test:
1. create 1 owner node
2. create 1 user node
3. create 2 producer nodes
4. create 1 group on owner node
5. all other nodes join the group
6. owner node and user node send POST TRX in time gap following normal distribution(nora)
7. verify owner node and user node has the same trx_id list at the end of test (since producer node DOES NOT apply POST trx)
8. clean up
*/
func TestGroupPostContents(t *testing.T) {
logger.Debugf("_____________TestGroupPostContents_RUNNING_____________")
logger.Debugf("_____________CREATE_GROUP_____________")
var groupseed string
var groupId string
//owner create a group
//get owner node
ownerNode := nodes[OWNER_NODE]
userNode := nodes[USER_NODE]
groupName := "testgroup"
groupseed, groupId, err := CreateGroup(ownerNode, groupName)
if err != nil {
t.Errorf("create group on owner node failed with error <%s>", err)
t.Fail()
}
logger.Debugf("OK: group {Name <%s>, GroupId<%s>} created on node <%s>", groupName, groupId, ownerNode.NodeName)
time.Sleep(1 * time.Second)
logger.Debugf("____________JOIN_GROUP_____________")
for _, node := range nodes {
if node.NodeName == OWNER_NODE {
//skip owner node
continue
} else {
if err = JoinGroup(node, groupseed, groupId); err != nil {
logger.Warningf("node <%s> join group failed with error <%s>", node.NodeName, groupId, err)
t.Fail()
} else {
logger.Debugf("OK: node <%s> join group <%s> done", node.NodeName, groupId)
}
time.Sleep(1 * time.Second)
}
}
logger.Debugf("____________START_POST_____________")
//send trxs randomly
logger.Debugf("Try post <%d> trxs", posts)
trxs := make(map[string]string) //trx_id: trx_content
i := 0
for i < posts {
var postContent string
var resp []byte
var err error
r := GetGussRandNum(int64(randRangeMin), int64(randRangeMax)) // from 10ms (0.01s) to 500ms (1s)
if r%2 == 0 {
logger.Debugf("owner node try post trx")
postContent = fmt.Sprintf(`{"data": {"type":"Add","object":{"type":"Note","content":"post_content_from_%s_%d","name":"%s_%d"},"target":{"id":"%s","type":"Group"}}}`, OWNER_NODE, i, OWNER_NODE, i, groupId)
_, resp, err = testnode.RequestAPI(ownerNode.APIBaseUrl, fmt.Sprintf("/api/v1/group/%s/content", groupId), "POST", postContent)
} else {
logger.Debugf("user node try post trx")
postContent = fmt.Sprintf(`{"data": {"type":"Add","object":{"type":"Note","content":"post_content_from_%s_%d","name":"%s_%d"},"target":{"id":"%s","type":"Group"}}}`, USER_NODE, i, USER_NODE, i, groupId)
_, resp, err = testnode.RequestAPI(userNode.APIBaseUrl, fmt.Sprintf("/api/v1/group/%s/content", groupId), "POST", postContent)
}
if err != nil {
logger.Errorf("post content to api error %s", err)
t.Fail()
}
var objmap map[string]interface{}
if err = json.Unmarshal(resp, &objmap); err != nil {
// store trx id, verify it later on each group
logger.Errorf("Data Unmarshal error %s", err)
t.Fail()
}
if objmap["trx_id"] != nil {
logger.Debugf("OK: post with trxid: <%s>", objmap["trx_id"].(string))
trxs[objmap["trx_id"].(string)] = "posted"
} else {
logger.Errorf("resp body was not included trx_id %s", string(resp))
t.Fail()
}
logger.Debugf("sleep %d ms ", r)
time.Sleep(time.Duration(r) * time.Millisecond)
i++
}
//sleep 5 seconds to make sure all node received latest block
time.Sleep(5 * time.Second)
logger.Debugf("____________VERIFY_TRX_____________")
//verify all nodes (except producer1 and producer2) have all trxs sent by owner and user
time.Sleep(15 * time.Second)
for _, node := range nodes {
if node.NodeName == PRODUCER_NODE1 || node.NodeName == PRODUCER_NODE2 {
continue
}
if err = VerifyTrxOnGroup(node, groupId, trxs); err != nil {
logger.Errorf("VerifyTrxOnGroup failed: %s", err)
t.Fail()
}
}
}
/*
How to test
1. create 1 group on owner node
2. owner post 100 trxs
3. user1 join group
4. wait 10s for user1 to finish sync (should be quite enough)
5. user1 send a POST trx
6. verify POST send successful
*/
func TestBasicSync(t *testing.T) {
logger.Debugf("_____________TestBasicSync_RUNNING_____________")
//owner create a group
//get owner node
ownerNode := nodes[OWNER_NODE]
userNode := nodes[USER_NODE]
groupName := "testgroup"
logger.Debugf("_____________CREATE_GROUP_____________")
groupSeed, groupId, err := CreateGroup(ownerNode, groupName)
if err != nil {
t.Fail()
}
logger.Debugf("_____________OWNER_POST_TO_GROUP_____________")
trxs, err := PostToGroup(ownerNode, groupId, 100)
if err != nil {
t.Fail()
}
time.Sleep(time.Second * 15)
logger.Debugf("_____________OWNER_VERIFY_TRXS_____________")
err = VerifyTrxOnGroup(ownerNode, groupId, trxs)
if err != nil {
t.Fail()
}
logger.Debugf("_____________USER_JOIN_GROUP_____________")
err = JoinGroup(userNode, groupSeed, groupId)
if err != nil {
t.Fail()
}
logger.Debugf("_____________WAIT_USERNODE_SYNC_DONE_____________")
//wait 20s for user node to finish sync
time.Sleep(time.Duration(20) * time.Second)
//check user node get all trxs
err = VerifyTrxOnGroup(userNode, groupId, trxs)
if err != nil {
t.Fail()
}
logger.Debugf("_____________LEAVE_AND_CLEAR_____________")
err = LeaveGroup(userNode, groupId)
if err != nil {
logger.Errorf("leave group failed: %s", err)
t.Fail()
}
err = ClearGroup(userNode, groupId)
if err != nil {
logger.Errorf("clear group failed: %s", err)
t.Fail()
}
err = LeaveGroup(ownerNode, groupId)
if err != nil {
logger.Errorf("leave group failed: %s", err)
t.Fail()
}
err = ClearGroup(ownerNode, groupId)
if err != nil {
logger.Errorf("clear group failed: %s", err)
t.Fail()
}
}
/*
func TestBasicMultiProducerBft(t *testing.T) {
logger.Debugf("_____________TestBasicMultiProducerBft_RUNNING_____________")
//owner create a group
//get owner node
ownerNode := nodes[OWNER_NODE]
userNode := nodes[USER_NODE]
p1node := nodes[PRODUCER_NODE1]
p2node := nodes[PRODUCER_NODE2]
groupName := "testgroup"
groupSeed, groupId, err := CreateGroup(ownerNode, groupName)
if err != nil {
t.Fail()
}
trxs, err := PostToGroup(ownerNode, groupId, 100)
if err != nil {
t.Fail()
}
err = VerifyTrxOnGroup(ownerNode, groupId, trxs)
if err != nil {
t.Fail()
}
err = JoinGroup(userNode, groupSeed, groupId)
if err != nil {
t.Fail()
}
err = JoinGroup(p1node, groupSeed, groupId)
if err != nil {
t.Fail()
}
err = JoinGroup(p2node, groupSeed, groupId)
if err != nil {
t.Fail()
}
//wait 20s for all node to finish sync
time.Sleep(time.Duration(20) * time.Second)
epochOwner, err := GetEpochOnGroup(ownerNode, groupId)
if err != nil {
t.Fail()
}
epochUser, err := GetEpochOnGroup(userNode, groupId)
if err != nil {
t.Fail()
}
epochp1, err := GetEpochOnGroup(p1node, groupId)
if err != nil {
t.Fail()
}
epochp2, err := GetEpochOnGroup(p2node, groupId)
if err != nil {
t.Fail()
}
logger.Debugf("OK: ownernode epoch <%d>, usernode epoch <%d>, p1 epoch <%d>, p2 epoch <%d>",
epochOwner, epochUser, epochp1, epochp2)
if epochUser != epochOwner || epochp1 != epochOwner || epochp2 != epochOwner {
logger.Errorf("User node check epoch failed, highest epoch <%d>, should be <%d>", epochUser, epochOwner)
t.Fail()
}
logger.Debugf("OK: ownernode epoch <%d>, p2 epoch <%d>", epochOwner, epochp2)
logger.Debugf("_____________LEAVE_AND_CLEAR_____________")
err = LeaveGroup(userNode, groupId)
if err != nil {
t.Fail()
}
err = ClearGroup(userNode, groupId)
if err != nil {
t.Fail()
}
err = LeaveGroup(ownerNode, groupId)
if err != nil {
t.Fail()
}
err = ClearGroup(ownerNode, groupId)
if err != nil {
t.Fail()
}
err = LeaveGroup(p1node, groupId)
if err != nil {
t.Fail()
}
err = ClearGroup(p1node, groupId)
if err != nil {
t.Fail()
}
err = LeaveGroup(p2node, groupId)
if err != nil {
t.Fail()
}
err = ClearGroup(p2node, groupId)
if err != nil {
t.Fail()
}
}
*/
func CreateGroup(node *testnode.NodeInfo, groupName string) (groupseed, groupId string, err error) {
logger.Debugf("node <%s> try create group with name <%s>", node.NodeName, groupName)
status, resp, err := testnode.RequestAPI(node.APIBaseUrl, "/api/v1/group", "POST", fmt.Sprintf(`{"group_name":"%s","app_key":"default", "consensus_type":"poa","encryption_type":"public"}`, groupName))
if err == nil || status != 200 {
var objmap map[string]interface{}
if err := json.Unmarshal(resp, &objmap); err != nil {
logger.Errorf("Data Unmarshal error %s", err)
return "", "", err
} else {
groupseed = string(resp)
seedurl := objmap["seed"]
groupId = testnode.SeedUrlToGroupId(seedurl.(string))
logger.Debugf("OK: group {Name <%s>, GroupId<%s>} created on node <%s>", groupName, groupId, node.NodeName)
}
} else {
logger.Errorf("create group on owner node failed with error <%s>", err)
return "", "", err
}
return groupseed, groupId, nil
}
func JoinGroup(node *testnode.NodeInfo, seed string, groupId string) error {
logger.Debugf("node <%s> try join group with groupId <%s>", node.NodeName, groupId)
_, _, err := testnode.RequestAPI(node.APIBaseUrl, "/api/v2/group/join", "POST", seed)
if err != nil {
logger.Warningf("node <%s> join group failed with error <%s>", node.NodeName, groupId, err)
return err
} else {
logger.Debugf("OK: node <%s> join group <%s> done", node.NodeName, groupId)
return nil
}
}
func PostToGroup(node *testnode.NodeInfo, groupId string, trxNum int) (map[string]string, error) {
logger.Debugf("node <%s> try send <%d> trxs to group with groupId <%s>", node.NodeName, trxNum, groupId)
trxs := make(map[string]string) //trx_id: trx_content
i := 0
for i < trxNum {
var postContent string
var resp []byte
var err error
r := GetGussRandNum(int64(randRangeMin), int64(randRangeMax)) // from 10ms (0.01s) to 500ms (1s)
logger.Debugf("node <%s> try post trx", node.NodeName)
postContent = fmt.Sprintf(`{"data": {"type":"Add","object":{"type":"Note","content":"post_content_from_%s_%d","name":"%s_%d"},"target":{"id":"%s","type":"Group"}}}`,
node.NodeName, i,
node.NodeName, i,
groupId)
_, resp, err = testnode.RequestAPI(node.APIBaseUrl, fmt.Sprintf("/api/v1/group/%s/content", groupId), "POST", postContent)
if err != nil {
logger.Errorf("post content to api error %s", err)
return nil, err
}
var objmap map[string]interface{}
if err = json.Unmarshal(resp, &objmap); err != nil {
// store trx id, verify it later on each group
logger.Errorf("Data Unmarshal error %s", err)
return nil, err
}
if objmap["trx_id"] != nil {
logger.Debugf("OK: post with trxid: <%s>", objmap["trx_id"].(string))
trxs[objmap["trx_id"].(string)] = "posted"
} else {
logger.Errorf("resp body was not included trx_id %s", string(resp))
err = fmt.Errorf("resp error")
return nil, err
}
time.Sleep(time.Duration(r) * time.Millisecond)
i++
}
return trxs, nil
}
func requestAPI(baseUrl string, endpoint string, method string, payload interface{}, headers http.Header, result interface{}, isSkipValidate bool) (int, []byte, error) {
_url, err := url.JoinPath(baseUrl, endpoint)
if err != nil {
return 0, nil, fmt.Errorf("url.JoinPath(%s, %s) failed: %s", baseUrl, endpoint, err)
}
statusCode, resp, err := utils.RequestAPI(_url, method, payload, headers, result)
if err != nil || statusCode >= 400 {
e := fmt.Errorf("%s %s failed: %s, payload: %+v, response: %s", method, endpoint, err, payload, resp)
logger.Error(e)
return statusCode, resp, e
}
if result != nil {
if err := json.Unmarshal(resp, result); err != nil {
logger.Errorf("json.Unmarshal %+v failed: %s", resp, err)
return statusCode, resp, err
}
}
if !isSkipValidate {
validate := validator.New()
if err := validate.Struct(result); err != nil {
e := fmt.Errorf("validate.Struct failed: %s, response: %s", err, resp)
logger.Error(e)
return statusCode, resp, e
}
}
return statusCode, resp, nil
}
func verifyTrx(node *testnode.NodeInfo, groupId string, trxId string) bool {
max := 20
for i := 0; i < max; i++ {
path := fmt.Sprintf("/api/v1/trx/%s/%s", groupId, trxId)
_, resp, err := requestAPI(node.APIBaseUrl, path, "GET", nil, nil, nil, true)
if err != nil {
logger.Errorf("curl -X GET %s/%s failed: %s", node.APIBaseUrl, path, err)
return false
}
var result pb.Trx
if err := protojson.Unmarshal(resp, &result); err != nil {
logger.Errorf("protojson.Unmarshal failed: %s", err)
return false
}
if result.TrxId == trxId {
return true
} else {
logger.Warnf("idx: %d trxId except: %s actual: %s", i, trxId, result.TrxId)
time.Sleep(1 * time.Second)
continue
}
}
return false
}
func VerifyTrxOnGroup(node *testnode.NodeInfo, groupId string, trxs map[string]string) error {
for trxId, _ := range trxs {
logger.Debugf("check trx <%s>", trxId)
if verifyTrx(node, groupId, trxId) {
logger.Debugf("Ok: ---- node <%s> trx <%s> verified", node.NodeName, trxId)
} else {
err := fmt.Errorf("node <%s> verify trx <%s> failed", node.NodeName, trxId)
logger.Error(err)
return err
}
}
return nil
}
func GetGroupStatus(node *testnode.NodeInfo, groupId string) (string, error) {
logger.Debugf("node <%s> get groupstatus on group with groupId <%s>", node.NodeName, groupId)
_, resp, err := testnode.RequestAPI(node.APIBaseUrl, "/api/v1/groups", "GET", "")
if err != nil {
logger.Errorf("node <%s> get group info failed with error <%s>", node.NodeName, err.Error())
return "", err
}
groupslist := &api.GroupInfoList{}
if err := json.Unmarshal(resp, &groupslist); err != nil {
logger.Errorf("parse peer group error %s", err)
return "", err
}
groupInfo := groupslist.GroupInfos[0]
return groupInfo.RexSyncerStatus, nil
}
func LeaveGroup(node *testnode.NodeInfo, groupId string) error {
//leave group
status, resp, err := testnode.RequestAPI(node.APIBaseUrl, "/api/v1/group/leave", "POST", fmt.Sprintf(`{"group_id":"%s"}`, groupId))
if err != nil {
logger.Errorf("Leave group test failed with response code %d, resp <%s>, err <%s>", status, string(resp), err.Error())
return err
} else if status != 200 {
err = fmt.Errorf("leave group test failed with response code %d, resp <%s>", status, string(resp))
logger.Errorf(err.Error())
return err
} else {
logger.Debugf("OK: node <%s> leave group <%s>", node.NodeName, groupId)
}
return nil
}
func ClearGroup(node *testnode.NodeInfo, groupId string) error {
//clean group data
status, resp, err := testnode.RequestAPI(node.APIBaseUrl, "/api/v1/group/clear", "POST", fmt.Sprintf(`{"group_id":"%s"}`, groupId))
if err != nil {
logger.Errorf("clean group test failed with response code %d, resp <%s>, err <%s>", status, string(resp), err.Error())
return err
} else if status != 200 {
err = fmt.Errorf("clean group test failed with response code %d, resp <%s>", status, string(resp))
logger.Errorf(err.Error())
return err
} else {
logger.Debugf("OK : node <%s> clear group date <%s>", node.NodeName, groupId)
}
return nil
}
// Box muller
func GetGussRandNum(min, max int64) int64 {
sigma := (float64(min) + float64(max)) / 2
miu := (float64(max) - sigma) / 3
rand.Seed(time.Now().UnixNano())
x := rand.Float64()
x1 := rand.Float64()
a := math.Cos(2*math.Pi*x) * math.Sqrt((-2)*math.Log(x1))
result := a*miu + sigma
return int64(result)
}