-
Notifications
You must be signed in to change notification settings - Fork 10
/
message_relay.go
140 lines (130 loc) · 3.45 KB
/
message_relay.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
// Copyright (c) 2020 The Meter.io developers
// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying
// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html>
package consensus
import (
"fmt"
"math"
)
// Assumptions to use this:
// myIndex is always 0 for proposer (or leader in consensus)
// indexes starts from 0
// 1st layer: 0 (proposer)
// 2nd layer: [1, 2], [3, 4], [5, 6], [7, 8]
// 3rd layer (32 groups): [9..] ...
func GetRelayPeers(myIndex, size int) (peers []int) {
peers = []int{}
if myIndex > size {
fmt.Println("Input wrong!!! myIndex > size")
return
}
replicas := 8
if size <= replicas {
replicas = size
for i := 1; i <= replicas; i++ {
peers = append(peers, (myIndex+i)%size)
}
} else {
replica1 := 8
replica2 := 4
replica3 := 2
limit1 := int(math.Ceil(float64(size)/float64(replica1))) * 2
limit2 := limit1 + int(math.Ceil(float64(size)/float64(replica2)))*4
if myIndex < limit1 {
base := myIndex * replica1
for i := 1; i <= replica1; i++ {
peers = append(peers, (base+i)%size)
}
} else if myIndex >= limit1 && myIndex < limit2 {
base := replica1*limit1 + (myIndex-limit1)*replica2
for i := 1; i <= replica2; i++ {
peers = append(peers, (base+i)%size)
}
} else if myIndex >= limit2 {
base := replica1*limit1 + (limit2-limit1)*replica2 + (myIndex-limit2)*replica3
for i := 1; i <= replica3; i++ {
peers = append(peers, (base+i)%size)
}
}
}
return
// if myIndex == 0 {
// var k int
// if maxIndex >= 8 {
// k = 8
// } else {
// k = maxIndex
// }
// for i := 1; i <= k; i++ {
// peers = append(peers, i)
// }
// return
// }
// if maxIndex <= 8 {
// return //no peer
// }
// var groupSize, groupCount int
// groupSize = ((maxIndex - 8) / 16) + 1
// groupCount = (maxIndex - 8) / groupSize
// // fmt.Println("groupSize", groupSize, "groupCount", groupCount)
// if myIndex <= 8 {
// mySet := (myIndex - 1) / 4
// myRole := (myIndex - 1) % 4
// for i := 0; i < 8; i++ {
// group := (mySet * 8) + i
// if group >= groupCount {
// return
// }
// begin := 9 + (group * groupSize)
// if myRole == 0 {
// peers = append(peers, begin)
// } else {
// end := begin + groupSize - 1
// if end > maxIndex {
// end = maxIndex
// }
// middle := (begin + end) / 2
// peers = append(peers, middle)
// }
// }
// } else {
// // I am in group, so begin << myIndex << end
// // if wrap happens, redundant the 2nd layer
// group := (maxIndex - 8) / 16
// begin := 9 + (group * groupSize)
// end := begin + groupSize - 1
// if end > maxIndex {
// end = maxIndex
// }
// var peerIndex int
// var wrap bool = false
// if myIndex == end && end != begin {
// peers = append(peers, begin)
// }
// if peerIndex = myIndex + 1; peerIndex <= maxIndex {
// peers = append(peers, peerIndex)
// } else {
// wrap = true
// }
// if peerIndex = myIndex + 2; peerIndex <= maxIndex {
// peers = append(peers, peerIndex)
// } else {
// wrap = true
// }
// if peerIndex = myIndex + 4; peerIndex <= maxIndex {
// peers = append(peers, peerIndex)
// } else {
// wrap = true
// }
// if peerIndex = myIndex + 8; peerIndex <= maxIndex {
// peers = append(peers, peerIndex)
// } else {
// wrap = true
// }
// if wrap == true {
// peers = append(peers, (myIndex%8)+1)
// peers = append(peers, (myIndex%8)+1+8)
// }
// }
// return
}