forked from hiero-ledger/hiero-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mirror_network.go
57 lines (48 loc) · 1.18 KB
/
mirror_network.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
package hedera
import (
"math/rand"
)
type mirrorNetwork struct {
networkNodes map[string]*mirrorNode
network []string
index uint
}
func newMirrorNetwork() *mirrorNetwork {
return &mirrorNetwork{
networkNodes: make(map[string]*mirrorNode),
network: make([]string, 0),
index: 0,
}
}
func contains(arr []string, str string) bool {
for _, a := range arr {
if a == str {
return true
}
}
return false
}
func (network *mirrorNetwork) setNetwork(newNetwork []string) {
for _, n := range network.network {
if !contains(newNetwork, n) {
delete(network.networkNodes, n)
}
}
for _, url := range newNetwork {
if !contains(network.network, url) {
network.network = append(network.network, url)
network.networkNodes[url] = newMirrorNode(url)
}
}
network.index = 0
if len(network.network) > 0 {
rand.Shuffle(len(network.network), func(i, j int) {
network.network[i], network.network[j] = network.network[j], network.network[i]
})
}
}
func (network *mirrorNetwork) getNextMirrorNode() *mirrorNode {
node := network.networkNodes[network.network[network.index]]
network.index = (network.index + 1) % uint(len(network.network))
return node
}