-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
107 lines (93 loc) · 1.89 KB
/
util.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
package bitbox
import (
"fmt"
"log"
"sync"
"time"
"github.com/btcsuite/btcd/rpcclient"
"github.com/google/uuid"
)
func newRpcClient(host string) (client *rpcclient.Client, err error) {
client, err = rpcclient.New(
&rpcclient.ConnConfig{
Host: host,
User: "test",
Pass: "test",
HTTPPostMode: true,
DisableTLS: true,
}, nil)
return
}
//waitUntilNodeStart TODO set time limit
func waitUntilNodeStart(node Node) (err error) {
for i := 0; ; i++ {
err = node.Client().Ping()
if err != nil {
if i == 40 {
fmt.Println("trying to get info", err)
i = 0
}
time.Sleep(time.Millisecond * 100)
continue
}
break
}
return
}
type createNodeFunc = func(index int, masterNodePort, strIndex string) (Node, error)
func newNodeSet(number int, createNode createNodeFunc) (nodes []Node, err error) {
sessionID := uuid.New().String()
getStrIndex := func(index int) string {
return fmt.Sprintf("%s_%d", sessionID, index)
}
var masterNodePort string
node, err := createNode(0, "", getStrIndex(0))
if err != nil {
return
}
nodes = append(nodes, node)
masterNodePort = node.Info().NodePort
var wg sync.WaitGroup
wg.Add(number - 1)
for i := 1; i < int(number); i++ {
i := i
go func() {
node, err = createNode(i, masterNodePort, getStrIndex(i))
if err != nil {
return
}
nodes = append(nodes, node)
wg.Done()
}()
}
wg.Wait()
return
}
func initBitboxMempool(b Bitbox) (err error) {
err = b.Generate(0, 200)
if err != nil {
return err
}
addr, err := b.Address(0)
if err != nil {
return err
}
//TODO max waiting time
for {
if bal, err := b.Balance(0); err == nil && bal > 101 {
break
}
time.Sleep(time.Millisecond * 200)
}
for i := 0; i < 50; i++ {
_, err := b.Send(0, addr, 2)
if err != nil {
log.Println(err)
}
err = b.Generate(0, 1)
if err != nil {
log.Println(err)
}
}
return
}